Skip to content

Fix qwen3_moe sanitize() function's layer-0 MoE assumption - #1875

Merged
michalk8 merged 5 commits into
ml-explore:mainfrom
twallgren:pr1a-qwen3-moe-sanitize-fix
Sep 14, 2026
Merged

michalk8 merged 5 commits into
ml-explore:mainfrom
twallgren:pr1a-qwen3-moe-sanitize-fix

Conversation

@twallgren

@twallgren twallgren commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: AI was used to write the fix and the tests with some guidance from me. It went through many rounds of code reviews with a wide variety of agent frameworks and models to check for any issues with the core logic, styling, edge cases, etc.

The issue this is addressing:
qwen3_moe.py's sanitize() function decides whether to stack MoE weights by checking for a fixed key on layer 0. If layer 0 isn't a MoE layer then that key won't exist and the gate will end up skipping the stacking for every layer.
Seems like all of the Qwen3 MoE configs that are out right now have a MoE layer at layer 0 so they're not affected by this bug, but that could change so we should be prepared for that situation.

The solution:
Changed qwen3_moe.py's sanitize check from a fixed layer-0 check to presence-based detection across whichever local MoE layers actually exist in the checkpoint.

…ints

sanitize() gated expert-weight stacking on whether layer 0's raw
expert keys were present. Under pipeline sharding a rank only
downloads its own local layers' weight files, so a raw checkpoint
can have no layer-0 key at all even though the rank's own local
layers still need stacking. Switched to a presence-based scan over
whichever layers are actually in the weights dict.
@twallgren twallgren changed the title Fix qwen3_moe sanitize() gate to support ranks with no layer 0 Fix qwen3_moe sanitize() function's layer-0 MoE assumption Sep 10, 2026
The comment described a pipeline-sharded partial-download scenario
that a review found unreachable in practice: that loading path
requires an MLX-converted (already-stacked) checkpoint and errors
before any partial download happens. The real bug is architectural:
whether layer 0 is an MoE layer is config-driven, so a layer-0-only
gate is wrong regardless of sharding.
twallgren added a commit to twallgren/mlx-lm that referenced this pull request Sep 10, 2026
…tion

Same fix as qwen3_moe.py (ml-explore#1875): the comment described an
unreachable pipeline-sharded partial-download scenario. The real bug
is architectural — whether layer 0 is an MoE layer is config-driven,
so a layer-0-only gate is wrong regardless of sharding.
twallgren added a commit to twallgren/mlx-lm that referenced this pull request Sep 11, 2026
…tion

Same fix as qwen3_moe.py (ml-explore#1875): the comment described an
unreachable pipeline-sharded partial-download scenario. The real bug
is architectural — whether layer 0 is an MoE layer is config-driven,
so a layer-0-only gate is wrong regardless of sharding.

@michalk8 michalk8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, I've added your fix to rest of the models.

@michalk8
michalk8 marked this pull request as ready for review September 14, 2026 12:43
@michalk8
michalk8 merged commit d441a74 into ml-explore:main Sep 14, 2026
2 checks passed
@twallgren
twallgren deleted the pr1a-qwen3-moe-sanitize-fix branch September 14, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants