From c1c05e90292df0826012c6f4882f5c3287118e5c Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 31 Oct 2026 17:56:50 +0100 Subject: [PATCH] gemma4_bf16: a picture longer than the window reads it with the window behind only A run of more rows than `sliding_window` (1120 soft tokens against 1024) is attended again with attn_paged_ahead, where the window bounds the keys behind a row, as transformers' mask does. A shorter run keeps the two-sided window, which is the same mask for it. Where no kernel serves the ahead form, the two-sided one stays. --- arch/gemma4_bf16/gemma4_bf16.cpp | 59 +++++++++++++++++++++----------- docs/ARCHITECTURES.md | 8 ++--- tests/arch_test.cpp | 32 +++++++++++++---- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index b8037f52..27b964cd 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -462,6 +462,9 @@ struct Gemma4Attn { * kernel serves it. */ bool want_bidi = false; rad_op op_bidi = 0, op_bidi_row = 0; + /* The same where the window bounds the keys behind a row only: a run longer than the window + * (op_ahead_row where q is read in the row); 0 where no kernel serves it. */ + rad_op op_ahead = 0, op_ahead_row = 0; bool bidi_ok() const { return op_bidi || op_bidi_row; } /* the feed-forward's gate_up read into the cache beside this block's all-reduce, or 0 (see * "Prefetch beside the reductions") */ @@ -743,8 +746,13 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const } } if (want_bidi) { - if (op_attn_row) op_bidi_row = attn("attn_paged_qrow", {w.qkv}, 0, 0); - else op_bidi = attn("attn_paged", {w.q}, 0, 0); + if (op_attn_row) { + op_bidi_row = attn("attn_paged_qrow", {w.qkv}, 0, 0); + op_ahead_row = attn("attn_paged_qrow_ahead", {w.qkv}, 0, 0); + } else { + op_bidi = attn("attn_paged", {w.q}, 0, 0); + op_ahead = attn("attn_paged_ahead", {w.q}, 0, 0); + } } /* The tree form over the decode rows: a verify block of 1 + n_spec rows a sequence, so the @@ -872,18 +880,23 @@ int Gemma4Attn::declare_shared(RadBuilder* b, Names& nm, const char* pre) { RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)), RAD_NOWEIGHTS), {w.q}, {w.attn}); - if (want_bidi) - op_bidi = rw(b, RAD_OP(b, "attn_paged", - RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok), - RAD_INT("head_dim", cls.head_dim), - RAD_INT("gqa", cls.gqa()), RAD_INT("causal", 0), - RAD_INT("window", cls.window), RAD_INT("n_head", cls.n_head), - RAD_INT("block_size", RAD_KV_BLOCK), - RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx), - RAD_F64("scale", 1.0), - RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)), - RAD_NOWEIGHTS), - {w.q}, {w.attn}); + if (want_bidi) { + auto bidi = [&](const char* op) { + return rw(b, RAD_OP(b, op, + RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok), + RAD_INT("head_dim", cls.head_dim), + RAD_INT("gqa", cls.gqa()), RAD_INT("causal", 0), + RAD_INT("window", cls.window), RAD_INT("n_head", cls.n_head), + RAD_INT("block_size", RAD_KV_BLOCK), + RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx), + RAD_F64("scale", 1.0), + RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)), + RAD_NOWEIGHTS), + {w.q}, {w.attn}); + }; + op_bidi = bidi("attn_paged"); + op_ahead = bidi("attn_paged_ahead"); + } if (g.world > 1) op_ar = rw(b, RAD_OP(b, "all_reduce", RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd), @@ -1076,10 +1089,11 @@ void Gemma4Attn::attend(RadCtx* c, const RadBatch* batch) const { /* A run's rows (RadBatch::n_bidi) see every key of their run, later ones included: attention over * their sequence's cache up to the run's end without the causal bound, written over what the - * causal pass left in their rows, on a sliding layer. Its window stays, two-sided: (causal or the - * run) and the window, which holds every key of a run up to the window's length. The keys are all - * in the cache: the step stored its own, and a run's earlier rows are this step's or a prefix - * hit's. */ + * causal pass left in their rows, on a sliding layer. Its window stays: (causal or the run) and + * the window, as transformers' mask has it. The window bounds the keys behind a row; within it a + * run's far end is in reach of every row up to the window's length, and a longer run is read with + * attn_paged_ahead, whose window bounds the keys behind only. The keys are all in the cache: the + * step stored its own, and a run's earlier rows are this step's or a prefix hit's. */ void Gemma4Attn::attend_bidi(RadCtx* c, const RadBatch* batch, bool q_dense) const { if (batch->n_bidi <= 0 || !bidi_ok()) return; const RadKVGroupBatch* kvb = kv_batch(batch, kv); @@ -1092,15 +1106,20 @@ void Gemma4Attn::attend_bidi(RadCtx* c, const RadBatch* batch, bool q_dense) con const RadOperand tb = praw2(table ? table + s * pitch : nullptr, RAD_I32, 1, pitch); const RadOperand used = praw(batch->bidi_used + i, RAD_I32, 1); const RadOperand out = brow_slice(w.attn, r0, n, qd); + /* A run longer than the window has rows that miss its far end under a two-sided window; + * transformers' mask bounds only the keys behind. */ + const bool ahead = n > cls.window; + const rad_op bidi_row = ahead && op_ahead_row ? op_ahead_row : op_bidi_row; + const rad_op bidi = ahead && op_ahead ? op_ahead : op_bidi; if (op_bidi_row) { - RAD_ISSUE_N(c, op_bidi_row, n, bcol_at(w.qkv, r0, qkvd, 0, qd, n), + RAD_ISSUE_N(c, bidi_row, n, bcol_at(w.qkv, r0, qkvd, 0, qd, n), kv_cache(kv, kv_layer), tb, used, RAD_NONE, RAD_NONE, RAD_NONE, out); continue; } if (!q_dense) RAD_ISSUE_N(c, op_lift_q, n, bcol_at(w.qkv, r0, qkvd, 0, qd, n), brow_slice(w.q, r0, n, qd)); - RAD_ISSUE_N(c, op_bidi, n, brow_slice(w.q, r0, n, qd), kv_cache(kv, kv_layer), tb, used, + RAD_ISSUE_N(c, bidi, n, brow_slice(w.q, r0, n, qd), kv_cache(kv, kv_layer), tb, used, RAD_NONE, RAD_NONE, RAD_NONE, out); } } diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index b7e9614d..360dd8ed 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -1563,10 +1563,10 @@ tower and no audio encoder; `rad_block_gemma4_unified.h` holds what replaces the overlays every layer, which differs little on most pictures. The scheduler ends no prefill chunk inside such a run (`Scheduler::bidi_fit`), the trunk pass names its runs (`RadBatch::n_bidi`), and each sliding layer attends a run's rows a second time with `attn_paged` at `causal` 0 over the - cache up to the run's end, written over their causal result. The window stays two-sided there: - a run longer than the window (1120 soft tokens against 1024) sees its later keys only up to a - window ahead, as vLLM bounds them, where transformers' mask bounds only the keys behind. Read - causally, the 12B answers pictures differently and often wrongly (on DocVQA, "The image does not + cache up to the run's end, written over their causal result. The window stays, as in + transformers' mask (the window AND (causal OR the same picture)): it bounds the keys behind a row. + A run longer than the window (1120 soft tokens against 1024) is read with `attn_paged_ahead`, whose + window bounds the keys behind only, so its first rows see the run's far end. Read causally, the 12B answers pictures differently and often wrongly (on DocVQA, "The image does not provide information" for an answer it reads off the page). `RADIANCE_MEDIA_BIDI=0` keeps every layer causal. diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index a8662d2c..13fe41b3 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -10777,9 +10777,17 @@ TEST(the_gemma4_12b_reads_a_pictures_rows_both_ways) { const char* mv[] = { "gemma4_unified_vision", "3840", "1120", "48", "vision" }; keys.insert(keys.end(), std::begin(mk), std::end(mk)); vals.insert(vals.end(), std::begin(mv), std::end(mv)); - static const int32_t row[] = { 40 }, rows[] = { 10 }, seq[] = { 1 }, used[] = { 30 }; + static const int32_t row[] = { 40 }, seq[] = { 1 }, used[] = { 30 }; const int64_t L_sliding = 10; /* layers 5 and 11 are global */ + /* A run of 10 rows within the window, and one longer than a window of 8, whose rows read the + * keys behind them only through the window (attn_paged_ahead). */ + const int32_t run = 10; + static const int32_t rows[] = { run }; + for (const int win : { 1024, 8 }) for (int mode = 0; mode < 3; ++mode) { /* 0 on, 1 no key, 2 RADIANCE_MEDIA_BIDI=0 */ + std::vector wvals = vals; + for (size_t k = 0; k < keys.size(); ++k) + if (!std::strcmp(keys[k], "sliding_window")) wvals[k] = win == 8 ? "8" : "1024"; for (bool fused : { false, true }) { for (int world : { 1, 2 }) { for (int rank = 0; rank < world; ++rank) { @@ -10791,7 +10799,7 @@ TEST(the_gemma4_12b_reads_a_pictures_rows_both_ways) { } RadModelMeta meta = gemma4_12b_meta(); meta.n_kv = (int)keys.size() - (mode == 1 ? 1 : 0); - meta.kv_key = keys.data(); meta.kv_val = vals.data(); + meta.kv_key = keys.data(); meta.kv_val = wvals.data(); RadBuildCtx ctx = build_ctx(rank, world); ctx.max_enc_patches = 1120; if (mode == 2) setenv("RADIANCE_MEDIA_BIDI", "0", 1); @@ -10804,10 +10812,18 @@ TEST(the_gemma4_12b_reads_a_pictures_rows_both_ways) { for (const auto& o : b.ops) if ((o.op == "attn_paged" || o.op == "attn_paged_qrow") && pint(&o, "causal") == 0) { ++nc; - CHECK_EQ(pint(&o, "window"), 1024); + CHECK_EQ(pint(&o, "window"), win); CHECK_EQ(pint(&o, "head_dim"), 256); } CHECK_EQ(nc, on ? (int)L_sliding : 0); + int na = 0; + for (const auto& o : b.ops) + if (o.op == "attn_paged_ahead" || o.op == "attn_paged_qrow_ahead") { + ++na; + CHECK_EQ(pint(&o, "causal"), 0); + CHECK_EQ(pint(&o, "window"), win); + } + CHECK_EQ(na, on ? (int)L_sliding : 0); RadBatch batch = prefill_batch(); batch.n_bidi = 1; @@ -10821,11 +10837,15 @@ TEST(the_gemma4_12b_reads_a_pictures_rows_both_ways) { rad_op last_attn = 0; for (const auto& i : c.issues) { const RecOp* o = b.op_of(i.op); - if (!o || (o->op != "attn_paged" && o->op != "attn_paged_qrow")) continue; + if (!o || (o->op != "attn_paged" && o->op != "attn_paged_qrow" && + o->op != "attn_paged_ahead" && o->op != "attn_paged_qrow_ahead")) + continue; if (pint(o, "causal") == 1) { last_attn = i.op; continue; } ++again; CHECK(last_attn != 0); /* behind the causal pass */ - CHECK_EQ(i.n, 10); + const bool ahead = o->op.size() > 6 && o->op.compare(o->op.size() - 6, 6, "_ahead") == 0; + CHECK_EQ(ahead, run > win); + CHECK_EQ(i.n, run); REQUIRE(i.opd.size() == 8); CHECK_EQ((int)i.opd[1].kind, (int)RAD_OPK_KV); CHECK(i.opd[3].raw == used); @@ -10834,7 +10854,7 @@ TEST(the_gemma4_12b_reads_a_pictures_rows_both_ways) { CHECK(i.opd[6].kind == RAD_OPK_NONE); const int64_t qd = (16 / world) * (pint(o, "head_dim")); CHECK_EQ(i.opd[7].offset, 40 * qd); /* its rows of the output */ - CHECK_EQ(i.opd[7].rows, 10); + CHECK_EQ(i.opd[7].rows, run); last_attn = 0; } CHECK_EQ(again, on ? (int)L_sliding : 0);