Skip to content

fix(loading): repack NVFP4 exports on the Gemma 4 Unified route - #3188

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma4-unified-nvfp4
Nov 22, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma4-unified-nvfp4

Conversation

@Jaeyeong-CHOI

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

Copy link
Copy Markdown
Contributor

Summary

load_gemma4_unified did not repack an NVFP4 export's quantized linears, so RedHatAI/gemma-4-12B-it-NVFP4 failed with Weight not found. The unified weight sanitizer now ends with the shared NVFP4 repack, as load_gemma4_vlm does for the other Gemma 4 checkpoints. It is a no-op for a checkpoint without such triplets.

Related issues

Closes #3187.

Type of change

  • fix — bug fix

Cause

resolve_and_sanitize_gemma4_unified_weights (src/loading/vlm_gemma_unified.rs) resolved the per-expert scale flags and normalized the keys. It did not call sanitize_modelopt_nvfp4_weights, which the Gemma 4 VLM route reaches through sanitize_gemma4_nvfp4_weights. A compressed-tensors linear stayed as weight_packed / weight_scale / weight_global_scale, so the text model found no weight; a ModelOpt linear stayed as a uint8 weight with its two scales.

Change

  • resolve_and_sanitize_gemma4_unified_weights calls models::sanitize_modelopt_nvfp4_weights after the key sanitizer. It runs on the sanitized key names, and it is keyed on the tensors (a weight_scale_2, or a weight_packed with a weight_global_scale), so a checkpoint without them comes out as before. The doc comment says so.
  • Nothing else: the text config already takes the NVFP4 layout through gemma4::ModelArgs, as on the other Gemma 4 route.

Before / after

GB10 (sm_121, CUDA 13.0), release builds of 363a0ba6b (main, the base of this branch) and of this PR's tree, generate -n 200 --temp 0, MLXCEL_SDPA_DETERMINISTIC=1. The image is a 448 x 448 red square with a blue square in the middle.

Checkpoint main, all four prompts This PR: capital of France / 2 + 2 / one sentence about the sea / (image) the two main colors
RedHatAI/gemma-4-12B-it-NVFP4 @ 8965ad1c9 (compressed-tensors) Error: Failed to load Gemma4 Unified text model: Weight not found: language_model.model.layers.0.self_attn.q_proj.weight, exit 1 Paris / 4 / The vast, restless sea stretches toward the horizon, carrying the secrets of a thousand sunken worlds beneath its rhythmic waves. / The two main colors in this image are red and blue.
coolthor/gemma-4-12B-it-NVFP4A16 @ f706714c2 (compressed-tensors, weights only) same error Paris / 4 / The vast, rhythmic pulse of the ocean carries the secrets of a thousand sunken civilizations beneath its restless surface. / The two main colors in this image are red and blue.
r0b0tlab/gemma-4-12B-it-nvfp4 @ 9c12a471a (ModelOpt) Error: … Unsupported packed weight: language_model.model.layers.0.self_attn.q_proj.weight is uint8 with …weight_scale, exit 1 still fails, later and for another reason: Error: Failed to load Gemma4 Unified vision embedder: Weight not found: vision_embedder.patch_ln1.weight

The third row is a second gap this PR does not close: that export uses another key layout for the vision embedder (see the issue). Its language model now gets through the repack (328 groups), which is all this PR claims for ModelOpt; the ModelOpt half is otherwise covered by the unit test.

Checkpoints that load today answer byte for byte as on main: mlx-community/gemma-4-12B-it-4bit @ 73bcf0909 (the unified route, text and image), axteam/gemma-4-E2B-it-NVFP4 (text and image), and the first prompt on gemma-3-4b-it-4bit, mgoin/Qwen3-0.6B-NVFP4, iromu/Gemma3-1B-tools-NVFP4 and nvidia/Qwen2.5-VL-7B-Instruct-NVFP4.

Test plan

Run on this PR's commit (one commit on 363a0ba6b, tree b8835fff3ba9) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings
  • cargo build --release --features cuda
  • The two new tests by name (src/loading/vlm_gemma_unified_tests.rs): unified_nvfp4_export_is_repacked_for_both_producers (a ModelOpt and a compressed-tensors checkpoint are repacked under the sanitized keys, to byte-identical weight maps; the float text and vision-embedder tensors are routed as before) and unified_checkpoint_without_nvfp4_triplets_is_left_as_sanitized (a float tensor, and a uint8 plane with only a weight_scale, come out byte for byte as the key sanitizer alone leaves them): pass
  • cargo test --profile test-fast --features cuda --lib -- loading::vlm gemma4 unified modelopt nvfp4 compressed: 638 passed
  • Negative control, the repack call removed: the repack test fails, the no-op test passes
  • make verify-test-cuda (cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 16089 passed, 5 failed, 500 ignored. The five are the ones that fail on unmodified 363a0ba6b on this host: four mla::turbo_latent tests (fix(cuda): reductions with a differing output dtype return garbage (sum over bool, any/all over non-bool) #3184, fixed by fix(cuda): write reductions through the output dtype #3185) and commands::serve::tests::serve_preflight_kv_mode_on_deepseek_v2_follows_the_absorption_decision. This PR touches none of that code
  • Self-hosted ci.yml jobs a fork PR does not get, by hand: clippy (cargo clippy -p mlxcel --lib --tests -- -D warnings, default features); license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refs
  • Validated with real checkpoints: the table above
  • xla-compile: not run (no IREE on this host); no XLA file is touched
  • cargo deny check: not applicable (no manifest or lockfile change)
  • Metal and ROCm: not built or run here
  • A unified checkpoint with NVFP4 MoE experts, or with a quantized audio or vision embedder: none at hand, not looked at

Notes for reviewers

  • Not gated on the config. Like sanitize_gemma4_nvfp4_weights on the other Gemma 4 route, the repack is keyed on the tensors, not on quantization_config.
  • Order. The repack runs after the key sanitizer, so it sees language_model.model.* names and its own model.language_model. remap has nothing to do.
  • Follow-up. I will file the other vision-embedder key layout separately; with it, the ModelOpt 12B exports named in the issue should load end to end.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not needed (no option or documented behaviour changes)
  • Updated // Used by: ... comments: the caller of the changed function is unchanged

load_gemma4_unified did not repack an NVFP4 export's quantized linears, as
load_gemma4_vlm does for Gemma 4. A compressed-tensors (llm-compressor)
export of gemma-4-12B failed with Weight not found, and a ModelOpt one
reached the linear loaders as packed bytes. The unified weight sanitizer now
ends with the shared NVFP4 repack, which is a no-op for a checkpoint without
such triplets.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata platform:linux Linux (CUDA / packaging) specific labels Nov 22, 2026
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3187 and the PR body. No code changes pushed.

Findings

  • The repack call mirrors load_gemma4_vlm: it is keyed on the tensors (weight_scale_2, or weight_packed with weight_global_scale), runs after the key sanitizer so it sees language_model.model.* names, and reads the group size from the top-level quantization_config that the unified config carries. A checkpoint without triplets passes through unchanged.
  • The text model binds the repacked planes through the same gemma4::ModelArgs quantization path as the VLM route, so no config override is needed.
  • Out of scope, as the PR notes: fused MoE experts or vision/audio embedder linears under compressed-tensors names are not covered by the split in sanitize_gemma4_unified_weights. No checkpoint at hand needs it.
  • Minor: the Used by list on sanitize_modelopt_nvfp4_weights does not yet name resolve_and_sanitize_gemma4_unified_weights. I will fold that one-line doc update into fix(loading): map module-layout vision keys on the Gemma 4 Unified route #3192, which touches the same file and needs a rebase push anyway, to avoid an extra CI cycle here.

Security / performance: no new input surface. The repack is the existing shared routine, and the extra pass over the key map is O(keys) once per load.

Validation (Apple Silicon, Metal)

  • cargo test --profile test-fast --lib -- unified: 78 passed, including both new tests
  • cargo test --profile test-fast --lib -- nvfp4: 51 passed
  • cargo clippy -p mlxcel --lib --tests -- -D warnings: clean
  • CUDA behavior relies on the author's GB10 runs in the PR body.

@inureyes
inureyes merged commit 5cfee8d into lablup:main Nov 22, 2026
32 checks passed
inureyes added a commit to Jaeyeong-CHOI/mlxcel that referenced this pull request Nov 22, 2026
The Used by list missed the Qwen2.5-VL, Gemma 3 VLM and Gemma 4 Unified loaders that call it directly.

Refs lablup#3188
inureyes added a commit that referenced this pull request Nov 22, 2026
…ute (#3192)

* fix(loading): repack NVFP4 exports on the Gemma 4 Unified route

load_gemma4_unified did not repack an NVFP4 export's quantized linears, as
load_gemma4_vlm does for Gemma 4. A compressed-tensors (llm-compressor)
export of gemma-4-12B failed with Weight not found, and a ModelOpt one
reached the linear loaders as packed bytes. The unified weight sanitizer now
ends with the shared NVFP4 repack, which is a no-op for a checkpoint without
such triplets.

* fix(loading): map module-layout vision keys on the Gemma 4 Unified route

The ModelOpt NVFP4 exports of Gemma 4 12B keep the vision embedder's layers
under embed_vision. and its projection under
embed_vision.multimodal_embedder., where google/gemma-4-12B-it has
vision_embedder.* and embed_vision.embedding_projection. The unified key
normalization now maps that layout onto the one the loader reads, so such a
checkpoint no longer stops at Weight not found:
vision_embedder.patch_ln1.weight.

* docs: list every caller of sanitize_modelopt_nvfp4_weights

The Used by list missed the Qwen2.5-VL, Gemma 3 VLM and Gemma 4 Unified loaders that call it directly.

Refs #3188

---------

Co-authored-by: Jeongkyu Shin <[email protected]>
@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:models Model architectures, weights, loading, metadata platform:linux Linux (CUDA / packaging) specific priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(loading): Gemma 4 Unified (gemma4_unified) NVFP4 exports fail to load because the route skips the repack

2 participants