Skip to content

fix(models): keep per-expert NVFP4 tensor scales in the Qwen3.5 MoE stacking - #3196

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/qwen35-moe-expert-global-scale
Nov 22, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:fix/qwen35-moe-expert-global-scale

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

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-NVFP4 was 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 fix

Cause

load_text_weights repacks 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_weights then stacked the experts with a loop that removes weight, scales and biases and nothing else. The scales stayed in the map, and refuse_unstacked_expert_sidecars refused the stacked plane, as it is meant to. Qwen3NextModel::sanitize_weights has the same loop.

Change

  • switch_layers::drain_numbered_expert_planes (new): drains {experts_root}.{e}.{src_proj}.{leaf} for e from 0 until an expert has no weight, and inserts each leaf stacked under {stacked_prefix}.{leaf}. The leaves are EXPERT_PLANE_LEAVES through stack_expert_leaf, so global_scale lands as the [num_experts] inv_global_scale that SwitchLinear reads.
  • qwen3_5::sanitize_weights: its two stacking loops (the w1 / w2 / w3 names and the gate_proj / up_proj / down_proj names) 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.
  • For a checkpoint without the scale the three leaves are stacked as before (mlxcel_core::stack over 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).

Prompt main This PR
capital of France, one word Error: 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 1 Paris
2 + 2, just the number same error 4
one sentence about the sea same error at 200 tokens the answer is still in the reasoning channel, as it is for mlx-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.
(image) the two main colors same error Red and blue

Load: 80 s, 20.9 GB resident.

The fixed runs log quantized layout does not match declared metadata 160 times. That is this family's existing behaviour for a compressed-tensors NVFP4 export, whose config has no MLX quantization block (RedHatAI/Qwen3.5-9B-NVFP4, which loads on main, 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-lm nvfp4 and mxfp4 exports 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, tree e3f9e430d94a.

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings
  • The three new tests by name: draining_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 through SwitchLinear; without an expert 0 nothing is taken), sanitize_weights_stacks_per_expert_nvfp4_tensor_scales (qwen3_5.rs: both namings through sanitize_weights, then the Qwen3.5 expert block loads the result) and qwen3_next_sanitize_stacks_per_expert_nvfp4_tensor_scales (qwen3_next_tests.rs): pass
  • cargo test --profile test-fast --features cuda --lib -- qwen3_5 qwen3_next switch_layers qwen4_exp qwen3_omni: 347 passed
  • Negative control, the helper made to leave the scale behind as the old loops did: all three new tests fail
  • make 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 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 a real checkpoint: the table above. The release binary for those runs was built one step earlier, from a tree that differs from this PR's only in six lines of the Qwen3-Next test's config (my first version of that test left out required config fields and failed to parse; the fix is test-only, and everything else in this list was re-run on the final tree)
  • A Qwen3-Next export with per-expert NVFP4 linears: none at hand; that pass is covered by its unit test only
  • A ModelOpt export with per-expert linears on this family: none that loads (nvidia/Qwen3.6-35B-A3B-NVFP4 is MIXED_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 same
  • 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

Notes for reviewers

  • Why a helper in 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.
  • A leaf only some experts carry stacks shorter than weight, as with the old loops, and SwitchLinear's shape checks refuse it.
  • Qwen4-Exp and the Qwen3-Omni talker reach the Qwen3-Next expert types through their own sanitizers and are not changed; their tests are in the filtered run above.

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 new helper carries one

…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.
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3195. No changes pushed.

  • Correctness: drain_numbered_expert_planes stacks weight / scales / biases exactly as the three removed loops did (same expert order, same stop condition at the first missing weight), and routes global_scale through stack_expert_leaf, so it lands as the [num_experts] inv_global_scale the same way the DeepSeek-V3, Mistral 4 and PhiMoE loops produce it. The Qwen3.5 "slot already filled" skip and pass order are unchanged. A partially present sidecar produces a short vector that SwitchLinear's shape check refuses, and refuse_unstacked_expert_sidecars is untouched, so a family that still drops the scale keeps failing loudly.
  • Security: weight-map transforms only; no new input surface.
  • Performance: same number of stack ops per projection; the input handles are dropped when the helper returns, as before.

Validation on Apple Silicon (Metal), with the branch merged onto current main (d6ef8437):

  • cargo test --profile test-fast --lib -- qwen3_5 qwen3_next switch_layers qwen4_exp qwen3_omni --test-threads=1: 347 passed, 0 failed
  • cargo clippy -p mlxcel --lib --tests -- -D warnings: clean
  • cargo fmt --all -- --check: clean

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(models): Qwen3.5 MoE NVFP4 stacking drops the per-expert tensor scale, so the export is refused at load

2 participants