Skip to content
dbcccccccPublic

About

The AI model transforming formula image into Typst code.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

TypLens V2-Pro

TypLens V2-Pro converts an image of a printed mathematical formula — including wide single-line equations, matrices, piecewise cases(...) definitions, and multi-line & = derivations — directly into clean, idiomatic Typst math source. Its 121.5M-parameter model runs completely locally in a browser using 84.9 MB INT4 (WebGPU) or 130.7 MB INT8 (WASM) ONNX graphs. No LaTeX intermediate step, API key, upload, or server-side inference is required.

Repository name: TypLens. Model version: V2-Pro (v2.0). Release tag: TypLens-V2.

Hugging Face model repository · GitHub repository

Run the V2-Pro browser example

The inference-only browser example includes exact v2-gray-naflex-1024 preprocessing, 2D antialiased bilinear position embedding interpolation, KV-cached ONNX inference (WebGPU INT4 + multi-threaded WASM INT8), conservative Typst syntax checks, and editable output. It contains no training code or training data.

Ready-to-run download: typlens-v2-browser.zip. Extract it, install Node.js 22.13 or newer, run node serve.mjs in the extracted folder, and open http://127.0.0.1:4173/. Both default variants (int4 + q8) and ONNX Runtime Web 1.22.0 are bundled; no npm install or download is needed.

From source:

git clone https://github.com/dbccccccc/TypLens.git
cd TypLens/browser
npm ci
npm start

Browsers with WebGPU automatically select Compact INT4 (84.9 MB, WebGPU) on first load; browsers without WebGPU automatically select Balanced INT8 (130.7 MB, 4-thread WASM). Images never leave your browser tab.

What changed from V1.1

TypLens V2-Pro is a ground-up architectural, data, and runtime upgrade:

  • Aspect-preserving NaFlex vision backbone (google/siglip2-base-patch16-naflex): Replaces V1.1's fixed 384×384 square DeiT/TrOCR encoder (which squashed wide formulas) with a 12-layer SigLIP 2 NaFlex encoder folded mathematically from RGB into a single grayscale channel (max_err = 5.1e-7). Formulas keep their natural aspect ratio across dynamic 16×16 patch grids (up to 1,024 patches, up to 2,048 px per side).
  • From-scratch 8-layer modern Typst decoder: 512-dim decoder with RoPE, GQA (8Q / 4KV), SwiGLU, and RMSNorm (halving KV-cache footprint relative to standard MHA), paired with a 4,096 math-aware byte-level BPE vocabulary that cuts token count nearly in half compared to V1.1's 1,199-token vocab.
  • 974k clean, compiler-verified native Typst training corpus: Built from OleehyO/latex-formulas (ArXiv printed), linxy/LaTeX_OCR (printed split only), and 260k balanced native Typst multi-line / calculus / accent / matrix synthetic formulas. Every label was canonicalized with v2-typst-canonical-v2 (eliminating MiTeX mat(delim: #none, zws, ...) and escaped \( artifacts) and verified to compile cleanly with Typst 0.15.
  • WebGPU-safe 2-graph KV-cache ONNX + 4-bit MatMulNBits: Hierarchical 4+4 KV-cache stacking keeps every Concat node within the universal 8-storage-buffer WebGPU minimum, enabling 84.9 MB INT4 browser GPU inference alongside 130.7 MB INT8 multi-threaded WASM.

Migration from V1.1: V2-Pro uses dynamic NaFlex patches ([1, N, 256] + pos_embeddings [1, N, 768]) and a 4,096-token vocabulary. Replace preprocessing, tokenizer bytes, position grid, and both ONNX graphs together. See INFERENCE.md for the complete specification.

Files and sizes

Format Files Weight bytes Size (decimal MB) Recommended runtime
Native FP32 model.safetensors 486,064,528 486.1 PyTorch / research
Browser Compact INT4 onnx/int4/encoder.onnx + decoder.onnx 84,940,876 84.9 Browser WebGPU (auto-default with WebGPU)
Browser Balanced INT8 onnx/int8/encoder.onnx + decoder.onnx 130,656,170 130.7 Browser WASM (4 threads) / Desktop CPU
Browser Full FP32 onnx/fp32/encoder.onnx + decoder.onnx 486,807,706 486.8 Unquantized reference
Shared metadata token-bytes.json + encoder_pos_grid.f32 846,115 0.85 Required by all variants

See release-manifest.json for exact SHA-256 digests.

Benchmark summary

1. Independent zero-leakage test set (frozen_test_phase2, 450 formulas, 8 suites)

Evaluated with greedy decoding, strict Typst 0.15 compilation, and rendered visual CDM F1:

Metric V2-Pro (best-model, FP32)
First-try Typst compile rate 99.33% (447 / 450)
Mean visual CDM F1 0.9699
Visual exact match (cdm_exact) 82.22% (pixel-exact 77.78%)
Token accuracy 99.11%
Long formulas (257–512 tokens, n=31) 100.0% compile · 0.9986 CDM F1
Ultra-long formulas (513+ tokens, n=12) 100.0% compile · 0.9840 CDM F1

2. Unseen out-of-distribution head-to-head: V1.1 vs V2-Pro (12 fresh L1–L5 screenshots)

Includes Microsoft Word Cambria Math font, dark-mode screenshots, JPEG-compressed warm PDF crops, 1329 px wide equations, 3-line Navier-Stokes PDEs, and a 4-line SGD convergence proof (none in training or test sets):

Model Size Compile Visual Exact Mean CDM F1 Mean Soft IoU
TypLens V1.1 INT8 33.1 MB 12/12 2/12 0.6258 0.3513
TypLens V1.1 FP32 117.2 MB 12/12 2/12 0.6437 0.3691
TypLens V2-Pro INT8 (q8) 130.7 MB 11/12 4/12 0.7976 0.5020
TypLens V2-Pro INT4 (int4) 84.9 MB 12/12 5/12 0.8367 0.5590
TypLens V2-Pro FP32 486.8 MB 12/12 5/12 0.8427 0.5990

See MODEL_CARD.md and evaluation-summary.json for full per-suite metrics, browser Chromium timings, and known limitations.

License and provenance

  • Project-authored decoder architecture, browser inference code, preprocessing, and example images: MIT License (LICENSE).
  • Upstream vision encoder initialization: Google siglip2-base-patch16-naflex under Apache License 2.0 (licenses/SIGLIP2-APACHE-2.0.txt).
  • Full upstream and training-data attribution (datasets are not redistributed): see NOTICE.md.

About

The AI model transforming formula image into Typst code.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages