Repository navigation
fix(vision): normalize DeepSeek-OCR CLIP encoder layers at eps 1e-5 - #3740
Open
Jaeyeong-CHOI wants to merge 1 commit into
Open
Jaeyeong-CHOI wants to merge 1 commit into
Jaeyeong-CHOI wants to merge 1 commit into
Conversation
This branch has not been deployed
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 #3737.
DeepSeek-OCR's CLIP-L encoder layers (
layer_norm1/layer_norm2in each of the 24 layers) normalized at eps 1e-6. The checkpoint's owndeepencoder.pybuilds them withlayernorm_epsilon = 1e-5, and mlx-vlm moveddeepseekocr/config.pyVisionConfig.layer_norm_epsfrom 1e-6 to 1e-5 in Neywa-Labs/mlx-vlm@c6a0d788.config.jsondeclares 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()setslayer_norm_eps: 1e-5, with a comment naming both sources; thepre_layrnormcomment 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 returnsClipConfig::default(); its doc comment now says 1e-5 and why.unlimited_ocr_clip_config()keeps its explicitlayer_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 upstreamconfig.py.src/loading/vlm_deepseekocr_tests.rs:deepseekocr_clip_keeps_layer_norm_eps_1e6becomesdeepseekocr_clip_uses_layer_norm_eps_1e5, asserting bothdeepseekocr_clip_config()andClipConfig::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 valuescripts/deepseekocr_encoder_reference.pyprints 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.CHANGELOG.md:## [Unreleased]/### Fixedentry.Other callers of
ClipConfig::default()and the CLIP encoderClipConfig/ClipEncoder(src/vision/encoders/deepseekocr_clip.rs) are built in exactly these places:load_deepseekocr_vlmviadeepseekocr_clip_config(): changes, as intended.load_unlimited_ocr_vlmviaunlimited_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. Base9402cf893. Commit tree0a2a3241. The full test suite and the traces below ran on tree39193167, which differs from0a2a3241only in the wording of one code comment insrc/vision/encoders/deepseekocr_clip.rs(git diff 39193167 0a2a3241);cargo fmt --check, clippy and the targeted tests below were re-run on0a2a3241. Reference: mlx-vlmc6a0d788(Blaizzy/mlx-vlm), mlx 0.32.5 (CUDA), transformers 5.24.1. Checkpointmlx-community/DeepSeek-OCR-4bit, revision4958b6b0d46d7dc3cf27556989dab659996ea426.Tests
cargo test --locked --profile test-fast --features cuda --lib -- --test-threads=1 deepseekocr clip unlimited_ocr got_ocr gelu:main+ the new testsloading::vlm::deepseekocr::tests::deepseekocr_clip_uses_layer_norm_eps_1e5left: 1e-6, right: 1e-5Re-run on the commit's 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 -- --nocapturewith the checkpoint linked atmodels/deepseek-ocr-4bit: passes on both arms. CLIP output absmean 0.13053 onmain, 0.13072 with the fix; SAM 0.04304 on both.scripts/deepseekocr_encoder_reference.pyprints CLIP 0.13060 / SAM 0.04305 under mlx-vlmc6a0d788and CLIP 0.13067 / SAM 0.04305 under its parentc745aa70(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 frommainand from the fix (different sha256):mlxcel generate -m <dir> --image <page> -p "Free OCR." -n 160 --temp 0: the reply is byte-identical betweenmainand 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-promptwith mlx-vlmc6a0d788scoring 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:mainmainmainmainThe one disagreement on the mixed page is the same undecided position on both arms (reference gap 0.026, the reference's choice ranked 2nd).
mainagainst 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.pyand 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 ofc6a0d788) and at eps 1e-5 (c6a0d788). Both mlxcel arms areClipConfig::default()with onlylayer_norm_epsset 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.main(1e-6)main(1e-6)main(1e-6)main(1e-6)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
mainon 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 -- --checkcargo clippy --locked --workspace --all-targets --features cuda -- -D warningspython3 scripts/ci/check_used_by.py,bash scripts/ci/check_used_by_test.sh(all cases passed)make verify-test-cuda, tree39193167before and after: 18,891 passed, 2 failed, 578 ignored over 207test resultlines. Both failures are onmainalready and unrelated to this change; test: fix two stale tests that keep make verify-test red #3720 fixes both:tests/mlx_test_guard_coverage.rs(src/vision/encoders/nemotron_h_nano_omni_tests.rs:276 pos_embed_resample_runs_in_f32lacks the guard) andtests/step3p7_parity.rstext_decode_delegates_sequence_local_native_batching(assertion failed: !wrapper.supports_padded_prefill()). On unmodifiedmain(9402cf893),cargo test --locked --profile test-fast --features cuda --test mlx_test_guard_coverage --test step3p7_parity -- --test-threads=1fails both the same way (5 passed / 1 failed and 7 passed / 1 failed, same messages).git merge-treemerges the commit onto currentorigin/main(9402cf893) without conflict.Limits
main; end to end with mlxcel's own SAM output the comparison is mixed. One deterministic synthetic input, one run per configuration.c6a0d788on transformers 5.24.1. The checkpoint's remote code (modeling_deepseekv2.py, imported by transformers while resolving the tokenizer throughauto_map) does not import on transformers 5 (LlamaFlashAttention2is gone), so the reference loaded the same weights from a directory whoseconfig.jsonhasauto_mapremoved (all other files are links to the checkpoint). That only changes how transformers resolves the tokenizer class; mlx-vlm's ownDeepseekOCRProcessorand model code run as usual.c6a0d788(DeepSeek-OCRinference_modetruncation, Unlimited-SVG routing, SAM global-attention chunking) are not ported here.