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

+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 "