Skip to content

chat_qwen: Gemma 4's empty thought channel, where the file's template opens one - #56

Merged
yhirose merged 5 commits into
masterfrom
gemma4-chat-thought
Nov 7, 2026
Merged

yhirose merged 5 commits into
masterfrom
gemma4-chat-thought

Conversation

@yhirose

@yhirose yhirose commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Gemma 4 12B's and 31B's chat templates, when not thinking, add an empty thought channel after the model's turn (<|channel>thought\n<channel|>). E2B's template does not. Without it the 12B writes the channel itself before its answer.

What

  • chat_qwen's Gemma 4 prompt ends with gemma4_reply_prefix(m): the channel where the file's tokenizer.chat_template contains it as a string literal, else nothing.
  • There is no jinja here, so the template is read by what it contains, as llama.cpp's llm_chat_detect_template tells templates apart. The size of the model is not consulted.

Verified (RTX 3090, with #55 underneath so the 12B and 31B load)

prompt tokens before after reply
12B 21 25 before: <|channel>thought\n<channel|>The capital of France is Paris.; after: The capital of France is Paris.
31B 21 25 The capital of France is Paris.
E2B 21 21 The capital of France is Paris. (unchanged)

The 12B oracle in #55 renders the template with jinja2 and gets the same four tokens at the prompt's end.

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.
@yhirose
yhirose force-pushed the gemma4-chat-thought branch from 1d08800 to d69d74f Compare November 7, 2026 15:37
@yhirose
yhirose merged commit d69d74f into master Nov 7, 2026
9 checks passed
@yhirose
yhirose deleted the gemma4-chat-thought 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