core, libr4d, arch: chained launches with block drafters, more prefill overlap, weight reads beside the all-reduces

Chained launches on the AQL backend (a dependent kernel waits on a queue counter in its own
prologue), on by default where the model's plugin measured a gain with its block drafter (the
Gemma 4 31B and 26B); the prefill overlap runs attention per slice with narrower lane-1
reductions; the next GEMM's weight is read into the on-die cache beside each all-reduce at decode
heights.

Squashed from:

- core, libr4d: chained launches -- dependent kernels wait in their prologue, not behind the barrier bit
- core, libr4d: chain a kernel by its name, not by the row that launched it
- libr4d, core: chained forms hold nothing across their body
- libr4d: a chained kernel's prologue polls its counter less often after the first few tries
- core/sched: the advance kernel's chained form
- docs: GUIDE lists RADIANCE_CHAIN; chained launches stay opt-in
- core/device: a chained packet behind a chained one keeps its barrier bit while a plain one may run
- libr4d: qkv_norm_rope and its kv_store fold as chained forms
- libr4d: the split-KV decode attention and its merge as chained forms
- libr4d: the four-rank fused junction's tail form as a chained form
- libr4d: the attention, prologue and junction chained forms hold nothing across their body
- libr4d: the 31B's windowed attention and quant_act_i8 as chained forms; r4d_chain.h stubs for the host emulator
- libr4d: DFlash2's kernels as chained forms
- libr4d: chained forms for the decode tail
- abi, core, arch: chained launches on by default where the plugin measured a gain with its drafter
- arch/gemma4_bf16: the prefill overlap's lane-1 all-reduces take the narrow launch (`overlap` 1)
- arch/gemma4_bf16, libr4d: the prefill overlap runs attention per slice; kv_len_trim
- abi, core, libr4d: RAD_LAYOUT_ANY, and a device-only weight read (prefetch)
- arch: read the next GEMM's weight into the cache beside each collective at decode heights

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 committed 2026-10-21 03:00:48 -05:00
1 parent 1bb7ad3ef9
commit ab2b3be6c0
53 files changed
+2239 -220

No files matched your search

+47
View File
@@ -587,6 +587,53 @@ inline rad_op rw(RadBuilder* b, rad_op h,
return h;
}
/* ---- A weight read into the on-die cache beside a collective (libr4d `prefetch`, docs/OPS.md).
* At decode heights a GEMM streams its weight once from memory, and a collective in front of it
* leaves the card's memory idle for its round trip. prefetch_beside issues `op`, a `prefetch` of
* the next GEMM's weight `w`, behind the GEMM before the collective, so the two do not share the
* memory, and the next GEMM finds its leading bytes in the cache. Nothing it reads or writes is a
* model value, so on the second lane it keeps that lane's rules with no buffer named
* (core/runtime/ctx.h): it is no collective, keeps no counter and touches no arena byte.
*
* Only at decode and verify heights, up to kPrefetchRows rows: a DFlash2 verify's 8 a sequence, a
* z-lab DFlash verify's 16, a plain decode's one. With the ceiling at 64, a prompt's prefill steps
* (40 rows on the 31B's test prompt) took the reads too, and a freshly started 31B server's first
* greedy reply then differed on 4 of 6 starts (4x R9700, the cards' memory scribbled before each
* start); at 16, and with no reads at all, 0 of 6. Per-op output hashes later located that
* divergence at the prefill all-reduce's first reuse of a double-buffered slot: a race in the
* all-reduce, which its full fence on prefill-sized messages (libr4d) cures, not in anything the
* reads touch. The reads stay off prompt steps all the same. */
constexpr int64_t kPrefetchRows = 16;
/* The read goes on lane 0 itself, right after the GEMM, where its chained form (libr4d
* r4dchain_prefetch) waits for that GEMM and then reads beside the collective after it without
* holding the collective: no join between the lanes, so no record on the GEMM and no barrier bit
* on it, and the read starts as the GEMM's last block ends rather than ~10 us later on lane 1. On
* 4x R9700 (31B DFlash2, three rounds, every reply identical): code 385-386 tok/s against 363-365,
* prose 284-286 against 269-270, decode after a 6.4k prompt 580-583 against 538-541. Only where
* launches chain (rad_chain_on, the core's decision), since a plain launch would read in series
* there; elsewhere the read goes on lane 1, behind a join from lane 0. */
inline bool prefetch_lane0() { return rad_chain_on() != 0; }
inline void prefetch_beside(RadCtx* c, rad_op op, rad_weight w, int64_t T) {
if (!op || T > kPrefetchRows) return;
if (prefetch_lane0()) {
RAD_ISSUE(c, op, RAD_W(w));
return;
}
if (rad_lane_join(c, 0, 1) == RAD_OK && rad_lane(c, 1) == RAD_OK) RAD_ISSUE(c, op, RAD_W(w));
rad_lane(c, 0);
}
/* RADIANCE_PREFETCH=0 declares no reads at all (docs/GUIDE.md); read at declare. */
inline bool prefetch_enabled() {
const char* e = std::getenv("RADIANCE_PREFETCH");
return !(e && !std::strcmp(e, "0"));
}
/* The `prefetch` of `w`'s first `bytes`, where a library serves it and the reads are on; 0
* otherwise. */
inline rad_op decl_prefetch(RadBuilder* b, rad_weight w, int64_t bytes) {
if (!w || bytes <= 0 || !prefetch_enabled() || !rad_op_declared(b, "prefetch")) return 0;
return RAD_OP(b, "prefetch", RAD_PARAMS(RAD_INT("bytes", bytes)), RAD_WEIGHTS(w));
}
/* ================================================================== geometry */
/* Dimensions come from model metadata, so one plugin covers every size in a family. Sharded
* dimensions are already divided by world_size: the plugin declares divided dimensions and emits
+29
View File
@@ -127,6 +127,9 @@ struct Dflash2 {
/* The target's embedding scale, applied to the query block as the drafter was trained
* (dflash_config.input_embedding_scale): 1 for Qwen, sqrt(hidden) in bf16 for Gemma. */
double escale = 1.0;
/* Bytes of the next GEMM's weight read into the cache beside each all-reduce of the query
* pass (prefetch_beside, rad_arch.h); 0 reads none. */
int64_t prefetch = 0;
int64_t steps() const { return block - 1; }
int64_t ng(int64_t n_embd) const { return group ? n_embd / group : 0; }
/* The kernel projection emits [2, taps, NG] per token (prepare's set, then finish's);
@@ -264,6 +267,13 @@ struct Dflash2 {
* op_aan[i] after layer i-1's feed-forward (layer i's attention norm, i >= 1), op_afinal after
* the last layer's (the final norm). All or none. */
rad_op op_afn[MAX_LAYERS] = {}, op_aan[MAX_LAYERS] = {}, op_afinal = 0;
/* The reads beside the query pass's all-reduces (cfg.prefetch), each the GEMMs that follow
* in their order: beside the attention's, the feed-forward's coefficient projection, then
* gate_up; beside the feed-forward's, the next layer's attention coefficient projection, then
* its q -- after the last layer, the draft head; and o's beside the attention itself. 0 where
* none is declared. */
rad_op op_pfa[MAX_LAYERS][2] = {}, op_pff[MAX_LAYERS][2] = {}, op_pfo[MAX_LAYERS] = {};
rad_weight pfa_w[MAX_LAYERS][2] = {}, pff_w[MAX_LAYERS][2] = {}, pfo_w[MAX_LAYERS] = {};
/* head and selector */
rad_op op_gather = 0, op_dquant = 0, op_dlogits = 0, op_dtopk = 0;
@@ -900,6 +910,22 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
RAD_WEIGHTS(w_pred, w_succ)),
{w.cand, w.unary, w.hp}, {w.tokens});
/* The reads beside the all-reduces, after every weight they read is declared. */
if (op_ar && cfg.prefetch > 0)
for (int64_t l = 0; l < g.n_layer; ++l) {
const int i = (int)l;
pfa_w[i][0] = w_fcp[i];
pfa_w[i][1] = w_gu[i].w;
pff_w[i][0] = l + 1 < g.n_layer ? w_acp[i + 1] : w_dh;
pff_w[i][1] = l + 1 < g.n_layer ? w_q[i + 1].w : 0;
for (int j = 0; j < 2; ++j) {
op_pfa[i][j] = decl_prefetch(b, pfa_w[i][j], cfg.prefetch);
op_pff[i][j] = decl_prefetch(b, pff_w[i][j], cfg.prefetch);
}
pfo_w[i] = w_o[i].w;
op_pfo[i] = decl_prefetch(b, pfo_w[i], cfg.prefetch);
}
return RAD_OK;
}
@@ -989,6 +1015,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.k, T), RAD_NONE, w_k[i].bref());
RAD_ISSUE_N(c, op_vp[i], T, brows(w.h2q, T), brows(w.h2s, T),
RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.v, T), RAD_NONE, w_v[i].bref());
prefetch_beside(c, op_pfo[i], pfo_w[i], T);
if (op_qkr[i]) {
RAD_ISSUE_N(c, op_qkr[i], T, brows(w.q, T), brows(w.k, T), brows(w.v, T),
praw(batch->positions, RAD_I32, T), RAD_W(w_qn[i]), RAD_W(w_kn[i]),
@@ -1012,6 +1039,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
RAD_ISSUE_N(c, op_qat, T, brows(w.attn, T), brows(w.atq, T), brows(w.ats, T));
RAD_ISSUE_N(c, op_op[i], T, brows(w.atq, T), brows(w.ats, T),
RAD_W(w_o[i].w), RAD_W(w_o[i].s), brows(w.h, T), RAD_NONE, w_o[i].bref());
for (int j = 0; j < 2; ++j) prefetch_beside(c, op_pfa[i][j], pfa_w[i][j], T);
/* Reduce before the closing convolution. It is linear with coefficients from the
* replicated norm, so either order gives the same result; reducing first keeps x's
* update agreed across ranks. */
@@ -1038,6 +1066,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
}
RAD_ISSUE_N(c, op_dn[i], T, brows(w.ffq, T), brows(w.ffs, T),
RAD_W(w_dn[i].w), RAD_W(w_dn[i].s), brows(w.h, T), RAD_NONE, w_dn[i].bref());
for (int j = 0; j < 2; ++j) prefetch_beside(c, op_pff[i][j], pff_w[i][j], T);
if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
RAD_ISSUE_N(c, op_fconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T),
wrow_slice(w_fcb[i], 1, 1, cfg.taps * g.n_embd), brows(w.h2, T));
+24 -2
View File
@@ -112,6 +112,23 @@ inline double embed_scale(int64_t hidden) {
return rad_bf16_to_f32(rad_f32_to_bf16((float)std::sqrt((double)hidden)));
}
/* prefetch where a library serves it: the 31B trunk reading the next GEMM's leading weight bytes
* into the on-die cache beside the all-reduce in front of that GEMM, at decode heights (on lane 0
* as a chained read, or on the second lane: rad_arch.h prefetch_lane0), and the DFlash2 drafter's
* reads likewise. It writes nothing the model reads. Each read takes a weight's first 20 MB. With
* the reads on lane 0 (two rounds on 4x R9700): 20 MB 391 / 391 code tok/s, 289 / 288 prose;
* 16 MB 386 / 384, 285 / 283; 24 MB 388 / 387; 12 MB 368 / 369. */
constexpr int64_t kPrefetchBytes = (int64_t)20 << 20;
/* down_proj's read, the trunk GEMM no other read covers: its first 9 MB, beside the attention. It
* took the place of o_proj's read there: the 31B at tp 4 on 4x R9700, two rounds, every reply
* identical, 380 / 281 / 203 tok/s against 367 / 271 / 196 with o_proj's 16 MB; 10, 11, 12, 14
* and 20 MB of down_proj 379, 377, 373, 373 and 362; down_proj beside o_proj's read, or beside the
* feed-forward reduction, slower than either alone. The reads beside a reduction are one queue,
* and a longer one beside the attention pushes gate_up's read past the reduction it should hide
* in. */
constexpr int64_t kPrefetchDownBytes = (int64_t)9 << 20;
/* Block fp8 as libr4d's fp8a8 GEMMs read it: fp8_e4m3 codes, a bf16 scale per 128x128 block, no
* transform (a recipe's `rtn codes=fp8_e4m3 block=128x128 scale=bf16`). */
inline bool block_fp8_128(const RadEncoding& e) {
@@ -149,25 +166,30 @@ inline long long dflash_block(const RadModelMeta* meta) {
/* DFlash proposes its whole block in one pass, so its depth is the block it was trained at less
* the anchor, and nothing else: 15 for z-lab's Gemma 4 drafters (block_size 16). True when the
* container carries one and `out` now names its depth. */
* container carries one and `out` now names its depth. Chained launches measured a gain with it
* (the 26B-A4B's speculative step, +1% on 4x R9700), so the probe asks for them
* (RadArchProbe::chain_pays). */
inline bool dflash_probe(const RadModelMeta* meta, RadArchProbe* out) {
if (dflash_kind(meta) != 1) return false;
const long long bs = dflash_block(meta);
if (bs <= 1) return false;
out->draft_depth = (int)(bs - 1);
out->draft_depth_fixed = 1;
out->chain_pays = 1;
return true;
}
/* The same for a DFlash2 drafter, for a plugin that serves rad_block_dflash2.h (the 31B's): its
* anchor predicts nothing either, so the depth is the trained block less one, 7 for RedHatAI's
* gemma-4-31B-it-speculator.dflash2 (block_size 8). */
* gemma-4-31B-it-speculator.dflash2 (block_size 8). Chained launches measured a gain with it (the
* 31B's speculative step, +13% on 4x R9700), so the probe asks for them. */
inline bool dflash2_probe(const RadModelMeta* meta, RadArchProbe* out) {
if (dflash_kind(meta) != 2) return false;
const long long bs = dflash_block(meta);
if (bs <= 1) return false;
out->draft_depth = (int)(bs - 1);
out->draft_depth_fixed = 1;
out->chain_pays = 1;
return true;
}