Repository navigation
models: a one-row prefill product runs on the decode's GEMVs - #53
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_andgemm_q6k_nt32_underTL_PROFILE=1).What
prefill_chunk_'sgemm: one row goes togemv_m, the decode's GEMV over the same rowwmat. It uses the decode's activation form (f32 whenexact) and its prepared-activation scratch.a16andmmq(CUDA's f16 activations and int8 GEMM) are set per chunk shape (set_forms).check_qwen: the long prompt again in chunks of n-1 and 1, held to the imperative logits it already has.Speed
bench_qwen_llama 512 128 3 0, M1 Pro, two alternating rounds, tok/s:tg128 is unchanged.
Verified
check_qwenis 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.CUDA
The same rows move from the int8 GEMM to the decode's GEMVs on CUDA too. This is not measured there yet:
check_qwenon E2B and its pp512 on the 3090 are to follow.