four-rank ar_hc_write: the exact one-shot with the gated residual's write

At --tp 4 each gated-residual connection ran all_reduce and hc_write_bf16
as two launches. ar_hc_write_4rank fuses them: the wide exact one-shot,
reduced in place, with r4d_ar_hc_write_word as its epilogue. It takes the
steps the exact one-shot would (under 1 MiB, and under min_bytes on wht6);
larger and lossy messages keep the two launches. Two ranks unchanged.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-10-05 12:52:54 +02:00
1 parent f1d4a54581
commit c36e6fa2b3
10 files changed
+394 -35

No files matched your search

+11 -5
View File
@@ -156,9 +156,10 @@ struct HyperConn {
int64_t hn() const { return c.hc * g.n_embd; }
/* THE REDUCING WRITE THIS STEP ISSUES, on the wire its message goes out on; 0 where that wire
* has no fused form, and the block's own collective then runs in front of the plain write. */
* has no fused form, and the block's own collective then runs in front of the plain write. At
* four ranks there is no rotated form, so a step ar_exact_write_takes declines gets 0. */
rad_op ar_write(int64_t T) const {
return ar_wire_is_exact(g, T) ? op_ar_write : op_ar_write6;
return ar_exact_write_takes(g, T) ? op_ar_write : op_ar_write6;
}
/* WHETHER THIS STEP'S WRITE REDUCES THE BLOCK'S OUTPUT. The block in front asks the same
* question of the same T -- ar_taken with ar_take() -- before skipping its own collective. */
@@ -293,15 +294,17 @@ inline int HyperConn::declare(RadBuilder* b, Names& nm, const Geom& geom, const
{w.x.x, w.inj, w.h}, {w.h});
if (!op_write) return RAD_E_INVAL;
/* A handle that did not resolve is 0 and not an error: a backend without the fused kernel
* keeps the pair, and takes_ar() then answers no on every step. */
if (c.reduce && g.world == 2)
* keeps the pair, and takes_ar() then answers no on every step. At four ranks only the
* exact form exists, and it takes a step only where ar_wide_write_exact says the
* standalone collective would have been the exact one-shot. */
if (c.reduce && (g.world == 2 || g.world == 4))
op_ar_write = rw(b, RAD_OP(b, "ar_hc_write",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_INT("hc", c.hc), RAD_INT("world_size", g.world),
RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS),
{w.x.x, w.inj, w.h}, {w.x.x, w.h});
if (op_ar_write && !g.wire_exact)
if (op_ar_write && !g.wire_exact && g.world == 2)
op_ar_write6 = rw(b, RAD_OP(b, "ar_hc_write",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_INT("hc", c.hc), RAD_INT("world_size", g.world),
@@ -314,6 +317,9 @@ inline int HyperConn::declare(RadBuilder* b, Names& nm, const Geom& geom, const
inline int HyperConn::declare_gather(RadBuilder* b, const GatherSrc& src) {
if (!op_ar_write || src.top_k <= 0 || !src.ye || !src.ew || !src.sorted) return RAD_OK;
/* Two ranks only: there is no four-rank gather form yet, so there the block keeps its gather
* and this write takes the reduction alone (both bounds stay 0). */
if (g.world != 2) return RAD_OK;
const bool shared = src.sh && src.sg;
gsrc = src;
/* EVERY ROW COUNT ONE PASS CARRIES, prefill chunks included: the kernel reads its slots from
+39 -2
View File
@@ -324,13 +324,50 @@ inline bool ar_fused_ok(const Geom& g, int64_t T) {
return ar_wire_is_exact(g, T) || (g.n_embd % kArWire6Chunk) == 0;
}
/* THE FOUR-RANK FUSED WRITE'S STEPS. At four ranks ar_hc_write exists on the exact wire only, and
* it is the exact ONE-SHOT with the write behind it -- so it may take a message only where the
* standalone `all_reduce` would have sent that message exact and as a one-shot:
*
* exact --tp-wire exact, or a message the four-rank lossy row serves exactly itself: its
* tiered-int8 groups of 32 must tile each of the four shards (numel % 128), and below
* `min_bytes` it hands the message to the exact entry. ar_wire_is_exact's three facts,
* with that wire's tile.
* one-shot under 1 MiB. From there libr4d's exact entry runs the two-shot instead
* (R4D_AR_EXACT_TWOSHOT_MIN_BYTES, r4d_ar_entry.hip), which moves half the bytes, so
* the pair stays. Either way the bytes are the same; this bound is a cost, not a
* correctness line.
*
* ar_wire_is_exact itself stays false off two ranks: the embedding's collective asks it whether
* layer 0's fused norm absorbs that reduction, and at four no norm does. Both ranks evaluate this
* on the same T, so neither can take a different branch. */
static const int64_t kArWideWireTile = 128;
static const int64_t kArWideOneshotBytes = 1024 * 1024;
inline bool ar_wide_write_exact(const Geom& g, int64_t T) {
if (g.world != 4) return false;
const int64_t numel = T * g.n_embd;
const int64_t bytes = numel * 2; /* bf16 on the wire */
if (numel <= 0 || bytes >= kArWideOneshotBytes) return false;
if (g.wire_exact) return true;
if (numel % kArWideWireTile) return true;
return bytes < g.wire_min_bytes;
}
/* WHETHER A GATED RESIDUAL'S EXACT FUSED WRITE TAKES THE ALL-REDUCE AT T ROWS: at two ranks on every
* step whose wire is exact, at four where ar_wide_write_exact says so. The write
* (HyperConn::ar_write) and the block in front (ar_taken with kArOutExact) both ask exactly this. */
inline bool ar_exact_write_takes(const Geom& g, int64_t T) {
return g.world == 2 ? ar_wire_is_exact(g, T) : ar_wide_write_exact(g, T);
}
/* WHICH WIRES THE CONSUMER OF A BLOCK'S ALL-REDUCE TAKES. A block is declared without knowing what
* follows it, so the architecture tells it:
*
* kArOutNorm the next block's norm, which takes either wire it has a fused form for
* (ar_fused_ok);
* kArOutExact a gated residual's write with the exact fused form only, so a message that would
* go out compressed stays with the block;
* go out compressed stays with the block (ar_exact_write_takes -- at four ranks
* also one the standalone row would send as a two-shot);
* kArOutBoth a gated residual's write with a fused form on each wire, which takes every
* message (HyperConn::ar_take). */
enum ArOut : int {
@@ -344,7 +381,7 @@ enum ArOut : int {
inline bool ar_taken(const Geom& g, int64_t T, bool out, int take) {
if (!out) return false;
if (take == kArOutBoth) return true;
if (take == kArOutExact) return ar_wire_is_exact(g, T);
if (take == kArOutExact) return ar_exact_write_takes(g, T);
return ar_fused_ok(g, T);
}
+8 -4
View File
@@ -332,10 +332,14 @@ resolves to, not the same kernel over fewer heads. Any world size above the KV h
multiple of it: 4 is a multiple of 2, 3 and 5 are not, and those widths are refused at declare
time.
**The fused all-reduce forms are two-rank.** At four ranks nothing fuses: the hyper-connection pair
runs the standalone sequence — `all_reduce` (libr4d's 4-rank oneshot/twoshot), then
`hc_write_bf16`, then `moe_gather` — 2 to 3 extra launches per pair against the two-rank fused
form. The unfused sequence is declared beside the probe, so nothing is unresolved at boot. The
**The fused all-reduce forms are two-rank, except the exact write.** At four ranks `ar_hc_write`
exists on the exact wire only (`ar_hc_write_4rank`, the 4-rank one-shot with the write as its
epilogue, on the plain all-reduce's buffers). A step takes it where the standalone `all_reduce`
would have been the exact one-shot: under 1 MiB, and with `--tp-wire wht6` also under
`--tp-wire-min-kb`. Larger messages, and every lossy one, keep the standalone sequence —
`all_reduce` (4-rank two-shot or ti8), then `hc_write_bf16`. There is no four-rank
`ar_gather_hc_write`, so `moe_gather` stays a launch of its own. The unfused sequence is declared
beside the fused one, so nothing is unresolved at boot. The
attention's own fusion is not a collective and does carry over: the sparse decode with the output
gate and the quantiser in its merge (`attn_paged_gate_quant`) has gqa-6, page-4 rows. The per-step cost against tp2 on the
same cards is unmeasured.
+2 -1
View File
@@ -771,7 +771,8 @@ and the input, and the reduced sum lands in it.
`ar_hc_write` is `all_reduce` of `y` followed by `hc_write` of the reduced `y`, for a block whose
output feeds a gated residual rather than a norm. `y` is INOUT for the same reason `x` is above.
It serves both wires, one op a wire: a caller declares the rotated form beside the exact one and
issues whichever the step's message goes out on, and a `wire` of 6 is 6 at every size.
issues whichever the step's message goes out on, and a `wire` of 6 is 6 at every size. At four
ranks only the exact form exists.
`ar_gather_hc_write` is `moe_gather` followed by `ar_hc_write`: the gather's output IS the
message, so `y` is written by the gather half before it is reduced in place, and the five gather
+10
View File
@@ -340,6 +340,16 @@ void r4d_ar_oneshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee
long my_flags, long seq_ctrs, long slot_stride16, long inp,
long out, long n_elem, long dtype, long rank, long stream,
long nblocks, long nthreads, long drain, long acq, long pub);
// r4d_ar_hc_write at four ranks: the 4-rank exact one-shot of `y` [M][n] bf16, in place, with
// hc_write as its epilogue -- byte-identical to that all-reduce then hc_write. Same buffer trio,
// peer order, flag values and slots as r4d_ar_oneshot_4rank_exact, so the two (and the two-shots)
// may share one instance. `irow`/`hrow` as r4d_ar_hc_write's; y is tight.
void r4d_ar_hc_write_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
long peer_flags0, long peer_flags1, long peer_flags2,
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
long y, long inj, long h, long M, long n, long hc, long irow,
long hrow, long rank, long stream, long nblocks, long nthreads,
long drain, long acq, long pub);
void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers);
// ---- all-reduce: two-shot, 4 or 8 ranks over P2P -------------------------------------------
+99 -14
View File
@@ -381,15 +381,7 @@ extern "C" int r4d_rad_ar_init(const RadParam* p, int n_p, int rank, int world,
/* The fused all-reduce + norm declares `M`(range) and `n` rather than `numel`: its message is the
* post-projection activation. Same world, same buffers, different arithmetic to get the bound. */
extern "C" int r4d_rad_ar_ln_init(const RadParam* p, int n_p, int rank, int world,
void** out) {
if (!out) return RAD_E_INVAL;
*out = nullptr;
const long long ws_param = pgeti_hi(p, n_p, "world_size", world);
if (ws_param != world) return RAD_E_SHAPE;
if (world != 2) return RAD_E_SHAPE; /* the handshake is one peer flag per block */
static int ar_mn_acquire(const RadParam* p, int n_p, int rank, int world, void** out) {
const long long M = pgeti_hi(p, n_p, "M", 0);
const long long K = pgeti_hi(p, n_p, "n", 0);
if (M <= 0 || K <= 0) return RAD_E_INVAL;
@@ -401,6 +393,36 @@ extern "C" int r4d_rad_ar_ln_init(const RadParam* p, int n_p, int rank, int worl
return RAD_OK;
}
extern "C" int r4d_rad_ar_ln_init(const RadParam* p, int n_p, int rank, int world,
void** out) {
if (!out) return RAD_E_INVAL;
*out = nullptr;
const long long ws_param = pgeti_hi(p, n_p, "world_size", world);
if (ws_param != world) return RAD_E_SHAPE;
if (world != 2) return RAD_E_SHAPE; /* the handshake is one peer flag per block */
return ar_mn_acquire(p, n_p, rank, world, out);
}
/* The four-rank ar_hc_write. Its key is the one the plain all_reduce at the same call site builds
* -- {rank, 4, bf16, M_hi * n}, and the blocks declare that all_reduce's numel up to max_tok * n --
* so the fused write and the standalone collective it stands in for run on the SAME scratch, flags
* and counters, and a step that takes either one keeps a single launch sequence on that set.
* Exact wire only: a declared `wire` 6 is refused here, at declare, rather than at the first step. */
extern "C" int r4d_rad_ar_hc_wide_init(const RadParam* p, int n_p, int rank, int world,
void** out) {
if (!out) return RAD_E_INVAL;
*out = nullptr;
const long long ws_param = pgeti_hi(p, n_p, "world_size", world);
if (ws_param != world) return RAD_E_SHAPE;
if (world != 4) return RAD_E_SHAPE; /* the peer bundle is compiled for four */
if (pgeti_hi(p, n_p, "wire", 0) != 0) return RAD_E_UNSUPPORTED;
return ar_mn_acquire(p, n_p, rank, world, out);
}
extern "C" void r4d_rad_ar_fini(void* instance) {
/* One fini per successful init, and the instance is shared across every op that resolved to
* the same world: the last release frees it. */
@@ -1176,12 +1198,16 @@ extern "C" int64_t r4d_rad_ar_gather_hc_write_scratch(const RadArgs* a) {
return M > 0 && k > 0 ? M * k * (int64_t)sizeof(int) : 0;
}
extern "C" int r4d_rad_ar_hc_write(const RadArgs* a, RadStream s) {
const ArInst* in = (const ArInst*)(a ? a->instance : nullptr);
if (!in) return RAD_E_STATE;
if (in->world != 2) return RAD_E_SHAPE;
if (in->dtype != 0) return RAD_E_DTYPE;
/* The operand and shape rules ar_hc_write states at every width. */
struct HcWriteArgs {
const RadTensor* y = nullptr;
const RadTensor* inj = nullptr;
const RadTensor* h = nullptr;
long long M = 0, n = 0, hc = 0, irow = 0, hrow = 0;
int wire = 0;
};
static int ar_hc_write_args(const RadArgs* a, const ArInst* in, HcWriteArgs* o) {
const RadTensor* y = r4d_opt(a, AH_Y);
const RadTensor* inj = r4d_opt(a, AH_INJ);
const RadTensor* h = r4d_opt(a, AH_H);
@@ -1203,6 +1229,27 @@ extern "C" int r4d_rad_ar_hc_write(const RadArgs* a, RadStream s) {
const int wire = (int)r4d_geti_or(a, "wire", 0);
if (wire != 0 && wire != 6) return RAD_E_INVAL;
o->y = y; o->inj = inj; o->h = h;
o->M = M; o->n = n; o->hc = hc; o->irow = irow; o->hrow = hrow;
o->wire = wire;
return RAD_OK;
}
extern "C" int r4d_rad_ar_hc_write(const RadArgs* a, RadStream s) {
const ArInst* in = (const ArInst*)(a ? a->instance : nullptr);
if (!in) return RAD_E_STATE;
if (in->world != 2) return RAD_E_SHAPE;
if (in->dtype != 0) return RAD_E_DTYPE;
HcWriteArgs w;
const int arc = ar_hc_write_args(a, in, &w);
if (arc != RAD_OK) return arc;
const RadTensor* const y = w.y;
const RadTensor* const inj = w.inj;
const RadTensor* const h = w.h;
const long long M = w.M, n = w.n, hc = w.hc, irow = w.irow, hrow = w.hrow;
const int wire = w.wire;
R4dArCfg cfgv;
r4d_cfg_ar(a, &cfgv);
const long words16 = (long)(M * n * 2 / 16);
@@ -1228,3 +1275,41 @@ extern "C" int r4d_rad_ar_hc_write(const RadArgs* a, RadStream s) {
nblocks, cfgv.nthreads, cfgv.drain, cfgv.acq));
return RAD_OK;
}
/* AT FOUR RANKS, EXACT WIRE ONLY: the wide one-shot with the write as its epilogue. The four-rank
* lossy wire is the ti8 two-shot and has no fused form, so a `wire` 6 is refused (init already
* refuses to build it). The architecture issues this only for a message the standalone row would
* have served with the exact one-shot (ar_wide_write_exact, arch/common/rad_fp8.h); larger exact
* messages are still served correctly here, as a one-shot, which the standalone entry would have
* replaced by the two-shot above R4D_AR_EXACT_TWOSHOT_MIN_BYTES -- same bytes, more traffic.
*
* The block count is the plain one-shot's rule (ar_setup's nb_for over the whole message under
* nb_cap); a block's words, flags and slot are the one-shot's, which is what lets the two share an
* instance. */
extern "C" int r4d_rad_ar_hc_write_4rank(const RadArgs* a, RadStream s) {
const ArInst* in = (const ArInst*)(a ? a->instance : nullptr);
if (!in) return RAD_E_STATE;
if (in->world != 4) return RAD_E_SHAPE;
if (in->dtype != 0) return RAD_E_DTYPE;
HcWriteArgs w;
const int arc = ar_hc_write_args(a, in, &w);
if (arc != RAD_OK) return arc;
if (w.wire != 0) return RAD_E_UNSUPPORTED;
R4dArCfg cfgv;
r4d_cfg_ar(a, &cfgv);
const long words16 = (long)(w.M * w.n * 2 / 16);
int nblocks = cfgv.nblocks > 0 ? cfgv.nblocks : nb_for(words16, in->nb_cap);
if (nblocks > in->max_blocks) nblocks = in->max_blocks;
R4D_TRY("ar_hc_write_4rank",
r4d_ar_hc_write_4rank(in->peer_scratch[0], in->peer_scratch[1], in->peer_scratch[2],
in->peer_flags[0], in->peer_flags[1], in->peer_flags[2],
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
r4d_addr(w.y), r4d_addr(w.inj), r4d_addr(w.h),
(long)w.M, (long)w.n, (long)w.hc, (long)w.irow, (long)w.hrow,
(long)in->rank, (long)(uintptr_t)s, nblocks, cfgv.nthreads,
cfgv.drain, cfgv.acq, cfgv.pub));
return RAD_OK;
}
+149
View File
@@ -9,9 +9,13 @@
// Sends (ws-1)*N bytes per rank: for small messages only, where latency dominates.
// The two-shot covers larger sizes.
//
// Also r4d_ar_hc_write_4rank: the same one-shot with hc_write as its epilogue (ar_hc_write at
// width, exact wire only).
//
// The IPC helpers live in r4d_ar_oneshot_2rank_exact.hip; this unit links beside it.
#include "r4d.h"
#include "r4d_ar_wide.h"
#include "r4d_ar_hc.h" // the hc write word, shared with the two-rank fused forms
#include <stdexcept>
#include <string>
@@ -108,6 +112,75 @@ __global__ void r4d_ar_oneshot_wide_exact_kernel(
}
}
// THE SAME ONE-SHOT WITH THE GATED RESIDUAL'S WRITE AS ITS EPILOGUE, bf16 only. `y` is the
// message and is reduced IN PLACE; everything up to the reduce is the kernel above, step for step,
// because both run on one instance's buffers in one stream: the same seq take, the same handshake
// value 2*s, the same (parity, source) slots and the same block partition of the word space. The
// reduce is the same ar_vsum, so the stored word is the plain all-reduce's to the bit; then
// h[r][c*n + i] += inj[r][c] * y[r][i] in hc_write's arithmetic over that bf16 word
// (r4d_ar_hc_write_word, as the two-rank r4d_ar_hc_write_kernel).
//
// In place is safe: a thread reads every source of word k before it stores word k, its own block
// pushed that word to the peers before the handshake, and no peer reads this rank's `y`. A word is
// eight elements and n is a multiple of eight, so a word never straddles a row and the thread that
// reduces it owns every stream element behind it.
template <int WS>
__global__ void r4d_ar_hc_write_wide_kernel(
int4* __restrict__ y4, R4DArWidePeers<int4, WS - 1> peers, const int4* __restrict__ my_base,
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs,
const unsigned short* __restrict__ inj, unsigned short* __restrict__ h, int n16, int n,
int hc, long irow, long hrow, int slot_stride16, int rank, int drain, int acq, int pub) {
constexpr int NPEERS = WS - 1;
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
__shared__ unsigned int s_seq, s_slot;
r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot); // device-resident, replay-safe
const unsigned int s = s_seq;
const int parity = (int)s_slot;
const int w = (n16 + nb - 1) / nb;
const int start = b * w;
int end = start + w; if (end > n16) end = n16;
int4* ps[NPEERS];
#pragma unroll
for (int p = 0; p < NPEERS; ++p)
ps[p] = peers.scratch[p] + ((size_t)parity * WS + rank) * (size_t)slot_stride16;
{
int k = start + tid;
for (; k + 3 * nt < end; k += 4 * nt) {
const int4 v0 = y4[k], v1 = y4[k + nt], v2 = y4[k + 2 * nt], v3 = y4[k + 3 * nt];
#pragma unroll
for (int p = 0; p < NPEERS; ++p) {
ps[p][k] = v0; ps[p][k + nt] = v1; ps[p][k + 2 * nt] = v2; ps[p][k + 3 * nt] = v3;
}
}
for (; k < end; k += nt) {
const int4 v = y4[k];
#pragma unroll
for (int p = 0; p < NPEERS; ++p) ps[p][k] = v;
}
}
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
const int4* src[WS];
#pragma unroll
for (int r = 0; r < WS; ++r)
src[r] = (r == rank) ? (const int4*)y4
: (my_base + ((size_t)parity * WS + r) * (size_t)slot_stride16);
const int wpr = n >> 3; // 16B words a row
for (int k = start + tid; k < end; k += nt) {
const int4 v = ar_vsum<__hip_bfloat16, 8, WS>(src, k);
y4[k] = v;
const int row = k / wpr, i0 = (k - row * wpr) * 8;
r4d_ar_hc_write_word(inj, h, row, i0, n, hc, irow, hrow,
reinterpret_cast<const unsigned short*>(&v));
}
}
// --- host side ---
template <int WS>
static void ar_wide_launch(const long* peer_scratch, const long* peer_flags, long my_scratch,
@@ -205,6 +278,82 @@ void r4d_ar_oneshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee
dtype, rank, stream, nblocks, nthreads, drain, acq, pub);
}
// ar_wide_launch's checks, for the fused write: the same rank range, slot stride, grid clamps and
// allocation extents, plus the write's own two shape rules.
template <int WS>
static void ar_hc_write_wide_launch(const long* peer_scratch, const long* peer_flags,
long my_scratch, long my_flags, long seq_ctrs,
long slot_stride16, long y, long inj, long h, long M, long n,
long hc, long irow, long hrow, long rank, long stream_i,
long nblocks, long nthreads, long drain, long acq, long pub) {
constexpr int NPEERS = WS - 1;
static_assert(NPEERS <= R4D_AR_WIDE_MAX_PEERS, "npeers exceeds the peer bundle bound");
if (rank < 0 || rank >= WS)
throw std::runtime_error("ar_hc_write: rank " + std::to_string(rank) +
" out of range for the " + std::to_string(WS) +
" ranks this entry point is compiled for");
if (M <= 0 || n <= 0 || n % 8)
throw std::runtime_error("ar_hc_write: n must be a positive multiple of 8");
if (hc < 1 || hrow % 8)
throw std::runtime_error("ar_hc_write: the streams must be whole 16B words");
const long n16l = M * n / 8; // bf16: eight elements a 16B word
if (n16l > (long)slot_stride16)
throw std::runtime_error("ar_hc_write: message (" + std::to_string(n16l) +
" 16B words) exceeds the per-source slot stride (" +
std::to_string(slot_stride16) + ")");
const int n16 = (int)n16l;
int nb = (int)nblocks;
if (nb < 1) nb = R4D_AR_NB_DESIGN;
if (nb > n16) nb = n16;
if (nb > R4D_AR_WIDE_MAX_BLOCKS) nb = R4D_AR_WIDE_MAX_BLOCKS;
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
R4DArWidePeers<int4, NPEERS> peers{};
for (int i = 0; i < NPEERS; ++i) {
if (!peer_scratch[i] || !peer_flags[i])
throw std::runtime_error("ar_hc_write: null peer scratch/flag pointer at index " +
std::to_string(i));
peers.scratch[i] = (int4*)peer_scratch[i];
peers.flags[i] = (unsigned int*)peer_flags[i];
peers.rank[i] = (i < (int)rank) ? i : i + 1;
}
const long slot_bytes = 2L * WS * (long)slot_stride16 * 16;
const long flag_bytes = (long)nb * WS * (long)sizeof(unsigned int);
r4d_ar_check_extent(my_scratch, slot_bytes, "ar_hc_write", "my_scratch");
r4d_ar_check_extent(my_flags, flag_bytes, "ar_hc_write", "my_flags");
r4d_ar_check_extent(seq_ctrs, 2L * R4D_AR_WIDE_MAX_BLOCKS * (long)sizeof(unsigned int),
"ar_hc_write", "seq_ctrs");
for (int i = 0; i < NPEERS; ++i) {
r4d_ar_check_extent(peer_scratch[i], slot_bytes, "ar_hc_write", "peer scratch");
r4d_ar_check_extent(peer_flags[i], flag_bytes, "ar_hc_write", "peer flags");
}
hipStream_t st = (hipStream_t)stream_i;
r4d_ar_hc_write_wide_kernel<WS><<<nb, nt, 0, st>>>(
(int4*)y, peers, (const int4*)my_scratch, (unsigned int*)my_flags, (unsigned int*)seq_ctrs,
(const unsigned short*)inj, (unsigned short*)h, n16, (int)n, (int)hc, irow, hrow,
(int)slot_stride16, (int)rank, (int)drain, (int)acq, (int)pub);
}
void r4d_ar_hc_write_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
long peer_flags0, long peer_flags1, long peer_flags2,
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
long y, long inj, long h, long M, long n, long hc, long irow,
long hrow, long rank, long stream, long nblocks, long nthreads,
long drain, long acq, long pub) {
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
ar_hc_write_wide_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, y, inj, h,
M, n, hc, irow, hrow, rank, stream, nblocks, nthreads, drain, acq,
pub);
}
void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers) {
if (max_blocks) *max_blocks = R4D_AR_WIDE_MAX_BLOCKS;
if (nb_design) *nb_design = R4D_AR_NB_DESIGN;
+3
View File
@@ -183,6 +183,9 @@ int r4d_rad_ar_ln_init(const RadParam*, int, int rank, int world, void** out);
int r4d_rad_ar_ln_had_quant_i8(const RadArgs*, RadStream);
int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs*, RadStream);
int r4d_rad_ar_hc_write(const RadArgs*, RadStream);
/* ar_hc_write at four ranks, exact wire only, on the plain four-rank all-reduce's instance. */
int r4d_rad_ar_hc_wide_init(const RadParam*, int, int rank, int world, void** out);
int r4d_rad_ar_hc_write_4rank(const RadArgs*, RadStream);
int r4d_rad_ar_gather_hc_write(const RadArgs*, RadStream);
int64_t r4d_rad_ar_gather_hc_write_scratch(const RadArgs*);
+25 -2
View File
@@ -2158,8 +2158,8 @@ static const RadOpSchema kSchemas[] = {
"the two-rank all-reduce and the residual add, RMS norm and block-scaled fp8 "
"quantisation that always follow it, in one kernel. Both wires."),
SCHEMA("ar_hc_write", pArHcWrite, oArHcWrite,
"the two-rank all-reduce and the gated residual's write that follows it, in one "
"kernel. Both wires."),
"the all-reduce and the gated residual's write that follows it, in one kernel. Both "
"wires at two ranks, the exact one at four."),
SCHEMA("ar_gather_hc_write", pArGatherHcWrite, oArGatherHcWrite,
"moe_gather, then ar_hc_write, in one kernel: the routed experts' rows summed back to "
"token order (with the shared arm folded when present) ARE the all-reduce's message, "
@@ -2724,6 +2724,14 @@ static const RadConstraint cArHcWrite[] = {
C_EQ("world_size", 2),
C_P2P,
};
/* The same bounds at four ranks, where the handshake is the wide family's (r4d_ar_wide.h). Exact
* wire only, and that cannot be a constraint here: the exact op declares no `wire` and an absent
* key fails a constraint, so the init and the entry refuse wire 6 instead. */
static const RadConstraint cArHcWrite4[] = {
C_GE("M", 1), C_DIV("n", 8), C_GE("hc", 2), C_LE("hc", 8), C_IN("dtype", "bf16"),
C_EQ("world_size", 4),
C_P2P,
};
/* ar_hc_write's bounds and the gather's: top_k up to the 64 slots a row's table holds in LDS. */
static const RadConstraint cArGatherHcWrite[] = {
C_GE("M", 1), C_DIV("n", 8), C_GE("hc", 2), C_LE("hc", 8), C_IN("dtype", "bf16"),
@@ -4775,6 +4783,21 @@ static const RadKernelInfo kKernels[] = {
r4d_rad_ar_ln_init, r4d_rad_ar_fini, r4d_rad_ar_hc_write, nullptr, nullptr, nullptr,
r4d_shape_ar_hc_write },
{ "ar_hc_write_4rank", "ar_hc_write", "norm",
"the four-rank exact all-reduce and the gated residual's write that follows it, in one kernel: "
"h[s] += inj[s] * y over the reduced y",
"exactly 4 ranks, all peer-accessible; exact wire only; n a multiple of 8 and y unpitched, "
"because y is the message and a block pushes it as 16B words; the stream pitch a multiple of 8; "
"hc 2..8; the wide one-shot's handshake, slots and block partition, on the plain four-rank "
"all-reduce's instance; cudagraph-safe",
"bf16 throughout; the sum in fp32 in ascending rank order and rounded to bf16 exactly as the "
"four-rank exact all-reduce stores it, and the write in hc_write's arithmetic over that bf16 "
"value -- byte-identical to the pair it replaces",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cArHcWrite4), ROW(tAr), nullptr,
r4d_rad_ar_hc_wide_init, r4d_rad_ar_fini, r4d_rad_ar_hc_write_4rank, nullptr, nullptr, nullptr,
r4d_shape_ar_hc_write },
{ "ar_gather_hc_write", "ar_gather_hc_write", "norm",
"moe_gather folded in front of ar_hc_write: each block sums its words' tokens back from the "
"routed experts' rows, keeps the result as its message, and then reduces and writes exactly as "
+48 -7
View File
@@ -1855,10 +1855,11 @@ TEST(a_sizing_declare_leaves_the_step_state_alone_and_shrinks_only_rows) {
* bf16 expert, leaving an odd count of quantised ones, and the grouped GEMM takes its experts as
* two EQUAL tables by parity. The recipe protects experts in pairs for exactly that reason.
* - the FUSED all-reduces are world-2 kernels (`ar_wire_is_exact` and `ar_fused_ok` both return
* false off two, and `HyperConn` declares `ar_hc_write` only at world 2), so at four the chain
* is unfused: a standalone `all_reduce` where the collective happens. The contrast with world 2
* in the same test is the contract -- it fails loudly if a fused declare is ever gated on the
* wrong condition.
* false off two), with one exception: `ar_hc_write` also exists at four, on the exact wire only
* (no `wire` key, never the rotated form) and with no gather in front of it. Every block still
* declares its standalone `all_reduce`, which serves the steps the fused write declines. The
* contrast with world 2 in the same test is the contract -- it fails loudly if a fused declare
* is ever gated on the wrong condition.
*
* Every expected number is derived from the meta this file builds (n_head 24, n_head_kv 2, head_dim
* 256, num_experts 512), so the fixture and the assertions cannot drift apart. Where a number is
@@ -1998,11 +1999,16 @@ TEST(flashnext_tp4_replicates_kv_heads_and_splits_expert_widths) {
CHECK_EQ(gu->w.size(), (size_t)(2 * ne_all)); /* codes and scales, every expert once */
CHECK_EQ(count_op(b[r], "moe_gemm_q"), 0);
/* (4) THE UNFUSED CHAIN: a standalone collective per reduction, and none of the three
* fused world-2 forms. */
/* (4) A standalone collective per reduction, the exact four-rank ar_hc_write beside it,
* and neither of the other two fused forms. */
CHECK(count_op(b[r], "all_reduce") > 0);
CHECK_EQ(pint(find_op(b[r], "all_reduce"), "world_size"), world);
CHECK_EQ(count_op(b[r], "ar_hc_write"), 0);
CHECK(count_op(b[r], "ar_hc_write") > 0);
for (const RecOp& o : b[r].ops) {
if (o.op != "ar_hc_write") continue;
CHECK_EQ(pint(&o, "world_size"), world);
CHECK(param(&o, "wire") == nullptr); /* exact only */
}
CHECK_EQ(count_op(b[r], "ar_gather_hc_write"), 0);
CHECK_EQ(count_op(b[r], "ar_rmsnorm_quant_fp8"), 0);
}
@@ -2093,6 +2099,41 @@ TEST(flashnext_tp4_replicates_kv_heads_and_splits_expert_widths) {
}
}
/* THE FOUR-RANK FUSED WRITE'S STEPS: exactly those the standalone collective would have served
* as the exact one-shot -- under 1 MiB, and on the lossy wire also under its floor (or off its
* 128-element tile). The block in front and the write ask the same predicate. At two ranks it
* is ar_wire_is_exact unchanged, and ar_wire_is_exact itself stays false at four. */
{
rad::arch::Geom g4{};
g4.world = 4; g4.n_embd = 2560; g4.wire_exact = 1; g4.wire_min_bytes = 128 * 1024;
CHECK(rad::arch::ar_exact_write_takes(g4, 8)); /* a decode step, 40 KiB */
CHECK(rad::arch::ar_exact_write_takes(g4, 204)); /* 1020 KiB */
CHECK(!rad::arch::ar_exact_write_takes(g4, 205)); /* 1025 KiB: the two-shot's */
CHECK(!rad::arch::ar_wire_is_exact(g4, 8));
g4.wire_exact = 0;
CHECK(rad::arch::ar_exact_write_takes(g4, 25)); /* 125 KiB, under the floor */
CHECK(!rad::arch::ar_exact_write_takes(g4, 26)); /* 130 KiB, the ti8 wire's */
rad::arch::Geom g2 = g4;
g2.world = 2;
for (int64_t T : { 1, 8, 25, 26, 205, 4096 })
CHECK(rad::arch::ar_exact_write_takes(g2, T) == rad::arch::ar_wire_is_exact(g2, T));
}
/* ...and the lossy wire at four declares no rotated write and no gather form. */
{
RadBuilder b4;
RadBuildCtx c4 = build_ctx(0, world);
c4.max_tok = 2048; c4.max_seqs = 8; c4.max_spec = 3; c4.max_ctx = 200000;
c4.tp_wire_lossy = 1;
c4.tp_wire_min_bytes = 128 * 1024;
REQUIRE_EQ(qwen4exp_fp8::declare(&b4, &meta, &c4), RAD_OK);
CHECK(count_op(b4, "ar_hc_write") > 0);
for (const RecOp& o : b4.ops) {
if (o.op != "ar_hc_write") continue;
CHECK(param(&o, "wire") == nullptr);
}
CHECK_EQ(count_op(b4, "ar_gather_hc_write"), 0);
}
/* A WORLD THAT CANNOT REPLICATE -- not a multiple of the KV head count -- is refused by name,
* not rounded into a half head. */
{