Repository navigation
Conversation
…he Mac decode kernels RowDecode serves Gemma 4 checkpoints without a MoE block (the 31B): attn_tail, the GeGLU MLP and a new dense_tail kernel. Decode projections read every MLX affine width the row kernels read (2-8 bits, groups of 32/64/128): the 4-bit row matvec, affine_rows for other widths, and the lane matmul where it reads the width. A stack of mixed widths (oQ overrides on q/k/v or gate/up) runs one matmul per run of equal widths; every kernel is row-invariant, so windows, concurrent rounds and one-row steps keep the same bits. The router reads any width; experts stay 4-bit. Rows-backend stacks are shared with mlx_lm modules as views, so the stack costs no second copy. check() accepts the dense layout and oQ overrides and names any width, group or mode no kernel reads. The tiny-model tests run every family test on MoE and dense layouts, uniform and oQ-mixed. oQ packs that bundle an MTP assistant (language_model.mtp.*) load with those tensors dropped; the family drafts with --drafter.
…or the row kernels by its shape Gemma 4's decode projections ran every row of a verify window or a concurrent round through the row matvec (4-bit) or affine_rows (other widths), so each row re-read the weights. Each linear now picks, once at load and from its width, group and shape alone, between those and the simdgroup matrix kernels: simd_qmm for 4-bit groups of 32/64, simd_qmm_bits for 4/5/6/8-bit groups of 64/128 (it now reads 4-bit codes, so 4-bit groups of 128 load their scales as stored). Every 5/6/8-bit and 4-bit group-128 linear takes the matrix kernels; 4-bit groups of 32/64 take them from a few million weights, so a 128-wide router stays on the row matvec. A linear's kernel never depends on the row count, and the one-row scalar twins are checked bit-equal to the matrix kernels per weight at load, so windows, concurrent rounds and one-row steps keep one set of bits. The M5 keeps its rows/lane choice. TF_GEMMA_DENSE=rows|matrix|auto overrides the choice for measurements.
|
Added a second commit (a6ff795): before M5, each Gemma 4 decode linear takes the simdgroup matrix kernels or the row kernels, chosen once at load from its bits, group and shape and never from the row count, per the rule in #154. 4-bit groups of 128 now load their scales as stored. On an M3 Ultra, chat decode with DFlash at 4 streams goes from 45.8 to 77.5 tok/s on a 31B oQ4e and from 172 to 214 on The M5 is unchanged: its default stays |
|
Thank you, @gilby. The 31B dense layout and oQ's per-layer mixed widths on the row kernels, with the 26B's bits unchanged and both bambocher packs serving exactly, is a careful extension. The Python engine is frozen (#286) and new work goes into the Zig engine, so this won't merge. The Zig engine has no Gemma 4 family yet, though its server already parses Gemma 4's tool calls and thought channel. When Gemma 4 comes to it, your rules carry over with your credit: one matmul per run of equal widths, a check that names any width, group or mode no kernel reads, and the dense tail. The Zig engine runs Metal kernels directly, so your row kernels are a natural place to start. |
…, MLX's and mlx-lm's ops, transformers' tower, z-lab's DFlash glue.metal ports gilby's gemma4-dense reference (a6ff795: RowDecode, _EMIT, qkv_prep, the tails and attention) and, where it calls MLX or mlx-lm, their kernels' arithmetic: gemma_rms_looped is MLX's rms_looped (rms_norm.metal, the kernel mx.fast.rms_norm runs past 4,096), gemma_embed MLX's dequantize, gemma_geglu and gemma_softcap mlx-lm's gemma4_text ops, gemma_rope_freqs its ProportionalRoPE frequencies (MIT, Apple). The image tower and image.zig follow transformers' modeling_gemma4 and Gemma4ImageProcessorPil (Apache 2.0, the HuggingFace Team); the drafter z-lab's MLX DFlash model (MIT, Z Lab).
…de kernels and the MLX ops around them Ports gilby's qkv_prep, attend (partials and merge), attn_tail and dense_tail (#360) with their rounding points, plus MLX's looped RMS norm, the scaled 4-bit embedding, mlx_lm's geglu and logit soft-cap as bf16 ops, and the global layers' proportional RoPE table from MLX's precise pow. tf-gemma4-glue checks every output against the Python engine's at 1, 2 and 16 rows, sliding and global layers: all bits equal.
…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.
…'s and mlx-lm's ops, transformers' tower, z-lab's DFlash glue.metal ports gilby's gemma4-dense reference (a6ff795: RowDecode, _EMIT, qkv_prep, the tails and attention) and, where it calls MLX or mlx-lm, their kernels' arithmetic: gemma_rms_looped is MLX's rms_looped (rms_norm.metal, the kernel mx.fast.rms_norm runs past 4,096), gemma_embed MLX's dequantize, gemma_geglu and gemma_softcap mlx-lm's gemma4_text ops, gemma_rope_freqs its ProportionalRoPE frequencies (MIT, Apple). The image tower and image.zig follow transformers' modeling_gemma4 and Gemma4ImageProcessorPil (Apache 2.0, the HuggingFace Team); the drafter z-lab's MLX DFlash model (MIT, Z Lab).
…de kernels and the MLX ops around them Ports gilby's qkv_prep, attend (partials and merge), attn_tail and dense_tail (ashhart#360) with their rounding points, plus MLX's looped RMS norm, the scaled 4-bit embedding, mlx_lm's geglu and logit soft-cap as bf16 ops, and the global layers' proportional RoPE table from MLX's precise pow. tf-gemma4-glue checks every output against the Python engine's at 1, 2 and 16 rows, sliding and global layers: all bits equal.
…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 ashhart#360 and cshintov's #10.
…, MLX's and mlx-lm's ops, transformers' tower, z-lab's DFlash glue.metal ports gilby's gemma4-dense reference (a6ff795: RowDecode, _EMIT, qkv_prep, the tails and attention) and, where it calls MLX or mlx-lm, their kernels' arithmetic: gemma_rms_looped is MLX's rms_looped (rms_norm.metal, the kernel mx.fast.rms_norm runs past 4,096), gemma_embed MLX's dequantize, gemma_geglu and gemma_softcap mlx-lm's gemma4_text ops, gemma_rope_freqs its ProportionalRoPE frequencies (MIT, Apple). The image tower and image.zig follow transformers' modeling_gemma4 and Gemma4ImageProcessorPil (Apache 2.0, the HuggingFace Team); the drafter z-lab's MLX DFlash model (MIT, Z Lab).
…, MLX's and mlx-lm's ops, transformers' tower, z-lab's DFlash glue.metal ports gilby's gemma4-dense reference (a6ff795: RowDecode, _EMIT, qkv_prep, the tails and attention) and, where it calls MLX or mlx-lm, their kernels' arithmetic: gemma_rms_looped is MLX's rms_looped (rms_norm.metal, the kernel mx.fast.rms_norm runs past 4,096), gemma_embed MLX's dequantize, gemma_geglu and gemma_softcap mlx-lm's gemma4_text ops, gemma_rope_freqs its ProportionalRoPE frequencies (MIT, Apple). The image tower and image.zig follow transformers' modeling_gemma4 and Gemma4ImageProcessorPil (Apache 2.0, the HuggingFace Team); the drafter z-lab's MLX DFlash model (MIT, Z Lab).
What this changes
RowDecodeserves Gemma 4 checkpoints without a MoE block (the 31B dense) withattn_tail, the GeGLU MLP and a newdense_tailkernel, and decode projections read every MLX affine width the row kernels read (2-8 bits, groups of32/64/128): the 4-bit row matvec,
affine_rowsfor other widths, and the lane matmul where it reads the width. A stackof mixed widths (oQ overrides on q/k/v or gate/up) runs one matmul per run of equal widths; every kernel is
row-invariant, so windows, concurrent rounds and one-row steps keep the same bits. The router reads any width; experts
stay 4-bit, and
check()names any width, group or mode no kernel reads. A uniform 4-bit checkpoint keeps its kernelsand its bits. Packs that bundle an MTP assistant load with those tensors dropped (the family drafts with
--drafter). Part of #322.Second commit: per-shape matrix kernels before M5. On M1–M4, Gemma 4's decode projections ran every row of a DFlash
verify window, and every stream of a
--parallelround, through the 4-bit row matvec oraffine_rows, so each rowre-read the weights. Following the #154 rule, each projection now picks its kernel once at load, from its width,
group and shape (bits, group, N, K), never from the row count:
simd_qmmfor 4-bit groups of 32/64, andsimd_qmm_bitsfor 4/5/6/8-bit groups of 64/128.simd_qmm_bitsnow reads 4-bit codes too, so 4-bit groups of128 load their scales as stored;
Every 5/6/8-bit and 4-bit g128 linear takes the matrix kernels; 4-bit g32/g64 takes them from 2²² weights. In
practice every attention, MLP and head linear of the 26B-A4B and 31B qualifies, and only a 4-bit 128-wide router stays
on the row matvec. The experts and the 8-bit router kernel are unchanged. The matrix kernels' one-row scalar twins are
checked bit-equal to the MMA kernel per weight at load, as
simd_qmm_backendalready does, and a weight whose twindiffers runs the MMA kernel at every count, so windows, concurrent rounds and one-row steps keep one set of bits for
each linear. M5 is unchanged: it keeps
rows, orlanewith--lane-kernels on.TF_GEMMA_DENSE=rows|matrix|autooverrides the choice, for A/B measurement only. The only shared file touched is
simd_qmm_bits.py(a 4-bit case inLWand a newreads());fits(), whichsimd_qmm_backendroutes on, is unchanged, so Qwen's routing and bits donot change.
The bits change on M1–M4 for the projections that move: the matrix kernels sum in a different order than the row
kernels. The new bits are closer to the fp32 product (below).
Receipt
Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: this
branch (0.6.4 + this change) against 0.6.4 (6ea5ade), MLX 0.32.3, M5 Ultra 256 GB. Checkpoints:
mlx-community/gemma-4-26b-a4b-it-4bit@ 0d77464 withz-lab/gemma-4-26B-A4B-it-DFlash@ 77d4202 (--drafter-bits 8);unigilby/gemma-4-31B-it-qat-oQ8e(uniform 8-bit, groups of 64; local copy of the upload) with a 31B DFlashdrafter (
target_layer_ids1, 12, 23, 35, 46, 57) at--drafter-bits 8 --lane-kernels on;unigilby/gemma-4-26B-A4B-it-qat-oQ4e(4-bit base, q/k/v and gate/up promoted to one width a layer, experts 4-bit);and the public mixed-stack packs
bambocher/gemma-4-26B-A4B-it-qat-oQ4e-mtp@ b1b7261 (4-bit g64, 56 per-layeroverrides) and
bambocher/gemma-4-31B-it-qat-oQ4e-mtp@ 23d3b9d (4-bit g64, 63 overrides). Servers:--parallel 4 --no-thinking --context 70000 --snapshot-dir none(8192 for theexactness-only runs).
Exactness, from
tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false:--levels 1,2,4 --alone --serial, T=1.0 and T=0, 3 reps: 0 failed,every stream equal to its solo run (3/3, 6/6, 12/12) and to
"draft": false. Everybench_openaireply (24 arun, greedy and sampled) has the same
token_shaon 0.6.4 and on this branch in all four runs: the bits areunchanged for this checkpoint.
--levels 1,2,4 --alone --serial, T=1.0 and T=0: 0 failed, every cell 3/3, 6/6, 12/12 alone and4/4 (T=1.0), 1/1 (T=0) against
"draft": false. Startup: windows of up to 16 rows reproduce one-row steps.--levels 2 --alone --serial: 6/6 alone, 2/2 (T=1.0) and 1/1 (T=0) against"draft": false, inboth prompts.
both load, windows of up to 16 rows reproduce one-row steps (rows backend for the 26B, lane for the 31B), and
--levels 2 --alone --serialgives 0 failed, 6/6 alone in every cell, 2/2 (T=1.0) and 1/1 (T=0) against"draft": false, code and chat.Decode speed before and after, from
tools/bench_openai.py(26B-A4B 4-bit with DFlash, medians of 5, before / after/ after / before):
The replies are token-identical across all four runs (above), so the T=1.0 chat gap is timing spread on the same work
(its five samples range 199-264 tok/s in every run). 31B dense oQ8e (before: refused, "this one lacks a MoE block in
every layer"): fibonacci-raw 40.3 tok/s at T=1.0, 65.2 at T=0; gpu-chat-no-think 31.8 and 33.5;
bench_concurrentaggregates at N=1/2/4, T=0: code 137.4 / 166.9 / 168.7, chat 33.1 / 55.1 / 90.0.
Prompt speed before and after, from
tools/prefill_cold.py(26B-A4B 4-bit, medians of 3, tok/s, same order):31B dense oQ8e (before: refused): 2K 1,302, 8K 1,287, 16K 1,232, 32K 1,129, 64K 917 tok/s.
On v0.6.5: this branch rebased onto v0.6.5 (609ca41) as 76c83c3. Mac host suite (M5 Ultra): 5065 passed, 1 failed, 596 skipped; the one failure,
test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on v0.6.5 too.Tests run, with pass, fail and skip counts: Mac host suite on this branch: 4757 passed, 1 failed, 595 skipped (the failure,
test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on 0.6.4 too).tests/test_gemma4_family.pyandtests/test_gemma4_kernels.py(changed) on 0.6.4's source: 65 failed, 39 passed(the dense and oQ-mixed cases); on this branch they pass; without MLX: 2 skipped.
M3 Ultra (no tensor units, so the rows backend), v0.6.5 vs this branch, MLX 0.32.3, mlx-lm 0.32.0,
unigilby/gemma-4-31B-it-qat-oQ8ewith the 31B DFlash drafter at--drafter-bits 8: v0.6.5 refuses at load ("TensorFold's Gemma 4 kernels cover the MoE checkpoints (mlx-community/gemma-4-26b-a4b-it-4bit); this one lacks a MoE block in every layer"). This branch serves it:token_shadrafted =="draft": false== d5a9c199f3c0; resumed == fresh: the same 7,343-token prompt twice on oneserver (greedy), the second served 7,340 tokens from the prompt cache,
token_shab12aba8cd79a both times; at--parallel 4,--levels 2,4 --alone --serial0 failed and 0 unequal in all 8 cells.tools/bench_openai.py --tokens 256 --reps 5, medians, tok/s: fibonacci-raw greedy 59.9, sampled 41.9; gpu-chat greedy 27.0, sampled 28.3. N=2 / N=4 greedy aggregate: code 64.5 / 65.5, chat 38.7 / 41.3.Checked on v0.6.5 (2026-10-04), M5 Ultra:
tools/prefill_cold.py(2k / 8k / 16k, 3 reps, median tok/s, alternated with v0.6.5, two runs each) onmlx-community/gemma-4-26b-a4b-it-4bit, which v0.6.5 serves: v0.6.5 8,366 / 8,493 / 8,082 and 8,400 / 8,525 / 8,107; this branch 8,349 / 8,489 / 8,077 and 8,359 / 8,511 / 8,097.Not run, and why: M1, M2 and M4 (no hardware). CUDA: not touched.
Receipt for the second commit (per-shape matrix kernels before M5)
Environment: MLX 0.32.3, Mac Studio M3 Ultra (no tensor units). Checkpoints: a TensorFold-shaped oQ4e of
google/gemma-4-31B-it-qat-q4_0-unquantized(4-bit g64 base with 5/6-bit promotions, not on the Hub) with the 31BDFlash drafter, and
unigilby/gemma-4-26B-A4B-it-qat-oQ4ewithz-lab/gemma-4-26B-A4B-it-DFlash,--drafter-bits 8.The served A/B ran the same commit on another 0.6.5-based tree; the diff applies to this branch unchanged, and the
test counts below are on this branch.
Served decode, from
tools/bench_openai.py(256 tokens, 3 reps) andtools/bench_concurrent.py --levels 4, rows(before) vs auto (after). Arms ran before / after / after / before, each on a fresh server with
--parallel 4 --context 65536; the two runs of each arm agree within about 2%. tok/s:--no-drafts, T=1 / T=0--no-draftsWindow costs at load, 31B oQ4e, from the server's own exactness check (ms by rows): before 1 → 33.7, 4 → 54.2,
8 → 94.4, 16 → 172.1, shared 32 → 355, 64 → 673; after 1 → 34.5, 4 → 42.7, 8 → 42.8, 16 → 65.2, shared 32 → 139,
64 → 239.
instruct model, and both arms emit degenerate text. On the 26B the changed bits turn "- - - …", which the drafter
predicts (227/236 accepted), into "-----…", which it doesn't (0–3/571), so single-stream "code" drops from 300 to
72 tok/s at T=0 although each round is cheaper (15 vs 18–26 ms). On the 31B "code" goes up (1 stream 57.8 → 59.7,
4 streams 69.5 → 121.1). The chat rows above are the meaningful ones.
fp32-referenced quality: relative RMS error of each projection against x·Wᵀ in fp32 with the dequantized weights,
on the activations a real 16-row window feeds it, mean / max over all projections:
unigilby/gemma-4-26B-A4B-it-qat-oQ4eNo projection is less accurate on matrix. 31B teacher-forced logits against mlx_lm's forward: top-1 identical, KL
≤ 0.007 on both backends.
Exactness after:
bench_concurrent --levels 1,2,4 --alone --serial, code and chat at T=1 and T=0 on both models:0 failed, every stream equal to its run alone (2/2, 4/4, 8/8) and to
"draft": false.Tests on the M3 Ultra:
tests/test_gemma4_matrix.py(new: every width and group, each row count 1..16 gives theone-row bits, stable run to run, within 1% of fp32 and no worse than rows,
autofixes one kernel a linear, mixedstacks) does not import on the first commit;
test_gemma4_family.pyandtest_gemma4_kernels.pynow run every caseon
rows,matrixandauto, and their new cases fail on the first commit (76 failed). On this branch the gemma4and simd_qmm suites give 284 passed and 47 skipped (the lane cases, which need M5). Host suite: 4777 passed, 958
skipped, 6 failed; the 6 are
tests/control/test_cli.py/test_pty.py, which need a terminal and fail the same wayon the first commit.
M5 Ultra sanity (default stays
rows): on the M5 Ultra the family's default resolves torows(andlanewith--lane-kernels on) before and after this commit. Tiny Gemma 4 models (MoE and dense, uniform and oQ-mixed) on both backends: logits of windows of 1..16 rows after 20- and 150-token prompts, drafted replies (greedy and sampled) and a four-stream concurrent decode hash identically on the first commit and this one (280 of 280). Host suite on the M5: 5041 passed, 620 skipped, 1 failed on the first commit; 5145 passed, 620 skipped, 1 failed on this one: the 104 new cases pass, nothing else moves, and the one failure istest_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, which fails on v0.6.5 too. The changedtest_gemma4_family.py/test_gemma4_kernels.pyon the first commit's source: 76 failed there as on the M3 Ultra; the lane cases run on the M5 and pass on both.Not run: M1, M2 and M4 (no hardware). CUDA: not touched.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.