Archived
four-rank ar_gather_hc_write: the MoE gather in front of the exact one-shot write
At --tp 4 each MoE connection ran moe_gather and the fused write as two launches, three or four with the inverse table. ar_gather_hc_write_4rank runs r4d_ar_gather_word as the prologue of ar_hc_write_4rank, with the two-rank slot table and grid rule. The arithmetic is moe_gather's plus ar_hc_write_4rank's, so the bytes are unchanged. One and two ranks unchanged.
This commit is contained in:
1 parent
c36e6fa2b3
commit
2f56e3c477
12 files changed
+415
-36
No files matched your search
@@ -350,6 +350,23 @@ void r4d_ar_hc_write_4rank(long peer_scratch0, long peer_scratch1, long peer_scr
|
||||
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);
|
||||
// r4d_ar_gather_hc_write at four ranks: r4d_ar_hc_write_4rank with the MoE gather as its prologue
|
||||
// -- byte-identical to moe_gather, then the 4-rank exact all-reduce, then hc_write. The gather's
|
||||
// operands, `inv` and the grid rule are r4d_ar_gather_hc_write's; the buffers, peer order, flag
|
||||
// values and slots are r4d_ar_oneshot_4rank_exact's. Exact wire only.
|
||||
void r4d_ar_gather_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 ye, long ew, long sorted,
|
||||
long sh, long sg, long ye_ld, long ew_ld, long sh_ld,
|
||||
long sg_ld, long top_k, long sig, long rank, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long inv);
|
||||
// The inverse slot table both widths' gather forms read at a prefill-sized call: inv[f] = the
|
||||
// sorted row holding slot f, -1 where none does, over T = M*top_k int32. One block, on `stream`.
|
||||
// Returns 0 or a negative / HIP error code.
|
||||
int r4d_ar_gather_inv(long inv, long sorted, long T, long stream);
|
||||
void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers);
|
||||
|
||||
// ---- all-reduce: two-shot, 4 or 8 ranks over P2P -------------------------------------------
|
||||
|
||||
+79
-6
@@ -1105,12 +1105,18 @@ enum { AH_Y = 0, AH_INJ, AH_H };
|
||||
* ar_hc_write's; wire 6 builds its slot table in LDS at every size and takes no scratch. */
|
||||
enum { AG_YE = 0, AG_EW, AG_SORTED, AG_SH, AG_SG, AG_Y, AG_INJ, AG_H };
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_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_gather_hc_write states at every width: moe_gather's five and
|
||||
* ar_hc_write's three, and the inverse table's scratch. */
|
||||
struct GatherHcWriteArgs {
|
||||
const RadTensor *ye = nullptr, *ew = nullptr, *so = nullptr, *sh = nullptr, *sg = nullptr;
|
||||
const RadTensor *y = nullptr, *inj = nullptr, *h = nullptr;
|
||||
long long M = 0, n = 0, hc = 0, top_k = 0, irow = 0, hrow = 0;
|
||||
long long ye_ld = 0, ew_ld = 0, sh_ld = 0, sg_ld = 0;
|
||||
int sig = 0, wire = 0;
|
||||
bool has_inv = false;
|
||||
};
|
||||
|
||||
static int ar_gather_hc_write_args(const RadArgs* a, const ArInst* in, GatherHcWriteArgs* o) {
|
||||
const RadTensor* ye = r4d_opt(a, AG_YE);
|
||||
const RadTensor* ew = r4d_opt(a, AG_EW);
|
||||
const RadTensor* so = r4d_opt(a, AG_SORTED);
|
||||
@@ -1159,7 +1165,37 @@ extern "C" int r4d_rad_ar_gather_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;
|
||||
const bool has_inv = a->scratch && a->scratch_bytes >= T * (long long)sizeof(int);
|
||||
|
||||
o->ye = ye; o->ew = ew; o->so = so; o->sh = sh; o->sg = sg;
|
||||
o->y = y; o->inj = inj; o->h = h;
|
||||
o->M = M; o->n = n; o->hc = hc; o->top_k = top_k; o->irow = irow; o->hrow = hrow;
|
||||
o->ye_ld = ye_ld; o->ew_ld = ew_ld; o->sh_ld = sh_ld; o->sg_ld = sg_ld;
|
||||
o->sig = sig; o->wire = wire;
|
||||
o->has_inv = a->scratch && a->scratch_bytes >= T * (long long)sizeof(int);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_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;
|
||||
|
||||
GatherHcWriteArgs g;
|
||||
const int arc = ar_gather_hc_write_args(a, in, &g);
|
||||
if (arc != RAD_OK) return arc;
|
||||
const RadTensor* const ye = g.ye;
|
||||
const RadTensor* const ew = g.ew;
|
||||
const RadTensor* const so = g.so;
|
||||
const RadTensor* const sh = g.sh;
|
||||
const RadTensor* const sg = g.sg;
|
||||
const RadTensor* const y = g.y;
|
||||
const RadTensor* const inj = g.inj;
|
||||
const RadTensor* const h = g.h;
|
||||
const long long M = g.M, n = g.n, hc = g.hc, top_k = g.top_k, irow = g.irow, hrow = g.hrow;
|
||||
const long long ye_ld = g.ye_ld, ew_ld = g.ew_ld, sh_ld = g.sh_ld, sg_ld = g.sg_ld;
|
||||
const int sig = g.sig, wire = g.wire;
|
||||
const bool has_inv = g.has_inv;
|
||||
|
||||
R4dArCfg cfgv;
|
||||
r4d_cfg_ar(a, &cfgv);
|
||||
@@ -1190,6 +1226,43 @@ extern "C" int r4d_rad_ar_gather_hc_write(const RadArgs* a, RadStream s) {
|
||||
return rc == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
/* AT FOUR RANKS, EXACT WIRE ONLY: r4d_rad_ar_hc_write_4rank with the gather in front, on the same
|
||||
* instance (r4d_rad_ar_hc_wide_init). The architecture issues it only on a step whose write takes
|
||||
* the exact one-shot (ar_exact_write_takes, arch/common/rad_fp8.h) and whose gather is one pass's.
|
||||
* The block count is that write's rule, widened by the launcher until a block's slot table fits in
|
||||
* LDS -- or, where that would take more blocks than the rule asks for and the scratch is there,
|
||||
* kept and served from the inverse table, as at two ranks. */
|
||||
extern "C" int r4d_rad_ar_gather_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;
|
||||
|
||||
GatherHcWriteArgs g;
|
||||
const int arc = ar_gather_hc_write_args(a, in, &g);
|
||||
if (arc != RAD_OK) return arc;
|
||||
if (g.wire != 0) return RAD_E_UNSUPPORTED;
|
||||
|
||||
R4dArCfg cfgv;
|
||||
r4d_cfg_ar(a, &cfgv);
|
||||
const long words16 = (long)(g.M * g.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_gather_hc_write_4rank",
|
||||
r4d_ar_gather_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(g.y), r4d_addr(g.inj), r4d_addr(g.h), (long)g.M, (long)g.n, (long)g.hc,
|
||||
(long)g.irow, (long)g.hrow, r4d_addr(g.ye), r4d_addr(g.ew), r4d_addr(g.so),
|
||||
g.sh ? r4d_addr(g.sh) : 0L, g.sh ? r4d_addr(g.sg) : 0L, (long)g.ye_ld,
|
||||
(long)g.ew_ld, (long)g.sh_ld, (long)g.sg_ld, (long)g.top_k, (long)g.sig,
|
||||
(long)in->rank, (long)(uintptr_t)s, nblocks, cfgv.nthreads, cfgv.drain, cfgv.acq,
|
||||
cfgv.pub, g.has_inv ? (long)(uintptr_t)a->scratch : 0L));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* The inverse slot table the exact wire's prefill form reads: one int a (token, slot) at the
|
||||
* band's M. The rotated wire's table is in LDS. */
|
||||
extern "C" int64_t r4d_rad_ar_gather_hc_write_scratch(const RadArgs* a) {
|
||||
|
||||
@@ -474,6 +474,15 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags,
|
||||
return (int)hipGetLastError();
|
||||
}
|
||||
|
||||
/* The inverse slot table alone, on `stream`, for the wide fused gather (r4d_ar_oneshot_Nrank_exact.hip):
|
||||
* the same kernel and the same launch as above, so both widths read one table. */
|
||||
int r4d_ar_gather_inv(long inv, long sorted, long T, long stream_i) {
|
||||
if (!inv || !sorted || T < 1) return -1;
|
||||
r4d_ar_gather_inv_kernel<<<1, 1024, 0, (hipStream_t)stream_i>>>((int*)inv, (const int*)sorted,
|
||||
(int)T);
|
||||
return (int)hipGetLastError();
|
||||
}
|
||||
|
||||
void r4d_ar_ipc_free(long p) { if (p) (void)hipFree((void*)p); }
|
||||
void r4d_ar_ipc_memzero(long p, long size) { if (p) HIPOK(hipMemset((void*)p, 0, size)); }
|
||||
|
||||
|
||||
@@ -10,12 +10,13 @@
|
||||
// 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).
|
||||
// width, exact wire only), and r4d_ar_gather_hc_write_4rank: that with moe_gather as its prologue
|
||||
// (ar_gather_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 "r4d_ar_hc.h" // the gather and hc write words, shared with the two-rank fused forms
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
@@ -181,6 +182,98 @@ __global__ void r4d_ar_hc_write_wide_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// THE SAME WRITE WITH THE MoE GATHER AS ITS PROLOGUE, bf16 only: the four-rank form of the
|
||||
// two-rank r4d_ar_gather_hc_write_kernel. `y` is not read but produced -- a block's words are
|
||||
// summed back to token order from the routed experts' rows by r4d_ar_gather_word, kept as this
|
||||
// rank's message and pushed to every peer, exactly as if moe_gather had written y and the kernel
|
||||
// above had read it back. From the handshake on it IS the kernel above: the same seq take, the
|
||||
// same value 2*s, the same (parity, source) slots, the same ar_vsum in ascending rank order over
|
||||
// the same sources -- this rank's own word from y, which the same thread stored before the
|
||||
// handshake -- and the same r4d_ar_hc_write_word over the stored bf16 word. So the bytes are those
|
||||
// of moe_gather, the four-rank exact all-reduce and hc_write.
|
||||
//
|
||||
// The slot table is the two-rank kernel's: without INV a block's rows (at most R4D_ARG_ROWS, which
|
||||
// the launcher sizes the grid for) find their slots once, in LDS, by one pass over the sorted
|
||||
// list; with INV they read the inverse table r4d_ar_gather_inv built in front.
|
||||
template <int WS, bool INV>
|
||||
__global__ void r4d_ar_gather_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,
|
||||
R4dArGather G, const int* __restrict__ inv) {
|
||||
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;
|
||||
__shared__ int s_i[R4D_ARG_ROWS * R4D_ARG_SLOTS];
|
||||
__shared__ float s_w[R4D_ARG_ROWS * R4D_ARG_SLOTS];
|
||||
__shared__ float s_g[R4D_ARG_ROWS];
|
||||
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;
|
||||
const int wpr = n >> 3; // 16B words a row
|
||||
const int row_lo = start / wpr;
|
||||
const int nrow = end > start ? (end - 1) / wpr - row_lo + 1 : 0;
|
||||
const int K = G.top_k;
|
||||
|
||||
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;
|
||||
|
||||
/* The slot table. A slot no sorted row claims reads -1, so the clear and the scan are two
|
||||
* phases with a barrier between them, as in moe_gather. */
|
||||
if (!INV) {
|
||||
for (int x = tid; x < nrow * K; x += nt) s_i[x] = -1;
|
||||
__syncthreads();
|
||||
{
|
||||
const int lo = row_lo * K, hi = (row_lo + nrow) * K;
|
||||
for (int i = tid; i < G.T; i += nt) {
|
||||
const int f = G.sorted[i];
|
||||
if (f >= lo && f < hi) s_i[f - lo] = i;
|
||||
}
|
||||
}
|
||||
for (int x = tid; x < nrow * K; x += nt)
|
||||
s_w[x] = bf2f(G.ew[(long)(row_lo + x / K) * G.ew_ld + x % K]);
|
||||
if (G.sh)
|
||||
for (int r = tid; r < nrow; r += nt) {
|
||||
const float v0 = bf2f(G.sg[(long)(row_lo + r) * G.sg_ld]);
|
||||
s_g[r] = G.sig ? (1.0f / (1.0f + __expf(-v0))) : v0;
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
|
||||
/* The gather, one word a thread a pass, kept and pushed to every peer. */
|
||||
for (int k = start + tid; k < end; k += nt) {
|
||||
const int row = k / wpr, i0 = (k - row * wpr) * 8;
|
||||
const int4 out = r4d_ar_gather_word<INV>(G, inv, s_i, s_w, s_g, row, row - row_lo, i0);
|
||||
y4[k] = out;
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) ps[p][k] = out;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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,
|
||||
@@ -354,6 +447,126 @@ void r4d_ar_hc_write_4rank(long peer_scratch0, long peer_scratch1, long peer_scr
|
||||
pub);
|
||||
}
|
||||
|
||||
// ar_hc_write_wide_launch's checks plus the gather's, and the two-rank gather's grid rule: at least
|
||||
// wide enough that no block spans more than R4D_ARG_ROWS rows, or -- where that would take more
|
||||
// blocks than the reduction asked for and `inv` is given -- the reduction's count over the inverse
|
||||
// table. Every rank computes the same count and the same choice from the same M, n and scratch.
|
||||
template <int WS>
|
||||
static void ar_gather_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 ye, long ew, long sorted, long sh, long sg, long ye_ld,
|
||||
long ew_ld, long sh_ld, long sg_ld, long top_k, long sig, long rank, long stream_i,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub, long inv) {
|
||||
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_gather_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_gather_hc_write: n must be a positive multiple of 8");
|
||||
if (hc < 1 || hrow % 8)
|
||||
throw std::runtime_error("ar_gather_hc_write: the streams must be whole 16B words");
|
||||
if (top_k < 1 || top_k > R4D_ARG_SLOTS)
|
||||
throw std::runtime_error("ar_gather_hc_write: top_k " + std::to_string(top_k) +
|
||||
" outside 1.." + std::to_string(R4D_ARG_SLOTS));
|
||||
if (!ye || !ew || !sorted || ye_ld % 8 || (sh && sh_ld < n) || (sh != 0) != (sg != 0))
|
||||
throw std::runtime_error("ar_gather_hc_write: the gather's operands are not 16B rows");
|
||||
|
||||
const long n16l = M * n / 8; // bf16: eight elements a 16B word
|
||||
if (n16l > (long)slot_stride16)
|
||||
throw std::runtime_error("ar_gather_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;
|
||||
const int wpr = (int)(n / 8);
|
||||
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = R4D_AR_NB_DESIGN;
|
||||
if (nb > R4D_AR_WIDE_MAX_BLOCKS) nb = R4D_AR_WIDE_MAX_BLOCKS;
|
||||
/* A block of w words starting anywhere touches at most (w - 1) / wpr + 2 rows. */
|
||||
const int wmax = (R4D_ARG_ROWS - 2) * wpr + 1;
|
||||
const int nb_min = (n16 + wmax - 1) / wmax;
|
||||
const bool by_inv = inv != 0 && nb_min > nb;
|
||||
if (!by_inv && nb < nb_min) nb = nb_min;
|
||||
if (nb > n16) nb = n16;
|
||||
if (nb > R4D_AR_WIDE_MAX_BLOCKS)
|
||||
throw std::runtime_error("ar_gather_hc_write: the slot table needs " + std::to_string(nb) +
|
||||
" blocks, above the " + std::to_string(R4D_AR_WIDE_MAX_BLOCKS) +
|
||||
" the flags hold, and no inverse table was given");
|
||||
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_gather_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_gather_hc_write", "my_scratch");
|
||||
r4d_ar_check_extent(my_flags, flag_bytes, "ar_gather_hc_write", "my_flags");
|
||||
r4d_ar_check_extent(seq_ctrs, 2L * R4D_AR_WIDE_MAX_BLOCKS * (long)sizeof(unsigned int),
|
||||
"ar_gather_hc_write", "seq_ctrs");
|
||||
for (int i = 0; i < NPEERS; ++i) {
|
||||
r4d_ar_check_extent(peer_scratch[i], slot_bytes, "ar_gather_hc_write", "peer scratch");
|
||||
r4d_ar_check_extent(peer_flags[i], flag_bytes, "ar_gather_hc_write", "peer flags");
|
||||
}
|
||||
|
||||
R4dArGather G;
|
||||
G.ye = (const unsigned short*)ye;
|
||||
G.ew = (const unsigned short*)ew;
|
||||
G.sorted = (const int*)sorted;
|
||||
G.sh = (const unsigned short*)sh;
|
||||
G.sg = (const unsigned short*)sg;
|
||||
G.ye_ld = ye_ld; G.ew_ld = ew_ld; G.sh_ld = sh_ld; G.sg_ld = sg_ld;
|
||||
G.top_k = (int)top_k; G.T = (int)(M * top_k); G.sig = (int)sig;
|
||||
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
if (by_inv) {
|
||||
const int irc = r4d_ar_gather_inv(inv, sorted, (long)G.T, stream_i);
|
||||
if (irc != 0)
|
||||
throw std::runtime_error("ar_gather_hc_write: the inverse table's launch failed (" +
|
||||
std::to_string(irc) + ")");
|
||||
r4d_ar_gather_hc_write_wide_kernel<WS, true><<<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, G,
|
||||
(const int*)inv);
|
||||
} else {
|
||||
r4d_ar_gather_hc_write_wide_kernel<WS, false><<<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, G,
|
||||
nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void r4d_ar_gather_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 ye, long ew, long sorted,
|
||||
long sh, long sg, long ye_ld, long ew_ld, long sh_ld,
|
||||
long sg_ld, long top_k, long sig, long rank, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long inv) {
|
||||
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
|
||||
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
|
||||
ar_gather_hc_write_wide_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, y,
|
||||
inj, h, M, n, hc, irow, hrow, ye, ew, sorted, sh, sg, ye_ld,
|
||||
ew_ld, sh_ld, sg_ld, top_k, sig, rank, stream, nblocks,
|
||||
nthreads, drain, acq, pub, inv);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
+4
-2
@@ -704,7 +704,8 @@ extern "C" int r4d_fuse_attn_paged_gate_quant(const RadParam* p, int n_p, uint64
|
||||
|
||||
/* ==========================================================================================
|
||||
* ar_gather_hc_write -- moe_gather, then ar_hc_write.
|
||||
* (kernel: libr4d/r4d_ar_oneshot_2rank_exact.hip, r4d_ar_gather_hc_write_kernel; shim: r4d_ar_entry.hip)
|
||||
* (kernel: libr4d/r4d_ar_oneshot_2rank_exact.hip, r4d_ar_gather_hc_write_kernel, and at four ranks
|
||||
* libr4d/r4d_ar_oneshot_Nrank_exact.hip, r4d_ar_gather_hc_write_wide_kernel; shim: r4d_ar_entry.hip)
|
||||
*
|
||||
* Operands: [0] y_expert [1] expert_w [2] sorted_tok [3] shared? [4] shared_gate? [5] y (inout)
|
||||
* [6] inj [7] h (inout)
|
||||
@@ -712,7 +713,8 @@ extern "C" int r4d_fuse_attn_paged_gate_quant(const RadParam* p, int n_p, uint64
|
||||
* Two declared ops and nothing in between: the gather writes `y`, which is the reduction's message
|
||||
* and is then reduced in place and written into the streams. The shared-arm pair is optional in
|
||||
* both ops at the same positions, so its absence propagates through the wiring. `wire` is the
|
||||
* reducing write's, and it goes to that step unchanged.
|
||||
* reducing write's, and it goes to that step unchanged; so does `world_size`, which at four selects
|
||||
* ar_hc_write_4rank, the reduction and write the four-rank row runs.
|
||||
*/
|
||||
extern "C" int r4d_fuse_ar_gather_hc_write(const RadParam* p, int n_p, uint64_t present, int step,
|
||||
RadFuseStep* out) {
|
||||
|
||||
@@ -188,6 +188,8 @@ int r4d_rad_ar_hc_wide_init(const RadParam*, int, int rank, int world, void** o
|
||||
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*);
|
||||
/* ar_gather_hc_write at four ranks, exact wire only, on ar_hc_write_4rank's instance. */
|
||||
int r4d_rad_ar_gather_hc_write_4rank(const RadArgs*, RadStream);
|
||||
|
||||
/* -- GEMM (r4d_gemm_*.hip) -- */
|
||||
int r4d_rad_gemm_bf16_m16 (const RadArgs*, RadStream);
|
||||
|
||||
+29
-1
@@ -2163,7 +2163,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
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, "
|
||||
"and the gated residual's write follows the reduction. Both wires."),
|
||||
"and the gated residual's write follows the reduction. Both wires at two ranks, the "
|
||||
"exact one at four."),
|
||||
};
|
||||
|
||||
/* ================================================================================ constraints
|
||||
@@ -2738,6 +2739,13 @@ static const RadConstraint cArGatherHcWrite[] = {
|
||||
C_EQ("world_size", 2), C_GE("top_k", 1), C_LE("top_k", 64),
|
||||
C_P2P,
|
||||
};
|
||||
/* The same at four ranks, exact wire only for cArHcWrite4's reason: the init and the entry refuse a
|
||||
* declared `wire` 6, since the exact op declares no `wire` and an absent key fails a constraint. */
|
||||
static const RadConstraint cArGatherHcWrite4[] = {
|
||||
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_GE("top_k", 1), C_LE("top_k", 64),
|
||||
C_P2P,
|
||||
};
|
||||
static const RadConstraint cGatedQuantFp8[] = {
|
||||
C_DIV("n", 4), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"),
|
||||
};
|
||||
@@ -4815,6 +4823,26 @@ static const RadKernelInfo kKernels[] = {
|
||||
r4d_rad_ar_gather_hc_write_scratch, nullptr, nullptr,
|
||||
r4d_shape_ar_gather_hc_write, nullptr, r4d_fuse_ar_gather_hc_write },
|
||||
|
||||
{ "ar_gather_hc_write_4rank", "ar_gather_hc_write", "norm",
|
||||
"moe_gather folded in front of the four-rank ar_hc_write: each block sums its words' tokens "
|
||||
"back from the routed experts' rows, keeps the result as its message and pushes it, and then "
|
||||
"reduces and writes exactly as ar_hc_write_4rank does",
|
||||
"exactly 4 ranks, all peer-accessible; exact wire only; n a multiple of 8, y unpitched and "
|
||||
"y_expert's pitch a multiple of 8, because both move as 16B words; top_k <= 64; the stream "
|
||||
"pitch a multiple of 8; hc 2..8; the wide one-shot's handshake and slots on the plain four-rank "
|
||||
"all-reduce's instance; the grid widened until a block's slot table fits in LDS, or the "
|
||||
"inverse table in the scratch where that would take more blocks than the reduction asks for; "
|
||||
"cudagraph-safe",
|
||||
"bf16 throughout; the gather's sums and narrows are moe_gather's (round-to-nearest-even, the "
|
||||
"shared arm's product rounded before the add), 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 three kernels it replaces",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cArGatherHcWrite4), ROW(tAr), nullptr,
|
||||
r4d_rad_ar_hc_wide_init, r4d_rad_ar_fini, r4d_rad_ar_gather_hc_write_4rank,
|
||||
r4d_rad_ar_gather_hc_write_scratch, nullptr, nullptr,
|
||||
r4d_shape_ar_gather_hc_write, nullptr, r4d_fuse_ar_gather_hc_write },
|
||||
|
||||
{ "qk_norm_rope_gate", "qk_norm_rope_gate", "attn",
|
||||
"the gated-attention QKV preamble in one pass: split [q|gate] per head, QK RMS norm over the "
|
||||
"head, partial NeoX RoPE and the gate copy",
|
||||
|
||||
Reference in new issue
Block a user