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,001 training pairs, printed math only, zero handwriting):
    • OleehyO/latex-formulas (cleaned_formulas ArXiv printed): 479,251 unique compiler-verified formulas (2Γ— upweighting on long/multi-line formulas β†’ 628,355 train pairs).
    • linxy/LaTeX_OCR (full printed split only): 77,371 unique compiler-verified formulas (86,046 train pairs).
    • Balanced 4-family native Typst synthetic generator: 260,000 formulas generated (259,600 train pairs, 250 val, 150 frozen 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.
  • Optimization:
    • Two-stage BF16 SFT on RTX PRO 6000 (batch_size=128, 3 epochs = 22,830 optimizer steps = 2.92M formula 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_test CDM F1 0.9712 vs SFT 0.9699, with SFT stronger on cdm_exact 82.22% vs 79.78% and soft_iou 0.8906 vs 0.8763); SFT best-model was selected as the primary balanced release checkpoint.

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 (100 patches) finished in 1.65 s (encoder 82.7 ms); example-multiline.png (351 patches, 88 tokens) finished in 6.09 s. Zero WebGPU validation or console errors.
  • Balanced INT8 (WASM, 4 threads): example-cases.png finished in 939 ms; example-multiline.png (90 tokens) finished in 3.82 s. Zero console errors.
  • Python ↔ JS preprocessing parity: 0.000 max pixel diff; 2D position interpolation parity <= 5.0e-6 across 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 x occasionally predicted as chi).
    • 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.

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
Safetensors
Model size
0.1B params
Tensor type
F32
Β·
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for dbcccc/TypLens

Quantized
(7)
this model