Repository navigation
fix(perf): align Gemma dense GeGLU with mlx-lm - #43
Merged
Merged
Conversation
Switch the Gemma2 and Gemma3 dense MLP to the tanh-approximation GeGLU that Python mlx-lm uses (`nn.gelu_approx`), replacing the erf-based precise GELU the Rust path had been running. The two activations are numerically close but not identical, and matching mlx-lm's choice both improves parity and lets the dense MLP reuse Gemma4's already-compiled `compiled_gelu_approx_mlp_forward` / `compiled_geglu_approx_activation` kernels instead of the separate precise-GELU ones. The mlxcel-core kernel doc comments are updated to reflect which models now drive each variant. The change also fuses the quantized attention prologue for both models. Gemma2 gains `UnifiedLinear::forward_split_rope_quantized` (QKV projection + split + reshape + transpose + RoPE), and Gemma3 gains `forward_split_norm_rope_quantized`, which additionally applies the GemmaRMSNorm on Q/K before RoPE via the new `fused_qkv_project_split_norm_rope` bridge. Both call sites fall back to the original step-by-step Rust path when the fused-QKV weights are not quantized, so behaviour is unchanged there. On M5 Max the alignment lifts `gemma2-2b-4bit` from 196.72 to 241.96 tok/s decode (81% to 100% of mlx-lm) and `gemma3-4b-4bit` from 146.43 to 182.16 tok/s (81% to 100%), with the `gemma3-4b-4bit` VLM row rising from 127.61 to 159.58 tok/s; `gemma-2b` and `gemma3-1b` stay healthy as guardrails. A greedy generation on `gemma-3-4b-it-4bit` confirms the fused Gemma3 norm-rope path plus the gelu_approx MLP still produces coherent text.
inureyes
added a commit
that referenced
this pull request
May 23, 2026
Bump version to 0.0.30. Highlights: unified pre-load memory estimator with the new mlxcel inspect subcommand and --estimate-memory preflight (#64, #65, #66, #67), Molmo v1 and InternVL VLM architectures (#41, #37), Gemma 3n / Phi-3.5 / Gemma / Jamba decode performance work (#42, #43, #44, #60, #62), and server --ctx-size as a shared parallel context budget (#57).
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
Switch the Gemma2 and Gemma3 dense MLP to the tanh-approximation GeGLU that Python mlx-lm uses (`nn.gelu_approx`), replacing the erf-based precise GELU the Rust path had been running. The two activations are numerically close but not identical, and matching mlx-lm's choice both improves parity and lets the dense MLP reuse Gemma4's already-compiled `compiled_gelu_approx_mlp_forward` / `compiled_geglu_approx_activation` kernels instead of the separate precise-GELU ones. The mlxcel-core kernel doc comments are updated to reflect which models now drive each variant. The change also fuses the quantized attention prologue for both models. Gemma2 gains `UnifiedLinear::forward_split_rope_quantized` (QKV projection + split + reshape + transpose + RoPE), and Gemma3 gains `forward_split_norm_rope_quantized`, which additionally applies the GemmaRMSNorm on Q/K before RoPE via the new `fused_qkv_project_split_norm_rope` bridge. Both call sites fall back to the original step-by-step Rust path when the fused-QKV weights are not quantized, so behaviour is unchanged there. On M5 Max the alignment lifts `gemma2-2b-4bit` from 196.72 to 241.96 tok/s decode (81% to 100% of mlx-lm) and `gemma3-4b-4bit` from 146.43 to 182.16 tok/s (81% to 100%), with the `gemma3-4b-4bit` VLM row rising from 127.61 to 159.58 tok/s; `gemma-2b` and `gemma3-1b` stay healthy as guardrails. A greedy generation on `gemma-3-4b-it-4bit` confirms the fused Gemma3 norm-rope path plus the gelu_approx MLP still produces coherent text.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
Bump version to 0.0.30. Highlights: unified pre-load memory estimator with the new mlxcel inspect subcommand and --estimate-memory preflight (lablup#64, lablup#65, lablup#66, lablup#67), Molmo v1 and InternVL VLM architectures (lablup#41, lablup#37), Gemma 3n / Phi-3.5 / Gemma / Jamba decode performance work (lablup#42, lablup#43, lablup#44, lablup#60, lablup#62), and server --ctx-size as a shared parallel context budget (lablup#57).
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.
Summary
Aligns the Gemma2 / Gemma3 dense MLP with Python mlx-lm's tanh-approximation GeGLU and fuses the quantized attention prologue for both models. This is both a parity correction and a decode speedup.
What changed
nn.gelu_approx(tanh-approx GeGLU), matching mlx-lm, instead of the erf-based precise GELU. The dense path reuses Gemma4's already-compiledcompiled_gelu_approx_mlp_forward/compiled_geglu_approx_activationkernels.mlxcel-corekernel doc comments are updated to reflect which models drive each variant.UnifiedLinear::forward_split_rope_quantized(QKV projection + split + reshape + transpose + RoPE).forward_split_norm_rope_quantized, which additionally applies GemmaRMSNorm on Q/K before RoPE via the newfused_qkv_project_split_norm_ropebridge.Benchmarks (M5 Max)
gemma2-2b-4bit(text)gemma3-4b-4bit(text)gemma3-4b-4bit(VLM)gemma-2bandgemma3-1bstay healthy as guardrails.Verification
cargo fmt --all -- --checkcleancargo clippy --all-targets --features metal,accelerate -- -D warningsclean (rebuilds the C++ bridge)gemma-3-4b-it-4bitproduces coherent text through the fused Gemma3 norm-rope path + gelu_approx MLP