Repository navigation
feat(models): complete DeepSeek-OCR VLM (connector, processor, loader, runtime) - #655
Merged
Merged
Conversation
…, runtime) 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
…, 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
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.
Completes the DeepSeek-OCR port (#540), building on the SAM + CLIP encoders and decoder hooks merged in #651.
Connector / wrapper (
vision::deepseekocr)Per view, channel-fuses
[CLIP(drop CLS), SAM(flattened)], projects2048 -> 1280(quantized linear), then assembles the 2D tile mosaic: the global view becomes a16x17grid withimage_newlinecolumns (272 rows), tiles become an(h*10) x (w*10 + 1)grid, and each image's[tiles, global, view_separator]rows scatter into the<image>placeholder positions of the embedded prompt (merge_llava). The placeholder tokens are all id 128815, so the scatter fills them in feature order.Processor (
vision::processors::deepseekocr)Pads a global 1024 view (aspect-preserving, grey pad) and, for images larger than a tile, selects a closest-aspect-ratio grid of 640 tiles (product 2..9). Channels-last
(x/255 - 0.5)/0.5normalization. Computes the flat placeholder count per image:(16+1)*16 + 1, plus(10w+1)*10hwhen tiled.Loader + runtime
vlm_deepseekocrparseslanguage_config(injecting the root quantization block and amodel_type), loads the SAM / CLIP / projector /image_newline/view_separatorby ref, and hands themodel.*keys to the shareddeepseekdecoder. Both on-disk layouts are handled (view_seperatorspelling,model.prefix). Detection, theModelType/LoadedModel/VlmRuntimeRefarms,<image>(128815) expansion, and image-token output suppression complete the runtime.Validation (
mlx-community/DeepSeek-OCR-4bit)Free OCR converts a price table to markdown on both the no-tile path (273 placeholders) and the 3x2-tile path (893 = 273 + 31*20 placeholders):
<|grounding|>prompts emit<|ref|>...<|/ref|><|det|>[[x1, y1, x2, y2]]<|/det|>boxes (coords 0-1000). The vision towers were numerically validated against mlx-vlm in #651.Closes #540