Skip to content

fix: bind gemma-4 NVFP4 vision tower so image input works - #750

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-749-nvfp4-vision-key-remap
Jul 12, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-749-nvfp4-vision-key-remap

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

The ModelOpt NVFP4 export gemma-4-31b-it-nvfp4 loaded and generated on the text path but rejected every image request with Error: Images provided but model is not a vision-language model. Its vision front-end is nested under a leading model. prefix (model.vision_tower.*, model.embed_vision.*) that two loader stages never handled, so the checkpoint was misdetected as text-only and, even on the VLM path, the vision tower found no weights to bind. This binds the vision tower so the model serves image input like its 4-bit sibling.

Root cause

The safetensors index for this checkpoint holds 1728 tensors: 1372 model.language_model.* (quantized text decoder), 355 model.vision_tower.*, and 1 model.embed_vision.embedding_projection.weight. Two stages assumed the MLX-community unprefixed names: gemma4_has_vision_weights scanned the index only for vision_tower. / embed_vision., so the checkpoint matched neither and routed to ModelType::Gemma4 (text) instead of Gemma4VLM; and normalize_nvfp4_keys remapped the text decoder and model.embed_vision. but had no model.vision_tower. branch, so the 355 vision-tower tensors stayed under model. and never bound. The vision front-end is unquantized (ModelOpt keeps model.vision_tower* / model.embed_vision* in quantization_config.ignore, and the tensors carry no weight_scale_2 sidecar), so it needs key renaming only, not an NVFP4 transcode.

What changed

  • src/models/sanitize.rs: normalize_nvfp4_keys now strips model.vision_tower. -> vision_tower. alongside the existing text-decoder / model.embed_vision. / model.lm_head. remaps, and reports the full remapped count (1728 for this checkpoint, previously 1372) instead of just the language-model subset. repack_nvfp4_weights_to_quantized leaves the vision tensors alone because they carry no weight_scale_2.
  • src/models/detection.rs: gemma4_has_vision_weights also recognizes the model.-prefixed vision keys, so an NVFP4 multimodal export routes to Gemma4VLM.
  • src/loading/vlm_gemma.rs: load_gemma4_vlm runs sanitize_gemma4_nvfp4_weights after loading so the text decoder remaps and repacks and the vision keys remap on the VLM path. This is a no-op for MLX-community Gemma 4 VLM checkpoints (no model.language_model. keys and no weight_scale_2 sidecars, so both inner stages skip immediately).
  • src/models/mod.rs: re-export sanitize_gemma4_nvfp4_weights for the VLM loader.
  • Unit test normalize_nvfp4_keys_remaps_vision_tower_and_embed_vision covers the vision-key remap (the .linear.weight projection nesting, the embed_vision projection, and the bare std_bias / position_embedding_table tensors).
  • Docs: GB10 VLM benchmark table row is now a pass, and the #749 notes in model_tests_gb10.md and model_tests.md describe the fix. The single-model VLM benchmark CSV is committed as evidence.

Validation (GB10, CUDA, SM 12.1)

Before (from the issue repro):

Error: Images provided but model is not a vision-language model

After, image path (generate -m models/gemma-4-31b-it-nvfp4 -p "What is in this image?" --image tests/fixtures/test_image.png -n 32):

Remapping 1728 NVFP4-style weight keys to MLX-community convention...
Repacking 180 ModelOpt NVFP4 weight groups to MLX native NVFP4 via direct triplet transcode...
Model loaded in 58.559s (resident: 30.39 GB, peak: 34.34 GB).
Loaded 1 image(s).
Gemma4: expanded 1 image slot(s) into dynamic soft tokens (278 total tokens)
This image consists of a solid, flat background divided into two colors: a dark grey area on the top and a burnt orange/terracotta area on the

After, text-only path (generate -m models/gemma-4-31b-it-nvfp4 -p "Hello" -n 8) still works: Hello! How can I help you today (4.93 tok/s).

Benchmark row (scripts/bench_decode.sh models/gemma-4-31b-it-nvfp4 --vlm --cooldown 0), added to the GB10 VLM table: gemma-4-31b-it-nvfp4 pass, 278 prompt tokens, 27 generated tokens, 353.20 prefill tok/s, 4.87 decode tok/s.

Out of scope

Audio weights are absent from this export (0 tensors match audio despite config.json carrying an audio_config), so the audio tower is not loaded and is out of scope for this fix.

Test plan

  • cargo test --lib models::sanitize --features cuda (70 passed, including the new vision-remap test)
  • cargo clippy --lib --tests --features cuda -- -D warnings (clean)
  • Real-model image + text validation on GB10 (outputs above)

Closes #749

inureyes added 2 commits July 12, 2026 11:24
ModelOpt NVFP4 Gemma 4 exports (e.g. gemma-4-31b-it-nvfp4) loaded and generated on the text path but rejected every image request with "Images provided but model is not a vision-language model". The vision front-end is nested under a leading `model.` prefix (`model.vision_tower.*`, `model.embed_vision.*`), which two loader stages never accounted for: `gemma4_has_vision_weights` scanned the safetensors index only for the unprefixed `vision_tower.` / `embed_vision.` names, so the checkpoint was misdetected as text-only and routed to Gemma4 instead of Gemma4VLM; and `normalize_nvfp4_keys` remapped the text decoder and `model.embed_vision.` but had no `model.vision_tower.` branch, so even on the VLM path the 355 vision-tower tensors stayed under `model.` and the vision tower found no weights to bind.

Three coordinated changes:

- `normalize_nvfp4_keys` (sanitize.rs) now strips `model.vision_tower.` -> `vision_tower.` alongside the existing text-decoder and embed_vision remaps, and reports the full remapped count (1728 for this checkpoint, was 1372) instead of just the language-model subset. The vision front-end is unquantized (ModelOpt keeps `model.vision_tower*` / `model.embed_vision*` in `quantization_config.ignore` and the tensors carry no `weight_scale_2`), so it needs key renaming only; `repack_nvfp4_weights_to_quantized` leaves it untouched.
- `gemma4_has_vision_weights` (detection.rs) also recognizes the `model.`-prefixed vision keys, so an NVFP4 multimodal export routes to Gemma4VLM.
- `load_gemma4_vlm` (vlm_gemma.rs) runs `sanitize_gemma4_nvfp4_weights` after loading, so the text decoder remaps and repacks and the vision keys remap on the VLM path. This is a no-op for MLX-community Gemma 4 VLM checkpoints (no `model.language_model.` keys, no `weight_scale_2` sidecars). The re-export is added in models/mod.rs.

Added a unit test covering the vision-key remap (vision_tower `.linear.weight` nesting, embed_vision projection, and the bare `std_bias` / `position_embedding_table` tensors).

Validated on GB10 (CUDA, SM 12.1) against the local gemma-4-31b-it-nvfp4: the image prompt now generates a coherent caption and the loader logs "Remapping 1728 NVFP4-style weight keys"; the text-only path still generates normally. Audio weights are absent from this export (0 audio tensors despite `audio_config`), so the audio tower is out of scope.

Refs #749
Replace the FAIL row for gemma-4-31b-it-nvfp4 in the GB10 VLM benchmark table with the measured pass (27 generated tokens, 353.20 prefill / 4.87 decode tok/s) and rewrite the accompanying notes in model_tests_gb10.md (VLM-table note and Summary notable-changes) and model_tests.md (CSV inventory and Overall Status) to state that the image-input failure is fixed by #749 rather than an open finding. The text-table note for the model now reads "image input works (see VLM note; fixed by #749)".

Commit the single-model VLM benchmark CSV as evidence: `benchmarks/cuda_gb10_vlm_2026-07-12_single_gemma-4-31b-it-nvfp4.csv`.

Refs #749
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata status:review Under review labels Jul 12, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Verification note: the touched-module test suites all pass on this branch (models::sanitize 70, loading:: 211, multimodal:: 132; clippy --lib --tests clean; real-model image and text generation validated on GB10). The broader cargo test --lib models:: run aborts on this host with non-deterministic CUDA graph-capture errors (cuGraphAddKernelNode invalid argument on one run, cudaStreamEndCapture previous-error-during-capture on the next). This is the known GB10 post-benchmark-day driver signature first recorded on 2026-07-08 (it reproduces on every branch after a heavy bench day while scoped tests pass); today the host ran the full 159-model sweep. The PR changes are pure key-string remapping and model-type routing with no CUDA code paths, so the aborts are attributed to host driver state, not this change.

gemma4_has_vision_weights gained a branch to recognize the model.vision_tower. / model.embed_vision. prefixed keys that ModelOpt NVFP4 exports use (issue #749), but detection_tests.rs only exercised the pre-existing unprefixed vision_tower. case. Add a test that builds a minimal safetensors index with each prefixed key and asserts get_model_type still routes to Gemma4VLM, so a regression in the new branch fails a fast unit test instead of only surfacing on a real checkpoint.

Validation:
- cargo test --lib models::detection --features cuda (20 passed)
- cargo clippy --lib --tests --features cuda -- -D warnings (clean)
- cargo fmt --check on the touched file (clean)

Refs #749
@inureyes inureyes added status:done Completed and removed status:review Under review labels Jul 12, 2026
@inureyes
inureyes merged commit 3fa7cc1 into main Jul 12, 2026
4 checks passed
@inureyes
inureyes deleted the fix/issue-749-nvfp4-vision-key-remap branch July 12, 2026 02:41
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
## Summary

The ModelOpt NVFP4 export `gemma-4-31b-it-nvfp4` loaded and generated on the text path but rejected every image request with `Error: Images provided but model is not a vision-language model`. Its vision front-end is nested under a leading `model.` prefix (`model.vision_tower.*`, `model.embed_vision.*`) that two loader stages never handled, so the checkpoint was misdetected as text-only and, even on the VLM path, the vision tower found no weights to bind. This binds the vision tower so the model serves image input like its 4-bit sibling.

## Root cause

The safetensors index for this checkpoint holds 1728 tensors: 1372 `model.language_model.*` (quantized text decoder), 355 `model.vision_tower.*`, and 1 `model.embed_vision.embedding_projection.weight`. Two stages assumed the MLX-community unprefixed names: `gemma4_has_vision_weights` scanned the index only for `vision_tower.` / `embed_vision.`, so the checkpoint matched neither and routed to `ModelType::Gemma4` (text) instead of `Gemma4VLM`; and `normalize_nvfp4_keys` remapped the text decoder and `model.embed_vision.` but had no `model.vision_tower.` branch, so the 355 vision-tower tensors stayed under `model.` and never bound. The vision front-end is unquantized (ModelOpt keeps `model.vision_tower*` / `model.embed_vision*` in `quantization_config.ignore`, and the tensors carry no `weight_scale_2` sidecar), so it needs key renaming only, not an NVFP4 transcode.

## What changed

- `src/models/sanitize.rs`: `normalize_nvfp4_keys` now strips `model.vision_tower.` -> `vision_tower.` alongside the existing text-decoder / `model.embed_vision.` / `model.lm_head.` remaps, and reports the full remapped count (1728 for this checkpoint, previously 1372) instead of just the language-model subset. `repack_nvfp4_weights_to_quantized` leaves the vision tensors alone because they carry no `weight_scale_2`.
- `src/models/detection.rs`: `gemma4_has_vision_weights` also recognizes the `model.`-prefixed vision keys, so an NVFP4 multimodal export routes to `Gemma4VLM`.
- `src/loading/vlm_gemma.rs`: `load_gemma4_vlm` runs `sanitize_gemma4_nvfp4_weights` after loading so the text decoder remaps and repacks and the vision keys remap on the VLM path. This is a no-op for MLX-community Gemma 4 VLM checkpoints (no `model.language_model.` keys and no `weight_scale_2` sidecars, so both inner stages skip immediately).
- `src/models/mod.rs`: re-export `sanitize_gemma4_nvfp4_weights` for the VLM loader.
- Unit test `normalize_nvfp4_keys_remaps_vision_tower_and_embed_vision` covers the vision-key remap (the `.linear.weight` projection nesting, the embed_vision projection, and the bare `std_bias` / `position_embedding_table` tensors).
- Docs: GB10 VLM benchmark table row is now a pass, and the `lablup#749` notes in `model_tests_gb10.md` and `model_tests.md` describe the fix. The single-model VLM benchmark CSV is committed as evidence.

## Validation (GB10, CUDA, SM 12.1)

Before (from the issue repro):

```
Error: Images provided but model is not a vision-language model
```

After, image path (`generate -m models/gemma-4-31b-it-nvfp4 -p "What is in this image?" --image tests/fixtures/test_image.png -n 32`):

```
Remapping 1728 NVFP4-style weight keys to MLX-community convention...
Repacking 180 ModelOpt NVFP4 weight groups to MLX native NVFP4 via direct triplet transcode...
Model loaded in 58.559s (resident: 30.39 GB, peak: 34.34 GB).
Loaded 1 image(s).
Gemma4: expanded 1 image slot(s) into dynamic soft tokens (278 total tokens)
This image consists of a solid, flat background divided into two colors: a dark grey area on the top and a burnt orange/terracotta area on the
```

After, text-only path (`generate -m models/gemma-4-31b-it-nvfp4 -p "Hello" -n 8`) still works: `Hello! How can I help you today` (4.93 tok/s).

Benchmark row (`scripts/bench_decode.sh models/gemma-4-31b-it-nvfp4 --vlm --cooldown 0`), added to the GB10 VLM table: `gemma-4-31b-it-nvfp4` pass, 278 prompt tokens, 27 generated tokens, 353.20 prefill tok/s, 4.87 decode tok/s.

## Out of scope

Audio weights are absent from this export (0 tensors match audio despite `config.json` carrying an `audio_config`), so the audio tower is not loaded and is out of scope for this fix.

## Test plan

- [x] `cargo test --lib models::sanitize --features cuda` (70 passed, including the new vision-remap test)
- [x] `cargo clippy --lib --tests --features cuda -- -D warnings` (clean)
- [x] Real-model image + text validation on GB10 (outputs above)

Closes lablup#749
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 priority:high High 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: NVFP4 direct transcode leaves gemma-4 vision-tower keys unmapped, so gemma-4-31b-it-nvfp4 rejects image input

1 participant