This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libr4d/r4d_ar_gather_2rank.hip
Deadcode 4a61d7296b 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).
2026-10-29 12:04:24 +01:00

186 lines
9.5 KiB
Plaintext

// r4d_ar_gather_2rank.hip: r4d_ar_gather_2rank -- multi-block one-shot P2P all-GATHER, exactly two
// ranks, for dual R9700 (gfx1201/RDNA4, PCIe).
//
// A kernel rather than r4d_p2p_copy2d: a copy moves the data but carries no handshake, so the
// reader cannot know the writer's bytes have landed, and ordering it with a fence plus a barrier
// collective costs three launches and a second rendezvous. This is the one-shot all-reduce minus
// the reduction (same push, per-block flag protocol and double-buffered slot); it is a separate
// file because the output shapes differ (numel vs world_size * numel).
//
// Used by the vocab-parallel sampler. lm_head is row-sharded, so each rank reduces its vocabulary
// slice to k candidates and only the candidate sets are combined (32 KiB at 4 rows x 1024
// candidates, against ~1 GiB for full logits). The payload is (u32 token id, f32 logit) pairs,
// so the bytes are moved, never summed.
//
// `dtype` only converts n_elem to bytes.
#include "r4d.h"
#include "r4d_ar_seq.h"
#include <cstdint>
#include <stdexcept>
#include <string>
#define R4D_AR_GATHER_SPIN_MAX 4000000000ULL
#define R4D_AR_GATHER_MAX_BLOCKS 512
__device__ __forceinline__ void r4d_g_store_rel(unsigned int* p, unsigned int v) {
__hip_atomic_store(p, v, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_SYSTEM);
}
__device__ __forceinline__ unsigned int r4d_g_load_acq(const unsigned int* p) {
return __hip_atomic_load(p, __ATOMIC_ACQUIRE, __HIP_MEMORY_SCOPE_SYSTEM);
}
// Where a rank's words land. `row16` is one row of this rank's contribution, in 16-byte words.
//
// row16 <= 0 or row16 == n16 -- one row: rank r's whole slice at r * n16 (plain concatenation).
// otherwise -- row-interleaved: input [rows][row16], output
// [rows][world][row16], so row m holds every rank's words for
// that row side by side.
//
// The vocab-parallel sampler's merge reads one row of gathered candidates per position at a single
// row stride, which the concatenated form cannot express. Interleaving costs one division per word
// and saves a transpose launch. `dp16` is the output's row pitch in words: 2 * row16 for a tight
// [rows][2][row16], more when the gathered rows are the leading columns of a wider buffer.
__device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who, int dp16) {
if (row16 <= 0 || row16 >= n16) return who * n16 + k;
const int m = k / row16, c = k - m * row16;
return m * dp16 + who * row16 + c;
}
// Where word k of this rank's contribution is read: `sp16` is the input's row pitch in words,
// row16 when its rows are tight, more when they are a window of a wider row.
__device__ __forceinline__ int r4d_g_src(int k, int row16, int n16, int sp16) {
if (row16 <= 0 || row16 >= n16 || sp16 == row16) return k;
const int m = k / row16, c = k - m * row16;
return m * sp16 + c;
}
// Block b owns words [start,end) of the 16B-word space and drives its own flag handshake; no
// cross-block sync, as in the all-reduce.
__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, 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
const unsigned int s = s_seq;
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;
int end = start + w; if (end > n16) end = n16;
unsigned int hck = 0u; // the push check (r4d_ar_seq.h)
// Push the chunk to the peer slot and place this rank's own slice (a local store, no handshake)
// while the push is in flight.
{
int k = start + tid;
for (; k + 3 * nt < end; k += 4 * nt) {
int4 v0 = in4[r4d_g_src(k, row16, n16, sp16)], v1 = in4[r4d_g_src(k + nt, row16, n16, sp16)],
v2 = in4[r4d_g_src(k + 2 * nt, row16, n16, sp16)],
v3 = in4[r4d_g_src(k + 3 * nt, row16, n16, sp16)];
ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3;
hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + nt)) +
r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * nt));
out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v0;
out4[r4d_g_dst(k + nt, row16, n16, rank, dp16)] = v1;
out4[r4d_g_dst(k + 2 * nt, row16, n16, rank, dp16)] = v2;
out4[r4d_g_dst(k + 3 * nt, row16, n16, rank, dp16)] = v3;
}
for (; k < end; k += nt) {
int4 v = in4[r4d_g_src(k, row16, n16, sp16)];
ps[k] = v;
hck += r4d_archk_u4(v, 4u * (unsigned)k);
out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v;
}
}
// Drain modes, as in the all-reduce: order the pushes before the release flag so the peer cannot
// see the flag before the data.
r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s);
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
__syncthreads();
if (tid == 0) {
if (drain == 2) __threadfence_system();
r4d_g_store_rel(&peer_flags[b], s);
unsigned long long z = 0;
// A handshake that cannot complete is a lockstep violation; trap rather than pass on stale
// bytes as the peer's.
while (r4d_ar_seq_behind(r4d_g_load_acq(&my_flags[b]), s)) {
if (++z > R4D_AR_GATHER_SPIN_MAX) __builtin_trap();
}
}
__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(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.
{
const int who = 1 - rank;
int k = start + tid;
for (; k + 3 * nt < end; k += 4 * nt) {
out4[r4d_g_dst(k, row16, n16, who, dp16)] = ms[k];
out4[r4d_g_dst(k + nt, row16, n16, who, dp16)] = ms[k + nt];
out4[r4d_g_dst(k + 2 * nt, row16, n16, who, dp16)] = ms[k + 2 * nt];
out4[r4d_g_dst(k + 3 * nt, row16, n16, who, dp16)] = ms[k + 3 * nt];
}
for (; k < end; k += nt) out4[r4d_g_dst(k, row16, n16, who, dp16)] = ms[k];
}
}
void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
long seq_ctrs, long slot_stride16, long inp, long out, long n_elem,
long dtype, long rank, long row_elems, long stream_i, long nblocks,
long nthreads, long drain, long acq, long 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;
if (nbytes % 16 != 0) throw std::runtime_error("all_gather: n_elem*esize not 16B-aligned");
if (rank != 0 && rank != 1) throw std::runtime_error("all_gather: rank must be 0 or 1");
if (nbytes / 16 > slot_stride16) throw std::runtime_error("all_gather: message larger than a slot");
const int n16 = (int)(nbytes / 16);
// One row of this rank's contribution, in 16-byte words. Zero means one row (plain
// concatenation); otherwise rows must be whole 16-byte words and divide the message.
long rowb = (long)row_elems * esize;
if (row_elems > 0 && (rowb % 16 != 0 || nbytes % rowb != 0))
throw std::runtime_error("all_gather: row is not a whole number of 16B words");
const int row16 = row_elems > 0 ? (int)(rowb / 16) : 0;
// The output's row pitch in elements, 0 for a tight [rows][2][row]: whole words, at least the
// two ranks' row.
const long dpb = (long)dpitch * esize;
if (dpitch > 0 && (row16 <= 0 || dpb % 16 != 0 || dpitch < 2 * row_elems))
throw std::runtime_error("all_gather: the output pitch is not a whole number of 16B words "
"at least two rows wide");
const int dp16 = dpitch > 0 ? (int)(dpb / 16) : 2 * row16;
// The input's row pitch in elements, 0 for tight rows: whole words, at least a row.
const long spb = (long)spitch * esize;
if (spitch > 0 && (row16 <= 0 || spb % 16 != 0 || spitch < row_elems))
throw std::runtime_error("all_gather: the input pitch is not a whole number of 16B words "
"at least a row wide");
const int sp16 = spitch > 0 ? (int)(spb / 16) : row16;
int nb = (int)nblocks;
if (nb < 1) nb = 1;
if (nb > n16) nb = n16;
if (nb > R4D_AR_GATHER_MAX_BLOCKS) nb = R4D_AR_GATHER_MAX_BLOCKS;
// Block capacity of the counter allocation shared with the all-reduce (r4d_ar_seq.h).
const unsigned nbw = (unsigned)r4d_ar_max_blocks();
if ((unsigned)nb > nbw) nb = (int)nbw;
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes
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, stage);
}