Repository navigation
Add Gemma 4 26B MoE support (MLX) - #4844
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements Mixture of Experts (MoE) support for the Gemma4 model by introducing Router and Experts modules and updating the decoder layer to handle parallel MLP and expert processing. While the implementation provides the necessary architectural components, the review feedback identifies several critical issues: the weight sanitization logic fails to account for standard Hugging Face expert indexing and 3D tensor requirements, and it misses remapping for the router and new normalization layers. Additionally, there is a likely broken relative import for SwitchGLU and a potential double-normalization issue in the MoE execution path that deviates from standard architectures.
I am having trouble creating individual review comments. Click here to see my feedback.
unsloth/models/gemma4_text.py (713-729)
The sanitize method's suffix checks (e.g., k.endswith(".experts.down_proj")) will fail to match standard Hugging Face MoE keys, which typically include an expert index (e.g., .experts.0.down_proj.weight). Furthermore, the method does not implement the necessary logic to aggregate individual expert weights into the 3D tensors required by the SwitchGLU implementation (which expects a shape like (num_experts, out, in)). This will prevent the model from loading expert weights correctly.
unsloth/models/gemma4_text.py (166)
The relative import from .switch_layers import SwitchGLU will fail if switch_layers.py is not present in the same directory. If SwitchGLU is intended to be a component from the mlx-lm package (as suggested by the error message on line 169), it should be imported using its full package path (e.g., from mlx_lm.models.switch_layers import SwitchGLU). If it is a local file, it appears to be missing from this pull request.
unsloth/models/gemma4_text.py (708)
The sanitize method is missing remapping logic for several critical components: the router weights (projection and normalization) and the newly added normalization layers (post_feedforward_layernorm_1, post_feedforward_layernorm_2, pre_feedforward_layernorm_2). Without remapping, these parameters will remain at their default initialization values instead of being loaded from the model checkpoint.
unsloth/models/gemma4_text.py (447)
In the MoE path, h is normalized by post_feedforward_layernorm after already being processed by branch-specific normalization layers (post_feedforward_layernorm_1 and post_feedforward_layernorm_2). This results in a double normalization for both the shared MLP and expert outputs, which is likely not intended and deviates from standard parallel MoE architectures. Consider removing the branch-specific post-norms if the architecture specifies a single normalization after the sum.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b0214fafdc
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
Answer to gemini's comments
|
* unsloth gemma4 support files * some fixes * Fixing cache.empty() calls (#4813) * Fixing cache.empty() calls * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Manan Shah <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * Fix/gemma4 mlx (#4816) * Fixing cache.empty() calls * fixing for mlx versions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Manan Shah <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * removed bidirectional check for 31b (#4839) Co-authored-by: Manan17 <[email protected]> * Add Gemma 4 26B MoE support (MLX) (#4844) * removed bidirectional check for 31b * Change gemma4_text for moe * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Manan Shah <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * fix(gemma4): cast RoPE offset to int before mx.arange() (#4901) * fix(gemma4): cast RoPE offset to int before mx.arange() * fix(gemma4): use zero-based arange + offset to avoid CPU-GPU sync * qwen3.6 patches for multi-turn chat * qwen3.6 script * removing unnecessary scripts * displaying errors for not installed packages --------- Co-authored-by: Roland Tannous <[email protected]> Co-authored-by: Manan Shah <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Manan17 <[email protected]> Co-authored-by: Théophile Lafargue <[email protected]>
Add Gemma 4 26B MoE support with Router, GeGLU, and Experts classes backed by mlx_lm's SwitchGLU, MoE forward path in TransformerBlock running shared MLP and expert MoE in parallel, weight remapping in sanitize for SwitchGLU format, new ModelArgs fields (num_experts, top_k_experts, moe_intermediate_size), and a clear error for mlx_lm < 0.31 which lacks switch_layers