Repository navigation
fix(vision): clamp Molmo2 image features before the float16 merge - #2969
Conversation
An image prompt on mlx-community/Molmo2-8B-4bit answered with token 0
("!") at every position, while a text-only prompt was fine. The
checkpoint's floats are float16. The vision tower runs in float32, and
two of its 1,605,632 projected values for a one-image prompt are about
8.2e4, past the float16 maximum of 65504. get_input_embeddings cast the
features to the text dtype, which made those two values +inf, and the
one-hot matmul that scatters the features multiplied them by the zeros of
every other row: NaN in one hidden dimension of all 436 tokens, NaN
logits, argmax 0.
mlx-vlm clamps the tower's output at this point (clip_image_features,
Neywa-Labs/mlx-vlm#1165): NaN to zero, and +-65000 for float16. Port that
clamp, keyed on the dtype the features are added in, and move the merge
into merge_image_features so it can be tested on synthetic arrays. Values
inside the range merge bit for bit as before.
On GB10 the same prompts now describe both fixtures, and the merged
embeddings go from 2 infinities and 436 NaNs to none.
|
Implementation hardening for #2969. Reviewed #2968: image features are sanitized and clamped before the narrowing cast and embedding merge. The dtype-specific bounds preserve ordinary values and prevent nonfinite values from contaminating unrelated text rows. No additional security or performance defect was found. Validation: The five Molmo2 merge regressions passed on the combined CUDA candidate tree, covering float16 overflow, in-range identity, wide dtypes, and already-narrow input. A full Molmo2-8B checkpoint inference was not re-run. Commands: |
Summary
An image prompt on
mlx-community/Molmo2-8B-4bitanswered with token 0 (!) at every position, while a text-only prompt on the same checkpoint was fine. The checkpoint's float tensors are float16, two values of the vision tower's output are above the float16 maximum, and the merge cast them to the text dtype and scattered them with a one-hot matmul, which spreads the resultinginfover the whole sequence as NaN. This ports the clamp mlx-vlm applies at that point (Neywa-Labs/mlx-vlm#1165).Related issues
Closes #2968.
Type of change
fix— bug fixCause
Statistics from
Molmo2VLModel::get_input_embeddingswith temporary instrumentation (never committed), once6855bc8and on this change, same prompt (436 tokens, 392 of them image patches,tests/fixtures/test_image_shapes.png):main+infone_hot @ image_featuressrc/vision/molmo2_vl.rscast the tower's float32 output to the text dtype and added it withmatmul(one_hot, image_features).0 * infis NaN, so a single overflowed value makes its hidden dimension NaN in every row, image patch or not: 436 NaN is one column of the 436-token sequence. NaN logits, argmax 0, and token 0 of this vocabulary is!. The second fixture (test_image.png) reads the same with a largest value of 80604.72.The projector has no output norm, and the Hugging Face model adds the features in the model dtype, which is float32 in the original config. mlx-vlm clamps the tower's output before the add since Neywa-Labs/mlx-vlm#1165 (
clip_image_features: NaN to zero, ±65000 for float16, the dtype's largest value otherwise).Change
image_feature_clip_limitandclip_image_features: mlx-vlm's clamp, keyed on the dtype the features are added in. Here the tower output is float32 and is cast afterwards; mlx-vlm runs the tower in the weight dtype and clamps there. Both end at ±64992 in float16 (65000 rounded to the float16 grid).merge_image_features: the clamp, the cast and the existing one-hot scatter, moved out ofget_input_embeddingsso the merge can be tested on synthetic arrays. The scatter is unchanged, and values inside the range merge bit for bit as before.Before / after
GB10 (sm_121, CUDA 13.0),
generate --image <fixture> -p "Describe the image." -n 48 --temp 0, release builds:mainis the pinned28d884a5binary, which answers the same asce6855bc8.mainmlx-community/Molmo2-8B-4bit@4fcbe9265776(floats F16)test_image_shapes.png!× 48test_image.png!× 48Aliyovic/molmo2-4b-mlx-8bit@7fdd4d51c51c(floats F32, largest tower value 27412)The shapes fixture is a red square (top left), a blue circle (top right) and a green triangle (bottom) on light gray; the other is a solid orange square. A text-only prompt on the 8B checkpoint answers "Hello!" before and after.
Test plan
Run on this PR's commit (
262c09f55, one commit once6855bc8) on a DGX Spark (GB10).cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warnings(the Linux spelling of the template's line)cargo build --release --features cudacargo test --profile test-fast --features cuda --lib vision::molmo2_vl -- --test-threads=1: 5 passed. With the clamp removed, 3 of the 5 fail, among thema_feature_past_the_float16_maximum_stays_finite_and_localcargo test --profile test-fast --features cuda --lib molmo2 -- --test-threads=1: 15 passedmake verify-test-cuda(cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 186 of 187 test binaries pass (15491 tests passed, 1 failed, 496 ignored). The one failure isnative_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a cleanmain(it is order-dependent; described in the notes of perf(cuda): fuse the f32 tanh GELU into one bit-exact kernel (#2780) #2967)ci.ymljobs a pull request from a fork does not get and this change can reach, run by hand:clippy(cargo clippy -p mlxcel --lib --tests -- -D warnings, default features) andxla-compile(cargo check --features cuda,xla-iree --all-targets,cargo check --no-default-features --features xla-diagnostics --all-targets): all three passmake verify-python-tooling, license headers, cross-repo refs: 219 Python tests OK, license headers OK, the one cross-repository reference this adds is qualified (Blaizzy/mlx-vlm#1165)features_inside_the_range_merge_exactly_as_before), and the 4B checkpoint's replies are identicalcargo deny check: not run and not applicable (no manifest, lockfile ordeny.tomlchange)make verifyNotes for reviewers
mlx-community/Molmo2-8B-*conversions were not run. Only the 4-bit checkpoint was.mlx_vlm/models/molmo2/molmo2.py) and not its reply.Checklist
docs/: no user-facing behavior or supported-model change beyond the fix// Used by: ...comments on the functions I added.envfiles committed