From 812cdeceb010380c24c6e78fff7c911626d966e8 Mon Sep 17 00:00:00 2001 From: mici81 Date: Wed, 28 Oct 2026 12:20:10 +0100 Subject: [PATCH] gemma3 + libr4d: the fused all-reduce junction on the lossy wire, for rows of whole 64-element groups Under --tp-wire wht6 a TranslateGemma step of 2-3 sequences (16-24 rows) sent its junction as three launches (the standalone lossy all_reduce, rmsnorm, rmsnorm_quant_fp8): the fused ar_rmsnorm_post_quant_fp8 sent the exact wire only, and the compressed fused form needed a row of whole 512-element chunks, which Gemma 3 27B's 5376 is not (10.5). - r4d_fused_quant_fp8.hip: a row of whole 64-element groups that is not whole chunks ends in a partial chunk whose missing groups go out as zeros and are not stored back. Every real group is the same 64 elements as in the flat message, so its bytes and the sum are the standalone wire's. The (row, chunk) mapping is a template parameter (PAD, wire 6 only), so a row of whole chunks (Qwen3.8's 5120) runs exactly the code it ran before; as a runtime branch it cost a Qwen3.8 x4 step 1.3-1.6 %. POST (the sandwich post-norm) on either wire. - r4d_ar_entry.hip: K % 64 instead of K % 512 for wire 6, a size check of the padded message against the slot and the local pack, and the local pack's scale region a sixteenth of the slot instead of a thirty-second (padding adds up to 7 groups a row: a 2048-row prefill chunk would have overrun the old size). - gemma3: the junction declares its lossy twin (op_ar6) and takes it up to kAr6MaxRows (24); a taller step keeps the standalone all-reduce, which spread over more blocks wins there (the fused kernel gives a block one row). TranslateGemma-27B MXFP4 + DFlash2, 2x R9700, --tp-wire wht6 --draft-short-from 8, greedy, aggregate tok/s, two rounds, junction limit never / 24 / any: 2 streams (16 rows) 350.7 346.2 / 370.4 367.0 / 357.7 356.0 3 streams (24 rows) 455.8 451.2 / 462.8 460.6 / 437.3 436.8 4 streams (32 rows) 553.7 553.6 / 553.9 560.2 / 521.0 523.0 8 streams (32 rows) 717.4 718.0 / 716.7 720.8 / 662.4 661.9 So 2 streams +6 %, 3 +2 %, 4 and 8 unchanged. Bit identity: with --tp-wire-min-kb 1 (one stream's steps go out lossy) the fused path gives the unfused one's greedy top-5 logprobs, 300 of 300 tokens, max |d| 0, on two pieces (x1 there 197.6 -> 212.6 tok/s). Qwen3.8-27B MXFP4: 300 of 300 identical on two prompts, speed unchanged. Tests: arch_test (the lossy-wire junction test now also expects a 24-row step to fuse through the twin, other op instances than the 8-row step's, and the 64-row prefill to stay unfused). Co-Authored-By: Claude Opus 5.5 (1M context) --- arch/gemma3/gemma3.cpp | 60 +++++++++++++++++++++++----- libr4d/r4d.h | 4 +- libr4d/r4d_ar_entry.hip | 19 ++++++--- libr4d/r4d_fused_quant_fp8.hip | 72 +++++++++++++++++++++++++--------- libr4d/r4d_rows.cpp | 13 +++--- tests/arch_test.cpp | 33 +++++++++++++--- 6 files changed, 156 insertions(+), 45 deletions(-) diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index 702d7b16..37d6975a 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -828,6 +828,17 @@ struct G3Junction { } }; +/* The tallest step that takes the fp8 junction's lossy twin (G3JunctionFP8::op_ar6); a taller one + * keeps the standalone lossy all-reduce, unfused. The fused kernel gives each block one row, so past + * a few sequences the standalone all-reduce, spread over more blocks, wins despite its extra + * launches. TranslateGemma-27B MXFP4 + DFlash2 at tp 2 on 2x R9700, --tp-wire wht6, production's + * flags (--draft-short-from 8), greedy, aggregate tok/s, two rounds, limit never / 16 / 24 / any: + * 2 streams (16 rows) 350.7 346.2 / 371.8 367.7 / 370.4 367.0 / 357.7 356.0; 3 (24 rows) 455.8 + * 451.2 / 455.1 454.2 / 462.8 460.6 / 437.3 436.8; 4 (32 rows) 553.7 553.6 / 557.9 554.2 / 553.9 + * 560.2 / 521.0 523.0; 8 (32 rows, the short block) 717.4 718.0 / 717.2 720.5 / 716.7 720.8 / + * 662.4 661.9. One stream's 8 rows go out exact under the default --tp-wire-min-kb (op_ar). */ +constexpr int64_t kAr6MaxRows = 24; + /* ---- the MXFP4 trunk's junction (libr4d `rmsnorm_quant_fp8`): the add of the branch `x` into * the stream `res`, the next block's norm and its E4M3 quantiser, in one op. It has no post-norm * slot (rmsnorm_quant_i8's w_post is Gemma 4's), so the branch's post-norm runs just before it: @@ -836,18 +847,20 @@ struct G3Junction { struct G3JunctionFP8 { rad_op op = 0; /* At two ranks: the branch's all-reduce and post-norm folded in front - * (ar_rmsnorm_post_quant_fp8), `x` then being this rank's partial sum. One launch. The kernel - * sends the exact wire only, so under --tp-wire it is declared all the same and taken by the - * steps whose message goes out exact anyway (ar_wire_is_exact: a decode step of a few rows, - * under --tp-wire-min-kb); a wider step keeps the lossy all-reduce, unfused. */ - rad_op op_ar = 0; + * (ar_rmsnorm_post_quant_fp8), `x` then being this rank's partial sum. One launch. `op_ar` sends + * the exact wire, taken by the steps whose message goes out exact (ar_wire_is_exact: a decode + * step of a few rows, under --tp-wire-min-kb); `op_ar6` the rotated 6-bit one, declared under a + * lossy --tp-wire and taken by the steps above that up to kAr6MaxRows, byte-identical to the + * standalone lossy all-reduce, rmsnorm and rmsnorm_quant_fp8 it replaces. */ + rad_op op_ar = 0, op_ar6 = 0; + Geom gm{}; rad_buf x = 0, res = 0, q = 0, s = 0; rad_weight w = 0, wp = 0; int64_t n = 0; int declare(RadBuilder* b, const Geom& g, rad_buf x_, rad_buf res_, rad_weight w_, const G3Linear& into, rad_weight wp_ = 0) { - x = x_; res = res_; w = w_; wp = wp_; q = into.q8; s = into.s8; n = g.n_embd; + x = x_; res = res_; w = w_; wp = wp_; q = into.q8; s = into.s8; n = g.n_embd; gm = g; if (!into.mx || into.shared_in || !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", @@ -870,14 +883,33 @@ struct G3JunctionFP8 { const rad_buf wa[4] = { x, q, s, res }; rad_op_reads (b, op_ar, rd, 2); rad_op_writes(b, op_ar, wa, 4); + /* The lossy twin where that wire is reachable and the row is whole 64-element + * groups (the kernel pads a partial last 512-element chunk). */ + if (!g.wire_exact && n % 64 == 0) { + const RadParam p6[] = { RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n), + RAD_F64("eps", g.eps), RAD_INT("group", RAD_FP8_BLOCK), + RAD_INT("world_size", g.world), RAD_STR("dtype", g.dtype), + RAD_F64("wadd", g.wadd), RAD_INT("wire", 6) }; + op_ar6 = rad_decl_op(b, "ar_rmsnorm_post_quant_fp8", p6, + (int)(sizeof p6 / sizeof p6[0]), ws, 2); + if (op_ar6) { + rad_op_reads (b, op_ar6, rd, 2); + rad_op_writes(b, op_ar6, wa, 4); + } + } } } return RAD_OK; } + /* The fused op for a T-row step's wire, or 0 when its all-reduce stays standalone. */ + rad_op ar_op(int64_t T) const { + if (op_ar && ar_wire_is_exact(gm, T)) return op_ar; + return T <= kAr6MaxRows ? op_ar6 : 0; + } /* x, residual?, w -> q, scale, out_bf16?[, w_post]: the bf16 normed copy is not wanted. */ void step(RadCtx* c, int64_t T, bool with_ar = false) const { if (with_ar) - RAD_ISSUE_N(c, op_ar, T, brows(x, T), brows(res, T), RAD_W(w), brows(q, T), brows(s, T), + RAD_ISSUE_N(c, ar_op(T), T, brows(x, T), brows(res, T), RAD_W(w), brows(q, T), brows(s, T), RAD_NONE, RAD_W(wp)); else RAD_ISSUE_N(c, op, T, brows(x, T), res ? brows(res, T) : RAD_NONE, RAD_W(w), @@ -935,6 +967,7 @@ struct Model { G3JunctionFP8 jf0{}; bool fuse_mx = false; /* the MXFP4 trunk's junctions (G3JunctionFP8) */ bool fuse_mx_ar = false; /* ... each with the all-reduce and post-norm folded in */ + bool fuse_mx_ar6 = false; /* ... on the lossy wire too (G3JunctionFP8::op_ar6) */ bool fuse_ar = false; /* The DFlash2 drafter and the trunk taps it reads, only when the container carries one and @@ -1346,6 +1379,12 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c ar = lay.jf_am.op_ar != 0 && (l + 1 >= g.n_layer || lay.jf_ma.op_ar != 0); } m.fuse_mx_ar = ar; + bool ar6 = ar; + for (int64_t l = 0; ar6 && l < g.n_layer; ++l) { + const Layer& lay = m.layers[(size_t)l]; + ar6 = lay.jf_am.op_ar6 != 0 && (l + 1 >= g.n_layer || lay.jf_ma.op_ar6 != 0); + } + m.fuse_mx_ar6 = ar6; } /* ---- the weight reads beside the decode all-reduces, at more than one rank; * --weight-prefetch off declares none. */ @@ -1674,8 +1713,11 @@ static void step(RadCtx* c, const RadBatch* batch) { for (size_t li = 0; li < m.layers.size(); ++li) { const Layer& l = m.layers[li]; const bool last = li + 1 == m.layers.size(); - /* Per step: the fused junction sends the exact wire only (G3JunctionFP8::op_ar). */ - const bool far = m.fuse_mx_ar && ar_wire_is_exact(m.g, T); + /* Per step: the fused junction takes the step's all-reduce on the exact wire + * (G3JunctionFP8::op_ar), and on the lossy one up to kAr6MaxRows where every junction + * declared its twin (op_ar6). */ + const bool far = m.fuse_mx_ar && + (ar_wire_is_exact(m.g, T) || (m.fuse_mx_ar6 && T <= kAr6MaxRows)); l.attn.step(c, batch, true, true, far); prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h); if (!far) l.attn.post(c, T); diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 48415f5f..513c0eaa 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -791,8 +791,8 @@ int r4d_gate_quant_fp8(long g, long x, long y, long q, long s, int M, int n, in // on the norm's load. IPC arguments are r4d_ar_oneshot_2rank_exact's; `nblocks` is clamped to M // because a block owns whole rows, and `x` takes no pitch because it is also the message. Like the // int8 twin r4d_ar_ln_had_quant_i8 it has two wires: `wire` 0 is the exact bf16 payload, 6 the -// rotated 6-bit one. Wire 6 needs `n` to be a multiple of r4d_ar_rmsnorm_quant_fp8_chunk_elems() -// so a chunk never straddles a row. +// rotated 6-bit one. Wire 6 needs `n` to be a multiple of 64 (a group never straddles a row); a +// row that is not whole r4d_ar_rmsnorm_quant_fp8_chunk_elems() chunks pads its last one with zeros. int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long peer_flags, long my_flags, long seq_ctrs, long slot_stride16, long x, long res, long w, long y, long q, long s, diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 6faed029..471cd894 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -351,11 +351,13 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code, sq = r4d_ar_ipc_alloc(kSeqWords(maxb) * 4, 0, handle, &hlen)); /* This rank's own packed copy, for the rotated 6-bit wire only: the reduce must fold exactly - * the bytes it sent. The 6-bit payload is 3/8 of the bf16 message and the scales 1/64, so half - * the message plus a thirty-second plus a page is ample. */ + * the bytes it sent. The 6-bit payload is 3/8 of the bf16 message and the f32 scales 1/32 of + * it, laid out from half the slot; a row padded to whole 512-element chunks + * (ar_rmsnorm_quant_fp8) adds up to 7 groups' scales a row, so the scales get a sixteenth + * (enough for any row of 512 or more) plus a page. ar_rmsnorm_quant checks the fit. */ if (in->dtype != 2) { R4D_TRY("ar_ipc_alloc(locpack)", - lp = r4d_ar_ipc_alloc(in->slot_bytes / 2 + in->slot_bytes / 32 + 4096, 0, + lp = r4d_ar_ipc_alloc(in->slot_bytes / 2 + in->slot_bytes / 16 + 4096, 0, handle, &hlen)); } @@ -1353,11 +1355,18 @@ static int rad_ar_rmsnorm_quant(const RadArgs* a, RadStream s, bool hi8) { if (wire != 0 && wire != 6) return RAD_E_INVAL; if (wire == 6) { const int ce = r4d_ar_rmsnorm_quant_fp8_chunk_elems(); - if (ce <= 0 || K % ce) return RAD_E_SHAPE; + /* Whole 64-element groups a row; the last 512-element chunk may be partial (padded). */ + if (ce <= 0 || K % 64) return RAD_E_SHAPE; /* This rank's packed-half side buffer exists for every bf16 instance; missing means a * broken init. */ if (!in->locpack) return RAD_E_STATE; - if (wpo) return RAD_E_UNSUPPORTED; // the post-norm is the exact wire's only + /* The padded message must fit: payload (3/4 byte an element) in the peer slot's half and + * in locpack's, the bf16 scales in the slot's other half, the f32 ones in locpack after + * half the slot (its allocation: a sixteenth of the slot plus a page). */ + const long long chunks = M * ((K + ce - 1) / ce), groups = chunks * (ce / 64); + if (chunks * (ce * 6 / 8) > in->slot_bytes / 2 || groups * 2 > in->slot_bytes / 2 || + groups * 4 > in->slot_bytes / 16 + 4096) + return RAD_E_SHAPE; } R4dArCfg cfgv; diff --git a/libr4d/r4d_fused_quant_fp8.hip b/libr4d/r4d_fused_quant_fp8.hip index 23c615d6..79d8e483 100644 --- a/libr4d/r4d_fused_quant_fp8.hip +++ b/libr4d/r4d_fused_quant_fp8.hip @@ -310,6 +310,10 @@ __global__ __launch_bounds__(1024) void gate_quant_fp8_kernel( // Rotation and amax close over a 64-element group, so the bytes are identical; the chunk size only // sets ownership. A chunk may not straddle a row (both ranks must agree on a block's bytes), and a // wave owns a whole chunk, so a smaller chunk employs more waves (see r4d_ar_ln_had_quant_i8.hip). +// A row of whole 64-element groups that is not whole chunks (TranslateGemma's 5376 = 10.5 chunks) +// ends in a partial chunk: its missing groups go out as zeros and are not stored back. Every real +// group is the same 64 elements as in the flat message, so its bytes, and the sum, are the +// standalone wire's; with whole chunks the mapping is the flat one. // // The reduced row goes back to global `x`, not LDS: unlike the int8 twin this norm needs no // in-place rotation, and pass two re-reads the row L1-hot as the standalone norm does. @@ -372,11 +376,16 @@ __device__ __forceinline__ unsigned int arq_load_acq(const unsigned int* p) { // POST: a sandwich-norm junction (Gemma 3): the reduced sum is post-normed against `wp` before the // residual add, `v = bf16(sum * s1 * (wp + wadd))` with s1 the sum's own rms, as rmsnorm_quant_i8's -// POST computes it. `x` still receives the reduced sum. Exact wire and a residual only. +// POST computes it. `x` still receives the reduced sum. Either wire (the compressed one has already +// stored the sum into `x`); a residual only. // // HI8: ar_rmsnorm_had_quant_i8g, pass two in norm_quant_row's rotated int8 form. The reduction and // the norm are untouched, so `x`, the residual and `y` are ar_rmsnorm_quant_fp8's bytes. -template +// PAD (wire 6 only): the row is whole 64-element groups but not whole chunks, so its last chunk is +// padded and a chunk's elements are found by (row, chunk in row). Without it the flat index is the +// element offset, the code a row of whole chunks has always run. +template __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w, const void* __restrict__ wp, @@ -393,7 +402,9 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( const int r0 = (int)(((long)b * M) / nb), r1 = (int)(((long)(b + 1) * M) / nb); /* Chunks per row and this block's window into the global chunk numbering, derived identically * on both ranks from (M, nb, n). */ - const int cpr = WIRE ? (int)(n / ARQ_CE) : 0; + const int cpr = WIRE ? (int)(PAD ? (n + ARQ_CE - 1) / ARQ_CE : n / ARQ_CE) : 0; + const int gpr = (int)(n / ARQ_GROUP); /* the real groups a row has; the rest of its last chunk pads */ + (void)gpr; const int c00 = r0 * cpr, cpb = (r1 - r0) * cpr; const uint32_t lane = tid & (ARQ_WV - 1), wave = tid / ARQ_WV; const int lig = (int)(lane & (ARQ_LPG - 1)), gw = (int)(lane / ARQ_LPG); @@ -430,8 +441,8 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( } else { /* Rotate, amax, quantise and pack straight into the peer's slot. A wave owns whole chunks, * so every lane emits ARQ_NWORD dwords per chunk and the flush is lane-uniform. No group is - * partial (n is whole chunks, a block owns whole rows), so the standalone kernel's `live` - * guard is unnecessary and every cross-lane stage is fully active. */ + * partial (n is whole groups, a block owns whole rows), so every cross-lane stage is fully + * active; a group past the row's end (a partial last chunk) packs zeros. */ for (int ci = (int)wave; ci < cpb; ci += (int)(THR / ARQ_WV)) { const int c = c00 + ci; unsigned int acc[ARQ_NWORD]; @@ -440,10 +451,18 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( #pragma unroll for (int t = 0; t < ARQ_ITERS; ++t) { const int g = c * ARQ_CG + t * ARQ_GPW + gw; - const long off = (long)g * ARQ_GROUP + (long)lig * ARQ_EPL; float v[ARQ_EPL]; + if constexpr (PAD) { + const int cr = c / cpr, gr = (c - cr * cpr) * ARQ_CG + t * ARQ_GPW + gw; + const bool live = gr < gpr; + const long off = (long)cr * n + (long)gr * ARQ_GROUP + (long)lig * ARQ_EPL; #pragma unroll - for (int i = 0; i < ARQ_EPL; ++i) v[i] = bf2f(x[off + i]); + for (int i = 0; i < ARQ_EPL; ++i) v[i] = live ? bf2f(x[off + i]) : 0.0f; + } else { + const long off = (long)g * ARQ_GROUP + (long)lig * ARQ_EPL; +#pragma unroll + for (int i = 0; i < ARQ_EPL; ++i) v[i] = bf2f(x[off + i]); + } arq_wht(v, lig); float a = 0.0f; #pragma unroll @@ -590,9 +609,18 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( + (float)((int)(lsy & ARQ_MASK) + ARQ_QMIN) * lsc; } arq_wht(v, lig); - const long off = (long)g * ARQ_GROUP + (long)lig * ARQ_EPL; + if constexpr (PAD) { + const int cr = c / cpr, gr = (c - cr * cpr) * ARQ_CG + gl; + if (gr < gpr) { + const long off = (long)cr * n + (long)gr * ARQ_GROUP + (long)lig * ARQ_EPL; #pragma unroll - for (int i = 0; i < ARQ_EPL; ++i) x[off + i] = f2bf(v[i]); + for (int i = 0; i < ARQ_EPL; ++i) x[off + i] = f2bf(v[i]); + } + } else { + const long off = (long)g * ARQ_GROUP + (long)lig * ARQ_EPL; +#pragma unroll + for (int i = 0; i < ARQ_EPL; ++i) x[off + i] = f2bf(v[i]); + } } } /* Pass two reads all of `x`'s row; each wave wrote only its own chunks. */ @@ -610,13 +638,14 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( (void)pr; float ss = 0.0f; - if constexpr (POST && WIRE == 0 && RES) { + if constexpr (POST && RES) { /* Pass one reduces and takes the sum's sum of squares; pass two post-norms it, adds it * into the stream and takes the stream's. Each thread reads back only its own columns. */ float s1s = 0.0f; for (uint32_t i = tid; i < n; i += THR) { - const uint16_t a = f2bf(bf2f(xr[i]) + bf2f(pr[i])); - xr[i] = a; + uint16_t a; + if constexpr (WIRE == 0) { a = f2bf(bf2f(xr[i]) + bf2f(pr[i])); xr[i] = a; } + else { a = xr[i]; } const float av = bf2f(a); s1s += av * av; } @@ -862,7 +891,7 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer int nblocks, int nthreads, int drain, int acq, int wire, long loc_pack, long loc_scale_off, long wp, bool hi8) { if (!x || !w || !q || !s) return -1; - if (wp && (!res || wire != 0)) return -10; // a post-norm needs the stream and the exact wire + if (wp && !res) return -10; // a post-norm needs the stream if (hi8 && wp) return -11; if (hi8 && (n % (int)kGroup)) return -12; if (!peer_scratch || !my_scratch || !peer_flags || !my_flags || !seq_ctrs) return -1; @@ -871,8 +900,9 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer // four columns (see norm_quant_row). if (n % 8) return -5; if (wire != 0 && wire != 6) return -7; - // A chunk may not straddle a row, or the ranks would disagree about a block's bytes. - if (wire == 6 && (n % ARQ_CE)) return -8; + // A group may not straddle a row, or the ranks would disagree about a block's bytes; a row's + // last chunk may be partial (its missing groups go out as zeros). + if (wire == 6 && (n % ARQ_GROUP)) return -8; if (wire == 6 && (!loc_pack || loc_scale_off <= 0)) return -9; if (!r_ld) r_ld = n; if (!y_ld) y_ld = n; @@ -887,8 +917,9 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer // The width must be r4d_act_threads', as for the standalone norm (it fixes the summation order). const int thr = nthreads ? nthreads : r4d_act_threads(M, n); if (thr != 256 && thr != 512 && thr != 1024) return -6; -#define R4D_ARQ_H(wf, rs, T, W, PO, H) \ - hipLaunchKernelGGL((ar_rmsnorm_quant_fp8_kernel), dim3((uint32_t)nb), \ + const bool pad = wire == 6 && (n % ARQ_CE) != 0; +#define R4D_ARQ_HP(wf, rs, T, W, PO, H, PD) \ + hipLaunchKernelGGL((ar_rmsnorm_quant_fp8_kernel), dim3((uint32_t)nb), \ dim3(T), 0, \ (hipStream_t)stream, (uint16_t*)x, (uint16_t*)res, (const void*)w, \ (const void*)wp, \ @@ -898,12 +929,16 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer (long)loc_scale_off, M, (uint32_t)n, \ (uint32_t)nkb, eps, wadd, (uint32_t)r_ld, (uint32_t)y_ld, (uint32_t)q_ld, \ (uint32_t)s_ld, (int)slot_stride16, drain, acq, (unsigned)nbw) +#define R4D_ARQ_H(wf, rs, T, W, PO, H) \ + do { if (pad) R4D_ARQ_HP(wf, rs, T, W, PO, H, (W == 6)); \ + else R4D_ARQ_HP(wf, rs, T, W, PO, H, false); } while (0) #define R4D_ARQ_P(wf, rs, T, W, PO) R4D_ARQ_H(wf, rs, T, W, PO, false) #define R4D_ARQ_T(wf, rs, T, W) \ do { if (hi8) R4D_ARQ_H(wf, rs, T, W, false, true); \ else R4D_ARQ_P(wf, rs, T, W, false); } while (0) #define R4D_ARQ_W(wf, rs, T) \ - do { if (wire == 6) R4D_ARQ_T(wf, rs, T, 6); \ + do { if (wire == 6 && rs && wp) R4D_ARQ_P(wf, rs, T, 6, true); \ + else if (wire == 6) R4D_ARQ_T(wf, rs, T, 6); \ else if (rs && wp) R4D_ARQ_P(wf, rs, T, 0, true); \ else R4D_ARQ_T(wf, rs, T, 0); } while (0) #define R4D_ARQ(wf, rs) \ @@ -917,6 +952,7 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer #undef R4D_ARQ_T #undef R4D_ARQ_P #undef R4D_ARQ_H +#undef R4D_ARQ_HP return hipGetLastError() == hipSuccess ? 0 : -4; } diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index a730810e..a69b5db8 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -1448,7 +1448,7 @@ static const RadOperandSpec oArRmsNormQuantFp8[] = { }; /* ar_rmsnorm_post_quant_fp8: the same with a sandwich post-norm of the reduced sum before the add - * (Gemma 3's junction, arch/gemma3), exact wire only. Its own op: an optional seventh operand on + * (Gemma 3's junction, arch/gemma3), on either wire. Its own op: an optional seventh operand on * ar_rmsnorm_quant_fp8 would break every plugin that issues that op's six. */ static const RadOperandSpec oArRmsNormPostQuantFp8[] = { { "x", RAD_OPD_INOUT, 0 }, @@ -3529,7 +3529,7 @@ static const RadOpSchema kSchemas[] = { SCHEMA("ar_rmsnorm_post_quant_fp8", pArRmsNormQuantFp8, oArRmsNormPostQuantFp8, "ar_rmsnorm_quant_fp8 with a post-norm of the reduced sum against w_post before the " - "residual add (a sandwich-norm junction), exact wire only, byte-identical to all_reduce, " + "residual add (a sandwich-norm junction), either wire, byte-identical to all_reduce, " "rmsnorm, rmsnorm_quant_fp8."), SCHEMA("ar_rmsnorm_quant_fp8", pArRmsNormQuantFp8, oArRmsNormQuantFp8, "the two-rank all-reduce and the residual add, RMS norm and block-scaled fp8 " @@ -8756,7 +8756,7 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "exactly 2 ranks; n a multiple of 8 and x unpitched, because x is the message and a block pushes " "its rows as 16B words; group 128; a block owns whole ROWS, so the block count is clamped to M; " "either wire, chosen by `wire` -- the rotated 6-bit payload additionally needs n to be a " - "multiple of 512, so that a chunk never straddles a row; residual and out_bf16 may be null", + "multiple of 64, so that a group never straddles a row (a row's last 512-element chunk may be partial); residual and out_bf16 may be null", "bf16 in, E4M3 out with an f32 scale per (row, 128 columns) and an optional bf16 out; codes " "taken over the BF16-ROUNDED norm output, and the reduced sum rounded to bf16 exactly as the " "standalone all-reduce of the SAME wire stores it -- byte-identical to the pair it replaces", @@ -8773,7 +8773,7 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "back", "exactly 2 ranks; n a multiple of 128 (a rotation block cannot straddle a row) and x unpitched; " "group 128; a block owns whole ROWS, so the block count is clamped to M; either wire, chosen by " - "`wire` -- the rotated 6-bit payload additionally needs n to be a multiple of 512; residual and " + "`wire` -- the rotated 6-bit payload pads a row's partial last 512-element chunk; residual and " "out_bf16 may be null", "bf16 in, int8 out with an f32 scale per (row, 128 columns) and an optional bf16 out of the " "UNROTATED norm output; x and the residual are ar_rmsnorm_quant_fp8's bytes, and the codes " @@ -8786,8 +8786,9 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, { "ar_rmsnorm_post_quant_fp8", "ar_rmsnorm_post_quant_fp8", "quant", "the two-rank all-reduce, a post-norm of the reduced sum, the residual add, RMS norm and " "block-scaled fp8 quantisation in one kernel: a sandwich-norm junction (Gemma 3)", - "exactly 2 ranks, the exact wire; n a multiple of 8 and x unpitched; group 128; a block owns whole " - "rows; the residual is required", + "exactly 2 ranks, either wire chosen by `wire` (the rotated 6-bit payload needs n a multiple of 64; " + "a row's last 512-element chunk may be partial); n a multiple of 8 and x unpitched; group 128; a " + "block owns whole rows; the residual is required", "bf16 in, E4M3 out with an f32 scale per (row, 128 columns); x receives the reduced sum", RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, ROW(cArRmsNormQuantFp8), ROW(tAr), nullptr, diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 91cc8a70..b79be67e 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -8226,10 +8226,12 @@ TEST(gemma3_without_fused_junctions_every_linear_quantises_into_what_it_reads) { CHECK(quant >= 2 * (int)m.layers.size()); } -/* Under a lossy --tp-wire the fused all-reduce junctions, which send the exact wire only, still - * serve the steps whose message goes out exact anyway: a decode step's few rows fold every - * junction's all-reduce in, while a prefill chunk above --tp-wire-min-kb keeps the lossy - * all-reduce, unfused. Both families: the MXFP4 trunk's fp8 junction and a w4a8 trunk's int8 one. */ +/* Under a lossy --tp-wire a decode step's few rows, whose message goes out exact, fold every + * junction's all-reduce in. On the MXFP4 trunk a lossy step up to kAr6MaxRows (24) does too, + * through the fp8 junction's lossy twin (ar_rmsnorm_post_quant_fp8 at wire 6; TranslateGemma's + * 5376-wide row is whole 64-element groups, its last 512-element chunk padded); a taller one, like a + * prefill chunk, keeps the lossy all-reduce, unfused. A w4a8 trunk's int8 junction sends the exact + * wire only. Both families: the MXFP4 trunk's fp8 junction and a w4a8 trunk's int8 one. */ TEST(gemma3_under_a_lossy_wire_small_steps_keep_the_fused_junction) { MetaKV mk = gemma3_meta(); const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP); @@ -8244,7 +8246,7 @@ TEST(gemma3_under_a_lossy_wire_small_steps_keep_the_fused_junction) { ctx.tp_wire_min_bytes = 128 * 1024; REQUIRE_EQ(gemma3::declare(&b, mk.meta(), &ctx), RAD_OK); const gemma3::Model& m = gemma3::g_model[rank]; - if (is_mx) CHECK(m.fuse_mx && m.fuse_mx_ar); + if (is_mx) CHECK(m.fuse_mx && m.fuse_mx_ar && m.fuse_mx_ar6); else CHECK(!m.fuse_mx && m.fuse && m.fuse_ar); auto issued = [&](const RadCtx& c, std::initializer_list names) { int n = 0; @@ -8263,6 +8265,27 @@ TEST(gemma3_under_a_lossy_wire_small_steps_keep_the_fused_junction) { CHECK_EQ(fused_pre, 0); /* Every junction the decode step fused is an all-reduce the prefill step issues alone. */ CHECK_EQ(issued(*v[1], { "all_reduce" }), issued(*v[0], { "all_reduce" }) + fused_dec); + /* 24 rows (24 x 5376 x 2 bytes, above the floor): the MXFP4 trunk fuses the same junctions + * through the lossy twin, other op instances than the decode step's; the int8 one cannot. */ + const auto mid = kit(b, {}, { 24 }); + const auto vm = run_passes(b, gemma3::step, { mid.get() }, rank, 2); + CHECK_PASSES(b, vm); + const int fused_mid = issued(*vm[0], { "ar_rmsnorm_quant_i8", "ar_rmsnorm_post_quant_fp8" }); + if (is_mx) { + CHECK_EQ(fused_mid, fused_dec); + CHECK_EQ(issued(*vm[0], { "all_reduce" }), issued(*v[0], { "all_reduce" })); + std::vector dec_ids; + for (const RecIssue& i : v[0]->issues) + if (const RecOp* o = b.op_of(i.op); o && o->op == "ar_rmsnorm_post_quant_fp8") + dec_ids.push_back((int)i.op); + int shared = 0; + for (const RecIssue& i : vm[0]->issues) + if (const RecOp* o = b.op_of(i.op); o && o->op == "ar_rmsnorm_post_quant_fp8") + shared += std::find(dec_ids.begin(), dec_ids.end(), (int)i.op) != dec_ids.end(); + CHECK_EQ(shared, 0); + } else { + CHECK_EQ(fused_mid, 0); + } } }