Skip to content

fix(perf): align Gemma dense GeGLU with mlx-lm - #43

Merged
inureyes merged 1 commit into
mainfrom
fix/gemma-dense-geglu
May 20, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/gemma-dense-geglu

Conversation

@inureyes

Copy link
Copy Markdown
Member

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

  • GeGLU parity: the Gemma2/Gemma3 dense MLP now uses nn.gelu_approx (tanh-approx GeGLU), matching mlx-lm, instead of the erf-based precise GELU. The dense path reuses Gemma4's already-compiled compiled_gelu_approx_mlp_forward / compiled_geglu_approx_activation kernels. mlxcel-core kernel doc comments are updated to reflect which models drive each variant.
  • Fused quantized QKV:
    • Gemma2 → UnifiedLinear::forward_split_rope_quantized (QKV projection + split + reshape + transpose + RoPE).
    • Gemma3 → forward_split_norm_rope_quantized, which additionally applies GemmaRMSNorm on Q/K before RoPE via the new fused_qkv_project_split_norm_rope bridge.
  • Faithful fallback: both attention call sites fall back to the original step-by-step Rust path for non-quantized weights, so behaviour there is unchanged.

Benchmarks (M5 Max)

Model Before After vs mlx-lm
gemma2-2b-4bit (text) 196.72 241.96 tok/s 81% → 100%
gemma3-4b-4bit (text) 146.43 182.16 tok/s 81% → 100%
gemma3-4b-4bit (VLM) 127.61 159.58 tok/s —

gemma-2b and gemma3-1b stay healthy as guardrails.

Verification

  • cargo fmt --all -- --check clean
  • cargo clippy --all-targets --features metal,accelerate -- -D warnings clean (rebuilds the C++ bridge)
  • Greedy generation on gemma-3-4b-it-4bit produces coherent text through the fused Gemma3 norm-rope path + gelu_approx MLP

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 inureyes added type:performance Performance improvements area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata labels May 20, 2026
@inureyes
inureyes merged commit 3d74abd into main May 20, 2026
3 checks passed
@inureyes
inureyes deleted the fix/gemma-dense-geglu branch May 20, 2026 15:52
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).
@inureyes inureyes added this to the 0.1 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant