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/libhalo/halo_ar_gather_Nrank.hip
Deadcode 9ed3ef7a7b radiance 1.15.0
New models and formats:
- IQR for Qwen3.8-27B: weights on the int8 grid under GPTQ, a trellis down each GPTQ column
  (IQR-V2), and IQR-V3, a computed trellis code under one scale a row, kept at its codes' width in
  VRAM and decoded in the GEMM; 15 IQR-V3 recipes from ~2 to ~7 bits a weight
  (data/recipes/q38-27b-*-v3.recipe, docs/IQR.md)
- libhalo: a kernel library for Strix Halo (gfx1151), preferred there over libr3d
- Nemotron 3.5 Embed 1B and 8B (nemotron35_embed): text and image embeddings through a Pixtral
  tower, on the card and on the CPU
- Nemotron 3.5 Rerank 8B
- /v1/systemone: Cloudflare's clef and clef-flash, Convai Innovations' Laya and Mica beside
  LiquidAI d1; scored steps read the candidates' logits from the head table with no lm_head pass
- LFM2's 8-bit trunk: block-fp8 or int8 linears with the fused producers, and the d1-3B recipe
- DeepSeek-V4.1's reply format with DSML tool calls and typed argument tags

Weights on the drive:
- The file tier reads only the experts the pass routes rows to; the draft head's weights stay in
  the host pool
- A hot VRAM cache for routed experts read off the drive, reads deposited straight into free host
  pool slots, and copyless evictions; the file tier's counters are now ssd_*
- GLM-5 Next shards its routed experts by expert, not by width, so they can live on the drive
- The stall watch counts drive reads as progress, and the DMA lane's room check reads the lane's
  own completion fence

Quantisation tooling:
- --calib-corpus, --calib-dir and --calib-layers record each linear's input Hessian; beside
  --kld-record the reference's own run records them
- --kld-top N records a sparse KL reference (the N most likely tokens and the tail's mass)
- GPTQ in a third of the time with the same codes, trellis GPTQ on the card, vector codebooks
  with coordinate descent, and rad-convert --reuse repeated, each container supplying what the
  earlier ones lack

Speculation:
- Prompt lookup alone (--spec-ngram K, --spec-ngram-min/-max): each sequence keeps an index of its
  n-grams, and a proposal is verified only when its measured step cost says it pays;
  --draft-window adaptive follows it

Speed:
- IQR decode: the narrow tile splits K for four times the int8 tile's waves and folds each 128-K
  block once; at 8 tokens down and the output projections run in 0.61-0.82x and 0.70-0.92x FP8's
  time (2-6 bits); IQ6R-S-V3 with its DFlash2 drafter steps in 26.3 ms at TP2, against 29.4 ms
  before and FP8's 27.3
- Qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8
- libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and
  rope; KLATE again for the head-256 gqa6 paged prefill; gram_accum on the matrix cores

Features:
- /health, /v1/health and /ping answer 503 with "status": "stuck" once the device has advanced
  nothing for a minute or a queue has faulted, so a supervisor can restart a wedged server (#356)
- --image-gen-max-pixels and --image-gen-max-refs size an image generator's buffers (#344)
- The startup report lists only the options not at their default, with a count; -v lists all
  (#250)
- --preempt-wait-ms bounds how long a preempted prefill waits for its whole-recompute room
- --placement layer_offload spills dense layers so one --max-model-len sequence fits
- Startup refuses amdgpu with CWSR off and says how to fix it;
  --proceed-with-incompatible-configuration starts anyway under an UNSUPPORTED banner

Fixes:
- The n-gram read-ahead hands each row to the reader once and reads only as far ahead as the row
  cache holds, and pauses while decode reads: about one drive read a row instead of two (#355)
- A file-tier window reads each of its runs once, not once a run per run
- The decode split law's verify floor is bounded by the batch's workgroups: max_seqs 256 no longer
  declares 64x the scratch (#342)
- A prefetch DMA list's mark stays out of the signal pool until the engine has read it, and a
  prefill stage copies only the blocks its own tables name (#343)
- The split-KV row sum is read with fetch-inactive (#328)
- LFM2 on the host backend: libref and libavx serve the row-form ops it declares (#348)
- The preempted-prefill gate skips a KV group that holds no blocks (#352)
- Every request entry point bounded, gated and sanitised (JSON depth, invalid UTF-8, control bytes
  in the log, the dashboard's CSP, cross-origin browsers on the loopback-only routes); a prompt id
  outside the vocabulary refused at admission; --api-key "" refused at startup
- The telemetry the HTTP thread reads is atomic
- An RMS norm row no longer depends on how many rows its launch carries; prefill attention's
  rescale is decided by live rows only
- A --kld-record prefill chunk that finishes no sequence gets its logits on a tied-head model
- A kernel's lazily made counter block is zeroed outside the lock other ranks take (a TP2 first-step
  hang)

Changes: Plugin ABI 32. Every plugin exports a layout fingerprint of the ABI structs and the loader
refuses a plugin without it or with another (#302), so out-of-tree plugins must be rebuilt.
Appended: RadBuildCtx calib_* and gen_max_*, RadArchProbe gen_max_*, RadBatch score_only and
plugin_words, RadDeviceProps::unified, RadChatFormat arg_json_value_prefix.
2026-10-30 22:01:01 +01:00

319 lines
18 KiB
Plaintext

/* Derived from libr4d/r4d_ar_gather_Nrank.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// halo_ar_gather_Nrank.hip: halo_ar_gather_3rank, halo_ar_gather_4rank, halo_ar_gather_8rank -- one-shot P2P all-GATHER,
// 3, 4 or 8 ranks, gfx1201. WS is compile-time; the 2-rank gather has its own file, as does the
// 2-rank one-shot all-reduce.
//
// halo_ar_gather_2rank.hip handshakes with one peer flag per block, so it serves two ranks only. The
// vocab-parallel sampler declares an all_gather whenever the vocabulary is sharded; without this
// kernel only libref's host ref_all_gather resolves at wider worlds and the serve is refused. It is
// the wide one-shot all-reduce with the reduction removed: same push, per-block handshake and
// double-buffered (parity, source) scratch, with the fp32 sum replaced by placement.
//
// The payload is (u32 token id, f32 logit) pairs, so it is moved and never summed. `dtype` only
// converts n_elem to bytes.
//
// Placement: row16 is one row of this rank's contribution in 16-byte words. halo_g_dst_ws
// concatenates when there is one row, and otherwise interleaves rows (input [rows][row16], output
// [rows][WS][row16]) so the merge reads each position's candidates at a single row stride (see
// halo_ar_gather_2rank.hip).
//
// The IPC helpers and the handshake live in halo_ar_wide.h; halo_ar_entry.hip owns the instance and
// the peer arrays.
#include "halo.h"
#include "halo_ar_wide.h"
#include "halo_chain.h"
#include <cstdint>
#include <stdexcept>
#include <string>
// Peer arguments are in ascending global rank with the current rank removed; `out` is
// world_size * n_elem elements. Declared extern "C" here so the definitions below get C linkage;
// halo_ar_entry.hip repeats the prototypes at its call site, since halo.h does not declare them.
extern "C" void halo_ar_gather_3rank(long peer_scratch0, long peer_scratch1,
long peer_flags0, long peer_flags1,
long my_scratch, long my_flags, long seq_ctrs,
long slot_stride16, long inp, long out, long n_elem,
long dtype, long rank, long row_elems, long stream,
long nblocks, long nthreads, long drain, long acq, long pub,
long dpitch, long spitch);
extern "C" void halo_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
long peer_flags0, long peer_flags1, long peer_flags2,
long my_scratch, long my_flags, long seq_ctrs,
long slot_stride16, long inp, long out, long n_elem,
long dtype, long rank, long row_elems, long stream,
long nblocks, long nthreads, long drain, long acq, long pub,
long gate, long gate_ctr, long dpitch, long spitch, long chain);
extern "C" void halo_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
long peer_scratch3, long peer_scratch4, long peer_scratch5,
long peer_scratch6, long peer_flags0, long peer_flags1,
long peer_flags2, long peer_flags3, long peer_flags4,
long peer_flags5, long peer_flags6, long my_scratch,
long my_flags, long seq_ctrs, long slot_stride16, long inp,
long out, long n_elem, long dtype, long rank, long row_elems,
long stream, long nblocks, long nthreads, long drain, long acq,
long pub, long dpitch, long spitch);
// Where a rank's words land: the 2-rank halo_g_dst generalised to WS. Named differently because two
// __device__ definitions of one name in the two units would violate the ODR.
// `dp16` is the output's row pitch in words, WS * row16 when the gathered rows are tight.
__device__ __forceinline__ int halo_g_dst_ws(int k, int row16, int n16, int who, int dp16) {
if (row16 <= 0 || row16 >= n16) return who * n16 + k;
const int m = k / row16, c = k - m * row16;
return m * 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 halo_g_src_ws(int k, int row16, int n16, int sp16) {
if (row16 <= 0 || row16 >= n16 || sp16 == row16) return k;
const int m = k / row16, c = k - m * row16;
return m * sp16 + c;
}
// Block b owns words [start,end) of the 16B-word space and drives its own handshake; no cross-block
// sync, as in the all-reduce.
// The body; the plain kernel and its chained form (halo_chain.h) wrap it below. The chained form
// waits before the sequence take, so a block of the next launch on the instance never starts
// ahead of this one's; no block returns early.
template <int WS>
__device__ __forceinline__ void halo_ar_gather_wide_body(
const int4* __restrict__ in4, HaloArWidePeers<int4, WS - 1> peers,
const int4* __restrict__ my_base, int4* __restrict__ out4,
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs,
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
int pub, long long* gate, unsigned int* gate_ctr) {
constexpr int NPEERS = WS - 1;
// Placement holds WS source words live per output word, so the 2x unroll spills at ws=8 (same
// limit as the one-shot all-reduce).
constexpr int RU = (WS <= 4) ? 2 : 1;
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
__shared__ unsigned int s_seq, s_slot;
const long long gate_word = halo_ar_link_word(gate, (unsigned int)tid);
halo_ar_seq_take(seq_ctrs, HALO_AR_WIDE_MAX_BLOCKS, s_seq, s_slot); // device-resident, replay-safe
const unsigned int s = s_seq;
const int parity = (int)s_slot;
const int w = (n16 + nb - 1) / nb;
const int start = b * w;
int end = start + w; if (end > n16) end = n16;
// Every peer lays its scratch out identically, with one slot per source rank. Our words go into
// the slot numbered by our rank in every peer's scratch.
int4* ps[NPEERS];
#pragma unroll
for (int p = 0; p < NPEERS; ++p)
ps[p] = peers.scratch[p] + ((size_t)parity * WS + rank) * (size_t)slot_stride16;
// Push, 4x unrolled; loaded words are reused across peers so the input is read once. This rank's
// own slice is placed after the handshake with all the others, from one src[] table.
{
int k = start + tid;
for (; k + 3 * nt < end; k += 4 * nt) {
const int4 v0 = in4[halo_g_src_ws(k, row16, n16, sp16)];
const int4 v1 = in4[halo_g_src_ws(k + nt, row16, n16, sp16)];
const int4 v2 = in4[halo_g_src_ws(k + 2 * nt, row16, n16, sp16)];
const int4 v3 = in4[halo_g_src_ws(k + 3 * nt, row16, n16, sp16)];
#pragma unroll
for (int p = 0; p < NPEERS; ++p) {
ps[p][k] = v0; ps[p][k + nt] = v1; ps[p][k + 2 * nt] = v2; ps[p][k + 3 * nt] = v3;
}
}
for (; k < end; k += nt) {
const int4 v = in4[halo_g_src_ws(k, row16, n16, sp16)];
#pragma unroll
for (int p = 0; p < NPEERS; ++p) ps[p][k] = v;
}
}
// Publish 2*seq to every peer's flag word [b][our rank], then wait on our own [b][source] for
// every source. The protocol is halo_ar_wide.h's, shared with the all-reduces on these buffers; a
// handshake that cannot complete traps rather than pass on stale bytes.
halo_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
// One handshake, and the placement after it reads this card's scratch and input only: the last
// block here ends the engine's hold, as in the one-shot all-reduce this rides beside.
halo_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
// Place every rank's slice. Slot [parity][our rank] of our own scratch is never written, so, as
// in the all-reduce, our rank's source is the input itself, read at its own pitch.
const int4* src[WS];
#pragma unroll
for (int r = 0; r < WS; ++r)
src[r] = (r == rank) ? in4
: (my_base + ((size_t)parity * WS + r) * (size_t)slot_stride16);
{
int k = start + tid;
if (RU == 2) {
for (; k + nt < end; k += 2 * nt) {
const int k0 = halo_g_src_ws(k, row16, n16, sp16), k1 = halo_g_src_ws(k + nt, row16, n16, sp16);
#pragma unroll
for (int r = 0; r < WS; ++r) {
out4[halo_g_dst_ws(k, row16, n16, r, dp16)] = src[r][r == rank ? k0 : k];
out4[halo_g_dst_ws(k + nt, row16, n16, r, dp16)] = src[r][r == rank ? k1 : k + nt];
}
}
}
for (; k < end; k += nt) {
const int k0 = halo_g_src_ws(k, row16, n16, sp16);
#pragma unroll
for (int r = 0; r < WS; ++r)
out4[halo_g_dst_ws(k, row16, n16, r, dp16)] = src[r][r == rank ? k0 : k];
}
}
}
template <int WS>
__global__ void halo_ar_gather_wide_kernel(
const int4* __restrict__ in4, HaloArWidePeers<int4, WS - 1> peers,
const int4* __restrict__ my_base, int4* __restrict__ out4,
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs,
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
int pub, long long* gate, unsigned int* gate_ctr) {
halo_ar_gather_wide_body<WS>(in4, peers, my_base, out4, my_flags, seq_ctrs, n16, slot_stride16,
rank, row16, dp16, sp16, drain, acq, pub, gate, gate_ctr);
}
template <int WS>
__global__ void halo_r4dchain_ar_gather_wide(
const int4* __restrict__ in4, HaloArWidePeers<int4, WS - 1> peers,
const int4* __restrict__ my_base, int4* __restrict__ out4,
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs,
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
int pub, long long* gate, unsigned int* gate_ctr) {
halo_chain_wait();
halo_ar_gather_wide_body<WS>(in4, peers, my_base, out4, my_flags, seq_ctrs, n16, slot_stride16,
rank, row16, dp16, sp16, drain, acq, pub, gate, gate_ctr);
halo_chain_done();
}
// --- host side ---
// `row_elems` is the row width in elements, 0 for plain concatenation. The entry point checks it
// already; it is rechecked here because this is the half that throws.
template <int WS>
static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_flags, long my_scratch,
long my_flags, long seq_ctrs, long slot_stride16, long inp,
long out, long n_elem, long dtype, long rank, long row_elems,
long stream_i, long nblocks, long nthreads, long drain, long acq,
long pub, long gate, long gate_ctr, long dpitch, long spitch,
bool chain = false) {
constexpr int NPEERS = WS - 1;
static_assert(NPEERS <= HALO_AR_WIDE_MAX_PEERS, "npeers exceeds the peer bundle bound");
if (rank < 0 || rank >= WS)
throw std::runtime_error("all_gather: rank " + std::to_string(rank) +
" out of range for the " + std::to_string(WS) +
" ranks this entry point is compiled for");
const int esize = (dtype == 2) ? 4 : 2;
const long nbytes = (long)n_elem * esize;
if (nbytes % 16 != 0) throw std::runtime_error("all_gather: n_elem*esize not 16B-aligned");
const long n16l = nbytes / 16;
if (n16l > (long)slot_stride16)
throw std::runtime_error("all_gather: message (" + std::to_string(n16l) +
" 16B words) exceeds the per-source slot stride (" +
std::to_string(slot_stride16) + ")");
const int n16 = (int)n16l;
long rowb = (long)row_elems * esize;
if (row_elems > 0 && (rowb % 16 != 0 || nbytes % rowb != 0))
throw std::runtime_error("all_gather: row is not a whole number of 16B words");
const int row16 = row_elems > 0 ? (int)(rowb / 16) : 0;
// The output's row pitch in elements, 0 for a tight [rows][WS][row]: whole words, at least the
// WS ranks' row (the gathered rows as the leading columns of a wider buffer).
const long dpb = (long)dpitch * esize;
if (dpitch > 0 && (row16 <= 0 || dpb % 16 != 0 || dpitch < (long)WS * row_elems))
throw std::runtime_error("all_gather: the output pitch is not a whole number of 16B words "
"at least " + std::to_string(WS) + " rows wide");
const int dp16 = dpitch > 0 ? (int)(dpb / 16) : WS * row16;
// The input's row pitch in elements, 0 for tight rows: whole words, at least a row (a rank's
// window of a wider row).
const long spb = (long)spitch * esize;
if (spitch > 0 && (row16 <= 0 || spb % 16 != 0 || spitch < row_elems))
throw std::runtime_error("all_gather: the input pitch is not a whole number of 16B words "
"at least a row wide");
const int sp16 = spitch > 0 ? (int)(spb / 16) : row16;
int nb = (int)nblocks;
if (nb < 1) nb = HALO_AR_NB_DESIGN;
if (nb > n16) nb = n16;
if (nb > HALO_AR_WIDE_MAX_BLOCKS) nb = HALO_AR_WIDE_MAX_BLOCKS;
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
HaloArWidePeers<int4, NPEERS> peers{};
for (int i = 0; i < NPEERS; ++i) {
if (!peer_scratch[i] || !peer_flags[i])
throw std::runtime_error("all_gather: null peer scratch/flag pointer at index " +
std::to_string(i));
peers.scratch[i] = (int4*)peer_scratch[i];
peers.flags[i] = (unsigned int*)peer_flags[i];
peers.rank[i] = (i < (int)rank) ? i : i + 1;
}
// As in the all-reduce sharing this instance, check against the allocations: scratch backs 2*WS
// slots of the stride, flags one word per (block, source rank), seq a handshake and a parity
// word per block of capacity.
const long slot_bytes = 2L * WS * (long)slot_stride16 * 16;
const long flag_bytes = (long)nb * WS * (long)sizeof(unsigned int);
halo_ar_check_extent(my_scratch, slot_bytes, "all_gather", "my_scratch");
halo_ar_check_extent(my_flags, flag_bytes, "all_gather", "my_flags");
halo_ar_check_extent(seq_ctrs, 2L * HALO_AR_WIDE_MAX_BLOCKS * (long)sizeof(unsigned int),
"all_gather", "seq_ctrs");
for (int i = 0; i < NPEERS; ++i) {
halo_ar_check_extent(peer_scratch[i], slot_bytes, "all_gather", "peer scratch");
halo_ar_check_extent(peer_flags[i], flag_bytes, "all_gather", "peer flags");
}
hipStream_t st = (hipStream_t)stream_i;
(chain ? halo_r4dchain_ar_gather_wide<WS> : halo_ar_gather_wide_kernel<WS>)<<<nb, nt, 0, st>>>(
(const int4*)inp, peers, (const int4*)my_scratch, (int4*)out, (unsigned int*)my_flags,
(unsigned int*)seq_ctrs, n16, (int)slot_stride16, (int)rank, row16, dp16, sp16, (int)drain,
(int)acq, (int)pub, (long long*)gate, (unsigned int*)gate_ctr);
}
/* Three ranks, e.g. Qwen3.8-Flash-Next padded to 36 heads / 3 KV heads (docs/TP3.md). */
void halo_ar_gather_3rank(long peer_scratch0, long peer_scratch1,
long peer_flags0, long peer_flags1, long my_scratch,
long my_flags, long seq_ctrs, long slot_stride16, long inp, long out,
long n_elem, long dtype, long rank, long row_elems, long stream,
long nblocks, long nthreads, long drain, long acq, long pub, long dpitch,
long spitch) {
const long ps[2] = {peer_scratch0, peer_scratch1};
const long pf[2] = {peer_flags0, peer_flags1};
/* No link gate at three ranks: the row answers 1, the engine releases by barrier. */
ar_gather_wide_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
0L, 0L, dpitch, spitch);
}
void halo_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
long peer_flags0, long peer_flags1, long peer_flags2, long my_scratch,
long my_flags, long seq_ctrs, long slot_stride16, long inp, long out,
long n_elem, long dtype, long rank, long row_elems, long stream,
long nblocks, long nthreads, long drain, long acq, long pub, long gate,
long gate_ctr, long dpitch, long spitch, long chain) {
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
ar_gather_wide_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
gate, gate_ctr, dpitch, spitch, chain != 0);
}
void halo_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
long peer_scratch3, long peer_scratch4, long peer_scratch5,
long peer_scratch6, long peer_flags0, long peer_flags1, long peer_flags2,
long peer_flags3, long peer_flags4, long peer_flags5, long peer_flags6,
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
long inp, long out, long n_elem, long dtype, long rank, long row_elems,
long stream, long nblocks, long nthreads, long drain, long acq,
long pub, long dpitch, long spitch) {
const long ps[7] = {peer_scratch0, peer_scratch1, peer_scratch2, peer_scratch3,
peer_scratch4, peer_scratch5, peer_scratch6};
const long pf[7] = {peer_flags0, peer_flags1, peer_flags2, peer_flags3,
peer_flags4, peer_flags5, peer_flags6};
/* No link gate at eight ranks: the row answers 1, the engine releases by barrier. */
ar_gather_wide_launch<8>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
0L, 0L, dpitch, spitch);
}