Skip to content

fix(vision): match the Unlimited-OCR processor crop limit and prompt - #3473

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/unlimited-ocr-preprocessing
Nov 27, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/unlimited-ocr-preprocessing

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

Closes #3472

Unlimited-OCR ran with DeepSeek-OCR's crop limit (9 tiles) and, on mlx-community/Unlimited-OCR-4bit, with DeepSeek-OCR's chat template, which ends the prompt with a space token. The reference (mlx-vlm's UnlimitedOCRProcessor, and baidu/Unlimited-OCR itself) allows 32 tiles and renders no trailing space. This PR gives Unlimited-OCR its own processor preset and a built-in template that replaces the shipped one. DeepSeek-OCR is unchanged.

On GB10 CUDA with Unlimited-OCR-4bit, the prompt ids now equal mlx-vlm e8035ae3's on a 1600x1200, a 1000x3000 and a 1920x1280 page (1507, 1537 and 897 tokens; main builds 898, 608 and 898). The date line main puts before its answer on the two report pages (2014-10-25 15:00:00 AM, 2014-10-20 15:00:00) is gone, and on the tall page main's 1. 2. 3. 4. ... loop becomes the transcription. Teacher-forced against the float32 reference over the reference's own reply, the fix disagrees at no decided position on any of the three pages; main disagrees at one on each, the first token, where the reference's <|det|> has a gap of 12.9 to 19.7. mlx-vlm given the same trailing space produces a date line too, so that token is the cause; mlx-vlm removed it from its own template in Neywa-Labs/mlx-vlm#2169.

What changed

  • src/vision/processors/deepseekocr.rs: DeepSeekOcrProcessor::unlimited_ocr(&full_config), the V1 preset with max_tiles UNLIMITED_OCR_MAX_CROPS (32), or UNLIMITED_OCR_PAGE_LAYOUT_MAX_CROPS (16) when sparse_attention_config.training_stage == "dash". That is mlx-vlm's max_crops = max_crops or (16 if page_layout else 32) with page_layout detected the way its from_pretrained does. Everything else (min 2 tiles, 640 tiles, 1024 global view, no crop at or under 640x640, the grid search) is the shared code.
  • src/loading/vlm_deepseekocr.rs: load_unlimited_ocr_vlm builds DeepSeekOcrProcessor::unlimited_ocr(&full_config) instead of DeepSeekOcrProcessor::default(). load_deepseekocr_vlm keeps default().
  • src/server/chat_template.rs: UNLIMITED_OCR_CHAT_TEMPLATE, mlx-vlm's UnlimitedOCRProcessor.default_chat_template (each message's content, one space between messages, nothing after the last), with typed content rendered as its text parts like the Florence-2 / Nemotron-Parse template. UnlimitedOcrVLM joins NemotronParseVLM in builtin_template_overrides_checkpoint and gets the template from builtin_chat_template, so it replaces the shipped chat_template.jinja for mlxcel generate, mlxcel-server and examples/logit_trace alike. The page images still enter as tokens after BOS (insert_unlimited_ocr_image_tokens, unchanged), where mlx-vlm's single <image> sits.
  • src/server/unlimited_ocr_chat_template_tests.rs (new) and the tests in deepseekocr.rs.

Not changed here:

  • DeepSeek-OCR's prompt. mlx-vlm renders <image>\nFree OCR. and its DeepSeek-OCR process_one drops the last id in inference mode (input_ids[:-1]), so its ids end 201 '\n', 21431, 126041, 16; mlxcel's end 21431, 126041, 16, 223. Same length (898 on 1600x1200), different ids. That is a separate question for DeepSeek-OCR and this PR pins its current template with a test.
  • Unlimited-OCR-2. Only the processor limit follows mlx-vlm (16). Its <PAGE> prompt, page2d RoPE and DASH attention are feat(vlm): add Unlimited-OCR-2 (page2d RoPE and DASH sparse attention) #3253, and no Unlimited-OCR-2 checkpoint was run.
  • processor_config.json max_crops / page_layout overrides, which mlx-vlm honours and no published checkpoint sets.
  • docs/supported-models.md: its Unlimited-OCR entry still says the processor is reused, which is true of the code but not of the limit. fix(vision): match DeepSeek-OCR CLIP and SAM encoder numerics #3456 rewrites the same line, so I left it for whichever lands second.

Verification

Spark2: NVIDIA GB10, driver 580.178.04, CUDA 13.0.88, Linux 7.0.0-1019-nvidia aarch64, rustc 1.97.1 (the pinned toolchain), --features cuda, --profile test-fast, MLX_ENABLE_TF32=0 MLXCEL_SDPA_DETERMINISTIC=1. Base db3a231413a51f3c4c4a2a165b8e8a571f45293b (still main when this was written). Commit tree dba792283816f4a8353e9f28b5c6852cf38ba6ca, the tree the unit tests, clippy and the gate ran on. The real-checkpoint binaries were built from tree 16348799, the same change before cargo fmt rewrapped four lines; the five mlxcel generate cases were rerun on binaries from dba79228 and are byte-identical (see "Real checkpoints").

Tests

cargo test --locked --profile test-fast --features cuda -p mlxcel --lib -- <name> --exact --test-threads=1. The "main" column is main plus the new tests, with unlimited_ocr() stubbed to return Self::default() (what the loader built on main) so the processor tests compile.

Test main (+ tests) fix
vision::processors::deepseekocr::tests::unlimited_ocr_crops_up_to_32_tiles FAIL: left: 9, right: 32 pass
vision::processors::deepseekocr::tests::unlimited_ocr_2_crops_up_to_16_tiles FAIL: left: 9, right: 16 pass
vision::processors::deepseekocr::tests::deepseekocr_keeps_9_tiles pass (guard) pass
server::unlimited_ocr_chat_template_tests::unlimited_ocr_replaces_the_shipped_deepseek_ocr_template FAIL: left: "Free OCR. ", right: "Free OCR." pass
server::unlimited_ocr_chat_template_tests::unlimited_ocr_builtin_joins_messages_with_one_space FAIL: a template is selected (no built-in, no shipped template) pass
server::unlimited_ocr_chat_template_tests::deepseek_ocr_keeps_its_shipped_template pass (guard) pass

The grid and token values in the processor tests are mlx-vlm's: dynamic_preprocess from mlx-vlm e8035ae3 and a throwaway test printing closest_grid + placeholder_count agree on all 30 sizes I tried (pages, screens, receipts, 641x641 to 4000x1000) at 9, 16 and 32 tiles. Related filters on the fix tree, all passing: deepseekocr 30, unlimited_ocr 11, deepseek_vl2 14, got_ocr 26, chat_template 197 (5 ignored), nemotron_parse 32, florence2 188, plamo2_vl 26, minicpm 48 (2 ignored), builtin_template 6, vlm_runtime 62.

Real checkpoints

mlx-community/Unlimited-OCR-4bit revision 945968c0, mlx-community/DeepSeek-OCR-4bit revision 4958b6b0. Synthetic pages: the 1600x1200 and 1920x1280 report pages from #3456, and a 1000x3000 notice (aspect 1:3). Prompt Free OCR., temperature 0, 160 tokens. Every model process ran alone under a watchdog (stop below 25 GiB available or above an RSS bound); none tripped.

Prompt length (BOS + image tokens + Free, OCR, ., and on main the extra ), from examples/logit_trace # prompt_tokens and mlx-vlm's processor:

Page Grid main / fix / mlx-vlm main fix mlx-vlm e8035ae3 fix ids = mlx-vlm ids
1600x1200 3x2 / 4x3 / 4x3 898 1507 1507 yes
1000x3000 1x3 / 2x6 / 2x6 608 1537 1537 yes
1920x1280 3x2 / 3x2 / 3x2 898 897 897 yes

mlxcel-server, one /v1/chat/completions request with the 1600x1200 page as a data URL: usage.prompt_tokens 898 on main, 1507 on the fix.

First line of the free-running reply (mlxcel generate):

Page main fix mlx-vlm float32
1600x1200 2014-10-25 15:00:00 AM <|det|>title [40, 60, 595, 105]<|/det|>Northern Network: Q3 Operations <|det|>title [41, 62, 595, 105]<|/det|>Northern Network: Q3 Operations
1000x3000 1. 2. 3. 4. 5. 6. ... (all 160 tokens) <|det|>title [68, 24, 843, 42]<|/det|>Dock Door Replacement Notice <|det|>title [68, 26, 843, 44]<|/det|>Dock Door Replacement Notice
1920x1280 2014-10-20 15:00:00 <|det|>title [38, 64, 560, 112]<|/det|>Northern Network: Q3 Operations <|det|>title [38, 64, 562, 112]<|/det|>Northern Network: Q3 Operations

After the first line, the fix's 160-token reply has the same text as the float32 reference's on all three pages, with every box coordinate within 2 (on the report pages main's text after its date line is a prefix of the same text, cut shorter by the tokens the date line used). The server reply on the 1600x1200 page shows the same date line on main and none on the fix.

mlx-vlm with the trailing space added on its side (Free OCR. , which its template prints verbatim) opens with a date line as well, in bf16 and float32: 1600x1200 2018年1月1日 (1508 tokens), 1920x1280 2014-10-25 15:00:00 AM (bf16) / 2014-10-25 15:45:00 (float32, 898 tokens). Without the space, none of its six runs does.

Which change does what, from two extra builds (main + only the crop limit, main + only the template), same pages, same trace setup as below:

Page Arm prompt tokens first line decided disagreements max logit delta perplexity vs reference
1600x1200 crops only 1508 2014年1月1日 1 / 117 9.307 +19.79%
1600x1200 template only 897 title 0 / 117 17.060 +7.93%
1600x1200 both (this PR) 1507 title 0 / 117 6.410 +7.25%
1000x3000 crops only 1538 1. 2. 3. ... 1 / 113 7.971 +21.07%
1000x3000 template only 607 title 0 / 113 18.124 +12.70%
1000x3000 both (this PR) 1537 title 0 / 113 8.624 +8.79%
1920x1280 crops only 898 2014-10-20 15:00:00 1 / 119 11.747 +11.85%
1920x1280 template only 897 title 0 / 119 4.627 +1.76%

The date line and the tall-page loop come from the trailing space alone. On these clean synthetic pages the crop limit does not change the greedy text (the template-only reply has the same text as the reference, boxes within 3); what it changes is that the prompt is the reference's, so the reference trace runs at all without --mlxcel-prompt, and the logits move closer to the reference (max delta 17.06 to 6.41 and 18.12 to 8.62, perplexity gap 7.93% to 7.25% and 12.70% to 8.79%). I have no page where the coarser grid changes the transcription.

DeepSeek-OCR-4bit, 1600x1200 and 1000x3000: mlxcel generate replies byte-identical between main and the fix (724 and 643 bytes), trace prompt ids (898, 608) and every trace row identical, server reply and usage identical.

Teacher-forced traces

examples/logit_trace MODEL REPLY 128 1 8 with MLXCEL_TRACE_IMAGE, over the float32 mlx-vlm reference's own greedy reply, against scripts/mlx_vlm_image_logit_trace.py --float32 scoring the same 128 tokens on its own prompt; scripts/compare_logit_traces.py --decided 2. mlx-vlm's float32 SAM batch fails on CUDA with cudaMallocAsync ... out of memory (#3456), so the reference ran SAM one image at a time through the same wrapper #3456 used.

Page Arm top-1 disagreement decided (gap >= 2) logit delta on the reference's token p50 / p90 / max perplexity vs reference
1600x1200 main 7 / 128 1 / 117 (position 0, gap 16.40) 1.319 / 4.343 / 17.060 +19.54%
1600x1200 fix 6 / 128 0 / 117 0.887 / 3.350 / 6.410 +7.25%
1000x3000 main 9 / 128 1 / 113 (position 0, gap 12.93) 1.676 / 5.767 / 16.124 +24.54%
1000x3000 fix 5 / 128 0 / 113 1.076 / 4.197 / 8.624 +8.79%
1920x1280 main 3 / 128 1 / 119 (position 0, gap 19.75) 0.982 / 3.129 / 11.747 +11.85%
1920x1280 fix 2 / 128 0 / 119 0.781 / 2.888 / 4.627 +1.76%

At position 0 the reference's token is <|det|> (128818); main's top-1 is 201 (the start of the date) on the report pages and 1 on the tall page. On 1920x1280, where the grids agree, mlx-vlm scoring main's own 898 ids (--mlxcel-prompt) agrees with main at every decided position (0 / 120, max delta 7.24): with the same prompt the two forwards match, and the difference is the prompt. On the other two pages mlx-vlm refuses main's prompt (image-token count differs: mlx-vlm 1503, mlxcel 893 and 1533, 603).

The gap the fix leaves (perplexity +1.8% to +8.8%) is the encoder: with #3456 (e287f903e) applied on top of this commit (the merged tree 818b69dc, built and traced the same way), the prompt ids still equal the reference's and the trace closes up.

Page Arm top-1 disagreement decided logit delta p50 / p90 / max perplexity vs reference
1600x1200 this PR + #3456 3 / 128 0 / 117 0.147 / 0.509 / 3.143 -0.05%
1000x3000 this PR + #3456 2 / 128 0 / 113 0.210 / 0.733 / 2.271 +0.20%
1920x1280 this PR + #3456 1 / 128 0 / 119 0.108 / 0.406 / 1.106 -0.14%

Verification limits

  • GB10 CUDA only; Metal and ROCm not run. Three synthetic pages, one prompt, 128 traced tokens each.
  • The remaining gap to the reference on this PR alone (perplexity +1.8% to +8.8%) closes with fix(vision): match DeepSeek-OCR CLIP and SAM encoder numerics #3456 on top (table above); the merged tree was traced but not run through the gate.
  • Unlimited-OCR-2's 16-tile limit is covered by a unit test only.
  • baidu/Unlimited-OCR (the original, non-MLX checkpoint) was not loaded; its prompt rules were read from its modeling_unlimitedocr.py, chat_template.jinja and preprocessor_config.json at 8249d20b.
  • Multi-page (several images, one <image>) requests were not run. They go through the same processor and template.

Unlimited-OCR reused DeepSeek-OCR's processor and, on the MLX conversion,
DeepSeek-OCR's chat template. Both differ from the Unlimited-OCR reference
(mlx-vlm's UnlimitedOCRProcessor and baidu/Unlimited-OCR):

- The crop grid was capped at DeepSeek-OCR's 9 tiles. The reference allows
  32 (16 for an Unlimited-OCR-2 config whose
  sparse_attention_config.training_stage is "dash", mlx-vlm's page_layout).
  A 1600x1200 page was cut 3x2 (893 image tokens) instead of 4x3 (1503),
  and a 1000x3000 page 1x3 (603) instead of 2x6 (1533).
  DeepSeekOcrProcessor::unlimited_ocr() is the V1 preset with that limit,
  and load_unlimited_ocr_vlm uses it.
- mlx-community/Unlimited-OCR-4bit ships DeepSeek-OCR's chat_template.jinja,
  which prints a space after every message, so the prompt ended in token
  223 (' ') that the reference never feeds: 898 tokens where mlx-vlm has 897
  on a 1920x1280 page. The model then opened its answer with an invented
  date line ("2014-10-20 15:00:00"); mlx-vlm given the same trailing space
  does the same. mlx-vlm removed this suffix from its own template in
  Neywa-Labs/mlx-vlm#2169. Unlimited-OCR now renders a built-in template
  (mlx-vlm's default_chat_template: message texts joined by one space,
  nothing after the last, typed content as its text parts) that replaces the
  shipped one, through the builtin_template_overrides_checkpoint path
  Nemotron-Parse uses.

DeepSeek-OCR keeps its 9-tile limit and its shipped template.

Six tests: unlimited_ocr_crops_up_to_32_tiles,
unlimited_ocr_2_crops_up_to_16_tiles,
unlimited_ocr_replaces_the_shipped_deepseek_ocr_template and
unlimited_ocr_builtin_joins_messages_with_one_space fail before this change;
deepseekocr_keeps_9_tiles and deepseek_ocr_keeps_its_shipped_template pin
DeepSeek-OCR.

On GB10 CUDA with Unlimited-OCR-4bit, the prompt ids now equal mlx-vlm
e8035ae3's on 1600x1200, 1000x3000 and 1920x1280 pages (1507, 1537, 897
tokens), the date line is gone, and a teacher-forced trace against the
float32 reference has no disagreement at a decided position on any of the
three (main: one each, the first token, reference gap 12.9 to 19.7).
DeepSeek-OCR-4bit output and traces are byte-identical before and after.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority labels Nov 27, 2026
The Unlimited-OCR entry in docs/supported-models.md said the DeepSeek-OCR processor is reused unchanged, which stopped being true once the crop limit and prompt template diverged. The entry now names the 32-crop limit (16 for `dash` checkpoints) and the built-in template, and CHANGELOG.md gains the Unreleased / Fixed entry the PR did not have. The PR body left this line for whichever of lablup#3456 and this PR merged second.

Refs lablup#3472, Refs lablup#3473
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Target PR: #3473. Linked issue: #3472.

Findings

  • Crop limit: DeepSeekOcrProcessor::unlimited_ocr matches mlx-vlm UnlimitedOCRProcessor (references/mlx-vlm at 466069f5): max_crops = 16 if page_layout else 32, with page_layout set from sparse_attention_config.training_stage == "dash" in config.json, as its from_pretrained does. read_sanitized_vlm_config keeps that key, so the loader sees it. DeepSeek-OCR keeps 9.
  • Template: the built-in template renders the message texts joined by one space with nothing after the last, mlx-vlm's default_chat_template. It overrides the shipped template only for UnlimitedOcrVLM, and the image tokens still go after BOS where mlx-vlm's single <image> sits.
  • Security: the crop limit is a compile-time constant (32), not derived from request data. The image size only selects a grid among products of 2 to 32, so one image is at most 32 tiles of 640x640 plus the 1024 global view (about 3.1k image tokens). The grid search runs a fixed-size enumeration independent of the image dimensions, and an extreme aspect ratio still lands on a grid with at most 32 tiles. The request-level image count stays bounded by the server's existing media limits. Per-image work grows at most about 3.5x over the old 9-tile cap, which is the reference behaviour.
  • Performance: no change on the decode path; the extra cost is the larger prefill the reference also pays.
  • Gap fixed (835051e5c): docs/supported-models.md still said the processor is reused unchanged (the PR body left that line for whichever of fix(vision): match DeepSeek-OCR CLIP and SAM encoder numerics #3456 and this PR landed second, and fix(vision): match DeepSeek-OCR CLIP and SAM encoder numerics #3456 has now merged), and there was no CHANGELOG entry. Both are added. No code change.

Also merged origin/main (with #3456) into the branch; src/loading/vlm_deepseekocr.rs merged without conflict.

Validation (Mac Studio M3 Ultra, Metal, merged tree)

Remote CI was cancelled by maintainer decision; the merge relies on the local checks below.

  • cargo fmt --all -- --check: pass
  • cargo clippy -p mlxcel -p mlxcel-core --all-targets --features metal,accelerate -- -D warnings: pass
  • cargo test -p mlxcel --lib --profile test-fast --features metal,accelerate -- <filter> --test-threads=1: deepseekocr 35, unlimited_ocr 12, chat_template 197 (5 ignored), builtin_template 6, nemotron_parse 32, got_ocr 26, clip_ 41, all passed.
  • No Unlimited-OCR or DeepSeek-OCR checkpoint is on this host, so the PR's GB10 prompt-id and trace results were not reproduced here.

@inureyes
inureyes merged commit 822bc89 into lablup:main Nov 27, 2026
30 of 33 checks passed
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(vision): Unlimited-OCR crops like DeepSeek-OCR and ends its prompt with a stray space

2 participants