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

+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);