From f1e0969a836394cfc770256499e8a78b6aa75344 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Nov 2026 00:40:52 -0500 Subject: [PATCH] models: a one-row prefill product runs on the decode's GEMVs 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. --- bench/models/check_qwen.cpp | 9 +++++++++ bench/models/qwen.h | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/bench/models/check_qwen.cpp b/bench/models/check_qwen.cpp index 86b534c..30afc31 100644 --- a/bench/models/check_qwen.cpp +++ b/bench/models/check_qwen.cpp @@ -478,6 +478,15 @@ static bool check_imperative(const qm::gg::model& m, const Oracle& O, bool held) long_ok = l5 < batched_bound; std::printf("\n%lld-token prompt, batched vs imperative: top-5 logits maxrel %.2e %s\n", (long long)longp.size(), l5, long_ok ? "OK" : "FAIL"); + // Again with its last chunk one token (as a 513-token prompt's is), which runs + // on the decode's GEMVs. + qm::reset_cache(B); + qm::prefill_batched(B, longp, (int64_t)longp.size() - 1); + const double o5 = qm::top5_maxrel(logits_of(B).data(), imp_long.data(), B.c.VOCAB); + const bool one_ok = o5 < batched_bound; + long_ok &= one_ok; + std::printf("the same in chunks of %lld and 1: top-5 logits maxrel %.2e %s\n", + (long long)longp.size() - 1, o5, one_ok ? "OK" : "FAIL"); } const bool f16_ok = !qm::can_prefill_batched(B, (int64_t)longp.size()) || check_f16_sums(B, longp, imp_long); diff --git a/bench/models/qwen.h b/bench/models/qwen.h index a0245df..26c15fc 100644 --- a/bench/models/qwen.h +++ b/bench/models/qwen.h @@ -1180,13 +1180,23 @@ inline int64_t prefill_chunk_(Model& M, int64_t T, bool exact = false, bool last // same source rather than taking it as a parameter that could disagree. int64_t pos0 = seq_cache(M).pos; // The f16-sum GEMMs read their activation as f16 where prefill_a16 says; `a16s` - // is that copy of `a`. - const bool a16 = prefill_a16(M, exact), mmq = prefill_mmq(M, exact); + // is that copy of `a`. One row (a one-token chunk, or the last row past the + // KV-sharing line) is a decode step's product: the decode's GEMVs, which read the + // weights once at their bandwidth where a tile GEMM wastes all its rows but one + // (E2B's 20 shared layers took a quarter of a Metal pp512), and read f32. + bool a16 = false, mmq = false; + auto set_forms = [&] { + a16 = T > 1 && prefill_a16(M, exact); + mmq = T > 1 && prefill_mmq(M, exact); + }; + set_forms(); // A GEMM a run of one format, each into its columns of y. The bf16 GEMMs // write y densely, so a bf16 run is the whole projection, as every bf16 build // loads it (load_wmat). auto gemm = [&](gpu::span a, gpu::span a16s, const wmat& W, gpu::span y, int64_t n, int64_t k) { + if (T == 1) + return gemv_m(W, a, y, exact ? gpu::act_form::f32 : M.gemv_form, sp(M.scratch.act)); for (const auto& p : W.parts) { const gpu::span w = W.at(p); if (p.type != bf16_type) { @@ -1244,6 +1254,7 @@ inline int64_t prefill_chunk_(Model& M, int64_t T, bool exact = false, bool last } pos0 += T - 1; T = 1; + set_forms(); norm(x, sp(L.an), sp(P.h), sp(P.h16), false); } }