gemma4k, arch/gemma4: attn_paged_qrow -- q in the fused row only where gemma4k reads it

8772d5b handed the sliding layers' attn_paged q as a column slice of the fused [q|k|v] row. gemma4k
reads that; libr4d's paged attention reads q only dense and refuses the slice with RAD_E_STRIDE,
which stops the engine at the first step. Under attn_paged the first library in --kernels wins, so
the slice was only safe with gemma4k ahead of libr4d -- and the deploy script's default order
(libr4d,gemma4k,libavx) took the server down at layer 0's attention on both profile runs.

gemma4k now declares the same kernels a second time under attn_paged_qrow: attn_paged's schema
field for field, with q read at its own row pitch. Only gemma4k declares it, so it resolves there or
nowhere. The sliding layers ask for it once; where it resolves they read q in place (no lift), and
where it does not -- a hierarchy without gemma4k, or RADIANCE_GEMMA4_FUSE=0 -- q is lifted into a
dense block by a `cast` again and attn_paged reads that, which every attention library takes. The
lift's buffer is declared only then, since an unnamed buffer is live for the whole program. A miss
is the swa group's first op carrying a block_size, and answers rad_kv_block_size with the
RAD_KV_BLOCK it declares, as attn_paged would. No libref row, as the joins.

The kernel order still matters for speed -- gemma4k first is the order this model was measured with
(ARCHITECTURES.md) -- but no longer for whether the server runs. A startup note says which form
was taken.

arch_test: the slice reaches attn_paged_qrow and never attn_paged; refused, every sliding layer
lifts q for attn_paged; a decode step issues either form; and the three older gemma4 tests
count the sliding layers' attention under its new name. docs: OPS.md's row and paragraph (and a
line of the joins' text reflowed to the width, as one of gemma4.cpp's), ARCHITECTURES.md's kernel
paragraph, which now names expert_scale too, and GUIDE's FUSE row.

Not yet run on the cards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 committed 2026-10-12 14:40:04 -05:00
1 parent e173365823
commit 73c3dc38be
6 files changed
+168 -41

No files matched your search

+40 -13
View File
@@ -217,7 +217,7 @@ struct Layer {
int full = 0;
/* attention */
rad_weight w_anorm = 0, w_qkv = 0, w_q = 0, w_k = 0, w_qn = 0, w_kn = 0, w_o = 0, w_apost = 0;
rad_op op_anorm = 0, op_qkv = 0, op_qknr = 0;
rad_op op_anorm = 0, op_qkv = 0, op_qknr = 0, op_lift = 0;
rad_op op_q = 0, op_k = 0, op_vnorm = 0, op_qn = 0, op_kn = 0, op_qrope = 0, op_krope = 0;
rad_op op_store = 0, op_attn = 0, op_o = 0, op_ar_o = 0, op_apost = 0, op_add_a = 0;
/* dense arm */
@@ -269,7 +269,8 @@ struct Model {
double escale = 1.0, softcap = 0.0;
rad_buf b_x = 0, b_h = 0, b_o = 0;
rad_buf b_qkv = 0, b_atts = 0; /* sliding */
rad_buf b_qkv = 0, b_qs = 0, b_atts = 0; /* sliding; q_s where q is lifted */
int qrow = -1; /* attn_paged_qrow resolved: -1 not asked yet, then 0 or 1 */
rad_buf b_qf = 0, b_kf = 0, b_vf = 0, b_attf = 0; /* full */
rad_buf b_ff2 = 0, b_gu = 0, b_act = 0; /* the arms' join; the dense arm */
rad_buf b_rn = 0, b_logit = 0, b_ids = 0, b_ew = 0, b_es = 0, b_hx = 0;
@@ -977,8 +978,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_NOWEIGHTS),
{buf}, {buf});
};
auto attn = [&](const Kind& k, rad_buf q, rad_buf out) {
return rw(b, RAD_OP(b, "attn_paged",
auto attn = [&](const Kind& k, rad_buf q, rad_buf out, const char* op = "attn_paged") {
return rw(b, RAD_OP(b, op,
RAD_PARAMS(RAD_RANGE("q_len", 1, T), RAD_INT("head_dim", k.hd),
RAD_INT("gqa", k.nh / k.nkv), RAD_INT("causal", 1),
RAD_INT("window", k.window), RAD_INT("n_head", k.nh),
@@ -1001,7 +1002,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
/* THE SANDWICH JOINS: asked of the kernel libraries as one op each (gemma4k serves them) and
* declared as the chain where nothing does -- or where RADIANCE_GEMMA4_FUSE=0 asks for the
* chain, the A/B switch, which turns off gemma4k's `expert_scale` with them. The fused kernels round where the chain rounds (gemma4k/g4k_norm.hip).
* chain, the A/B switch, which turns off gemma4k's `expert_scale` and `attn_paged_qrow` with
* them. The fused kernels round where the chain rounds (gemma4k/g4k_norm.hip).
* libref has no row for the joins on purpose, so "nothing serves them" stays reachable. */
const char* fuse_env = std::getenv("RADIANCE_GEMMA4_FUSE");
const bool fuse_joins = !(fuse_env && fuse_env[0] == '0');
@@ -1089,12 +1091,30 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_F64("wadd", 0.0), RAD_INT("v_norm", 1)),
RAD_WEIGHTS(y.w_qn, y.w_kn)),
{m.b_qkv}, {m.b_qkv});
/* Q IS READ WHERE THE PROJECTION LEFT IT, a column slice of the fused [q|k|v] row:
* gemma4k's attention takes q at any row pitch, so there is no copy into a dense block
* -- a launch and a [T, q] round trip a sliding layer. libr4d's attention reads q only
* dense and refuses the slice by name (RAD_E_STRIDE), which is one more reason the
* order is `--kernels gemma4k,libr4d` for this model. */
y.op_attn = attn(k, m.b_qkv, m.b_atts);
/* Q IS READ WHERE THE PROJECTION LEFT IT, a column slice of the fused [q|k|v] row, by
* `attn_paged_qrow` -- attn_paged with q at its own row pitch -- which only gemma4k
* declares, so no `--kernels` order can hand the slice to a library that reads q only
* dense (libr4d's refuses it with RAD_E_STRIDE, which stops the engine). That saves the
* lift: a launch and a [T, q] round trip a sliding layer. Where the op does not
* resolve, or RADIANCE_GEMMA4_FUSE=0 asks for the chain, q is lifted into a dense block
* for attn_paged, which every attention library takes. Asked once, because every
* sliding layer asks the same question; a miss is still the group's first op carrying
* a block_size, and answers rad_kv_block_size with the RAD_KV_BLOCK it declares. */
if (fuse_joins && m.qrow != 0) {
y.op_attn = attn(k, m.b_qkv, m.b_atts, "attn_paged_qrow");
m.qrow = y.op_attn ? 1 : 0;
}
if (!y.op_attn) {
/* declared here, not with the others: a buffer no op names is live for the whole
* program, and where q is read in place nothing names this one */
if (!m.b_qs) m.b_qs = decl_b(b, nm.f("q_s"), RAD_BF16, {T, k.q()});
y.op_lift = rw(b, RAD_OP(b, "cast",
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", k.q()),
RAD_STR("from", "bf16"), RAD_STR("to", "bf16")),
RAD_NOWEIGHTS),
{m.b_qkv}, {m.b_qs});
y.op_attn = attn(k, m.b_qs, m.b_atts);
}
y.op_store = kv_store(k, m.b_qkv, m.b_qkv);
y.op_o = gemm(T, E, k.q(), y.w_o, m.b_atts, m.b_o);
} else {
@@ -1467,6 +1487,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
n_joins, (long long)(m.moe ? 2 * m.n_layer : m.n_layer),
!fuse_joins ? " -- RADIANCE_GEMMA4_FUSE=0, the rmsnorm / add / mul_weight chain"
: n_joins == 0 ? " -- not served (load gemma4k), the rmsnorm / add / mul_weight chain" : "");
if (m.ks.nh > 0)
rad_note(b, "sliding attention: q %s", m.qrow == 1
? "read in the fused qkv row (gemma4k attn_paged_qrow)"
: "lifted into a dense block for attn_paged (attn_paged_qrow not served, or "
"RADIANCE_GEMMA4_FUSE=0)");
if (m.moe) {
auto how = [&](int n_fp8) {
return n_fp8 == m.n_layer ? "block fp8" : n_fp8 == 0 ? "bf16" : "mixed";
@@ -1625,8 +1650,10 @@ static void step(RadCtx* c, const RadBatch* batch) {
RAD_ISSUE(c, y.op_qknr, brows(m.b_qkv, T), pos, RAD_W(y.w_qn), RAD_W(y.w_kn));
RAD_ISSUE(c, y.op_store, bcol(m.b_qkv, qd, kvd, T), bcol(m.b_qkv, qd + kvd, kvd, T),
praw(slot, RAD_I32, T), kv_cache(k.kv, (int)li));
RAD_ISSUE_N(c, y.op_attn, batch->max_q_len, bcol(m.b_qkv, 0, qd, T), kv_cache(k.kv, (int)li),
bt, su, RAD_NONE, RAD_NONE, cu, brows(m.b_atts, T));
if (y.op_lift) RAD_ISSUE(c, y.op_lift, bcol(m.b_qkv, 0, qd, T), brows(m.b_qs, T));
RAD_ISSUE_N(c, y.op_attn, batch->max_q_len,
y.op_lift ? brows(m.b_qs, T) : bcol(m.b_qkv, 0, qd, T),
kv_cache(k.kv, (int)li), bt, su, RAD_NONE, RAD_NONE, cu, brows(m.b_atts, T));
RAD_ISSUE(c, y.op_o, brows(m.b_atts, T), RAD_W(y.w_o), brows(m.b_o, T));
} else {
RAD_ISSUE(c, y.op_q, brows(m.b_h, T), RAD_W(y.w_q), brows(m.b_qf, T));
+7 -4
View File
@@ -520,10 +520,13 @@ expert parity, so a rank holds an even number of experts.
**Kernels.** `--kernels gemma4k,libr4d,libavx`, **gemma4k first.** It serves all of the trunk's
attention -- the sliding layers (h256, gqa 2) and the full layers at gqa 8, 4 and 2 (one or two,
four, eight ranks) -- plus `pair_argmax` and the two sandwich-norm joins (`norm_add_norm`,
`join_add_scale`), which the plugin declares as the rmsnorm / add chain where nothing serves them;
the startup note says how many were fused. libr4d has h256/gqa2 and h512/gqa8 rows as well,
written for the dense 31B; with gemma4k first the 26B-A4B runs on the kernels it was measured with.
four, eight ranks) -- plus `pair_argmax` and the fusions: the two sandwich-norm joins
(`norm_add_norm`, `join_add_scale`), `expert_scale`, and `attn_paged_qrow`, the sliding layers'
attention reading q where the fused qkv projection left it. The plugin declares each one's unfused
form where nothing serves it -- the rmsnorm / add chain, `embed_lookup` + `mul`, a `cast` lifting q
for `attn_paged` -- and the startup notes say which were taken. libr4d has h256/gqa2 and h512/gqa8
rows as well, written for the dense 31B; with gemma4k first the 26B-A4B runs on the kernels it was
measured with.
Without `gemma4k` at all, the full layers have no kernel at four and eight ranks, and a drafter head
sharded over more than one rank has no `pair_argmax`.
+1 -1
View File
@@ -1859,7 +1859,7 @@ rad-kbench --home $RADIANCE_HOME --max-cases 1 # are the kernels correct at al
| `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) |
| `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) |
| `RADIANCE_G4K_WMMA` | `0` keeps `gemma4k`'s prefill attention on its scalar kernel instead of the WMMA one. An A/B switch and a way out if the matrix path misbehaves on a card; read once |
| `RADIANCE_GEMMA4_FUSE` | `0` makes `arch/gemma4` declare the chains instead of `gemma4k`'s fusions: the rmsnorm / add chain for the sandwich joins (`norm_add_norm`, `join_add_scale`), and `embed_lookup` + `mul` for `expert_scale`. An A/B switch; read at declare |
| `RADIANCE_GEMMA4_FUSE` | `0` makes `arch/gemma4` declare the chains instead of `gemma4k`'s fusions: the rmsnorm / add chain for the sandwich joins (`norm_add_norm`, `join_add_scale`), `embed_lookup` + `mul` for `expert_scale`, and a `cast` lifting q for `attn_paged` for `attn_paged_qrow`. An A/B switch; read at declare |
There is also a family of instruments for engine development — `RADIANCE_DEBUG_*` (residual probes,
idle scrubs, routing and issue dumps), `RADIANCE_OP_ORACLE*` (named ops checked against `libref` on
+16 -5
View File
@@ -294,13 +294,14 @@ rounding.
-log(max_timescale) / max(n/2 - 1, 1))`, each step in f32 as the reference's torch expressions
are, then rounded.
### gemma4k's fusions: the sandwich-norm joins and `expert_scale` — no reference row
### gemma4k's fusions: the sandwich-norm joins, `expert_scale`, `attn_paged_qrow` — no reference row
| op | params | operands |
|---|---|---|
| `norm_add_norm` | `M`(range) `n` `eps`:f64 `dtype` | `a`, `w_a`w, `x`:inout, `w_y`w → `y` |
| `join_add_scale` | `M`(range) `n` `eps`:f64 `dtype` | `d`, `w_d`w, `r`, `w_r`w, `w_j`w, `x`:inout, `s`w, `w_n`w? → `y`? |
| `expert_scale` | `M`(range) `n_expert` `top_k` `dtype` | `ids`, `w_e`w, `ew`:inout |
| `attn_paged_qrow` | `q_len`(range) `head_dim` `gqa` `block_size`/der `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out` |
Gemma's sandwich norms, each one kernel in place of a chain of `rmsnorm`, `add` and `mul_weight`.
`norm_add_norm` is `t = rmsnorm(a) * w_a; x += t; y = rmsnorm(x) * w_y` -- a post-norm, the
@@ -308,16 +309,26 @@ residual add, the next pre-norm. `join_add_scale` is `x = (x + rmsnorm(rmsnorm(d
rmsnorm(r) * w_r) * w_j) * s` -- two arms' post-norms, their sum, its norm, the residual add and a
one-element layer scalar (Gemma 4's MoE feed-forward) -- and, given `w_n` and `y` (both or
neither), `y = rmsnorm(x) * w_n` over the `x` it writes: the next layer's input norm, or the final
norm after the last layer, which would otherwise be a launch of its own reading that `x` back. Every value the chain stores is rounded to
the activation dtype at the same point, so the two differ from the chain only in the order a sum
of squares is added in: with the same order they are bit-identical to it, and against a chain
summed in another block width a norm factor can differ in its last bit.
norm after the last layer, which would otherwise be a launch of its own reading that `x` back.
Every value the chain stores is rounded to the activation dtype at the same point, so the two
differ from the chain only in the order a sum of squares is added in: with the same order they are
bit-identical to it, and against a chain summed in another block width a norm factor can differ in
its last bit.
`expert_scale` is `ew[t, k] = ew[t, k] * w_e[ids[t, k]]` over `[M, top_k]`, `w_e` an `[n_expert]`
table and an id outside it reading 0: Gemma 4's per_expert_scale on the renormalised routing
weights, in place of an `embed_lookup` of the table by the ids and a `mul` into the weights. One
product rounded once, so the pair's value bit for bit.
`attn_paged_qrow` is `attn_paged` -- the same parameters, operands and values -- with `q` read at
its own row pitch: a column slice of a wider row, the q part of a fused `[q|k|v]` projection, read
where it lies instead of lifted into a dense block by a `cast` first. It is a name of its own
because an `attn_paged` kernel may refuse a pitched `q` (libr4d's do, with `RAD_E_STRIDE`, at the
first step), and under `attn_paged` whichever library is first in `--kernels` wins the op: an
architecture handing it the slice would depend on an order it cannot see. Asked for by name, the op
resolves to a library that reads the pitch or to nothing, and the architecture lifts `q` for
`attn_paged` where it is nothing.
**libref has no row for any of them, on purpose.** An architecture asks for a join and declares the
chain where it does not resolve (`rad_op_resolved`), and a reference row would resolve it
everywhere -- so a model loaded without the library that serves the join would resolve it to the
+53
View File
@@ -96,6 +96,38 @@ static const RadOperandSpec oEscale[] = {
{ "w_e", RAD_OPD_WEIGHT, 0 },
{ "ew", RAD_OPD_INOUT, 0 },
};
/* attn_paged_qrow is attn_paged's schema restated field for field under a name only this library
* declares. The attention kernels below take q at any row pitch, so q can be read where a fused
* [q|k|v] projection left it; libr4d's paged attention reads q only dense and refuses the slice by
* name (RAD_E_STRIDE), which stops the engine at the first step. Under attn_paged, whichever
* library comes first in `--kernels` wins the op, so an architecture handing it the slice depends
* on an order it cannot see. Under its own name it resolves to this library or to nothing, and the
* architecture falls back to the lift and attn_paged where it is nothing. No libref row, as the
* joins, for the same reason. */
static const RadParamSpec pAttnQrow[] = {
{ "q_len", RAD_P_INT, RAD_REQUIRED, 0 },
{ "head_dim", RAD_P_INT, RAD_REQUIRED, 0 },
{ "gqa", RAD_P_INT, RAD_REQUIRED, 0 },
{ "block_size", RAD_P_INT, RAD_DERIVED, 0 },
{ "causal", RAD_P_INT, RAD_REQUIRED, 0 },
{ "window", RAD_P_INT, RAD_REQUIRED, 0 },
{ "q_dtype", RAD_P_STR, RAD_REQUIRED, 0 },
{ "kv_dtype", RAD_P_STR, RAD_REQUIRED, 0 },
{ "scale", RAD_P_F64, RAD_OPTIONAL, 0 },
{ "max_ctx", RAD_P_INT, RAD_OPTIONAL, 0 },
{ "n_head", RAD_P_INT, RAD_OPTIONAL, 0 },
{ "max_seqs", RAD_P_INT, RAD_OPTIONAL, 0 },
};
static const RadOperandSpec oAttnQrow[] = {
{ "q", RAD_OPD_IN, 0 },
{ "kv_cache", RAD_OPD_IN, 0 },
{ "block_table", RAD_OPD_IN, 0 },
{ "seqused", RAD_OPD_IN, 0 },
{ "k_descale", RAD_OPD_IN, 1 },
{ "v_descale", RAD_OPD_IN, 1 },
{ "cu_seqlens", RAD_OPD_IN, 1 },
{ "out", RAD_OPD_OUT, 0 },
};
static const RadOpSchema kSchemas[] = {
{ "pair_argmax", ROW(pPick), ROW(oPick),
"idx[m] = the id of the largest value among pairs[m, 0..n), each pair an int32 id in the first "
@@ -115,6 +147,10 @@ static const RadOpSchema kSchemas[] = {
"ew[t, k] = ew[t, k] * w_e[ids[t, k]] over [M, top_k], w_e an [n_expert] table, an id outside "
"it reading 0; one product rounded once, so the embed_lookup + mul pair's value bit for bit. "
"Gemma 4's per_expert_scale on the renormalised routing weights." },
{ "attn_paged_qrow", ROW(pAttnQrow), ROW(oAttnQrow),
"attn_paged, parameter for parameter and value for value, with q read at its own row pitch: "
"a column slice of a wider row -- the q part of a fused [q|k|v] projection -- read where it "
"lies rather than lifted into a dense block first." },
};
/* ==================================================================== operand descriptions
@@ -411,10 +447,25 @@ static const RadConstraint cEscale[] = { C_GE("top_k", 1), C_LE("top_k", 64), C
#define G4K_ATTN_PAIR(HD, G) \
G4K_ATTN_ROW(HD, G, bf16kv, cH##HD##x##G##B, "bf16") \
G4K_ATTN_ROW(HD, G, fp8kv, cH##HD##x##G##F, "fp8-e4m3 (per-(seq,head) descales)")
/* attn_paged_qrow: the same kernel and the same constraints. NO opd_shape -- no libref row, so no
* oracle; g4k_emu's attention cases already take q as a pitched slice of a wider row. */
#define G4K_QROW_ROW(HD, G, KVN, C, KVDOC) \
{ "attn_paged_qrow_h" #HD "_gqa" #G "_" #KVN, "attn_paged_qrow", "attn", \
"paged attention reading q at its own row pitch, any window, causal or not", \
"head_dim " #HD ", " #G " queries per kv head, paged block 16, any q_len, varlen", \
"bf16 query (any row pitch), " KVDOC " kv cache, bf16 out, f32 partials", \
RAD_DOMAIN_DEVICE, 10, ROW(C), NOTUNE, \
nullptr, nullptr, g4k_attn_h##HD##_gqa##G##_##KVN, g4k_attn_h##HD##_scratch, nullptr, \
nullptr, nullptr, nullptr, nullptr, nullptr },
#define G4K_QROW_PAIR(HD, G) \
G4K_QROW_ROW(HD, G, bf16kv, cH##HD##x##G##B, "bf16") \
G4K_QROW_ROW(HD, G, fp8kv, cH##HD##x##G##F, "fp8-e4m3 (per-(seq,head) descales)")
static const RadKernelInfo kKernels[] = {
G4K_ATTN_PAIR(256, 1) G4K_ATTN_PAIR(256, 2) G4K_ATTN_PAIR(256, 4) G4K_ATTN_PAIR(256, 8)
G4K_ATTN_PAIR(512, 1) G4K_ATTN_PAIR(512, 2) G4K_ATTN_PAIR(512, 4) G4K_ATTN_PAIR(512, 8)
G4K_QROW_PAIR(256, 1) G4K_QROW_PAIR(256, 2) G4K_QROW_PAIR(256, 4) G4K_QROW_PAIR(256, 8)
G4K_QROW_PAIR(512, 1) G4K_QROW_PAIR(512, 2) G4K_QROW_PAIR(512, 4) G4K_QROW_PAIR(512, 8)
/* Described as libref describes it, so rad-kbench runs both arms against the oracle. */
{ "pair_argmax_f32", "pair_argmax", "select",
@@ -472,6 +523,8 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, pick_host, nullptr, nullptr, nullptr, shape_pick,
nullptr, nullptr, nullptr },
};
#undef G4K_QROW_PAIR
#undef G4K_QROW_ROW
#undef G4K_ATTN_PAIR
#undef G4K_ATTN_ROW
+51 -18
View File
@@ -3444,7 +3444,9 @@ TEST(a_gemma4_moe_trunk_declares_both_arms_and_the_joins) {
RadBuildCtx ctx = build_ctx();
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
CHECK_EQ(count_op(b, "attn_paged"), 6);
/* five sliding layers read q in the fused row (attn_paged_qrow), the full one attn_paged */
CHECK_EQ(count_op(b, "attn_paged_qrow"), 5);
CHECK_EQ(count_op(b, "attn_paged"), 1);
CHECK_EQ(count_op(b, "kv_store"), 6);
/* One join after attention and one after the two feed-forward arms, a layer. */
CHECK_EQ(count_resolved(b, "norm_add_norm"), 6);
@@ -3474,11 +3476,11 @@ TEST(gemma4_moe_kv_groups_are_both_full_and_the_window_rides_the_op) {
CHECK_EQ((int)g.d.kind, RAD_KV_FULL);
CHECK_EQ(g.d.window, 0);
}
const RecOp* sl = find_op(b, "attn_paged", 0);
const RecOp* sl = find_op(b, "attn_paged_qrow", 0);
REQUIRE(sl != nullptr);
CHECK_EQ(pint(sl, "head_dim"), 256);
CHECK_EQ(pint(sl, "window"), 1024);
const RecOp* fu = find_op(b, "attn_paged", 5);
const RecOp* fu = find_op(b, "attn_paged", 0);
REQUIRE(fu != nullptr);
CHECK_EQ(pint(fu, "head_dim"), 512);
CHECK_EQ(pint(fu, "gqa"), 8);
@@ -3531,8 +3533,10 @@ TEST(gemma4_falls_back_to_the_chain_where_no_join_resolves) {
CHECK_EQ(count_issued(fused, "join_add_scale"), 6);
CHECK_EQ(count_issued(chain, "norm_add_norm"), 0);
CHECK_EQ(count_issued(chain, "join_add_scale"), 0);
CHECK_EQ(count_issued(fused, "attn_paged"), 6);
CHECK_EQ(count_issued(chain, "attn_paged"), 6);
for (const auto* v : { &fused, &chain }) {
CHECK_EQ(count_issued(*v, "attn_paged_qrow"), 5);
CHECK_EQ(count_issued(*v, "attn_paged"), 1);
}
/* The chain is longer, by the norms and adds the joins fold in. */
CHECK(count_issued(chain, "rmsnorm") > count_issued(fused, "rmsnorm"));
CHECK(count_issued(chain, "add") > count_issued(fused, "add"));
@@ -3625,20 +3629,49 @@ TEST(gemma4_dflash_taps_are_live_from_the_layer_that_writes_them) {
CHECK_EQ(taps, 3);
}
/* The sliding layers' attention reads q where the fused projection left it: no copy op between
* the qkv projection and the attention, and the attention's declared reads name the fused row. */
/* The sliding layers' attention reads q where the fused projection left it -- no copy between the
* qkv projection and the attention -- where attn_paged_qrow is served, and lifts q for attn_paged
* where it is not. */
TEST(gemma4_sliding_attention_reads_q_in_the_fused_row) {
RadBuilder b;
RadModelMeta meta = gemma4_moe_meta();
RadBuildCtx ctx = build_ctx();
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
const int qkv = buf_named(b, "qkv_s");
REQUIRE(qkv > 0);
CHECK_EQ(buf_named(b, "q_s"), 0);
const RecOp* at = find_op(b, "attn_paged", 0);
REQUIRE(at != nullptr);
CHECK(std::find(at->reads.begin(), at->reads.end(), (rad_buf)qkv) != at->reads.end());
CHECK_EQ(count_op(b, "cast"), 0); /* no drafter here, so no taps either */
/* Served, attn_paged_qrow reads the slice and nothing is lifted; refused -- a hierarchy without
* gemma4k -- q is lifted into q_s for attn_paged, and the slice reaches no attn_paged either
* way, since that op may resolve to a library that reads q only dense. */
for (int refuse = 0; refuse < 2; ++refuse) {
RadBuilder b;
if (refuse) b.refuse.insert("attn_paged_qrow");
RadModelMeta meta = gemma4_moe_meta();
RadBuildCtx ctx = build_ctx();
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
const int qkv = buf_named(b, "qkv_s"), qs = buf_named(b, "q_s");
REQUIRE(qkv > 0);
CHECK_EQ(qs > 0, refuse == 1);
auto reads = [](const RecOp& o, int buf) {
return buf > 0 && std::find(o.reads.begin(), o.reads.end(), (rad_buf)buf) != o.reads.end();
};
int qrow = 0, paged_slice = 0, paged_lifted = 0;
for (const auto& o : b.ops) {
if (!o.resolved) continue;
if (o.op == "attn_paged_qrow" && reads(o, qkv)) ++qrow;
if (o.op == "attn_paged" && reads(o, qkv)) ++paged_slice;
if (o.op == "attn_paged" && reads(o, qs)) ++paged_lifted;
}
const int sliding = refuse ? paged_lifted : qrow;
CHECK(sliding > 0);
CHECK_EQ(refuse ? qrow : paged_lifted, 0);
CHECK_EQ(paged_slice, 0);
CHECK_EQ(count_op(b, "cast"), refuse ? sliding : 0); /* the lifts; no drafter, no taps */
RadCtx c; c.b = &b; c.rank = 0; c.world = 1;
RadBatch batch = decode_batch();
c.batch = &batch;
gemma4::step(&c, &batch);
CHECK(c.step_fail.empty());
const std::vector<std::string> issued = issued_ops(b, c);
CHECK_EQ(count_issued(issued, "attn_paged_qrow"), refuse ? 0 : sliding);
CHECK_EQ(count_issued(issued, "cast"), refuse ? sliding : 0);
for (const auto& o : b.ops)
if (o.resolved) CHECK(o.uses_declared);
}
}
/* The feed-forward join writes the next layer's input norm: with the joins served, the only