Skip to content

fix(models): refuse mismatched experts in family-local MoE stacking - #3335

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3320-moe-expert-shape-checks
Nov 23, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3320-moe-expert-shape-checks

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Six MoE families stack per-expert planes in their own sanitize code: DeepSeek-V3, Mistral4, PhiMoE, OLMoE, MiMo-V2 and Mellum. They passed the parts straight to mlxcel_core::stack / stack_owned / stack_expert_leaf, and that FFI is not fallible, so one malformed expert in a checkpoint raised MLX's [stack] All arrays must have the same shape across the cxx bridge and aborted the process at load. Each site now runs #3304's refuse_mismatched_expert_parts before the stack call and returns a load error naming the expert's on-disk key, its shape and dtype, and expert 0's shape and dtype.

Changes

  • switch_layers::refuse_mismatched_expert_parts is now pub(crate) and its Used by list names the six families.
  • DeepSeek-V3 (sanitize_weights), Mistral4 (sanitize_weights) and PhiMoE (sanitize_weights): the per-leaf loop collects (expert, &array) pairs, checks them, then stacks. The global_scale sidecar is exempt because stack_expert_leaf concatenates it, as in fix(models): return a load error on mismatched MoE expert shapes #3304.
  • OLMoE sanitize_weights: returns Result<WeightMap, String>; the removed experts are checked before stack. OlmoeModel::load propagates the error.
  • MiMo-V2 stack_experts: checks the drained parts before stack_owned.
  • Mellum MellumModel::sanitize and its stack_experts return Result<(), String>; MellumModel::load propagates.
  • Mistral4 sanitize_weights returns Result<(), String>; the callers in Mistral4Model::load, load_from_text_config, src/loading/mod.rs and src/loading/vlm_pixtral.rs propagate it.
  • On error nothing is inserted for the refused leaf.

Tests

Each family has one test on synthetic weights where expert 1's gate weight is [4, 6] and expert 0's is [4, 8]; each asserts the sanitize call returns Err naming expert 1's key and both shapes. The PhiMoE test sits in phimoe.rs's inline sanitize_tests module beside the existing stacking test, because PhiMoeModel::sanitize_weights is private and phimoe_tests.rs is a sibling module that cannot reach it.

Failing first: with the helper short-circuited to Ok(()), all six tests abort the test binary with [stack] All arrays must have the same shape (SIGABRT); with the check they pass.

Selectors run (--profile test-fast --features metal,accelerate --lib, under the GPU lock): mismatched_expert (9 passed, the six new tests plus the existing Qwen3.5, Laguna and Kimi-K3 ones), models::deepseek_v3 (73), models::mistral4 (4), models::phimoe (15), models::olmoe (2, 2 ignored), models::mimo_v2 (20), models::mellum (7), models::switch_layers (66, 1 ignored), coverage_tests (18). The broad --test-threads=1 gate in the issue is left to the orchestrator.

cargo clippy --lib --tests --features metal,accelerate -- -D warnings, cargo fmt --all -- --check and the scripts/ci contract checks pass.

Checkpoint regressions

mlxcel generate -p "The capital of France is" -n 24 --temp 0, release build, under the GPU lock; each answers "Paris.":

  • gigachat3-10b-a1.8b-bf16 (DeepSeek-V3, per-expert layout stacked by this code)
  • olmoe-1b-7b-0125-4bit (OLMoE, per-expert layout stacked by this code)
  • gigachat3-10b-a1.8b-mlx-8bit and moonlight-16b-a3b-instruct-4bit (DeepSeek-V3, quantized)

No local Mistral4, PhiMoE or Mellum checkpoint exists; MiMo-V2-Flash-4bit (162 GB) was skipped under the 100 GB limit.

The bilingual technical report is in TECHNICAL_REPORTS/3335-family-moe-expert-shape-checks-20261123.{en,ko}.md.

Closes #3320

@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 arch:moe Sparse mixture-of-experts decoder labels Nov 23, 2026
inureyes added a commit that referenced this pull request Nov 23, 2026
Bilingual report for the family-local MoE expert shape checks (#3320).
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
DeepSeek-V3, Mistral4, PhiMoE, OLMoE, MiMo-V2 and Mellum stack per-expert planes in their own sanitize code. A checkpoint with one malformed expert reached mlxcel_core::stack, whose MLX shape exception crosses the cxx bridge and aborts the process. Each site now runs the shared refuse_mismatched_expert_parts check (now pub(crate)) before stacking and returns a load error naming the expert's key and both shapes. The global_scale sidecar stays exempt because it is concatenated.

Mistral4 sanitize_weights, OLMoE sanitize_weights and Mellum sanitize/stack_experts become fallible, and their callers in the loaders propagate the error. Each family gets a test on synthetic weights where expert 1's gate weight is [4, 6] and expert 0's is [4, 8].

Closes #3320
Bilingual report for the family-local MoE expert shape checks (#3320).
@inureyes
inureyes force-pushed the fix/issue-3320-moe-expert-shape-checks branch from c81fcf2 to bcaa16a Compare November 23, 2026 10:01
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 10:05
@inureyes
inureyes merged commit 1a4d81d into main Nov 23, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3320-moe-expert-shape-checks branch November 23, 2026 10:05
inureyes added a commit to Jaeyeong-CHOI/mlxcel that referenced this pull request Nov 26, 2026
Main changed `drain_numbered_expert_planes` to return `Result` (lablup#3335), so the ERNIE text loader now propagates its error instead of discarding it, which would also fail `clippy -D warnings` on `unused_must_use`. A plane whose dtype differs from expert 0 now fails with the helper's message naming the expert key instead of a later `Weight not found` for the stacked projection.

Add a regression test covering the three malformed numbered-expert layouts the loader refuses: a dtype mismatch (reported by the shared helper), a missing expert, and an extra expert beyond `moe_num_experts`.
Jaeyeong-CHOI added a commit to Jaeyeong-CHOI/mlxcel that referenced this pull request Nov 26, 2026
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 added a commit to Jaeyeong-CHOI/mlxcel that referenced this pull request Nov 26, 2026
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.
@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 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): refuse mismatched experts in family-local MoE stacking

1 participant