Model Card β TypLens V2-Pro
For a runnable local browser application, see browser/README.md. The public repository contains inference code, model specifications, and license notices only; training scripts and datasets are not redistributed.
TypLens V2-Pro converts a cropped image of a printed mathematical formula β from
short inline expressions to wide equations, matrices, piecewise cases(...),
and multi-line & = derivations β directly into clean, idiomatic Typst
math source. Its 121,508,096-parameter architecture runs locally in desktop
browsers via 84.9 MB INT4 (WebGPU) or 130.7 MB INT8 (WASM) ONNX graphs.
Repository name: TypLens. Model version: V2-Pro (v2.0). Release tag: TypLens-V2.
Hugging Face model repository Β· GitHub repository
Model architecture
| Component | Specification |
|---|---|
| Vision encoder | 12-layer SigLIP 2 NaFlex ViT (google/siglip2-base-patch16-naflex), hidden size 768, 12 heads, 16Γ16 patches, 16Γ16 base 2D position grid interpolated via antialiased bilinear (align_corners=False) |
| Single-channel grayscale fold | RGB patch projection kernels summed analytically into 1 luminance channel prior to training (verified exact on grayscale inputs, max_err = 5.07e-7) |
| Vision-to-decoder bridge | Linear(768 β 512, bias=False) + RMSNorm(512) |
| Autoregressive decoder | 8-layer custom Transformer (d=512), RoPE (theta=10000), GQA (8 query heads / 4 KV heads, head_dim=64), SwiGLU (ffn=1536), RMSNorm, untied output projection (lm_head) |
| Vocabulary | 4,096 math-aware byte-level BPE (5 special tokens + 256 lossless raw byte fallbacks + seeded Typst math primitives + corpus BPE merges) |
| Context limits | Up to 1,024 vision patches (max_grid_dim=128, up to 2,048 px per axis) and 2,048 decoder positions (trained up to 1,536 target tokens; default generation budget 1,024 tokens) |
| Total parameters | 121,508,096 (85.4M encoder + 0.4M bridge + 35.7M decoder) |
Training summary
- Corpus (
974,001training pairs, printed math only, zero handwriting):OleehyO/latex-formulas(cleaned_formulasArXiv printed):479,251unique compiler-verified formulas (2Γupweighting on long/multi-line formulas β628,355train pairs).linxy/LaTeX_OCR(fullprinted split only):77,371unique compiler-verified formulas (86,046train pairs).- Balanced 4-family native Typst synthetic generator:
260,000formulas generated (259,600train pairs,250val,150frozen test;35%multi-line& =/& <=derivations,30%nested calculus/fractions,20%dense accents/scripts,15%matrices &cases(...)) rendered at varied font sizes and DPIs.
- Canonicalization & compiler filtering (
v2-typst-canonical-v2):- LaTeX formulas converted via Rust
tex2typst, normalized to idiomatic Typst (mat(delim: #none, zws, ...)β& ... \, unescaped balanced(...), merged spaced digits,dot.cβdot.op,^ooβ^infinity), and strictly filtered with Typst 0.15 so 100% of training/validation/test targets compile cleanly. - Strict document-isolated splitting and SHA-256 deduplication prevent image or label leakage between train, validation (
750), and frozen test (450) sets.
- LaTeX formulas converted via Rust
- Optimization:
- Two-stage BF16 SFT on RTX PRO 6000 (
batch_size=128,3epochs =22,830optimizer steps =2.92Mformula exposures): 800-step encoder-frozen alignment warmup (align_lr=3.5e-4), followed by full joint fine-tuning with differential cosine LRs (encoder_lr=5e-5,decoder_lr=2.0e-4,label_smoothing=0.05, realistic grayscale screenshot/PDF degradation augmentations). - Best checkpoint (
step 22,830) selected by validation Typst-CDM F1 (0.9657,val_token_acc=99.23%). A 1,000-step Typst-RLVR GRPO post-training experiment was also evaluated (frozen_testCDM F10.9712vs SFT0.9699, with SFT stronger oncdm_exact82.22%vs79.78%andsoft_iou0.8906vs0.8763); SFTbest-modelwas selected as the primary balanced release checkpoint.
- Two-stage BF16 SFT on RTX PRO 6000 (
Files and sizes
| Format | Files | Bytes | Decimal MB | Notes |
|---|---|---|---|---|
| Native FP32 | model.safetensors |
486,064,528 | 486.1 | Optimizer-free PyTorch weights |
| Browser Compact INT4 | onnx/int4/encoder.onnx + decoder.onnx |
84,940,876 | 84.9 | 4-bit symmetric MatMulNBits (block_size=32), WebGPU-ready |
| Browser Balanced INT8 | onnx/int8/encoder.onnx + decoder.onnx |
130,656,170 | 130.7 | Dynamic per-channel QInt8 MatMul/Gemm, WASM-ready |
| Browser Full FP32 | onnx/fp32/encoder.onnx + decoder.onnx |
486,807,706 | 486.8 | Unquantized ONNX (max_logit_diff = 4.29e-6 vs PyTorch) |
| Shared metadata | token-bytes.json + encoder_pos_grid.f32 |
846,115 | 0.85 | 4,096 byte pieces + 16Γ16Γ768 base float32 position grid |
Evaluation
1. Independent zero-leakage test set (frozen_test_phase2, 450 formulas)
Greedy decoding (temperature=0, max_new_tokens=1024), evaluated automatically
by compiling both prediction and reference with Typst 0.15 and computing
connected-component visual CDM F1 + soft pixel IoU:
| Slice | Count | Compile rate | Mean CDM F1 | CDM Exact | Soft IoU |
|---|---|---|---|---|---|
| Overall | 450 | 99.33% | 0.9699 | 82.22% | 0.8906 |
001β064 tokens |
192 | 99.48% | 0.9778 | 82.81% | 0.8968 |
065β128 tokens |
145 | 100.00% | 0.9551 | 83.45% | 0.8862 |
129β256 tokens |
70 | 97.14% | 0.9638 | 85.71% | 0.8974 |
257β512 tokens |
31 | 100.00% | 0.9986 | 74.19% | 0.9069 |
513+ tokens |
12 | 100.00% | 0.9840 | 58.33% | 0.7626 |
Per-suite breakdown (450 formulas):
| Suite | Count | Compile | Mean CDM F1 | CDM Exact |
|---|---|---|---|---|
im2latex-long |
7 | 100.0% | 1.0000 | 100.0% |
im2latex-ordinary |
19 | 100.0% | 1.0000 | 89.5% |
oleehyo-ordinary |
131 | 99.2% | 0.9751 | 84.7% |
oleehyo-long |
143 | 98.6% | 0.9302 | 69.9% |
synth-matrix_cases |
22 | 100.0% | 1.0000 | 100.0% |
synth-dense_accents |
24 | 100.0% | 0.9987 | 100.0% |
synth-nested_calc |
52 | 100.0% | 0.9997 | 96.2% |
synth-multiline |
52 | 100.0% | 0.9955 | 75.0% |
2. Unseen out-of-distribution comparison: V1.1 vs V2-Pro (12 fresh L1βL5 screenshots)
Tested with each version's exact native preprocessing on 12 newly authored screenshots spanning Word Cambria Math, GitHub/VS Code dark mode, warm JPEG PDF crops, 1,329 px wide equations, 3-line Navier-Stokes PDEs, and a 4-line SGD convergence proof:
| Model | Size | Compile | CDM Exact | Mean CDM F1 | Mean Soft IoU | 4T CPU Median |
|---|---|---|---|---|---|---|
| V1.1 INT8 | 33.1 MB | 12/12 | 2/12 | 0.6258 | 0.3513 | 469 ms |
| V1.1 FP32 | 117.2 MB | 12/12 | 2/12 | 0.6437 | 0.3691 | 664 ms |
V2-Pro INT8 (q8) |
130.7 MB | 11/12 | 4/12 | 0.7976 | 0.5020 | 797 ms |
V2-Pro INT4 (int4) |
84.9 MB | 12/12 | 5/12 | 0.8367 | 0.5590 | 1,461 ms |
| V2-Pro FP32 | 486.8 MB | 12/12 | 5/12 | 0.8427 | 0.5990 | 852 ms |
3. Live browser verification (Desktop Chromium, ONNX Runtime Web 1.22.0)
- Compact INT4 (
WebGPU):example-cases.png(100patches) finished in1.65 s(encoder82.7 ms);example-multiline.png(351patches, 88 tokens) finished in6.09 s. Zero WebGPU validation or console errors. - Balanced INT8 (
WASM, 4 threads):example-cases.pngfinished in939 ms;example-multiline.png(90 tokens) finished in3.82 s. Zero console errors. - Python β JS preprocessing parity:
0.000max pixel diff; 2D position interpolation parity<= 5.0e-6across 10 aspect ratios.
Intended use and limitations
- Intended use: Single cropped printed mathematical formulas (single-line or multi-line display blocks) from PDFs, papers, slides, or dark/light editors.
- Out of scope: Handwritten formulas, full-page document layout OCR, and non-mathematical prose paragraphs.
- Known failure modes:
- Unseen non-Computer-Modern fonts (e.g., Microsoft Word Cambria Math italic
xoccasionally predicted aschi). - Very low-resolution screenshots (~130 PPI) may drop subtle
bold(...)weight on accented variables. - Deeply nested 4-line formulas may occasionally omit a closing delimiter or exhibit minor dynamic-INT8 quantization jitter (Compact INT4 / FP32 track closer to reference weights on the hardest long formulas).
- Always inspect generated Typst before publishing; syntax validation does not guarantee mathematical equivalence.
- Unseen non-Computer-Modern fonts (e.g., Microsoft Word Cambria Math italic
Provenance and license
- Vision encoder initialization:
google/siglip2-base-patch16-naflex(Copyright Google LLC, Apache License 2.0; retained in licenses/SIGLIP2-APACHE-2.0.txt). - Decoder, bridge, tokenizer, preprocessing, and browser interface: Original project contributions licensed under MIT (LICENSE).
- Training datasets (not redistributed):
OleehyO/latex-formulas,linxy/LaTeX_OCR, and project-authored Typst synthetic renders. See NOTICE.md for complete attribution and scope notices.
- Downloads last month
- 116
Model tree for dbcccc/TypLens
Base model
google/siglip2-base-patch16-naflex