Skip to content

fix(vision): clamp Molmo2 image features before the float16 merge - #2969

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/molmo2-f16-image-feature-overflow
Nov 15, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/molmo2-f16-image-feature-overflow

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An image prompt on mlx-community/Molmo2-8B-4bit answered 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 resulting inf over 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 fix

Cause

Statistics from Molmo2VLModel::get_input_embeddings with temporary instrumentation (never committed), on ce6855bc8 and on this change, same prompt (436 tokens, 392 of them image patches, tests/fixtures/test_image_shapes.png):

Stage dtype main This PR
text embeddings float16 finite, max 0.8125 same
vision tower output float32 finite, max 81998.11, 2 of 1605632 values above 65504 same
features after the cast to the text dtype float16 2 values +inf finite, max 64992
one_hot @ image_features float16 436 NaN no NaN
merged embeddings float16 436 NaN no NaN, max 64992

src/vision/molmo2_vl.rs cast the tower's float32 output to the text dtype and added it with matmul(one_hot, image_features). 0 * inf is 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_limit and clip_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 of get_input_embeddings so the merge can be tested on synthetic arrays. The scatter is unchanged, and values inside the range merge bit for bit as before.
  • Molmo2 only. Molmo-Point merges in float32. Molmo v1 has the same cast-then-matmul shape, but mlx-vlm does not clamp there and I have no checkpoint that fails, so it is left as it is. The OpenXLA Molmo2 path prepares float32 embeddings and is not touched.

Before / after

GB10 (sm_121, CUDA 13.0), generate --image <fixture> -p "Describe the image." -n 48 --temp 0, release builds: main is the pinned 28d884a5 binary, which answers the same as ce6855bc8.

Checkpoint Fixture main This PR
mlx-community/Molmo2-8B-4bit @ 4fcbe9265776 (floats F16) test_image_shapes.png ! × 48 "The image features a minimalist composition with three distinct geometric shapes arranged against a light gray background. At the top left is a solid red square, while the top right contains a solid blue circle. Below these, centered at the bottom of the image"
same test_image.png ! × 48 "The image shows a large, solid orange square. It's a simple, minimalist design with no text, objects, or additional elements. The orange color is vibrant and uniform throughout the square, creating a bold and eye-catching visual. The square"
Aliyovic/molmo2-4b-mlx-8bit @ 7fdd4d51c51c (floats F32, largest tower value 27412) both a correct description identical reply

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 on ce6855bc8) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings (the Linux spelling of the template's line)
  • cargo build --release --features cuda
  • cargo 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 them a_feature_past_the_float16_maximum_stays_finite_and_local
  • cargo test --profile test-fast --features cuda --lib molmo2 -- --test-threads=1: 15 passed
  • make 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 is native_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a clean main (it is order-dependent; described in the notes of perf(cuda): fuse the f32 tanh GELU into one bit-exact kernel (#2780) #2967)
  • The self-hosted ci.yml jobs 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) and xla-compile (cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets): all three pass
  • make 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)
  • Validated with real checkpoints: the table above
  • Traced per "Judging a change that moves the numbers": the change moves numbers only where they were NaN. Values inside the range merge bit for bit (features_inside_the_range_merge_exactly_as_before), and the 4B checkpoint's replies are identical
  • cargo deny check: not run and not applicable (no manifest, lockfile or deny.toml change)
  • Metal: not built or run here. The change is backend-neutral array code; the new tests first run on Metal in the nightly make verify

Notes for reviewers

  • The clamp is the reference's behaviour, not the float32 original's. On this checkpoint two values per image go from about 8.2e4 to 64992. A float16 residual stream cannot hold the original values, and with the clamp the 8B answers correctly on both fixtures.
  • The other mlx-community/Molmo2-8B-* conversions were not run. Only the 4-bit checkpoint was.
  • mlx-vlm's output was not compared. Its CPU build on this host (mlx 0.32.4, mlx-vlm 0.7.12) had printed nothing for the first prompt after 31 minutes and was stopped, so the reference here is its source (mlx_vlm/models/molmo2/molmo2.py) and not its reply.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: no user-facing behavior or supported-model change beyond the fix
  • Updated // Used by: ... comments on the functions I added
  • No secrets, credentials, or .env files committed

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.
@inureyes inureyes added area:inference Generation, sampling, decoding (incl. speculative, DRY) modeltype:vlm Vision-language model priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions labels Nov 15, 2026
@inureyes

Copy link
Copy Markdown
Member

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: cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1 on the combined candidate tree has passed the affected module tests noted above; the broader run is still finishing. cargo fmt --all -- --check and python3 scripts/ci/check_kernel_dtype_keys.py passed. Required GitHub checks are checked again immediately before merge.

@inureyes
inureyes merged commit a43555c into lablup:main Nov 15, 2026
31 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 15, 2026
@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

area:inference Generation, sampling, decoding (incl. speculative, DRY) modeltype:vlm Vision-language model priority:medium Medium 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): Molmo2-8B float16 checkpoints emit only "!" on image prompts (unclamped vision features overflow to NaN)

2 participants