Repository navigation
fix: bind gemma-4 NVFP4 vision tower so image input works - #750
Conversation
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
|
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 |
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
## 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
Summary
The ModelOpt NVFP4 export
gemma-4-31b-it-nvfp4loaded and generated on the text path but rejected every image request withError: Images provided but model is not a vision-language model. Its vision front-end is nested under a leadingmodel.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), 355model.vision_tower.*, and 1model.embed_vision.embedding_projection.weight. Two stages assumed the MLX-community unprefixed names:gemma4_has_vision_weightsscanned the index only forvision_tower./embed_vision., so the checkpoint matched neither and routed toModelType::Gemma4(text) instead ofGemma4VLM; andnormalize_nvfp4_keysremapped the text decoder andmodel.embed_vision.but had nomodel.vision_tower.branch, so the 355 vision-tower tensors stayed undermodel.and never bound. The vision front-end is unquantized (ModelOpt keepsmodel.vision_tower*/model.embed_vision*inquantization_config.ignore, and the tensors carry noweight_scale_2sidecar), so it needs key renaming only, not an NVFP4 transcode.What changed
src/models/sanitize.rs:normalize_nvfp4_keysnow stripsmodel.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_quantizedleaves the vision tensors alone because they carry noweight_scale_2.src/models/detection.rs:gemma4_has_vision_weightsalso recognizes themodel.-prefixed vision keys, so an NVFP4 multimodal export routes toGemma4VLM.src/loading/vlm_gemma.rs:load_gemma4_vlmrunssanitize_gemma4_nvfp4_weightsafter 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 (nomodel.language_model.keys and noweight_scale_2sidecars, so both inner stages skip immediately).src/models/mod.rs: re-exportsanitize_gemma4_nvfp4_weightsfor the VLM loader.normalize_nvfp4_keys_remaps_vision_tower_and_embed_visioncovers the vision-key remap (the.linear.weightprojection nesting, the embed_vision projection, and the barestd_bias/position_embedding_tabletensors).#749notes inmodel_tests_gb10.mdandmodel_tests.mddescribe the fix. The single-model VLM benchmark CSV is committed as evidence.Validation (GB10, CUDA, SM 12.1)
Before (from the issue repro):
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):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-nvfp4pass, 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.jsoncarrying anaudio_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)Closes #749