Skip to content

MLXLLM Gemma4Text: add MoE block (router + experts) for the text path - #364

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
yooz-labs:upstream/gemma4-llm-moe
Jul 21, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
yooz-labs:upstream/gemma4-llm-moe

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

The MLXLLM Gemma4Text text path can load dense and (since #342) KV-shared Gemma4 checkpoints, but not MoE ones (26B-A4B): loading fails with Unhandled keys ["experts", "router", "post_feedforward_layernorm_1", "post_feedforward_layernorm_2", "pre_feedforward_layernorm_2"]. The complete MoE implementation already exists in MLXVLM/Models/Gemma4.swift (#180, #228); this brings it to the LLM text path.

Adds: Gemma4TextRouter, Gemma4TextExperts (SwitchGLU), the enable_moe_block config + dense/sparse decoder branch, and the fused-expert weight remap in sanitize (experts.gate_up_proj/down_proj -> switch_glu.*). Pairs with the KV-sharing already merged in #342.

Verified by loading + generating mlx-community/gemma-4-26b-a4b-it-4bit through the LLM text path (correct output at native context). Ported from ml-explore's own MLXVLM implementation.

Brings the MLXVLM Gemma4 MoE implementation to the LLM text path so the
26B-A4B (MoE) checkpoint loads through MLXLLM, not just MLXVLM. Adds
Gemma4TextRouter, Gemma4TextExperts (SwitchGLU), the enable_moe_block config +
dense/sparse decoder branch, and the fused-expert weight remap in sanitize.
The E-series KV-sharing it pairs with is already upstream (ml-explore#342).

Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs ml-explore#180, ml-explore#228).
}
sanitized[k] = v

// Remap .experts.down_proj -> .experts.switch_glu.down_proj.weight

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The original sanitized[k] = value pass-through runs before the remap checks. For MoE keys, the original key (e.g. .experts.down_proj) is inserted into the dictionary alongside the remapped key (.experts.switch_glu.down_proj.weight). The continue only prevents the second pass-through it doesn't remove the first assignment. MoE keys should be remapped instead of passed through, not in addition.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change looks good, thank you!

@davidkoski
davidkoski merged commit e6e3de7 into ml-explore:main Jul 21, 2026
2 checks passed
@neuromechanist
neuromechanist deleted the upstream/gemma4-llm-moe branch July 21, 2026 20:22
dkgv pushed a commit to dkgv/mlx-swift-lm that referenced this pull request Jul 30, 2026
Brings the MLXVLM Gemma4 MoE implementation to the LLM text path so the
26B-A4B (MoE) checkpoint loads through MLXLLM, not just MLXVLM. Adds
Gemma4TextRouter, Gemma4TextExperts (SwitchGLU), the enable_moe_block config +
dense/sparse decoder branch, and the fused-expert weight remap in sanitize.
The E-series KV-sharing it pairs with is already upstream (ml-explore#342).

Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs ml-explore#180, ml-explore#228).
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
…s-dispatch-20261025

Gemma 4: reuse the shared compiled GeGLU in the dense MLP
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.

3 participants