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_dflash_select_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

274 lines
14 KiB
Plaintext

/* Derived from libr4d/r4d_dflash_select_bf16.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// DFlash2's candidate selector: edge scores and the path walk, one workgroup per sequence.
//
// A block-diffusion drafter proposes every position of a block in one backbone pass, so position
// l+1 cannot condition on the choice at l. The selector restores that dependence: each position
// keeps the draft head's top K candidates, a low-rank bilinear form scores each (predecessor,
// candidate) edge between consecutive positions, and a greedy walk picks one token per position.
//
// score[s, l, c] = unary[s, l, c]
// + sum_r pred[pid(s,l)][r] * hp[s,l][r] * succ[cand[s,l,c]][r]
//
// pid(s, 0) = anchor[s * anchor_stride] the token the target just committed
// pid(s, l) = cand[s, l-1, chosen(s, l-1)] the walk's own previous pick
//
// `hp` is optional; absent means ones, giving the bigram form score = unary + <pred[pid],
// succ[cand]>. That is DSpark's Markov head (a rank-256 learned bigram bias
// markov_w2 @ markov_w1[prev] on the draft logits, prev = anchor at position 0, then the last
// pick). The reference runs it as a full-vocabulary [rank, n_vocab] GEMM per position, serially;
// here it costs only the candidate rows of two codebooks. Restricting to the base head's top K
// affects only acceptance, since drafts are verified.
//
// The walk is serial in l, which is why this is a kernel rather than a batched GEMM plus argmax.
// The reference (vLLM's _score_edges einsum) materialises [steps, K, K] scores and reads one row
// per step, since the predecessor is only known after the previous step. Here the predecessor's
// codebook row is fetched after the previous argmax, inside one workgroup.
//
// The arithmetic is tiny (steps * K * R multiply-adds per sequence, 28K at 7/16/256), so this is a
// latency kernel: one workgroup per sequence, no global synchronisation.
#include <hip/hip_runtime.h>
#include "halo_gdn_args.h"
#include "halo_dflash_conv_body.h" // halo_bf2f, halo_u16
#include "halo_common.h" // halo_wave_sum
#include "halo_chain.h"
#define DSL_THREADS 256
#define DSL_WAVE 32
#define DSL_WAVES (DSL_THREADS / DSL_WAVE)
#define DSL_MAX_K 32 // the entry point rejects more; ssc is sized for it
#define DSL_MAX_R 512 // and sa for this
// cand [rows, cand_ld] i32 global token ids, row = s * steps + l
// unary [rows, cand_ld] f32 the draft head's logit for each candidate (row_topk's value plane)
// hp [rows, hp_ld] bf16 hidden_projection(x) for the same row
// anchor [M * anchor_stride] i32 the last committed token of each sequence, at stride s
// pred [vocab, R] bf16 predecessor codebook
// succ [vocab, R] bf16 successor codebook
// out [M, out_ld] i32 the chosen token per step
//
// Ids outside [0, V) do occur (row_topk_merge reports an unfilled slot as -1, and step 0's
// predecessor is whatever the anchor plane holds). As in ref_dflash_select, an out-of-range
// predecessor contributes nothing and an out-of-range candidate scores its unary term alone.
// The body; the plain kernel and its chained form (halo_chain.h) wrap it below.
__device__ __forceinline__ void halo_dflash_select_body(
const int* __restrict__ cand, const float* __restrict__ unary,
const halo_u16* __restrict__ hp, const int* __restrict__ anchor,
const halo_u16* __restrict__ pred, const halo_u16* __restrict__ succ,
int* __restrict__ out, int steps, int K, int R, int V,
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 & (DSL_WAVE - 1);
const int wave = tid / DSL_WAVE;
// a[r] = pred[pid][r] * hp[r]: the candidate-independent half, computed once per step.
__shared__ float sa[DSL_MAX_R];
__shared__ float ssc[DSL_MAX_K];
__shared__ int spid; // the token the previous step chose: this step's predecessor
// Candidates per wave, at most: c = wave, wave + DSL_WAVES, ...
constexpr int CPW = (DSL_MAX_K + DSL_WAVES - 1) / DSL_WAVES;
for (int l = 0; l < steps; ++l) {
const long row = (long)s * steps + l;
// Candidate ids and unary scores do not depend on the predecessor, so load them first,
// overlapping the codebook row fetch. A wave past K re-reads the last candidate and scores
// nothing.
int cid[CPW];
float un[CPW];
#pragma unroll
for (int i = 0; i < CPW; ++i) {
const int c = wave + i * DSL_WAVES;
const long at = row * cand_ld + (c < K ? c : K - 1);
cid[i] = cand[at];
un[i] = unary[at];
}
// Step 0's predecessor is the committed anchor; later steps use the previous pick, left in
// LDS by thread 0 before the barrier that ended that step.
const int pid = (l == 0) ? anchor[(long)s * anchor_stride] : spid;
const bool pid_ok = pid >= 0 && pid < V;
const halo_u16* __restrict__ prow = pred + (long)(pid_ok ? pid : 0) * R;
/* hp absent means ones: the predecessor's row is used as is, without reading a plane. */
if (!pid_ok) {
for (int r = tid; r < R; r += DSL_THREADS) sa[r] = 0.0f;
} else if (hp) {
const halo_u16* __restrict__ hrow = hp + row * hp_ld;
for (int r = tid; r < R; r += DSL_THREADS)
sa[r] = halo_bf2f(prow[r]) * halo_bf2f(hrow[r]);
} else {
for (int r = tid; r < R; r += DSL_THREADS)
sa[r] = halo_bf2f(prow[r]);
}
__syncthreads();
// One wave per candidate. R is a multiple of the wave size, so the reduction is a barrier-free
// xor butterfly. An out-of-range candidate reads row 0 and drops the sum, keeping the loads
// out of a branch.
#pragma unroll
for (int i = 0; i < CPW; ++i) {
const int c = wave + i * DSL_WAVES;
if (c < K) {
const bool ok = cid[i] >= 0 && cid[i] < V;
const halo_u16* __restrict__ srow = succ + (long)(ok ? cid[i] : 0) * R;
float acc = 0.0f;
#pragma unroll 8
for (int r = lane; r < R; r += DSL_WAVE) acc += sa[r] * halo_bf2f(srow[r]);
acc = halo_wave_sum(ok ? acc : 0.0f);
if (lane == 0) ssc[c] = acc + un[i];
}
}
__syncthreads();
// K <= 32 and the walk is serial, so one thread does the argmax. Ties go to the lower index
// (row_topk's rule, the more likely candidate). Seeded at -inf, as in ref, so a NaN in the
// first slot cannot win by position.
if (tid == 0) {
int best = 0;
float bv = -INFINITY;
for (int c = 0; c < K; ++c)
if (ssc[c] > bv) { bv = ssc[c]; best = c; }
const int tok = cand[row * cand_ld + best];
spid = tok;
out[(long)s * out_ld + l] = tok;
}
// The next step reads spid and overwrites sa, which other threads may still be reading.
__syncthreads();
}
}
__global__ __launch_bounds__(DSL_THREADS) void halo_dflash_select_kernel(
const int* __restrict__ cand, const float* __restrict__ unary,
const halo_u16* __restrict__ hp, const int* __restrict__ anchor,
const halo_u16* __restrict__ pred, const halo_u16* __restrict__ succ,
int* __restrict__ out, int steps, int K, int R, int V,
int cand_ld, int hp_ld, int out_ld, int anchor_stride) {
halo_dflash_select_body(cand, unary, hp, anchor, pred, succ, out, steps, K, R, V, cand_ld, hp_ld,
out_ld, anchor_stride);
}
__global__ __launch_bounds__(DSL_THREADS) void halo_r4dchain_dflash_select(
const int* __restrict__ cand, const float* __restrict__ unary,
const halo_u16* __restrict__ hp, const int* __restrict__ anchor,
const halo_u16* __restrict__ pred, const halo_u16* __restrict__ succ,
int* __restrict__ out, int steps, int K, int R, int V,
int cand_ld, int hp_ld, int out_ld, int anchor_stride) {
halo_chain_wait();
halo_dflash_select_body(cand, unary, hp, anchor, pred, succ, out, steps, K, R, V, cand_ld, hp_ld,
out_ld, anchor_stride);
halo_chain_done();
}
/* `chain`: the chained form (the plugin entry, where the row resolved with `chain`). */
static int dflash_select_emit(
const void* cand, const void* unary, const void* hp, const void* anchor,
const void* pred, const void* succ, void* out,
int M, int steps, int K, int R, int V, int cand_ld, int hp_ld, int out_ld,
int anchor_stride, void* stream, bool chain);
extern "C" int halo_dflash_select_bf16(
const void* cand, const void* unary, const void* hp, const void* anchor,
const void* pred, const void* succ, void* out,
int M, int steps, int K, int R, int V, int cand_ld, int hp_ld, int out_ld,
int anchor_stride, void* stream)
{
return dflash_select_emit(cand, unary, hp, anchor, pred, succ, out, M, steps, K, R, V, cand_ld,
hp_ld, out_ld, anchor_stride, stream, false);
}
static int dflash_select_emit(
const void* cand, const void* unary, const void* hp, const void* anchor,
const void* pred, const void* succ, void* out,
int M, int steps, int K, int R, int V, int cand_ld, int hp_ld, int out_ld,
int anchor_stride, void* stream, bool chain)
{
if (M <= 0 || steps <= 0) return 0;
if (K < 1 || K > DSL_MAX_K) return -1;
if (R < DSL_WAVE || R > DSL_MAX_R || (R % DSL_WAVE)) return -1;
if (V < 1) return -1;
if (cand_ld < K || out_ld < steps || anchor_stride < 1) return -1;
/* hp may be null (the bigram form); then hp_ld is unused. */
if (hp && hp_ld < R) return -1;
hipLaunchKernelGGL((chain ? halo_r4dchain_dflash_select : halo_dflash_select_kernel), dim3(M),
dim3(DSL_THREADS), 0, (hipStream_t)stream,
(const int*)cand, (const float*)unary, (const halo_u16*)hp,
(const int*)anchor, (const halo_u16*)pred, (const halo_u16*)succ,
(int*)out, steps, K, R, V, cand_ld, hp_ld, out_ld, anchor_stride);
return (int)hipGetLastError();
}
// ============================================================== the plugin ABI entry points
// Shared operand helpers: halo_gdn_args.h.
/* ------------------------------------------------------------------ dflash select */
enum { DS_CAND = 0, DS_UNARY, DS_HP, DS_ANCHOR, DS_PRED, DS_SUCC, DS_OUT };
/* A rank-2 [rows, width] operand with an explicit row pitch. `width` is checked, not derived:
* three planes are read at the same row index, and a wrong-width plane could still have a
* plausible row count. */
static long long ds_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 = halo_row_pitch(t);
return (pitch >= width) ? pitch : -1;
}
extern "C" int halo_rad_dflash_select(const RadArgs* a, RadStream s) {
const RadTensor* cand = halo_opt(a, DS_CAND);
const RadTensor* un = halo_opt(a, DS_UNARY);
const RadTensor* hp = halo_opt(a, DS_HP);
const RadTensor* anc = halo_opt(a, DS_ANCHOR);
const RadTensor* pr = halo_opt(a, DS_PRED);
const RadTensor* sc = halo_opt(a, DS_SUCC);
const RadTensor* out = halo_opt(a, DS_OUT);
/* Only hp may be absent. */
if (!cand || !un || !anc || !pr || !sc || !out) return HALO_NO(RAD_E_INVAL);
const long long steps = halo_geti_or(a, "steps", 0);
const long long K = halo_geti_or(a, "top_k", 0);
const long long R = halo_geti_or(a, "rank", 0);
const long long V = halo_geti_or(a, "n_vocab", 0);
/* M comes from the output plane (one row per sequence), not the parameter: RadArgs carries the
* band's bound (max_seqs) for M, as for q_len in fill_paged. The parameter would demand
* max_seqs * steps candidate rows on a one-sequence step. */
if (out->rank != 2) return HALO_NO(RAD_E_SHAPE);
const long long M = out->shape[0];
if (M <= 0 || steps <= 0 || K <= 0 || R <= 0 || V <= 0) return HALO_NO(RAD_E_INVAL);
/* The input planes are read at row s*steps+l and must hold M*steps rows; a short plane would
* silently draft from another request's candidates. */
const long long rows = M * steps;
const long long cand_ld = ds_plane(cand, RAD_I32, K, rows);
const long long un_ld = ds_plane(un, RAD_F32, K, rows);
const long long out_ld = ds_plane(out, RAD_I32, steps, M);
/* Absent hp may arrive as a missing operand or as an empty one with null data (operands are
* positional); both mean the bigram form. */
const bool has_hp = hp && hp->data;
const long long hp_ld = has_hp ? ds_plane(hp, RAD_BF16, R, rows) : 0;
if (cand_ld < 0 || un_ld < 0 || hp_ld < 0 || out_ld < 0) return HALO_NO(RAD_E_SHAPE);
/* The kernel indexes cand and unary with one pitch. */
if (cand_ld != un_ld) return HALO_NO(RAD_E_STRIDE);
/* The codebooks are gathered by global token id, and candidates arrive merged across ranks, so
* both must be replicated over the whole vocabulary; a row-sharded one would fail silently. */
if (pr->dtype != RAD_BF16 || sc->dtype != RAD_BF16) return HALO_NO(RAD_E_DTYPE);
if (!halo_contig(pr) || !halo_contig(sc)) return HALO_NO(RAD_E_STRIDE);
if (halo_numel(pr) < V * R || halo_numel(sc) < V * R) return HALO_NO(RAD_E_SHAPE);
/* anchor is usually the step's token_ids (`block` ids per sequence, anchor first), hence the
* stride; a packed [M] array is stride 1. */
const long long astr = halo_geti_or(a, "anchor_stride", 1);
if (anc->dtype != RAD_I32 && anc->dtype != RAD_U32) return HALO_NO(RAD_E_DTYPE);
if (astr < 1 || !halo_contig(anc)) return HALO_NO(RAD_E_SHAPE);
if (halo_numel(anc) < (M - 1) * astr + 1) return HALO_NO(RAD_E_SHAPE);
/* The chained form at decode heights, where the row resolved with `chain`. */
const bool ch = halo_geti_or(a, "chain", 0) != 0 && M <= 64;
return HALO_RAW(dflash_select_emit(cand->data, un->data, has_hp ? hp->data : nullptr,
anc->data, pr->data, sc->data,
out->data, (int)M, (int)steps, (int)K, (int)R, (int)V,
(int)cand_ld, (int)hp_ld, (int)out_ld, (int)astr, s, ch));
}