Repository navigation
check_qwen: Gemma 4 E4B, an oracle over unsloth's Q4_K_M - #52
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).
… 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).
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.
Gemma 4 E4B runs on the code already in master; this adds its check.
What
bench/models/gemma4_e4b_oracle_data.h: the numpy oracle (tools/qwen_oracle.py) over unsloth'sgemma-4-E4B-it-Q4_K_M.gguf(weights fingerprint7d1f4da887ddfe03). Same 769-token prompt as E2B's, past the 512-position window.check_qwen: the oracle table gets E4B's row (gemma4, 42 layers, width 2560).E4B differs from E2B only in size: width 2560, two kv heads, FF 10240 on every layer, the last 18 layers sharing KV, and five local layers to a global one.
Verified (M1 Pro, Metal)
check_qwenstops at "no oracle for this model".chat_qwenanswers a short question and stops at<turn|>.Speed
bench_qwen_llama 512 128 3 0againstllama-bench -p 512 -n 128 -r 3, two alternating rounds, tok/s:The prefill gap is the Metal quantized prefill's, the next piece of work.