Repository navigation
Description
Summary
Falcon OCR (tiiuae/Falcon-OCR, ~300M) is a compact early-fusion OCR VLM. Verified against the upstream directory and the #548 triage note: it has no separate vision encoder. Images are cut into 16x16 patches and linearly projected straight into the token stream, and a single decoder processes image and text under a hybrid mask (bidirectional within each image block, causal over text). This is architecturally unlike the ViT-plus-connector VLMs already in the tree, so the "OCR vision encoder" framing should be read as an early-fusion patch projector, not a standalone tower.
Concretizes the falcon_ocr triage entry in #548.
Upstream reference
- Directory: https://github.com/Blaizzy/mlx-vlm/tree/main/mlx_vlm/models/falcon_ocr
- Top-level model + patch projector: https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/falcon_ocr/falcon_ocr.py
- Language (decoder): https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/falcon_ocr/language.py
- Layout-aware OCR: https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/falcon_ocr/layout.py
- Processor: https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/falcon_ocr/processing_falcon_ocr.py
- Config: https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/falcon_ocr/config.py
Note: there is no vision.py in this directory, consistent with the no-vision-encoder architecture.
Public checkpoint
Confirmed available on HuggingFace: tiiuae/Falcon-OCR (3k+ downloads). It loads as SafeTensors and can be validated or converted directly. State: a dedicated mlx-community conversion was not located at filing time (MLX-specific conversion unconfirmed).
Architecture notes (upstream + #548)
model_type: falcon_ocr(same string in the sub-configs; the config is also expressible via rawdim/n_layers/n_heads/n_kv_heads/ffn_dimkeys).- No vision encoder: 16x16 image patches are linearly projected into the token stream (early fusion).
- Hybrid attention mask: bidirectional within each image block, causal over text.
- Decoder blocks are Llama-derived with custom pieces: fused QKV, per-head Q / K RMSNorm, learned per-head attention sinks, a squared-ReLU gated MLP, and a 3-D rotary scheme that concatenates 1-D temporal rotary with a 2-D per-head spatial rotary applied at image positions.
- Weight names use a
tok_embeddings/layers.N.attention/feed_forward/outputscheme (not HF-style); the fused MLP weight ships interleaved and must be split at load. - The model disallows chunked prefill; confirm this does not conflict with mlxcel's prefill path.
layout.pyprovides layout-aware OCR (region / reading-order handling) on top of the raw OCR decode.
Implementation plan
- Text backbone: the initial brief suggested reusing the Falcon-H1 backbone (
src/models/falcon_h1.rs, detection arm"falcon_h1"), but the upstreamlanguage.pyis Llama-derived with the custom pieces above, not the Falcon-H1 Mamba / attention hybrid. Verify the shippedconfig.jsonfirst; most likely this needs a small dedicated decoder rather than the falcon_h1 path. Borrow per-head attention sinks fromsrc/models/gpt_oss.rsand the 2-D / 3-D position-index machinery fromsrc/models/qwen_mrope_state.rsfor the spatial rotary. - Early-fusion patch projector, hybrid mask, and squared-ReLU MLP are new but small; add them in the new model module.
- Placeholder / image-block expansion: reuse
src/models/multimodal_placeholders.rsand the OCR VLM merge paths already in the tree (src/vision/dots_ocr.rs+src/loading/vlm_dots_ocr.rs,src/models/paddleocr_vl.rs+src/loading/vlm_paddleocr.rs) as templates for image-token region handling. - Loader: handle the non-HF weight naming and the interleaved fused-MLP split at the
from_weightsboundary; vision-free means no separate tower weights, but confirm precision handling (patch projector precision vs quantized decoder). - Layout: port
layout.pyregion / reading-order handling as a post-processing stage over the OCR output.
Touchpoints & acceptance criteria
- Config parse: confirm raw-vs-HF key scheme and
model_type: falcon_ocragainst the shippedtiiuae/Falcon-OCRconfig.json. - Early-fusion patch projector (16x16 linear projection) + hybrid image / text mask.
- Decoder custom pieces: fused QKV, per-head Q / K RMSNorm, per-head attention sinks, squared-ReLU gated MLP, 3-D concat rotary.
- Image-token / image-block placeholder expansion + merge.
-
from_weights+sanitize: non-HF weight-name remap, interleaved fused-MLP split, precision handling; verify the no-chunked-prefill constraint against mlxcel's prefill path. - Layout-aware post-processing from
layout.py. - Arch-string arm
"falcon_ocr"insrc/models/detection.rs. - Registration in
src/model_metadata.rs(for_each_model_registration!). - TP / distributed arch-string +
generate_vlmsummary if applicable. -
_tests.rsunit tests. -
docs/supported-models.mdupdated. - Real-checkpoint validation:
./target/release/mlxcel generate -m models/Falcon-OCR --image <doc-image>produces correct OCR text (and layout when the layout path is exercised). Followdocs/adding-models.md.
Effort
MEDIUM. Small model, but the delta is a bespoke early-fusion decoder (custom rotary / sinks / MLP) plus non-HF weight loading and layout post-processing, not a ViT-plus-connector reuse.
Activity
fix(models): declare the dense KV layout for Falcon-OCR
feat(models): wire the Falcon-OCR layout stage into the CLI flow
fix(security): bound --layout-detections input and crop lazily
fix(models): stop duplicating Falcon-OCR prefill state per token
feat(models): add Falcon-OCR (falcon_ocr) early-fusion VLM support (#…
feat(models): add Jina VLM (jvlm) vision-language support (#1076)
docs(reports): add technical reports for the #847/#848/#849 VLM port …
feat(models): add Falcon-OCR (falcon_ocr) early-fusion VLM support (l…
feat(models): add Jina VLM (jvlm) vision-language support (lablup#1076)
docs(reports): add technical reports for the lablup#847/lablup#848/la…