Skip to content

fix(vision): normalize DeepSeek-OCR CLIP encoder layers at eps 1e-5 - #3740

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/deepseekocr-clip-layernorm-eps
Dec 4, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/deepseekocr-clip-layernorm-eps

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #3737.

DeepSeek-OCR's CLIP-L encoder layers (layer_norm1 / layer_norm2 in each of the 24 layers) normalized at eps 1e-6. The checkpoint's own deepencoder.py builds them with layernorm_epsilon = 1e-5, and mlx-vlm moved deepseekocr/config.py VisionConfig.layer_norm_eps from 1e-6 to 1e-5 in Neywa-Labs/mlx-vlm@c6a0d788. config.json declares no eps, so the default decides. #3456 left DeepSeek-OCR at 1e-6 until the reference moved ("Whether DeepSeek-OCR should move to 1e-5 is a question for the reference first"); it now has.

What changed

  • src/vision/encoders/deepseekocr_clip.rs: ClipConfig::default() sets layer_norm_eps: 1e-5, with a comment naming both sources; the pre_layrnorm comment no longer says the encoder layers use 1e-6 for DeepSeek-OCR. The default is the DeepSeek-OCR encoder's configuration, so the loader and the encoder parity test both follow it.
  • src/loading/vlm_deepseekocr.rs: deepseekocr_clip_config() still returns ClipConfig::default(); its doc comment now says 1e-5 and why. unlimited_ocr_clip_config() keeps its explicit layer_norm_eps: 1e-5, now equal to DeepSeek-OCR's; its doc comment says so and that it stays explicit because each follows its own upstream config.py.
  • src/loading/vlm_deepseekocr_tests.rs: deepseekocr_clip_keeps_layer_norm_eps_1e6 becomes deepseekocr_clip_uses_layer_norm_eps_1e5, asserting both deepseekocr_clip_config() and ClipConfig::default(). The Unlimited-OCR test's "Only the eps differs" comment is reworded, since nothing differs now.
  • tests/deepseekocr_encoder_parity.rs: the CLIP reference signature moves from 0.13067 to 0.13060, the value scripts/deepseekocr_encoder_reference.py prints under mlx-vlm c6a0d788 (it prints 0.13067 on the parent commit, c745aa70, at eps 1e-6). The tolerance and the quick-GELU / erf-GELU note are unchanged.
  • docs/supported-models.md (Unlimited-OCR entry): no longer lists the eps as a difference from DeepSeek-OCR.

Other callers of ClipConfig::default() and the CLIP encoder

ClipConfig / ClipEncoder (src/vision/encoders/deepseekocr_clip.rs) are built in exactly these places:

  • load_deepseekocr_vlm via deepseekocr_clip_config(): changes, as intended.
  • load_unlimited_ocr_vlm via unlimited_ocr_clip_config(): already 1e-5, unchanged.
  • tests/deepseekocr_encoder_parity.rs: follows the default, reference updated.
  • src/vision/deepseekocr_batched_tests.rs, deepseekocr_clip_quant_tests.rs, unlimited_ocr_tests.rs: synthetic encoders that spell out every field, including their own eps; unchanged.

DeepSeek-OCR 2 has no CLIP stage (its SAM output feeds a Qwen2 query resampler) and GOT-OCR 2.0 has no CLIP encoder; neither builds a ClipConfig, so neither changes. The SAM encoder (deepseekocr_sam.rs) is a separate module with its own norms and is not touched.

Verification

NVIDIA GB10 (sm_121), driver 580.178.04, CUDA 13.0.88, Linux 7.0.0-1019-nvidia aarch64, rustc 1.97.1, --features cuda, --profile test-fast, MLXCEL_SDPA_DETERMINISTIC=1. Base 9402cf893. Commit tree d0af87b7, which differs from tree 0a2a3241 only in CHANGELOG.md (an entry since removed). The full test suite and the traces below ran on tree 39193167, which differs from 0a2a3241 only in the wording of one code comment in src/vision/encoders/deepseekocr_clip.rs (git diff 39193167 0a2a3241); cargo fmt --check, clippy and the targeted tests below were re-run on 0a2a3241. Reference: mlx-vlm c6a0d788 (Blaizzy/mlx-vlm), mlx 0.32.5 (CUDA), transformers 5.24.1. Checkpoint mlx-community/DeepSeek-OCR-4bit, revision 4958b6b0d46d7dc3cf27556989dab659996ea426.

Tests

cargo test --locked --profile test-fast --features cuda --lib -- --test-threads=1 deepseekocr clip unlimited_ocr got_ocr gelu:

main + the new tests fix
loading::vlm::deepseekocr::tests::deepseekocr_clip_uses_layer_norm_eps_1e5 FAIL: left: 1e-6, right: 1e-5 pass
the five filters together 177 passed, 1 failed, 3 ignored 178 passed, 0 failed, 3 ignored

Re-run on tree 0a2a3241: 178 passed, 0 failed, 3 ignored, and the encoder parity test below passes with the same signatures (CLIP 0.13072, SAM 0.04304).

MLXCEL_REQUIRE_MODELS=1 cargo test ... --test deepseekocr_encoder_parity -- --nocapture with the checkpoint linked at models/deepseek-ocr-4bit: passes on both arms. CLIP output absmean 0.13053 on main, 0.13072 with the fix; SAM 0.04304 on both. scripts/deepseekocr_encoder_reference.py prints CLIP 0.13060 / SAM 0.04305 under mlx-vlm c6a0d788 and CLIP 0.13067 / SAM 0.04305 under its parent c745aa70 (eps 1e-6). The absmean is a coarse bf16 signature: both mlxcel arms are within 0.0002 of both references, so it does not separate them, and the test's tolerance (0.005) passes either eps; the reference value is updated for information only.

Real checkpoint: generation and teacher-forced traces

The two 1920x1280 pages #3456 used (a mixed text/table page, "mixed" below, and a dense page of alphanumeric codes, "dense"), prompt Free OCR., binaries from main and from the fix (different sha256):

  • mlxcel generate -m <dir> --image <page> -p "Free OCR." -n 160 --temp 0: the reply is byte-identical between main and the fix on both pages.
  • examples/logit_trace (MLXCEL_TRACE_IMAGE), teacher-forced over the fix's reply, 128 positions, on both arms; scripts/mlx_vlm_image_logit_trace.py --mlxcel-prompt with mlx-vlm c6a0d788 scoring the same prompt ids (identical to mlxcel's on every run) and targets, in float32 (SAM run one image at a time) and in bf16; scripts/compare_logit_traces.py --decided 2:
page reference arm top-1 disagreements (decided) logit delta on the reference's choice p50 / p99 / max
mixed f32 main 1/128 (0/123) 0.086 / 0.491 / 0.605
mixed f32 fix 1/128 (0/123) 0.092 / 0.550 / 0.613
mixed bf16 main 1/128 (0/123) 0.125 / 0.500 / 0.625
mixed bf16 fix 1/128 (0/123) 0.125 / 0.500 / 0.625
dense f32 main 0/128 (0/125) 0.064 / 0.520 / 0.586
dense f32 fix 0/128 (0/125) 0.082 / 0.389 / 0.461
dense bf16 main 0/128 (0/125) 0.125 / 0.500 / 0.625
dense bf16 fix 0/128 (0/125) 0.125 / 0.375 / 0.500

The one disagreement on the mixed page is the same undecided position on both arms (reference gap 0.026, the reference's choice ranked 2nd). main against the fix directly: 0/128 top-1 disagreements on both pages, logit delta max 0.375 and 0.625.

Against the f32 reference the fix is slightly worse on the mixed page at every percentile (p50 0.086 -> 0.092, p99 0.491 -> 0.550, max 0.605 -> 0.613) and on the dense page's median (0.064 -> 0.082), and better on the dense page's tail (p99 0.520 -> 0.389, max 0.586 -> 0.461). Against the bf16 reference the mixed page is unchanged and only the dense page's p99 and max improve. Each of these moves is within about one bf16 step at these logit magnitudes (0.125 between neighbouring bf16 values here), so the traces cannot tell in which direction the eps change moves parity. The case for the change is the reference definition (deepencoder.py and mlx-vlm both use 1e-5), not a measured parity gain.

Encoder output, element-wise

The encoder parity test's absmean cannot separate the two eps values, so the CLIP output was also compared element by element on the same deterministic input as tests/deepseekocr_encoder_parity.rs, against mlx-vlm at eps 1e-6 (c745aa70, the parent of c6a0d788) and at eps 1e-5 (c6a0d788). Both mlxcel arms are ClipConfig::default() with only layer_norm_eps set to 1e-6 (main) or 1e-5 (fix), built from the commit's tree; outputs are bf16, compared in f32 (a throwaway test and script, not part of this PR). The two references' SAM outputs are identical, so the CLIP stage is also compared with the reference's SAM output fed to mlxcel's CLIP, which isolates the stage the eps acts on.

CLIP input mlxcel arm vs reference max abs mean abs p99 abs relative L2
reference SAM output main (1e-6) 1e-6 0.146 0.00552 0.0410 0.0542
reference SAM output main (1e-6) 1e-5 0.246 0.00569 0.0454 0.0597
reference SAM output fix (1e-5) 1e-6 0.198 0.00562 0.0449 0.0575
reference SAM output fix (1e-5) 1e-5 0.211 0.00537 0.0409 0.0542
mlxcel's own SAM output main (1e-6) 1e-6 0.531 0.00841 0.0771 0.1029
mlxcel's own SAM output main (1e-6) 1e-5 0.342 0.00814 0.0723 0.0939
mlxcel's own SAM output fix (1e-5) 1e-6 0.441 0.00851 0.0742 0.0985
mlxcel's own SAM output fix (1e-5) 1e-5 0.418 0.00817 0.0708 0.0929

For scale: the eps change alone moves the reference's CLIP output by relative L2 0.0396 (max 0.231) and mlxcel's by 0.0503 (max 0.211); mlxcel's SAM output differs from the reference's by relative L2 0.0068.

With the reference's SAM output as input, each mlxcel arm is closer, by relative L2, mean and p99, to the reference with the same eps than to the other one, and against the current reference (1e-5) the fix is closer than main on all four measures (relative L2 0.0597 -> 0.0542, max 0.246 -> 0.211). With mlxcel's own SAM output the end-to-end comparison is mixed: against the 1e-5 reference the fix improves relative L2 and p99 slightly but has a larger max (0.342 -> 0.418) and about the same mean. Each configuration was run once.

Checks

  • cargo fmt --all -- --check
  • cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings
  • python3 scripts/ci/check_used_by.py, bash scripts/ci/check_used_by_test.sh (all cases passed)
  • make verify-test-cuda, tree 39193167 before and after: 18,891 passed, 2 failed, 578 ignored over 207 test result lines. Both failures were pre-existing on the base 9402cf893 and are unrelated to this change; current main fixes both (feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262) #3686, test: restore MLX guard scan and no-GPU catalog test gates #3765): tests/mlx_test_guard_coverage.rs (src/vision/encoders/nemotron_h_nano_omni_tests.rs:276 pos_embed_resample_runs_in_f32 lacks the guard) and tests/step3p7_parity.rs text_decode_delegates_sequence_local_native_batching (assertion failed: !wrapper.supports_padded_prefill()). On unmodified main (9402cf893), cargo test --locked --profile test-fast --features cuda --test mlx_test_guard_coverage --test step3p7_parity -- --test-threads=1 fails both the same way (5 passed / 1 failed and 7 passed / 1 failed, same messages).

git merge-tree merges the commit onto current origin/main (5bf7b8628) without conflict. On the merged tree, the Python tooling tests (python3 -m unittest discover -s tests -p 'test_*.py'), the scripts/ci checks CI runs and cargo fmt --all -- --check pass.

Limits

  • No measured parity gain on the two traced pages (see above); the logit traces only show the change is within the existing rounding. At the encoder output the eps difference is resolvable when the CLIP stage is fed the reference's SAM output, and there the fix tracks the 1e-5 reference more closely than main; end to end with mlxcel's own SAM output the comparison is mixed. One deterministic synthetic input, one run per configuration.
  • The mlx-vlm reference ran with mlx-vlm c6a0d788 on transformers 5.24.1. The checkpoint's remote code (modeling_deepseekv2.py, imported by transformers while resolving the tokenizer through auto_map) does not import on transformers 5 (LlamaFlashAttention2 is gone), so the reference loaded the same weights from a directory whose config.json has auto_map removed (all other files are links to the checkpoint). That only changes how transformers resolves the tokenizer class; mlx-vlm's own DeepseekOCRProcessor and model code run as usual.
  • The other parts of mlx-vlm c6a0d788 (DeepSeek-OCR inference_mode truncation, Unlimited-SVG routing, SAM global-attention chunking) are not ported here.
  • Unlimited-OCR was not re-traced: its eps was already 1e-5 and its code path is unchanged.
  • Not run on Metal.

@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority area:models Model architectures, weights, loading, metadata modeltype:vlm Vision-language model labels Dec 4, 2026
@Jaeyeong-CHOI
Jaeyeong-CHOI force-pushed the fix/deepseekocr-clip-layernorm-eps branch from 23d2efe to d6a96f8 Compare December 4, 2026 06:23
mlx-vlm builds the DeepSeek-OCR and Unlimited-OCR `VisionConfig` with `from_dict`, so a `vision_config.layer_norm_eps` in `config.json` overrides the 1e-5 default. The loaders hardcoded the eps and ignored a declared value. Both CLIP config builders now take the full config and use a declared positive finite `layer_norm_eps`, falling back to 1e-5; the released checkpoints declare none, so their behavior is unchanged.

A new test pins the override and the fallback for zero, negative, string and null values, and the 1e-5 test now also covers the released `vision_config` shape.

Refs lablup#3737
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Maintainer hardening pass, reviewed against #3737.

Verified against mlx-vlm c6a0d788: deepseekocr/config.py VisionConfig.layer_norm_eps is 1e-5 and feeds only layer_norm1 / layer_norm2 in each CLIP layer. pre_layrnorm is nn.LayerNorm(hidden) at the MLX default 1e-5 and stays fixed; the CLIP vision tower has no post_layernorm. unlimited_ocr/config.py is also 1e-5, and DeepSeek-OCR 2 (no CLIP stage) is untouched. Changing ClipConfig::default() rather than overriding in the loader is fine: the only callers of the default are the DeepSeek-OCR loader, the Unlimited-OCR loader (which sets eps explicitly) and the encoder parity test; the synthetic test encoders spell out every field.

Fix pushed (cd3e84d): mlx-vlm builds VisionConfig with from_dict, so a vision_config.layer_norm_eps in config.json overrides the default. Both loaders now honor a declared positive finite value and fall back to 1e-5; the released checkpoints declare none, so behavior there is this PR's. A new test pins the override and the fallback for invalid values. Also merged current main.

Validation (GB10, CUDA):

  • cargo clippy --all-targets -- -D warnings on default features and --features cuda; cargo fmt --check.
  • Lib tests filtered on deepseekocr clip unlimited_ocr: 135 passed. deepseekocr_encoder_parity with MLXCEL_REQUIRE_MODELS=1 and mlx_test_guard_coverage pass (CLIP absmean 0.13052, SAM 0.04302).
  • mlxcel generate on mlx-community/DeepSeek-OCR-4bit, Free OCR., 160 tokens, temp 0, on tests/fixtures/got_ocr_page.png and tests/fixtures/nemotron_parse_page.png, comparing the released config (1e-5) with a copy declaring layer_norm_eps: 1e-6: the generated text is identical on both pages, consistent with the PR's finding that the change is within rounding at the output.

@inureyes
inureyes merged commit 4517a2a into lablup:main Dec 4, 2026
36 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 4, 2026
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 modeltype:vlm Vision-language model priority:low Low priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(vision): DeepSeek-OCR CLIP encoder normalizes at eps 1e-6 where the checkpoint and mlx-vlm use 1e-5

2 participants