Repository navigation
feat(vision): DeepSeek-OCR SAM + CLIP encoders and decoder VLM hooks - #651
Merged
Merged
Conversation
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
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
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
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_3stride-2 conv compressor.final_out_chansis 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_thetaserde defaults (the DeepSeek-OCRlanguage_configomits both) andforward_with_embeddings/embed_tokensoverrides so the shared DeepSeek MoE decoder can serve as the VLM text backbone.Validation
tests/deepseekocr_encoder_parity.rsloads the realmlx-community/DeepSeek-OCR-4bitvision 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.