Skip to content

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

Open
neuromechanist wants to merge 1 commit into
ml-explore:mainfrom
yooz-labs:upstream/gemma4-llm-moe
Open

MLXLLM Gemma4Text: add MoE block (router + experts) for the text path#364
neuromechanist wants to merge 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.

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