Repository navigation
fix(models): keep per-expert NVFP4 tensor scales in the Qwen3.5 MoE stacking - #3196
Merged
inureyes merged 1 commit intoNov 22, 2026
Merged
Conversation
…tacking
An NVFP4 export from NVIDIA ModelOpt or compressed-tensors stores each expert
as its own linear, and the repack leaves that expert's tensor scale beside its
plane. The Qwen3.5 and Qwen3-Next stacking passes took weight / scales / biases
only, so the scales stayed behind and the expert loader refused the plane
("... was left unstacked"). Both passes now go through one helper,
drain_numbered_expert_planes, which stacks every expert plane leaf, the scale
included, as the DeepSeek-V3, Mistral 4 and PhiMoE loops already do.
This was referenced Nov 20, 2026
Member
Hardening reviewReviewed against #3195. No changes pushed.
Validation on Apple Silicon (Metal), with the branch merged onto current
|
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Qwen3.5 MoE expert stacking dropped the per-expert tensor scale an NVFP4 repack leaves beside each expert's plane, so
RedHatAI/Qwen3.6-35B-A3B-NVFP4was refused at load (… was left unstacked). The Qwen3.5 and Qwen3-Next stacking passes now go through one helper that stacks every expert plane leaf, the scale included, as the DeepSeek-V3, Mistral 4 and PhiMoE loops already do.Related issues
Closes #3195.
Type of change
fix— bug fixCause
load_text_weightsrepacks an NVFP4 export's linears for every text family and, for an expert stored as its own linear, leaves that expert's tensor scale at…experts.{e}.{proj}.global_scale(#2351).qwen3_5::sanitize_weightsthen stacked the experts with a loop that removesweight,scalesandbiasesand nothing else. The scales stayed in the map, andrefuse_unstacked_expert_sidecarsrefused the stacked plane, as it is meant to.Qwen3NextModel::sanitize_weightshas the same loop.Change
switch_layers::drain_numbered_expert_planes(new): drains{experts_root}.{e}.{src_proj}.{leaf}forefrom 0 until an expert has noweight, and inserts each leaf stacked under{stacked_prefix}.{leaf}. The leaves areEXPERT_PLANE_LEAVESthroughstack_expert_leaf, soglobal_scalelands as the[num_experts]inv_global_scalethatSwitchLinearreads.qwen3_5::sanitize_weights: its two stacking loops (thew1/w2/w3names and thegate_proj/up_proj/down_projnames) call the helper. The order of the passes and the "slot already filled" skip are as before.Qwen3NextModel::sanitize_weights: its loop calls the helper.mlxcel_core::stackover the experts in index order), so nothing changes for it. The diff removes more lines than it adds outside the tests.Before / after
GB10 (sm_121, CUDA 13.0), release builds of
363a0ba6b(main, the base of this branch) and of this PR,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:RedHatAI/Qwen3.6-35B-A3B-NVFP4@e94a930c8(compressed-tensors, 256 experts per layer, 30880 quantized linears).mainError: Failed to load Qwen3.5 text model: model.layers.0.mlp.switch_mlp.gate_proj: the stacked NVFP4 expert plane carries no per-expert output multiplier, but model.layers.0.mlp.experts.4.up_proj.global_scale (a ModelOpt weight_scale_2 sidecar) was left unstacked; …, exit 1Paris4mlx-community/Qwen3.5-35B-A3B-4bit; with-n 1200:The sea stretches endlessly toward the horizon, its rhythmic waves whispering ancient secrets to the shore.Red and blueLoad: 80 s, 20.9 GB resident.
The fixed runs log
quantized layout does not match declared metadata160 times. That is this family's existing behaviour for a compressed-tensors NVFP4 export, whose config has no MLXquantizationblock (RedHatAI/Qwen3.5-9B-NVFP4, which loads onmain, logs it 248 times there); the layers load from their shapes. Not changed here.Checkpoints that load today answer byte for byte as on
main(16 runs): two text prompts on mlx-community/Qwen3.5-35B-A3B-4bit, the mlx-lmnvfp4andmxfp4exports of Qwen3.5-35B-A3B from #3144, RedHatAI/Qwen3.5-9B-NVFP4, axteam/Qwen3.5-0.8B-NVFP4, axteam/Qwen3.5-0.8B-FP8-dynamic and mgoin/Qwen3-0.6B-NVFP4, and an image prompt on the first and the fourth.Test plan
Run on a DGX Spark (GB10). The PR is one commit on
363a0ba6b, treee3f9e430d94a.cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warningsdraining_numbered_expert_planes_keeps_the_modelopt_sidecars(switch_layers_global_scale_tests.rs: both source namings; every per-expert key is drained, the scales come out as the[num_experts]multiplier in expert order, an unrelated tensor is left, the plane loads throughSwitchLinear; without an expert 0 nothing is taken),sanitize_weights_stacks_per_expert_nvfp4_tensor_scales(qwen3_5.rs: both namings throughsanitize_weights, then the Qwen3.5 expert block loads the result) andqwen3_next_sanitize_stacks_per_expert_nvfp4_tensor_scales(qwen3_next_tests.rs): passcargo test --profile test-fast --features cuda --lib -- qwen3_5 qwen3_next switch_layers qwen4_exp qwen3_omni: 347 passedmake verify-test-cuda(cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 16090 passed, 5 failed, 500 ignored. The five are the ones that fail on unmodified363a0ba6bon this host: fourmla::turbo_latenttests (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) andcommands::serve::tests::serve_preflight_kv_mode_on_deepseek_v2_follows_the_absorption_decision. This PR touches none of that codeci.ymljobs 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 refsnvidia/Qwen3.6-35B-A3B-NVFP4isMIXED_PRECISION, a separate gap). The unit tests use the ModelOpt-shaped fixture (insert_modelopt_expert_plane); after the repack the two producers' planes are the samexla-compile: not run (no IREE on this host); no XLA file is touchedcargo deny check: not applicable (no manifest or lockfile change)Notes for reviewers
switch_layers.rs. The three loops were copies of one another, and the leaf list already lives there (EXPERT_PLANE_LEAVES). OLMoE, dots1 and Solar Open, which the guard's comment names, could move onto it the same way; I left them, having no checkpoint to check them with.weight, as with the old loops, andSwitchLinear's shape checks refuse it.Checklist
docs/: not needed (no option or documented behaviour changes)// Used by: ...comments: the new helper carries one