Repository navigation
fix(vision): match the Unlimited-OCR processor crop limit and prompt - #3473
Merged
inureyes merged 3 commits intoNov 27, 2026
Merged
Conversation
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.
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
Member
Hardening reviewTarget PR: #3473. Linked issue: #3472. Findings
Also merged Validation (Mac Studio M3 Ultra, Metal, merged tree)Remote CI was cancelled by maintainer decision; the merge relies on the local checks below.
|
4 of 5 tasks
This was referenced Nov 27, 2026
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.
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'sUnlimitedOCRProcessor, andbaidu/Unlimited-OCRitself) 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-vlme8035ae3's on a 1600x1200, a 1000x3000 and a 1920x1280 page (1507, 1537 and 897 tokens;mainbuilds 898, 608 and 898). The date linemainputs 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 pagemain's1. 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;maindisagrees 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 withmax_tilesUNLIMITED_OCR_MAX_CROPS(32), orUNLIMITED_OCR_PAGE_LAYOUT_MAX_CROPS(16) whensparse_attention_config.training_stage == "dash". That is mlx-vlm'smax_crops = max_crops or (16 if page_layout else 32)withpage_layoutdetected the way itsfrom_pretraineddoes. 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_vlmbuildsDeepSeekOcrProcessor::unlimited_ocr(&full_config)instead ofDeepSeekOcrProcessor::default().load_deepseekocr_vlmkeepsdefault().src/server/chat_template.rs:UNLIMITED_OCR_CHAT_TEMPLATE, mlx-vlm'sUnlimitedOCRProcessor.default_chat_template(each message's content, one space between messages, nothing after the last), with typed content rendered as itstextparts like the Florence-2 / Nemotron-Parse template.UnlimitedOcrVLMjoinsNemotronParseVLMinbuiltin_template_overrides_checkpointand gets the template frombuiltin_chat_template, so it replaces the shippedchat_template.jinjaformlxcel generate,mlxcel-serverandexamples/logit_tracealike. 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 indeepseekocr.rs.Not changed here:
<image>\nFree OCR.and its DeepSeek-OCRprocess_onedrops the last id in inference mode (input_ids[:-1]), so its ids end201 '\n', 21431, 126041, 16; mlxcel's end21431, 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.<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.jsonmax_crops/page_layoutoverrides, 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. Basedb3a231413a51f3c4c4a2a165b8e8a571f45293b(stillmainwhen this was written). Commit treedba792283816f4a8353e9f28b5c6852cf38ba6ca, the tree the unit tests, clippy and the gate ran on. The real-checkpoint binaries were built from tree16348799, the same change beforecargo fmtrewrapped four lines; the fivemlxcel generatecases were rerun on binaries fromdba79228and are byte-identical (see "Real checkpoints").cargo fmt --all -- --checkcargo clippy --locked --workspace --all-targets --features cuda -- -D warningspython3 scripts/ci/check_used_by.py(used-by: OK, 78 top-level pub fn in 2 shared files, 57 annotated, 635 caller-file references resolved),bash scripts/ci/check_used_by_test.sh(all cases passed),python3 scripts/insert_apache_header.py --check,python3 scripts/ci/check_env_var_docs.py,python3 scripts/ci/check_kernel_dtype_keys.py,python3 scripts/ci/check_cross_repo_refs.py(no bare 3+-digit refs added)make verify-test-cudawith a private 0700TMPDIR(07:52 to 08:36): 17,242 passed, 7 failed, 517 ignored over 199test resultlines, treedba79228before and after. The failures, none in code this PR touches:models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_context(fix(cuda): MLX CUDA RoPE gives every row of a broadcast input the same rotation, failingmake verify-test-cudaon main #3442, fix in fix(cuda): rotate broadcast RoPE inputs in a dense output #3443)models::raon1_mini::speculative::tests::{a_steered_verify_block_applies_the_alpha_exactly_as_decode_does, a_verify_block_equals_the_decode_chain_byte_for_byte, rollback_after_a_partial_accept_equals_a_fresh_decode_of_the_accepted_tokens, the_exactness_probe_passes_on_the_tiny_model}(fix: Raon-1 mini correctness and validation follow-ups #3457)mlxcel-xlavalidation::tests::registered_fixtures_are_byte_exactandvalidation::tests::emit_all_reproduces_registered_goldens(arch plamo2: FAIL ... DIFF prefill.mlir, 955 golden lines vs 1015 emitted). The same two tests run alone on unmodifiedmain(db3a23141, this commit's changes stashed) fail with the same messages and line counts;src/lib/mlxcel-xlais untouched here. No open issue covers them (gh search issuesfor the test names andplamo2 golden); fix(vision): round resize ties half to even in Qwen-VL and seven other processors #3464 and fix(vision): rescale out-of-bounds images from the original size like the reference #3466 report the same two.git merge-tree --write-treeof this commit with fix(vision): match DeepSeek-OCR CLIP and SAM encoder numerics #3456 (e287f903e) is clean; the two touchsrc/loading/vlm_deepseekocr.rsin different hunks. No dependency.Tests
cargo test --locked --profile test-fast --features cuda -p mlxcel --lib -- <name> --exact --test-threads=1. The "main" column ismainplus the new tests, withunlimited_ocr()stubbed to returnSelf::default()(what the loader built onmain) so the processor tests compile.vision::processors::deepseekocr::tests::unlimited_ocr_crops_up_to_32_tilesleft: 9, right: 32vision::processors::deepseekocr::tests::unlimited_ocr_2_crops_up_to_16_tilesleft: 9, right: 16vision::processors::deepseekocr::tests::deepseekocr_keeps_9_tilesserver::unlimited_ocr_chat_template_tests::unlimited_ocr_replaces_the_shipped_deepseek_ocr_templateleft: "Free OCR. ", right: "Free OCR."server::unlimited_ocr_chat_template_tests::unlimited_ocr_builtin_joins_messages_with_one_spacea template is selected(no built-in, no shipped template)server::unlimited_ocr_chat_template_tests::deepseek_ocr_keeps_its_shipped_templateThe grid and token values in the processor tests are mlx-vlm's:
dynamic_preprocessfrom mlx-vlme8035ae3and a throwaway test printingclosest_grid+placeholder_countagree 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:deepseekocr30,unlimited_ocr11,deepseek_vl214,got_ocr26,chat_template197 (5 ignored),nemotron_parse32,florence2188,plamo2_vl26,minicpm48 (2 ignored),builtin_template6,vlm_runtime62.Real checkpoints
mlx-community/Unlimited-OCR-4bitrevision945968c0,mlx-community/DeepSeek-OCR-4bitrevision4958b6b0. Synthetic pages: the 1600x1200 and 1920x1280 report pages from #3456, and a 1000x3000 notice (aspect 1:3). PromptFree 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 onmainthe extra), fromexamples/logit_trace# prompt_tokensand mlx-vlm's processor:e8035ae3mlxcel-server, one/v1/chat/completionsrequest with the 1600x1200 page as a data URL:usage.prompt_tokens898 onmain, 1507 on the fix.First line of the free-running reply (
mlxcel generate):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 Operations1. 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 Notice2014-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 OperationsAfter 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 onmainand 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: 1600x12002018年1月1日(1508 tokens), 1920x12802014-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:2014年1月1日1. 2. 3. ...2014-10-20 15:00:00The 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 generatereplies byte-identical betweenmainand 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 8withMLXCEL_TRACE_IMAGE, over the float32 mlx-vlm reference's own greedy reply, againstscripts/mlx_vlm_image_logit_trace.py --float32scoring the same 128 tokens on its own prompt;scripts/compare_logit_traces.py --decided 2. mlx-vlm's float32 SAM batch fails on CUDA withcudaMallocAsync ... out of memory(#3456), so the reference ran SAM one image at a time through the same wrapper #3456 used.At position 0 the reference's token is
<|det|>(128818);main's top-1 is201(the start of the date) on the report pages and1on the tall page. On 1920x1280, where the grids agree, mlx-vlm scoringmain's own 898 ids (--mlxcel-prompt) agrees withmainat 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 refusesmain's prompt (image-token count differs: mlx-vlm 1503, mlxcel 893and1533, 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 tree818b69dc, built and traced the same way), the prompt ids still equal the reference's and the trace closes up.Verification limits
baidu/Unlimited-OCR(the original, non-MLX checkpoint) was not loaded; its prompt rules were read from itsmodeling_unlimitedocr.py,chat_template.jinjaandpreprocessor_config.jsonat8249d20b.<image>) requests were not run. They go through the same processor and template.