Repository navigation
Conversation
No kernels and no drafts: the reference path, split into hidden/head for the engine. TF_GEMMA4_PIPELINE=1 decodes one step ahead on the GPU.
… default One-token steps run kernels/gemma/v1: stacked q/k/v and gate/up matmuls, qkv_norm, attn_tail, route, expert_gateup (8 experts' gate+up+GeGLU in one pass), expert_down (+ weighted sum) and moe_tail (post norms + residual + layer scalar + next input norm). Prompts keep mlx_lm's forward. M4 Pro, one-row synchronous step: 14.62 -> 12.65 ms (68.4 -> 79.1 tok/s). Against the same 4-bit weights run with fp32 activations (tools/gemma4_truth_eval.py, 5,742 sampled answer tokens): trimmed KL 0.00245 vs mlx_lm decode 0.00251, argmax = truth's 98.66% vs 98.61%. TF_GEMMA4_FUSED=0 keeps mlx_lm's forward.
Same per-lane sums, same bits (identical greedy tokens); expert down 1.69 -> 1.37 ms a token over 30 layers, about 0.2 ms off the pipelined step.
Both run as one threadgroup between matmuls on the step's critical path, and each load after a barrier waited its full latency. Same bits (identical greedy tokens); step 12.46 -> 11.86 ms.
|
Thanks @cshintov for the Gemma 4 contribution. It now decodes on the lane engine with every row exact, drafted, resumed and concurrent, but its single-stream decode is still a little behind mlx_lm, so it moves to 0.3.5.1 in the next few days while we speed up the one-row step. The PR stays open until then, and you'll be credited as the author when it lands. |
PR #10 as submitted, its eight commits in one, on the 0.3.0 serial engine: - families/gemma4: mlx_lm's Gemma 4 with the backbone and the tied, soft-capped vocabulary head apart. Prompts run mlx_lm's forward; one-token steps of the MoE checkpoints run the fused kernels. - kernels/gemma/v1: q|k|v and the dense gate|up stacked into one matmul each, the three head norms in one kernel, the post-attention norm and residual with the three norms that read it, top 8 of 128 experts with their weights, the 8 experts' gate/up with GeGLU in one pass over the input, their down projections with the weighted sum, and the post-feedforward norms, residual, layer scalar and next input norm in one kernel. - tools/gemma4_decode_bench.py and tools/gemma4_truth_eval.py: step timing, and each decode path's distance from the same 4-bit weights run with fp32 activations. - Tests on tiny random configs, the recipe page and README rows. Measured on an M4 Pro (MLX 0.31.2): 80-81 tok/s against 70 for mlx_lm's forward through the server, with KL against the fp32 reference on par with mlx_lm's own decode. On 0.3.5 the README's model table and the recipe index gain Gemma's rows; the PR's speed row and layout line have no counterpart in 0.3.5's README. 0.3.5 has no serial engine, so the next commits put the family on the lane engine.
|
Gemma 4 26B-A4B is in 0.3.6. Your family and fused one-row kernels shipped as 6ff8a47 under your name, and they now run on our lane engine, so every width is exact: 9/9 drafted equal to serial, and 16 concurrent streams equal their solo runs. Prompts process at or above mlx_lm from 2k to 64k on an M3 Ultra. Decode is 1.10-1.45x mlx_lm today, and a drafter and grouped experts come next. Thank you for a whole new family. |
|
Thanks for the merge 🫡 |
…eedy replies equal to the Python reference native/gemma4.zig serves the dense 31B on the lane host (context 32,768 by default); metal.zig registers gemma4 in mlx-q4g64 and refuses --speed-up and --learn for it by name; the tests that took gemma4 as an unserved type use gemma4_unified, which still is. gemma4_gate.py served: on an M5 Ultra the four prompts' greedy token SHAs equal the oracle's alone, with drafts and four at once; thinking stays in reasoning_content and a tool call parses. docs/recipes/gemma-4-native.md is the recipe; docs/credits.md credits gilby's #360 and cshintov's #10.
Adds a
gemma4family (Gemma 4,mlx-community/gemma-4-26b-a4b-it-4bit) and fused one-row decode kernels for its MoE checkpoints. Through the server it decodes about 15% faster than mlx_lm's forward on the same engine, and it is as close to an fp32 reference as mlx_lm's own decode.What changed:
families/gemma4: mlx_lm's Gemma 4 with the backbone and the tied, soft-capped head apart, on the serial engine, one token a round (no drafts: the family does not setmulti_row_exact).TF_GEMMA4_PIPELINE=1decodes one step ahead with alternating KV buffers on the full-attention layers. ProportionalRoPE's private_freqsare materialized at load so the engine thread can run the model.kernels/gemma/v1: per layer, q/k(/v) stacked into one matmul plus one kernel for the head norms; post-attention norm, residual and the three norms that read it in one kernel; dense gate/up stacked; routing (top 8 of 128, ties to the lower id) in one kernel; the 8 experts' gate/up + GeGLU in one pass over the input; the 8 experts' down + weighted sum; the three post-feedforward norms, residual, layer scalar and next input norm in one kernel. Before and after attention compiled per layer, graph handed to the GPU every 8 layers. RoPE, cache and attention stay mlx_lm's.TF_GEMMA4_FUSED=0keeps mlx_lm's forward; checkpoints outside the kernels' shapes (non-4-bit, other group sizes, per-layer inputs, shared KV) fall back with a message.tools/gemma4_decode_bench.py(step timing) andtools/gemma4_truth_eval.py(KL against the same 4-bit weights run with fp32 activations).docs/recipes/gemma-4.md, README model, speed and layout rows.tests/test_gemma4_family.py,tests/test_gemma4_kernels.py; the kernel-package test now allows a family without kernels.Measured on an M4 Pro, 64 GB, MLX 0.31.2. Through the server (
tools/bench_openai.py, 512 tokens, 5 reps, back to back), median tok/s:In-engine one step ahead: 13.34 to 11.7-11.9 ms a token (floor 7.9 ms at 273 GB/s). The recipe page records where the remaining time goes (latency in the per-layer kernel chain, not bandwidth) and what was tried and rejected.
Quality against fp32 activations, 5,742 tokens of the model's own answers to 12 prompts: trimmed KL 0.00245 (mlx_lm decode 0.00251), median 4.0e-6 (4.1e-6), top-1 98.66% (98.61%). The fused mean KL (0.00893) comes from 2 near-tie tokens; per-kernel checks at those positions match mlx_lm within bf16.
Tests: 206 passed, 68 skipped.