Archived
TP4 for Qwen3.8-Flash-Next: unfused chain, four refusals answered
World 4 refused at declare on two counts and at boot on a third the audit
found; the fourth is unchanged by design.
- rad_arch.h: a world above the KV head count replicates the heads in rank
pairs instead of dividing them. Geom gains kv_off; each rank declares
n_head_kv 1 and gqa 6. A world that is not a multiple of the head count
is still refused, by name.
- rad_block_attn_gated_fp8.h: k/v pass the row span LinearFP8 already
takes, gated on world > n_head_kv_all so an even shard is untouched and
TP2 declares byte-identically.
- qwen4exp_fp8.cpp: above two ranks the ranks own whole experts (5 blocks
of 128 do not cut four ways); the parity tables stay two-rank and the
router stays replicated.
- libr4d: attn_{prefill,decode}_h256_gqa6_bs4 rows and their TU -- the
gqa8 body at ratio 6, page 4. No fused form is instantiated at this
geometry; the unfused pair is what resolves.
- libr4d: r4d_ar_gather_Nrank.hip. The vocab-parallel sampler declares an
all_gather at any world > 1 and libr4d shipped only the 2-rank row, so a
TP4 boot was refused by the reference-kernel gate rather than served
wrong. The wide one-shot all-reduce with the reduction removed.
- rad_block_attn_gated.h / rad_block_attn_fp8.h: refuse a world above the
KV head count. They read n_head_kv and declare no span, so they would
have booted and read half a KV head -- fluent wrong text.
- tests/arch_test.cpp: the four-rank declare test, and the fake records
the shard span the real builder stores.
- README, ARCHITECTURES: TP4 in the present tense, with the costs named
and the rate against TP2 left unmeasured.
This commit is contained in:
1 parent
966bda899a
commit
7cf4c005e1
14 files changed
+926
-31
No files matched your search
@@ -63,6 +63,9 @@ set(R4D_UNITS
|
||||
# Twelve, head_dim 256 only: Qwen3.8-Flash-Next's QSA layers are 24 over 2. Four lines that
|
||||
# set the ratio and include the unit above, which is the parameterised body.
|
||||
"r4d_attn_paged_h256_gqa12|"
|
||||
# Six, head_dim 256 and the same 4-token page: the same QSA layers at four ranks, where each KV
|
||||
# head is replicated across a rank pair. Again four lines that set the ratio and include the body.
|
||||
"r4d_attn_paged_h256_gqa6_bs4|"
|
||||
"r4d_attn_vit_h72_bf16|"
|
||||
# The vision tower's own small ops: layer norm, the learned-grid position embedding, and the
|
||||
# biased GEMM with a linear layer's activation and residual in its store.
|
||||
@@ -87,6 +90,7 @@ set(R4D_UNITS
|
||||
# copy of it, which is the one thing it must not have.
|
||||
"r4d_ar_entry|"
|
||||
"r4d_ar_gather_2rank|"
|
||||
"r4d_ar_gather_Nrank|"
|
||||
"r4d_ar_oneshot_2rank_exact|"
|
||||
"r4d_ar_oneshot_2rank_wht6|"
|
||||
"r4d_ar_oneshot_Nrank_exact|"
|
||||
|
||||
@@ -527,6 +527,112 @@ extern "C" int r4d_rad_ar_gather_2rank(const RadArgs* a, RadStream s) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ wide (4 / 8 rank) all-gather
|
||||
* The gather the 2-rank one cannot be at width: its handshake is one peer flag per block, so the
|
||||
* wide form takes the wide family's peer arrays and r4d_ar_handshake instead (r4d_ar_wide.h), and
|
||||
* it rides the SAME instance, scratch, flags and counters as the wide one-shots above -- a slot
|
||||
* holds a full message here exactly as it does there, and this message is the INPUT. The two
|
||||
* launchers live in r4d_ar_gather_Nrank.hip, which is the only family unit whose caller is inside
|
||||
* the plugin, so they are declared here rather than in r4d.h. */
|
||||
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);
|
||||
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,
|
||||
long peer_flags2, long peer_flags3, long peer_flags4,
|
||||
long peer_flags5, long peer_flags6, 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);
|
||||
|
||||
/* Every check the 2-rank gather makes, and the same `row` rule: an all-gather's output is
|
||||
* world_size times its input, so this is not ar_setup either. */
|
||||
static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in, ArCall* c,
|
||||
long long* row_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 */
|
||||
|
||||
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);
|
||||
if (n <= 0 || n > in->max_numel) return RAD_E_SHAPE;
|
||||
/* "y is [world_size * numel] with rank r's contribution at offset r * numel" (docs/OPS.md).
|
||||
* A y of any other extent is a caller that means something this kernel does not do. */
|
||||
if ((long long)r4d_numel(y) != (long long)n * in->world) return RAD_E_SHAPE;
|
||||
|
||||
/* The row width, if the caller asked for the row-interleaved placement. It has to divide the
|
||||
* message and be a whole number of 16-byte words, both of which libr4d rechecks. */
|
||||
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;
|
||||
|
||||
/* 16-byte vector stores, and libr4d throws on a message that is not a whole number of them. */
|
||||
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;
|
||||
|
||||
R4dArCfg cfgv;
|
||||
r4d_cfg_ar(a, &cfgv);
|
||||
const long words16 = (long)n * in->esize / 16;
|
||||
int nblocks = cfgv.nblocks > 0 ? cfgv.nblocks : nb_for(words16, in->nb_cap);
|
||||
if (nblocks > in->max_blocks) nblocks = in->max_blocks;
|
||||
|
||||
*out_in = in;
|
||||
c->in = in;
|
||||
c->inp = r4d_addr(x);
|
||||
c->out = r4d_addr(y);
|
||||
c->numel = n;
|
||||
c->nblocks = nblocks;
|
||||
c->nthreads = cfgv.nthreads;
|
||||
c->drain = cfgv.drain;
|
||||
c->acq = cfgv.acq;
|
||||
c->pub = cfgv.pub;
|
||||
*row_out = row;
|
||||
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);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_4rank",
|
||||
r4d_ar_gather_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,
|
||||
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));
|
||||
return 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);
|
||||
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],
|
||||
in->peer_scratch[3], in->peer_scratch[4], in->peer_scratch[5],
|
||||
in->peer_scratch[6],
|
||||
in->peer_flags[0], in->peer_flags[1], in->peer_flags[2],
|
||||
in->peer_flags[3], in->peer_flags[4], in->peer_flags[5],
|
||||
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));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ 2-rank one-shot */
|
||||
|
||||
extern "C" int r4d_rad_ar_oneshot_2rank_exact(const RadArgs* a, RadStream s) {
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
// r4d_ar_gather_Nrank.hip: r4d_ar_gather_4rank, r4d_ar_gather_8rank -- one-shot P2P all-GATHER,
|
||||
// 4 or 8 ranks, gfx1201. WS is compile-time; the 2-rank gather keeps its own file, exactly as the
|
||||
// one-shot all-reduce's does.
|
||||
//
|
||||
// WHY IT EXISTS. r4d_ar_gather_2rank.hip is the engine's only device all-gather and its handshake
|
||||
// is ONE PEER FLAG per block, not a tree (r4d_ar_entry.hip:492), so it is compiled for two ranks
|
||||
// and refuses every other width. The vocab-parallel sampler declares an all_gather whenever the
|
||||
// vocabulary is sharded, so at world 4 nothing but libref's host ref_all_gather resolves and the
|
||||
// serve is refused by name. This is the wide one-shot all-reduce with the reduction REMOVED: same
|
||||
// push, same per-block handshake, same double-buffered (parity, source) scratch, and the fp32 sum
|
||||
// replaced by placement.
|
||||
//
|
||||
// THE PAYLOAD IS MOVED AND NEVER SUMMED. What the sampler gathers is (u32 global token id, f32
|
||||
// logit) pairs; a token id's bit pattern read as f32 is a denormal, so adding two lanes would
|
||||
// work on the logit and destroy the id. `dtype` is taken for symmetry with the family and used
|
||||
// only to turn n_elem into bytes.
|
||||
//
|
||||
// THE PLACEMENT. row16 is one ROW of this rank's contribution in 16-byte words and r4d_g_dst_ws
|
||||
// says where a rank's words land: the plain concatenation when there is one row, and otherwise
|
||||
// ROW-INTERLEAVED -- input [rows][row16], output [rows][WS][row16] -- because the merge that
|
||||
// consumes this reads one row of gathered candidates per sampled position at a single row stride.
|
||||
// See r4d_ar_gather_2rank.hip, whose comment states why that is not a convenience.
|
||||
//
|
||||
// The IPC helpers and the handshake live in r4d_ar_wide.h; this unit links beside r4d_ar_entry.hip,
|
||||
// which owns the instance and the peer arrays.
|
||||
#include "r4d.h"
|
||||
#include "r4d_ar_wide.h"
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
// The launchers this unit provides, in the family's shape: the peer arguments are ascending global
|
||||
// rank with the current rank removed, and `out` is world_size * n_elem elements. Declared extern
|
||||
// "C" here, at arm's length from the definitions below, so those definitions get C linkage; the
|
||||
// caller is r4d_ar_entry.hip, which owns the instance and repeats the two prototypes at its call
|
||||
// site (r4d.h declares the rest of the family and is not this unit's file).
|
||||
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);
|
||||
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,
|
||||
long peer_flags2, long peer_flags3, long peer_flags4,
|
||||
long peer_flags5, long peer_flags6, 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);
|
||||
|
||||
// WHERE A RANK'S WORDS LAND -- the 2-rank r4d_g_dst at a compile-time width. Named apart from it
|
||||
// on purpose: same rule, different WS, and two __device__ definitions of one name would be an ODR
|
||||
// violation between the two units.
|
||||
__device__ __forceinline__ int r4d_g_dst_ws(int k, int row16, int n16, int WS, int who) {
|
||||
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;
|
||||
}
|
||||
|
||||
// Block b owns words [start,end) of the 16B-word space and drives its own handshake; no cross-block
|
||||
// sync, exactly as in the all-reduce this rides beside.
|
||||
template <int WS>
|
||||
__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) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
// The placement holds WS source words live per output word, so the 2x unroll spills at ws=8 --
|
||||
// the same ceiling the one-shot all-reduce draws for the same reason.
|
||||
constexpr int RU = (WS <= 4) ? 2 : 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;
|
||||
|
||||
// Every peer lays its scratch out identically, so one index serves all of them: the slot that
|
||||
// NAMES THE SOURCE RANK. We write our own words into the same slot number of every peer's
|
||||
// scratch, and every peer reads them out of the slot numbered by OUR rank.
|
||||
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;
|
||||
|
||||
// push: 4x-unrolled, and the loaded words are reused across peers so the input is read once.
|
||||
// This rank's own slice is NOT placed here -- the pass after the handshake places every rank's
|
||||
// slice from one src[] table, and duplicating the local store to hide it here would be the only
|
||||
// thing the all-reduce's shape does not carry over.
|
||||
{
|
||||
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];
|
||||
#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];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) ps[p][k] = v;
|
||||
}
|
||||
}
|
||||
|
||||
// The wide family's own protocol: publish 2*seq to every peer's flag word [b][our rank], then
|
||||
// wait on our own [b][source] for every source. Flag values, parity and the slot guard are
|
||||
// r4d_ar_wide.h's, shared with the all-reduces this instance's buffers serve, and a handshake
|
||||
// that cannot complete traps rather than pass stale bytes on as a peer's.
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
|
||||
|
||||
// The peers' slices, now staged in this rank's scratch, into the places that name them. Slot
|
||||
// [parity][our rank] of my own scratch is written by nobody, so -- exactly as in the all-reduce
|
||||
// -- the source that names OUR rank is the input itself and every lane is branch-free.
|
||||
const int4* src[WS];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
src[r] = (r == rank) ? in4
|
||||
: (my_base + ((size_t)parity * WS + r) * (size_t)slot_stride16);
|
||||
{
|
||||
int k = start + tid;
|
||||
if (RU == 2) {
|
||||
for (; k + nt < end; k += 2 * nt) {
|
||||
#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];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (; k < end; k += nt) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
out4[r4d_g_dst_ws(k, row16, n16, WS, r)] = src[r][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- host side ---
|
||||
// `row_elems` is the row width in ELEMENTS, 0 for the plain concatenation; the entry point has
|
||||
// already checked that it divides the message and is a whole number of 16-byte words, and it is
|
||||
// rechecked here because this is the half that throws.
|
||||
template <int WS>
|
||||
static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_flags, 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_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("all_gather: rank " + std::to_string(rank) +
|
||||
" out of range for the " + std::to_string(WS) +
|
||||
" ranks this entry point is compiled for");
|
||||
|
||||
const int esize = (dtype == 2) ? 4 : 2;
|
||||
const long nbytes = (long)n_elem * esize;
|
||||
if (nbytes % 16 != 0) throw std::runtime_error("all_gather: n_elem*esize not 16B-aligned");
|
||||
const long n16l = nbytes / 16;
|
||||
if (n16l > (long)slot_stride16)
|
||||
throw std::runtime_error("all_gather: message (" + std::to_string(n16l) +
|
||||
" 16B words) exceeds the per-source slot stride (" +
|
||||
std::to_string(slot_stride16) + ")");
|
||||
const int n16 = (int)n16l;
|
||||
|
||||
long rowb = (long)row_elems * esize;
|
||||
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;
|
||||
|
||||
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("all_gather: 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;
|
||||
}
|
||||
|
||||
// Same buffers, same rule as the all-reduce this shares an instance with: scratch backs 2*WS
|
||||
// slots of the declared stride, flags one word per (block, source rank), seq a handshake and a
|
||||
// parity word per block of capacity -- checked against the allocations, not the arguments.
|
||||
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, "all_gather", "my_scratch");
|
||||
r4d_ar_check_extent(my_flags, flag_bytes, "all_gather", "my_flags");
|
||||
r4d_ar_check_extent(seq_ctrs, 2L * R4D_AR_WIDE_MAX_BLOCKS * (long)sizeof(unsigned int),
|
||||
"all_gather", "seq_ctrs");
|
||||
for (int i = 0; i < NPEERS; ++i) {
|
||||
r4d_ar_check_extent(peer_scratch[i], slot_bytes, "all_gather", "peer scratch");
|
||||
r4d_ar_check_extent(peer_flags[i], flag_bytes, "all_gather", "peer flags");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
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, long peer_flags2,
|
||||
long peer_flags3, long peer_flags4, long peer_flags5, long peer_flags6,
|
||||
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) {
|
||||
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,
|
||||
peer_flags4, peer_flags5, peer_flags6};
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// R4D attention at SIX queries per KV head, head_dim 256 -- the same QSA layers of
|
||||
// Qwen3.8-Flash-Next that r4d_attn_paged_h256_gqa12.hip serves at two ranks, served at FOUR: the
|
||||
// two KV heads are replicated across the rank pairs, so each rank answers 6 queries for the one KV
|
||||
// head it holds and the ratio the constraint table gates on is 6 rather than 12.
|
||||
//
|
||||
// Four lines of geometry and the dispatch itself, because r4d_attn_paged_gqa8.hip's kernels are
|
||||
// templates on `gqa` and that unit is the parameterised body. What the ratio changes is
|
||||
// exactly two bounds, and both are in the ROW table rather than here:
|
||||
//
|
||||
// the decode verify width 64 / gqa = 10, against 5 at gqa 12 and 8 at gqa 8. A workgroup holds
|
||||
// 64 (query, head) rows and carries q_len * gqa of them, so this is
|
||||
// the WIDEST of the three legs: MTP's K=3 verify is q_len 4 and fits
|
||||
// either ratio, and a draft block of six or seven positions -- which
|
||||
// the gqa12 leg has to send to the prefill kernel -- fits the decode
|
||||
// kernel here.
|
||||
// a prefill block's width 384 / gqa = 64 query positions, against 32 at gqa 12 and 48 at
|
||||
// gqa 8.
|
||||
//
|
||||
// NO head_dim 128 LEG, for the gqa12 file's reason: nothing in scope is gqa 6 at 128, and an
|
||||
// instantiation nothing resolves is a kernel nobody has run. R4D_ATTN_H128 leaves those four entry
|
||||
// points out of this translation unit entirely rather than shipping them untested.
|
||||
#define R4D_ATTN_GQA 6
|
||||
#define R4D_ATTN_H128 0
|
||||
// THE KV PAGE IS FOUR TOKENS HERE, AND IT IS THE ONLY GEOMETRY IN THIS LIBRARY WHERE IT IS NOT 16.
|
||||
//
|
||||
// Qwen3.8-Flash-Next's QSA indexer selects 4-TOKEN BLOCKS of the key history. With a page of 4 a
|
||||
// selected block IS a page, so the selection is a paged block table and the sparse attention is
|
||||
// this kernel with a different table -- instead of a new attention kernel, which is the most
|
||||
// expensive kind of kernel in this tree to get right. docs/QSA.md is the decomposition. The page
|
||||
// is 4 at this ratio for exactly the reason it is 4 at gqa 12 -- the model's page, not the
|
||||
// parallelism's; what TP4 changes is the queries a head, not the storage.
|
||||
//
|
||||
// IT COSTS NOTHING. A page size is pure storage, so the only thing it can move is the block-table
|
||||
// walk -- `NB = TILE / BS` entries a tile instead of one, four instead of one at TILE 16 -- and at
|
||||
// a realistic context that is inside the noise of the step, with identical output either way.
|
||||
//
|
||||
// THE ROWS HAVE TO AGREE. `C_EQ("block_size", 4)` on all four gqa6-bs4 rows is what makes the core
|
||||
// size the pages at 4 (RadKVGroupDecl carries no block size on purpose -- it is taken off the
|
||||
// resolved kernel). A row that says 16 against this define is a kernel that refuses at issue
|
||||
// rather than at declare.
|
||||
#define G8_BLOCK_SIZE 4
|
||||
|
||||
#include "r4d_attn_paged_gqa8.hip"
|
||||
@@ -119,6 +119,18 @@ int r4d_rad_attn_decode_gq_h256_gqa12_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_gq_h256_gqa12_bf16_describe(const RadArgs*, RadStream, RadLaunchDesc*,
|
||||
int, int*);
|
||||
int64_t r4d_rad_attn_decode_h256_gqa12_scratch(const RadArgs*);
|
||||
/* Six queries a KV head, head_dim 256, paged block 4 -- the same QSA layers at four ranks, where
|
||||
* the KV head is replicated across a rank pair. Same body again; the verify width is 64/6 = 10
|
||||
* against gqa12's 5, which is the decode rows' constraint value. */
|
||||
int r4d_rad_attn_prefill_h256_gqa6_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h256_gqa6_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa6_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa6_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*,
|
||||
int, int*);
|
||||
int r4d_rad_attn_decode_h256_gqa6_bf16 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa6_bf16_describe(const RadArgs*, RadStream, RadLaunchDesc*,
|
||||
int, int*);
|
||||
int64_t r4d_rad_attn_decode_h256_gqa6_scratch(const RadArgs*);
|
||||
|
||||
/* -- gated delta net (r4d_gdn_*.hip) -- */
|
||||
int r4d_rad_gdn_chunk_scan (const RadArgs*, RadStream);
|
||||
@@ -149,6 +161,9 @@ int r4d_rad_ar_init(const RadParam*, int, int rank, int world, void** out);
|
||||
void r4d_rad_ar_fini(void*);
|
||||
int r4d_rad_ar_oneshot_2rank_exact(const RadArgs*, RadStream);
|
||||
int r4d_rad_ar_gather_2rank (const RadArgs*, RadStream);
|
||||
/* The gather at a wide group's handshake (r4d_ar_gather_Nrank.hip launches it). */
|
||||
int r4d_rad_ar_gather_4rank (const RadArgs*, RadStream);
|
||||
int r4d_rad_ar_gather_8rank (const RadArgs*, RadStream);
|
||||
int r4d_rad_ar_oneshot_2rank_wht6 (const RadArgs*, RadStream);
|
||||
int r4d_rad_ar_oneshot_4rank_exact(const RadArgs*, RadStream);
|
||||
int r4d_rad_ar_oneshot_8rank_exact(const RadArgs*, RadStream);
|
||||
|
||||
@@ -2206,6 +2206,35 @@ static const RadConstraint cAttnDecodeGqH256G12Bf16[] = {
|
||||
C_GE("causal", 0), C_LE("q_len", 5), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
C_EQ("group", 128), C_IN("act", "sigmoid"),
|
||||
};
|
||||
/* SIX QUERIES PER KV HEAD, head_dim 256, paged block 4 (r4d_attn_paged_h256_gqa6_bs4.hip) -- the
|
||||
* SAME QSA layers as the gqa12 leg above, at four ranks. The model has two KV heads; a world of
|
||||
* four replicates each across a rank pair, so a rank declares 6 queries over the 1 kv head it
|
||||
* holds and `gqa` is 6 where the TP2 leg reads 12. What the ratio changes is the two bounds, as
|
||||
* ever: `q_len <= 10` is the same `q_len * gqa <= 64` at 6 queries a position rather than 12, so
|
||||
* this leg's bucket boundary sits where the plain h256/gqa6 leg's does, and a draft block of six or
|
||||
* seven positions -- which gqa12 hands to the prefill kernel -- is a decode launch here. The page
|
||||
* stays 4 because it is the model's indexer's block size, not a consequence of the parallelism.
|
||||
*
|
||||
* NO `attn_paged_gate_quant` ROW AT THIS GEOMETRY. The body instantiates the `_gq` entry points at
|
||||
* every ratio, but v1 of this port registers none of them as a row, so the sparse path's gate and
|
||||
* quantiser resolve to the standalone pair declared beside the probe. That is the design, not an
|
||||
* omission. */
|
||||
static const RadConstraint cAttnPrefillH256G6Bs4Fp8[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 4),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
static const RadConstraint cAttnPrefillH256G6Bs4Bf16[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 4),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeH256G6Bs4Fp8[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 4),
|
||||
C_GE("causal", 0), C_LE("q_len", 10), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeH256G6Bs4Bf16[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 4),
|
||||
C_GE("causal", 0), C_LE("q_len", 10), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
};
|
||||
static const RadConstraint cAttnPrefillH128G8Fp8[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 8), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
@@ -2304,6 +2333,14 @@ static const RadConstraint cArGather2[] = {
|
||||
C_EQ("world_size", 2), C_IN("dtype", "bf16 fp16 f16 fp32 f32"),
|
||||
C_P2P,
|
||||
};
|
||||
/* The same gather at a width whose handshake is the wide family's tree and not one peer flag. The
|
||||
* dtype set is cArGather2's verbatim for the same reason -- the two callers' synonyms. */
|
||||
static const RadConstraint cArGather4[] = {
|
||||
C_EQ("world_size", 4), C_IN("dtype", "bf16 fp16 f16 fp32 f32"),
|
||||
};
|
||||
static const RadConstraint cArGather8[] = {
|
||||
C_EQ("world_size", 8), C_IN("dtype", "bf16 fp16 f16 fp32 f32"),
|
||||
};
|
||||
|
||||
static const RadConstraint cArExact[] = {
|
||||
C_EQ("world_size", 2), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
||||
@@ -3530,6 +3567,48 @@ static const RadKernelInfo kKernels[] = {
|
||||
r4d_shape_attn_paged_gate_quant, nullptr, r4d_fuse_attn_paged_gate_quant,
|
||||
r4d_rad_attn_decode_gq_h256_gqa12_bf16_describe },
|
||||
|
||||
/* The TP4 leg of Flash-Next's sparse attention: the same kernels the gqa12 rows above resolve, at
|
||||
* the ratio a four-rank world hands a rank (see cAttnPrefillH256G6Bs4Fp8). The two are not
|
||||
* alternatives -- `gqa` is a per-rank parameter, so world 2 resolves gqa 12 and these four rows
|
||||
* never win there, and world 4 resolves gqa 6 and the gqa12 rows never win. */
|
||||
{ "attn_prefill_h256_gqa6_bs4_fp8kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 4, varlen",
|
||||
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
|
||||
ROW(cAttnPrefillH256G6Bs4Fp8), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8, nullptr, nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_prefill_h256_gqa6_bs4_bf16kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 4, varlen",
|
||||
"bf16 query, bf16 kv cache, bf16 out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
|
||||
ROW(cAttnPrefillH256G6Bs4Bf16), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_bf16, nullptr, nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_decode_h256_gqa6_bs4_fp8kv", "attn_paged", "attn",
|
||||
"paged attention, causal or not, split-KV (decode / speculative verify / draft block)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 4, q_len*gqa <= 64, no window",
|
||||
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnDecodeH256G6Bs4Fp8), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_decode_h256_gqa6_fp8, r4d_rad_attn_decode_h256_gqa6_scratch,
|
||||
nullptr, nullptr,
|
||||
r4d_shape_attn_paged, nullptr, nullptr, r4d_rad_attn_decode_h256_gqa6_fp8_describe },
|
||||
|
||||
{ "attn_decode_h256_gqa6_bs4_bf16kv", "attn_paged", "attn",
|
||||
"paged attention, causal or not, split-KV (decode / speculative verify / draft block)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 4, q_len*gqa <= 64, no window",
|
||||
"bf16 query, bf16 kv cache, bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnDecodeH256G6Bs4Bf16), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_decode_h256_gqa6_bf16, r4d_rad_attn_decode_h256_gqa6_scratch,
|
||||
nullptr, nullptr,
|
||||
r4d_shape_attn_paged, nullptr, nullptr, r4d_rad_attn_decode_h256_gqa6_bf16_describe },
|
||||
|
||||
{ "attn_prefill_h128_gqa8_fp8kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 128, 8 queries per kv head, paged block 16, varlen",
|
||||
@@ -3750,6 +3829,30 @@ static const RadKernelInfo kKernels[] = {
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_gather_2rank, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_gather },
|
||||
|
||||
{ "ar_gather_4rank", "all_gather", "ar",
|
||||
"all-gather, one-shot push over P2P IPC scratch, per-block handshake",
|
||||
"exactly 4 ranks, all peer-accessible; y is [4 * numel] with rank r's input at r * numel; one "
|
||||
"scratch slot per (parity, source rank); shares the wide family's flag/seq/scratch set; "
|
||||
"cudagraph-safe",
|
||||
"any payload -- the bytes are moved, never summed, so the (u32 id, f32 logit) pairs the "
|
||||
"vocab-parallel sampler gathers survive intact",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT,
|
||||
ROW(cArGather4), ROW(tAr), nullptr,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_gather_4rank, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_gather },
|
||||
|
||||
{ "ar_gather_8rank", "all_gather", "ar",
|
||||
"all-gather, one-shot push over P2P IPC scratch, per-block handshake",
|
||||
"exactly 8 ranks, all peer-accessible; y is [8 * numel] with rank r's input at r * numel; one "
|
||||
"scratch slot per (parity, source rank); shares the wide family's flag/seq/scratch set; "
|
||||
"cudagraph-safe",
|
||||
"any payload -- the bytes are moved, never summed, so the (u32 id, f32 logit) pairs the "
|
||||
"vocab-parallel sampler gathers survive intact",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT,
|
||||
ROW(cArGather8), ROW(tAr), nullptr,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_gather_8rank, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_gather },
|
||||
|
||||
{ "ar_oneshot_2rank_exact", "all_reduce", "ar",
|
||||
"all-reduce (sum), one-shot push over P2P IPC scratch",
|
||||
"exactly 2 ranks, peer-accessible; scratch sized by the full message; cudagraph-safe",
|
||||
|
||||
Reference in new issue
Block a user