Skip to content

models: a one-row prefill product runs on the decode's GEMVs - #53

Merged
yhirose merged 1 commit into
masterfrom
metal-qprefill
Nov 7, 2026
Merged

yhirose merged 1 commit into
masterfrom
metal-qprefill

Conversation

@yhirose

@yhirose yhirose commented Nov 7, 2026

Copy link
Copy Markdown
Owner

A one-row product in the batched prefill now runs on the decode's GEMVs instead of a tile GEMM.

Why

The prefill ran every projection as a tile GEMM, also where the chunk has one row:

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).

What

  • prefill_chunk_'s gemm: one row goes to gemv_m, the decode's GEMV over the same row wmat. It uses 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).
    • They are 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).

Speed

bench_qwen_llama 512 128 3 0, M1 Pro, two alternating rounds, tok/s:

master this llama.cpp
Gemma 4 E2B pp512 1452-1457 1687-1725 2218
Gemma 4 E4B pp512 472 503-504 577-601

tg128 is unchanged.

Verified

  • check_qwen is 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.
  • Its time on Gemma 3 1B is within the noise (42-50 s, three alternating rounds each).
  • ctest passes.

CUDA

The same rows move from the int8 GEMM to the decode's GEMVs on CUDA too. This is not measured there yet: check_qwen on E2B and its pp512 on the 3090 are to follow.

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.
@yhirose
yhirose merged commit f1e0969 into master Nov 7, 2026
9 checks passed
@yhirose
yhirose deleted the metal-qprefill 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