Repository navigation
fix(models): refuse mismatched experts in family-local MoE stacking - #3335
Merged
Merged
Conversation
inureyes
added a commit
that referenced
this pull request
Nov 23, 2026
Bilingual report for the family-local MoE expert shape checks (#3320).
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
force-pushed
the
fix/issue-3320-moe-expert-shape-checks
branch
from
November 23, 2026 10:01
c81fcf2 to
bcaa16a
Compare
inureyes
marked this pull request as ready for review
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.
4 of 5 tasks
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.
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
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 shapeacross the cxx bridge and aborted the process at load. Each site now runs #3304'srefuse_mismatched_expert_partsbefore 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_partsis nowpub(crate)and itsUsed bylist names the six families.sanitize_weights), Mistral4 (sanitize_weights) and PhiMoE (sanitize_weights): the per-leaf loop collects(expert, &array)pairs, checks them, then stacks. Theglobal_scalesidecar is exempt becausestack_expert_leafconcatenates it, as in fix(models): return a load error on mismatched MoE expert shapes #3304.sanitize_weights: returnsResult<WeightMap, String>; the removed experts are checked beforestack.OlmoeModel::loadpropagates the error.stack_experts: checks the drained parts beforestack_owned.MellumModel::sanitizeand itsstack_expertsreturnResult<(), String>;MellumModel::loadpropagates.sanitize_weightsreturnsResult<(), String>; the callers inMistral4Model::load,load_from_text_config,src/loading/mod.rsandsrc/loading/vlm_pixtral.rspropagate it.Tests
Each family has one test on synthetic weights where expert 1's gate
weightis[4, 6]and expert 0's is[4, 8]; each asserts the sanitize call returnsErrnaming expert 1's key and both shapes. The PhiMoE test sits inphimoe.rs's inlinesanitize_testsmodule beside the existing stacking test, becausePhiMoeModel::sanitize_weightsis private andphimoe_tests.rsis 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=1gate in the issue is left to the orchestrator.cargo clippy --lib --tests --features metal,accelerate -- -D warnings,cargo fmt --all -- --checkand thescripts/cicontract 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-8bitandmoonlight-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