gemma3: a prefill step of one sequence in two pieces, block boundaries on the second lane

qwen35_fp8's pipelined prefill on Gemma 3's MXFP4 trunk at two ranks: every block runs a piece
at a time on lane 0 (the attention over its own query bounds and positions, the GeGLU MLP), and the
work between two blocks a piece at a time on lane 1 -- the block's all-reduce at `overlap` 1, its
post-norm and the fp8 junction (the add and the next norm with its quantiser), after the last block
the add, the DFlash2 taps and the final norm. RADIANCE_GEMMA3_PIPE=0 turns it off.

- Every q|k|v and gate|up reads one shared E4M3 input pair under the pipeline (the junctions write
  it), so the second lane's buffers cost one pair, not one a linear (rad_buf_concurrent keeps them
  live for the whole program).
- A window KV group's seqused counts from its rebased first block, so the first piece's cannot be
  its position: it is the group's seqused less the second piece's rows, computed on the card by a
  new libr4d op, sub_i32 (y = x - s[0], int32; documented in OPS.md, a selftest case). The first
  try used the position and faulted on a 12k prompt past the 1024-token window.
- G3Linear and G3JunctionFP8 row-sliced forms; each block's overlap all-reduce.

Bit for bit the serial step: TranslateGemma's KL recordings at chunk 2048 and one sequence a step
(so a document's steps from 1024 rows pipeline) compare equal with the pipeline on and off. Prefill
on two R9700s, chunk 2048: 6k tokens 1.27 -> 1.21 s, 12k 2.48 -> 2.42 s; at chunk 1024 (pieces of
512) no gain, so a deployment wants 2048 (against 1024: -9 % / -7 %). arch_test: a 2048-row TP2
step runs in pieces with its collectives on lane 1 and the shared pair; RADIANCE_GEMMA3_PIPE=0 runs
it serially.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-24 11:10:08 +02:00
1 parent 57d2cb4043
commit 73b1d039a0
7 files changed
+502 -9

No files matched your search

+380 -9
View File
@@ -104,6 +104,9 @@ struct G3Linear {
const G3Linear* in_from = nullptr);
void step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const;
void gemm(RadCtx* c, rad_buf y, int64_t T) const;
/* The same over rows [r0, r0 + n) of an E4M3 (MXFP4) linear, for the pipelined prefill. */
void step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const;
void gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const;
};
inline int G3Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char* base,
@@ -295,6 +298,18 @@ inline void G3Linear::gemm(RadCtx* c, rad_buf y, int64_t T) const {
brows(y, T), RAD_NONE, mx ? RAD_W(wref) : RAD_NONE);
}
inline void G3Linear::gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const {
RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, K / RAD_FP8_BLOCK),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, n, N), RAD_NONE, RAD_W(wref));
}
inline void G3Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const {
if (!shared_in)
RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K),
brow_slice(s8, r0, n, K / RAD_FP8_BLOCK));
gemm_rows(c, y, r0, n);
}
/* ---- the GPTQ calibration tap (rad_arch.h's calib_*): the Gram of one linear's bf16 input,
* summed over the corpus in a PERSIST [K, K] f32 buffer. Declared only with RADIANCE_CALIB_DIR set,
* for the layers RADIANCE_G3_CALIB_LAYERS names ("lo:hi", default every layer): a down projection's
@@ -343,6 +358,12 @@ struct Gemma3Attn {
/* Calibration: q|k|v's shared input and o_proj's (this rank's columns). */
G3Tap tap_in{}, tap_o{};
const bool* tap_live = nullptr;
/* Set before declare by a pipelined deployment: the all-reduce a second time at `overlap` 1 (a
* launch that leaves the CUs to the compute beside it), and q|k|v's input pair shared by every
* block (the junctions write it a piece at a time on the second lane). */
int ar_overlap = 0;
rad_op op_ar_ov = 0;
const G3Linear* pipe_in = nullptr;
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const ClassGeom& cgeo,
rad_kvgroup group, const Wire& wire, double scale);
@@ -352,6 +373,17 @@ struct Gemma3Attn {
void post(RadCtx* c, int64_t T) const {
RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T));
}
/* The pipelined prefill (step_pipelined): the block over one prefill sequence in two pieces,
* its input pair already written; hands the first piece behind its out projection. */
void step_pieces(RadCtx* c, const RadBatch* batch, ArPieces* pc, const int32_t* used0) const;
/* The tail a piece at a time on the second lane: the overlap all-reduce and the post-norm. */
void ar_rows(RadCtx* c, int64_t r0, int64_t n) const {
RAD_ISSUE_N(c, op_ar_ov, n * g.n_embd, brow_slice(w.h, r0, n, g.n_embd), RAD_NONE);
}
void post_rows(RadCtx* c, int64_t r0, int64_t n) const {
RAD_ISSUE_N(c, op_post, n, brow_slice(w.h, r0, n, g.n_embd), RAD_W(w_post),
brow_slice(w.h, r0, n, g.n_embd));
}
};
int Gemma3Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const ClassGeom& cgeo,
@@ -375,7 +407,7 @@ int Gemma3Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
RAD_ARCH_TRY(ln_q.declare(b, nm, g, nm.f("blk.%d.attn_q", l),
{ nm.ckpt("%s.%lld.self_attn.q_proj.weight", pre, (long long)l) },
qd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.q));
qd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.q, {}, pipe_in));
const G3Linear* qin = &ln_q; /* k and v read q's int8 pair */
RAD_ARCH_TRY(ln_k.declare(b, nm, g, nm.f("blk.%d.attn_k", l),
{ nm.ckpt("%s.%lld.self_attn.k_proj.weight", pre, (long long)l) },
@@ -460,6 +492,13 @@ int Gemma3Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
RAD_INT("min_bytes", g.wire_min_bytes)),
RAD_NOWEIGHTS),
{w.h}, {w.h});
if (g.world > 1 && ar_overlap > 0)
op_ar_ov = 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),
RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact),
RAD_INT("min_bytes", g.wire_min_bytes), RAD_INT("overlap", ar_overlap)),
RAD_NOWEIGHTS),
{w.h}, {w.h});
op_post = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
@@ -474,6 +513,49 @@ int Gemma3Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
return RAD_OK;
}
/* Each piece is the chunk a span of 1 makes: its rows, positions and slots, and an attention over
* its own query bounds (ArPieces::cu) seeing the cache up to its own end -- `used0`, the group's
* seqused less the second piece's rows, for the first; the step's seqused for the second, which
* reads the first piece's keys from the cache as a later chunk would (the sliding window included:
* a window group's seqused counts from its rebased first block). */
void Gemma3Attn::step_pieces(RadCtx* c, const RadBatch* batch, ArPieces* pc,
const int32_t* used0) const {
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
const int32_t* table = kvb ? kvb->block_table : nullptr;
const int32_t* used = kvb ? kvb->seqused : nullptr;
const int64_t T = batch->n_tok, R = pc->rows;
const int64_t qd = cls.q_dim(), kvd = cls.kv_dim();
const int64_t tpitch = kvb ? kvb->block_table_pitch : 0;
for (int64_t p = 0; p < 2; ++p) {
const int64_t r0 = p == 0 ? 0 : R;
const int64_t n = p == 0 ? R : T - R;
const int32_t* pos = batch->positions + r0;
ln_q.gemm_rows(c, w.q, r0, n);
ln_k.gemm_rows(c, w.k, r0, n);
ln_v.gemm_rows(c, w.v, r0, n);
if (op_qnr) {
RAD_ISSUE_N(c, op_qnr, n, brow_slice(w.q, r0, n, qd), brow_slice(w.k, r0, n, kvd),
brow_slice(w.v, r0, n, kvd), praw(pos, RAD_I32, n), RAD_W(w_qn), RAD_W(w_kn));
} else {
RAD_ISSUE_N(c, op_qn, n * cls.n_head, brow_slice(w.q, r0, n, qd), RAD_W(w_qn),
brow_slice(w.q, r0, n, qd));
RAD_ISSUE_N(c, op_kn, n * cls.n_head_kv, brow_slice(w.k, r0, n, kvd), RAD_W(w_kn),
brow_slice(w.k, r0, n, kvd));
RAD_ISSUE_N(c, op_rq, n, brow_slice(w.q, r0, n, qd), praw(pos, RAD_I32, n));
RAD_ISSUE_N(c, op_rk, n, brow_slice(w.k, r0, n, kvd), praw(pos, RAD_I32, n));
}
RAD_ISSUE_N(c, op_kv, n, brow_slice(w.k, r0, n, kvd), brow_slice(w.v, r0, n, kvd),
praw(slot + r0, RAD_I32, n), kv_cache(kv, layer));
RAD_ISSUE_N(c, op_attn, n, brows(w.q, T), kv_cache(kv, layer),
praw2(table, RAD_I32, 1, tpitch),
praw(p == 0 ? used0 : used, RAD_I32, 1),
RAD_NONE, RAD_NONE, praw(pc->cu + p, RAD_I32, 2), brows(w.attn, T));
ln_o.step_rows(c, w.attn, w.h, r0, n);
if (p == 0) pc->hand(r0, n);
}
}
void Gemma3Attn::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tail_fused,
bool ar_fused) const {
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
@@ -538,6 +620,10 @@ struct Gemma3Mlp {
/* Calibration: gate|up's input and down's (this rank's columns). */
G3Tap tap_gu{}, tap_dn{};
const bool* tap_live = nullptr;
/* Set before declare by a pipelined deployment, as Gemma3Attn's. */
int ar_overlap = 0;
rad_op op_ar_ov = 0;
const G3Linear* pipe_in = nullptr;
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire);
void step(RadCtx* c, const RadBatch* batch, bool entry_done = false, bool tail_fused = false,
@@ -545,6 +631,24 @@ struct Gemma3Mlp {
void post(RadCtx* c, int64_t T) const {
RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T));
}
/* The pipelined prefill: the block over rows [r0, r0 + n), its input pair already written, up
* to the down projection; then the tail a piece at a time on the second lane. */
void body_rows(RadCtx* c, int64_t r0, int64_t n) const {
ln_gu.gemm_rows(c, w.gu, r0, n);
RAD_ISSUE_N(c, op_act, n, brow_slice(w.gu, r0, n, 2 * g.n_ff), brow_slice(w.ffn, r0, n, g.n_ff));
ln_down.step_rows(c, w.ffn, w.h, r0, n);
}
void ar_rows(RadCtx* c, int64_t r0, int64_t n) const {
RAD_ISSUE_N(c, op_ar_ov, n * g.n_embd, brow_slice(w.h, r0, n, g.n_embd), RAD_NONE);
}
void post_rows(RadCtx* c, int64_t r0, int64_t n) const {
RAD_ISSUE_N(c, op_post, n, brow_slice(w.h, r0, n, g.n_embd), RAD_W(w_post),
brow_slice(w.h, r0, n, g.n_embd));
}
void add_rows(RadCtx* c, int64_t r0, int64_t n) const {
RAD_ISSUE_N(c, op_add, n, brow_slice(w.x, r0, n, g.n_embd), brow_slice(w.h, r0, n, g.n_embd),
brow_slice(w.x, r0, n, g.n_embd));
}
};
int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire) {
@@ -562,7 +666,7 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
{ nm.ckpt("%s.%lld.mlp.gate_proj.weight", pre, (long long)l),
nm.ckpt("%s.%lld.mlp.up_proj.weight", pre, (long long)l) },
2 * g.n_ff, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.gu,
{ g.n_ff, g.n_ff }));
{ g.n_ff, g.n_ff }, pipe_in));
RAD_ARCH_TRY(ln_down.declare(b, nm, g, nm.f("blk.%d.ffn_down", l),
{ nm.ckpt("%s.%lld.mlp.down_proj.weight", pre, (long long)l) },
g.n_embd, g.n_ff, RAD_SHARD_COL, grp_layer(l), w.ffn, w.h));
@@ -589,6 +693,13 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
RAD_INT("min_bytes", g.wire_min_bytes)),
RAD_NOWEIGHTS),
{w.h}, {w.h});
if (g.world > 1 && ar_overlap > 0)
op_ar_ov = 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),
RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact),
RAD_INT("min_bytes", g.wire_min_bytes), RAD_INT("overlap", ar_overlap)),
RAD_NOWEIGHTS),
{w.h}, {w.h});
op_post = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)),
@@ -685,10 +796,12 @@ struct G3JunctionFP8 {
rad_weight w = 0, wp = 0;
int64_t n = 0;
/* `shared_ok`: `into` reads the pipelined prefill's shared input pair, which this junction is
* then the writer of. */
int declare(RadBuilder* b, const Geom& g, rad_buf x_, rad_buf res_, rad_weight w_,
const G3Linear& into, rad_weight wp_ = 0) {
const G3Linear& into, rad_weight wp_ = 0, bool shared_ok = false) {
x = x_; res = res_; w = w_; wp = wp_; q = into.q8; s = into.s8; n = g.n_embd;
if (!into.mx || into.shared_in || !q || !s || !w) return RAD_E_INVAL;
if (!into.mx || (into.shared_in && !shared_ok) || !q || !s || !w) return RAD_E_INVAL;
if (!rad_op_declared(b, "rmsnorm_quant_fp8")) return RAD_OK;
op = RAD_OP(b, "rmsnorm_quant_fp8",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n), RAD_F64("eps", g.eps),
@@ -723,6 +836,12 @@ struct G3JunctionFP8 {
RAD_ISSUE_N(c, op, T, brows(x, T), res ? brows(res, T) : RAD_NONE, RAD_W(w),
brows(q, T), brows(s, T), RAD_NONE);
}
/* Rows [r0, r0 + n), without the all-reduce: the pipelined prefill's second lane. */
void step_rows(RadCtx* c, int64_t r0, int64_t rows) const {
RAD_ISSUE_N(c, op, rows, brow_slice(x, r0, rows, n), res ? brow_slice(res, r0, rows, n) : RAD_NONE,
RAD_W(w), brow_slice(q, r0, rows, n), brow_slice(s, r0, rows, n / RAD_FP8_BLOCK),
RAD_NONE);
}
};
struct Layer {
@@ -773,10 +892,31 @@ struct Model {
bool calib = false, tap_live = false;
int64_t calib_lo = 0, calib_hi = 0;
long long calib_rows = 0;
/* The pipelined prefill (step_pipelined): on where declare allowed it; the input pair every
* block's q|k|v and gate|up read (the junctions write it); the bounds buffer and its pinned
* table of {0, pipe_rows(T), T}; whether rank 0 has said it pipelines, or why not. */
bool pipe_on = false;
G3Linear pipe_in{};
rad_buf b_pipe_cu = 0;
/* Each KV group's seqused for the first piece, [2] (sliding, global): the step's less the
* second piece's rows (sub_i32 from b_pipe_cu[3] = T - R). A window group's seqused counts from
* its rebased first block, not from position 0, so the first piece's position cannot stand in. */
rad_buf b_pipe_used = 0;
rad_op op_pipe_used = 0;
int32_t* pipe_tbl = nullptr;
bool pipe_tbl_tried = false;
bool pipe_noted = false;
bool pipe_serial_noted = false;
};
static Model g_model[MAX_RANKS];
/* The narrowest step the pipelined prefill takes, and its first piece: half the step rounded up
* to 64 rows (qwen35_fp8 says why). */
static const int64_t kPipeMinRows = 1024;
static int64_t pipe_rows(int64_t T) { return ((T + 1) / 2 + 63) / 64 * 64; }
/* ================================================================== the DFlash2 drafter
*
* A second model at absolute layers n_layer.. with its own KV group (rad_block_dflash2.h), as
@@ -917,6 +1057,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
if (ctx->rank < 0 || ctx->rank >= MAX_RANKS) return RAD_E_INVAL;
Model& m = g_model[ctx->rank];
if (m.pipe_tbl) rad_dev_free(m.pipe_tbl, RAD_MEM_HOST_PINNED);
m = Model{};
m.nm = Names(ctx->scope);
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16));
@@ -1042,6 +1183,24 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_NOWEIGHTS),
{m.b_x}, {m.b_x});
/* The pipelined prefill (step_pipelined) at two ranks wherever a step can be wide enough, on an
* MXFP4 trunk with fused junctions (decided below; RADIANCE_GEMMA3_PIPE=0 turns it off). Its
* second lane touches the stream, the branch output, the shared input pair, the taps and the
* bounds, so those leave the sharing pool (Ctx::lane1_); the pair is one for every block so
* that this costs one pair, not one a linear. */
{
const char* e = std::getenv("RADIANCE_GEMMA3_PIPE");
m.pipe_on = g.world == 2 && g.max_tok >= kPipeMinRows && !calib_dir() &&
!(e && std::strcmp(e, "0") == 0);
}
if (m.pipe_on) {
m.pipe_in.mx = true;
m.pipe_in.K = g.n_embd;
m.pipe_in.q8 = decl_b(b, nm.f("pipe_in.q8"), RAD_F8E4M3, {g.max_tok, g.n_embd});
m.pipe_in.s8 = decl_b(b, nm.f("pipe_in.s8"), RAD_F32, {g.max_tok, g.n_embd / RAD_FP8_BLOCK});
if (!m.pipe_in.q8 || !m.pipe_in.s8) return RAD_E_INVAL;
}
Gemma3Attn::Wire aw{}; aw.x = m.b_x; aw.h = m.b_h; aw.q = m.b_q; aw.k = m.b_k; aw.v = m.b_v; aw.attn = m.b_attn;
Gemma3Mlp::Wire mw{}; mw.x = m.b_x; mw.h = m.b_h; mw.gu = m.b_gu; mw.ffn = m.b_ffn;
@@ -1052,6 +1211,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
const ClassGeom& cgeo = lay.global ? m.cg : m.cs;
const rad_kvgroup kg = lay.global ? m.kv_g : m.kv_s;
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, kg));
if (m.pipe_on) {
lay.attn.ar_overlap = lay.mlp.ar_overlap = 1;
lay.attn.pipe_in = lay.mlp.pipe_in = &m.pipe_in;
}
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, cgeo, kg, aw, attn_scale));
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw));
}
@@ -1093,20 +1256,25 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
/* ---- the MXFP4 trunk's junctions, when every block's inputs are E4M3 pairs it can write. */
if (g.n_layer > 0 && !m.fuse && !calib_dir()) {
bool ok = true;
/* Under the pipelined prefill every q and gate|up reads the shared pair, which the
* junctions write; otherwise each owns its own. */
const bool own = !m.pipe_on;
for (const Layer& lay : m.layers)
ok = ok && lay.attn.ln_q.mx && !lay.attn.ln_q.shared_in && lay.attn.ln_k.shared_in &&
lay.attn.ln_v.shared_in && lay.mlp.ln_gu.mx && !lay.mlp.ln_gu.shared_in;
ok = ok && lay.attn.ln_q.mx && lay.attn.ln_q.shared_in != own && lay.attn.ln_k.shared_in &&
lay.attn.ln_v.shared_in && lay.mlp.ln_gu.mx && lay.mlp.ln_gu.shared_in != own;
if (ok) {
RAD_ARCH_TRY(m.jf0.declare(b, g, m.b_x, 0, m.layers[0].attn.w_norm, m.layers[0].attn.ln_q));
RAD_ARCH_TRY(m.jf0.declare(b, g, m.b_x, 0, m.layers[0].attn.w_norm, m.layers[0].attn.ln_q,
0, !own));
ok = m.jf0.op != 0;
for (int64_t l = 0; ok && l < g.n_layer; ++l) {
Layer& lay = m.layers[(size_t)l];
RAD_ARCH_TRY(lay.jf_am.declare(b, g, m.b_h, m.b_x, lay.mlp.w_norm, lay.mlp.ln_gu,
lay.attn.w_post));
lay.attn.w_post, !own));
ok = lay.jf_am.op != 0;
if (ok && l + 1 < g.n_layer) {
RAD_ARCH_TRY(lay.jf_ma.declare(b, g, m.b_h, m.b_x, m.layers[(size_t)l + 1].attn.w_norm,
m.layers[(size_t)l + 1].attn.ln_q, lay.mlp.w_post));
m.layers[(size_t)l + 1].attn.ln_q, lay.mlp.w_post,
!own));
ok = lay.jf_ma.op != 0;
}
}
@@ -1119,6 +1287,35 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
}
m.fuse_mx_ar = ar;
}
/* The pipeline needs the fused fp8 junctions and every block's overlap all-reduce. */
if (m.pipe_on) {
bool ok = m.fuse_mx;
/* ... and the pieces' row-sliced linears are the E4M3 (MXFP4) path throughout. */
for (const Layer& lay : m.layers)
ok = ok && lay.attn.op_ar_ov && lay.mlp.op_ar_ov && lay.attn.ln_o.mx && lay.mlp.ln_down.mx;
m.pipe_on = ok;
}
if (m.pipe_on) {
RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_x));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_h));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.pipe_in.q8));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.pipe_in.s8));
if (m.b_dfsrc) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_dfsrc));
m.b_pipe_cu = decl_b(b, nm.f("pipe_cu"), RAD_I32, {4});
RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_pipe_cu));
m.b_pipe_used = decl_b(b, nm.f("pipe_used"), RAD_I32, {2});
RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_pipe_used));
m.op_pipe_used = rad_op_declared(b, "sub_i32")
? RAD_OP(b, "sub_i32", RAD_PARAMS(RAD_RANGE("M", 1, 1)), RAD_NOWEIGHTS) : 0;
if (m.op_pipe_used) {
const rad_buf rd[1] = { m.b_pipe_cu };
const rad_buf wr[1] = { m.b_pipe_used };
rad_op_reads (b, m.op_pipe_used, rd, 1);
rad_op_writes(b, m.op_pipe_used, wr, 1);
} else {
m.pipe_on = false; /* no way to put the first piece's seqused on the card */
}
}
/* ---- the calibration tap. It reads the bf16 input of every linear, which the fused junctions
* never write, so under it none is declared (above): any container calibrates, unfused. */
@@ -1180,6 +1377,172 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
return RAD_OK;
}
/* ============================== A prefill step in two pieces ==============================
*
* qwen35_fp8's step_pipelined on Gemma 3's trunk: at two ranks every layer of a prefill chunk makes
* two all-reduces, each in a hard chain behind its block (all-reduce, post-norm, the junction's add
* and next norm, then the next block). A step of one prefill sequence of at least kPipeMinRows tokens
* is cut into two pieces of about half; every block runs a piece at a time on the first lane, and
* the work between two blocks -- the block's all-reduce at `overlap` 1, its post-norm and the fp8
* junction, and after the last block its add, plus the drafter taps and the final norm -- runs a
* piece at a time on the second. The ops are the serial step's at a narrower width, so a pipelined
* step is bit for bit the serial one where a chunk's rows do not depend on its width.
*
* The second lane's requirements (Ctx::lane1_) hold as in qwen35_fp8: every collective of the step
* is on it, the embedding's included; none carries a device-wide arrival counter; the buffers it
* touches left the sharing pool at declare, the per-linear input pairs replaced by one shared pair. */
template <class F>
static void pipe_on1(RadCtx* c, const F& issue) {
rad_lane(c, 1);
rad_lane_join(c, 0, 1);
issue();
rad_lane(c, 0);
}
static void pipe_wait1(RadCtx* c) { rad_lane_join(c, 1, 0); }
/* The work after block `s` (2l the attention of layer l, 2l + 1 its feed-forward), rows
* [r0, r0 + n). */
static void pipe_boundary(RadCtx* c, const Model& m, int64_t s, int64_t r0, int64_t n) {
const Geom& g = m.g;
const int64_t li = s / 2;
const Layer& l = m.layers[(size_t)li];
const bool last = li + 1 == (int64_t)m.layers.size();
if (s % 2 == 0) {
l.attn.ar_rows(c, r0, n);
l.attn.post_rows(c, r0, n);
l.jf_am.step_rows(c, r0, n);
return;
}
l.mlp.ar_rows(c, r0, n);
l.mlp.post_rows(c, r0, n);
if (!last) l.jf_ma.step_rows(c, r0, n);
else l.mlp.add_rows(c, r0, n);
for (int t = 0; t < m.n_taps; ++t) {
if (m.tap_layer[t] != (int)li) continue;
RAD_ISSUE_N(c, m.op_tap, n, brow_slice(m.b_x, r0, n, g.n_embd),
bcol_at(m.b_dfsrc, r0, (int64_t)m.n_taps * g.n_embd, (int64_t)t * g.n_embd,
g.n_embd, n));
}
if (last)
RAD_ISSUE_N(c, m.op_final, n, brow_slice(m.b_x, r0, n, g.n_embd), RAD_W(m.w_out),
brow_slice(m.b_h, r0, n, g.n_embd));
}
/* An attention block's first piece is done (ArPieces): lane 0 takes the previous block's second
* boundary before the second piece, and lane 1 this block's first behind it. */
struct PipeHand {
RadCtx* c = nullptr;
const Model* m = nullptr;
int64_t s = 0;
};
static void pipe_hand(void* u, int64_t r0, int64_t n) {
const PipeHand& h = *(const PipeHand*)u;
pipe_wait1(h.c);
pipe_on1(h.c, [&] { pipe_boundary(h.c, *h.m, h.s, r0, n); });
}
/* The bounds of this step's pieces on the card, {0, pipe_rows(T), T}, if the step is pipelined;
* null for a serial step, and null with `failed` set when the step has been failed (a rank-local
* failure past the batch's own conditions: the other rank would pipeline). */
static const int32_t* pipe_ready(RadCtx* c, const RadBatch* batch, Model& m, bool* failed) {
const int64_t T = batch->n_tok;
if (!m.pipe_on || !m.b_pipe_cu || T < kPipeMinRows || T > m.g.max_tok ||
batch->phase != RAD_PHASE_PREFILL)
return nullptr;
auto serial = [&](const char* why) -> const int32_t* {
if (!m.pipe_serial_noted && m.g.rank == 0)
fprintf(stderr, "radiance: gemma3: a %lld-row prefill step runs serially: %s\n",
(long long)T, why);
m.pipe_serial_noted = true;
return nullptr;
};
if (batch->n_seq != 1 || batch->n_seq_decode != 0) return serial("more than one sequence");
if (!batch->positions || batch->rope_pos) return serial("no one-component positions");
const RadKVGroupBatch* kvs = kv_batch(batch, m.kv_s);
const RadKVGroupBatch* kvg = kv_batch(batch, m.kv_g);
if (!kvs || !kvg || !kvs->slot_mapping || !kvg->slot_mapping || !kvs->block_table ||
!kvg->block_table || !kvs->seqused || !kvg->seqused)
return serial("a KV group without slots, tables or lengths");
int32_t* d = (int32_t*)rad_buf_ptr(c, m.b_pipe_cu);
if (!d || !rad_buf_ptr(c, m.b_pipe_used)) return nullptr;
if (!m.pipe_tbl_tried) {
m.pipe_tbl_tried = true;
const int64_t n = m.g.max_tok + 1;
m.pipe_tbl = (int32_t*)rad_dev_alloc(4 * n * (int64_t)sizeof(int32_t), RAD_MEM_HOST_PINNED);
if (m.pipe_tbl)
for (int64_t t = 0; t < n; ++t) {
m.pipe_tbl[4 * t] = 0;
m.pipe_tbl[4 * t + 1] = (int32_t)pipe_rows(t);
m.pipe_tbl[4 * t + 2] = (int32_t)t;
m.pipe_tbl[4 * t + 3] = (int32_t)(t - pipe_rows(t));
}
}
if (!m.pipe_tbl ||
rad_memcpy_async(d, m.pipe_tbl + 4 * T, 4 * (int64_t)sizeof(int32_t), rad_stream(c)) !=
RAD_OK) {
rad_step_fail(c, "gemma3: the pipelined prefill's piece bounds could not be put on the card "
"(no pinned host memory, or the copy failed); RADIANCE_GEMMA3_PIPE=0 runs "
"prefill serially");
*failed = true;
return nullptr;
}
int32_t* u = (int32_t*)rad_buf_ptr(c, m.b_pipe_used);
RAD_ISSUE_N(c, m.op_pipe_used, 1, praw(kvs->seqused, RAD_I32, 1), praw(d + 3, RAD_I32, 1),
praw(u, RAD_I32, 1));
RAD_ISSUE_N(c, m.op_pipe_used, 1, praw(kvg->seqused, RAD_I32, 1), praw(d + 3, RAD_I32, 1),
praw(u + 1, RAD_I32, 1));
return d;
}
static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int32_t* pcu) {
const Geom& g = m.g;
const int64_t T = batch->n_tok, R = pipe_rows(T);
const int64_t L = (int64_t)m.layers.size();
if (!m.pipe_noted) {
m.pipe_noted = true;
if (g.rank == 0)
fprintf(stderr, "radiance: gemma3: prefill steps of one sequence from %lld rows are "
"pipelined: two pieces, block boundaries on the second lane\n",
(long long)kPipeMinRows);
}
RAD_ISSUE(c, m.op_embed, praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok), brows(m.b_x, T));
/* The embedding's reduction and layer 0's norm over the whole step, on lane 1 with every other
* collective. */
pipe_on1(c, [&] {
if (m.op_embed_ar) RAD_ISSUE_N(c, m.op_embed_ar, T * g.n_embd, brows(m.b_x, T), RAD_NONE);
m.jf0.step(c, T);
});
pipe_wait1(c);
for (int64_t li = 0; li < L; ++li) {
const Layer& l = m.layers[(size_t)li];
PipeHand h{c, &m, 2 * li};
ArPieces pc{};
pc.rows = R; pc.done = pipe_hand; pc.u = &h; pc.cu = pcu; pc.in_ready = true;
const int32_t* used0 = (const int32_t*)rad_buf_ptr(c, m.b_pipe_used) + (l.global ? 1 : 0);
l.attn.step_pieces(c, batch, &pc, used0);
if (pc.handed != 1) {
rad_step_fail(c, "gemma3: an attention block did not run its two pieces");
return;
}
pipe_wait1(c);
pipe_on1(c, [&] { pipe_boundary(c, m, 2 * li, R, T - R); });
l.mlp.body_rows(c, 0, R);
pipe_wait1(c);
pipe_on1(c, [&] { pipe_boundary(c, m, 2 * li + 1, 0, R); });
l.mlp.body_rows(c, R, T - R);
pipe_wait1(c);
pipe_on1(c, [&] { pipe_boundary(c, m, 2 * li + 1, R, T - R); });
}
pipe_wait1(c);
if (batch->n_out > 0) {
RAD_ISSUE_N(c, m.op_gather, batch->n_out, brows(m.b_h, T),
praw(batch->out_ids, RAD_I32, batch->n_out), brows(m.b_hout, batch->n_out));
RAD_ISSUE_N(c, m.op_logits, batch->n_out, brows(m.b_hout, batch->n_out), RAD_W(m.w_lm),
brows(m.b_logits, batch->n_out));
}
}
static void step(RadCtx* c, const RadBatch* batch) {
Model& m = g_model[rad_rank(c)];
if (batch->enc) {
@@ -1197,6 +1560,14 @@ static void step(RadCtx* c, const RadBatch* batch) {
else m.df.context(c, batch);
return;
}
{
bool pipe_failed = false;
if (const int32_t* pcu = pipe_ready(c, batch, m, &pipe_failed)) {
step_pipelined(c, batch, m, pcu);
return;
}
if (pipe_failed) return;
}
const int64_t T = batch->n_tok;
/* The drafter conditions on a tapped layer's output, which the next layer overwrites: copy it
* into the tap's column slice once the layer's residual is complete. */
+3
View File
@@ -1282,6 +1282,7 @@ libref function says which (`libref/ref_audio.cpp`).
| `iota` | `M`(range) `start` `step` `period`/opt | → `y` |
| `cu_repeat` | `M`(range) `mult` | `len` → `y` |
| `int_affine` | `M`(range) `src` `mul` `add` | `x` → `y` |
| `sub_i32` | `M`(range) | `x`, `s` → `y` |
**Frames.** A lane is one piece; a frame is one step of its language model, whose branches (the
conditional first, then the unconditional) are consecutive rows, `rep` of them a lane.
@@ -1316,6 +1317,8 @@ conditional first, then the unconditional) are consecutive rows, `rep` of them a
`y[j] = (src[j] < 0 ? 0 : x[src[j]]) mul[j] + add[j]` over three lists of integers of one length
(`src` -1 a constant): the shapes and bounds a model derives from a pass's device geometry, such
as a decoder's volume at each of its scales.
- `sub_i32`: `y[i] = x[i] - s[0]`, int32: a device length less a device offset (a pipelined
prefill's first piece takes its KV group's `seqused` less the second piece's rows).
**Windows.** The denoiser runs over windows of frames, each entry of a window step a lane's latent
in a state buffer `stride` rows a lane (`lat_cu` bounds each entry's rows, `win_lane` names its
+19
View File
@@ -988,6 +988,13 @@ __global__ __launch_bounds__(kT) void cu_repeat_kernel(const int* __restrict__ l
if (j < n) y[j] = j * len[0] * mult;
}
__global__ __launch_bounds__(kT) void sub_i32_kernel(const int* __restrict__ x,
const int* __restrict__ s, int* __restrict__ y,
int n) {
const int j = blockIdx.x * kT + threadIdx.x;
if (j < n) y[j] = x[j] - s[0];
}
// ---------------------------------------------------------------------------- attention
// Dense causal attention over short sequences, head_dim 256, one query head a key head: a depth
// decoder runs a frame's handful of positions, the codebooks so far. A workgroup a (sequence,
@@ -1641,6 +1648,18 @@ extern "C" int r4d_rad_int_affine(const RadArgs* a, RadStream s) {
return st_of(hipGetLastError());
}
extern "C" int r4d_rad_sub_i32(const RadArgs* a, RadStream s) {
const RadTensor *x = r4d_opt(a, 0), *sc = r4d_opt(a, 1), *y = r4d_opt(a, 2);
if (!x || !sc || !y) return R4D_NO(RAD_E_INVAL);
if (!is_i32(x) || !is_i32(sc) || !is_i32(y)) return R4D_NO(RAD_E_DTYPE);
if (!r4d_contig(x) || !r4d_contig(y)) return R4D_NO(RAD_E_STRIDE);
const long long n = r4d_numel(y);
if (n <= 0 || r4d_numel(x) < n || r4d_numel(sc) < 1) return R4D_NO(RAD_E_SHAPE);
hipLaunchKernelGGL(sub_i32_kernel, dim3(blocks_for(n)), dim3(kT), 0, (hipStream_t)s,
(const int*)x->data, (const int*)sc->data, (int*)y->data, (int)n);
return st_of(hipGetLastError());
}
extern "C" int r4d_rad_cu_repeat(const RadArgs* a, RadStream s) {
const RadTensor *len = r4d_opt(a, 0), *y = r4d_opt(a, 1);
if (!len || !y) return R4D_NO(RAD_E_INVAL);
+1
View File
@@ -248,6 +248,7 @@ int r4d_rad_attn_dense_small_h256 (const RadArgs*, RadStream);
int r4d_rad_iota (const RadArgs*, RadStream);
int r4d_rad_cu_repeat (const RadArgs*, RadStream);
int r4d_rad_int_affine (const RadArgs*, RadStream);
int r4d_rad_sub_i32 (const RadArgs*, RadStream);
/* r4d_video.hip */
int r4d_rad_rms_mod (const RadArgs*, RadStream);
int r4d_rad_rms_mod_quant_fp8 (const RadArgs*, RadStream);
+13
View File
@@ -2580,6 +2580,10 @@ static const RadParamSpec pIota[] = { P_INT("M"), P_INT("start"), P_INT("step"),
P_INTO("period") };
static const RadOperandSpec oIota[] = { { "y", RAD_OPD_OUT, 0 } };
static const RadParamSpec pCuRepeat[] = { P_INT("M"), P_INT("mult") };
static const RadParamSpec pSubI32[] = { P_INT("M") };
static const RadOperandSpec oSubI32[] = {
{ "x", RAD_OPD_IN, 0 }, { "s", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 },
};
static const RadOperandSpec oCuRepeat[] = { { "len", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 } };
static const RadParamSpec pIntAffine[] = { P_INT("M"), P_STR("src"), P_STR("mul"), P_STR("add") };
static const RadOperandSpec oIntAffine[] = { { "x", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 } };
@@ -3522,6 +3526,9 @@ static const RadOpSchema kSchemas[] = {
SCHEMA("cu_repeat", pCuRepeat, oCuRepeat, "bounds of equal sequences, y[j] = j * len[0] * mult."),
SCHEMA("int_affine", pIntAffine, oIntAffine,
"y[j] = (src[j] < 0 ? 0 : x[src[j]]) * mul[j] + add[j] over three integer lists."),
SCHEMA("sub_i32", pSubI32, oSubI32,
"y[i] = x[i] - s[0], int32: a device length less a device offset (a pipelined prefill's "
"first piece sees its group's seqused less the second piece's rows)."),
SCHEMA("snake_aa", pSnakeAa, oSnakeAa,
"BigVGAN's alias-free SnakeBeta over each sequence, f32: replicate-padded, upsampled "
"`ratio` times by `f_up`, SnakeBeta, filtered by `f_down` at stride `ratio`."),
@@ -9071,6 +9078,12 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
nullptr, 0, NOTUNE,
nullptr, nullptr, r4d_rad_iota },
{ "sub_i32", "sub_i32", "elem",
"a device int32 vector less a device scalar", "contiguous i32", "i32",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
nullptr, 0, NOTUNE,
nullptr, nullptr, r4d_rad_sub_i32 },
{ "cu_repeat", "cu_repeat", "elem",
"bounds of equal sequences from a device length", "contiguous i32", "i32",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
+21
View File
@@ -6941,6 +6941,26 @@ void mul_weight_f32_case(const Plugin& pl) {
}
}
/* sub_i32: a vector less a device scalar, negatives included. Exact. */
void sub_i32_cases(const Plugin& pl) {
const RadKernelInfo* r = find(pl, "sub_i32");
if (!r) { ok("sub_i32", false, "row absent"); return; }
const int64_t n = 300;
std::vector<int32_t> x((size_t)n);
for (int64_t i = 0; i < n; ++i) x[(size_t)i] = (int32_t)(i * 37 - 4000);
const std::vector<int32_t> sv = { 1234 };
void* dx = up_i32(x);
void* ds = up_i32(sv);
void* dy = dalloc((size_t)n * 4);
const int rc = launch(r, { T(dx, RAD_I32, { n }), T(ds, RAD_I32, { 1 }), T(dy, RAD_I32, { n }) },
{ PI("M", n) });
if (rc != RAD_OK) { ok("sub_i32", false, "rc " + std::to_string(rc)); return; }
std::vector<int32_t> want((size_t)n);
for (int64_t i = 0; i < n; ++i) want[(size_t)i] = x[(size_t)i] - 1234;
const int64_t bad = differ(down_i32(dy, (size_t)n), want);
ok("sub_i32 exact", bad == 0, fmt("%lld entries differ", (long long)bad));
}
/* iota with and without a period. Exact. */
void iota_cases(const Plugin& pl) {
const RadKernelInfo* r = find(pl, "iota");
@@ -27309,6 +27329,7 @@ static void device_phase(const Plugin& pl) {
if (sel("rvq_embed_bf16")) au::rvq_embed_cases(pl);
if (sel("frame_store")) au::frame_store_cases(pl);
if (sel("iota")) au::iota_cases(pl);
if (sel("sub_i32")) au::sub_i32_cases(pl);
if (sel("gelu_bf16")) au::gelu_case(pl);
if (sel("mul_weight_bf16")) au::mul_weight_f32_case(pl);
if (sel("qk_norm_rope_bf16")) au::qk_norm_rope_f32_gain_case(pl);
+65
View File
@@ -6555,6 +6555,71 @@ TEST(gemma3_steps_lint_clean_at_one_and_two_ranks) {
}
}
/* The pipelined prefill on the MXFP4 trunk at two ranks: a 2048-row step of one sequence runs in two
* pieces of 1024; every collective is on the second lane, the layers' all-reduces a piece at a time
* at `overlap` 1 (two blocks a layer, two pieces a block) and the embedding's whole; the attention and
* the DFlash2 taps run a piece at a time; q|k|v and gate|up read one shared input pair. A prompt's
* first chunk and a later one alike. RADIANCE_GEMMA3_PIPE=0 runs the same step serially. */
TEST(gemma3_a_wide_prefill_runs_in_two_pieces_with_its_boundaries_on_the_second_lane) {
MetaKV mk = gemma3_dflash2_meta();
const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP);
for (int off : { 0, 1 })
for (int rank = 0; rank < 2; ++rank)
for (int32_t pctx : { 0, 4096 }) {
if (off) setenv("RADIANCE_GEMMA3_PIPE", "0", 1);
RadBuilder b;
b.encs = { { "blk.", mx } };
RadBuildCtx ctx = spec_ctx(rank, 2, 7);
ctx.max_tok = 2048;
const int rc = stderr_of_rc([&] { return gemma3::declare(&b, mk.meta(), &ctx); });
if (off) unsetenv("RADIANCE_GEMMA3_PIPE");
REQUIRE_EQ(rc, RAD_OK);
const gemma3::Model& m = gemma3::g_model[rank];
CHECK(m.fuse_mx);
CHECK_EQ(m.pipe_on, !off);
CHECK(m.n_taps > 0);
const auto pf = kit(b, {}, { 2048 }, pctx);
const auto v = run_passes(b, gemma3::step, { pf.get() }, rank, 2);
CHECK_PASSES(b, v);
const RadCtx& c = *v[0];
CHECK(c.step_fail.empty());
const int64_t E = m.g.n_embd, L = (int64_t)m.layers.size();
int ar = 0, ar_lane0 = 0, ar_piece = 0, ar_overlap = 0;
int attn = 0, wide_attn = 0, taps = 0, wide_taps = 0;
for (const RecIssue& i : c.issues) {
const RecOp* o = b.op_of(i.op);
if (!o) continue;
if (o->op == "all_reduce") {
++ar;
ar_lane0 += i.lane == 0;
ar_piece += i.n == 1024 * E;
ar_overlap += pint(o, "overlap") == 1;
}
if (o->op == "attn_paged") { ++attn; wide_attn += i.n > 1024; }
if (o->op == "cast") { ++taps; wide_taps += i.n > 1024; }
}
if (off) {
CHECK(c.joins.empty());
CHECK_EQ(ar_lane0, ar);
CHECK_EQ(attn, (int)L);
continue;
}
CHECK_EQ(ar_lane0, 0);
CHECK_EQ(ar, 4 * (int)L + 1);
CHECK_EQ(ar_piece, 4 * (int)L);
CHECK_EQ(ar_overlap, 4 * (int)L);
CHECK_EQ(attn, 2 * (int)L);
CHECK_EQ(wide_attn, 0);
CHECK_EQ(taps, 2 * m.n_taps);
CHECK_EQ(wide_taps, 0);
CHECK(!c.joins.empty());
for (const gemma3::Layer& l : m.layers) {
CHECK(l.attn.ln_q.q8 == m.pipe_in.q8);
CHECK(l.mlp.ln_gu.q8 == m.pipe_in.q8);
}
}
}
/* The DFlash2 drafter: a block of 8 (depth 7, fixed), taps copied out after the named layers into
* one column-sliced buffer, the mask token from the config, and the drafter's own KV group after
* the trunk's two. Its input_embedding_scale must be the trunk's sqrt(hidden) held in bf16