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