Skip to content

feat(vision): DeepSeek-OCR SAM + CLIP encoders and decoder VLM hooks - #651

Merged
inureyes merged 1 commit into
mainfrom
feat/issue-540-deepseek-ocr
Jul 4, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/issue-540-deepseek-ocr

Conversation

@inureyes

@inureyes inureyes commented Jul 4, 2026

Copy link
Copy Markdown
Member

Foundational infrastructure for the DeepSeek-OCR port (#540), built and validated before the full model integration (connector, processor, loader, runtime) lands.

SAM encoder (deepseekocr_sam)

SAM-style ViT-B: patch-embed conv, bicubic-resampled absolute position embedding, 12 windowed/global attention blocks with a decomposed relative-position bias (a formula that exists nowhere else in-tree), a two-conv neck, and the net_2/net_3 stride-2 conv compressor. final_out_chans is a constructor parameter (1024 OCR, 896 OCR-2). A channels-last conv-weight gate tolerates both on-disk layouts.

CLIP encoder (deepseekocr_clip)

CLIP-style ViT-L body: ingests the SAM grid as its patch embeddings (its own conv is bypassed), prepends a class token, adds a resampled learned position embedding (plain cubic, contrasted with SAM's antialias), pre_layrnorm, then 24 pre-LN layers. Returns tokens including CLS (the connector drops it).

Decoder hooks (deepseek)

rms_norm_eps / rope_theta serde defaults (the DeepSeek-OCR language_config omits both) and forward_with_embeddings / embed_tokens overrides so the shared DeepSeek MoE decoder can serve as the VLM text backbone.

Validation

tests/deepseekocr_encoder_parity.rs loads the real mlx-community/DeepSeek-OCR-4bit vision weights (bf16), runs both encoders on a deterministic input, and asserts parity vs the mlx-vlm reference: SAM absmean 0.04304 (matches to bf16 precision), CLIP absmean 0.14316 vs 0.14356 (bf16 tolerance through 24 layers). Co-located unit tests cover the window partition round-trip, the interpolation matrix, and the rel-pos gather.

Part of #540. The full model (channel-fusion + 2D-mosaic connector, dynamic-tiling processor, two-layout loader, runtime wiring, real-checkpoint OCR) follows in a subsequent PR.

Foundation for the DeepSeek-OCR port. Adds the two vision towers and the shared-decoder hooks they will feed, validated numerically against the mlx-vlm reference before the full model integration lands.

deepseekocr_sam: SAM-style ViT-B with windowed / global attention, a decomposed relative-position bias, a two-conv neck, and the net_2/net_3 stride-2 conv compressor (final channel count parameterized for OCR vs OCR-2). Channels-last conv-weight gate, PyTorch-matching bicubic position-embedding resample.

deepseekocr_clip: CLIP-style ViT-L body that ingests the SAM grid as its patch embeddings, prepends a class token, adds a resampled position embedding, runs pre_layrnorm and 24 pre-LN layers.

deepseek: serde defaults for rms_norm_eps / rope_theta and forward_with_embeddings / embed_tokens overrides so the shared DeepSeek MoE decoder can serve as a VLM text backbone.

Validated on mlx-community/DeepSeek-OCR-4bit (bf16 vision): with a deterministic input the SAM output matches the reference to bf16 precision (absmean 0.04304) and CLIP within bf16 tolerance (absmean 0.14316 vs 0.14356). Co-located unit tests cover the window partition round-trip, the interpolation matrix, and the relative-position gather.

Part of #540.
@inureyes inureyes added area:models Model architectures, weights, loading, metadata type:enhancement New features, capabilities, or significant additions labels Jul 4, 2026
@inureyes
inureyes merged commit f3dc9c6 into main Jul 4, 2026
4 checks passed
@inureyes
inureyes deleted the feat/issue-540-deepseek-ocr branch July 4, 2026 05:24
inureyes added a commit that referenced this pull request Jul 4, 2026
…, runtime) (#655)

Wires the SAM + CLIP encoders (PR #651) and the shared DeepSeek MoE decoder into a working DeepSeek-OCR model end to end.

Processor: pad-to-square 1024 global view plus a dynamic closest-aspect-ratio grid of 640 tiles, channels-last (x/255 - 0.5)/0.5 normalization, and the flat placeholder count each image expands to.

Wrapper: per-view fuse [CLIP(drop CLS), SAM(flattened)] into a quantized linear projector, assemble the 2D tile mosaic with image_newline columns and a trailing view_separator, and scatter each image's [tiles, global, view_separator] rows into the <image> positions of the embedded prompt.

Loader and runtime: two on-disk layouts (view_seperator spelling, model. prefix), language_config with root-quantization and model_type injection, detection plus ModelType / LoadedModel / VlmRuntimeRef arms, <image> (128815) placeholder expansion, and image-token output suppression.

Validated on mlx-community/DeepSeek-OCR-4bit: converts a price table to a markdown table on both the no-tile path (273 placeholders) and the 3x2-tile path (893 placeholders), and emits grounding boxes for <|grounding|> prompts.

Closes #540
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…ablup#651)

Foundation for the DeepSeek-OCR port. Adds the two vision towers and the shared-decoder hooks they will feed, validated numerically against the mlx-vlm reference before the full model integration lands.

deepseekocr_sam: SAM-style ViT-B with windowed / global attention, a decomposed relative-position bias, a two-conv neck, and the net_2/net_3 stride-2 conv compressor (final channel count parameterized for OCR vs OCR-2). Channels-last conv-weight gate, PyTorch-matching bicubic position-embedding resample.

deepseekocr_clip: CLIP-style ViT-L body that ingests the SAM grid as its patch embeddings, prepends a class token, adds a resampled position embedding, runs pre_layrnorm and 24 pre-LN layers.

deepseek: serde defaults for rms_norm_eps / rope_theta and forward_with_embeddings / embed_tokens overrides so the shared DeepSeek MoE decoder can serve as a VLM text backbone.

Validated on mlx-community/DeepSeek-OCR-4bit (bf16 vision): with a deterministic input the SAM output matches the reference to bf16 precision (absmean 0.04304) and CLIP within bf16 tolerance (absmean 0.14316 vs 0.14356). Co-located unit tests cover the window partition round-trip, the interpolation matrix, and the relative-position gather.

Part of lablup#540.
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…, runtime) (lablup#655)

Wires the SAM + CLIP encoders (PR lablup#651) and the shared DeepSeek MoE decoder into a working DeepSeek-OCR model end to end.

Processor: pad-to-square 1024 global view plus a dynamic closest-aspect-ratio grid of 640 tiles, channels-last (x/255 - 0.5)/0.5 normalization, and the flat placeholder count each image expands to.

Wrapper: per-view fuse [CLIP(drop CLS), SAM(flattened)] into a quantized linear projector, assemble the 2D tile mosaic with image_newline columns and a trailing view_separator, and scatter each image's [tiles, global, view_separator] rows into the <image> positions of the embedded prompt.

Loader and runtime: two on-disk layouts (view_seperator spelling, model. prefix), language_config with root-quantization and model_type injection, detection plus ModelType / LoadedModel / VlmRuntimeRef arms, <image> (128815) placeholder expansion, and image-token output suppression.

Validated on mlx-community/DeepSeek-OCR-4bit: converts a price table to a markdown table on both the no-tile path (273 placeholders) and the 3x2-tile path (893 placeholders), and emits grounding boxes for <|grounding|> prompts.

Closes lablup#540
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant