Repository navigation
fix Gemma 4 MoE router -- softmax order + fuse norm dispatches - #228
Merged
Merged
Conversation
TheTom
marked this pull request as draft
April 21, 2026 15:50
TheTom
force-pushed
the
pr/upstream-gemma4-moe
branch
from
April 21, 2026 16:09
25d551c to
6bd3747
Compare
TheTom
marked this pull request as ready for review
April 21, 2026 16:10
TheTom
force-pushed
the
pr/upstream-gemma4-moe
branch
from
April 21, 2026 16:13
6bd3747 to
bb856ae
Compare
Softmax was applied to all 128 expert scores before top-k selection, so selected weights came from the full distribution rather than just the top-k. Moved softmax to after selection so it operates only on the chosen experts. Drops the renormalization step since softmax on top-k already produces a valid distribution. Also fuses norm + scale from 3 dispatches into one MLXFast.rmsNorm call.
TheTom
force-pushed
the
pr/upstream-gemma4-moe
branch
from
April 21, 2026 16:14
bb856ae to
7ad51df
Compare
Contributor
Author
|
before/after on gemma-4-26b-a4b-it-4bit, greedy decode (no chat template, no temperature): prompt: "Explain the difference between TCP and UDP in one sentence:" before (softmax all 128 experts → top-k): after (top-k → softmax selected): both degrade to repetition without a chat template, but the routing fix produces recognizable English instead of garbled characters. |
davidkoski
approved these changes
May 14, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
changes look good, thank you!
NivDvir
added a commit
to NivDvir/mlx-swift-lm
that referenced
this pull request
May 27, 2026
Per davidkoski's ml-explore#238 standard ("people can always go back to the PR the lines came from to see the discussion") applied harder: the two comment blocks I'd kept were both restatements of what the variable names and call structure already say. Brings the comment-line ratio on this PR from ~40% to 0%, in line with the maintainer's own recent PRs (ml-explore#283 2%, ml-explore#235 3%, ml-explore#228 0%).
neuromechanist
added a commit
to yooz-labs/mlx-swift-lm
that referenced
this pull request
Jun 22, 2026
Bring the LLM text path to parity with the complete MLXVLM Gemma4 implementation so text-only loading of MoE (26B-A4B) and E-series KV-sharing (E4B) checkpoints works: - MoE block: Gemma4TextRouter + Gemma4TextExperts (SwitchGLU), dense+sparse decoder branch gated on enable_moe_block, and the fused-expert weight remap in sanitize (experts.gate_up_proj/down_proj -> switch_glu.*). - has_kv-conditional attention: k_proj/k_norm/v_norm are optional; KV-shared layers (index >= firstKvSharedLayerIdx) carry none and reuse an earlier layer's KV at runtime. Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs ml-explore#180, ml-explore#228); the E-series KV-sharing parallels the in-flight ml-explore#330 / ml-explore#350. Verified via yooz-engine's InfiniteLive parity: 26B-A4B (correct answer), E4B (exact greedy), 12B (exact greedy), all at native context.
davidkoski
pushed a commit
that referenced
this pull request
Jul 21, 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 (#342). Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs #180, #228).
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
…plore#228) Softmax was applied to all 128 expert scores before top-k selection, so selected weights came from the full distribution rather than just the top-k. Moved softmax to after selection so it operates only on the chosen experts. Drops the renormalization step since softmax on top-k already produces a valid distribution. Also fuses norm + scale from 3 dispatches into one MLXFast.rmsNorm call.
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).
davidtai
added a commit
to davidtai/mlx-swift-lm
that referenced
this pull request
Oct 6, 2026
Integrate buffered and overlapping compression-window state across calls, preserving the causal pool mask and enabling the existing chunked-prefill regression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Two improvements to
Gemma4TextRouter:Softmax order: softmax was applied to all 128 expert scores before top-k
selection, so selected weights came from the full distribution rather than just
the top-k. Moved softmax to after selection so it operates only on the chosen
experts. Also drops the renormalization step (divide by sum) since softmax on
top-k already produces a valid distribution.
Fused norm: norm + scale used 3 separate dispatches (
norm(x),x * rootSize,x * scale). Fused into oneMLXFast.rmsNormcallwith
weight = scale * rootSize.Results
gemma-4-26b-a4b-it-4bit on M5 Max 128GB:
Decode speed: 32.7 → 35.9 tok/s (+9.8% from fused norm)
Raw completion (no chat template, greedy):
\n\nsدle- l_e_e_e_e_e\n\nin the ownle of kind-approachter-approachterGarbled characters → recognizable English. Both degrade to repetition without
a chat template, but routing fix produces coherent tokens.
Long generation (500 tokens, chat template, cryptography essay):
Both produce coherent essays. The fixed router covers more historical ground
in the same token budget (includes Scytale, Al-Kindi, frequency analysis
attribution that the original version omits).
Checklist
pre-commit run --all-filesto format my code