Skip to content

feat(models): add Falcon OCR (falcon_ocr) VLM support #848

Closed

Description

@inureyes

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

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 raw dim / n_layers / n_heads / n_kv_heads / ffn_dim keys).
  • 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 / output scheme (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.py provides 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 upstream language.py is Llama-derived with the custom pieces above, not the Falcon-H1 Mamba / attention hybrid. Verify the shipped config.json first; most likely this needs a small dedicated decoder rather than the falcon_h1 path. Borrow per-head attention sinks from src/models/gpt_oss.rs and the 2-D / 3-D position-index machinery from src/models/qwen_mrope_state.rs for 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.rs and 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_weights boundary; vision-free means no separate tower weights, but confirm precision handling (patch projector precision vs quantized decoder).
  • Layout: port layout.py region / 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_ocr against the shipped tiiuae/Falcon-OCR config.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" in src/models/detection.rs.
  • Registration in src/model_metadata.rs (for_each_model_registration!).
  • TP / distributed arch-string + generate_vlm summary if applicable.
  • _tests.rs unit tests.
  • docs/supported-models.md updated.
  • 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). Follow docs/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

  1. added
    type:enhancement
    priority:medium
    status:ready
    area:models
    on Jul 21, 2026
  2. added
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Aug 7, 2026
  3. added
    status:done
    and removed
    status:review
    on Aug 7, 2026
  4. added 3 commits that reference this issue on Aug 7, 2026
    6029c96
    6026fe8
    0f72757
  5. self-assigned this
    on Aug 31, 2026
  6. added 3 commits that reference this issue on Oct 27, 2026
    83cc4ae
    650768c
    c9f4217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelspriority:mediumstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions