Repository navigation
models: Gemma 4 12B and 31B, kv heads a layer's own and a global layer's v its k - #55
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.
…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).
yhirose
force-pushed
the
gemma4-12b-31b
branch
from
November 7, 2026 14:41
8c553e8 to
d7d19e9
Compare
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.
Stacked on #53: merge #53 first. Its "chunks of N and 1" comparison goes through
O.agreehere, so the 12B holds it too.Gemma 4 12B and 31B (unsloth's Q4_K_M) load and run. master refused them at load:
attention.head_count_kvis an array there, read as a u32 (gguf: value type mismatch (have 9, want 4)).What
Config::NKV_L/nkv(l), read by the same helper as the per-layer MLP width (an array whose entries are all equal is kept as the scalar). The KV caches, the q|k|v splits, the norms' and RoPE's rows and the scratch take a layer's own count, on the decode, the batched prefill and the captured graph. The scratch is sized by the widest layer (qkv_proj(),attn_proj()).attn_v; its v is its k projection before k's norm and RoPE (HF'sattention_k_eq_v, llama.cpp'sVcur = wv ? mm(wv, cur) : Kcur).layer_qkv_partsreads 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.tools/qwen_oracle.pytakes both, and renders a Gemma 4 prompt with the file's own chat template (jinja2),LONG_USERrepeated until it passes the sliding window. E2B's prompt comes out identical to its header's (769 tokens); 12B's is 1534 tokens, past its 1024 window.bench/models/gemma4_12b_oracle_data.his the 12B's.sensitive(see below): its batched-vs-token-by-token comparisons are held by KL and the top token, as the chat's defaults already are, and its KV cache error is printed, not held. The KL rule is now one predicate in qwen.h (close_by_kl,KL_BOUND), which check_quant uses too.Why the 12B is held by KL
Against the oracle the 12B is tight: final norm 3.6e-4, top-5 logits 3.4e-5, 30 greedy tokens exact. But about 10 of the prompt's 1534 rows are ill-conditioned. numpy in f32 and numpy with the KV cache rounded to f16 give them entirely different values by layer 29 (position 240's v: -0.18, 0.31, ... vs 1.01, -1.07, ...), while their neighbours agree to 1e-4. It stays with both C++ paths in f32 (f32 cache, exact attention). llama.cpp's gemma4 has the same structure (attention scale 1, no attention softcap). Any two paths that round differently disagree on those rows, so the top-5 bounds fail (KV cache 5e-2 vs 3e-2, a 1088-token prompt's top-5 0.15 vs 1e-2) while the distributions agree (KL 5.6e-6 on the prompt, 2.5e-9 on the 1088-token one).
The KL rule still catches wiring errors on the 12B: a batched prefill with v's norm skipped fails at KL 9.6, one with k's RoPE a position off at KL 8.5.
Verified (RTX 3090, CUDA)
dump_logits ... fixedon those 10 models: every section byte-identical to master. The 27B's dump runs out of host memory on master too; its greedy chat answer is identical to master's.<turn|>("The capital of France is Paris.").Speed
bench_qwen_llama 512 128 5 0andllama-bench -p 512 -n 128 -r 5 -ngl 99(llama.cpp 19cce6919), two alternating rounds, tok/s:Notes
O.agree(...)(the 12B is held by KL there too). With models: a one-row prefill product runs on the decode's GEMVs #53 underneath,dump_logits ... fixedon the 10 models is byte-identical to models: a one-row prefill product runs on the decode's GEMVs #53 alone, and check_qwen is ALL OK on all of them and the 12B.