Skip to content

Add Gemma 4 26B MoE support (MLX) - #4844

Merged
Manan17 merged 3 commits into
fix/ui-fixfrom
fix/gemma4-mlx
Apr 4, 2026
Merged

Manan17 merged 3 commits into
fix/ui-fixfrom
fix/gemma4-mlx

Conversation

@Manan17

@Manan17 Manan17 commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

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

@Manan17 Manan17 changed the title Add Gemma 4 26B MoE support Add Gemma 4 26B MoE support (MLX) Apr 4, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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)

critical

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)

high

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)

high

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)

medium

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread unsloth/models/gemma4_text.py Resolved
@Manan17

Manan17 commented Apr 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Answer to gemini's comments

  1. This is wrong. Gemma4 HF weights use .experts.down_proj and .experts.gate_up_proj as 3D tensors (already aggregated across experts), not per-expert keys like .experts.0.down_proj. I verified this by checking the actual HF safetensors. The 26B model works with our sanitize, I tested it.
  2. This file gets installed INTO mlx_lm/models/ where switch_layers.py lives. The relative import from .switch_layers import SwitchGLU is correct because both files are in the same package. It's not a standalone file.
  3. The router weights (router.proj, router.norm, router.scale, router.per_expert_scale) and the extra norm layers (post_feedforward_layernorm_1, etc.) don't need remapping — their HF names already match our model's attribute names. Only experts.down_proj and experts.gate_up_proj need remapping because HF uses fused weights while SwitchGLU expects split weights.
  4. This matches the actual Gemma4 architecture. Google's implementation has norm on each branch AND a shared norm after the sum.

@Manan17
Manan17 merged commit 324bdf4 into fix/ui-fix Apr 4, 2026
@Manan17
Manan17 deleted the fix/gemma4-mlx branch April 4, 2026 07:31
danielhanchen pushed a commit that referenced this pull request Apr 17, 2026
* 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]>
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.

1 participant