Skip to content

metal: the quantized GEMM steps K by 32 and reads a K-quant run of 16 - #54

Merged
yhirose merged 7 commits into
masterfrom
metal-qgemm-k32
Nov 7, 2026
Merged

yhirose merged 7 commits into
masterfrom
metal-qgemm-k32

Conversation

@yhirose

@yhirose yhirose commented Nov 7, 2026

Copy link
Copy Markdown
Owner

The block-quantized prefill GEMM stepped K by 16, so a thread's weights a
step were 8 (one scale decoded for 8 weights) and every 16 of K paid the
step's two barriers. Gemma 4 E2B's and E4B's MLPs sat at 2.7 TFLOPS on
Q4_K while bf16 with half sums reaches 3.9.

  • steel_body_ takes its K step as a template argument (BK, 16 by
    default), and TL_STEEL_KERNEL_BK sizes the tiles for it.
    TL_STEEL_KERNEL_ST forwards to it with 16, so the other GEMMs are
    unchanged.
  • The block formats' kernels (gemm_nt, nt32) step by 32. Kernel
    names and the host are unchanged.
  • Each reader states its run (RUN): the weights vec(n, k, v) gives from
    a k that is a multiple of RUN. Q4_K, Q5_K and Q6_K read 16 (one scale
    for them, with 32-bit loads); bf16, Q8_0, Q4_0 and Q4_1 read 8.
    SteelLoaderF static_asserts that a thread's weights a step are whole
    runs, and reads them run by run.
  • The sums stay f32, so the results do not change.

GEMM alone (M1 Pro, 512 rows, TFLOPS), master -> new: Q4_K 1536x12288
2.79 -> 2.87, 6144x1536 2.67 -> 2.75; Q5_K 2.53 -> 2.70, 2.36 -> 2.55;
Q6_K 6144x1536 2.14 -> 2.47. A step of 64 was slower.

pp512 (bench_qwen_llama, M1 Pro, tok/s), master -> new: Gemma 4 E2B 1512
-> 1565, E4B 495 -> 512, Llama 3.2 1B Q4_K_M 1301 -> 1342, Qwen3 0.6B
2508 -> 2564, Gemma 3 1B 1631 -> 1623.

Tests: four mutants of the readers and the loader (Q4_K's word, Q5_K's
fifth bit, Q6_K's half, the loader's run offset) each fail the existing
GEMM over block-quantized rows test. check_qwen is ALL OK on Gemma 4
E2B, Gemma 3 1B, PLaMo 0.8B, Qwen3 Q4, Qwen3.5 Q4, and Llama 3.2 1B
Q4_0, Q4_K_M and Q5_K_M. ctest passes in 10.7 s, as before.

The batched prefill ran every projection as a tile GEMM, also where the
chunk has one row: Gemma 4's layers past the KV-sharing line (their last
row alone, #48) and a prompt's one-token last chunk (a 513-token
prompt's). A tile GEMM wastes all its rows but one there; on Metal,
E2B's 20 shared layers took about a quarter of pp512 (about 94 of 354 ms
a prompt, gemm_q4k_nt32_ and gemm_q6k_nt32_ under TL_PROFILE=1).

- prefill_chunk_'s gemm: one row goes to gemv_m, the decode's GEMV over
  the same row wmat, in the decode's activation form (f32 when exact)
  and its prepared-activation scratch.
- The GEMV reads f32, so a16 and mmq (CUDA's f16 activations and int8
  GEMM) are set per chunk shape (set_forms): off for one row, from the
  start of a one-token chunk or from the KV-sharing line on. Every
  branch that reads them then writes and reads f32.
- check_qwen: the long prompt again in chunks of n-1 and 1, held to the
  imperative logits it already has. Feeding the GEMV the wrong activation
  fails this check alone on Gemma 3 1B (7.8e-01).

On CUDA this moves the same rows from the int8 GEMM to the decode's
GEMVs as well; not measured there yet.

pp512 (bench_qwen_llama 512 128 3 0, M1 Pro, two alternating rounds,
tok/s), master -> this: Gemma 4 E2B 1452-1457 -> 1687-1725, E4B 472 ->
503-504 (llama.cpp 2218 and 577-601). tg128 unchanged.

Tests: check_qwen ALL OK on Gemma 3 1B, Gemma 4 E2B, Llama 3.2 1B Q4,
Qwen2.5 0.5B f16, Qwen3 Q4, Qwen3.5 Q4 and PLaMo 3 0.8B (peak 1.1 to 4.7
GB). Its time on Gemma 3 1B is within the noise (42-50 s, three
alternating rounds each). ctest passes.
…r's v its k

Gemma 4 12B and 31B differ from E2B in two ways the forward did not take:

- attention.head_count_kv is one count a layer (12B: 8 on the local layers, 1 on
  the global ones; 31B: 16 and 4). Config reads it as it reads the per-layer MLP
  width (one layer_dim helper for both, an array of equal entries kept as the
  scalar): NKV_L and nkv(l) beside ff(l) and hd(l). The caches, the q|k|v splits,
  the norms' and RoPE's rows and the scratch (sized by the widest layer:
  qkv_proj(), attn_proj()) take a layer's own count, on the decode, the batched
  prefill and the captured graph alike.
- A global layer has no attn_v: its v is its k projection, before k's norm and
  RoPE (HF's attention_k_eq_v; llama.cpp's Vcur = wv ? mm(wv, cur) : Kcur).
  layer_qkv_parts reads k's rows for v too, so v leaves the fused GEMV as k does
  and every path downstream is a plain q|k|v layer's. Measured against copying k
  into v after the projection on the 12B: tg128 and pp512 within noise both ways
  (the extra rows are 0.13% of a token's weight bytes), so the copy's extra code
  path is not worth it.

tools/qwen_oracle.py takes both, and renders a Gemma 4 prompt with the file's own
chat template (jinja2), LONG_USER repeated until it passes the sliding window: E2B's
prompt is unchanged, 12B's is 1534 tokens. bench/models/gemma4_12b_oracle_data.h is
the 12B Q4_K_M's.

check_qwen: the 12B against its oracle (final norm 3.6e-4, top-5 logits 3.4e-5,
30 greedy tokens exact). About 10 of its prompt's rows are ill-conditioned: numpy
in f32 and numpy with the KV cache rounded to f16 give them entirely different
values by layer 29, their neighbours within 1e-4. Any two paths that round
differently disagree there (the batched prefill and the token-by-token steps: KV
cache 5e-2, a 1088-token prompt's top-5 logits 0.15), so the 12B's oracle is
marked sensitive: its paths are held as the chat's defaults are, by KL and the top
token (Oracle::agree), and its KV cache's error is shown, not held. A batched
prefill with v's norm skipped, or k's RoPE a position off, still fails it. The KL
rule is one predicate in qwen.h (close_by_kl, KL_BOUND), which check_quant uses too.
…zes per layer

- Config::qkv_proj(l), the q|k|v width of layer l, which attn_proj(l), the widest
  qkv_proj() and the fused-qkv check all read (the check mixed the layer's heads
  with the widest head width).
- k_eq_v is left empty where no layer's v is its k, as layer_dim leaves FF_L and
  NKV_L where every layer's is the same, so array_path() asks .empty() of each.
- bench_qwen_prefill sizes its layer-0 GEMMs and its one-layer attention by layer
  0's own widths (attn_proj(0), hd(0), ff(0), nkv(0)) rather than hand-written
  formulas over the widest.
- check_qwen takes the long prompt's imperative logits on the way through the
  prompt where it is the prompt's head, rather than running it token by token a
  second time (every oracle here; the 12B's check 110 s -> 90 s, its numbers the same).
… opens one

Gemma 4 12B's and 31B's chat templates add <|channel>thought\n<channel|> after the
model's turn when not thinking; E2B's does not. Without it the 12B writes the
channel itself before answering. The file's tokenizer.chat_template decides, read
by the literal it contains, as llama.cpp's llm_chat_detect_template tells
templates apart.
E4B is E2B's structure at another size: width 2560, two kv heads, FF
10240 on every layer, the last 18 layers reading the KV of the last
layer of their kind before them, and five local layers to a global one.
It needed no code change, only its oracle.

- gemma4_e4b_oracle_data.h: tools/qwen_oracle.py over unsloth's
  gemma-4-E4B-it-Q4_K_M.gguf (fingerprint 7d1f4da887ddfe03), the same
  769-token prompt as E2B's, past the 512-position window. 640 s, peak
  5.4 GB on an M1 Pro.
- check_qwen's table names it by its shape (42 layers, width 2560).

Before, check_qwen stops at "no oracle for this model". Now it is ALL OK
on Metal: final norm 1.34e-04, top-5 logits 3.64e-06, the greedy 30
tokens matching, the batched prefill and the sampling paths too (96 s,
peak 4.2 GB). chat_qwen answers a short question and stops at <turn|>.

Speed (M1 Pro, bench_qwen_llama 512 128 3 0 against llama-bench -p 512
-n 128 -r 3, two alternating rounds, tok/s): pp512 471-475 (llama.cpp
577-601), tg128 30.6-32.2 (29.4-29.5).
The block-quantized prefill GEMM stepped K by 16, so a thread's weights a
step were 8 (one scale decoded for 8 weights) and every 16 of K paid the
step's two barriers. Gemma 4 E2B's and E4B's MLPs sat at 2.7 TFLOPS on
Q4_K while bf16 with half sums reaches 3.9.

- steel_body_ takes its K step as a template argument (BK, 16 by
  default), and TL_STEEL_KERNEL_BK sizes the tiles for it.
  TL_STEEL_KERNEL_ST forwards to it with 16, so the other GEMMs are
  unchanged.
- The block formats' kernels (gemm_<fmt>_nt_, _nt32_) step by 32. Kernel
  names and the host are unchanged.
- Each reader states its run (RUN): the weights vec(n, k, v) gives from
  a k that is a multiple of RUN. Q4_K, Q5_K and Q6_K read 16 (one scale
  for them, with 32-bit loads); bf16, Q8_0, Q4_0 and Q4_1 read 8.
  SteelLoaderF static_asserts that a thread's weights a step are whole
  runs, and reads them run by run.
- The sums stay f32, so the results do not change.

GEMM alone (M1 Pro, 512 rows, TFLOPS), master -> new: Q4_K 1536x12288
2.79 -> 2.87, 6144x1536 2.67 -> 2.75; Q5_K 2.53 -> 2.70, 2.36 -> 2.55;
Q6_K 6144x1536 2.14 -> 2.47. A step of 64 was slower.

pp512 (bench_qwen_llama, M1 Pro, tok/s), master -> new: Gemma 4 E2B 1512
-> 1565, E4B 495 -> 512, Llama 3.2 1B Q4_K_M 1301 -> 1342, Qwen3 0.6B
2508 -> 2564, Gemma 3 1B 1631 -> 1623.

Tests: four mutants of the readers and the loader (Q4_K's word, Q5_K's
fifth bit, Q6_K's half, the loader's run offset) each fail the existing
GEMM over block-quantized rows test. check_qwen is ALL OK on Gemma 4
E2B, Gemma 3 1B, PLaMo 0.8B, Qwen3 Q4, Qwen3.5 Q4, and Llama 3.2 1B
Q4_0, Q4_K_M and Q5_K_M. ctest passes in 10.7 s, as before.
@yhirose
yhirose merged commit cd7b0f4 into master Nov 7, 2026
9 checks passed
@yhirose
yhirose deleted the metal-qgemm-k32 branch November 7, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant