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_2rank.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

184 lines
9.5 KiB
Plaintext

/* Derived from libr4d/r4d_ar_gather_2rank.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// halo_ar_gather_2rank.hip: halo_ar_gather_2rank -- multi-block one-shot P2P all-GATHER, exactly two
// ranks, for dual R9700 (gfx1201/RDNA4, PCIe).
//
// A kernel rather than halo_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 "halo.h"
#include "halo_ar_seq.h"
#include <cstdint>
#include <stdexcept>
#include <string>
#define HALO_AR_GATHER_SPIN_MAX 4000000000ULL
#define HALO_AR_GATHER_MAX_BLOCKS 512
__device__ __forceinline__ void halo_g_store_rel(unsigned int* p, unsigned int v) {
__hip_atomic_store(p, v, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_SYSTEM);
}
__device__ __forceinline__ unsigned int halo_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 halo_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 halo_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 halo_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) {
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
__shared__ unsigned int s_seq, s_slot;
halo_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 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 (halo_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[halo_g_src(k, row16, n16, sp16)], v1 = in4[halo_g_src(k + nt, row16, n16, sp16)],
v2 = in4[halo_g_src(k + 2 * nt, row16, n16, sp16)],
v3 = in4[halo_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 += halo_archk_u4(v0, 4u * (unsigned)k) + halo_archk_u4(v1, 4u * (unsigned)(k + nt)) +
halo_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + halo_archk_u4(v3, 4u * (unsigned)(k + 3 * nt));
out4[halo_g_dst(k, row16, n16, rank, dp16)] = v0;
out4[halo_g_dst(k + nt, row16, n16, rank, dp16)] = v1;
out4[halo_g_dst(k + 2 * nt, row16, n16, rank, dp16)] = v2;
out4[halo_g_dst(k + 3 * nt, row16, n16, rank, dp16)] = v3;
}
for (; k < end; k += nt) {
int4 v = in4[halo_g_src(k, row16, n16, sp16)];
ps[k] = v;
hck += halo_archk_u4(v, 4u * (unsigned)k);
out4[halo_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.
halo_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s);
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_waitcnt vmcnt(0)" ::: "memory"); // pre-gfx12 store wait
__syncthreads();
if (tid == 0) {
if (drain == 2) __threadfence_system();
halo_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 (halo_ar_seq_behind(halo_g_load_acq(&my_flags[b]), s)) {
if (++z > HALO_AR_GATHER_SPIN_MAX) __builtin_trap();
}
}
__syncthreads();
if (acq) __threadfence_system();
halo_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
for (int k = start + tid; k < end; k += nt) h += halo_archk_u4(ms[k], 4u * (unsigned)k);
});
// 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[halo_g_dst(k, row16, n16, who, dp16)] = ms[k];
out4[halo_g_dst(k + nt, row16, n16, who, dp16)] = ms[k + nt];
out4[halo_g_dst(k + 2 * nt, row16, n16, who, dp16)] = ms[k + 2 * nt];
out4[halo_g_dst(k + 3 * nt, row16, n16, who, dp16)] = ms[k + 3 * nt];
}
for (; k < end; k += nt) out4[halo_g_dst(k, row16, n16, who, dp16)] = ms[k];
}
}
void halo_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) {
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 > HALO_AR_GATHER_MAX_BLOCKS) nb = HALO_AR_GATHER_MAX_BLOCKS;
// Block capacity of the counter allocation shared with the all-reduce (halo_ar_seq.h).
const unsigned nbw = (unsigned)halo_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
halo_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);
}