Archived
radiance 1.14.0
New models and formats: - libr3d: a kernel library for RDNA3 (gfx11) cards - nomic-embed-text-v2-moe (the nomicbert plugin) - Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16 - DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and rad-convert --chat-template for a release that ships no template - GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe - Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8 Speed: - hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined - gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3 us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us) - Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 -> 19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 -> 40.3 ms a step) - Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms, coalesced one-token expert GEMMs - spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch) Speculation: - DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on) - A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with a block-16 tree that runs its short block under load Features: - Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and --expert-stats FILE for the routed experts each layer selects - --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards - Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for tip-only one drafter window below the tip; hits on models whose KV groups have different block sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib - Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the server does - MiniMax-H3: keyframes ride a latent upscale - Gemma 3's fused all-reduce junction on the lossy wire Fixes: - A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the engine with a scratch refusal at issue (#293) - --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link twice (#323) - A chat template that cannot render a conversation is refused at load, not mid-request - imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32) - --expert-profile refuses a profile recorded on another model - hc_read's one-grid wait traps past its deadline instead of reading a stale value Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
This commit is contained in:
1 parent
481fcd5df0
commit
4a61d7296b
160 files changed
+8790
-6819
No files matched your search
@@ -198,6 +198,7 @@ set(R4D_UNITS
|
||||
"r4d_lora_bf16|"
|
||||
"r4d_dflash_conv_t2_g16_bf16|"
|
||||
"r4d_dflash_select_bf16|"
|
||||
"r4d_dflash_tree_bf16|"
|
||||
# Routed MoE: router, scatter, the grouped block-scaled fp8 GEMM and gather, in one unit because
|
||||
# the other three only feed the GEMM.
|
||||
"r4d_moe|"
|
||||
|
||||
+24
-12
@@ -82,6 +82,10 @@ struct R4DArgs {
|
||||
// The engine's chained launch (RadKernelChainFn, r4d_chain.h): the decode path launches its
|
||||
// chained forms. fill_paged sets it from the resolved `chain` parameter.
|
||||
int chain;
|
||||
// A tree verify (attn_paged_qrow_tree, docs/OPS.md): RadBatch::spec_parent, one int32 a query
|
||||
// row, -2 for an ordinary causal row, else the row's parent's index within its sequence's rows
|
||||
// (-1 for the block's first). Read only by the O_TREE instances; null elsewhere.
|
||||
const int* spec_parent;
|
||||
};
|
||||
|
||||
// Rows of q and out. The split-KV scratch is laid out by dense query row, so the host sizing hook
|
||||
@@ -129,6 +133,9 @@ long r4d_attn_decode_h128_gqa2w_scratch_bytes(const R4DArgs* a);
|
||||
// ...with q at R4DArgs::q_ld (attn_paged_qrow).
|
||||
int r4d_attn_decode_h256_gqa2_qld_fp8kv (const R4DArgs* a, hipStream_t stream);
|
||||
int r4d_attn_decode_h256_gqa2_qld_bf16kv (const R4DArgs* a, hipStream_t stream);
|
||||
// ...and a tree verify's mask from R4DArgs::spec_parent (attn_paged_qrow_tree).
|
||||
int r4d_attn_decode_h256_gqa2_qld_tree_fp8kv (const R4DArgs* a, hipStream_t stream);
|
||||
int r4d_attn_decode_h256_gqa2_qld_tree_bf16kv (const R4DArgs* a, hipStream_t stream);
|
||||
// The geometry the attention kernels above are compiled for, so a caller can check a model
|
||||
// before the first launch.
|
||||
void r4d_attn_dims(int* head_dim, int* gqa, int* block_size, int* max_decode_rows);
|
||||
@@ -291,18 +298,22 @@ void r4d_ar_ipc_enable_peer(long peer);
|
||||
// VRAM. Zeroed; the address is valid on every card and the CPU. Startup-only.
|
||||
long r4d_ar_host_alloc(long size);
|
||||
void r4d_ar_host_free(long p);
|
||||
// `stage`, the last argument of every 2-rank kernel below: 0, or a host-mailbox instance's mirror
|
||||
// of its slots in this card's VRAM (same layout), which the reader's push check fills as it reads
|
||||
// the slot so the reduce reads VRAM instead of the link a second time (r4d_ar_seq.h).
|
||||
// Exact sum, fp32 accumulate, bf16 / fp16 / fp32 payload (dtype 0 / 1 / 2).
|
||||
void r4d_ar_oneshot_2rank_exact(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 stream, long nblocks, long nthreads, long drain,
|
||||
long acq);
|
||||
long acq, long stage = 0);
|
||||
// Same topology, with the wire payload Walsh-Hadamard rotated and quantised to 6 bits per element
|
||||
// in groups of 64 (plus a bf16 scale per group). Lossy; bf16 / fp16 payload only. loc_pack is this
|
||||
// rank's own packed copy, so the reduce folds exactly the bytes it sent.
|
||||
void r4d_ar_oneshot_2rank_wht6(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
long seq_ctrs, long loc_pack, long slot_stride_bytes,
|
||||
long scale_off_bytes, long inp, long out, long n_elem, long dtype,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq);
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long stage = 0);
|
||||
// The exact all-reduce of `y` [M][n] bf16 with hc_write as its epilogue: h[r][c*n + i] +=
|
||||
// inj[r][c] * y[r][i] over the reduced y, byte-identical to all_reduce then hc_write. `irow` and
|
||||
// `hrow` are the pitches of inj [M][hc] and h [M][hc*n] in elements; y is tight. A non-zero `gate`
|
||||
@@ -311,7 +322,7 @@ void r4d_ar_oneshot_2rank_wht6(long peer_scratch, long my_scratch, long peer_fla
|
||||
void r4d_ar_hc_write(long peer_scratch, long my_scratch, long peer_flags, 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 stream, long nblocks, long nthreads,
|
||||
long drain, long acq, long gate, long gate_ctr);
|
||||
long drain, long acq, long gate, long gate_ctr, long stage = 0);
|
||||
// The same with the MoE gather as its prologue: y is produced, not read, from the routed experts'
|
||||
// rows `ye` [M*top_k][ye_ld] summed to token order by weights `ew` [M][ew_ld] and `sorted`
|
||||
// [M*top_k], with the shared arm `sh` [M][sh_ld] and its gate `sg` folded when both are non-zero.
|
||||
@@ -323,7 +334,8 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags,
|
||||
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 stream, long nblocks, long nthreads,
|
||||
long drain, long acq, long inv, long gate, long gate_ctr);
|
||||
long drain, long acq, long inv, long gate, long gate_ctr,
|
||||
long stage = 0);
|
||||
// r4d_ar_hc_write and r4d_ar_gather_hc_write on the rotated 6-bit wire (r4d_ar_oneshot_2rank_wht6's
|
||||
// format, loc_pack and scratch split): byte-identical to all_reduce on that wire then hc_write,
|
||||
// with `ye` non-zero preceded by moe_gather. Gather operands as r4d_ar_gather_hc_write; its slot
|
||||
@@ -336,7 +348,7 @@ int r4d_ar_hc_write_wht6(long peer_scratch, long my_scratch, long peer_flags, l
|
||||
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 stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long gate, long gate_ctr);
|
||||
long gate, long gate_ctr, long stage = 0);
|
||||
int r4d_ar_max_blocks(void); // the two 2-rank kernels
|
||||
void r4d_ar_wht6_dims(int* group, int* bits, int* chunk_elems); // rotated-6-bit payload only
|
||||
|
||||
@@ -349,7 +361,7 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
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 dpitch,
|
||||
long spitch);
|
||||
long spitch, long stage = 0);
|
||||
|
||||
// ---- 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
|
||||
@@ -703,8 +715,8 @@ void r4d_moe_gemm_pin_down_dec(int pin);
|
||||
void r4d_hc_read_pin_prefill(int form);
|
||||
// Test hook: pins the gated residual read's decode kernels. 0 is the launcher's rule (kernels A and
|
||||
// B as one grid at one to seven rows where the per-stream counters exist, the tiled up-mix's K
|
||||
// passes pipelined from eight rows), 1 the kernels the rule replaced (A, then the up-mix in a second
|
||||
// launch; the tiled up-mix's passes in sequence). Bit-identical; r4d_selftest compares their bytes
|
||||
// passes pipelined from eight rows), 1 the two-grid form (A, then the up-mix in a second launch;
|
||||
// the tiled up-mix's passes unpipelined). Bit-identical; r4d_selftest compares their bytes
|
||||
// and --perfhc times both.
|
||||
void r4d_hc_read_pin_decode(int form);
|
||||
// Test hook: pins the MLA matrix row's key load. 0 is the launcher's rule (sixteen-byte vectors
|
||||
@@ -821,7 +833,7 @@ int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long peer_flag
|
||||
int M, int n, float eps, float wadd, int w_f32,
|
||||
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
||||
int nblocks, int nthreads, int drain, int acq, int wire,
|
||||
long loc_pack, long loc_scale_off, long wp = 0);
|
||||
long loc_pack, long loc_scale_off, long wp = 0, long stage = 0);
|
||||
int r4d_ar_rmsnorm_quant_fp8_chunk_elems(void);
|
||||
// Its rotated int8 form, as r4d_rmsnorm_had_quant_i8g: the reduction and the norm unchanged, the
|
||||
// norm's pass quantised by had_quant_act_i8g's rule. No post-norm.
|
||||
@@ -831,7 +843,7 @@ int r4d_ar_rmsnorm_had_quant_i8g(long peer_scratch, long my_scratch, long peer_
|
||||
int M, int n, float eps, float wadd, int w_f32,
|
||||
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
||||
int nblocks, int nthreads, int drain, int acq, int wire,
|
||||
long loc_pack, long loc_scale_off);
|
||||
long loc_pack, long loc_scale_off, long stage = 0);
|
||||
|
||||
// The same IPC with the sandwich-norm junction after it (r4d_rmsnorm_quant_i8.hip): post-norm of
|
||||
// the reduced `x` (`wp`), the residual add, the [1] scalar (`sc`), the norm (`w`) and the w4a8
|
||||
@@ -840,7 +852,7 @@ int r4d_ar_rmsnorm_quant_i8(long peer_scratch, long my_scratch, long peer_flags
|
||||
long seq_ctrs, long slot_stride16, long x, long res, long w, long wp,
|
||||
long sc, int sc_f32, long y, long q, long s, int M, int n, float eps,
|
||||
float wadd, int w_f32, int r_ld, int y_ld, long stream, int nblocks,
|
||||
int drain, int acq);
|
||||
int drain, int acq, long stage = 0);
|
||||
// ...and at four ranks, on the wide family's transport: r4d_ar_oneshot_4rank_exact's IPC
|
||||
// arguments (peers in ascending rank, this rank's own, the counters, the slot stride), `pub`, the
|
||||
// engine's link gate with the instance's landed counter (0 for none; r4d_ar_seq.h), and, with
|
||||
@@ -913,7 +925,7 @@ void r4d_ar_ln_had_quant_i8(long peer_scratch, long my_scratch, long peer_flags,
|
||||
long x, long residual, long wgain, long out_bf16, long q, long s,
|
||||
int M, int K, int had, float eps, float wadd,
|
||||
long stream, int nblocks, int nthreads, int drain, int acq,
|
||||
int wire = 0);
|
||||
int wire = 0, long stage = 0);
|
||||
// An elementwise gated product, then the same Hadamard and int8 quantiser. Mode 0 is silu(a)*b
|
||||
// (down_proj's input, vLLM's silu_and_mul); mode 1 is sigmoid(a)*b (o_proj's input, since
|
||||
// Qwen3_5Attention computes attn_output * sigmoid(gate)). Strides are in elements and may be 0 for
|
||||
|
||||
+30
-12
@@ -99,6 +99,8 @@ int rv_barrier(int world) {
|
||||
struct ArInst {
|
||||
int rank = 0, world = 0;
|
||||
long my_scratch = 0, my_flags = 0, my_seq = 0, locpack = 0;
|
||||
/* A 2-rank host-mailbox instance's VRAM mirror of its slots, 0 otherwise (r4d_ar_seq.h). */
|
||||
long my_stage = 0;
|
||||
long peer_scratch[R4D_MAX_PEERS] = { 0 };
|
||||
long peer_flags [R4D_MAX_PEERS] = { 0 };
|
||||
long slot_bytes = 0; /* per-slot capacity, 16-byte aligned */
|
||||
@@ -350,6 +352,17 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code,
|
||||
}
|
||||
sq = r4d_ar_ipc_alloc(kSeqWords(maxb) * 4, 0, handle, &hlen));
|
||||
|
||||
/* A host mailbox is read over the link past every cache, so the 2-rank kernels' push check
|
||||
* copies what it reads into this VRAM mirror for the reduce, crossing the link once
|
||||
* (r4d_ar_seq.h). Coarse-grained: only this rank's kernels touch it, each block its own words.
|
||||
* The wide family has no push check and reads its slots once. Host-memory slots free the same
|
||||
* VRAM over P2P, so the mirror keeps the footprint where the P2P instance has it. */
|
||||
long stg = 0;
|
||||
if (host && world == 2) {
|
||||
R4D_TRY("ar_ipc_alloc(stage)",
|
||||
stg = r4d_ar_ipc_alloc((long)nslots * in->slot_bytes, 0, handle, &hlen));
|
||||
}
|
||||
|
||||
/* This rank's own packed copy, for the rotated 6-bit wire only: the reduce must fold exactly
|
||||
* the bytes it sent. The 6-bit payload is 3/8 of the bf16 message and the f32 scales 1/32 of
|
||||
* it, laid out from half the slot; a row padded to whole 512-element chunks
|
||||
@@ -365,6 +378,7 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code,
|
||||
in->my_flags = fl;
|
||||
in->my_seq = sq;
|
||||
in->locpack = lp;
|
||||
in->my_stage = stg;
|
||||
in->gate_ctr = sq + 2L * maxb * 4; /* the word after the sequence counters (r4d_ar_seq.h) */
|
||||
|
||||
{
|
||||
@@ -475,6 +489,7 @@ void release(ArInst* in) {
|
||||
}
|
||||
r4d_ar_ipc_free(in->my_seq);
|
||||
r4d_ar_ipc_free(in->locpack);
|
||||
r4d_ar_ipc_free(in->my_stage);
|
||||
delete in;
|
||||
}
|
||||
|
||||
@@ -799,7 +814,7 @@ extern "C" int r4d_rad_ar_gather_2rank(const RadArgs* a, RadStream s) {
|
||||
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, (long)dpitch, (long)spitch));
|
||||
c.acq, (long)dpitch, (long)spitch, in->my_stage));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -869,7 +884,8 @@ extern "C" int r4d_rad_ar_oneshot_2rank_exact(const RadArgs* a, RadStream s) {
|
||||
r4d_ar_oneshot_2rank_exact(c.in->peer_scratch[0], c.in->my_scratch,
|
||||
c.in->peer_flags[0], c.in->my_flags, c.in->my_seq,
|
||||
c.in->slot16, c.inp, c.out, c.numel, c.in->dtype,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq,
|
||||
c.in->my_stage));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -911,7 +927,8 @@ extern "C" int r4d_rad_ar_oneshot_2rank_wht6(const RadArgs* a, RadStream s) {
|
||||
r4d_ar_oneshot_2rank_exact(c.in->peer_scratch[0], c.in->my_scratch,
|
||||
c.in->peer_flags[0], c.in->my_flags, c.in->my_seq,
|
||||
c.in->slot16, c.inp, c.out, c.numel, c.in->dtype,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq,
|
||||
c.in->my_stage));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -928,7 +945,8 @@ extern "C" int r4d_rad_ar_oneshot_2rank_wht6(const RadArgs* a, RadStream s) {
|
||||
c.in->peer_flags[0], c.in->my_flags, c.in->my_seq,
|
||||
c.in->locpack, c.in->slot_bytes, c.in->slot_bytes / 2,
|
||||
c.inp, c.out, c.numel, c.in->dtype,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq,
|
||||
c.in->my_stage));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -1283,7 +1301,7 @@ extern "C" int r4d_rad_ar_ln_had_quant_i8(const RadArgs* a, RadStream s) {
|
||||
r4d_addr(q), r4d_addr(sc),
|
||||
(int)M, (int)K, (int)had, eps, wadd,
|
||||
(long)(uintptr_t)s, nblocks, nthreads, cfgv.drain, cfgv.acq,
|
||||
wire));
|
||||
wire, in->my_stage));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -1404,7 +1422,7 @@ static int rad_ar_rmsnorm_quant(const RadArgs* a, RadStream s, bool hi8) {
|
||||
r4d_addr(sc), (int)M, (int)K, eps, wadd, w_f32,
|
||||
(int)rp, (int)yp, (int)qp, (int)sp,
|
||||
(long)(uintptr_t)s, nblocks, nthreads, cfgv.drain, cfgv.acq, wire,
|
||||
in->locpack, in->slot_bytes / 2)
|
||||
in->locpack, in->slot_bytes / 2, in->my_stage)
|
||||
: r4d_ar_rmsnorm_quant_fp8(
|
||||
in->peer_scratch[0], in->my_scratch,
|
||||
in->peer_flags[0], in->my_flags, in->my_seq,
|
||||
@@ -1412,7 +1430,7 @@ static int rad_ar_rmsnorm_quant(const RadArgs* a, RadStream s, bool hi8) {
|
||||
r4d_addr(sc), (int)M, (int)K, eps, wadd, w_f32,
|
||||
(int)rp, (int)yp, (int)qp, (int)sp,
|
||||
(long)(uintptr_t)s, nblocks, nthreads, cfgv.drain, cfgv.acq, wire,
|
||||
in->locpack, in->slot_bytes / 2, wpo ? r4d_addr(wpo) : 0);
|
||||
in->locpack, in->slot_bytes / 2, wpo ? r4d_addr(wpo) : 0, in->my_stage);
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
@@ -1491,7 +1509,7 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_i8(const RadArgs* a, RadStream s) {
|
||||
v.sl ? (v.sl->dtype == RAD_F32) : 0, r4d_addr(v.ob), r4d_addr(v.q), r4d_addr(v.sc), (int)M,
|
||||
(int)K, (float)r4d_getf(a, "eps", 1e-6), (float)r4d_getf(a, "wadd", 0.0),
|
||||
v.w->dtype == RAD_F32, (int)v.rp, (int)v.yp, (long)(uintptr_t)s, nblocks, cfgv.drain,
|
||||
cfgv.acq);
|
||||
cfgv.acq, in->my_stage);
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
@@ -1672,7 +1690,7 @@ extern "C" int r4d_rad_ar_gather_hc_write(const RadArgs* a, RadStream s) {
|
||||
r4d_addr(ew), r4d_addr(so), sh ? r4d_addr(sh) : 0L, sh ? r4d_addr(sg) : 0L,
|
||||
(long)ye_ld, (long)ew_ld, (long)sh_ld, (long)sg_ld, (long)top_k, (long)sig,
|
||||
(long)(uintptr_t)s, nblocks, R4D_AR_WHT6_FUSED_THREADS, cfgv.drain, cfgv.acq,
|
||||
early, early ? in->gate_ctr : 0L);
|
||||
early, early ? in->gate_ctr : 0L, in->my_stage);
|
||||
if (rc != 0) return RAD_E_SHAPE;
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
@@ -1687,7 +1705,7 @@ extern "C" int r4d_rad_ar_gather_hc_write(const RadArgs* a, RadStream s) {
|
||||
sh ? r4d_addr(sh) : 0L, sh ? r4d_addr(sg) : 0L, (long)ye_ld, (long)ew_ld, (long)sh_ld,
|
||||
(long)sg_ld, (long)top_k, (long)sig, (long)(uintptr_t)s, nblocks, cfgv.nthreads,
|
||||
cfgv.drain, cfgv.acq, has_inv ? (long)(uintptr_t)a->scratch : 0L, early,
|
||||
early ? in->gate_ctr : 0L);
|
||||
early ? in->gate_ctr : 0L, in->my_stage);
|
||||
if (rc != 0) return RAD_E_SHAPE;
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
@@ -1803,7 +1821,7 @@ extern "C" int r4d_rad_ar_hc_write(const RadArgs* a, RadStream s) {
|
||||
in->locpack, in->slot_bytes, in->slot_bytes / 2, r4d_addr(y), r4d_addr(inj),
|
||||
r4d_addr(h), (long)M, (long)n, (long)hc, (long)irow, (long)hrow, 0L, 0L, 0L, 0L, 0L,
|
||||
0L, 0L, 0L, 0L, 0L, 0L, (long)(uintptr_t)s, nblocks, R4D_AR_WHT6_FUSED_THREADS,
|
||||
cfgv.drain, cfgv.acq, early, early ? in->gate_ctr : 0L);
|
||||
cfgv.drain, cfgv.acq, early, early ? in->gate_ctr : 0L, in->my_stage);
|
||||
if (rc != 0) return RAD_E_SHAPE;
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
@@ -1816,7 +1834,7 @@ extern "C" int r4d_rad_ar_hc_write(const RadArgs* a, RadStream s) {
|
||||
in->my_seq, in->slot16, r4d_addr(y), r4d_addr(inj), r4d_addr(h),
|
||||
(long)M, (long)n, (long)hc, (long)irow, (long)hrow, (long)(uintptr_t)s,
|
||||
nblocks, cfgv.nthreads, cfgv.drain, cfgv.acq, early,
|
||||
early ? in->gate_ctr : 0L));
|
||||
early ? in->gate_ctr : 0L, in->my_stage));
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ __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 dp16, int sp16, int drain, int acq,
|
||||
unsigned int nbw) {
|
||||
unsigned int nbw, long stage) {
|
||||
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,6 +68,7 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
const int slot = (int)s_slot;
|
||||
int4* ps = peer_base + (size_t)slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
|
||||
const int w = (n16 + nb - 1) / nb;
|
||||
const int start = b * w;
|
||||
@@ -116,8 +117,10 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
__syncthreads();
|
||||
if (acq) __threadfence_system();
|
||||
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
|
||||
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
|
||||
for (int k = start + tid; k < end; k += nt)
|
||||
h += r4d_archk_u4(r4d_archk_keep(&ms[k], md), 4u * (unsigned)k);
|
||||
});
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
|
||||
// Place the peer's slice, now staged in this rank's scratch.
|
||||
{
|
||||
@@ -136,7 +139,8 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
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 dpitch, long spitch) {
|
||||
long nthreads, long drain, long acq, long dpitch, long spitch,
|
||||
long stage) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
const int esize = (dtype == 2) ? 4 : 2;
|
||||
const long nbytes = (long)n_elem * esize;
|
||||
@@ -177,5 +181,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, dp16, sp16, (int)drain, (int)acq, nbw);
|
||||
(int)slot_stride16, (int)rank, row16, dp16, sp16, (int)drain, (int)acq, nbw, stage);
|
||||
}
|
||||
@@ -125,7 +125,7 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel(
|
||||
int4* peer_base, const int4* my_base,
|
||||
unsigned int* peer_flags, unsigned int* my_flags, unsigned int* seq_ctrs,
|
||||
int M, int K, int slot_stride16, int drain, int acq,
|
||||
float eps, float wadd, float rnorm, unsigned int nbw) {
|
||||
float eps, float wadd, float rnorm, unsigned int nbw, long stage) {
|
||||
constexpr int VPL = HAD / 32, NW = THR / 32;
|
||||
constexpr int MAXB = R4D_ARN_MAXVALS / VPL;
|
||||
extern __shared__ float lds[];
|
||||
@@ -146,6 +146,7 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel(
|
||||
const unsigned int sq = s_seq;
|
||||
int4* ps = peer_base + (size_t)s_slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)s_slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
|
||||
// WIRE == 6 only. The slot is split as in the standalone compressed all-reduce: packed symbols
|
||||
// from the base, bf16 group scales from halfway. Chunk indices are global over the [M, K]
|
||||
@@ -263,7 +264,8 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel(
|
||||
r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, sq, [&](unsigned int& h) {
|
||||
if constexpr (WIRE == 0) {
|
||||
const int wpr = K >> 3;
|
||||
for (int k = r0 * wpr + tid; k < r1 * wpr; k += THR) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
|
||||
for (int k = r0 * wpr + tid; k < r1 * wpr; k += THR)
|
||||
h += r4d_archk_u4(r4d_archk_keep(&ms[k], md), 4u * (unsigned)k);
|
||||
} else {
|
||||
for (int ci = wave; ci < cpb; ci += NW) {
|
||||
const int c = c00 + ci;
|
||||
@@ -271,25 +273,32 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel(
|
||||
const unsigned int cd = (unsigned)c * (ARN_CBYTES / 4);
|
||||
#pragma unroll
|
||||
for (int w4 = 0; w4 + 4 <= ARN_NWORD; w4 += 4)
|
||||
h += r4d_archk_u4(*(const uint4*)(pb + w4 * 4 * ARN_WV + lane * 16),
|
||||
h += r4d_archk_u4(r4d_archk_keep((const uint4*)(pb + w4 * 4 * ARN_WV + lane * 16), md),
|
||||
cd + (unsigned)(w4 * ARN_WV + lane * 4));
|
||||
if constexpr (ARN_NWORD % 4 >= 2) {
|
||||
const uint2 u = *(const uint2*)(pb + (ARN_NWORD & ~3) * 4 * ARN_WV + lane * 8);
|
||||
const uint2 u =
|
||||
r4d_archk_keep((const uint2*)(pb + (ARN_NWORD & ~3) * 4 * ARN_WV + lane * 8), md);
|
||||
const unsigned int e = cd + (unsigned)((ARN_NWORD & ~3) * ARN_WV + lane * 2);
|
||||
h += r4d_archk_mix(u.x, e) + r4d_archk_mix(u.y, e + 1u);
|
||||
}
|
||||
if constexpr (ARN_NWORD % 2 == 1)
|
||||
h += r4d_archk_mix(*(const unsigned int*)(pb + (ARN_NWORD - 1) * 4 * ARN_WV + lane * 4),
|
||||
h += r4d_archk_mix(
|
||||
r4d_archk_keep((const unsigned int*)(pb + (ARN_NWORD - 1) * 4 * ARN_WV + lane * 4), md),
|
||||
cd + (unsigned)((ARN_NWORD - 1) * ARN_WV + lane));
|
||||
#pragma unroll
|
||||
for (int t = 0; t < ARN_ITERS; ++t) {
|
||||
const int gl = t * ARN_GPW + gw;
|
||||
if (lig == 0) h += r4d_archk_mix(m_sc[c * ARN_CG + gl], R4D_ARCHK_U16 + (unsigned)(c * ARN_CG + gl));
|
||||
if (lig == 0)
|
||||
h += r4d_archk_mix(r4d_archk_keep(&m_sc[c * ARN_CG + gl], md),
|
||||
R4D_ARCHK_U16 + (unsigned)(c * ARN_CG + gl));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
m_pay = r4d_archk_moved(m_pay, md);
|
||||
m_sc = r4d_archk_moved(m_sc, md);
|
||||
// The peer's rows are now in my scratch, in the same [M, K] layout as X.
|
||||
const __hip_bfloat16* P = (const __hip_bfloat16*)ms;
|
||||
|
||||
@@ -475,7 +484,8 @@ void r4d_ar_ln_had_quant_i8(long peer_scratch, long my_scratch, long peer_flags,
|
||||
long seq_ctrs, long slot_stride16,
|
||||
long x, long residual, long wgain, long out_bf16, long q, long s,
|
||||
int M, int K, int had, float eps, float wadd,
|
||||
long stream, int nblocks, int nthreads, int drain, int acq, int wire) {
|
||||
long stream, int nblocks, int nthreads, int drain, int acq, int wire,
|
||||
long stage) {
|
||||
if (K % 16) throw std::runtime_error("r4d ar_ln_had_quant_i8: K must be a multiple of 16");
|
||||
if (M < 1) throw std::runtime_error("r4d ar_ln_had_quant_i8: M must be positive");
|
||||
if (had < 2 || (had & (had - 1))) throw std::runtime_error(
|
||||
@@ -509,7 +519,7 @@ void r4d_ar_ln_had_quant_i8(long peer_scratch, long my_scratch, long peer_flags,
|
||||
(__hip_bfloat16*)residual, (const __hip_bfloat16*)wgain, (__hip_bfloat16*)out_bf16, \
|
||||
(signed char*)q, (float*)s, (int4*)peer_scratch, (const int4*)my_scratch, \
|
||||
(unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, M, K, (int)slot_stride16, \
|
||||
drain, acq, eps, wadd, rn, (unsigned)nbw)
|
||||
drain, acq, eps, wadd, rn, (unsigned)nbw, stage)
|
||||
#define R4D_ARN_T(H, W, R) switch (thr) { \
|
||||
case 128: R4D_ARN_LAUNCH(H, 128, W, R); break; \
|
||||
case 256: R4D_ARN_LAUNCH(H, 256, W, R); break; \
|
||||
|
||||
@@ -64,7 +64,7 @@ template <typename T, int LANES>
|
||||
__global__ void r4d_ar_oneshot_2rank_exact_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 drain, int acq, unsigned int nbw) {
|
||||
int n16, int slot_stride16, int drain, int acq, unsigned int nbw, long stage) {
|
||||
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
|
||||
@@ -72,6 +72,7 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
|
||||
const int slot = (int)s_slot;
|
||||
int4* ps = peer_base + (size_t)slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
|
||||
const int w = (n16 + nb - 1) / nb; // 16B words per block
|
||||
const int start = b * w;
|
||||
@@ -121,8 +122,10 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
|
||||
__syncthreads();
|
||||
if (acq) __threadfence_system();
|
||||
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
|
||||
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
|
||||
for (int k = start + tid; k < end; k += nt)
|
||||
h += r4d_archk_u4(r4d_archk_keep(&ms[k], md), 4u * (unsigned)k);
|
||||
});
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
// Reduce the chunk, 4x unrolled: out = local input + peer input (staged in local scratch).
|
||||
{
|
||||
int k = start + tid;
|
||||
@@ -149,7 +152,7 @@ __global__ void r4d_ar_hc_write_kernel(
|
||||
unsigned int* my_flags, unsigned int* 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 drain, int acq, unsigned int nbw, long long* gate,
|
||||
unsigned int* gate_ctr) {
|
||||
unsigned int* gate_ctr, long stage) {
|
||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
const long long gate_word = r4d_ar_link_word(gate, (unsigned int)tid);
|
||||
@@ -158,6 +161,7 @@ __global__ void r4d_ar_hc_write_kernel(
|
||||
const int slot = (int)s_slot;
|
||||
int4* ps = peer_base + (size_t)slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
|
||||
const int w = (n16 + nb - 1) / nb; // 16B words per block
|
||||
const int start = b * w;
|
||||
@@ -189,8 +193,10 @@ __global__ void r4d_ar_hc_write_kernel(
|
||||
__syncthreads();
|
||||
if (acq) __threadfence_system();
|
||||
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
|
||||
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
|
||||
for (int k = start + tid; k < end; k += nt)
|
||||
h += r4d_archk_u4(r4d_archk_keep(&ms[k], md), 4u * (unsigned)k);
|
||||
});
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
|
||||
|
||||
const int wpr = n >> 3; // 16B words a row
|
||||
@@ -237,7 +243,7 @@ __global__ void r4d_ar_gather_hc_write_kernel(
|
||||
unsigned int* my_flags, unsigned int* 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 drain, int acq, unsigned int nbw, R4dArGather G,
|
||||
const int* __restrict__ inv, long long* gate, unsigned int* gate_ctr) {
|
||||
const int* __restrict__ inv, long long* gate, unsigned int* gate_ctr, long stage) {
|
||||
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];
|
||||
@@ -249,6 +255,7 @@ __global__ void r4d_ar_gather_hc_write_kernel(
|
||||
const int slot = (int)s_slot;
|
||||
int4* ps = peer_base + (size_t)slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
|
||||
const int w = (n16 + nb - 1) / nb; // 16B words per block
|
||||
const int start = b * w;
|
||||
@@ -304,8 +311,10 @@ __global__ void r4d_ar_gather_hc_write_kernel(
|
||||
__syncthreads();
|
||||
if (acq) __threadfence_system();
|
||||
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
|
||||
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
|
||||
for (int k = start + tid; k < end; k += nt)
|
||||
h += r4d_archk_u4(r4d_archk_keep(&ms[k], md), 4u * (unsigned)k);
|
||||
});
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
|
||||
|
||||
for (int k = start + tid; k < end; k += nt) {
|
||||
@@ -411,7 +420,7 @@ int r4d_p2p_copy2d(long dst, long src, long dpitch, long spitch, long width, lon
|
||||
void r4d_ar_hc_write(long peer_scratch, long my_scratch, long peer_flags, 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 stream_i, long nblocks, long nthreads,
|
||||
long drain, long acq, long gate, long gate_ctr) {
|
||||
long drain, long acq, long gate, long gate_ctr, long stage) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
if (n <= 0 || M <= 0 || n % 8) throw std::runtime_error("ar_hc_write: n must be a positive multiple of 8");
|
||||
if (hc < 1 || hrow % 8) throw std::runtime_error("ar_hc_write: the streams must be whole 16B words");
|
||||
@@ -431,7 +440,7 @@ void r4d_ar_hc_write(long peer_scratch, long my_scratch, long peer_flags, long m
|
||||
(int4*)y, (int4*)peer_scratch, (const int4*)my_scratch, (unsigned*)peer_flags,
|
||||
(unsigned*)my_flags, (unsigned*)seq_ctrs, (const unsigned short*)inj, (unsigned short*)h,
|
||||
n16, (int)n, (int)hc, irow, hrow, (int)slot_stride16, (int)drain, (int)acq, nbw,
|
||||
(long long*)gate, (unsigned int*)gate_ctr);
|
||||
(long long*)gate, (unsigned int*)gate_ctr, stage);
|
||||
}
|
||||
|
||||
int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
@@ -439,7 +448,7 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags,
|
||||
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 stream_i, long nblocks, long nthreads,
|
||||
long drain, long acq, long inv, long gate, long gate_ctr) {
|
||||
long drain, long acq, long inv, long gate, long gate_ctr, long stage) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
if (n <= 0 || M <= 0 || n % 8 || hc < 1 || hrow % 8) return -1;
|
||||
if (top_k < 1 || top_k > R4D_ARG_SLOTS) return -2;
|
||||
@@ -483,13 +492,13 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags,
|
||||
(int4*)y, (int4*)peer_scratch, (const int4*)my_scratch, (unsigned*)peer_flags,
|
||||
(unsigned*)my_flags, (unsigned*)seq_ctrs, (const unsigned short*)inj, (unsigned short*)h,
|
||||
n16, (int)n, (int)hc, irow, hrow, (int)slot_stride16, (int)drain, (int)acq, nbw, G,
|
||||
(const int*)inv, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
(const int*)inv, (long long*)gate, (unsigned int*)gate_ctr, stage);
|
||||
} else {
|
||||
r4d_ar_gather_hc_write_kernel<false><<<nb, nt, 0, st>>>(
|
||||
(int4*)y, (int4*)peer_scratch, (const int4*)my_scratch, (unsigned*)peer_flags,
|
||||
(unsigned*)my_flags, (unsigned*)seq_ctrs, (const unsigned short*)inj, (unsigned short*)h,
|
||||
n16, (int)n, (int)hc, irow, hrow, (int)slot_stride16, (int)drain, (int)acq, nbw, G,
|
||||
nullptr, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
nullptr, (long long*)gate, (unsigned int*)gate_ctr, stage);
|
||||
}
|
||||
return (int)hipGetLastError();
|
||||
}
|
||||
@@ -544,7 +553,7 @@ void r4d_ar_oneshot_2rank_exact(long peer_scratch, long my_scratch, long peer_fl
|
||||
long my_flags, long seq_ctrs, long slot_stride16,
|
||||
long inp, long out, long n_elem, long dtype,
|
||||
long stream_i, long nblocks, long nthreads,
|
||||
long drain, long acq) {
|
||||
long drain, long acq, long stage) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
const int esize = (dtype == 2) ? 4 : 2;
|
||||
const long nbytes = (long)n_elem * esize;
|
||||
@@ -569,7 +578,7 @@ void r4d_ar_oneshot_2rank_exact(long peer_scratch, long my_scratch, long peer_fl
|
||||
#define L(T, LN) r4d_ar_oneshot_2rank_exact_kernel<T, LN><<<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)drain, (int)acq, nbw)
|
||||
(int)slot_stride16, (int)drain, (int)acq, nbw, stage)
|
||||
if (dtype == 0) L(__hip_bfloat16, 8);
|
||||
else if (dtype == 1) L(__half, 8);
|
||||
else if (dtype == 2) L(float, 4);
|
||||
|
||||
@@ -219,26 +219,27 @@ __device__ __forceinline__ void wht6_handshake(unsigned int* peer_flags, unsigne
|
||||
template <int CG = CHUNK_GROUPS>
|
||||
__device__ __forceinline__ void wht6_rehash_chunk(unsigned int& h, int c, int n_groups, int lane,
|
||||
int lig, int gw, const uint4* m_pay,
|
||||
const unsigned short* m_sc) {
|
||||
const unsigned short* m_sc, long md) {
|
||||
using Ch = Wht6Chunk<CG>;
|
||||
if constexpr (Ch::U4) {
|
||||
#pragma unroll
|
||||
for (int u = 0; u < Ch::NW / 4; ++u) {
|
||||
const long o = (long)c * (Ch::NW / 4 * WAVE) + (long)u * WAVE + lane;
|
||||
h += r4d_archk_u4(m_pay[o], 4u * (unsigned)o);
|
||||
h += r4d_archk_u4(r4d_archk_keep(&m_pay[o], md), 4u * (unsigned)o);
|
||||
}
|
||||
} else {
|
||||
const unsigned int* mp = reinterpret_cast<const unsigned int*>(m_pay);
|
||||
#pragma unroll
|
||||
for (int u = 0; u < Ch::NW; ++u) {
|
||||
const long o = (long)c * (Ch::NW * WAVE) + (long)u * WAVE + lane;
|
||||
h += r4d_archk_mix(mp[o], (unsigned)o);
|
||||
h += r4d_archk_mix(r4d_archk_keep(&mp[o], md), (unsigned)o);
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int t = 0; t < Ch::ITERS; ++t) {
|
||||
const int g = c * CG + t * GPW + gw;
|
||||
if (lig == 0 && g < n_groups) h += r4d_archk_mix(m_sc[g], R4D_ARCHK_U16 + (unsigned)g);
|
||||
if (lig == 0 && g < n_groups)
|
||||
h += r4d_archk_mix(r4d_archk_keep(&m_sc[g], md), R4D_ARCHK_U16 + (unsigned)g);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,6 +296,11 @@ struct Wht6Slot {
|
||||
uint4* p_pay; unsigned short* p_sc; // the peer's scratch: payload, bf16 scales
|
||||
const uint4* m_pay; const unsigned short* m_sc; // this rank's scratch, which the peer filled
|
||||
uint4* l_pay; float* l_sc; // this rank's own half, local only
|
||||
// Onto the host mailbox's VRAM mirror once the check has passed (r4d_ar_seq.h).
|
||||
__device__ __forceinline__ void moved(long md) {
|
||||
m_pay = r4d_archk_moved(m_pay, md);
|
||||
m_sc = r4d_archk_moved(m_sc, md);
|
||||
}
|
||||
};
|
||||
__device__ __forceinline__ Wht6Slot wht6_slot(char* peer_scratch, const char* my_scratch,
|
||||
char* loc_pack, int slot, long slot_stride_bytes,
|
||||
@@ -316,15 +322,16 @@ __global__ void r4d_ar_oneshot_2rank_wht6_kernel(
|
||||
const T* __restrict__ in, char* peer_scratch, const char* my_scratch, char* loc_pack,
|
||||
T* __restrict__ out, unsigned int* peer_flags, unsigned int* my_flags, unsigned int* seq_ctrs,
|
||||
int n_groups, int n_chunks, long slot_stride_bytes, long scale_off_bytes, int drain, int acq,
|
||||
unsigned int nbw) {
|
||||
unsigned int nbw, long stage) {
|
||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
||||
const int lane = tid & (WAVE - 1), wid = tid / WAVE, nwarp = nt / WAVE;
|
||||
const int lig = lane & (LPG - 1), gw = lane / LPG;
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
r4d_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); // device-resident, replay-safe
|
||||
const unsigned int s = s_seq;
|
||||
const Wht6Slot w = wht6_slot(peer_scratch, my_scratch, loc_pack, (int)s_slot,
|
||||
slot_stride_bytes, scale_off_bytes);
|
||||
Wht6Slot w = wht6_slot(peer_scratch, my_scratch, loc_pack, (int)s_slot,
|
||||
slot_stride_bytes, scale_off_bytes);
|
||||
const long md = r4d_archk_md(stage, my_scratch); // the host mailbox's VRAM mirror
|
||||
|
||||
const int cpb = (n_chunks + nb - 1) / nb; // block b owns chunks [c0,c1)
|
||||
const int c0 = b * cpb; int c1 = c0 + cpb; if (c1 > n_chunks) c1 = n_chunks;
|
||||
@@ -341,8 +348,9 @@ __global__ void r4d_ar_oneshot_2rank_wht6_kernel(
|
||||
wht6_handshake(peer_flags, my_flags, b, s, tid, drain, acq);
|
||||
r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, s, [&](unsigned int& h) {
|
||||
for (int c = c0 + wid; c < c1; c += nwarp)
|
||||
wht6_rehash_chunk(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc);
|
||||
wht6_rehash_chunk(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc, md);
|
||||
});
|
||||
w.moved(md);
|
||||
|
||||
const auto st = [&](long off, const float* v) {
|
||||
#pragma unroll
|
||||
@@ -366,7 +374,8 @@ static void r4d_ar_wht6_launch(int64_t peer_scratch, int64_t my_scratch, int64_t
|
||||
int64_t my_flags, int64_t seq_ctrs, int64_t loc_pack,
|
||||
int64_t slot_stride_bytes, int64_t scale_off_bytes, int64_t inp,
|
||||
int64_t out, int64_t n_elem, int64_t dtype, int64_t stream_i,
|
||||
int64_t nblocks, int64_t nthreads, int64_t drain, int64_t acq) {
|
||||
int64_t nblocks, int64_t nthreads, int64_t drain, int64_t acq,
|
||||
int64_t stage = 0) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
if (dtype != 0 && dtype != 1) throw std::runtime_error("pack ar: only bf16/fp16 payloads");
|
||||
if (n_elem % GROUP != 0) throw std::runtime_error("pack ar: n_elem not a multiple of GROUP");
|
||||
@@ -378,7 +387,7 @@ static void r4d_ar_wht6_launch(int64_t peer_scratch, int64_t my_scratch, int64_t
|
||||
(const T*)inp, (char*)peer_scratch, (const char*)my_scratch, (char*)loc_pack, (T*)out, \
|
||||
(unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, n_groups, n_chunks, \
|
||||
(long)slot_stride_bytes, (long)scale_off_bytes, (int)drain, (int)acq, \
|
||||
(unsigned)R4D_AR_MAX_BLOCKS)
|
||||
(unsigned)R4D_AR_MAX_BLOCKS, (long)stage)
|
||||
if (dtype == 0) LF(__hip_bfloat16);
|
||||
else LF(__half);
|
||||
#undef LF
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
void r4d_ar_oneshot_2rank_wht6(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
long seq_ctrs, long loc_pack, long slot_stride_bytes,
|
||||
long scale_off_bytes, long inp, long out, long n_elem, long dtype,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq) {
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long stage) {
|
||||
r4d_ar_wht6_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs, loc_pack,
|
||||
slot_stride_bytes, scale_off_bytes, inp, out, n_elem, dtype, stream, nblocks,
|
||||
nthreads, drain, acq);
|
||||
nthreads, drain, acq, stage);
|
||||
}
|
||||
|
||||
// Block cap of both all-reduce kernels (the flag array is sized from it).
|
||||
@@ -74,7 +75,7 @@ __global__ void r4d_ar_hc_write_wht6_kernel(
|
||||
const unsigned short* __restrict__ inj, unsigned short* __restrict__ h, int n, int hc,
|
||||
long irow, long hrow, int n_groups, int n_chunks, long slot_stride_bytes,
|
||||
long scale_off_bytes, int drain, int acq, unsigned int nbw, R4dArGather G, int rows_max,
|
||||
long long* gate, unsigned int* gate_ctr) {
|
||||
long long* gate, unsigned int* gate_ctr, long stage) {
|
||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
||||
const int lane = tid & (WAVE - 1), wid = tid / WAVE, nwarp = nt / WAVE;
|
||||
const int lig = lane & (LPG - 1), gw = lane / LPG;
|
||||
@@ -83,8 +84,9 @@ __global__ void r4d_ar_hc_write_wht6_kernel(
|
||||
const long long gate_word = r4d_ar_link_word(gate, (unsigned int)tid);
|
||||
r4d_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); // device-resident, replay-safe
|
||||
const unsigned int s = s_seq;
|
||||
const Wht6Slot w = wht6_slot(peer_scratch, my_scratch, loc_pack, (int)s_slot,
|
||||
slot_stride_bytes, scale_off_bytes);
|
||||
Wht6Slot w = wht6_slot(peer_scratch, my_scratch, loc_pack, (int)s_slot,
|
||||
slot_stride_bytes, scale_off_bytes);
|
||||
const long md = r4d_archk_md(stage, my_scratch); // the host mailbox's VRAM mirror
|
||||
|
||||
const int cpb = (n_chunks + nb - 1) / nb; // block b owns chunks [c0,c1)
|
||||
const int c0 = b * cpb; int c1 = c0 + cpb; if (c1 > n_chunks) c1 = n_chunks;
|
||||
@@ -147,8 +149,9 @@ __global__ void r4d_ar_hc_write_wht6_kernel(
|
||||
wht6_handshake(peer_flags, my_flags, b, s, tid, drain, acq);
|
||||
r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, s, [&](unsigned int& h) {
|
||||
for (int c = c0 + wid; c < c1; c += nwarp)
|
||||
wht6_rehash_chunk<CG>(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc);
|
||||
wht6_rehash_chunk<CG>(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc, md);
|
||||
});
|
||||
w.moved(md);
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
|
||||
|
||||
for (int c = c0 + wid; c < c1; c += nwarp)
|
||||
@@ -161,7 +164,7 @@ int r4d_ar_hc_write_wht6(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
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 stream_i, long nblocks, long nthreads, long drain, long acq,
|
||||
long gate, long gate_ctr) {
|
||||
long gate, long gate_ctr, long stage) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
if (n <= 0 || M <= 0 || n % 8 || hc < 1 || hrow % 8) return -1;
|
||||
if ((M * n) % GROUP) return -1;
|
||||
@@ -207,7 +210,7 @@ int r4d_ar_hc_write_wht6(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
(unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, (const unsigned short*)inj, \
|
||||
(unsigned short*)h, (int)n, (int)hc, irow, hrow, n_groups, n_chunks, slot_stride_bytes, \
|
||||
scale_off_bytes, (int)drain, (int)acq, (unsigned)R4D_AR_MAX_BLOCKS, G, rows_max, \
|
||||
(long long*)gate, (unsigned int*)gate_ctr)
|
||||
(long long*)gate, (unsigned int*)gate_ctr, stage)
|
||||
if (gather) LF(true, kWht6FusedCG);
|
||||
else LF(false, kWht6FusedCG);
|
||||
#undef LF
|
||||
|
||||
@@ -197,3 +197,27 @@ __device__ __forceinline__ void r4d_archk_wait(unsigned int* my_flags, unsigned
|
||||
__builtin_amdgcn_s_sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- The host mailbox's one read. A host twin's slots are pinned host memory (r4d_ar_entry.hip),
|
||||
// which the card reads past every cache over its own link, so reading the slot for the check and
|
||||
// again for the reduce crosses the link twice: at --p2p off a prefill all-reduce's reads doubled
|
||||
// (#323). Such an instance also has a mirror of its slots in this rank's VRAM, `md` bytes from
|
||||
// them (`stage` - my_scratch; 0 over P2P, where the slots are VRAM already). The re-hash reads each
|
||||
// word through r4d_archk_keep, which also stores it into the mirror, and once the check passes the
|
||||
// kernel moves its slot pointers onto the mirror (r4d_archk_moved) for the reduce. Only this block
|
||||
// writes and reads its words of the mirror, and the check's barriers order the two; a failed try's
|
||||
// copy is overwritten by the next.
|
||||
template <typename V>
|
||||
__device__ __forceinline__ V r4d_archk_keep(const V* p, long md) {
|
||||
const V v = *p;
|
||||
if (md) *reinterpret_cast<V*>(reinterpret_cast<uintptr_t>(p) + md) = v;
|
||||
return v;
|
||||
}
|
||||
template <typename V>
|
||||
__device__ __forceinline__ const V* r4d_archk_moved(const V* p, long md) {
|
||||
return reinterpret_cast<const V*>(reinterpret_cast<uintptr_t>(p) + md);
|
||||
}
|
||||
// The offset of an instance's mirror from its slots, from the launcher's `stage` (0: none).
|
||||
__device__ __forceinline__ long r4d_archk_md(long stage, const void* my_scratch) {
|
||||
return stage ? stage - (long)reinterpret_cast<uintptr_t>(my_scratch) : 0;
|
||||
}
|
||||
@@ -64,6 +64,10 @@
|
||||
// Neither changes the arithmetic.
|
||||
#define O_QLD 134217728
|
||||
#define O_KVREP 268435456
|
||||
// TREE: a tree verify (attn_paged_qrow_tree; R4DArgs::spec_parent). A row whose entry is not -2
|
||||
// sees, inside its sequence's query block, only itself and its ancestors, and its window is
|
||||
// measured from the block's first position plus its depth. Compiled in only where a row asks.
|
||||
#define O_TREE 536870912
|
||||
__host__ __device__ __forceinline__ int r4d_attn_tiles(int ctx, int tile) { return (ctx + tile - 1) / tile; }
|
||||
__host__ __device__ __forceinline__ int r4d_attn_tps(int ctx, int tile, int splits) {
|
||||
int t = r4d_attn_tiles(ctx, tile); return (t + splits - 1) / splits;
|
||||
@@ -112,6 +116,7 @@ void r4d_attn_decode_body(const R4DArgs& a, int splits)
|
||||
static_assert(!V4 || (OPT & O_F16), "fp8_v4 widens to f16");
|
||||
constexpr int QLD = (OPT & O_QLD) ? 1 : 0;
|
||||
constexpr int KVREP = (OPT & O_KVREP) ? 1 : 0;
|
||||
constexpr int TREE = (OPT & O_TREE) ? 1 : 0;
|
||||
typedef DT16<F16> D;
|
||||
typedef typename D::frag frag16;
|
||||
|
||||
@@ -177,6 +182,16 @@ void r4d_attn_decode_body(const R4DArgs& a, int splits)
|
||||
if (t_lo >= ntl) return;
|
||||
const int t_hi = min(t_lo + tps, ntl);
|
||||
|
||||
// TREE: the block's parents staged once, so each row's walk below reads LDS rather than a
|
||||
// chain of dependent global loads. Every early return above is uniform across the block.
|
||||
__shared__ int s_tpar[TREE ? 64 : 1];
|
||||
if constexpr (TREE) {
|
||||
if (a.spec_parent) {
|
||||
for (int r = tid; r < qlen && r < 64; r += NTHR) s_tpar[r] = a.spec_parent[qoff + r];
|
||||
__syncthreads();
|
||||
}
|
||||
}
|
||||
|
||||
const int row = warp * 16 + c;
|
||||
const int qi = row / GQA, hi = row - qi * GQA;
|
||||
const bool live = (qi < qlen);
|
||||
@@ -214,8 +229,28 @@ void r4d_attn_decode_body(const R4DArgs& a, int splits)
|
||||
const int klimit = cz ? pos
|
||||
: ((WIN && a.window > 0) ? min(ctx - 1, pos + a.window - 1)
|
||||
: ctx - 1);
|
||||
const int wlo = (WIN && a.window > 0) ? (pos - a.window + 1 > 0
|
||||
? pos - a.window + 1 : 0) : 0;
|
||||
// A tree verify's row (TREE): a bit per row of the block for itself and each ancestor, and
|
||||
// its depth, from which its window is measured. Parents precede children, so the walk ends.
|
||||
const int tbase = ctx - qlen;
|
||||
uint64_t tmask = 0;
|
||||
int tdep = 0;
|
||||
bool trow = false;
|
||||
if (TREE && a.spec_parent && qrow < 64) {
|
||||
int pj = s_tpar[qrow];
|
||||
if (pj != -2) {
|
||||
trow = true;
|
||||
tmask = 1ull << qrow;
|
||||
for (int at = qrow; pj >= 0 && pj < at; ) {
|
||||
tmask |= 1ull << pj;
|
||||
++tdep;
|
||||
at = pj;
|
||||
pj = s_tpar[pj];
|
||||
}
|
||||
}
|
||||
}
|
||||
const int wpos = (TREE && trow) ? tbase + tdep : pos;
|
||||
const int wlo = (WIN && a.window > 0) ? (wpos - a.window + 1 > 0
|
||||
? wpos - a.window + 1 : 0) : 0;
|
||||
float m_ref = -3.0e38f;
|
||||
float off = PSHIFT + 3.0e38f;
|
||||
float l_i = 0.0f;
|
||||
@@ -482,10 +517,13 @@ void r4d_attn_decode_body(const R4DArgs& a, int splits)
|
||||
for (int e = 0; e < 8; ++e) sc[e] = FOLDQ ? s[e] : s[e] * mul;
|
||||
|
||||
const int kbase = k0 + mt * 16 + 8 * h;
|
||||
if (!MSKIP || wave_any(kbase + 7 > klimit) || (WIN && wave_any(kbase < wlo))) {
|
||||
if (!MSKIP || wave_any(kbase + 7 > klimit) || (WIN && wave_any(kbase < wlo)) ||
|
||||
(TREE && wave_any(trow && kbase + 7 >= tbase))) {
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e)
|
||||
sc[e] = ((kbase + e <= klimit) && (!WIN || kbase + e >= wlo))
|
||||
sc[e] = ((kbase + e <= klimit) && (!WIN || kbase + e >= wlo) &&
|
||||
(!TREE || !trow || kbase + e < tbase ||
|
||||
((tmask >> (kbase + e - tbase)) & 1ull)))
|
||||
? sc[e] : -INFINITY;
|
||||
}
|
||||
float smax = sc[0];
|
||||
|
||||
@@ -280,8 +280,11 @@ extern "C" int GQ_SYM(r4d_rad_attn_prefill_h64_gqa, _bf16)(const RadArgs* a, Rad
|
||||
|
||||
/* Split-KV partial buffer size at declare time, from the band's upper bound. Declare passes
|
||||
* parameters and no tensors, so parameters come first and tensors are the fallback (see the
|
||||
* h256/gqa6 dispatch). The size is the maximum over every sequence count up to max_seqs, because
|
||||
* S * floor(128 / (kv_heads * S)) is not monotone in S. */
|
||||
* h256/gqa6 dispatch). The size is the maximum over every sequence count up to max_seqs and every
|
||||
* query length up to the band's: S * floor(128 / (kv_heads * S)) is not monotone in S, and the
|
||||
* split law's targets (r4d_decode_splits: 256 workgroups at q_len * gqa <= 32; above, 128 to 16384
|
||||
* tokens deep and more past it) can give a shorter query more splits: at gqa 8 a 4-row step needs
|
||||
* 1.6x the partials of the band bound 5. */
|
||||
/* `row` is a token's bytes in a (block, head) page, 0 for the 16-bit and E4M3 caches' 2 * head_dim
|
||||
* (fill_paged_row). */
|
||||
static int64_t g8_decode_scratch(const RadArgs* a, int head_dim, long long row = 0) {
|
||||
@@ -298,17 +301,19 @@ static int64_t g8_decode_scratch(const RadArgs* a, int head_dim, long long row =
|
||||
const long long gqa = r4d_geti_or(a, "gqa", 1);
|
||||
r.kv_heads = (int)(gqa > 0 ? n_head / gqa : n_head);
|
||||
r.num_seqs = (int)max_seqs;
|
||||
r.q_len = (int)r4d_geti_or(a, "q_len", 1);
|
||||
const long long q_hi = std::max(1LL, r4d_geti_or(a, "q_len", 1));
|
||||
r.max_ctx = (int)r4d_geti_or(a, "max_ctx", 0);
|
||||
r.block_size = (int)r4d_geti_or(a, "block_size", 16);
|
||||
r.splits = sp;
|
||||
if (r.max_ctx <= 0) return R4D_NO(RAD_E_INVAL);
|
||||
int64_t worst = 0;
|
||||
for (long long n = 1; n <= max_seqs; ++n) {
|
||||
r.num_seqs = (int)n;
|
||||
const int64_t need = GQ_SYM(r4d_attn_decode_gqa, _scratch_bytes)(&r);
|
||||
if (need > worst) worst = need;
|
||||
}
|
||||
for (long long q = 1; q <= q_hi; ++q)
|
||||
for (long long n = 1; n <= max_seqs; ++n) {
|
||||
r.q_len = (int)q;
|
||||
r.num_seqs = (int)n;
|
||||
const int64_t need = GQ_SYM(r4d_attn_decode_gqa, _scratch_bytes)(&r);
|
||||
if (need > worst) worst = need;
|
||||
}
|
||||
return worst;
|
||||
}
|
||||
const int rc = fill_paged_row(a, &r, head_dim, row > 0 ? row : 2LL * head_dim);
|
||||
|
||||
@@ -45,6 +45,13 @@ extern "C" int r4d_attn_decode_h256_gqa2_qld_fp8kv(const R4DArgs* a, hipStream_t
|
||||
extern "C" int r4d_attn_decode_h256_gqa2_qld_bf16kv(const R4DArgs* a, hipStream_t s) {
|
||||
return r4d_decode_launch_win<B_HEAD_DIM, B_GQA, B_OPT | O_QLD, 1>(a, s);
|
||||
}
|
||||
// ...and a tree verify's mask (O_TREE, attn_paged_qrow_tree), again its own instances.
|
||||
extern "C" int r4d_attn_decode_h256_gqa2_qld_tree_fp8kv(const R4DArgs* a, hipStream_t s) {
|
||||
return r4d_decode_launch_win<B_HEAD_DIM, B_GQA, B_OPT | O_QLD | O_TREE, 0>(a, s);
|
||||
}
|
||||
extern "C" int r4d_attn_decode_h256_gqa2_qld_tree_bf16kv(const R4DArgs* a, hipStream_t s) {
|
||||
return r4d_decode_launch_win<B_HEAD_DIM, B_GQA, B_OPT | O_QLD | O_TREE, 1>(a, s);
|
||||
}
|
||||
|
||||
// ---- prefill ---------------------------------------------------------------------------------
|
||||
// The h256/gqa6 prefill option set but at K prefetch 4 (P_OPT there is 3954187); O_KWIDE is valid
|
||||
|
||||
@@ -301,9 +301,12 @@ extern "C" int r4d_rad_attn_prefill_h256_bf16(const RadArgs* a, RadStream s) {
|
||||
* row with "invalid argument". q_len is the band's upper bound, since the region is sized once
|
||||
* for the worst step.
|
||||
*
|
||||
* The size is the maximum over every sequence count up to max_seqs: S * splits(S) is not monotone
|
||||
* in S (at kv_heads 2 the h256 law gives 60 at twelve sequences and 63 at nine, and the h128 law's
|
||||
* step-up doubles splits at one count only). The laws are closed forms, so the sweep is cheap. */
|
||||
* The size is the maximum over every sequence count up to max_seqs and every query length up to
|
||||
* the band's: S * splits(S) is not monotone in S (at kv_heads 2 the h256 law gives 60 at twelve
|
||||
* sequences and 63 at nine, and the h128 law's step-up doubles splits at one count only), and the
|
||||
* h256 law's targets (r4d_decode_splits: 256 workgroups at q_len * gqa <= 32; above, 128 to 16384
|
||||
* tokens deep and more past it) can give a shorter query more splits (at gqa 6 and 16384 tokens, 5
|
||||
* rows a sequence need 1.67x the band bound 6's). The laws are closed forms, so the sweep is cheap. */
|
||||
// Per-geometry byte counts over the shared partial layout, so one sweep serves all.
|
||||
// which: 0 = h256/gqa6, 1 = h128/gqa4, 2 = h256/gqa2 (Gemma 4 windowed).
|
||||
static int64_t decode_scratch_bytes_of(const R4DArgs* r, int which) {
|
||||
@@ -332,7 +335,7 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long lo
|
||||
r.kv_heads = (int)(gqa > 0 ? n_head / gqa : n_head) * rep;
|
||||
r.kv_rep = rep;
|
||||
r.num_seqs = (int)max_seqs;
|
||||
r.q_len = (int)r4d_geti_or(a, "q_len", 1);
|
||||
const long long q_hi = std::max(1LL, r4d_geti_or(a, "q_len", 1));
|
||||
r.max_ctx = (int)r4d_geti_or(a, "max_ctx", 0);
|
||||
r.block_size = (int)r4d_geti_or(a, "block_size", 16);
|
||||
r.splits = sp;
|
||||
@@ -340,11 +343,13 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long lo
|
||||
* for a launch that later picks more, so refuse. */
|
||||
if (r.max_ctx <= 0) return R4D_NO(RAD_E_INVAL);
|
||||
int64_t worst = 0;
|
||||
for (long long n = 1; n <= max_seqs; ++n) {
|
||||
r.num_seqs = (int)n;
|
||||
const int64_t need = decode_scratch_bytes_of(&r, which);
|
||||
if (need > worst) worst = need;
|
||||
}
|
||||
for (long long q = 1; q <= q_hi; ++q)
|
||||
for (long long n = 1; n <= max_seqs; ++n) {
|
||||
r.q_len = (int)q;
|
||||
r.num_seqs = (int)n;
|
||||
const int64_t need = decode_scratch_bytes_of(&r, which);
|
||||
if (need > worst) worst = need;
|
||||
}
|
||||
return worst;
|
||||
}
|
||||
|
||||
@@ -517,6 +522,32 @@ extern "C" int r4d_rad_attn_qrow_decode_h256_gqa2_bf16(const RadArgs* a, RadStre
|
||||
return r4d_attn_decode_h256_gqa2_qld_bf16kv(&r, (hipStream_t)s);
|
||||
}
|
||||
|
||||
/* ...under attn_paged_qrow_tree: the same, with the tree verify's mask from `spec_parent` (operand
|
||||
* 8, one int32 a query row; docs/OPS.md). Causal only, as the op is. */
|
||||
static int tree_operand(const RadArgs* a, R4DArgs* r) {
|
||||
const RadTensor* tp = a->n_t > 8 ? r4d_opt(a, 8) : nullptr;
|
||||
if (!tp || !tp->data) return R4D_NO(RAD_E_INVAL);
|
||||
if (tp->dtype != RAD_I32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (r4d_numel(tp) < r4d_q_total(*r)) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r->causal != 1 || r->causal_seq) return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
r->spec_parent = (const int*)tp->data;
|
||||
return RAD_OK;
|
||||
}
|
||||
extern "C" int r4d_rad_attn_qrow_tree_decode_h256_gqa2_fp8(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = decode_common(a, &r, 256, 2, 0, 1, true);
|
||||
if (rc == RAD_OK) rc = tree_operand(a, &r);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return r4d_attn_decode_h256_gqa2_qld_tree_fp8kv(&r, (hipStream_t)s);
|
||||
}
|
||||
extern "C" int r4d_rad_attn_qrow_tree_decode_h256_gqa2_bf16(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = decode_common(a, &r, 256, 2, 0, 1, true);
|
||||
if (rc == RAD_OK) rc = tree_operand(a, &r);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return r4d_attn_decode_h256_gqa2_qld_tree_bf16kv(&r, (hipStream_t)s);
|
||||
}
|
||||
|
||||
/* Head_dim 128 at 8 queries per kv head, any window, causal or not, q_len up to 16, under
|
||||
* attn_paged_qrow: the gqa-4 kernel with each kv head mapped onto two query groups
|
||||
* (R4DArgs::kv_rep = 2), the 31B's DFlash drafter's attention over a cache that holds each kv head
|
||||
|
||||
@@ -194,6 +194,9 @@ struct AttnArgs {
|
||||
int* tinfo; /* [tile] = (first row, positions) */
|
||||
float qscale; /* softmax scale * log2(e) */
|
||||
int max_blocks, n_seq, q_len, tps, kv_heads, causal, window, n_split;
|
||||
/* A tree verify (attn_paged_qrow_tree): RadBatch::spec_parent, one a query row; read only by
|
||||
* the TREE instances. */
|
||||
const int* tpar;
|
||||
};
|
||||
|
||||
/* Tile `tile` of a ragged batch: its sequence and which BQ-position slice of that sequence's query
|
||||
@@ -240,7 +243,7 @@ __device__ void find_tile(const int* cu, int n_seq, int tile, int* s_scan, int*
|
||||
/* ------------------------------------------------------------------ the attention kernel */
|
||||
/* The body; attn_kernel and its chained form r4dchain_attn_window (r4d_chain.h) wrap it below,
|
||||
* since its blocks return early. */
|
||||
template <int HD, int GQA, bool KV8>
|
||||
template <int HD, int GQA, bool KV8, bool TREE = false>
|
||||
__device__ __forceinline__ void attn_body(AttnArgs A) {
|
||||
constexpr int R = Geo<HD>::R, BK = Geo<HD>::BK, RW = R / kWaves, BQ = R / GQA;
|
||||
constexpr int NC = HD / 128; /* 4-element chunks a lane holds of a row */
|
||||
@@ -284,6 +287,17 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
|
||||
}
|
||||
if (npos <= 0) return; /* uniform across the block */
|
||||
|
||||
/* TREE: the sequence's first rows' parents staged once (a tree block is at most 64 rows), so
|
||||
* each row's walk below reads LDS rather than a chain of dependent global loads. */
|
||||
__shared__ int s_tpar[TREE ? 64 : 1];
|
||||
if constexpr (TREE) {
|
||||
if (A.tpar) {
|
||||
const int nst = imin(qlen, 64);
|
||||
for (int r = tid; r < nst; r += kThreads) s_tpar[r] = A.tpar[tokb + r];
|
||||
__syncthreads();
|
||||
}
|
||||
}
|
||||
|
||||
const int tok0 = tokb + pfirst;
|
||||
const int qbase = ctx - qlen + pfirst; /* absolute position of the tile's first query */
|
||||
const int plast = qbase + npos - 1;
|
||||
@@ -293,7 +307,10 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
|
||||
int hi = causal ? plast + 1 : ctx;
|
||||
if (!causal && window > 0) hi = imin(hi, plast + window);
|
||||
hi = imin(imin(hi, ctx), A.max_blocks * 16);
|
||||
const int lo = window > 0 ? imax(0, qbase - window + 1) : 0;
|
||||
/* A tree row's window is measured from the block's first position plus its depth, which can
|
||||
* be below its own position, so a tree launch starts every tile at the block's first. */
|
||||
const int tbase = ctx - qlen;
|
||||
const int lo = window > 0 ? imax(0, ((TREE && A.tpar) ? tbase : qbase) - window + 1) : 0;
|
||||
const int lo_a = lo & ~(BK - 1);
|
||||
const int nkt = hi > lo_a ? (hi - lo_a + BK - 1) / BK : 0;
|
||||
const int per = (nkt + A.n_split - 1) / A.n_split;
|
||||
@@ -306,6 +323,11 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
|
||||
float qf[RW][NC][4], o[RW][NC][4], m[RW], l[RW];
|
||||
int pos[RW];
|
||||
bool val[RW];
|
||||
/* TREE: each row's ancestors inside the block (a bit per row of the block, itself included),
|
||||
* whether it is a tree row at all, and the position its window is measured from. */
|
||||
uint64_t tm[RW];
|
||||
int wq[RW];
|
||||
bool tr[RW];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < RW; ++i) {
|
||||
const int r = wave * RW + i, p = r / GQA, g = r - p * GQA;
|
||||
@@ -313,6 +335,28 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
|
||||
pos[i] = qbase + p;
|
||||
m[i] = -__builtin_inff();
|
||||
l[i] = 0.0f;
|
||||
tm[i] = 0;
|
||||
wq[i] = pos[i];
|
||||
tr[i] = false;
|
||||
if constexpr (TREE) {
|
||||
const int rel = pfirst + p; /* the row within its sequence's rows */
|
||||
if (A.tpar && val[i] && rel < 64) {
|
||||
int pj = s_tpar[rel];
|
||||
if (pj != -2) {
|
||||
uint64_t mk = 1ull << rel;
|
||||
int dep = 0;
|
||||
for (int at = rel; pj >= 0 && pj < at; ) {
|
||||
mk |= 1ull << pj;
|
||||
++dep;
|
||||
at = pj;
|
||||
pj = s_tpar[pj];
|
||||
}
|
||||
tr[i] = true;
|
||||
tm[i] = mk;
|
||||
wq[i] = tbase + dep;
|
||||
}
|
||||
}
|
||||
}
|
||||
const uint16_t* qp = A.q + (long long)(tok0 + p) * A.q_rs + (long long)(kh * GQA + g) * A.q_hs;
|
||||
const float qs = A.qscale * kds; /* k_descale folds into the query */
|
||||
#pragma unroll
|
||||
@@ -391,7 +435,10 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
|
||||
}
|
||||
const int pq = pos[i];
|
||||
bool vis = kok && (causal ? kk <= pq : true);
|
||||
if (window > 0) vis = vis && (pq - kk < window) && (causal || kk - pq < window);
|
||||
if constexpr (TREE)
|
||||
if (tr[i] && vis && kk >= tbase) vis = ((tm[i] >> (kk - tbase)) & 1ull) != 0;
|
||||
const int wpq = TREE ? wq[i] : pq;
|
||||
if (window > 0) vis = vis && (wpq - kk < window) && (causal || kk - wpq < window);
|
||||
const float sv = vis ? sc[0] : -__builtin_inff();
|
||||
const float mn = __builtin_fmaxf(m[i], wave_max(sv));
|
||||
if (mn == -__builtin_inff()) return; /* nothing visible yet: uniform */
|
||||
@@ -974,12 +1021,14 @@ static constexpr int64_t scratch_need() {
|
||||
|
||||
/* The plain kernels and their chained forms (r4dchain*, RadKernelChainFn): the chained ones wait
|
||||
* for the kernels before them on the queue in their prologue and count themselves in at the end. */
|
||||
template <int HD, int GQA, bool KV8>
|
||||
__global__ __launch_bounds__(kThreads) void attn_kernel(AttnArgs A) { attn_body<HD, GQA, KV8>(A); }
|
||||
template <int HD, int GQA, bool KV8>
|
||||
template <int HD, int GQA, bool KV8, bool TREE = false>
|
||||
__global__ __launch_bounds__(kThreads) void attn_kernel(AttnArgs A) {
|
||||
attn_body<HD, GQA, KV8, TREE>(A);
|
||||
}
|
||||
template <int HD, int GQA, bool KV8, bool TREE = false>
|
||||
__global__ __launch_bounds__(kThreads) void r4dchain_attn_window(AttnArgs A) {
|
||||
r4d_chain_wait();
|
||||
attn_body<HD, GQA, KV8>(A);
|
||||
attn_body<HD, GQA, KV8, TREE>(A);
|
||||
r4d_chain_done();
|
||||
}
|
||||
template <int HD, int GQA>
|
||||
@@ -991,10 +1040,15 @@ __global__ __launch_bounds__(kThreads) void r4dchain_merge_window(AttnArgs A) {
|
||||
r4d_chain_done();
|
||||
}
|
||||
|
||||
template <int HD, int GQA, bool KV8>
|
||||
template <int HD, int GQA, bool KV8, bool TREE = false>
|
||||
static int launch_attn(const RadArgs* a, RadStream stream) {
|
||||
constexpr int R = Geo<HD>::R, BK = Geo<HD>::BK, BQ = R / GQA, ESZ = KV8 ? 1 : 2;
|
||||
if (!a || a->n_t < 8) return RAD_E_INVAL;
|
||||
if (!a || a->n_t < (TREE ? 9 : 8)) return RAD_E_INVAL;
|
||||
/* attn_paged_qrow_tree's ninth operand: one int32 a query row (docs/OPS.md). Causal only. */
|
||||
const RadTensor* tp = TREE ? rad_arg_in(a, 8) : nullptr;
|
||||
if (TREE && (!tp || !tp->data)) return RAD_E_INVAL;
|
||||
if (TREE && tp->dtype != RAD_I32) return RAD_E_DTYPE;
|
||||
if (TREE && rad_args_geti_or(a, "causal", 1) != 1) return RAD_E_UNSUPPORTED;
|
||||
const RadTensor* q = rad_arg_in(a, 0);
|
||||
const RadTensor* kv = rad_arg_in(a, 1);
|
||||
const RadTensor* bt = rad_arg_in(a, 2);
|
||||
@@ -1028,6 +1082,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
|
||||
if (!head_view(q, qh, HD, &rows, &q_rs, &q_hs) || !head_view(o, qh, HD, &orows, &o_rs, &o_hs))
|
||||
return RAD_E_SHAPE;
|
||||
if (orows < rows) return RAD_E_SHAPE;
|
||||
if (TREE && rad_tensor_numel(tp) < rows) return RAD_E_SHAPE;
|
||||
const int64_t n_seq = rad_tensor_numel(su);
|
||||
if (rows == 0) return RAD_OK;
|
||||
if (n_seq <= 0 || n_seq > (1 << 24)) return RAD_E_SHAPE;
|
||||
@@ -1081,7 +1136,8 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
|
||||
qmax_w = rad_args_geti_or(a, "q_len", rows);
|
||||
if (qmax_w <= 0 || qmax_w > rows) qmax_w = rows;
|
||||
}
|
||||
const bool shaped = qmax_w >= kWmmaMinQ && rows >= n_seq * (kWmmaMinQ / 2);
|
||||
/* Never a tree: its verify blocks are decode-sized, and only the scalar kernel reads one. */
|
||||
const bool shaped = !TREE && qmax_w >= kWmmaMinQ && rows >= n_seq * (kWmmaMinQ / 2);
|
||||
const bool aligned = rad_ptr_aligned(q->data, 16) && rad_ptr_aligned(o->data, 16) &&
|
||||
(q_rs | q_hs | o_rs | o_hs) % 8 == 0;
|
||||
if (shaped && aligned) {
|
||||
@@ -1159,16 +1215,17 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
|
||||
A.causal = (int)causal;
|
||||
A.window = (int)window;
|
||||
A.n_split = (int)ns;
|
||||
A.tpar = TREE ? (const int*)tp->data : nullptr;
|
||||
|
||||
hipStream_t s = (hipStream_t)stream;
|
||||
/* The chained forms where the row resolved with `chain` (launches chain), at decode heights
|
||||
* only: the 31B's full layers at a verify. */
|
||||
const bool chain = rad_args_geti_or(a, "chain", 0) != 0 && rows <= 64;
|
||||
if (chain)
|
||||
R4D_KLAUNCH(R4D_KNAME(r4dchain_attn_window<HD, GQA, KV8>),
|
||||
R4D_KLAUNCH(R4D_KNAME(r4dchain_attn_window<HD, GQA, KV8, TREE>),
|
||||
dim3((unsigned)nt, (unsigned)kvh, (unsigned)ns), dim3(kThreads), s, A);
|
||||
else
|
||||
R4D_KLAUNCH(R4D_KNAME(attn_kernel<HD, GQA, KV8>),
|
||||
R4D_KLAUNCH(R4D_KNAME(attn_kernel<HD, GQA, KV8, TREE>),
|
||||
dim3((unsigned)nt, (unsigned)kvh, (unsigned)ns), dim3(kThreads), s, A);
|
||||
if (ns > 1) {
|
||||
if (chain)
|
||||
@@ -1198,6 +1255,18 @@ R4D_WIN_DEF(256, 1) R4D_WIN_DEF(256, 2) R4D_WIN_DEF(256, 4) R4D_WIN_DEF(256, 8)
|
||||
R4D_WIN_DEF(512, 1) R4D_WIN_DEF(512, 2) R4D_WIN_DEF(512, 4) R4D_WIN_DEF(512, 8)
|
||||
#undef R4D_WIN_DEF
|
||||
|
||||
/* attn_paged_qrow_tree: the scalar kernel with a tree verify's mask (TREE). */
|
||||
#define R4D_WIN_TREE_DEF(HD, G) \
|
||||
int r4d_rad_attn_window_tree_h##HD##_gqa##G##_bf16(const RadArgs* a, RadStream s) { \
|
||||
return r4d_win::launch_attn<HD, G, false, true>(a, s); \
|
||||
} \
|
||||
int r4d_rad_attn_window_tree_h##HD##_gqa##G##_fp8(const RadArgs* a, RadStream s) { \
|
||||
return r4d_win::launch_attn<HD, G, true, true>(a, s); \
|
||||
}
|
||||
R4D_WIN_TREE_DEF(256, 1) R4D_WIN_TREE_DEF(256, 2) R4D_WIN_TREE_DEF(256, 4) R4D_WIN_TREE_DEF(256, 8)
|
||||
R4D_WIN_TREE_DEF(512, 1) R4D_WIN_TREE_DEF(512, 2) R4D_WIN_TREE_DEF(512, 4) R4D_WIN_TREE_DEF(512, 8)
|
||||
#undef R4D_WIN_TREE_DEF
|
||||
|
||||
/* Called at declare with no tensors. Partials are capped at kPartBudget (tile, head, split)
|
||||
* triples for any batch, so the size is a constant. */
|
||||
int64_t r4d_rad_attn_window_h128_scratch(const RadArgs*) { return r4d_win::scratch_need<128>(); }
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
// DFlash2's candidate lattice grown best-first into a tree (rad_drafter_tree), one workgroup per
|
||||
// sequence. docs/OPS.md, `dflash_tree`, is the contract; libref's ref_dflash_tree the reference.
|
||||
//
|
||||
// The selector (r4d_dflash_select_bf16.hip) walks one path: at each position the candidate with the
|
||||
// best edge score after the previous pick. A tree keeps more than one way forward where the walk is
|
||||
// unsure. Node 0 is the anchor. Expanding a node at lattice position l with token t scores every
|
||||
// candidate c at l + 1 exactly as the walk scores it after t,
|
||||
//
|
||||
// score[c] = unary[l+1, c] + sum_r pred[t][r] * hp[l+1][r] * succ[cand[l+1, c]][r]
|
||||
//
|
||||
// and pushes each valid candidate as a child with log probability parent + log_softmax(score/temp)
|
||||
// over the valid candidates. Each round takes the untaken child with the largest log probability
|
||||
// (ties to the one pushed first), until `nodes` are taken; the taken nodes, in order, are the
|
||||
// proposal, each with its parent's node index. A parent is always taken before its child.
|
||||
//
|
||||
// The same latency-kernel shape as the walk: `nodes` expansions of K dot products of R each, and a
|
||||
// reduction over a frontier of at most nodes * K entries per round, all in LDS.
|
||||
#include <hip/hip_runtime.h>
|
||||
#include "r4d_gdn_args.h"
|
||||
#include "r4d_dflash_conv_body.h" // r4d_bf2f, r4d_u16
|
||||
#include "r4d_common.h" // r4d_wave_sum
|
||||
#include "r4d_chain.h"
|
||||
|
||||
#define DTR_THREADS 256
|
||||
#define DTR_WAVE 32
|
||||
#define DTR_WAVES (DTR_THREADS / DTR_WAVE)
|
||||
#define DTR_MAX_K 32 // the entry point rejects more
|
||||
#define DTR_MAX_R 512
|
||||
#define DTR_MAX_N 63 // nodes and steps: the node table and the frontier below
|
||||
#define DTR_FRONT (DTR_MAX_N * DTR_MAX_K)
|
||||
|
||||
// cand [rows, cand_ld] i32 row = s * steps + l
|
||||
// unary [rows, cand_ld] f32
|
||||
// hp [rows, hp_ld] bf16 (null: ones, the bigram form)
|
||||
// anchor [M * anchor_stride] i32
|
||||
// pred, succ [vocab, R] bf16
|
||||
// tok, par [M, out_ld] i32 the taken nodes' tokens and parents, nodes a row
|
||||
__device__ __forceinline__ void r4d_dflash_tree_body(
|
||||
const int* __restrict__ cand, const float* __restrict__ unary,
|
||||
const r4d_u16* __restrict__ hp, const int* __restrict__ anchor,
|
||||
const r4d_u16* __restrict__ pred, const r4d_u16* __restrict__ succ,
|
||||
int* __restrict__ tok_out, int* __restrict__ par_out, int steps, int K, int R, int V, int N,
|
||||
float inv_temp, int cand_ld, int hp_ld, int out_ld, int anchor_stride)
|
||||
{
|
||||
const int s = blockIdx.x;
|
||||
const int tid = threadIdx.x;
|
||||
const int lane = tid & (DTR_WAVE - 1);
|
||||
const int wave = tid / DTR_WAVE;
|
||||
|
||||
__shared__ float sa[DTR_MAX_R];
|
||||
__shared__ float ssc[DTR_MAX_K];
|
||||
// the frontier: every child pushed so far, in push order; a taken one's lp becomes -inf
|
||||
__shared__ float f_lp[DTR_FRONT];
|
||||
__shared__ int f_tok[DTR_FRONT];
|
||||
__shared__ short f_par[DTR_FRONT];
|
||||
__shared__ short f_l[DTR_FRONT];
|
||||
__shared__ int n_front;
|
||||
// the taken nodes, node 0 the anchor
|
||||
__shared__ int n_tok[DTR_MAX_N + 1];
|
||||
__shared__ int n_l[DTR_MAX_N + 1];
|
||||
__shared__ float n_lp[DTR_MAX_N + 1];
|
||||
// the round's reduction
|
||||
__shared__ float r_lp[DTR_WAVES];
|
||||
__shared__ int r_ix[DTR_WAVES];
|
||||
__shared__ int take;
|
||||
|
||||
constexpr int CPW = (DTR_MAX_K + DTR_WAVES - 1) / DTR_WAVES;
|
||||
|
||||
if (tid == 0) {
|
||||
n_tok[0] = anchor[(long)s * anchor_stride];
|
||||
n_l[0] = -1;
|
||||
n_lp[0] = 0.0f;
|
||||
n_front = 0;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Push node n's children: every valid candidate at its next position. Uniform across the block.
|
||||
auto expand = [&](int n) {
|
||||
const int l = n_l[n] + 1;
|
||||
if (l >= steps) return;
|
||||
const long row = (long)s * steps + l;
|
||||
const int pid = n_tok[n];
|
||||
const bool pid_ok = pid >= 0 && pid < V;
|
||||
const r4d_u16* __restrict__ prow = pred + (long)(pid_ok ? pid : 0) * R;
|
||||
if (!pid_ok) {
|
||||
for (int r = tid; r < R; r += DTR_THREADS) sa[r] = 0.0f;
|
||||
} else if (hp) {
|
||||
const r4d_u16* __restrict__ hrow = hp + row * hp_ld;
|
||||
for (int r = tid; r < R; r += DTR_THREADS) sa[r] = r4d_bf2f(prow[r]) * r4d_bf2f(hrow[r]);
|
||||
} else {
|
||||
for (int r = tid; r < R; r += DTR_THREADS) sa[r] = r4d_bf2f(prow[r]);
|
||||
}
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int i = 0; i < CPW; ++i) {
|
||||
const int c = wave + i * DTR_WAVES;
|
||||
if (c < K) {
|
||||
const long at = row * cand_ld + c;
|
||||
const int cid = cand[at];
|
||||
const bool ok = cid >= 0 && cid < V;
|
||||
const r4d_u16* __restrict__ srow = succ + (long)(ok ? cid : 0) * R;
|
||||
float acc = 0.0f;
|
||||
#pragma unroll 8
|
||||
for (int r = lane; r < R; r += DTR_WAVE) acc += sa[r] * r4d_bf2f(srow[r]);
|
||||
acc = r4d_wave_sum(ok ? acc : 0.0f);
|
||||
if (lane == 0) ssc[c] = ok ? (acc + unary[at]) * inv_temp : -INFINITY;
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
// log_softmax over the valid candidates and the push, in c order, by one thread (K <= 32)
|
||||
if (tid == 0) {
|
||||
float mx = -INFINITY;
|
||||
for (int c = 0; c < K; ++c) mx = fmaxf(mx, ssc[c]);
|
||||
if (mx > -INFINITY) {
|
||||
float sum = 0.0f;
|
||||
for (int c = 0; c < K; ++c) if (ssc[c] > -INFINITY) sum += expf(ssc[c] - mx);
|
||||
const float lse = mx + logf(sum);
|
||||
int f = n_front;
|
||||
for (int c = 0; c < K && f < DTR_FRONT; ++c) {
|
||||
if (!(ssc[c] > -INFINITY)) continue;
|
||||
f_lp[f] = n_lp[n] + (ssc[c] - lse);
|
||||
f_tok[f] = cand[row * cand_ld + c];
|
||||
f_par[f] = (short)n;
|
||||
f_l[f] = (short)l;
|
||||
++f;
|
||||
}
|
||||
n_front = f;
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
};
|
||||
|
||||
expand(0);
|
||||
for (int j = 1; j <= N; ++j) {
|
||||
// the round: the largest untaken log probability, the earliest pushed among equals
|
||||
float best = -INFINITY;
|
||||
int bix = -1;
|
||||
const int nf = n_front;
|
||||
for (int f = tid; f < nf; f += DTR_THREADS) {
|
||||
const float v = f_lp[f];
|
||||
if (v > best) { best = v; bix = f; } // ascending f: the first of equals stays
|
||||
}
|
||||
for (int d = DTR_WAVE / 2; d > 0; d >>= 1) {
|
||||
const float ov = __shfl_xor(best, d);
|
||||
const int oi = __shfl_xor(bix, d);
|
||||
if (ov > best || (ov == best && oi >= 0 && (bix < 0 || oi < bix))) { best = ov; bix = oi; }
|
||||
}
|
||||
if (lane == 0) { r_lp[wave] = best; r_ix[wave] = bix; }
|
||||
__syncthreads();
|
||||
if (tid == 0) {
|
||||
float b = -INFINITY;
|
||||
int bi = -1;
|
||||
for (int w = 0; w < DTR_WAVES; ++w)
|
||||
if (r_lp[w] > b || (r_lp[w] == b && r_ix[w] >= 0 && (bi < 0 || r_ix[w] < bi))) {
|
||||
b = r_lp[w]; bi = r_ix[w];
|
||||
}
|
||||
int t, p, l;
|
||||
float lp;
|
||||
if (bi < 0 || !(b > -INFINITY)) {
|
||||
// a dry frontier: token 0 under the last node, never expanded
|
||||
t = 0; p = j - 1; l = steps; lp = -INFINITY;
|
||||
} else {
|
||||
t = f_tok[bi]; p = f_par[bi]; l = f_l[bi]; lp = f_lp[bi];
|
||||
f_lp[bi] = -INFINITY;
|
||||
}
|
||||
n_tok[j] = t; n_l[j] = l; n_lp[j] = lp;
|
||||
tok_out[(long)s * out_ld + (j - 1)] = t;
|
||||
par_out[(long)s * out_ld + (j - 1)] = p;
|
||||
take = (bi >= 0 && b > -INFINITY) ? 1 : 0;
|
||||
}
|
||||
__syncthreads();
|
||||
if (take && j < N) expand(j);
|
||||
}
|
||||
}
|
||||
|
||||
__global__ __launch_bounds__(DTR_THREADS) void r4d_dflash_tree_kernel(
|
||||
const int* __restrict__ cand, const float* __restrict__ unary,
|
||||
const r4d_u16* __restrict__ hp, const int* __restrict__ anchor,
|
||||
const r4d_u16* __restrict__ pred, const r4d_u16* __restrict__ succ,
|
||||
int* __restrict__ tok, int* __restrict__ par, int steps, int K, int R, int V, int N,
|
||||
float inv_temp, int cand_ld, int hp_ld, int out_ld, int anchor_stride) {
|
||||
r4d_dflash_tree_body(cand, unary, hp, anchor, pred, succ, tok, par, steps, K, R, V, N, inv_temp,
|
||||
cand_ld, hp_ld, out_ld, anchor_stride);
|
||||
}
|
||||
__global__ __launch_bounds__(DTR_THREADS) void r4dchain_dflash_tree(
|
||||
const int* __restrict__ cand, const float* __restrict__ unary,
|
||||
const r4d_u16* __restrict__ hp, const int* __restrict__ anchor,
|
||||
const r4d_u16* __restrict__ pred, const r4d_u16* __restrict__ succ,
|
||||
int* __restrict__ tok, int* __restrict__ par, int steps, int K, int R, int V, int N,
|
||||
float inv_temp, int cand_ld, int hp_ld, int out_ld, int anchor_stride) {
|
||||
r4d_chain_wait();
|
||||
r4d_dflash_tree_body(cand, unary, hp, anchor, pred, succ, tok, par, steps, K, R, V, N, inv_temp,
|
||||
cand_ld, hp_ld, out_ld, anchor_stride);
|
||||
r4d_chain_done();
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry point
|
||||
enum { DT_CAND = 0, DT_UNARY, DT_HP, DT_ANCHOR, DT_PRED, DT_SUCC, DT_TOK, DT_PAR };
|
||||
|
||||
static long long dt_plane(const RadTensor* t, uint32_t dtype, long long width, long long rows) {
|
||||
if (!t || !t->data || t->rank != 2) return -1;
|
||||
if (t->dtype != dtype) return -1;
|
||||
if (t->shape[1] < width || t->shape[0] < rows) return -1;
|
||||
const long long pitch = r4d_row_pitch(t);
|
||||
return (pitch >= width) ? pitch : -1;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_dflash_tree(const RadArgs* a, RadStream st) {
|
||||
const RadTensor* cand = r4d_opt(a, DT_CAND);
|
||||
const RadTensor* un = r4d_opt(a, DT_UNARY);
|
||||
const RadTensor* hp = r4d_opt(a, DT_HP);
|
||||
const RadTensor* anc = r4d_opt(a, DT_ANCHOR);
|
||||
const RadTensor* pr = r4d_opt(a, DT_PRED);
|
||||
const RadTensor* sc = r4d_opt(a, DT_SUCC);
|
||||
const RadTensor* tok = r4d_opt(a, DT_TOK);
|
||||
const RadTensor* par = r4d_opt(a, DT_PAR);
|
||||
if (!cand || !un || !anc || !pr || !sc || !tok || !par) return R4D_NO(RAD_E_INVAL);
|
||||
|
||||
const long long steps = r4d_geti_or(a, "steps", 0);
|
||||
const long long K = r4d_geti_or(a, "top_k", 0);
|
||||
const long long R = r4d_geti_or(a, "rank", 0);
|
||||
const long long V = r4d_geti_or(a, "n_vocab", 0);
|
||||
const long long N = r4d_geti_or(a, "nodes", steps);
|
||||
const double temp = rad_args_getf_or(a, "temp", 1.0);
|
||||
/* M from the output plane, as dflash_select: the parameter is the band's bound. */
|
||||
if (tok->rank != 2 || par->rank != 2) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long M = tok->shape[0];
|
||||
if (M <= 0 || steps <= 0 || K <= 0 || R <= 0 || V <= 0 || N <= 0) return R4D_NO(RAD_E_INVAL);
|
||||
if (K > DTR_MAX_K || R < DTR_WAVE || R > DTR_MAX_R || (R % DTR_WAVE) || N > DTR_MAX_N ||
|
||||
steps > DTR_MAX_N || !(temp > 0.0))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
const long long rows = M * steps;
|
||||
const long long cand_ld = dt_plane(cand, RAD_I32, K, rows);
|
||||
const long long un_ld = dt_plane(un, RAD_F32, K, rows);
|
||||
const long long out_ld = dt_plane(tok, RAD_I32, N, M);
|
||||
const long long par_ld = dt_plane(par, RAD_I32, N, M);
|
||||
const bool has_hp = hp && hp->data;
|
||||
const long long hp_ld = has_hp ? dt_plane(hp, RAD_BF16, R, rows) : 0;
|
||||
if (cand_ld < 0 || un_ld < 0 || hp_ld < 0 || out_ld < 0 || par_ld < 0) return R4D_NO(RAD_E_SHAPE);
|
||||
if (cand_ld != un_ld || out_ld != par_ld) return R4D_NO(RAD_E_STRIDE);
|
||||
if (pr->dtype != RAD_BF16 || sc->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(pr) || !r4d_contig(sc)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (r4d_numel(pr) < V * R || r4d_numel(sc) < V * R) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long astr = r4d_geti_or(a, "anchor_stride", 1);
|
||||
if (anc->dtype != RAD_I32 && anc->dtype != RAD_U32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (astr < 1 || !r4d_contig(anc)) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_numel(anc) < (M - 1) * astr + 1) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
const bool ch = r4d_geti_or(a, "chain", 0) != 0 && M <= 64;
|
||||
hipLaunchKernelGGL((ch ? r4dchain_dflash_tree : r4d_dflash_tree_kernel), dim3((unsigned)M),
|
||||
dim3(DTR_THREADS), 0, (hipStream_t)st,
|
||||
(const int*)cand->data, (const float*)un->data,
|
||||
has_hp ? (const r4d_u16*)hp->data : nullptr, (const int*)anc->data,
|
||||
(const r4d_u16*)pr->data, (const r4d_u16*)sc->data,
|
||||
(int*)tok->data, (int*)par->data, (int)steps, (int)K, (int)R, (int)V, (int)N,
|
||||
(float)(1.0 / temp), (int)cand_ld, (int)hp_ld, (int)out_ld, (int)astr);
|
||||
return R4D_RAW((int)hipGetLastError());
|
||||
}
|
||||
@@ -382,8 +382,8 @@ __device__ __forceinline__ unsigned int arq_load_acq(const unsigned int* p) {
|
||||
// HI8: ar_rmsnorm_had_quant_i8g, pass two in norm_quant_row's rotated int8 form. The reduction and
|
||||
// the norm are untouched, so `x`, the residual and `y` are ar_rmsnorm_quant_fp8's bytes.
|
||||
// PAD (wire 6 only): the row is whole 64-element groups but not whole chunks, so its last chunk is
|
||||
// padded and a chunk's elements are found by (row, chunk in row). Without it the flat index is the
|
||||
// element offset, the code a row of whole chunks has always run.
|
||||
// padded and a chunk's elements are found by (row, chunk in row). Without it, for a row of whole
|
||||
// chunks, the flat chunk index gives the element offset.
|
||||
template <bool WF32, bool RES, uint32_t THR, int WIRE, bool POST = false, bool HI8 = false,
|
||||
bool PAD = false>
|
||||
__global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel(
|
||||
@@ -393,7 +393,7 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel(
|
||||
int4* peer_base, const int4* my_base, unsigned int* peer_flags, unsigned int* my_flags,
|
||||
unsigned int* seq_ctrs, char* loc_pack, long loc_scale_off, int M, uint32_t n, uint32_t nkb,
|
||||
float eps, float wadd, uint32_t r_ld, uint32_t y_ld, uint32_t q_ld, uint32_t s_ld,
|
||||
int slot_stride16, int drain, int acq, unsigned int nbw) {
|
||||
int slot_stride16, int drain, int acq, unsigned int nbw, long stage) {
|
||||
__shared__ float lds[THR / kWave];
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
const int b = (int)blockIdx.x, nb = (int)gridDim.x;
|
||||
@@ -412,14 +412,15 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel(
|
||||
r4d_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); /* device-resident, replay-safe */
|
||||
const unsigned int sq = s_seq;
|
||||
int4* const ps = peer_base + (size_t)s_slot * slot_stride16;
|
||||
const int4* const ms = my_base + (size_t)s_slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)s_slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
/* The slot is split as in the standalone compressed all-reduce: packed symbols from the base,
|
||||
* bf16 group scales from half way up. Group indices are global over the [M][n] message. */
|
||||
char* const p_pay = (char*)ps;
|
||||
const char* const m_pay = (const char*)ms;
|
||||
const char* m_pay = (const char*)ms;
|
||||
unsigned short* const p_sc =
|
||||
(unsigned short*)((char*)ps + (size_t)(slot_stride16 >> 1) * 16);
|
||||
const unsigned short* const m_sc =
|
||||
const unsigned short* m_sc =
|
||||
(const unsigned short*)((const char*)ms + (size_t)(slot_stride16 >> 1) * 16);
|
||||
/* This rank's own packed half, laid out like the peer slot (symbols from the base, scales from
|
||||
* `loc_scale_off`) and indexed by global chunk and group, so blocks never overlap. */
|
||||
@@ -538,7 +539,7 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel(
|
||||
const int wpr = (int)(n >> 3);
|
||||
const int4* const m4 = (const int4*)ms;
|
||||
for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR)
|
||||
h += r4d_archk_u4(m4[k], 4u * (unsigned)k);
|
||||
h += r4d_archk_u4(r4d_archk_keep(&m4[k], md), 4u * (unsigned)k);
|
||||
} else {
|
||||
for (int ci = (int)wave; ci < cpb; ci += (int)(THR / ARQ_WV)) {
|
||||
const int c = c00 + ci;
|
||||
@@ -546,24 +547,33 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel(
|
||||
const unsigned int cd = (unsigned)c * (ARQ_CBYTES / 4);
|
||||
#pragma unroll
|
||||
for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4)
|
||||
h += r4d_archk_u4(*(const uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16),
|
||||
cd + (unsigned)(w4 * ARQ_WV) + lane * 4u);
|
||||
h += r4d_archk_u4(
|
||||
r4d_archk_keep((const uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16), md),
|
||||
cd + (unsigned)(w4 * ARQ_WV) + lane * 4u);
|
||||
if constexpr (ARQ_NWORD % 4 >= 2) {
|
||||
const uint2 u = *(const uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8);
|
||||
const uint2 u = r4d_archk_keep(
|
||||
(const uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8), md);
|
||||
const unsigned int e = cd + (unsigned)((ARQ_NWORD & ~3) * ARQ_WV) + lane * 2u;
|
||||
h += r4d_archk_mix(u.x, e) + r4d_archk_mix(u.y, e + 1u);
|
||||
}
|
||||
if constexpr (ARQ_NWORD % 2 == 1)
|
||||
h += r4d_archk_mix(*(const unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV + lane * 4),
|
||||
cd + (unsigned)((ARQ_NWORD - 1) * ARQ_WV) + lane);
|
||||
h += r4d_archk_mix(
|
||||
r4d_archk_keep((const unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV +
|
||||
lane * 4), md),
|
||||
cd + (unsigned)((ARQ_NWORD - 1) * ARQ_WV) + lane);
|
||||
#pragma unroll
|
||||
for (int t = 0; t < ARQ_ITERS; ++t) {
|
||||
const int g = c * ARQ_CG + t * ARQ_GPW + gw;
|
||||
if (lig == 0) h += r4d_archk_mix(m_sc[g], R4D_ARCHK_U16 + (unsigned)g);
|
||||
if (lig == 0)
|
||||
h += r4d_archk_mix(r4d_archk_keep(&m_sc[g], md),
|
||||
R4D_ARCHK_U16 + (unsigned)g);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
m_pay = r4d_archk_moved(m_pay, md);
|
||||
m_sc = r4d_archk_moved(m_sc, md);
|
||||
|
||||
if constexpr (WIRE == 6) {
|
||||
/* Reduce: unpack both halves of every chunk this block owns, add in fp32, inverse-rotate,
|
||||
@@ -889,7 +899,8 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer
|
||||
int M, int n, float eps, float wadd, int w_f32,
|
||||
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
||||
int nblocks, int nthreads, int drain, int acq, int wire,
|
||||
long loc_pack, long loc_scale_off, long wp, bool hi8) {
|
||||
long loc_pack, long loc_scale_off, long wp, bool hi8,
|
||||
long stage) {
|
||||
if (!x || !w || !q || !s) return -1;
|
||||
if (wp && !res) return -10; // a post-norm needs the stream
|
||||
if (hi8 && wp) return -11;
|
||||
@@ -928,7 +939,7 @@ static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer
|
||||
(unsigned int*)my_flags, (unsigned int*)seq_ctrs, (char*)loc_pack, \
|
||||
(long)loc_scale_off, M, (uint32_t)n, \
|
||||
(uint32_t)nkb, eps, wadd, (uint32_t)r_ld, (uint32_t)y_ld, (uint32_t)q_ld, \
|
||||
(uint32_t)s_ld, (int)slot_stride16, drain, acq, (unsigned)nbw)
|
||||
(uint32_t)s_ld, (int)slot_stride16, drain, acq, (unsigned)nbw, stage)
|
||||
#define R4D_ARQ_H(wf, rs, T, W, PO, H) \
|
||||
do { if (pad) R4D_ARQ_HP(wf, rs, T, W, PO, H, (W == 6)); \
|
||||
else R4D_ARQ_HP(wf, rs, T, W, PO, H, false); } while (0)
|
||||
@@ -962,11 +973,11 @@ extern "C" int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long
|
||||
int M, int n, float eps, float wadd, int w_f32,
|
||||
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
||||
int nblocks, int nthreads, int drain, int acq, int wire,
|
||||
long loc_pack, long loc_scale_off, long wp) {
|
||||
long loc_pack, long loc_scale_off, long wp, long stage) {
|
||||
return ar_rmsnorm_quant_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs,
|
||||
slot_stride16, x, res, w, y, q, s, M, n, eps, wadd, w_f32,
|
||||
r_ld, y_ld, q_ld, s_ld, stream, nblocks, nthreads, drain, acq,
|
||||
wire, loc_pack, loc_scale_off, wp, false);
|
||||
wire, loc_pack, loc_scale_off, wp, false, stage);
|
||||
}
|
||||
|
||||
// The same with had_quant_act_i8g's quantiser on the norm's pass (no post-norm).
|
||||
@@ -976,11 +987,12 @@ extern "C" int r4d_ar_rmsnorm_had_quant_i8g(long peer_scratch, long my_scratch,
|
||||
int M, int n, float eps, float wadd, int w_f32,
|
||||
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
||||
int nblocks, int nthreads, int drain, int acq,
|
||||
int wire, long loc_pack, long loc_scale_off) {
|
||||
int wire, long loc_pack, long loc_scale_off,
|
||||
long stage) {
|
||||
return ar_rmsnorm_quant_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs,
|
||||
slot_stride16, x, res, w, y, q, s, M, n, eps, wadd, w_f32,
|
||||
r_ld, y_ld, q_ld, s_ld, stream, nblocks, nthreads, drain, acq,
|
||||
wire, loc_pack, loc_scale_off, 0, true);
|
||||
wire, loc_pack, loc_scale_off, 0, true, stage);
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry points
|
||||
|
||||
+19
-22
@@ -159,11 +159,11 @@ constexpr uint32_t f8_waves(bool full, uint32_t nt, uint32_t mw, uint32_t fk, bo
|
||||
if (fk == 128u)
|
||||
return nt == 1u ? 16u : nt == 2u ? (full ? 16u : 12u) : nt == 3u ? 10u
|
||||
: (full && mw >= 8u) ? 10u : 9u;
|
||||
/* The gated three-tile form at 8, not 9. At 9 waves' 168 registers its epilogue spilled
|
||||
* 57 at the full tile (48 rows), which ran gate_up 51 % slower than 56 rows, and 10 below
|
||||
* it, 70 with the LDS-staged scales. 8 waves leave 192, where it spills 9 and 10. The gated
|
||||
* form is always MW 16, four waves a SIMD a block, so 8 and 9 both hold two blocks a WGP:
|
||||
* no occupancy is lost. */
|
||||
/* The gated three-tile form pins 8 waves, not 9. At 9 waves (168 registers) its epilogue
|
||||
* spills 57 registers at the full tile (48 rows) and 10 below it, 70 with the LDS-staged
|
||||
* scales; the full-tile spill makes gate_up 51% slower than at 56 rows. At 8 waves (192
|
||||
* registers) it spills 9 and 10. The gated form is always MW 16, four waves a SIMD a
|
||||
* block, so 8 and 9 waves both hold two blocks a WGP and no occupancy is lost. */
|
||||
return nt == 1u ? 12u : nt == 2u ? 10u : nt == 3u ? (gated ? 8u : 9u)
|
||||
: mw == 4u ? 7u : 8u;
|
||||
}
|
||||
@@ -171,7 +171,7 @@ constexpr uint32_t f8_waves(bool full, uint32_t nt, uint32_t mw, uint32_t fk, bo
|
||||
return nt == 1u ? 16u : nt == 2u ? (full ? 10u : 12u) : nt == 3u ? (full ? 9u : 10u)
|
||||
: full ? 7u : 9u;
|
||||
/* 10, not 12. The fold's LDS-staged scales (gemm_fp8a8_kernel) need a few more
|
||||
* registers than 12 waves' 120 leave, and spilled. gfx12 allocates registers 24 at a time, so
|
||||
* registers than the 120 that 12 waves leave, and spill there. gfx12 allocates registers 24 at a time, so
|
||||
* 11 waves leave 120 as well and 10 is the next step (144). No decode shape puts more than 8 of
|
||||
* these waves on a SIMD, so the lower pin costs no occupancy. */
|
||||
if (nt == 1u) return 10u;
|
||||
@@ -294,16 +294,15 @@ __device__ __forceinline__ void gemm_fp8a8_body(
|
||||
afrag + (size_t)(arow * ksuper + st) * r4d_fp8::kTileBytes, alive && st < ksuper);
|
||||
}
|
||||
|
||||
/* The fold's scales are staged through LDS a K tile ahead, beside the B tile. The
|
||||
* fold loaded them itself, after the next tile's prefetch had been issued; loads return in
|
||||
* order, so each fold waited for that whole prefetch, twice a tile at FK = 256, and the "one
|
||||
* tile ahead" overlapped nothing. Now the block's threads each load one weight-scale dword and
|
||||
* one activation scale of tile c0+FK with its B prefetch (a few each only in the selftest's
|
||||
* one- and two-wave pins) and store them at the top of the next pass with the B tile, so a fold
|
||||
* reads LDS. Two registers a thread: holding a tile's scales a lane (8 + 2*NT registers)
|
||||
* spilled at the decode shapes' register pins. Same values as before: bit-identical. A dead
|
||||
* wave's fp8 block scale reads as 0 instead of the plane row past the end it used to read; its
|
||||
* rows are never stored.
|
||||
/* The fold's scales are staged through LDS one K tile ahead, beside the B tile. A fold that
|
||||
* loaded its own scales after the next tile's prefetch was issued would wait for that whole
|
||||
* prefetch (loads return in order), twice a tile at FK = 256. Instead each thread loads one
|
||||
* weight-scale dword and one activation scale of tile c0+FK with its B prefetch (a few each
|
||||
* only in the selftest's one- and two-wave pins) and stores them with the B tile at the top of
|
||||
* the next pass, so a fold reads LDS. That costs two registers a thread; holding a tile's
|
||||
* scales per lane (8 + 2*NT registers) spills at the decode shapes' register pins. The staged
|
||||
* values are the ones a direct load reads, so results are bit-identical. A dead wave's fp8
|
||||
* block scale reads as 0; its rows are never stored.
|
||||
*
|
||||
* A tile holds kFolds = FK / kBlock scale blocks, and the fold at `ks` uses block ks / kBlock
|
||||
* of its tile. s_ws: I8, each wave's 16 bf16 row scales a block, [MW][kFolds][8] dwords
|
||||
@@ -647,12 +646,10 @@ __device__ __forceinline__ void gemm_fp8a8_body(
|
||||
const float* const w0 = (const float*)Y;
|
||||
uint16_t* const cy = (uint16_t*)C;
|
||||
/* Consecutive threads take consecutive weight rows, which are adjacent in a
|
||||
* partial plane ([nb][rows]), four rows a thread as one 16-byte load and kFinU quads in
|
||||
* flight at each z. The loop walked the token index fastest, `rows` floats apart, one
|
||||
* element a thread at a time: a cache line a load and a round trip each, which at
|
||||
* N = 512, M = 64 (four columns of blocks finishing 8,192 elements over four planes)
|
||||
* was most of a 45 us call. Each element is still summed from 0.0f in ascending z:
|
||||
* bit-identical. */
|
||||
* partial plane ([nb][rows]): four rows a thread as one 16-byte load, kFinU quads in
|
||||
* flight at each z. Walking the token index fastest would put consecutive threads
|
||||
* `rows` floats apart, a cache line and a round trip per element. Both branches sum
|
||||
* each element from 0.0f in ascending z, so they are bit-identical. */
|
||||
const uint32_t nr = rhi - rlo;
|
||||
const size_t zs = (size_t)rows * nb;
|
||||
if ((rows & 3u) == 0u && (nr & 3u) == 0u && ((uintptr_t)C & 7u) == 0u
|
||||
|
||||
+53
-53
@@ -17,6 +17,8 @@
|
||||
//
|
||||
// `x` depends on all of `d`, so no workgroup can produce a channel of `x` before every column of
|
||||
// `d` exists. The kernel boundary serves as the grid-wide barrier, without cooperative launch.
|
||||
// For a small decode step (E4M3, hc 4, n 2560, up to seven rows), hc_read_fused_kernel runs A and
|
||||
// B as one grid instead, its B blocks waiting on a ticket-ordered counter (see that kernel's note).
|
||||
//
|
||||
// Weight traffic, not arithmetic, decides the decomposition. At Qwen3.8-Flash-Next's shape
|
||||
// (n 2560, hc 4, LR 320) each mixing matrix is 6.55 MB and there are 96 connections: 1.27 GB per
|
||||
@@ -207,9 +209,10 @@ __device__ __forceinline__ float hc8_dot_down(const unsigned short* __restrict__
|
||||
return acc;
|
||||
}
|
||||
|
||||
/* hc8_dot_down for two rows of W_down at once, one read of N from LDS feeding both. A
|
||||
* wave owning two columns (four rows of a decode step) read the 20 KB vector once per column, and
|
||||
* kernel A's dots were LDS-bound. Each row's sum is the same fmaf sequence as hc8_dot_down's. */
|
||||
/* hc8_dot_down for two rows of W_down at once, one read of N from LDS feeding both, so a wave
|
||||
* owning two columns (four rows of a decode step) reads the 20 KB vector once rather than once per
|
||||
* column; kernel A's dots are LDS-bound. Each row's sum is the same fmaf sequence as
|
||||
* hc8_dot_down's. */
|
||||
__device__ __forceinline__ float hc8_dot16n(const float* nf, uint4 c) {
|
||||
float wf[16];
|
||||
e4m3x8(c.x, c.y, wf);
|
||||
@@ -320,9 +323,8 @@ __device__ __forceinline__ void hc8_up_load(const unsigned char* __restrict__ wr
|
||||
/* RPT rows of W_up with the same quarter against RG rows of `d`: each value of `d` read from LDS
|
||||
* feeds RPT products, and `d` sits in LDS as the bf16 it is, sixteen values in two 16-byte reads
|
||||
* (the quarter, its group and lr are multiples of 16 by r4d_hc8::group_ok, and the base is aligned).
|
||||
* Read as floats a value at a time, the dot was a chain of LDS round trips: 7.4 of the 22.8 us at
|
||||
* four rows with every byte cache-resident. Each (row, r) accumulates the same fmaf sequence on the
|
||||
* same values as hc8_dot_up. */
|
||||
* Read as floats a value at a time, the dot would be a chain of LDS round trips. Each (row, r)
|
||||
* accumulates the same fmaf sequence on the same values as hc8_dot_up. */
|
||||
template <int RG, int RPT>
|
||||
__device__ __forceinline__ void hc8_up_dot_rows(const unsigned short* __restrict__ sdh, long lr,
|
||||
const Hc8UpQuarter* o, int q4,
|
||||
@@ -373,7 +375,7 @@ __device__ __forceinline__ float wave_sum(float v) { return r4d_wave_sum(v); }
|
||||
|
||||
/* A barrier that orders LDS only. __syncthreads() fences at workgroup scope, which in
|
||||
* WGP mode on gfx12 waits for every outstanding global load: a load issued for the next pass of a
|
||||
* K loop could not stay in flight across it, and a software-pipelined loop ran as serially as the
|
||||
* K loop cannot stay in flight across it, so a software-pipelined loop runs as serially as the
|
||||
* plain one. Where a barrier guards only LDS tiles, this one leaves global loads in flight. */
|
||||
__device__ __forceinline__ void hc_lds_barrier() {
|
||||
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup", "local");
|
||||
@@ -882,9 +884,9 @@ constexpr long kHcReadAStaticLds = (long)(kWaves * kHcMax + kHcMax) * (long)size
|
||||
* Flash-Next's shape). Thread t's elements of `h` are then t + e * kThreads for e < HCP * PER, the
|
||||
* stream being e / PER, so every one of them and of the norm weight can be loaded before the first
|
||||
* is used: one round trip from the cache instead of one per stream for the sums of squares and
|
||||
* again for N, the ten-odd dependent latencies that set the decode step's time here (the kernel
|
||||
* ran 8.3 us at one row with every byte cache-resident). The sums and N take the same values in
|
||||
* the same order, so the output is bit-identical to the loops below. */
|
||||
* again for N. Those ten-odd dependent latencies, not bytes, set the kernel's time at decode. The
|
||||
* sums and N take the same values in the same order, so the output is bit-identical to the loops
|
||||
* below. */
|
||||
template <bool F8, int HCP = 0, int PER = 0>
|
||||
__device__ __forceinline__ void hc_read_a_body(const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const void* __restrict__ downv, long dld,
|
||||
@@ -896,7 +898,7 @@ __device__ __forceinline__ void hc_read_a_body(const unsigned short* __restrict_
|
||||
unsigned xpub) {
|
||||
/* 16-byte aligned, for the 16-byte LDS reads of the dots. The dynamic region starts
|
||||
* after the kernel's static LDS, which in hc_read_fused_kernel is not a multiple of 16; a
|
||||
* misaligned base ran the fused form's A blocks three times slower at four rows. */
|
||||
* misaligned base makes the fused form's A blocks three times slower at four rows. */
|
||||
extern __shared__ __attribute__((aligned(16))) unsigned short sN[]; /* [hc * n], transposed per 256-element chunk */
|
||||
__shared__ float s_part[kWaves][kHcMax];
|
||||
__shared__ float s_scale[kHcMax];
|
||||
@@ -1289,14 +1291,14 @@ __device__ __forceinline__ void hc_up_mix_body(const void* __restrict__ upv, lon
|
||||
const int ipb = kBR / hc; /* channels this workgroup owns */
|
||||
const long i0 = (long)bx * ipb;
|
||||
|
||||
/* Every load this kernel makes before its first barrier, issued together. At
|
||||
* decode it is a chain of dependent latencies, not a stream of bytes (12 us at four rows with
|
||||
* every byte cache-resident). `d` goes first, all of a thread's share at once (lr is at most
|
||||
* two thread strides here), since loads return in order and `d` is what the barrier waits on;
|
||||
* then this thread's quarter row of W_up, which does not depend on `d`; then, when one mix item
|
||||
* a thread covers the block's (RG * ipb <= the thread count, always at hc 4), the hc values N
|
||||
* of its channel from `h`, the norm gain and kernel A's scales. Same values, same order:
|
||||
* bit-identical. */
|
||||
/* Every load this kernel makes before its first barrier, issued together: at decode the
|
||||
* kernel is a chain of dependent latencies, not a stream of bytes. `d` goes first, all of a
|
||||
* thread's share at once (lr is at most two thread strides here), since loads return in order
|
||||
* and `d` is what the barrier waits on; then this thread's quarter row of W_up, which does not
|
||||
* depend on `d`; then, when one mix item a thread covers the block's (RG * ipb <= the thread
|
||||
* count, always at hc 4), the hc values N of its channel from `h`, the norm gain and kernel
|
||||
* A's scales. Only the issue order moves; the arithmetic sees the same values in the same
|
||||
* order, so the result is bit-identical. */
|
||||
constexpr int kDStage = 2;
|
||||
const bool dpre = lr <= (long)kDStage * kThreads;
|
||||
unsigned short dv[RG][kDStage]; /* bf16 bits; the E4M3 dot keeps them as is */
|
||||
@@ -1325,7 +1327,7 @@ __device__ __forceinline__ void hc_up_mix_body(const void* __restrict__ upv, lon
|
||||
const bool one = RG * ipb <= kThreads;
|
||||
float Npre[kHcMax];
|
||||
if constexpr (WAIT) {
|
||||
/* The fused form (hc_read_fused_kernel): `h` and the gain now, kernel A's half after it. */
|
||||
/* The fused form (hc_read_fused_kernel): `h` and the gain here, kernel A's half after it. */
|
||||
float hv[kHcMax], wv[kHcMax];
|
||||
const int rl = one && tid < RG * ipb ? tid / ipb : 0, ii = tid - rl * ipb;
|
||||
const long r = r0 + rl < rows ? r0 + rl : rows - 1;
|
||||
@@ -1475,9 +1477,9 @@ __device__ __forceinline__ void hc_up_mix_body(const void* __restrict__ upv, lon
|
||||
if (r >= rows) return;
|
||||
const long base = (i0 / kUpTB) * kUpTB;
|
||||
const unsigned short* const xr = x + r * xrow + base;
|
||||
/* A lane's four channels, read once for the amax, the codes and the rotation (it
|
||||
* read the group three times, each a dependent round trip at the end of the step's chain). The
|
||||
* amax is over the same 128 values and max is exact, so this is bit-identical. */
|
||||
/* A lane's four channels, read once for the amax, the codes and the rotation; separate reads
|
||||
* would each be a dependent round trip at the end of the step's chain. The amax is over the
|
||||
* same 128 values and max is exact, so the result is bit-identical. */
|
||||
static_assert(kUpTB == 4 * kWave, "a lane holds four channels of the group");
|
||||
float v[4];
|
||||
#pragma unroll
|
||||
@@ -1598,10 +1600,9 @@ static inline int hc_up_launch(const RadTensor* up, long uld, bool f8, const uns
|
||||
}
|
||||
|
||||
/* Rows of W_up a thread (hc_up_mix_kernel's RPT), where the wider workgroup's channels
|
||||
* still divide a scale group and the width. Two, measured at Flash-Next's shape (rad-kbench, cold,
|
||||
* the fused form at one, two and four rows): one row 18.4 / 18.9 / 20.6 us, two 17.0 / 17.8 /
|
||||
* 20.4; four, before the bf16 dot, lost to two at every count (27.0 us at four rows, past the
|
||||
* registers). */
|
||||
* still divide a scale group and the width. Two: at Flash-Next's shape (cold caches, the fused
|
||||
* form at one, two and four rows) two beats one at every row count, and four runs past the
|
||||
* register budget and loses to two. */
|
||||
constexpr int kUpRpt = 2;
|
||||
static inline int hc_up_rpt(bool f8, int hc, long n) {
|
||||
int rpt = f8 ? kUpRpt : 1;
|
||||
@@ -1611,20 +1612,18 @@ static inline int hc_up_rpt(bool f8, int hc, long n) {
|
||||
return rpt;
|
||||
}
|
||||
|
||||
/* hc_up_mix_kernel's launch: hc_up's row group; counter slots are (rows / rg) row groups of
|
||||
* n / kUpTB scale groups. */
|
||||
/* ============================================================== the previous decode kernels
|
||||
/* ============================================================== the two-grid decode kernels
|
||||
*
|
||||
* hc_read's decode kernels as they were before the one-grid form and the pipelined tiled up-mix:
|
||||
* kernel A, then the up-mix kernel in a second launch, and the tiled up-mix with its K passes in
|
||||
* sequence. Kept verbatim behind r4d_hc_read_pin_decode(1) so r4d_selftest can compare the
|
||||
* current forms with them byte for byte and --perfhc can time both from one build. */
|
||||
* hc_read's decode path as two launches: kernel A, then the up-mix kernel in a second launch, and
|
||||
* the tiled up-mix with its K passes unpipelined. r4d_hc_read_pin_decode(1) selects them, so
|
||||
* r4d_selftest can compare the served forms against them byte for byte and --perfhc can time both
|
||||
* from one build. They are the reference for that comparison; keep them as they are. */
|
||||
static int g_hc_decode_form = 0;
|
||||
|
||||
template <bool F8>
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
__attribute__((amdgpu_waves_per_eu(F8 ? 9 : 1, F8 ? 9 : 16)))
|
||||
void hc_read_a_prev_kernel(const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
void hc_read_a_2grid_kernel(const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const void* __restrict__ downv, long dld,
|
||||
const unsigned short* __restrict__ injw,
|
||||
unsigned short* __restrict__ dout, unsigned short* __restrict__ inj,
|
||||
@@ -1767,7 +1766,7 @@ void hc_read_a_prev_kernel(const unsigned short* __restrict__ h, const float* __
|
||||
|
||||
template <int RG, bool F8, bool I8 = false>
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
void hc_up_mix_prev_kernel(const void* __restrict__ upv, long uld,
|
||||
void hc_up_mix_2grid_kernel(const void* __restrict__ upv, long uld,
|
||||
const unsigned short* __restrict__ dbuf,
|
||||
const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const float* __restrict__ scalein,
|
||||
@@ -1877,7 +1876,7 @@ void hc_up_mix_prev_kernel(const void* __restrict__ upv, long uld,
|
||||
}
|
||||
}
|
||||
|
||||
static inline int hc_up_mix_prev_launch(const RadTensor* up, long uld, bool f8, bool i8,
|
||||
static inline int hc_up_mix_2grid_launch(const RadTensor* up, long uld, bool f8, bool i8,
|
||||
const unsigned short* dbuf,
|
||||
const unsigned short* h, const float* w, const float* scale,
|
||||
unsigned short* x, unsigned char* q, float* qs,
|
||||
@@ -1892,7 +1891,7 @@ static inline int hc_up_mix_prev_launch(const RadTensor* up, long uld, bool f8,
|
||||
const unsigned lds = (unsigned)(rg * lr * sizeof(float));
|
||||
const void* upp = up->data;
|
||||
#define HC_UPMIX_CASE3(RG_, F8_, I8_) \
|
||||
emit_rc = R4D_EMIT(stream, (hc_up_mix_prev_kernel<RG_, F8_, I8_>), grid, dim3(kThreads), lds, \
|
||||
emit_rc = R4D_EMIT(stream, (hc_up_mix_2grid_kernel<RG_, F8_, I8_>), grid, dim3(kThreads), lds, \
|
||||
upp, uld, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr, (long)rows, (long)n, \
|
||||
hc, \
|
||||
(long)lr, (long)hrow, (long)xrow, (long)qrow, (long)srow, (long)rqrow, \
|
||||
@@ -1914,6 +1913,8 @@ static inline int hc_up_mix_prev_launch(const RadTensor* up, long uld, bool f8,
|
||||
return emit_rc;
|
||||
}
|
||||
|
||||
/* hc_up_mix_kernel's launch: hc_up's row group; counter slots are (rows / rg) row groups of
|
||||
* n / kUpTB scale groups. */
|
||||
static inline int hc_up_mix_launch(const RadTensor* up, long uld, bool f8, bool i8,
|
||||
const unsigned short* dbuf,
|
||||
const unsigned short* h, const float* w, const float* scale,
|
||||
@@ -1924,7 +1925,7 @@ static inline int hc_up_mix_launch(const RadTensor* up, long uld, bool f8, bool
|
||||
long long srow, long long rqrow, long long rsrow, float wadd,
|
||||
RadStream stream) {
|
||||
if (g_hc_decode_form == 1)
|
||||
return hc_up_mix_prev_launch(up, uld, f8, i8, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr,
|
||||
return hc_up_mix_2grid_launch(up, uld, f8, i8, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr,
|
||||
rows, n, hc, lr, hrow, xrow, qrow, srow, rqrow, rsrow, wadd,
|
||||
stream);
|
||||
int emit_rc = RAD_OK;
|
||||
@@ -2598,11 +2599,10 @@ void hc_up_mix_tiled_kernel(const void* __restrict__ upv, long uld,
|
||||
|
||||
/* At the decode tile (RT 32, E4M3, lr a whole number of passes) the K loop is
|
||||
* software-pipelined: a pass issues the next pass's loads before staging its own from
|
||||
* registers, and the mix's operands are loaded with the first pass, where each pass waited
|
||||
* on its own loads. Addresses are computed once, nothing branches and the barriers order LDS
|
||||
* only (hc_lds_barrier), so the waits stay partial. 17.8 -> 16.6 us at 32 rows (rad-kbench,
|
||||
* cold): the kernel is bound less by memory than by its staging, barriers and WMMA. The staged
|
||||
* values are the loop's own, in the same order: bit-identical. */
|
||||
* registers, and the mix's operands are loaded with the first pass. Addresses are computed
|
||||
* once, nothing branches and the barriers order LDS only (hc_lds_barrier), so the waits stay
|
||||
* partial; the kernel is bound less by memory than by its staging, barriers and WMMA. The
|
||||
* staged values are the loop's own, in the same order: bit-identical. */
|
||||
constexpr int kAPer = RT * (kTKT / 8) / kThreads, kBPer = NT * (kTKT / 8) / kThreads;
|
||||
constexpr int kNPer = RT * NT / kThreads;
|
||||
constexpr bool kPipe = F8 && RT == 32 && kAPer == 1 && kBPer * kThreads == NT * (kTKT / 8)
|
||||
@@ -2767,10 +2767,10 @@ void hc_up_mix_tiled_kernel(const void* __restrict__ upv, long uld,
|
||||
|
||||
/* The rotated codes need a whole wave per row, while the codes below use kThreads / RT threads
|
||||
* per row, so the rotation walks the tile's rows a wave at a time first. */
|
||||
/* Both finishers below load what they read before using any of it -- the
|
||||
* rotation every row of the wave's, the codes a thread's four-channel runs -- where they
|
||||
* walked rows and channels a dependent load at a time; the codes' amax is over the same 128
|
||||
* values. The tail of this kernel is one block's chain, so these were its links. Bit-identical. */
|
||||
/* Both finishers below load what they read before using any of it (the rotation every row
|
||||
* of the wave's, the codes a thread's four-channel runs) rather than one dependent load at a
|
||||
* time, since the tail of this kernel is one block's latency chain. The codes' amax is over
|
||||
* the same 128 values, so the result is bit-identical. */
|
||||
if (rq) {
|
||||
const long gb = (i0 / kUpTB) * kUpTB;
|
||||
constexpr int kRPW = (RT + kWaves - 1) / kWaves; /* rows a wave */
|
||||
@@ -2819,11 +2819,11 @@ void hc_up_mix_tiled_kernel(const void* __restrict__ upv, long uld,
|
||||
}
|
||||
}
|
||||
|
||||
/* The tiled up-mix as it was before its K passes were pipelined (see the previous decode
|
||||
* kernels above; r4d_hc_read_pin_decode(1)). */
|
||||
/* The tiled up-mix with its K passes unpipelined, for r4d_hc_read_pin_decode(1) (see the
|
||||
* two-grid decode kernels above). */
|
||||
template <int RT, int NT, bool F8, bool I8 = false>
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
void hc_up_mix_tiled_prev_kernel(const void* __restrict__ upv, long uld,
|
||||
void hc_up_mix_tiled_2grid_kernel(const void* __restrict__ upv, long uld,
|
||||
const unsigned short* __restrict__ dbuf,
|
||||
const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const float* __restrict__ scalein,
|
||||
@@ -3246,7 +3246,7 @@ static inline void hc_up_mix_tiled_launch(int rt, bool f8, bool i8, const void*
|
||||
rows, n, hc, lr, hrow, xrow, qrow, srow, rqrow, rsrow, wadd)
|
||||
#define HC_UM_CASE3(RT_, F8_, I8_) \
|
||||
do { \
|
||||
if (g_hc_decode_form == 1) HC_UM_K((hc_up_mix_tiled_prev_kernel<RT_, kTNT, F8_, I8_>)); \
|
||||
if (g_hc_decode_form == 1) HC_UM_K((hc_up_mix_tiled_2grid_kernel<RT_, kTNT, F8_, I8_>)); \
|
||||
else HC_UM_K((hc_up_mix_tiled_kernel<RT_, kTNT, F8_, I8_>)); \
|
||||
} while (0)
|
||||
#define HC_UM_CASE(RT_, F8_) \
|
||||
@@ -4812,7 +4812,7 @@ extern "C" int r4d_rad_hc_read(const RadArgs* a, RadStream s) {
|
||||
inj ? (unsigned short*)inj->data : nullptr, scale, (long)rows, (long)n, (int)hc, \
|
||||
(long)lr, (long)ta, (long)hrow, (long)irow, eps, wadd)
|
||||
if (g_hc_decode_form == 1)
|
||||
emit_rc = f8 ? HC_READ_A(hc_read_a_prev_kernel<true>) : HC_READ_A(hc_read_a_prev_kernel<false>);
|
||||
emit_rc = f8 ? HC_READ_A(hc_read_a_2grid_kernel<true>) : HC_READ_A(hc_read_a_2grid_kernel<false>);
|
||||
else if (pre) emit_rc = HC_READ_A((hc_read_a_kernel<true, 4, 10>));
|
||||
else emit_rc = f8 ? HC_READ_A(hc_read_a_kernel<true>) : HC_READ_A(hc_read_a_kernel<false>);
|
||||
#undef HC_READ_A
|
||||
|
||||
@@ -175,6 +175,18 @@ int r4d_rad_attn_window_h512_gqa8_fp8 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_window_h128_scratch(const RadArgs*);
|
||||
int64_t r4d_rad_attn_window_h256_scratch(const RadArgs*);
|
||||
int64_t r4d_rad_attn_window_h512_scratch(const RadArgs*);
|
||||
/* attn_paged_qrow_tree (docs/OPS.md): the h256/gqa2 split-KV decode and the windowed kernels with a
|
||||
* tree verify's mask from operand 8, `spec_parent`. */
|
||||
int r4d_rad_attn_qrow_tree_decode_h256_gqa2_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_qrow_tree_decode_h256_gqa2_bf16(const RadArgs*, RadStream);
|
||||
#define R4D_WIN_TREE_DECL(HD, G) \
|
||||
int r4d_rad_attn_window_tree_h##HD##_gqa##G##_bf16(const RadArgs*, RadStream); \
|
||||
int r4d_rad_attn_window_tree_h##HD##_gqa##G##_fp8 (const RadArgs*, RadStream);
|
||||
R4D_WIN_TREE_DECL(256, 1) R4D_WIN_TREE_DECL(256, 2) R4D_WIN_TREE_DECL(256, 4) R4D_WIN_TREE_DECL(256, 8)
|
||||
R4D_WIN_TREE_DECL(512, 1) R4D_WIN_TREE_DECL(512, 2) R4D_WIN_TREE_DECL(512, 4) R4D_WIN_TREE_DECL(512, 8)
|
||||
#undef R4D_WIN_TREE_DECL
|
||||
/* dflash_tree (r4d_dflash_tree_bf16.hip): dflash_select's lattice grown best-first into a tree. */
|
||||
int r4d_rad_dflash_tree(const RadArgs*, RadStream);
|
||||
/* The top-1 of a row of all-gathered (id, value) pairs, written one id a row: the vocab-sharded
|
||||
* draft head's merge (r4d_pair_argmax_f32.hip). */
|
||||
int r4d_rad_pair_argmax_f32(const RadArgs*, RadStream);
|
||||
|
||||
@@ -308,7 +308,7 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_kernel(
|
||||
signed char* __restrict__ q, float* __restrict__ S, uint16_t* __restrict__ yo,
|
||||
int4* peer_base, const int4* my_base, unsigned int* peer_flags, unsigned int* my_flags,
|
||||
unsigned int* seq_ctrs, int M, uint32_t n, float eps, float wadd, uint32_t r_ld, uint32_t y_ld,
|
||||
int slot_stride16, int drain, int acq, unsigned int nbw) {
|
||||
int slot_stride16, int drain, int acq, unsigned int nbw, long stage) {
|
||||
extern __shared__ uint16_t ybuf[];
|
||||
__shared__ float l1[THR / kWave], l2[THR / kWave], lm[THR / kWave];
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
@@ -319,7 +319,8 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_kernel(
|
||||
r4d_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); /* device-resident, replay-safe */
|
||||
const unsigned int sq = s_seq;
|
||||
int4* const ps = peer_base + (size_t)s_slot * slot_stride16;
|
||||
const int4* const ms = my_base + (size_t)s_slot * slot_stride16;
|
||||
const int4* ms = my_base + (size_t)s_slot * slot_stride16;
|
||||
const long md = r4d_archk_md(stage, my_base); // the host mailbox's VRAM mirror (r4d_ar_seq.h)
|
||||
|
||||
/* push my rows into the peer's slot; `x` is a tight [M][n] of bf16 */
|
||||
const int wpr = (int)(n >> 3);
|
||||
@@ -346,8 +347,9 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_kernel(
|
||||
if (acq) __threadfence_system();
|
||||
r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, sq, [&](unsigned int& h) {
|
||||
for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR)
|
||||
h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
|
||||
h += r4d_archk_u4(r4d_archk_keep(&ms[k], md), 4u * (unsigned)k);
|
||||
});
|
||||
ms = r4d_archk_moved(ms, md);
|
||||
|
||||
const uint16_t* __restrict__ P = (const uint16_t*)ms; /* the peer's rows, in my scratch */
|
||||
const float sv = SCL ? scalar_of(sc, sc_f32) : 1.0f;
|
||||
@@ -938,7 +940,7 @@ extern "C" int r4d_ar_rmsnorm_quant_i8(long peer_scratch, long my_scratch, long
|
||||
long x, long res, long w, long wp, long sc, int sc_f32,
|
||||
long y, long q, long s, int M, int n, float eps, float wadd,
|
||||
int w_f32, int r_ld, int y_ld, long stream, int nblocks,
|
||||
int drain, int acq) {
|
||||
int drain, int acq, long stage) {
|
||||
if (!x || !w || !q || !s) return -1;
|
||||
if (!peer_scratch || !my_scratch || !peer_flags || !my_flags || !seq_ctrs) return -1;
|
||||
if (M <= 0 || n <= 0) return -2;
|
||||
@@ -961,7 +963,7 @@ extern "C" int r4d_ar_rmsnorm_quant_i8(long peer_scratch, long my_scratch, long
|
||||
(const int4*)my_scratch, (unsigned int*)peer_flags, \
|
||||
(unsigned int*)my_flags, (unsigned int*)seq_ctrs, M, (uint32_t)n, eps, \
|
||||
wadd, (uint32_t)r_ld, (uint32_t)y_ld, (int)slot_stride16, drain, acq, \
|
||||
(unsigned)nbw)
|
||||
(unsigned)nbw, stage)
|
||||
#define R4D_ARQ8(WF, RS, PO, SC) \
|
||||
do { if (thr == 1024) R4D_ARQ8_T(WF, RS, PO, SC, 1024); \
|
||||
else if (thr == 512) R4D_ARQ8_T(WF, RS, PO, SC, 512); \
|
||||
|
||||
+119
-1
@@ -123,6 +123,18 @@ static const RadOperandSpec oAttnPaged[] = {
|
||||
{ "cu_seqlens", RAD_OPD_IN, 1 }, /* absent = one q_len for the whole batch */
|
||||
{ "out", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* attn_paged_qrow with a tree verify's parents (RadBatch::spec_parent, rad_drafter_tree). */
|
||||
static const RadOperandSpec oAttnPagedTree[] = {
|
||||
{ "q", RAD_OPD_IN, 0 },
|
||||
{ "kv_cache", RAD_OPD_IN, 0 },
|
||||
{ "block_table", RAD_OPD_IN, 0 },
|
||||
{ "seqused", RAD_OPD_IN, 0 },
|
||||
{ "k_descale", RAD_OPD_IN, 1 },
|
||||
{ "v_descale", RAD_OPD_IN, 1 },
|
||||
{ "cu_seqlens", RAD_OPD_IN, 1 },
|
||||
{ "out", RAD_OPD_OUT, 0 },
|
||||
{ "spec_parent", RAD_OPD_IN, 0 },
|
||||
};
|
||||
|
||||
/* attn_paged with gate_quant_fp8 folded into its merge: attn_paged's parameters plus the two of
|
||||
* gate_quant_fp8's that are not implied (its `n` is `head_dim`, its rows attn_paged's (token,
|
||||
@@ -700,6 +712,28 @@ static const RadOperandSpec oDflashSelect[] = {
|
||||
{ "succ", RAD_OPD_WEIGHT, 0 },
|
||||
{ "tokens", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* dflash_tree: dflash_select's, plus the node count, the softmax temperature and the parents. */
|
||||
static const RadParamSpec pDflashTree[] = {
|
||||
{ "M", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "steps", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "top_k", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "rank", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "n_vocab", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "anchor_stride", RAD_P_INT, RAD_OPTIONAL },
|
||||
{ "nodes", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "temp", RAD_P_F64, RAD_OPTIONAL },
|
||||
{ "dtype", RAD_P_STR, RAD_REQUIRED },
|
||||
};
|
||||
static const RadOperandSpec oDflashTree[] = {
|
||||
{ "cand", RAD_OPD_IN, 0 },
|
||||
{ "unary", RAD_OPD_IN, 0 },
|
||||
{ "hp", RAD_OPD_IN, 1 },
|
||||
{ "anchor", RAD_OPD_IN, 0 },
|
||||
{ "pred", RAD_OPD_WEIGHT, 0 },
|
||||
{ "succ", RAD_OPD_WEIGHT, 0 },
|
||||
{ "tokens", RAD_OPD_OUT, 0 },
|
||||
{ "parents", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------ collectives */
|
||||
static const RadParamSpec pAllReduce[] = {
|
||||
@@ -3131,6 +3165,11 @@ static const RadOpSchema kSchemas[] = {
|
||||
"attn_paged, parameter for parameter and value for value, with q read at its own row "
|
||||
"pitch: a column slice of a wider row -- the q part of a fused [q|k|v] projection -- read "
|
||||
"where it lies rather than lifted into a dense block first."),
|
||||
SCHEMA("attn_paged_qrow_tree", pAttnPaged, oAttnPagedTree,
|
||||
"attn_paged_qrow over a tree verify (rad_drafter_tree): spec_parent is -2 for an "
|
||||
"ordinary causal row, else the row's parent's index within its sequence's rows (-1 for "
|
||||
"the block's first row). Inside the block such a row sees itself and its ancestors "
|
||||
"only, and its window is measured from the block's first position plus its depth."),
|
||||
SCHEMA("attn_paged_gate_quant", pAttnPagedGq, oAttnPagedGq,
|
||||
"attn_paged followed by gate_quant_fp8 over its output, in the split-KV merge: each "
|
||||
"merged (token, head) row is gated, rounded, written to `out` and quantised to E4M3 "
|
||||
@@ -3237,6 +3276,12 @@ static const RadOpSchema kSchemas[] = {
|
||||
"positions, and the greedy walk that turns a block of independent top-k sets into one "
|
||||
"path. Only the K edges leaving the previous pick are scored, because the walk is "
|
||||
"serial and the other K-1 rows of the score matrix are never read."),
|
||||
SCHEMA("dflash_tree", pDflashTree, oDflashTree,
|
||||
"dflash_select's lattice and edge scores grown best-first into a tree of `nodes` nodes: "
|
||||
"a taken node's children are the valid candidates at the next position, each with log "
|
||||
"probability parent + log_softmax(score / temp) over them; each round takes the largest "
|
||||
"untaken child (ties to the first pushed). tokens and parents list the taken nodes in "
|
||||
"order, a parent as its node index, 0 for the anchor."),
|
||||
SCHEMA("all_reduce", pAllReduce, oAllReduce,
|
||||
"sum over the tensor-parallel group. `exact` separates the exact sum from the "
|
||||
"quantised wire and a caller that does not declare it gets no kernel. At 4 and 8 ranks "
|
||||
@@ -3907,6 +3952,15 @@ static const RadConstraint cAttnDecodeH256Gqa2Fp8[] = {
|
||||
C_GE("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 2), C_EQ("block_size", 16),
|
||||
C_GE("causal", 0), C_LE("q_len", 32), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
/* attn_paged_qrow_tree on the same kernel: causal only (a tree has no two-sided reading). */
|
||||
static const RadConstraint cAttnTreeDecodeH256Gqa2Fp8[] = {
|
||||
C_GE("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 2), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_LE("q_len", 32), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
static const RadConstraint cAttnTreeDecodeH256Gqa2Bf16[] = {
|
||||
C_GE("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 2), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_LE("q_len", 32), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeH256Gqa2Bf16[] = {
|
||||
C_GE("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 2), C_EQ("block_size", 16),
|
||||
C_GE("causal", 0), C_LE("q_len", 32), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
@@ -4442,6 +4496,12 @@ static const RadConstraint cDflashSelect[] = {
|
||||
C_GE("steps", 1), C_GE("top_k", 1), C_LE("top_k", 32),
|
||||
C_DIV("rank", 32), C_LE("rank", 512), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* dflash_tree's frontier (nodes x top_k children) and its node table live in LDS. */
|
||||
static const RadConstraint cDflashTree[] = {
|
||||
C_GE("steps", 1), C_LE("steps", 63), C_GE("top_k", 1), C_LE("top_k", 32),
|
||||
C_DIV("rank", 32), C_LE("rank", 512), C_GE("nodes", 1), C_LE("nodes", 63),
|
||||
C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* The sandwich-norm joins hold 24 elements a thread over a 256-thread block (n <= 6144), and the
|
||||
* route form quantises in whole 128-column groups. pair_argmax takes any row; expert_scale's top_k
|
||||
* is the router's, at most 64. */
|
||||
@@ -6630,6 +6690,28 @@ static const RadKernelInfo kKernels[] = {
|
||||
r4d_rad_attn_qrow_decode_h256_gqa2_scratch,
|
||||
nullptr, nullptr, nullptr },
|
||||
|
||||
/* The same under attn_paged_qrow_tree: a tree verify's mask (O_TREE instances), causal only. */
|
||||
{ "attn_qrow_tree_decode_h256_gqa2_fp8kv", "attn_paged_qrow_tree", "attn",
|
||||
"paged attention over a tree verify, q at its own row pitch, sliding window, split-KV",
|
||||
"head_dim 256, 2 queries per kv head, paged block 16, q_len*gqa <= 64, causal",
|
||||
"bf16 query (any row pitch, a multiple of 4), fp8-e4m3 kv cache with per-(seq,head) descales, "
|
||||
"i32 parents, bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnTreeDecodeH256Gqa2Fp8), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_qrow_tree_decode_h256_gqa2_fp8,
|
||||
r4d_rad_attn_qrow_decode_h256_gqa2_scratch,
|
||||
nullptr, nullptr, nullptr },
|
||||
|
||||
{ "attn_qrow_tree_decode_h256_gqa2_bf16kv", "attn_paged_qrow_tree", "attn",
|
||||
"paged attention over a tree verify, q at its own row pitch, sliding window, split-KV",
|
||||
"head_dim 256, 2 queries per kv head, paged block 16, q_len*gqa <= 64, causal",
|
||||
"bf16 query (any row pitch, a multiple of 4), bf16 kv cache, i32 parents, bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnTreeDecodeH256Gqa2Bf16), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_qrow_tree_decode_h256_gqa2_bf16,
|
||||
r4d_rad_attn_qrow_decode_h256_gqa2_scratch,
|
||||
nullptr, nullptr, nullptr },
|
||||
|
||||
/* Head_dim 128 at gqa 8 under attn_paged_qrow (cAttnQrowH128G8Fp8 says why). */
|
||||
{ "attn_qrow_h128_gqa8_fp8kv", "attn_paged_qrow", "attn",
|
||||
"paged attention, any window, causal or not, split-KV (draft block); q at any row pitch",
|
||||
@@ -6818,6 +6900,27 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
#undef R4D_WIN_PAIR
|
||||
#undef R4D_WIN_ROW
|
||||
|
||||
/* ...and under attn_paged_qrow_tree, the scalar kernel with a tree verify's mask (the launch
|
||||
* refuses a non-causal one and never takes the WMMA kernel). */
|
||||
#define R4D_WIN_TREE_ROW(HD, G, KVN, KVS, C, KVDOC) \
|
||||
{ "attn_qrow_tree_h" #HD "_gqa" #G "_" #KVN, "attn_paged_qrow_tree", "attn", \
|
||||
"paged attention over a tree verify, q at its own row pitch, any window, causal", \
|
||||
"head_dim " #HD ", " #G " queries per kv head, paged block 16, any q_len, varlen", \
|
||||
"bf16 query (any row pitch), " KVDOC " kv cache, i32 parents, bf16 out, f32 partials", \
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, \
|
||||
ROW(C), NOTUNE, \
|
||||
nullptr, nullptr, r4d_rad_attn_window_tree_h##HD##_gqa##G##_##KVS, \
|
||||
r4d_rad_attn_window_h##HD##_scratch, \
|
||||
nullptr, nullptr, nullptr },
|
||||
#define R4D_WIN_TREE_PAIR(HD, GQ) \
|
||||
R4D_WIN_TREE_ROW(HD, GQ, bf16kv, bf16, cWinH##HD##G##GQ##Bf16, "bf16") \
|
||||
R4D_WIN_TREE_ROW(HD, GQ, fp8kv, fp8, cWinH##HD##G##GQ##Fp8, "fp8-e4m3 (per-(seq,head) descales)")
|
||||
R4D_WIN_TREE_PAIR(256, 1) R4D_WIN_TREE_PAIR(256, 2) R4D_WIN_TREE_PAIR(256, 4)
|
||||
R4D_WIN_TREE_PAIR(256, 8) R4D_WIN_TREE_PAIR(512, 1) R4D_WIN_TREE_PAIR(512, 2)
|
||||
R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
|
||||
#undef R4D_WIN_TREE_PAIR
|
||||
#undef R4D_WIN_TREE_ROW
|
||||
|
||||
{ "attn_prefill_h256_gqa12_fp8kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 12 queries per kv head, paged block 4, varlen",
|
||||
@@ -9015,6 +9118,16 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
nullptr, nullptr, r4d_rad_dflash_select, nullptr, nullptr, nullptr,
|
||||
r4d_shape_dflash_select },
|
||||
|
||||
{ "dflash_tree_bf16", "dflash_tree", "dflash",
|
||||
"DFlash2's candidate lattice grown best-first into a tree (rad_drafter_tree): the selector's "
|
||||
"edge scores for each taken node's children, one workgroup a sequence",
|
||||
"top_k <= 32, rank a multiple of 32 and <= 512, steps and nodes <= 63: the frontier and the "
|
||||
"node table are LDS extents",
|
||||
"i32 candidate ids, f32 unary logits, bf16 hp and both codebooks, i32 tokens and parents out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cDflashTree), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_dflash_tree, nullptr, nullptr, nullptr, nullptr },
|
||||
|
||||
/* pair_argmax, the sandwich-norm joins and expert_scale. The joins and expert_scale have no
|
||||
* opd_shape: libref has no row for them, and tests/r4d_emu holds their source to the replaced chain
|
||||
* bit for bit on the host. pair_argmax is described as libref describes it. */
|
||||
@@ -10034,6 +10147,8 @@ extern "C" int rad_kernel_chain(int i) {
|
||||
* plain, and the backend chains only the kernels named r4dchain* */
|
||||
"attn_decode_h256_gqa2_fp8kv", "attn_decode_h256_gqa2_bf16kv",
|
||||
"attn_qrow_decode_h256_gqa2_fp8kv", "attn_qrow_decode_h256_gqa2_bf16kv",
|
||||
/* ...and their tree verify forms (O_TREE instances, the same launch) */
|
||||
"attn_qrow_tree_decode_h256_gqa2_fp8kv", "attn_qrow_tree_decode_h256_gqa2_bf16kv",
|
||||
/* the fused junction: its tail form (the two-shot with the junction after it) only */
|
||||
"ar_rmsnorm_quant_i8", "ar_rmsnorm_quant_i8_4rank",
|
||||
/* the 31B's full layers: the windowed attention and its merge (r4d_attn_paged_window.hip,
|
||||
@@ -10041,6 +10156,9 @@ extern "C" int rad_kernel_chain(int i) {
|
||||
"attn_qrow_h512_gqa2_fp8kv", "attn_qrow_h512_gqa2_bf16kv",
|
||||
"attn_qrow_h512_gqa4_fp8kv", "attn_qrow_h512_gqa4_bf16kv",
|
||||
"attn_qrow_h512_gqa8_fp8kv", "attn_qrow_h512_gqa8_bf16kv",
|
||||
"attn_qrow_tree_h512_gqa2_fp8kv", "attn_qrow_tree_h512_gqa2_bf16kv",
|
||||
"attn_qrow_tree_h512_gqa4_fp8kv", "attn_qrow_tree_h512_gqa4_bf16kv",
|
||||
"attn_qrow_tree_h512_gqa8_fp8kv", "attn_qrow_tree_h512_gqa8_bf16kv",
|
||||
"quant_act_i8",
|
||||
/* DFlash2's query and context passes: the narrow fp8 GEMMs (the gated gate_up among
|
||||
* them), the activation quantiser, the norms, the conv projection, the opening
|
||||
@@ -10058,7 +10176,7 @@ extern "C" int rad_kernel_chain(int i) {
|
||||
* selector */
|
||||
"sample_temp_f32", "sample_topk_f32", "sample_merge_topk_f32", "sample_pick_f32",
|
||||
"ar_gather_4rank", "gemm_w8a8_nt_m64", "rowtopk_bf16", "rowtopk_merge",
|
||||
"dflash_select_bf16",
|
||||
"dflash_select_bf16", "dflash_tree_bf16",
|
||||
/* the weight read beside a collective (its blocks add before reading, r4d_prefetch.hip) */
|
||||
"prefetch",
|
||||
};
|
||||
|
||||
+112
-29
@@ -1052,14 +1052,19 @@ static void attn_oracle_one(const uint16_t* qh, const uint8_t* kv, int kv_is_fp8
|
||||
* extent, is 1024. 90ede42 read the parameter, trimmed a 2048-key context to 0, and the attention
|
||||
* over slice A's rows wrote nothing. */
|
||||
/* The full-attention decode split law (r4d_decode_splits), read through the scratch hook's declare
|
||||
* path, where the depth is the declared max_ctx: the region the law sizes over the region at one
|
||||
* forced segment is the law's segment count (the region is the worst over 1..max_seqs sequences,
|
||||
* which is the last count wherever n * splits(n) peaks there or ties, as in every batch below).
|
||||
* Qwen3.8-27B's TP2 rank: 12 query heads over 2 KV heads, head_dim 256. An 8-row verify (three
|
||||
* waves at gqa 6) splits by depth: up to 16384 tokens 128 / units (x4: 16), then at least 64, and
|
||||
* past 65536 512 / units capped at 256 (x1: 256, x2: 128, x4 and more: 64). A plain decode and
|
||||
* the short block's 4-row verify keep the 256-workgroup target, and the two-tiles-a-segment cap
|
||||
* still binds a short context (1024 tokens: 64 tiles of 16, at most 32 segments). */
|
||||
* path, where the depth is the declared max_ctx. Qwen3.8-27B's TP2 rank: 12 query heads over 2 KV
|
||||
* heads, head_dim 256. An 8-row verify (three waves at gqa 6) splits by depth: up to 16384 tokens
|
||||
* 128 / units (x4: 16), then at least 64, and past 65536 512 / units capped at 256 (x1: 256, x2:
|
||||
* 128, x4 and more: 64). A plain decode and the short block's 4-row verify keep the 256-workgroup
|
||||
* target, and the two-tiles-a-segment cap still binds a short context (1024 tokens: 64 tiles of
|
||||
* 16, at most 32 segments).
|
||||
*
|
||||
* The declared region is the worst over every sequence count up to max_seqs and every query length
|
||||
* up to q_len (a shorter step can split more: attn_decode_scratch), so the region over the region
|
||||
* at one forced segment is that worst, in the batch's own rows: `splits` is the law at the batch,
|
||||
* `region` what the declare path must hold, at least `splits`. Where a 5-row step at shallow depth
|
||||
* splits more per row than the 8-row verify (x1 at 65536, x2 and x4 at 16384, x4 at 1024), the
|
||||
* region is the shorter step's. */
|
||||
static void decode_split_law_case(const Plugin& pl) {
|
||||
const char* name = "attn_decode_h256_gqa6_bf16kv";
|
||||
if (!sel(name)) return;
|
||||
@@ -1077,23 +1082,25 @@ static void decode_split_law_case(const Plugin& pl) {
|
||||
a.world_size = 1;
|
||||
return r->scratch(&a);
|
||||
};
|
||||
struct Want { int64_t seqs, q_len, ctx, splits; };
|
||||
struct Want { int64_t seqs, q_len, ctx, splits, region; };
|
||||
const Want want[] = {
|
||||
{ 1, 8, 100000, 256 }, { 2, 8, 100000, 128 }, { 4, 8, 100000, 64 }, { 6, 8, 100000, 64 },
|
||||
{ 8, 8, 100000, 64 }, { 2, 8, 65536, 64 }, { 2, 8, 65552, 128 }, { 1, 8, 65536, 64 },
|
||||
{ 2, 8, 32768, 64 }, { 4, 8, 32768, 64 }, { 7, 8, 32768, 64 }, { 4, 8, 16400, 64 },
|
||||
{ 4, 8, 16384, 16 }, { 2, 8, 16384, 32 }, { 4, 8, 1024, 16 }, { 1, 8, 1024, 32 },
|
||||
{ 4, 8, 640, 16 }, { 8, 4, 100000, 16 }, { 16, 4, 100000, 8 }, { 4, 1, 100000, 32 },
|
||||
{ 1, 1, 100000, 128 },
|
||||
{ 1, 8, 100000, 256, 256 }, { 2, 8, 100000, 128, 128 }, { 4, 8, 100000, 64, 64 },
|
||||
{ 6, 8, 100000, 64, 64 }, { 8, 8, 100000, 64, 64 }, { 2, 8, 65536, 64, 64 },
|
||||
{ 2, 8, 65552, 128, 128 }, { 1, 8, 65536, 64, 80 }, { 2, 8, 32768, 64, 64 },
|
||||
{ 4, 8, 32768, 64, 64 }, { 7, 8, 32768, 64, 64 }, { 4, 8, 16400, 64, 64 },
|
||||
{ 4, 8, 16384, 16, 20 }, { 2, 8, 16384, 32, 40 }, { 4, 8, 1024, 16, 20 },
|
||||
{ 1, 8, 1024, 32, 32 }, { 4, 8, 640, 16, 16 }, { 8, 4, 100000, 16, 16 },
|
||||
{ 16, 4, 100000, 8, 8 }, { 4, 1, 100000, 32, 32 }, { 1, 1, 100000, 128, 128 },
|
||||
};
|
||||
int bad = 0;
|
||||
for (const Want& w : want) {
|
||||
const int64_t law = bytes(w.seqs, w.q_len, w.ctx, 0), one = bytes(w.seqs, w.q_len, w.ctx, 1);
|
||||
const int64_t got = one > 0 && law > 0 ? law / one : -1;
|
||||
if (got != w.splits) {
|
||||
std::printf(" FAIL decode split law: %lld x %lld rows over %lld: %lld segments, want %lld\n",
|
||||
const int64_t got = one > 0 && law > 0 && law % one == 0 ? law / one : -1;
|
||||
if (got != w.region || w.region < w.splits) {
|
||||
std::printf(" FAIL decode split law: %lld x %lld rows over %lld: a region of %lld "
|
||||
"segments, want %lld (the batch's own %lld)\n",
|
||||
(long long)w.seqs, (long long)w.q_len, (long long)w.ctx, (long long)got,
|
||||
(long long)w.splits);
|
||||
(long long)w.region, (long long)w.splits);
|
||||
++bad;
|
||||
}
|
||||
}
|
||||
@@ -1756,6 +1763,80 @@ static void attn_split_scratch_case(const Plugin& pl, const SplitGeo& g, const c
|
||||
}
|
||||
}
|
||||
|
||||
/* Every split-KV decode row's scratch at declare (the band's bound, parameters only, as the core
|
||||
* sizes it) against what its launch checks at issue (the same hook over the issue's tensors): for
|
||||
* each band bound up to 64 / gqa rows, every query length up to it and every sequence count up to
|
||||
* max_seqs must fit. Host only; the tensors carry shapes and aligned placeholder addresses. The
|
||||
* h256 split law targets 256 workgroups at q_len * gqa <= 32 and 128 above, so a step shorter than
|
||||
* its band's bound can need more partials than the bound: 1.13.0 sized the bound only, and an MTP
|
||||
* step of 4 rows at gqa 8 (band 5) refused with RAD_E_SCRATCH at issue (#293). */
|
||||
static void attn_decode_scratch_band_case(const Plugin& pl) {
|
||||
for (int k = 0; k < pl.kernel_count(); ++k) {
|
||||
const RadKernelInfo* r = pl.kernel_at(k);
|
||||
int hd = 0, gqa = 0;
|
||||
if (std::strncmp(r->name, "attn_decode_h", 13) || !r->scratch ||
|
||||
std::sscanf(r->name, "attn_decode_h%d_gqa%d", &hd, &gqa) != 2 || hd <= 0 || gqa <= 0)
|
||||
continue;
|
||||
if (!sel(r->name) && !sel("attn_decode_scratch")) continue;
|
||||
const char* kvs = std::strrchr(r->name, '_') + 1;
|
||||
const bool bf = !std::strcmp(kvs, "bf16kv");
|
||||
const int64_t rowe = !std::strcmp(kvs, "fp8v4") ? 25 * hd / 16
|
||||
: !std::strcmp(kvs, "k6v4") ? 22 * hd / 16 : 2 * hd;
|
||||
const int64_t bs = std::strstr(r->name, "_bs4_") ? 4 : 16;
|
||||
const int64_t max_seqs = 32, max_ctx = 200000, mb = (max_ctx + bs - 1) / bs;
|
||||
const int top = std::max(1, 64 / gqa);
|
||||
void* const fake = (void*)(uintptr_t)4096; /* aligned, never dereferenced */
|
||||
std::string why;
|
||||
int checked = 0;
|
||||
for (const int64_t kv : { 1, 2 }) {
|
||||
const int64_t n_head = kv * gqa;
|
||||
for (int band = 1; band <= top; ++band) {
|
||||
std::vector<RadParam> ps = {
|
||||
PI("q_len", band), PI("head_dim", hd), PI("gqa", gqa), PI("n_head", n_head),
|
||||
PI("max_seqs", max_seqs), PI("max_ctx", max_ctx), PI("block_size", bs),
|
||||
PI("causal", 1), PI("window", 0), PS("q_dtype", "bf16"),
|
||||
PS("kv_dtype", bf ? "bf16" : "fp8_e4m3") };
|
||||
RadArgs d;
|
||||
std::memset(&d, 0, sizeof(d));
|
||||
d.p = ps.data(); d.n_p = (int)ps.size(); d.world_size = 1;
|
||||
const int64_t declared = r->scratch(&d);
|
||||
if (declared < 0) { why += fmt("; kv%lld band %d: declare refused %lld", (long long)kv,
|
||||
band, (long long)declared); continue; }
|
||||
/* the issue: no n_head / max_seqs, so the hook reads the tensors */
|
||||
std::vector<RadParam> pi = {
|
||||
PI("q_len", band), PI("head_dim", hd), PI("gqa", gqa), PI("max_ctx", max_ctx),
|
||||
PI("block_size", bs), PI("causal", 1), PI("window", 0) };
|
||||
for (int q = 1; q <= band; ++q)
|
||||
for (int64_t n = 1; n <= max_seqs; ++n) {
|
||||
std::vector<RadTensor> ts = {
|
||||
T(fake, RAD_BF16, { n * q, n_head, hd }),
|
||||
T(fake, bf ? RAD_BF16 : RAD_F8E4M3, { n * mb, kv, bs, rowe }),
|
||||
T(fake, RAD_I32, { n, mb }), T(fake, RAD_I32, { n }),
|
||||
T(fake, RAD_F32, { n, kv }), T(fake, RAD_F32, { n, kv }),
|
||||
T(nullptr, RAD_I32, { n + 1 }),
|
||||
T(fake, RAD_BF16, { n * q, n_head, hd }) };
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
a.p = pi.data(); a.n_p = (int)pi.size(); a.world_size = 1;
|
||||
const int64_t need = r->scratch(&a);
|
||||
++checked;
|
||||
if (need < 0 || need > declared) {
|
||||
why += fmt("; kv%lld band %d: q_len %d x %lld sequences needs %lld, "
|
||||
"declared %lld", (long long)kv, band, q, (long long)n,
|
||||
(long long)need, (long long)declared);
|
||||
q = band;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ok(fmt("%s: decode scratch at declare covers every shorter step", r->name).c_str(),
|
||||
why.empty() && checked > 0,
|
||||
why.empty() ? fmt("%d issue geometries", checked) : why.substr(2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* The h256/gqa6 prefill over an 8-bit cache with e4m3 products (`prefill_fp8` 1 Q.K^T, 2 P.V, 3
|
||||
* both), against the inline oracle and against `exact`, the cache's exact row, on the same data.
|
||||
@@ -18141,13 +18222,13 @@ static void hc_read_i8(const Plugin& pl, int64_t M, bool real_stream, int form)
|
||||
}
|
||||
|
||||
/* The gated residual's read at its decode kernels, byte for byte: the launcher's rule (kernels A and
|
||||
* B as one grid at one to seven rows, the tiled up-mix pipelined from eight) against the kernels it
|
||||
* replaced, pinned through r4d_hc_read_pin_decode(1). E4M3 or int8 codes, on a real stream (the
|
||||
* B as one grid at one to seven rows, the tiled up-mix pipelined from eight) against the two-grid
|
||||
* form, pinned through r4d_hc_read_pin_decode(1). E4M3 or int8 codes, on a real stream (the
|
||||
* one-grid form keys its counters on it). The two forms must dispatch different kernels, so a pin
|
||||
* that is ignored cannot pass by comparing a form with itself. */
|
||||
static void hc_decode_forms(const Plugin& pl, int64_t M, bool i8) {
|
||||
const std::string nm = fmt("hc_read_e4m3 decode kernels at M=%lld, %s codes: the rule "
|
||||
"byte-identical to the previous kernels", (long long)M,
|
||||
"byte-identical to the two-grid form", (long long)M,
|
||||
i8 ? "int8" : "E4M3");
|
||||
const RadKernelInfo* r = find(pl, "hc_read_e4m3");
|
||||
if (!r) { skip(nm.c_str(), "row absent"); return; }
|
||||
@@ -18232,7 +18313,7 @@ static void hc_decode_forms(const Plugin& pl, int64_t M, bool i8) {
|
||||
const int rc = r->launch(&a, (RadStream)hs);
|
||||
const int nd = pl.record(nullptr, 0);
|
||||
if (rc != RAD_OK || st_stream_sync(hs) != hipSuccess) {
|
||||
ok(nm.c_str(), false, fmt("%s rc=%d", form ? "the previous kernels" : "the rule", rc));
|
||||
ok(nm.c_str(), false, fmt("%s rc=%d", form ? "the two-grid form" : "the rule", rc));
|
||||
pl.pin_hc_dec(0);
|
||||
(void)hipStreamDestroy(hs);
|
||||
return;
|
||||
@@ -18446,12 +18527,12 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed, bool
|
||||
if (pl.pin_hc) pl.pin_hc(form);
|
||||
time_now(kForm[form]);
|
||||
}
|
||||
/* The decode kernels against the ones they replaced (r4d_hc_read_pin_decode), under the
|
||||
/* The two-grid decode form (r4d_hc_read_pin_decode(1)) against the served one, under the
|
||||
* launcher's prefill rule. */
|
||||
if (pl.pin_hc_dec && f8) {
|
||||
if (pl.pin_hc) pl.pin_hc(0);
|
||||
pl.pin_hc_dec(1);
|
||||
time_now("the previous decode kernels");
|
||||
time_now("the two-grid decode form");
|
||||
pl.pin_hc_dec(0);
|
||||
time_now("the launcher's rule");
|
||||
}
|
||||
@@ -25538,7 +25619,8 @@ struct ArThread {
|
||||
* The engine carries this fact (device.p2p, RAD_FACT_P2P) and selects around its absence; a card
|
||||
* count is not enough. hipDeviceCanAccessPeer answers it without a kernel: on a box with the IOMMU
|
||||
* off the peer path is absent, the enable call throws `enable_peer: invalid device ordinal`, and
|
||||
* init returns RAD_E_DEVICE — a skip line, like the card-count gates, not a failure. */
|
||||
* init returns RAD_E_DEVICE. A missing path is a skip line, like the card-count gates, not a
|
||||
* failure. */
|
||||
static bool ar_peer_path_ok(const std::vector<int>& devs, int world) {
|
||||
if (world < 2 || (int)devs.size() < world) return false;
|
||||
for (int a = 0; a < world; ++a)
|
||||
@@ -28822,6 +28904,7 @@ static void device_phase(const Plugin& pl) {
|
||||
std::make_pair(8, 53), std::make_pair(1, 300),
|
||||
std::make_pair(15, 640) })
|
||||
attn_mixed_step_case(pl, *mr.g, mr.dec, mr.pre, mr.kv, c.first, c.second, 2048);
|
||||
attn_decode_scratch_band_case(pl);
|
||||
/* Flash-Next's QSA layers: twelve queries per KV head over 4-token pages, and six at
|
||||
* four ranks, each layout's prefill (a long history and a ragged last tile), decode, and the
|
||||
* widest verify (q_len 5 at gqa 12, 10 at gqa 6). */
|
||||
@@ -31144,8 +31227,8 @@ static void device_phase(const Plugin& pl) {
|
||||
for (int form : { 1, 2 }) hc_read_i8(pl, m, true, form);
|
||||
for (int64_t m : { 700, 2048 }) hc_read_i8(pl, m, true, 3);
|
||||
}
|
||||
/* The decode kernels against the ones they replaced: each one-grid instantiation (1 to 7
|
||||
* rows) and the tiled up-mix at both of its row tiles. */
|
||||
/* The served decode kernels against the two-grid form: each one-grid instantiation (1 to
|
||||
* 7 rows) and the tiled up-mix at both of its row tiles. */
|
||||
for (int64_t m : { 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 33, 64 })
|
||||
for (bool i8 : { false, true }) hc_decode_forms(pl, m, i8);
|
||||
}
|
||||
@@ -31613,7 +31696,7 @@ static void usage(void) {
|
||||
" --perfhc M [iters]: hc_read_e4m3 at Qwen3.8-Flash-Next's shape and M rows, every\n"
|
||||
" prefill-tile form that serves M (the wide kernels at the wide tile,\n"
|
||||
" PF2 from M=512) compared byte for byte and timed as one op each,\n"
|
||||
" then the previous decode kernels against the launcher's rule;\n"
|
||||
" then the two-grid decode form against the launcher's rule;\n"
|
||||
" run it under libr4d/isa/trace.sh for its launches.\n"
|
||||
" --cold MIB: with a --perf mode, time cold: read MIB through the caches before\n"
|
||||
" every timed launch (256 empties this card's L2 and last level) and\n"
|
||||
|
||||
Reference in new issue
Block a user