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

224 lines
12 KiB
Plaintext

/* Derived from libr4d/r4d_qsa_work_bf16.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// The QSA indexer's work list: which blocks complete this step, where their raw keys are, and
// where their block key goes.
//
// qsa_block_key reads each block's `ratio` raw keys from consecutive rows of one plane, but a
// block completing at decode has its keys partly in the tail and partly in this step, and blocks
// belong to different sequences. This op finds the completed blocks, gathers each one's raw keys
// into consecutive rows of a staging plane, and writes its destination page and first-token
// position: exactly qsa_block_key's three operands (docs/QSA.md). The copy is small (1 KiB per
// completing block) and keeps qsa_block_key single-source.
//
// The tail is a per-layer linear KV state ([slots, 1, ring, n]; `sidx` names the sequence's slot,
// not the step-local batch row); see halo_qsa_tail_store_bf16.hip, including why ring is
// ratio + n_spec. Keys are read at row q % ring to match.
//
// The launch is bounded on the host (ceil(T / ratio) + n_seq: one block per `ratio` tokens plus
// one straddling block per sequence). Work items past the real count get page = -1, which
// qsa_block_key skips.
//
// Returns a RAD status rather than throwing, for the reason given in halo_gemm_fp8a8.hip.
#include "halo.h"
#include "halo_args.h"
#include "halo_emit.h"
#include <hip/hip_runtime.h>
#include <cstdint>
namespace {
constexpr int kThreads = 128;
/* The sequence loop's bound, checked by the launcher against the real count from cu_seqlens. Not a
* row constraint on the declared seqs (rad-convert declares 256, which must still resolve). */
constexpr int kMaxSeq = 256;
__global__ __launch_bounds__(kThreads) void qsa_work_kernel(
const uint16_t* __restrict__ k, const int32_t* __restrict__ pos,
const int32_t* __restrict__ cu, const uint16_t* __restrict__ state,
const int32_t* __restrict__ sidx, const int32_t* __restrict__ bt,
uint16_t* __restrict__ stage, int32_t* __restrict__ page, int32_t* __restrict__ bpos,
int32_t* __restrict__ nc,
int n, int ratio, int ring, int seqs, int max_work, int k_ld, int s_slot, int s_row,
int sidx_ld, int bt_ld, int g_ld, const int32_t* __restrict__ rope, int rplane,
int bstep) {
const int w = (int)blockIdx.x;
/* Complete blocks per sequence (needed by the scorer and the selection), written by the first
* `seqs` workgroups instead of a separate launch. */
if (w < seqs && threadIdx.x == 0) {
const int t1 = cu[w + 1];
nc[w] = t1 > cu[w] ? (pos[t1 - 1] + 1) / ratio : 0;
}
if (w >= max_work) return;
/* The sequence owning work item w, by a running count recomputed in every workgroup (seqs is
* small; a prefix would need a second launch). */
int s = -1, j = 0;
int run = 0;
for (int i = 0; i < seqs && i < kMaxSeq; ++i) {
const int t0 = cu[i], t1 = cu[i + 1];
if (t1 <= t0) continue;
const int p0 = pos[t0], p1 = pos[t1 - 1];
/* A block completes this step when its last token, b * ratio + ratio - 1, is in [p0, p1].
* Both bounds use non-negative numerators: C division truncates toward zero, so
* (p1 - (ratio - 1)) / ratio would be 0, not -1, for p1 < ratio - 1. */
const int lo = p0 / ratio; /* ceil((p0-ratio+1)/ratio) */
const int hi = (p1 + 1) / ratio - 1; /* floor((p1-ratio+1)/ratio) */
const int cnt = hi >= lo ? hi - lo + 1 : 0;
if (w < run + cnt) { s = i; j = w - run + lo; break; }
run += cnt;
}
if (s < 0) {
/* Past the real count: page = -1 makes the host's bound safe. */
if (threadIdx.x == 0) {
page[w] = -1;
for (int c = 0; c < (rope ? 3 : 1); ++c) bpos[(size_t)w * bstep + c] = 0;
}
return;
}
const int b = j; /* the block index within the sequence */
const int t0 = cu[s];
const int p0 = pos[t0];
/* Column 0 is the committed slot; -1 marks a sequence with no state, whose blocks cannot be
* built (page -1, no gather). */
const int si = sidx[(size_t)s * sidx_ld];
if (threadIdx.x == 0) {
page[w] = si < 0 ? -1 : bt[(size_t)s * bt_ld + b];
if (!rope) {
bpos[(size_t)w * bstep] = b * ratio;
} else {
/* Rotary position of the block's first token, three components side by side, items
* bstep apart. A first token in this step reads its own components. One from an
* earlier step is text (only a decode row or a prompt's last chunk leaves a block open,
* and a prompt ends in text), so it rotates at its index plus the shift carried by
* this step's first row of the sequence. */
const int f = b * ratio;
for (int c = 0; c < 3; ++c)
bpos[(size_t)w * bstep + c] =
f >= p0 ? rope[(size_t)c * rplane + t0 + (f - p0)] : f + (rope[t0] - p0);
}
}
if (si < 0) return;
/* The block's raw keys, each from this step or from the tail. */
for (int i = 0; i < ratio; ++i) {
const int q = b * ratio + i; /* this key's position */
const uint16_t* src;
if (q >= p0) src = k + (size_t)(t0 + (q - p0)) * k_ld;
else src = state + (size_t)si * s_slot + (size_t)(q % ring) * s_row;
uint16_t* dst = stage + (size_t)(w * ratio + i) * g_ld;
for (int d = (int)threadIdx.x; d < n; d += kThreads) dst[d] = src[d];
}
}
} // namespace
// k [T][n] bf16, this step's raw index keys
// pos [T] i32
// cu [seqs + 1] i32
// state [slots][ring][n] bf16, the tail group's cache for this layer
// sidx [seqs][sidx_ld] i32, the tail group's state row; column 0 is the slot
// bt [seqs][bt_ld] i32, the block-key group's block table
// stage [max_work * ratio][n] bf16
// page [max_work] i32, -1 past the real count
// bpos [max_work][bstep] i32, a work item's first-token position at [w * bstep]
// rope [3][rplane] i32, optional: this step's rotary components (RadBatch::
// rope_pos); present, a work item's three components are
// bpos[w * bstep + 0..2]
static int halo_qsa_work_bf16_emit(long k, long pos, long cu, long state, long sidx, long bt,
long stage, long page, long bpos, long nc, int n, int ratio,
int ring, int seqs, int max_work, int k_ld, int s_slot,
int s_row, int sidx_ld, int bt_ld, int g_ld, long rope,
int rplane, int bstep, RadStream stream) {
if (!k || !pos || !cu || !state || !sidx || !bt || !stage || !page || !bpos || !nc)
return HALO_NO(RAD_E_INVAL);
if (n <= 0 || ratio <= 0 || ring < ratio || seqs <= 0 || max_work <= 0 || seqs > kMaxSeq)
return HALO_NO(RAD_E_SHAPE);
if (!k_ld) k_ld = n;
if (!s_row) s_row = n;
if (!g_ld) g_ld = n;
if (!sidx_ld) sidx_ld = 1;
return HALO_EMIT(stream, qsa_work_kernel, dim3((unsigned)max_work), dim3(kThreads), 0,
(const uint16_t*)k, (const int32_t*)pos, (const int32_t*)cu,
(const uint16_t*)state, (const int32_t*)sidx, (const int32_t*)bt,
(uint16_t*)stage, (int32_t*)page, (int32_t*)bpos, (int32_t*)nc, n, ratio,
ring, seqs, max_work, k_ld, s_slot, s_row, sidx_ld, bt_ld, g_ld,
(const int32_t*)rope, rplane, bstep);
}
// ============================================================== the plugin ABI entry point
enum { QW_K = 0, QW_POS, QW_CU, QW_STATE, QW_SIDX, QW_BT, QW_ROPE, QW_STAGE, QW_PAGE, QW_BPOS,
QW_NC };
extern "C" int halo_rad_qsa_work(const RadArgs* a, RadStream stream) {
const RadTensor* k = halo_opt(a, QW_K);
const RadTensor* po = halo_opt(a, QW_POS);
const RadTensor* cu = halo_opt(a, QW_CU);
const RadTensor* tl = halo_opt(a, QW_STATE);
const RadTensor* sx = halo_opt(a, QW_SIDX);
const RadTensor* bt = halo_opt(a, QW_BT);
const RadTensor* rp = halo_opt(a, QW_ROPE); /* optional: multi-component positions */
const RadTensor* st = halo_opt(a, QW_STAGE);
const RadTensor* pg = halo_opt(a, QW_PAGE);
const RadTensor* bp = halo_opt(a, QW_BPOS);
const RadTensor* nc = halo_opt(a, QW_NC);
if (!k || !po || !cu || !tl || !sx || !bt || !st || !pg || !bp || !nc)
return HALO_NO(RAD_E_INVAL);
if (k->rank != 2 || bt->rank != 2 || st->rank != 2) return HALO_NO(RAD_E_SHAPE);
/* The tail state arrives as its group declared it: [slots, heads, ring, n], heads 1. See
* halo_qsa_tail_store_bf16.hip. */
if (tl->rank != 4 || tl->shape[1] != 1) return HALO_NO(RAD_E_SHAPE);
if (tl->stride[3] != 1) return HALO_NO(RAD_E_STRIDE);
/* bt is the block-key group's table, not the attention group's; both are paged at `ratio`,
* so they share block indices (docs/QSA.md). */
const long long n = halo_geti_or(a, "n", 0);
const long long ratio = halo_geti_or(a, "ratio", 0);
const long long ring = halo_geti_or(a, "ring", 0);
if (n <= 0 || ratio <= 0 || ring < ratio) return HALO_NO(RAD_E_INVAL);
const long long seqs = halo_numel(cu) - 1;
const long long T = k->shape[0];
if (seqs < 1 || T < 1 || halo_numel(po) < T) return HALO_NO(RAD_E_SHAPE);
if (k->shape[1] != n || st->shape[1] != n) return HALO_NO(RAD_E_SHAPE);
if (tl->shape[2] < ring || tl->shape[3] != n) return HALO_NO(RAD_E_SHAPE);
if (bt->shape[0] < seqs) return HALO_NO(RAD_E_SHAPE);
const long long sxp = sx->rank >= 2 ? halo_row_pitch(sx) : 1;
if (sxp < 1 || halo_dim(sx, 0) < seqs) return HALO_NO(RAD_E_SHAPE);
/* The work count comes from `page`, and `stage` must hold ratio rows per item. */
const long long mw = halo_numel(pg);
if (mw < 1 || st->shape[0] < mw * ratio) return HALO_NO(RAD_E_SHAPE);
/* bpos is one position per work item, or a rank-2 [>= work, 1 or 3] column slice of a
* [max_work, 3] buffer (components side by side, items a row pitch apart); three columns are
* required with `rope`. rope is [3, rplane] component-major, rplane >= the step's tokens. */
long long bstep = 1;
if (bp->rank == 2) {
if (bp->shape[0] < mw || bp->stride[1] != 1 || bp->stride[0] < bp->shape[1] ||
(rp && bp->shape[1] < 3))
return HALO_NO(RAD_E_SHAPE);
bstep = bp->stride[0];
} else if (rp || halo_numel(bp) < mw) {
return HALO_NO(RAD_E_SHAPE);
}
if (rp && (rp->rank != 2 || rp->shape[0] < 3 || rp->shape[1] < T || !halo_contig(rp) ||
rp->dtype != RAD_I32))
return HALO_NO(RAD_E_SHAPE);
if (halo_numel(nc) < seqs) return HALO_NO(RAD_E_SHAPE);
/* nc is written by the first `seqs` workgroups, so the launch must be at least that wide. */
if (mw < seqs) return HALO_NO(RAD_E_SHAPE);
const long long kp = halo_row_pitch(k);
const long long bp2 = halo_row_pitch(bt), sp = halo_row_pitch(st);
if (kp < 0 || bp2 < 0 || sp < 0) return HALO_NO(RAD_E_STRIDE);
if (k->dtype != RAD_BF16 || tl->dtype != RAD_BF16) return HALO_NO(RAD_E_DTYPE);
return halo_qsa_work_bf16_emit(halo_addr(k), halo_addr(po), halo_addr(cu), halo_addr(tl),
halo_addr(sx), halo_addr(bt), halo_addr(st), halo_addr(pg),
halo_addr(bp), halo_addr(nc), (int)n, (int)ratio, (int)ring,
(int)seqs, (int)mw, (int)kp, (int)tl->stride[0],
(int)tl->stride[2], (int)sxp, (int)bp2, (int)sp,
halo_addr(rp), rp ? (int)rp->shape[1] : 0, (int)bstep, stream);
}