Skip to content

fix(models): preserve NVFP4 scales when stacking Nemotron-H experts - #3315

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/nemotron-h-moe-nvfp4-experts
Nov 26, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/nemotron-h-moe-nvfp4-experts

Conversation

@Jaeyeong-CHOI

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

Copy link
Copy Markdown
Contributor

Closes #3314

Summary

The ModelOpt export of Nemotron 3 Nano stores each routed expert as a separate linear. The repack produces a packed weight, block scales and a tensor-scale multiplier for each projection. Nemotron-H moved only the weight into switch_mlp. The first routed forward therefore sent packed weights to the dense gather_mm kernel.

NemotronHModel::sanitize_weights now uses drain_numbered_expert_planes from #3196 for both up_proj → fc1 and down_proj → fc2. It stacks the block scales with the weights and collects the tensor scales in expert order under inv_global_scale. After constructing each SwitchLinear, the loader releases that entry with the other consumed leaves.

The helper returns Result since #3335. sanitize_weights now returns Result<WeightMap, String> and its four callers propagate it (NemotronHModel::load, src/loading/special.rs, src/loading/vlm_nemotron_h_nano_omni.rs, src/models/nemotron_voicechat/llm.rs). A per-expert plane that disagrees with expert 0 in shape or dtype fails the load with the helper's message naming the expert key.

Real-checkpoint verification

Built main 36129ebb3 and the fix with cargo build --locked --release --features cuda --bin mlxcel on a DGX Spark (GB10). Both binaries ran generate -m "$MODEL" -n 200 --temp 0 -p "$PROMPT" with MLXCEL_SDPA_DETERMINISTIC=1 and MLX_ENABLE_TF32=0.

The target checkpoint was nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-NVFP4 @ 6efb4a2a1c1fa277ce7b3df7a1416255011b1c99.

Prompt Main Fix (exit 0)
What is the capital of France? Answer in one word. gather_mm abort, exit 134 Paris
What is 2 + 2? Answer with just the number. Same abort 4
Write one sentence about the sea. Same abort The sea stretches endlessly, its sapphire waves whispering ancient stories to anyone who listens.

The capital run failed on main with [gather_mm] Last dimension of first input with shape (168,1,2688) must match second to last dimension of second input with shape (128,336,1856).

Both binaries exited 0 on the four control prompts below. For each pair, the generated text matched byte for byte, and the CLI reported the same token count.

Control checkpoint Revision Prompts (reported tokens)
lmstudio-community/NVIDIA-Nemotron-3-Nano-30B-A3B-MLX-4bit 543873e66e36362182738b0d2863a766728045aa capital (166), sea (37)
nvidia/NVIDIA-Nemotron-Nano-9B-v2-NVFP4 8556c9164ddb43fe1f4f4ad730593b3c5e3f7328 capital (122), sea (200, the generation limit)

The first push of this PR ran the same comparison on base 251f08610 (commit d1922e66b), with two more controls: mgoin/Qwen3-0.6B-NVFP4 @ 545def2dd2492f4abd75435ccde6dd93d1d93e34 and mlx-community/gemma-3-4b-it-4bit @ 93724907d4ed1745d2fe50baadf3b0b01a65abf2, capital prompt each, main and fix identical. Those two were not rerun on the current base.

Test plan

CUDA verification used tree c9a61e85fcaf26883b3fe569a59e75b9e971780a (commit 99ff9e30d, base 36129ebb3).

  • cargo fmt --all -- --check passed.
  • cargo test --locked --profile test-fast --features cuda --lib -- --test-threads=1 per_expert_nvfp4_planes_are_stacked_with_their_tensor_scales passed. Three synthetic experts carry distinct up/down multipliers. The test checks their order and verifies that the model loads both projections as NVFP4 with per-expert multipliers.
  • mismatched_per_expert_planes_fail_sanitize_with_the_expert_key (new) passed. One expert's up_proj.weight in another dtype, and one expert's down_proj.weight in another shape, each make sanitize_weights return the helper's error naming backbone.layers.0.mixer.experts.2.<proj>.weight.
  • Dropping the ? on the helper call while keeping the tests made the new test fail (mismatched per-expert planes must fail to sanitize, exit 101), and cargo clippy --locked --features cuda --lib -- -D warnings refused the build with unused std::result::Result that must be used.
  • cargo test --locked --profile test-fast --features cuda --lib -- --test-threads=1 nemotron_h switch_layers passed (151 passed, 4 ignored).
  • cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings passed.
  • make verify-test-cuda with a private 0700 TMPDIR exited 2: 17,093 passed, 1 failed and 516 ignored. The failure is models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_context (cached k_pe at positions 16..24 must be YaRN-rotated (max diff 1.6620973)). I did not rerun the gate on unmodified 36129ebb3. The same test failed with the same message on main 251f08610 in the first push's gate, and this PR does not touch DeepSeek-V3.2.

On the first push (base 251f08610), restoring main's nemotron_h.rs while keeping the NVFP4 test made it fail with backbone.layers.0.mixer.switch_mlp.fc1.inv_global_scale was not stacked (exit 101). That mutation was not repeated on the current base.

Verification limits

Each target run still logged 80 NVFP4 layout warnings. On the fixed capital run of the first push, 46 named shared-expert up/down projections and 34 named mixer input/output projections. The numerical effect of those warnings remains unverified. I did not compare logits against a reference implementation or run Metal or ROCm. The Nemotron-H Nano Omni and voice-chat loaders now propagate the same error; they are covered by compilation and clippy only, not by a real checkpoint.

@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 modeltype:text Text-only language model arch:moe Sparse mixture-of-experts decoder labels Nov 24, 2026
@inureyes inureyes self-assigned this Nov 24, 2026
@Jaeyeong-CHOI
Jaeyeong-CHOI force-pushed the fix/nemotron-h-moe-nvfp4-experts branch from d1922e6 to 99ff9e3 Compare November 26, 2026 04:11
@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor Author

Rebased onto 36129ebb3 (one commit, 99ff9e30d). #3335 made drain_numbered_expert_planes return Result, and the previous commit discarded it, which would fail clippy -D warnings once merged with main even though there was no textual conflict. sanitize_weights now returns Result<WeightMap, String> and its four callers propagate the error; one new test covers a per-expert plane with a mismatched dtype or shape. I reran fmt, clippy, the Nemotron-H and switch-layer tests, the target checkpoint and four control prompts against a build of current main, and the full CUDA gate on a GB10, and replaced the numbers in the description. The description lists what was not rerun on the new base.

NemotronHModel::sanitize_weights stacks the per-expert planes with drain_numbered_expert_planes, so the ModelOpt block scales and tensor scale stay beside each packed weight. The helper returns Result since lablup#3335; sanitize_weights now returns Result<WeightMap, String> and its four callers propagate the error, so a plane that disagrees with expert 0 in shape or dtype fails the load with the helper's message naming the expert key.
@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor Author

Rebased again onto 4a9642f83 after #3333 merged (commit f38ec7c7e). The only conflict was the Used by: line of drain_numbered_expert_planes in src/models/switch_layers.rs, which now names both Nemotron-H and ERNIE 4.5 text. The other five files are byte-identical to 99ff9e30d, the commit the numbers in the description were measured on. I did not rerun the CUDA build or the checkpoints on this base. scripts/ci/check_used_by.py passes on this branch and on this branch merged with #3312.

@Jaeyeong-CHOI
Jaeyeong-CHOI force-pushed the fix/nemotron-h-moe-nvfp4-experts branch from 99ff9e3 to f38ec7c Compare November 26, 2026 04:34
@inureyes

Copy link
Copy Markdown
Member

Hardening summary

Summary

Merge-ready at f38ec7c7e. Nothing pushed by the maintainers: the one gap found (the discarded Result from drain_numbered_expert_planes after #3335) is the same one this rebase already fixes.

Reviewed (PR + issues)

Findings

  • MEDIUM, fixed by the contributor in f38ec7c7e: at d1922e66b, the new stacking call ignored the Result that drain_numbered_expert_planes returns since fix(models): refuse mismatched experts in family-local MoE stacking #3335. After merging with main, a malformed expert plane would have been dropped silently and the load would fail later as a missing switch_mlp weight, and clippy -D warnings would reject the unused Result. sanitize_weights now returns Result<WeightMap, String> and all four callers propagate it. A maintainer fix with the same shape was prepared in parallel and discarded in favour of this one.
  • The Used by: conflict with fix(models): load published ERNIE-4.5 text MoE expert layouts #3333 in switch_layers.rs is resolved, and the comment names both Nemotron-H and ERNIE 4.5 text.

Fixes

None needed beyond the contributor's rebase.

Security review

No new input surface. Malformed checkpoint planes now fail the load with a named key instead of being skipped.

Performance review

No change on the hot path. Stacking runs once at load.

Validation

  • Real checkpoint, on the PR's NVFP4 stacking change (d1922e66b, spark-101 GB10, release build, --temp 0, MLXCEL_SDPA_DETERMINISTIC=1): NVIDIA-Nemotron-3-Nano-30B-A3B-NVFP4 answered "Paris", "4", and a coherent sentence. With the nemotron_h.rs change reverted, per_expert_nvfp4_planes_are_stacked_with_their_tensor_scales fails with fc1.inv_global_scale was not stacked.
  • At f38ec7c7e (spark-102 GB10, --profile test-fast --features cuda, --test-threads=1): 204 lib tests matching nemotron pass, 3 ignored, including mismatched_per_expert_planes_fail_sanitize_with_the_expert_key.
  • cargo clippy --profile test-fast --features cuda --all-targets -p mlxcel -- -D warnings was clean on the equivalent maintainer tree (same signature change on current main).
  • CI on f38ec7c7e: success. f38ec7c7e merges cleanly with main at 1c284f1f3.

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

arch:moe Sparse mixture-of-experts decoder area:models Model architectures, weights, loading, metadata modeltype:text Text-only 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(models): Nemotron-H loads Nemotron 3 Nano ModelOpt NVFP4 experts as dense and aborts in gather_mm

2 participants