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_attn_args.h
T
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

179 lines
8.8 KiB
C++

/* Derived from libr4d/r4d_attn_args.h by the libhalo rename transform (2026-10-26);
* see libhalo/README.md. Op schemas, row names and stored weight layouts are
* shared with libr4d by design. Everything else in this file is libhalo's own
* and diverges as it is measured on gfx1151. */
/* halo_attn_args.h -- the paged-attention operand reader shared by every paged kernel geometry.
*
* The paged kernels (h256/gqa6, h128/gqa4, the gqa8/gqa12 instantiations; prefill and decode) take
* the same operands in the same order, so one reader serves them all.
*
* HaloArgs uses RadTensor's convention (strides in elements, spec 2.1), so this is field extraction
* plus the per-call validation a constraint cannot express: contiguity, alignment and buffer
* sizes, each refused with the code that names the class.
*
* Kernel return codes for an unserved shape (-1 head_dim or block_size, -2 gqa, -4 too many decode
* rows, -100-hipError for a launch failure) pass through unchanged. Getting one means a selection
* bug, and the value says which.
*/
#ifndef HALO_ATTN_ARGS_H
#define HALO_ATTN_ARGS_H
#include "halo.h"
#include "halo_args.h"
#include <cmath>
#include <cstring>
/* Checks libhalo's compiled-in geometry against the row table, so a libhalo rebuilt for a
* different head size cannot silently change what the rows mean. */
static inline int check_dims_once() {
int hd = 0, gqa = 0, bs = 0, rows = 0;
halo_attn_dims(&hd, &gqa, &bs, &rows);
return (hd == 256 && gqa == 6 && bs == 16 && rows == 64) ? RAD_OK : RAD_E_SHAPE;
}
/* Operand indices, from oAttnPaged in halo_rows.cpp. */
enum { AP_Q = 0, AP_KV, AP_BT, AP_SEQ, AP_KDS, AP_VDS, AP_CU, AP_OUT };
/* Fill HaloArgs from a RadArgs. Returns RAD_OK, or the negative code naming what is wrong.
*
* Ragged batches: the kernels take a scalar q_len and an optional cu_seqlens. Without cu_seqlens
* every sequence must have the same query length, which RAD_PHASE_MIXED steps (decode rows riding
* on another sequence's prefill chunk, core/sched/scheduler.cpp) do not satisfy; refusing them
* would abort the engine. So cu_seqlens is used whenever present, and the uniform arithmetic
* (q_len = total_q / num_seqs) only when it is not. In the ragged case q_len is the batch maximum,
* taken from the parameter the arch block issued the op with (batch->max_q_len).
*
* `q_pitched`: q may be a column slice of a wider row (attn_paged_qrow), its pitch in r->q_ld. Only
* the split-KV decode kernel reads q_ld; every other caller leaves this off.
*/
/* `row` is a token's bytes (elements for a 16-bit cache) in a (block, head) page: 2 * head_dim,
* or 25 * head_dim / 16 for the fp8_v4 cache (E4M3 K, int4 V, f16 group scales). */
static inline int fill_paged_row(const RadArgs* a, HaloArgs* r, int expect_head_dim, long long row,
bool q_pitched = false) {
const RadTensor* q = halo_opt(a, AP_Q);
const RadTensor* kv = halo_opt(a, AP_KV);
const RadTensor* bt = halo_opt(a, AP_BT);
const RadTensor* su = halo_opt(a, AP_SEQ);
const RadTensor* cu = halo_opt(a, AP_CU);
const RadTensor* o = halo_opt(a, AP_OUT);
if (!q || !kv || !bt || !su || !o) return HALO_NO(RAD_E_INVAL);
if (q->rank < 2 || o->rank < 2 || bt->rank != 2 || su->rank < 1) return HALO_NO(RAD_E_SHAPE);
if (kv->rank != 4) return HALO_NO(RAD_E_SHAPE);
/* The query may be [tokens, n_head, head_dim] or flattened to [tokens, n_head*head_dim]
* (a gated attention block's de-interleaved query). Both are the same dense block, so any rank
* is accepted; head_dim comes from the op parameter, which makes the flattened form readable. */
const long long head_dim = expect_head_dim;
const long long total_q = q->shape[0];
const long long num_seqs = bt->shape[0];
if (num_seqs <= 0 || total_q <= 0 || head_dim <= 0) return HALO_NO(RAD_E_SHAPE);
if (halo_numel(q) % (total_q * head_dim)) return HALO_NO(RAD_E_SHAPE);
const long long q_heads = halo_numel(q) / (total_q * head_dim);
if (q_heads <= 0) return HALO_NO(RAD_E_SHAPE);
/* Ragged exactly when cu_seqlens is present; see the note above fill_paged. */
const bool ragged = (cu != nullptr) && cu->data;
if (ragged) {
if (cu->rank != 1 || cu->shape[0] != num_seqs + 1) return HALO_NO(RAD_E_SHAPE);
if (!halo_contig(cu)) return HALO_NO(RAD_E_STRIDE);
} else if (total_q % num_seqs) {
return HALO_NO(RAD_E_SHAPE);
}
/* q and out are indexed as one dense [total_q, q_heads, head_dim] block; the kernel has no
* pitch argument for either -- except q under q_pitched, whose rows may be apart: dense inside
* a row, a pitch in whole 8-byte loads (the kernel reads q four elements at a time). */
long long q_ld = 0;
if (q_pitched && !halo_contig(q)) {
q_ld = halo_row_pitch(q);
if (q_ld < q_heads * head_dim || q_ld % 4) return HALO_NO(RAD_E_STRIDE);
} else if (!halo_contig(q)) {
return HALO_NO(RAD_E_STRIDE);
}
if (!halo_contig(o)) return HALO_NO(RAD_E_STRIDE);
if (o->shape[0] != total_q || halo_numel(o) != total_q * q_heads * head_dim)
return HALO_NO(RAD_E_SHAPE);
if (su->shape[0] != num_seqs) return HALO_NO(RAD_E_SHAPE);
if (!halo_contig(bt) || !halo_contig(su)) return HALO_NO(RAD_E_STRIDE);
/* The KV cache is pitched (kv_block_stride, kv_head_stride), so a padded cache is legal.
* Each (block, head) slot must be dense, because the kernel walks it with vector loads. */
const long long block_size = kv->shape[2];
if (kv->stride[3] != 1) return HALO_NO(RAD_E_STRIDE);
if (kv->stride[2] != row) return HALO_NO(RAD_E_STRIDE);
if (kv->shape[3] != row) return HALO_NO(RAD_E_SHAPE);
/* 16-byte alignment for 128-bit loads. */
if (!halo_aligned(q->data, 16) || !halo_aligned(o->data, 16) || !halo_aligned(kv->data, 16))
return HALO_NO(RAD_E_ALIGN);
const RadTensor* kds = halo_opt(a, AP_KDS);
const RadTensor* vds = halo_opt(a, AP_VDS);
std::memset(r, 0, sizeof(*r));
r->q = q->data;
r->kv = kv->data;
r->block_table = (const int*)bt->data;
r->seqused_k = (const int*)su->data;
r->out = o->data;
r->k_descale = kds ? (const float*)kds->data : nullptr;
r->v_descale = vds ? (const float*)vds->data : nullptr;
r->q_descale = nullptr; /* the query is bf16 in every variant */
r->q_ld = (long)q_ld; /* 0: dense */
r->scratch = a->scratch;
r->num_seqs = (int)num_seqs;
r->cu_q = ragged ? (const int*)cu->data : nullptr;
r->total_q = (int)total_q;
/* q_len is exact for a uniform batch and an upper bound for a ragged one; it sizes the
* prefill grid and the decode wave count, and the kernels read exact lengths from cu_seqlens.
* The bound is min(band, total_q): the `q_len` parameter is the resolved band selected by
* max_q_len, and total_q is a sum of lengths, so both are >= the true maximum. The min keeps
* decode legal (band bound 10 * gqa 6 = 60 rows fits a 64-row workgroup; total_q alone would
* be refused with -4). A loose bound only costs workgroups that exit on their first compare. */
r->q_len = (int)(total_q / num_seqs);
if (ragged) {
const long long band = halo_geti_or(a, "q_len", total_q);
r->q_len = (int)(band > 0 && band < total_q ? band : total_q);
}
r->q_heads = (int)q_heads;
r->kv_heads = (int)kv->shape[1];
r->head_dim = (int)head_dim;
r->block_size = (int)block_size;
r->max_blocks = (int)bt->shape[1];
r->kv_block_stride = (long)kv->stride[0];
r->kv_head_stride = (long)kv->stride[1];
r->window = (int)halo_geti_or(a, "window", 0);
r->causal = (int)halo_geti_or(a, "causal", 1);
r->causal_seq = nullptr;
/* Softmax scale; defaults to 1/sqrt(head_dim). */
r->scale = (float)halo_getf(a, "scale", 1.0 / std::sqrt((double)head_dim));
/* max_ctx is the host-visible context bound for the split law (seqused_k is device-side).
* Block-table width times block size is a safe upper bound. */
long long mc = halo_geti_or(a, "max_ctx", 0);
if (mc <= 0) mc = (long long)r->max_blocks * block_size;
r->max_ctx = (int)mc;
/* Set by the caller from the tuned `splits`; 0 means libhalo's own split law. */
r->splits = 0;
r->chain = halo_geti_or(a, "chain", 0) != 0 ? 1 : 0;
return RAD_OK;
}
/* The decode rows declare a `splits` axis; its default is 0, which is that same law. */
static inline int split_of(const RadArgs* a) {
HaloSplitCfg c;
halo_cfg_split(a, &c);
return c.splits;
}
static inline int fill_paged(const RadArgs* a, HaloArgs* r, int expect_head_dim,
bool q_pitched = false) {
return fill_paged_row(a, r, expect_head_dim, 2LL * expect_head_dim, q_pitched);
}
#endif /* HALO_ATTN_ARGS_H */