Archived
arch, data/recipes, libr4d: the 31B's prefill overlap, q|k|v as one linear, RedHatAI's DFlash2, its fc split
The 31B's prefill steps of 1024 rows or more run as two slices with each slice's all-reduces beside the other's compute; q, k and v as one linear where the container holds them so; the 31B serves RedHatAI's DFlash2 speculator; DFlash2's fc projection is split over the ranks, bit for bit. Squashed from: - arch/gemma4_bf16: a prefill step in two slices, each slice's reductions on the second lane - arch/gemma4_bf16: q, k and v as one linear where the container holds them so - data/recipes: the 31B -df recipes make q, k and v one linear (blk.*.attn_qkv) - arch/gemma4: the 31B serves RedHatAI's DFlash2 speculator (gemma-4-31B-it-speculator.dflash2) - libr4d: gemm_nt_q takes split_n, and all_gather lands rows at a wider output pitch - arch/common: DFlash2's fc split over the ranks as windows of block-aligned rows, bit for bit Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bdb37d5d16
commit
04e5480387
21 files changed
+1805
-199
No files matched your search
+4
-2
@@ -309,7 +309,8 @@ void r4d_ar_wht6_dims(int* group, int* bits, int* chunk_elems); // rotated-6-bit
|
||||
void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
long seq_ctrs, long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream, long nblocks,
|
||||
long nthreads, long drain, long acq);
|
||||
long nthreads, long drain, long acq, long dpitch,
|
||||
long spitch);
|
||||
|
||||
// ---- all-reduce: one-shot, push, 4 or 8 ranks over P2P -------------------------------------
|
||||
// The same algorithm at width (r4d_ar_oneshot_Nrank_exact.hip). Peer arguments are in ascending
|
||||
@@ -343,7 +344,8 @@ void r4d_ar_gather_3rank(long peer_scratch0, long peer_scratch1, long peer_flags
|
||||
long peer_flags1, long my_scratch, long my_flags, long seq_ctrs,
|
||||
long slot_stride16, long inp, long out, long n_elem, long dtype,
|
||||
long rank, long row_elems, long stream, long nblocks, long nthreads,
|
||||
long drain, long acq, long pub);
|
||||
long drain, long acq, long pub, long dpitch,
|
||||
long spitch);
|
||||
void r4d_ar_oneshot_8rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_scratch3, long peer_scratch4, long peer_scratch5,
|
||||
long peer_scratch6, long peer_flags0, long peer_flags1,
|
||||
|
||||
+44
-17
@@ -752,7 +752,7 @@ extern "C" void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long
|
||||
long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long gate, long gate_ctr);
|
||||
long gate, long gate_ctr, long dpitch, long spitch);
|
||||
extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_scratch3, long peer_scratch4, long peer_scratch5,
|
||||
long peer_scratch6, long peer_flags0, long peer_flags1,
|
||||
@@ -761,11 +761,14 @@ extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp,
|
||||
long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub);
|
||||
long pub, long dpitch, long spitch);
|
||||
|
||||
/* Not ar_setup: an all-gather's output is world_size times its input. */
|
||||
/* Not ar_setup: an all-gather's output is world_size times its input. With `row`, `y` may also be
|
||||
* the leading world_size * row columns of a wider buffer and `x` a row-wide window of one
|
||||
* (docs/OPS.md); `*dpitch_out` and `*spitch_out` are then their row pitches in elements, else 0. */
|
||||
static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in, ArCall* c,
|
||||
long long* row_out) {
|
||||
long long* row_out, long long* dpitch_out,
|
||||
long long* spitch_out) {
|
||||
const ArInst* in = (const ArInst*)(a ? a->instance : nullptr);
|
||||
if (!in) return RAD_E_STATE; /* init did not run: a core sequencing bug */
|
||||
if (in->world != ws) return RAD_E_SHAPE; /* the peer bundle is compiled for this width */
|
||||
@@ -773,7 +776,6 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in,
|
||||
const RadTensor* x = r4d_opt(a, AR_X);
|
||||
const RadTensor* y = r4d_opt(a, AR_Y);
|
||||
if (!x || !y) return RAD_E_INVAL; /* the gather has no in-place form */
|
||||
if (!r4d_contig(x) || !r4d_contig(y)) return RAD_E_STRIDE;
|
||||
if (y->data == x->data) return RAD_E_INVAL;
|
||||
|
||||
const long n = (long)r4d_numel(x);
|
||||
@@ -783,6 +785,27 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in,
|
||||
|
||||
const long long row = r4d_pgeti_or(a->p, a->n_p, "row", 0);
|
||||
if (row < 0 || (row > 0 && (n % row || (row * in->esize) % 16))) return RAD_E_SHAPE;
|
||||
/* A row-interleaved gather's [rows][world * row] may sit in the leading columns of a wider
|
||||
* buffer, the pitch in whole 16-byte words: rows land at that pitch, the rest of each row is
|
||||
* left alone. */
|
||||
long long dpitch = 0;
|
||||
if (!r4d_contig(y)) {
|
||||
const long long p = r4d_row_pitch(y);
|
||||
if (row <= 0 || y->rank != 2 || p <= 0 || y->shape[1] != (int64_t)row * in->world ||
|
||||
(p * in->esize) % 16)
|
||||
return RAD_E_STRIDE;
|
||||
dpitch = p;
|
||||
}
|
||||
/* Likewise x may be [rows][row] read at a wider pitch: a rank's window of a row it computed
|
||||
* wider (a split linear's block-aligned superset, arch/common/rad_block_dflash2.h). */
|
||||
long long spitch = 0;
|
||||
if (!r4d_contig(x)) {
|
||||
const long long p = r4d_row_pitch(x);
|
||||
if (row <= 0 || x->rank != 2 || p <= 0 || x->shape[1] != (int64_t)row ||
|
||||
(p * in->esize) % 16)
|
||||
return RAD_E_STRIDE;
|
||||
spitch = p;
|
||||
}
|
||||
|
||||
if (((long long)n * in->esize) % 16) return RAD_E_ALIGN;
|
||||
if (!r4d_aligned(x->data, 16) || !r4d_aligned(y->data, 16)) return RAD_E_ALIGN;
|
||||
@@ -804,43 +827,46 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in,
|
||||
c->acq = cfgv.acq;
|
||||
c->pub = cfgv.pub;
|
||||
*row_out = row;
|
||||
*dpitch_out = dpitch;
|
||||
*spitch_out = spitch;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_2rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 2, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 2, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_2rank",
|
||||
r4d_ar_gather_2rank(in->peer_scratch[0], in->my_scratch, in->peer_flags[0], in->my_flags,
|
||||
in->my_seq, in->slot16, c.inp, c.out, c.numel, in->dtype, in->rank,
|
||||
(long)row, (long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain,
|
||||
c.acq));
|
||||
c.acq, (long)dpitch, (long)spitch));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_3rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 3, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 3, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_3rank",
|
||||
r4d_ar_gather_3rank(in->peer_scratch[0], in->peer_scratch[1],
|
||||
in->peer_flags[0], in->peer_flags[1],
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->dtype, in->rank, (long)row,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub,
|
||||
(long)dpitch, (long)spitch));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_4rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 4, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 4, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
/* One handshake, placement after it local: the release is the one-shot's. */
|
||||
const long gate = link_gate(a), early = in->host ? 0L : gate;
|
||||
@@ -850,15 +876,15 @@ extern "C" int r4d_rad_ar_gather_4rank(const RadArgs* a, RadStream s) {
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->dtype, in->rank, (long)row,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub,
|
||||
early, early ? in->gate_ctr : 0L));
|
||||
early, early ? in->gate_ctr : 0L, (long)dpitch, (long)spitch));
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_8rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 8, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 8, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_8rank",
|
||||
r4d_ar_gather_8rank(in->peer_scratch[0], in->peer_scratch[1], in->peer_scratch[2],
|
||||
@@ -869,7 +895,8 @@ extern "C" int r4d_rad_ar_gather_8rank(const RadArgs* a, RadStream s) {
|
||||
in->peer_flags[6],
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->dtype, in->rank, (long)row,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub,
|
||||
(long)dpitch, (long)spitch));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,11 +38,20 @@ __device__ __forceinline__ unsigned int r4d_g_load_acq(const unsigned int* p) {
|
||||
//
|
||||
// The vocab-parallel sampler's merge reads one row of gathered candidates per position at a single
|
||||
// row stride, which the concatenated form cannot express. Interleaving costs one division per word
|
||||
// and saves a transpose launch.
|
||||
__device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who) {
|
||||
// and saves a transpose launch. `dp16` is the output's row pitch in words: 2 * row16 for a tight
|
||||
// [rows][2][row16], more when the gathered rows are the leading columns of a wider buffer.
|
||||
__device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who, int dp16) {
|
||||
if (row16 <= 0 || row16 >= n16) return who * n16 + k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * (2 * row16) + who * row16 + c;
|
||||
return m * dp16 + who * row16 + c;
|
||||
}
|
||||
|
||||
// Where word k of this rank's contribution is read: `sp16` is the input's row pitch in words,
|
||||
// row16 when its rows are tight, more when they are a window of a wider row.
|
||||
__device__ __forceinline__ int r4d_g_src(int k, int row16, int n16, int sp16) {
|
||||
if (row16 <= 0 || row16 >= n16 || sp16 == row16) return k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * sp16 + c;
|
||||
}
|
||||
|
||||
// Block b owns words [start,end) of the 16B-word space and drives its own flag handshake; no
|
||||
@@ -50,7 +59,8 @@ __device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who) {
|
||||
__global__ void r4d_ar_gather_2rank_kernel(
|
||||
const int4* __restrict__ in4, int4* peer_base, const int4* my_base, int4* __restrict__ out4,
|
||||
unsigned int* peer_flags, unsigned int* my_flags, unsigned int* seq_ctrs,
|
||||
int n16, int slot_stride16, int rank, int row16, int drain, int acq, unsigned int nbw) {
|
||||
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
|
||||
unsigned int nbw) {
|
||||
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, nbw, s_seq, s_slot); // device-resident, replay-safe
|
||||
@@ -68,14 +78,20 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
{
|
||||
int k = start + tid;
|
||||
for (; k + 3 * nt < end; k += 4 * nt) {
|
||||
int4 v0 = in4[k], v1 = in4[k + nt], v2 = in4[k + 2 * nt], v3 = in4[k + 3 * nt];
|
||||
int4 v0 = in4[r4d_g_src(k, row16, n16, sp16)], v1 = in4[r4d_g_src(k + nt, row16, n16, sp16)],
|
||||
v2 = in4[r4d_g_src(k + 2 * nt, row16, n16, sp16)],
|
||||
v3 = in4[r4d_g_src(k + 3 * nt, row16, n16, sp16)];
|
||||
ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3;
|
||||
out4[r4d_g_dst(k, row16, n16, rank)] = v0;
|
||||
out4[r4d_g_dst(k + nt, row16, n16, rank)] = v1;
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, rank)] = v2;
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, rank)] = v3;
|
||||
out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v0;
|
||||
out4[r4d_g_dst(k + nt, row16, n16, rank, dp16)] = v1;
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, rank, dp16)] = v2;
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, rank, dp16)] = v3;
|
||||
}
|
||||
for (; k < end; k += nt) {
|
||||
int4 v = in4[r4d_g_src(k, row16, n16, sp16)];
|
||||
ps[k] = v;
|
||||
out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v;
|
||||
}
|
||||
for (; k < end; k += nt) { int4 v = in4[k]; ps[k] = v; out4[r4d_g_dst(k, row16, n16, rank)] = v; }
|
||||
}
|
||||
// Drain modes, as in the all-reduce: order the pushes before the release flag so the peer cannot
|
||||
// see the flag before the data.
|
||||
@@ -100,19 +116,19 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
const int who = 1 - rank;
|
||||
int k = start + tid;
|
||||
for (; k + 3 * nt < end; k += 4 * nt) {
|
||||
out4[r4d_g_dst(k, row16, n16, who)] = ms[k];
|
||||
out4[r4d_g_dst(k + nt, row16, n16, who)] = ms[k + nt];
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, who)] = ms[k + 2 * nt];
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, who)] = ms[k + 3 * nt];
|
||||
out4[r4d_g_dst(k, row16, n16, who, dp16)] = ms[k];
|
||||
out4[r4d_g_dst(k + nt, row16, n16, who, dp16)] = ms[k + nt];
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, who, dp16)] = ms[k + 2 * nt];
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, who, dp16)] = ms[k + 3 * nt];
|
||||
}
|
||||
for (; k < end; k += nt) out4[r4d_g_dst(k, row16, n16, who)] = ms[k];
|
||||
for (; k < end; k += nt) out4[r4d_g_dst(k, row16, n16, who, dp16)] = ms[k];
|
||||
}
|
||||
}
|
||||
|
||||
void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
long seq_ctrs, long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream_i, long nblocks,
|
||||
long nthreads, long drain, long acq) {
|
||||
long nthreads, long drain, long acq, long dpitch, long spitch) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
const int esize = (dtype == 2) ? 4 : 2;
|
||||
const long nbytes = (long)n_elem * esize;
|
||||
@@ -126,6 +142,19 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
if (row_elems > 0 && (rowb % 16 != 0 || nbytes % rowb != 0))
|
||||
throw std::runtime_error("all_gather: row is not a whole number of 16B words");
|
||||
const int row16 = row_elems > 0 ? (int)(rowb / 16) : 0;
|
||||
// The output's row pitch in elements, 0 for a tight [rows][2][row]: whole words, at least the
|
||||
// two ranks' row.
|
||||
const long dpb = (long)dpitch * esize;
|
||||
if (dpitch > 0 && (row16 <= 0 || dpb % 16 != 0 || dpitch < 2 * row_elems))
|
||||
throw std::runtime_error("all_gather: the output pitch is not a whole number of 16B words "
|
||||
"at least two rows wide");
|
||||
const int dp16 = dpitch > 0 ? (int)(dpb / 16) : 2 * row16;
|
||||
// The input's row pitch in elements, 0 for tight rows: whole words, at least a row.
|
||||
const long spb = (long)spitch * esize;
|
||||
if (spitch > 0 && (row16 <= 0 || spb % 16 != 0 || spitch < row_elems))
|
||||
throw std::runtime_error("all_gather: the input pitch is not a whole number of 16B words "
|
||||
"at least a row wide");
|
||||
const int sp16 = spitch > 0 ? (int)(spb / 16) : row16;
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = 1;
|
||||
if (nb > n16) nb = n16;
|
||||
@@ -139,5 +168,5 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
r4d_ar_gather_2rank_kernel<<<nb, nt, 0, st>>>(
|
||||
(const int4*)inp, (int4*)peer_scratch, (const int4*)my_scratch, (int4*)out,
|
||||
(unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, n16,
|
||||
(int)slot_stride16, (int)rank, row16, (int)drain, (int)acq, nbw);
|
||||
(int)slot_stride16, (int)rank, row16, dp16, sp16, (int)drain, (int)acq, nbw);
|
||||
}
|
||||
@@ -32,14 +32,15 @@ extern "C" void r4d_ar_gather_3rank(long peer_scratch0, long peer_scratch1,
|
||||
long my_scratch, long my_flags, long seq_ctrs,
|
||||
long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub);
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long dpitch, long spitch);
|
||||
extern "C" void r4d_ar_gather_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 inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long gate, long gate_ctr);
|
||||
long gate, long gate_ctr, long dpitch, long spitch);
|
||||
extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_scratch3, long peer_scratch4, long peer_scratch5,
|
||||
long peer_scratch6, long peer_flags0, long peer_flags1,
|
||||
@@ -48,14 +49,23 @@ extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp,
|
||||
long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub);
|
||||
long pub, long dpitch, long spitch);
|
||||
|
||||
// Where a rank's words land: the 2-rank r4d_g_dst generalised to WS. Named differently because two
|
||||
// __device__ definitions of one name in the two units would violate the ODR.
|
||||
__device__ __forceinline__ int r4d_g_dst_ws(int k, int row16, int n16, int WS, int who) {
|
||||
// `dp16` is the output's row pitch in words, WS * row16 when the gathered rows are tight.
|
||||
__device__ __forceinline__ int r4d_g_dst_ws(int k, int row16, int n16, int who, int dp16) {
|
||||
if (row16 <= 0 || row16 >= n16) return who * n16 + k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * (WS * row16) + who * row16 + c;
|
||||
return m * dp16 + who * row16 + c;
|
||||
}
|
||||
|
||||
// Where word k of this rank's contribution is read: `sp16` is the input's row pitch in words,
|
||||
// row16 when its rows are tight, more when they are a window of a wider row.
|
||||
__device__ __forceinline__ int r4d_g_src_ws(int k, int row16, int n16, int sp16) {
|
||||
if (row16 <= 0 || row16 >= n16 || sp16 == row16) return k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * sp16 + c;
|
||||
}
|
||||
|
||||
// Block b owns words [start,end) of the 16B-word space and drives its own handshake; no cross-block
|
||||
@@ -65,8 +75,8 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers,
|
||||
const int4* __restrict__ my_base, int4* __restrict__ out4,
|
||||
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs,
|
||||
int n16, int slot_stride16, int rank, int row16, int drain, int acq, int pub,
|
||||
long long* gate, unsigned int* gate_ctr) {
|
||||
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
|
||||
int pub, long long* gate, unsigned int* gate_ctr) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
// Placement holds WS source words live per output word, so the 2x unroll spills at ws=8 (same
|
||||
// limit as the one-shot all-reduce).
|
||||
@@ -95,14 +105,17 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
{
|
||||
int k = start + tid;
|
||||
for (; k + 3 * nt < end; k += 4 * nt) {
|
||||
const int4 v0 = in4[k], v1 = in4[k + nt], v2 = in4[k + 2 * nt], v3 = in4[k + 3 * nt];
|
||||
const int4 v0 = in4[r4d_g_src_ws(k, row16, n16, sp16)];
|
||||
const int4 v1 = in4[r4d_g_src_ws(k + nt, row16, n16, sp16)];
|
||||
const int4 v2 = in4[r4d_g_src_ws(k + 2 * nt, row16, n16, sp16)];
|
||||
const int4 v3 = in4[r4d_g_src_ws(k + 3 * nt, row16, n16, sp16)];
|
||||
#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 = in4[k];
|
||||
const int4 v = in4[r4d_g_src_ws(k, row16, n16, sp16)];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) ps[p][k] = v;
|
||||
}
|
||||
@@ -117,7 +130,7 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
|
||||
|
||||
// Place every rank's slice. Slot [parity][our rank] of our own scratch is never written, so, as
|
||||
// in the all-reduce, our rank's source is the input itself and the loop is branch-free.
|
||||
// in the all-reduce, our rank's source is the input itself, read at its own pitch.
|
||||
const int4* src[WS];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
@@ -127,17 +140,19 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
int k = start + tid;
|
||||
if (RU == 2) {
|
||||
for (; k + nt < end; k += 2 * nt) {
|
||||
const int k0 = r4d_g_src_ws(k, row16, n16, sp16), k1 = r4d_g_src_ws(k + nt, row16, n16, sp16);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r) {
|
||||
out4[r4d_g_dst_ws(k, row16, n16, WS, r)] = src[r][k];
|
||||
out4[r4d_g_dst_ws(k + nt, row16, n16, WS, r)] = src[r][k + nt];
|
||||
out4[r4d_g_dst_ws(k, row16, n16, r, dp16)] = src[r][r == rank ? k0 : k];
|
||||
out4[r4d_g_dst_ws(k + nt, row16, n16, r, dp16)] = src[r][r == rank ? k1 : k + nt];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (; k < end; k += nt) {
|
||||
const int k0 = r4d_g_src_ws(k, row16, n16, sp16);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
out4[r4d_g_dst_ws(k, row16, n16, WS, r)] = src[r][k];
|
||||
out4[r4d_g_dst_ws(k, row16, n16, r, dp16)] = src[r][r == rank ? k0 : k];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,7 +165,7 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp,
|
||||
long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream_i, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub, long gate, long gate_ctr) {
|
||||
long pub, long gate, long gate_ctr, long dpitch, long spitch) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
static_assert(NPEERS <= R4D_AR_WIDE_MAX_PEERS, "npeers exceeds the peer bundle bound");
|
||||
|
||||
@@ -173,6 +188,20 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla
|
||||
if (row_elems > 0 && (rowb % 16 != 0 || nbytes % rowb != 0))
|
||||
throw std::runtime_error("all_gather: row is not a whole number of 16B words");
|
||||
const int row16 = row_elems > 0 ? (int)(rowb / 16) : 0;
|
||||
// The output's row pitch in elements, 0 for a tight [rows][WS][row]: whole words, at least the
|
||||
// WS ranks' row (the gathered rows as the leading columns of a wider buffer).
|
||||
const long dpb = (long)dpitch * esize;
|
||||
if (dpitch > 0 && (row16 <= 0 || dpb % 16 != 0 || dpitch < (long)WS * row_elems))
|
||||
throw std::runtime_error("all_gather: the output pitch is not a whole number of 16B words "
|
||||
"at least " + std::to_string(WS) + " rows wide");
|
||||
const int dp16 = dpitch > 0 ? (int)(dpb / 16) : WS * row16;
|
||||
// The input's row pitch in elements, 0 for tight rows: whole words, at least a row (a rank's
|
||||
// window of a wider row).
|
||||
const long spb = (long)spitch * esize;
|
||||
if (spitch > 0 && (row16 <= 0 || spb % 16 != 0 || spitch < row_elems))
|
||||
throw std::runtime_error("all_gather: the input pitch is not a whole number of 16B words "
|
||||
"at least a row wide");
|
||||
const int sp16 = spitch > 0 ? (int)(spb / 16) : row16;
|
||||
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = R4D_AR_NB_DESIGN;
|
||||
@@ -209,8 +238,8 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
r4d_ar_gather_wide_kernel<WS><<<nb, nt, 0, st>>>(
|
||||
(const int4*)inp, peers, (const int4*)my_scratch, (int4*)out, (unsigned int*)my_flags,
|
||||
(unsigned int*)seq_ctrs, n16, (int)slot_stride16, (int)rank, row16, (int)drain, (int)acq,
|
||||
(int)pub, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
(unsigned int*)seq_ctrs, n16, (int)slot_stride16, (int)rank, row16, dp16, sp16, (int)drain,
|
||||
(int)acq, (int)pub, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
}
|
||||
|
||||
/* Three ranks, e.g. Qwen3.8-Flash-Next padded to 36 heads / 3 KV heads (docs/TP3.md). */
|
||||
@@ -218,13 +247,14 @@ void r4d_ar_gather_3rank(long peer_scratch0, long peer_scratch1,
|
||||
long peer_flags0, long peer_flags1, long my_scratch,
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp, long out,
|
||||
long n_elem, long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub) {
|
||||
long nblocks, long nthreads, long drain, long acq, long pub, long dpitch,
|
||||
long spitch) {
|
||||
const long ps[2] = {peer_scratch0, peer_scratch1};
|
||||
const long pf[2] = {peer_flags0, peer_flags1};
|
||||
/* No link gate at three ranks: the row answers 1, the engine releases by barrier. */
|
||||
ar_gather_wide_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
|
||||
0L, 0L);
|
||||
0L, 0L, dpitch, spitch);
|
||||
}
|
||||
|
||||
void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
@@ -232,12 +262,12 @@ void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scrat
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp, long out,
|
||||
long n_elem, long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub, long gate,
|
||||
long gate_ctr) {
|
||||
long gate_ctr, long dpitch, long spitch) {
|
||||
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
|
||||
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
|
||||
ar_gather_wide_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
|
||||
gate, gate_ctr);
|
||||
gate, gate_ctr, dpitch, spitch);
|
||||
}
|
||||
|
||||
void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
@@ -247,7 +277,7 @@ void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scrat
|
||||
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
|
||||
long inp, long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub) {
|
||||
long pub, long dpitch, long spitch) {
|
||||
const long ps[7] = {peer_scratch0, peer_scratch1, peer_scratch2, peer_scratch3,
|
||||
peer_scratch4, peer_scratch5, peer_scratch6};
|
||||
const long pf[7] = {peer_flags0, peer_flags1, peer_flags2, peer_flags3,
|
||||
@@ -255,5 +285,5 @@ void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scrat
|
||||
/* No link gate at eight ranks: the row answers 1, the engine releases by barrier. */
|
||||
ar_gather_wide_launch<8>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
|
||||
0L, 0L);
|
||||
0L, 0L, dpitch, spitch);
|
||||
}
|
||||
+24
-10
@@ -931,7 +931,7 @@ extern "C" int r4d_gemm_fp8a8_block(void) { return (int)kBlock; }
|
||||
static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
|
||||
int M, int K, int N, int a_ld, int sa_ld, int w_ld, int sw_ld,
|
||||
long scratch, long scratch_bytes, RadStream stream,
|
||||
bool i8 = false);
|
||||
bool i8 = false, int split_n = 0);
|
||||
|
||||
extern "C" int r4d_gemm_fp8a8_nt_m16(long a, long ascale, long wq, long ws, long c,
|
||||
int M, int K, int N, int a_ld, int sa_ld, int w_ld, int sw_ld,
|
||||
@@ -961,12 +961,21 @@ extern "C" void r4d_gemm_fp8a8_pin_nt(int nt) {
|
||||
// Bytes of f32 scratch one r4d_gemm_fp8a8_nt_m16 call needs, or 0 when it will not split. The
|
||||
// caller may pass more, or none (the launch then runs unsplit: correct but slow, and unreported).
|
||||
// The bound is max_m() so the query and the band cannot drift apart.
|
||||
extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
|
||||
//
|
||||
// `split_n` (0: N) is the width the split is chosen for. The split depends on N alone, and it
|
||||
// fixes the order K partials are summed in, so a row slice of a wider linear (N rows of a
|
||||
// split_n-row weight) passes the whole width to sum each output as the whole linear does: the
|
||||
// slice's outputs are then the linear's to the bit.
|
||||
static long m16_scratch(int M, int K, int N, int split_n) {
|
||||
if (M <= 0 || K <= 0 || N <= 0 || M > r4d_gemm_fp8a8_nt_m16_max_m()) return 0;
|
||||
uint32_t ck = 0;
|
||||
const uint32_t k2 = narrow_ksplit((uint32_t)N, (uint32_t)K, (uint32_t)M, &ck);
|
||||
const uint32_t k2 = narrow_ksplit((uint32_t)(split_n > 0 ? split_n : N), (uint32_t)K,
|
||||
(uint32_t)M, &ck);
|
||||
return k2 > 1 ? (long)k2 * M * N * (long)sizeof(float) : 0;
|
||||
}
|
||||
extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
|
||||
return m16_scratch(M, K, N, 0);
|
||||
}
|
||||
|
||||
// C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T, M <= 64.
|
||||
//
|
||||
@@ -976,10 +985,12 @@ extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
|
||||
// ws [ceil(N/128)][ceil(K/128)] bf16, `sw_ld` elements a row (0 = ceil(K/128))
|
||||
// c [M][N] bf16
|
||||
//
|
||||
// Returns 0, or a negative code for a shape it does not serve. `scratch` may be 0.
|
||||
// Returns 0, or a negative code for a shape it does not serve. `scratch` may be 0. `split_n` is
|
||||
// r4d_gemm_fp8a8_nt_m16_scratch's.
|
||||
static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
|
||||
int M, int K, int N, int a_ld, int sa_ld, int w_ld, int sw_ld,
|
||||
long scratch, long scratch_bytes, RadStream stream, bool i8) {
|
||||
long scratch, long scratch_bytes, RadStream stream, bool i8,
|
||||
int split_n) {
|
||||
int emit_rc = RAD_OK;
|
||||
if (!a || !ascale || !wq || !ws || !c) return -1;
|
||||
if (M <= 0 || K <= 0 || N <= 0) return -2;
|
||||
@@ -1000,7 +1011,8 @@ static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
|
||||
const uint32_t gx = ((uint32_t)N + fm - 1) / fm;
|
||||
|
||||
uint32_t ck = 0;
|
||||
uint32_t ksplit = narrow_ksplit((uint32_t)N, (uint32_t)K, (uint32_t)M, &ck);
|
||||
uint32_t ksplit = narrow_ksplit((uint32_t)(split_n > 0 ? split_n : N), (uint32_t)K,
|
||||
(uint32_t)M, &ck);
|
||||
const long need = ksplit > 1 ? (long)ksplit * M * N * (long)sizeof(float) : 0;
|
||||
if (need && (!scratch || scratch_bytes < need)) { ksplit = 1; ck = 0; }
|
||||
|
||||
@@ -1260,10 +1272,10 @@ static int gemm_8a8_tiled(long a, long ascale, long wq, long ws, long c, int M,
|
||||
// The shared operand reader is r4d_fp8_args.h; see libr4d/r4d_args.h.
|
||||
extern "C" int64_t r4d_rad_gemm_fp8a8_m16_scratch(const RadArgs* a) {
|
||||
const long long M = r4d_geti_or(a, "M", 0), N = r4d_geti_or(a, "N", 0),
|
||||
K = r4d_geti_or(a, "K", 0);
|
||||
K = r4d_geti_or(a, "K", 0), sn = r4d_geti_or(a, "split_n", 0);
|
||||
if (M < 1 || N < 1 || K < 1) return 0;
|
||||
if (M > r4d_gemm_fp8a8_nt_m16_max_m()) return 0;
|
||||
return (int64_t)r4d_gemm_fp8a8_nt_m16_scratch((int)M, (int)K, (int)N);
|
||||
return (int64_t)m16_scratch((int)M, (int)K, (int)N, (int)sn);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_gemm_fp8a8_m16(const RadArgs* a, RadStream stream) {
|
||||
@@ -1276,7 +1288,8 @@ extern "C" int r4d_rad_gemm_fp8a8_m16(const RadArgs* a, RadStream stream) {
|
||||
const int e = gemm_fp8a8_nt_m16_emit(
|
||||
r4d_addr(g.a), r4d_addr(g.ascale), r4d_addr(g.b), r4d_addr(g.bscale), r4d_addr(g.y),
|
||||
(int)g.M, (int)g.K, (int)g.N, (int)g.a_ld, (int)g.sa_ld, (int)g.w_ld, (int)g.sw_ld,
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream);
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream, false,
|
||||
(int)r4d_geti_or(a, "split_n", 0));
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
@@ -1390,7 +1403,8 @@ extern "C" int r4d_rad_gemm_i8a8_m16(const RadArgs* a, RadStream stream) {
|
||||
const int e = gemm_fp8a8_nt_m16_emit(
|
||||
r4d_addr(g.a), r4d_addr(g.ascale), r4d_addr(g.b), r4d_addr(g.bscale), r4d_addr(g.y),
|
||||
(int)g.M, (int)g.K, (int)g.N, (int)g.a_ld, (int)g.sa_ld, (int)g.w_ld, (int)g.sw_ld,
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream, true);
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream, true,
|
||||
(int)r4d_geti_or(a, "split_n", 0));
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
|
||||
@@ -736,6 +736,9 @@ static const RadParamSpec pGemmNtQ[] = {
|
||||
{ "dtype", RAD_P_STR, RAD_REQUIRED },
|
||||
{ "pb1", RAD_P_INT, RAD_OPTIONAL },
|
||||
{ "pb2", RAD_P_INT, RAD_OPTIONAL },
|
||||
/* The width whose K split a row slice of a wider linear takes (docs/OPS.md); only the narrow
|
||||
* fp8a8 / i8a8 rows split K by N, and they read it. */
|
||||
{ "split_n", RAD_P_INT, RAD_OPTIONAL },
|
||||
};
|
||||
/* docs/OPS.md's `gemm_nt_q` is `a`, `a_scale`, `b`w, `b_scale`w -> `y`. Two of libr4d's six
|
||||
* quantised GEMMs need a seventh operand. RadLayout can declare aux streams beside a weight, but
|
||||
|
||||
+167
-4
@@ -11647,6 +11647,62 @@ static void fp8_numeric(const Plugin& pl, int64_t N, int64_t K) {
|
||||
why.empty(), why);
|
||||
}
|
||||
|
||||
/* A row slice of this linear issued with split_n = N (DenseFP8::declare_slice: a split
|
||||
* fc's share and its tail) must give each of its rows exactly as the whole GEMM does,
|
||||
* whatever K split its own width would pick: half the rows, and the last 128 (which
|
||||
* alone would split K the most). The slice's planes are quantised from its own rows,
|
||||
* which on whole 128-row blocks are the whole weight's codes and scales. */
|
||||
if (N >= 256) {
|
||||
const int64_t cut = (N / 2) / 128 * 128;
|
||||
std::string why;
|
||||
const std::pair<int64_t, int64_t> slices[] = { { 0, cut }, { N - 128, N } };
|
||||
for (const auto& sl : slices) {
|
||||
const int64_t lo = sl.first, hi = sl.second, Ns = hi - lo;
|
||||
std::vector<float> wsl(wf.begin() + lo * K, wf.begin() + hi * K);
|
||||
const Planes psl = fp8_block_planes(wsl, Ns, K);
|
||||
std::vector<RadParam> lps = { PI("N", Ns), PI("K", K), PI("group", 128) };
|
||||
std::vector<uint8_t> qsl;
|
||||
if (stored(nr, lps, 2, psl, { "codes" }, &qsl) != RAD_OK) {
|
||||
why = "the slice's layout hook refused";
|
||||
break;
|
||||
}
|
||||
void* d_wsl = dalloc((size_t)Ns * K);
|
||||
void* d_ysl = dalloc((size_t)M * Ns * 2);
|
||||
if (!d_wsl || !d_ysl) { why = "hipMalloc failed"; break; }
|
||||
st_memcpy(d_wsl, qsl.data(), qsl.size(), hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> tsl = {
|
||||
T(d_q, RAD_F8E4M3, { M, K }), T(d_qs, RAD_F32, { M, sk }),
|
||||
T(d_wsl, RAD_F8E4M3, { Ns, K }),
|
||||
T((char*)d_s + (size_t)(lo / 128) * (size_t)sk * 2, RAD_BF16,
|
||||
{ Ns / 128, sk }),
|
||||
T(d_ysl, RAD_BF16, { M, Ns }), T(nullptr, RAD_F32, { 1 }),
|
||||
T(nullptr, RAD_U8, { 1 }),
|
||||
};
|
||||
absent(tsl[5]); absent(tsl[6]);
|
||||
std::vector<RadParam> pss = {
|
||||
PI("M", M), PI("N", Ns), PI("K", K), PI("group", 128),
|
||||
PS("dtype", "fp8a8"), PI("split_n", N),
|
||||
};
|
||||
st_memset(d_ysl, 0, (size_t)M * Ns * 2);
|
||||
run_case(nr, tsl, pss);
|
||||
std::vector<uint16_t> ys((size_t)M * Ns);
|
||||
st_memcpy(ys.data(), d_ysl, ys.size() * 2, hipMemcpyDeviceToHost);
|
||||
for (int64_t m = 0; m < M && why.empty(); ++m)
|
||||
for (int64_t r = 0; r < Ns; ++r)
|
||||
if (ys[(size_t)(m * Ns + r)] != got[(size_t)(m * N + lo + r)]) {
|
||||
why = "rows [" + std::to_string((long long)lo) + ", " +
|
||||
std::to_string((long long)hi) + ") differ at m " +
|
||||
std::to_string((long long)m) + " row " +
|
||||
std::to_string((long long)(lo + r));
|
||||
break;
|
||||
}
|
||||
if (!why.empty()) break;
|
||||
}
|
||||
ok((std::string("gemm_fp8a8_nt_m16's row slices given split_n are the whole GEMM's "
|
||||
"rows, M ") + std::to_string((long long)M)).c_str(),
|
||||
why.empty(), why);
|
||||
}
|
||||
|
||||
/* ---- ...and with the gate and the quantiser folded into its epilogue -------------
|
||||
*
|
||||
* Byte equality: the engine substitutes the fold for the pair (this GEMM's bf16
|
||||
@@ -21025,6 +21081,12 @@ struct ArThread {
|
||||
/* Ranks in the collective: 2, or 3 for the wide family at a width that is not a power of
|
||||
* two. */
|
||||
int world = 2;
|
||||
/* The all-gather's output row pitch in elements, 0 for a tight [rows][world][row]; more lands
|
||||
* the gathered rows in the leading columns of a wider buffer. */
|
||||
int64_t pitch = 0;
|
||||
/* Its input row pitch, 0 for tight rows; more reads each row's first `row_elems` out of a wider
|
||||
* row whose other columns hold junk. */
|
||||
int64_t spitch = 0;
|
||||
};
|
||||
|
||||
static void ar_worker(ArThread* t) {
|
||||
@@ -21207,8 +21269,10 @@ static void gather_worker(ArThread* t) {
|
||||
|
||||
void* in = nullptr;
|
||||
void* out = nullptr;
|
||||
const int64_t rows = t->pitch > 0 ? t->numel / t->row_elems : 0;
|
||||
const int64_t out_n = t->pitch > 0 ? rows * t->pitch : t->numel * t->world;
|
||||
if (hipMalloc(&in, (size_t)t->numel * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
if (hipMalloc(&out, (size_t)t->numel * 4 * t->world) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
if (hipMalloc(&out, (size_t)out_n * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
|
||||
std::vector<uint32_t> h((size_t)t->numel);
|
||||
for (int64_t i = 0; i < t->numel; i += 2) {
|
||||
@@ -21216,10 +21280,29 @@ static void gather_worker(ArThread* t) {
|
||||
const float v = (float)(t->rank + 1) * 0.5f + (float)i; /* its logit */
|
||||
std::memcpy(&h[(size_t)i + 1], &v, 4);
|
||||
}
|
||||
st_memcpy(in, h.data(), h.size() * 4, hipMemcpyHostToDevice);
|
||||
st_memset(out, 0xAB, (size_t)t->numel * 4 * t->world); /* marks untouched slices */
|
||||
if (t->spitch > 0) { /* each row's contribution at the front of a wider row of junk */
|
||||
const int64_t srows = t->numel / t->row_elems;
|
||||
std::vector<uint32_t> hp((size_t)(srows * t->spitch), 0xCDCDCDCDu);
|
||||
for (int64_t m = 0; m < srows; ++m)
|
||||
std::memcpy(&hp[(size_t)(m * t->spitch)], &h[(size_t)(m * t->row_elems)],
|
||||
(size_t)t->row_elems * 4);
|
||||
st_free(in);
|
||||
if (hipMalloc(&in, hp.size() * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
st_memcpy(in, hp.data(), hp.size() * 4, hipMemcpyHostToDevice);
|
||||
} else {
|
||||
st_memcpy(in, h.data(), h.size() * 4, hipMemcpyHostToDevice);
|
||||
}
|
||||
st_memset(out, 0xAB, (size_t)out_n * 4); /* marks untouched slices */
|
||||
|
||||
RadTensor ts[2] = { T(in, RAD_F32, { t->numel }), T(out, RAD_F32, { t->numel * t->world }) };
|
||||
if (t->pitch > 0) { /* [rows][world * row], the leading columns of rows `pitch` apart */
|
||||
ts[1] = T(out, RAD_F32, { rows, t->world * t->row_elems });
|
||||
ts[1].stride[0] = t->pitch;
|
||||
}
|
||||
if (t->spitch > 0) { /* [rows][row], read `spitch` apart */
|
||||
ts[0] = T(in, RAD_F32, { t->numel / t->row_elems, t->row_elems });
|
||||
ts[0].stride[0] = t->spitch;
|
||||
}
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts; a.n_t = 2;
|
||||
@@ -21230,7 +21313,7 @@ static void gather_worker(ArThread* t) {
|
||||
t->rc_launch = t->row->launch(&a, nullptr);
|
||||
st_stream_sync(nullptr);
|
||||
|
||||
t->out.assign((size_t)t->numel * t->world, 0.0f);
|
||||
t->out.assign((size_t)out_n, 0.0f);
|
||||
st_memcpy(t->out.data(), out, t->out.size() * 4, hipMemcpyDeviceToHost);
|
||||
st_free(in);
|
||||
st_free(out);
|
||||
@@ -22310,6 +22393,86 @@ static void gather_wide_rank(const Plugin& pl, const std::vector<int>& devs, con
|
||||
bad == 0, std::to_string(bad) + " words wrong, " + std::to_string(bad_id) +
|
||||
" of them token ids");
|
||||
if (row->fini) for (ArThread& a : t) if (a.inst) row->fini(a.inst);
|
||||
|
||||
/* The same rows landed in the leading columns of a wider buffer, as a row-sharded linear whose
|
||||
* width does not divide among the ranks gathers into the front of its full output row: 64
|
||||
* columns (four words) past world * row, which must keep their marker. */
|
||||
{
|
||||
const int64_t pitch = W * rowe + 64;
|
||||
std::vector<ArThread> tp((size_t)W);
|
||||
std::vector<std::thread> thp;
|
||||
for (int r = 0; r < W; ++r) {
|
||||
tp[(size_t)r] = ArThread{ row, r, devs[(size_t)r], numel };
|
||||
tp[(size_t)r].world = W;
|
||||
tp[(size_t)r].row_elems = rowe;
|
||||
tp[(size_t)r].pitch = pitch;
|
||||
thp.emplace_back(gather_worker, &tp[(size_t)r]);
|
||||
}
|
||||
for (std::thread& x : thp) x.join();
|
||||
bool upp = true;
|
||||
std::string rcp;
|
||||
for (const ArThread& a : tp) {
|
||||
upp = upp && a.rc_init == RAD_OK && a.rc_launch == RAD_OK;
|
||||
rcp += std::to_string(a.rc_init) + "/" + std::to_string(a.rc_launch) + " ";
|
||||
}
|
||||
int64_t badp = 0, clobbered = 0;
|
||||
if (upp)
|
||||
for (const ArThread& a : tp)
|
||||
for (int64_t m = 0; m < nrows; ++m) {
|
||||
for (int r = 0; r < W; ++r)
|
||||
for (int64_t j = 0; j < rowe; ++j) {
|
||||
uint32_t got;
|
||||
std::memcpy(&got, &a.out[(size_t)(m * pitch + r * rowe + j)], 4);
|
||||
if (got != sent(r, m * rowe + j)) ++badp;
|
||||
}
|
||||
for (int64_t j = W * rowe; j < pitch; ++j) {
|
||||
uint32_t got;
|
||||
std::memcpy(&got, &a.out[(size_t)(m * pitch + j)], 4);
|
||||
if (got != 0xABABABABu) ++clobbered;
|
||||
}
|
||||
}
|
||||
ok((w + "-rank all-gather lands its rows at a wider output pitch, the rest untouched").c_str(),
|
||||
upp && badp == 0 && clobbered == 0,
|
||||
upp ? std::to_string(badp) + " words wrong, " + std::to_string(clobbered) +
|
||||
" words past the rows overwritten"
|
||||
: "init/launch rc " + rcp);
|
||||
if (row->fini) for (ArThread& a : tp) if (a.inst) row->fini(a.inst);
|
||||
}
|
||||
|
||||
/* And each rank's rows read out of a wider input row, as a split linear's rank hands over its
|
||||
* window of the block-aligned rows it computed: the junk around the window must not travel. */
|
||||
{
|
||||
const int64_t spitch = rowe + 64;
|
||||
std::vector<ArThread> ts2((size_t)W);
|
||||
std::vector<std::thread> ths;
|
||||
for (int r = 0; r < W; ++r) {
|
||||
ts2[(size_t)r] = ArThread{ row, r, devs[(size_t)r], numel };
|
||||
ts2[(size_t)r].world = W;
|
||||
ts2[(size_t)r].row_elems = rowe;
|
||||
ts2[(size_t)r].spitch = spitch;
|
||||
ths.emplace_back(gather_worker, &ts2[(size_t)r]);
|
||||
}
|
||||
for (std::thread& x : ths) x.join();
|
||||
bool ups = true;
|
||||
std::string rcs2;
|
||||
for (const ArThread& a : ts2) {
|
||||
ups = ups && a.rc_init == RAD_OK && a.rc_launch == RAD_OK;
|
||||
rcs2 += std::to_string(a.rc_init) + "/" + std::to_string(a.rc_launch) + " ";
|
||||
}
|
||||
int64_t bads = 0;
|
||||
if (ups)
|
||||
for (const ArThread& a : ts2)
|
||||
for (int64_t m = 0; m < nrows; ++m)
|
||||
for (int r = 0; r < W; ++r)
|
||||
for (int64_t j = 0; j < rowe; ++j) {
|
||||
uint32_t got;
|
||||
std::memcpy(&got, &a.out[(size_t)(m * W * rowe + r * rowe + j)], 4);
|
||||
if (got != sent(r, m * rowe + j)) ++bads;
|
||||
}
|
||||
ok((w + "-rank all-gather reads each rank's rows at a wider input pitch").c_str(),
|
||||
ups && bads == 0, ups ? std::to_string(bads) + " words wrong" : "init/launch rc " + rcs2);
|
||||
if (row->fini) for (ArThread& a : ts2) if (a.inst) row->fini(a.inst);
|
||||
}
|
||||
ok((w + "-rank all-gather fini ran").c_str(), true);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user