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

624 lines
32 KiB
Plaintext

/* Derived from libr4d/r4d_gdn_conv_w4_h128_bf16.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// halo_gdn_conv_w4_h128_bf16.hip -- the gated-delta-net depthwise causal convolution and the work fused around
// it, one entry point per serving phase.
//
// PREFILL (halo_gdn_conv_prep): everything the layer does between the qkv projection and the
// chunked scan, in one kernel:
//
// conv(x) depthwise causal convolution, width 4, silu, varlen, with the conv-state
// cache read at a sequence's first chunk and rewritten from its last tokens
// split the packed [q | k | v] channel range into three contiguous tensors
// l2norm over each 128-dim q and k head
// gate g = -exp(A_log) . softplus(a + dt_bias), beta = sigmoid(b)
// cumsum g summed along the chunk, which is what the scan actually consumes
//
// Replaces causal_conv1d_fn + fused_post_conv_prep + chunk_local_cumsum. The unfused pair writes
// the whole conv output to HBM and reads it straight back (34 MB each way per layer per rank at
// this model's shape); here it stays in registers.
//
// Layout: a workgroup is HPB waves, one wave per 128-dim head, four dims per lane, so a head's
// norm is a barrier-free wave reduction and each lane does one 8-byte access. Channel groups are
// counted in heads so a group never straddles the q/k/v boundaries (head multiples at every TP).
//
// Tokens are blocked by the 64-token chunk because the cumsum is per chunk: it becomes a wave
// prefix over 32 lanes of two tokens each. Each block reads three tokens of halo (4.7% of the
// traffic), cheaper than a barrier around a shared staging buffer.
//
// The conv output is rounded to bf16 before the norm and the store, as in the unfused pair, so
// this kernel is a bit-exact drop-in for it.
#include <hip/hip_runtime.h>
#include "halo.h"
#include "halo_gdn_args.h"
#include "halo_gdn_wmma.h"
#include "halo_common.h"
#include "halo_gdn_conv_tap.h"
// libr4d's rounding helpers (r4d_gdn_wmma.h) -- f2bf / f2bf2 / bf2f -- come from halo_gdn_wmma.h
// (included above): round-half-AWAY (one add, no bfe), the rounding libr4d uses. This kernel's
// bf16 rounding is observable, since the conv output is rounded before the l2 norm as the unfused
// pair does, so that same rounding is kept. gfx1151 has no v_cvt_pk_bf16_f32 either.
#define CP_HD 128 // dims per head, q k and v alike
#define CP_BT 64 // token block = the scan's chunk, so the cumsum stays wave-local
#define CP_W HALO_GDN_CONV_W // conv width
#define CP_ST (CP_W - 1) // conv state depth
// Tokens the decode update preloads into registers. 16 is ~13% slower than 8 (register pressure
// and predicated-load overhead outweigh the hidden round trips).
//
// The xb[] preload is indexed with a compile-time t: a register array under a runtime index
// compiles to a select tree (about a third of the kernel's instructions at HPB=1). Both readers
// unroll over CU_MAXT with a wave-uniform `t < slen` guard, so every index is static and the guard
// is a scalar branch. The preload is issued before the history load: the history address depends
// on `slot` (a cache_idx load) and its values are consumed immediately, so otherwise the two
// memory round trips serialise, with no other wave on the SIMD to hide them.
#define CU_MAXT 8
#define CP_DPL 4 // dims per lane (CP_HD / 32)
// cp_silu is in halo_gdn_conv_tap.h, shared with the recurrent update's folded convolution.
// softplus, in FLA's two-sided form with the same large-x cutoff
__device__ __forceinline__ float cp_softplus(float x, float thr) {
if (x > thr) return x;
return (x > 0.0f) ? (x + __logf(1.0f + __expf(-x)))
: __logf(1.0f + __expf(x));
}
// a and b are read where they lie: a chunk() of the ba projection, bf16 with row stride 2*H (or
// fp32), avoiding a .float().contiguous() copy and two launches per layer.
template <int ABF16>
__device__ __forceinline__ float cp_ab(const void* p, size_t i) {
return ABF16 ? bf2f(((const unsigned short*)p)[i]) : ((const float*)p)[i];
}
// `has_init` says whether the slot holds a history; its element width (int32 or byte) is given by
// the caller. The engine passes RadBatch::ctx_lens (int32); read as bytes, a ctx_len of 512 would
// look like 0 and the conv window would silently start from zeros.
__device__ __forceinline__ bool halo_has_init(const void* p, int i32, int n) {
if (!p) return false;
return i32 ? ((const int*)p)[n] != 0 : ((const unsigned char*)p)[n] != 0;
}
template <int HPB, int ABF16>
__global__ __launch_bounds__(HPB * 32)
void halo_gdn_conv_prep_kernel(const unsigned short* __restrict__ x, long xpitch,
const unsigned short* __restrict__ wgt,
const unsigned short* __restrict__ bias,
unsigned short* __restrict__ cstate,
long cs_seq, long cs_dim, long cs_tok,
const int* __restrict__ cache_idx, long ci_stride,
const void* __restrict__ has_init, int hi_i32,
const void* __restrict__ av, const void* __restrict__ bv,
long ab_stride,
const float* __restrict__ A_log, const float* __restrict__ dt_bias,
unsigned short* __restrict__ q, unsigned short* __restrict__ k,
unsigned short* __restrict__ v,
float* __restrict__ go, float* __restrict__ beta,
const int* __restrict__ cu, int N, int H, int Hg, float sp_thr)
{
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
// flat chunk id -> (sequence, chunk); the grid is an upper bound (see
// halo_gdn_kkt_solve_k128_c64_bf16.hip)
int n = -1, t0 = 0, bos = 0, rows = 0, slen = 0;
{
int acc = 0, want = blockIdx.x;
for (int i = 0; i < N; ++i) {
const int b = cu[i], e = cu[i + 1];
const int nc = (e - b + CP_BT - 1) / CP_BT;
if (want < acc + nc) { n = i; bos = b; slen = e - b; t0 = (want - acc) * CP_BT;
rows = min(CP_BT, slen - t0); break; }
acc += nc;
}
if (n < 0) return;
}
const bool first = (t0 == 0);
// ---- the gating half, on one workgroup per chunk ----------------------------------------
// g and beta depend only on a and b; computed by the y == 0 workgroups instead of a launch.
if (blockIdx.y == 0) {
for (int hv = warp; hv < H; hv += HPB) {
const float alog = __expf(A_log[hv]), dtb = dt_bias[hv];
const int i0 = 2 * lane, i1 = i0 + 1;
float g0 = 0.0f, g1 = 0.0f;
if (i0 < rows)
g0 = -alog * cp_softplus(cp_ab<ABF16>(av, (size_t)(bos + t0 + i0) * ab_stride + hv)
+ dtb, sp_thr);
if (i1 < rows)
g1 = -alog * cp_softplus(cp_ab<ABF16>(av, (size_t)(bos + t0 + i1) * ab_stride + hv)
+ dtb, sp_thr);
// inclusive wave scan of the per-lane pair sums, made exclusive by subtracting the pair
float p = g0 + g1;
#pragma unroll
for (int d = 1; d < 32; d <<= 1) {
const float o = __shfl_up(p, d, 32);
if (lane >= d) p += o;
}
const float pre = p - (g0 + g1); // sum of all lanes before this one
if (i0 < rows) {
go[(size_t)(bos + t0 + i0) * H + hv] = pre + g0;
beta[(size_t)(bos + t0 + i0) * H + hv] = 1.0f / (1.0f + __expf(
-cp_ab<ABF16>(bv, (size_t)(bos + t0 + i0) * ab_stride + hv)));
}
if (i1 < rows) {
go[(size_t)(bos + t0 + i1) * H + hv] = pre + g0 + g1;
beta[(size_t)(bos + t0 + i1) * H + hv] = 1.0f / (1.0f + __expf(
-cp_ab<ABF16>(bv, (size_t)(bos + t0 + i1) * ab_stride + hv)));
}
}
}
// ---- the convolution half ----------------------------------------------------------------
const int unit = blockIdx.y * HPB + warp; // global 128-dim head unit
const int d0 = unit * CP_HD + lane * CP_DPL; // this lane's first channel
const unsigned short* xp = x + (size_t)(bos + t0) * xpitch + d0;
float wt[CP_W][CP_DPL], bs[CP_DPL];
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
#pragma unroll
for (int i = 0; i < CP_W; ++i) wt[i][c] = bf2f(wgt[(size_t)(d0 + c) * CP_W + i]);
bs[c] = bias ? bf2f(bias[d0 + c]) : 0.0f;
}
// win[i] holds x[t - (W-1) + i]; seeded from the previous tokens, or from the conv state cache
// at the sequence's first chunk
float win[CP_W][CP_DPL];
{
const bool init = first && halo_has_init(has_init, hi_i32, n);
const unsigned short* sp = init ? (cstate + (size_t)cache_idx[(size_t)n * ci_stride] * cs_seq
+ (size_t)d0 * cs_dim) : nullptr;
#pragma unroll
for (int i = 0; i < CP_ST; ++i) { // i = 0,1,2 -> x[t0-3+i]
const int src = t0 - CP_ST + i;
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
float val = 0.0f;
if (src >= 0) val = bf2f(x[(size_t)(bos + src) * xpitch + d0 + c]);
else if (init) val = bf2f(sp[(size_t)c * cs_dim + (size_t)(CP_ST + src) * cs_tok]);
win[i][c] = val;
}
}
}
// ---- write the new conv state, from x, at the sequence's first chunk ---------------------
// The cache is updated in place, so the read above and this write must be in the same workgroup
// for the same channels. That is why the first chunk writes the state even though its tokens
// come from the last chunk.
if (first && cache_idx) {
unsigned short* sp = cstate + (size_t)cache_idx[(size_t)n * ci_stride] * cs_seq
+ (size_t)d0 * cs_dim;
const bool init = halo_has_init(has_init, hi_i32, n);
// Channel-major: the cache is [dim][slot] with slot contiguous, so a lane's CP_DPL*CP_ST
// shorts are one contiguous run. Slot-major order would make every store a strided scalar.
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
#pragma unroll
for (int i = 0; i < CP_ST; ++i) {
const int src = slen - CP_ST + i;
unsigned short val = 0;
if (src >= 0) val = x[(size_t)(bos + src) * xpitch + d0 + c];
else if (init) val = sp[(size_t)c * cs_dim + (size_t)(CP_ST + src) * cs_tok];
sp[(size_t)c * cs_dim + (size_t)i * cs_tok] = val;
}
}
}
const int hq = unit, hk = unit - Hg, hv = unit - 2 * Hg;
const int region = (unit < Hg) ? 0 : ((unit < 2 * Hg) ? 1 : 2);
for (int t = 0; t < rows; ++t) {
float o[CP_DPL];
{
const unsigned short* xr = xp + (size_t)t * xpitch;
const ushort4 raw = *(const ushort4*)xr;
win[CP_W - 1][0] = bf2f(raw.x); win[CP_W - 1][1] = bf2f(raw.y);
win[CP_W - 1][2] = bf2f(raw.z); win[CP_W - 1][3] = bf2f(raw.w);
}
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
float acc = bs[c];
#pragma unroll
for (int i = 0; i < CP_W; ++i) acc += wt[i][c] * win[i][c];
// round as the unfused pair does on the way to HBM
o[c] = bf2f(f2bf(cp_silu(acc)));
}
if (region < 2) {
float ss = 0.0f;
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) ss += o[c] * o[c];
ss += halo_xor_lane<16>(ss); ss += halo_xor_lane<8>(ss); ss += halo_xor_lane<4>(ss);
ss += halo_xor_lane<2>(ss); ss += halo_xor_lane<1>(ss);
const float inv = 1.0f / sqrtf(ss + 1e-6f); // L2NORM_EPS, as in the kernel it replaces
unsigned short* dst = (region ? k : q)
+ ((size_t)(bos + t0 + t) * Hg + (region ? hk : hq)) * CP_HD
+ lane * CP_DPL;
*(ushort4*)dst = make_ushort4(f2bf(o[0] * inv), f2bf(o[1] * inv),
f2bf(o[2] * inv), f2bf(o[3] * inv));
} else {
unsigned short* dst = v + ((size_t)(bos + t0 + t) * H + hv) * CP_HD + lane * CP_DPL;
*(ushort4*)dst = make_ushort4(f2bf(o[0]), f2bf(o[1]), f2bf(o[2]), f2bf(o[3]));
}
#pragma unroll
for (int i = 0; i < CP_W - 1; ++i)
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) win[i][c] = win[i + 1][c];
}
}
extern "C" int halo_gdn_conv_prep_w4_h128_bf16(
const void* x, long xpitch, const void* wgt, const void* bias, void* cstate,
long cs_seq, long cs_dim, long cs_tok, const void* cache_idx, long ci_stride,
const void* has_init, int hi_i32,
const void* a, const void* b, long ab_stride, int ab_is_bf16, const void* A_log,
const void* dt_bias, void* q, void* k, void* v, void* g, void* beta, const void* cu,
int N, int T, int H, int Hg, int K, int V, int width, float softplus_thr, void* stream)
{
if (K != CP_HD || V != CP_HD || width != CP_W) return -1;
const int units = 2 * Hg + H;
int hpb = 8;
while (hpb > 1 && (Hg % hpb || H % hpb)) hpb >>= 1;
const int nchunk_ub = (T + CP_BT - 1) / CP_BT + N;
dim3 grid(nchunk_ub, units / hpb), blk(hpb * 32);
#define CP_LAUNCH(P, AB) \
hipLaunchKernelGGL((halo_gdn_conv_prep_kernel<P, AB>), grid, blk, 0, (hipStream_t)stream, \
(const unsigned short*)x, xpitch, (const unsigned short*)wgt, \
(const unsigned short*)bias, (unsigned short*)cstate, \
cs_seq, cs_dim, cs_tok, (const int*)cache_idx, ci_stride, \
has_init, hi_i32, a, b, ab_stride, \
(const float*)A_log, (const float*)dt_bias, (unsigned short*)q, \
(unsigned short*)k, (unsigned short*)v, (float*)g, (float*)beta, \
(const int*)cu, N, H, Hg, softplus_thr)
#define CP_SWITCH(AB) \
switch (hpb) { \
case 8: CP_LAUNCH(8, AB); break; \
case 4: CP_LAUNCH(4, AB); break; \
case 2: CP_LAUNCH(2, AB); break; \
default: CP_LAUNCH(1, AB); break; \
}
if (ab_is_bf16) { CP_SWITCH(1); } else { CP_SWITCH(0); }
#undef CP_SWITCH
#undef CP_LAUNCH
return (int)hipGetLastError();
}
// ---------------------------------------------------------------------------------------------
// Decode-time convolution. Same filter, but a step's tokens are a speculative window and the
// state cache is a rolling buffer of width-1 + num_spec entries: a step reads its window starting
// at the slot the last accepted token left, and rewrites the buffer shifted by one. Writes q, k
// and v in their own layouts (no torch.cat for the recurrent kernel).
// ---------------------------------------------------------------------------------------------
template <int HPB>
__global__ __launch_bounds__(HPB * 32)
void halo_gdn_conv_update_kernel(const unsigned short* __restrict__ x, long xpitch,
const unsigned short* __restrict__ wgt,
const unsigned short* __restrict__ bias,
unsigned short* __restrict__ cstate,
long cs_seq, long cs_dim, long cs_tok, int state_len_max,
const int* __restrict__ cache_idx, long ci_stride,
const int* __restrict__ naccept,
unsigned short* __restrict__ q, unsigned short* __restrict__ k,
unsigned short* __restrict__ v,
const int* __restrict__ cu, int H, int Hg, int maxq)
{
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
const int n = blockIdx.x;
const int bos = cu[n], slen = cu[n + 1] - bos;
if (slen <= 0) return;
const int slot = cache_idx[(size_t)n * ci_stride];
// A negative slot is padding; 0 is a real slot. (vLLM reserves 0 as NULL_BLOCK_ID, but this
// engine's state slots are 0-based, as in libref/ref_gdn.cpp.)
if (slot < 0) return;
const int off = naccept ? (naccept[n] - 1) : 0;
// cache depth this step uses: as in FLA, the bound shrinks by the tokens this sequence lacks
const int slen_eff = state_len_max - (maxq - slen);
const int unit = blockIdx.y * HPB + warp;
const int d0 = unit * CP_HD + lane * CP_DPL;
// A channel's weights are CP_W contiguous shorts: one vector load per channel.
static_assert(CP_W == 4, "the weight load reads a channel's taps as one ushort4");
float wt[CP_W][CP_DPL], bs[CP_DPL];
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
const ushort4 w4 = *(const ushort4*)(wgt + (size_t)(d0 + c) * CP_W);
const unsigned short* ws = (const unsigned short*)&w4;
#pragma unroll
for (int i = 0; i < CP_W; ++i) wt[i][c] = bf2f(ws[i]);
bs[c] = bias ? bf2f(bias[d0 + c]) : 0.0f;
}
// This kernel's time is address arithmetic. size_t indexing gives each of the ~40 cache stores
// and every input tap a 64-bit multiply. The strides are small, so offsets are 32-bit ints off
// base pointers that carry the 64-bit part, and loops walk their pointers.
const int csd = (int)cs_dim, cst = (int)cs_tok, xpi = (int)xpitch;
unsigned short* const sp = cstate + (size_t)slot * cs_seq + (size_t)d0 * cs_dim;
const unsigned short* const xrow = x + (size_t)bos * xpitch + d0;
// Every input load of the step is issued before any is waited on; the cache rewrite below
// reuses the same values.
ushort4 xb[CU_MAXT];
const bool xpre = (slen <= CU_MAXT);
#pragma unroll
for (int t = 0; t < CU_MAXT; ++t)
if (xpre && t < slen) xb[t] = *(const ushort4*)(xrow + t * xpi);
float win[CP_W][CP_DPL];
unsigned short hist[CP_ST][CP_DPL]; // the raw history, for the shifted rewrite
{
const unsigned short* hp = sp + off * cst;
#pragma unroll
for (int i = 0; i < CP_ST; ++i)
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
const unsigned short raw = hp[c * csd + i * cst];
hist[i][c] = raw;
win[i][c] = bf2f(raw);
}
}
const int hq = unit, hk = unit - Hg, hv = unit - 2 * Hg;
const int region = (unit < Hg) ? 0 : ((unit < 2 * Hg) ? 1 : 2);
// Output pointer walked by a fixed stride per token, avoiding a 64-bit multiply each.
unsigned short* op = (region == 2)
? (v + ((size_t)bos * H + hv) * CP_HD + lane * CP_DPL)
: ((region ? k : q) + ((size_t)bos * Hg + (region ? hk : hq)) * CP_HD + lane * CP_DPL);
const int ostep = (region == 2) ? H * CP_HD : Hg * CP_HD;
#define CU_TOKEN_BODY(RAW) \
{ \
const ushort4 raw_ = (RAW); \
win[CP_W - 1][0] = bf2f(raw_.x); win[CP_W - 1][1] = bf2f(raw_.y); \
win[CP_W - 1][2] = bf2f(raw_.z); win[CP_W - 1][3] = bf2f(raw_.w); \
unsigned short out[CP_DPL]; \
_Pragma("unroll") \
for (int c = 0; c < CP_DPL; ++c) { \
float acc = bs[c]; \
_Pragma("unroll") \
for (int i = 0; i < CP_W; ++i) acc += wt[i][c] * win[i][c]; \
out[c] = f2bf(cp_silu(acc)); \
} \
*(ushort4*)op = make_ushort4(out[0], out[1], out[2], out[3]); \
op += ostep; \
_Pragma("unroll") \
for (int i = 0; i < CP_W - 1; ++i) \
_Pragma("unroll") \
for (int c = 0; c < CP_DPL; ++c) win[i][c] = win[i + 1][c]; \
}
if (xpre) {
// A guard, not a break: a data-dependent exit would leave the loop rolled and t a runtime
// index again.
#pragma unroll
for (int t = 0; t < CU_MAXT; ++t) {
if (t < slen) CU_TOKEN_BODY(xb[t]) // static index: a register read, not a tree
}
} else {
for (int t = 0; t < slen; ++t) CU_TOKEN_BODY(*(const ushort4*)(xrow + t * xpi))
}
// Rewrite the cache shifted by one: the tail of the old history (already in registers, read
// before any write, so the in-place update is safe), then this step's tokens. Channel-major, as
// in prefill: one contiguous run per lane instead of strided 2-byte stores.
// The split between the two sources is a loop bound, not a per-store test. `nh` is CP_W-2 on the
// shipped path but computed, so a caller that shrinks the window differently stays correct.
const int VAL = CP_W - 2;
const int nh = slen_eff - slen < 0 ? 0 : (slen_eff - slen);
#pragma unroll
for (int c = 0; c < CP_DPL; ++c) {
unsigned short* dst = sp + c * csd;
// History half, unrolled to hist's bound (i + 1 < CP_ST).
#pragma unroll
for (int i = 0; i + 1 < CP_ST; ++i)
if (i < nh && i < slen_eff) dst[i * cst] = hist[i + 1][c];
// Token half, walked by token so the xb index is the loop counter.
if (xpre) {
#pragma unroll
for (int t = 0; t < CU_MAXT; ++t) {
const int i = t + VAL;
if (i >= nh && i < slen_eff && t < slen) {
// Not `((const unsigned short*)&xb[t])[c]`: taking an element's address forces the
// whole array to scratch. With t and c compile-time, this ternary is a register read.
const ushort4 vv = xb[t];
dst[i * cst] = (c == 0) ? vv.x : (c == 1) ? vv.y : (c == 2) ? vv.z : vv.w;
}
}
}
// Remainder: slots whose token is outside [0, slen), or the whole range when slen exceeds the
// preload. Empty on the shipped path.
for (int i = nh; i < slen_eff; ++i) {
const int ti = i - VAL;
if (!xpre || ti < 0 || ti >= slen) dst[i * cst] = xrow[ti * xpi + c];
}
}
#undef CU_TOKEN_BODY
}
extern "C" int halo_gdn_conv_update_w4_h128_bf16(
const void* x, long xpitch, const void* wgt, const void* bias, void* cstate,
long cs_seq, long cs_dim, long cs_tok, int state_len_max, const void* cache_idx,
long ci_stride, const void* num_accepted, void* q, void* k, void* v, const void* cu,
int N, int H, int Hg, int K, int V, int width, int max_query_len, void* stream)
{
if (K != CP_HD || V != CP_HD || width != CP_W) return -1;
const int units = 2 * Hg + H;
int hpb = 8;
while (hpb > 1 && (Hg % hpb || H % hpb)) hpb >>= 1;
// A decode step has few sequences, so narrow the channel group until the grid covers the
// device (8 sequences at 8 heads per block would be 40 workgroups for 64 CUs).
while (hpb > 1 && (long)N * (units / hpb) < 256) hpb >>= 1;
dim3 grid(N, units / hpb), blk(hpb * 32);
#define CU_LAUNCH(P) \
hipLaunchKernelGGL((halo_gdn_conv_update_kernel<P>), grid, blk, 0, (hipStream_t)stream, \
(const unsigned short*)x, xpitch, (const unsigned short*)wgt, \
(const unsigned short*)bias, (unsigned short*)cstate, cs_seq, cs_dim, \
cs_tok, state_len_max, (const int*)cache_idx, ci_stride, \
(const int*)num_accepted, (unsigned short*)q, (unsigned short*)k, \
(unsigned short*)v, (const int*)cu, H, Hg, max_query_len)
switch (hpb) {
case 8: CU_LAUNCH(8); break;
case 4: CU_LAUNCH(4); break;
case 2: CU_LAUNCH(2); break;
default: CU_LAUNCH(1); break;
}
#undef CU_LAUNCH
return (int)hipGetLastError();
}
// ============================================================== the plugin ABI entry points
// Shared operand helpers: halo_gdn_args.h. Operand enums use an OPD_ prefix because CP_W and CP_ST
// are already kernel constants in this unit.
/* ------------------------------------------------------------------ conv prep */
/* Inputs first, outputs last -- docs/OPS.md's resolved table. */
enum { OPD_CP_X = 0, OPD_CP_W, OPD_CP_B, OPD_CP_ALOG, OPD_CP_DTB, OPD_CP_CSTATE, OPD_CP_CU,
OPD_CP_A, OPD_CP_BGATE, OPD_CP_CACHE_IDX, OPD_CP_HAS_INIT,
OPD_CP_Q, OPD_CP_K, OPD_CP_V, OPD_CP_G, OPD_CP_BETA };
extern "C" int halo_rad_gdn_conv_prep(const RadArgs* a, RadStream s) {
if (gdn_dims_ok() != RAD_OK) return HALO_NO(RAD_E_SHAPE);
const RadTensor* x = halo_opt(a, OPD_CP_X);
const RadTensor* w = halo_opt(a, OPD_CP_W);
const RadTensor* b = halo_opt(a, OPD_CP_B);
const RadTensor* alog = halo_opt(a, OPD_CP_ALOG);
const RadTensor* dtb = halo_opt(a, OPD_CP_DTB);
const RadTensor* cst = halo_opt(a, OPD_CP_CSTATE);
const RadTensor* cu = halo_opt(a, OPD_CP_CU);
const RadTensor* q = halo_opt(a, OPD_CP_Q);
const RadTensor* k = halo_opt(a, OPD_CP_K);
const RadTensor* v = halo_opt(a, OPD_CP_V);
const RadTensor* g = halo_opt(a, OPD_CP_G);
const RadTensor* beta = halo_opt(a, OPD_CP_BETA);
const RadTensor* ta = halo_opt(a, OPD_CP_A);
const RadTensor* tb = halo_opt(a, OPD_CP_BGATE);
const RadTensor* cidx = halo_opt(a, OPD_CP_CACHE_IDX);
const RadTensor* hin = halo_opt(a, OPD_CP_HAS_INIT);
if (!x || !w || !alog || !dtb || !cst || !cu || !q || !k || !v || !g || !beta)
return HALO_NO(RAD_E_INVAL);
/* Optional in the schema (so docs/OPS.md's shorter list stays a positional prefix) but
* required here: the kernel dereferences all three. */
if (!ta || !tb || !cidx) return HALO_NO(RAD_E_INVAL);
if (x->rank < 2 || q->rank != 3 || k->rank != 3 || v->rank != 3) return HALO_NO(RAD_E_SHAPE);
if (g->rank != 2 || beta->rank != 2 || cst->rank != 3 || ta->rank != 2) return HALO_NO(RAD_E_SHAPE);
int N = 0;
int rc = seqs_of(cu, &N);
if (rc != RAD_OK) return rc;
/* x may be a padded view (the layer's qkvz split), so its row pitch is an argument; inner
* axes must be dense. */
const long long xpitch = halo_row_pitch(x);
if (xpitch < 0) return HALO_NO(RAD_E_STRIDE);
const long long ab_stride = halo_row_pitch(ta);
if (ab_stride < 0) return HALO_NO(RAD_E_STRIDE);
if (halo_row_pitch(tb) != ab_stride) return HALO_NO(RAD_E_STRIDE);
if (!halo_contig(q) || !halo_contig(k) || !halo_contig(v) || !halo_contig(g) ||
!halo_contig(beta) || !halo_contig(w) || !halo_contig(alog) || !halo_contig(dtb))
return HALO_NO(RAD_E_STRIDE);
if (cidx->rank < 1) return HALO_NO(RAD_E_SHAPE);
const long long T = q->shape[0];
const long long Hg = q->shape[1];
const long long K = q->shape[2];
const long long H = v->shape[1];
const long long V = v->shape[2];
if (k->shape[0] != T || k->shape[1] != Hg || k->shape[2] != K) return HALO_NO(RAD_E_SHAPE);
if (v->shape[0] != T) return HALO_NO(RAD_E_SHAPE);
if (g->shape[0] != T || g->shape[1] != H) return HALO_NO(RAD_E_SHAPE);
if (beta->shape[0] != T || beta->shape[1] != H) return HALO_NO(RAD_E_SHAPE);
if (H == 0 || Hg == 0 || H % Hg) return HALO_NO(RAD_E_SHAPE);
const int width = (int)halo_geti_or(a, "conv_width", 4);
const float thr = (float)halo_getf(a, "softplus_thr", 20.0);
/* The l2 norm epsilon is compiled in (1e-6f); refuse any other value. */
if ((float)halo_getf(a, "l2_eps", 1e-6) != 1e-6f) return HALO_NO(RAD_E_UNSUPPORTED);
/* a and b may be bf16 or f32 (a flag, not two entry points). */
const int ab_is_bf16 = (ta->dtype == RAD_BF16) ? 1 : 0;
if (tb->dtype != ta->dtype) return HALO_NO(RAD_E_DTYPE);
if (!ab_is_bf16 && ta->dtype != RAD_F32) return HALO_NO(RAD_E_DTYPE);
/* bf16 x, weights, conv state and q/k/v; fp32 A_log, dt_bias, g and beta. */
HALO_DT(x, RAD_BF16); HALO_DT(w, RAD_BF16); HALO_DT(cst, RAD_BF16);
HALO_DT(q, RAD_BF16); HALO_DT(k, RAD_BF16); HALO_DT(v, RAD_BF16);
HALO_DT(alog, RAD_F32); HALO_DT(dtb, RAD_F32); HALO_DT(g, RAD_F32); HALO_DT(beta, RAD_F32);
return HALO_RAW(halo_gdn_conv_prep_w4_h128_bf16(
x->data, (long)xpitch, w->data, b ? b->data : nullptr, cst->data,
(long)cst->stride[0], (long)cst->stride[1], (long)cst->stride[2],
cidx->data, (long)cidx->stride[0], hin ? hin->data : nullptr,
/* has_init element width (see halo_has_init) */
hin && (hin->dtype == RAD_I32 || hin->dtype == RAD_U32),
ta->data, tb->data, (long)ab_stride, ab_is_bf16,
alog->data, dtb->data, q->data, k->data, v->data, g->data, beta->data,
cu->data, N, (int)T, (int)H, (int)Hg, (int)K, (int)V, width, thr, s));
}
/* ------------------------------------------------------------------ conv update */
enum { CUP_X = 0, CUP_W, CUP_B, CUP_CSTATE, CUP_SIDX, CUP_NACC, CUP_CU, CUP_Q, CUP_K, CUP_V };
extern "C" int halo_rad_gdn_conv_update(const RadArgs* a, RadStream s) {
if (gdn_dims_ok() != RAD_OK) return HALO_NO(RAD_E_SHAPE);
const RadTensor* x = halo_opt(a, CUP_X);
const RadTensor* w = halo_opt(a, CUP_W);
const RadTensor* b = halo_opt(a, CUP_B);
const RadTensor* cst = halo_opt(a, CUP_CSTATE);
const RadTensor* sidx = halo_opt(a, CUP_SIDX);
const RadTensor* nacc = halo_opt(a, CUP_NACC);
const RadTensor* cu = halo_opt(a, CUP_CU);
const RadTensor* q = halo_opt(a, CUP_Q);
const RadTensor* k = halo_opt(a, CUP_K);
const RadTensor* v = halo_opt(a, CUP_V);
if (!x || !w || !cst || !sidx || !cu || !q || !k || !v) return HALO_NO(RAD_E_INVAL);
if (cst->rank != 3 || q->rank != 3 || k->rank != 3 || v->rank != 3) return HALO_NO(RAD_E_SHAPE);
if (sidx->rank < 1) return HALO_NO(RAD_E_SHAPE);
int N = 0;
int rc = seqs_of(cu, &N);
if (rc != RAD_OK) return rc;
const long long xpitch = halo_row_pitch(x);
if (xpitch < 0) return HALO_NO(RAD_E_STRIDE);
if (!halo_contig(q) || !halo_contig(k) || !halo_contig(v) || !halo_contig(w))
return HALO_NO(RAD_E_STRIDE);
const long long T = q->shape[0];
const long long Hg = q->shape[1];
const long long K = q->shape[2];
const long long H = v->shape[1];
const long long V = v->shape[2];
if (H == 0 || Hg == 0 || H % Hg) return HALO_NO(RAD_E_SHAPE);
if (v->shape[0] != T || k->shape[0] != T) return HALO_NO(RAD_E_SHAPE);
/* bf16 x, weights, conv state and q/k/v, as in the prefill conv. */
HALO_DT(x, RAD_BF16); HALO_DT(w, RAD_BF16); HALO_DT(cst, RAD_BF16);
HALO_DT(q, RAD_BF16); HALO_DT(k, RAD_BF16); HALO_DT(v, RAD_BF16);
const int width = (int)halo_geti_or(a, "conv_width", 4);
/* state_len_max and max_query_len describe one window, so only one is free. The state cache
* is a rolling window read at the slot the last accepted token left, so a speculative
* rejection is a change of read offset, not a recompute (spec 10). The kernel splits it into
* `width - 2` history entries and the step's tokens at state_len_max - max_query_len, so that
* difference must be width - 2; max_query_len follows from the cache depth.
*
* Do not derive max_query_len from the step: the pool is sized for the deployment's deepest
* draft, and a non-drafting step would misplace the split, so the convolution would stop
* seeing the latest tokens and the model would loop on one phrase. */
const long long state_len_max = cst->shape[2];
const long long mql = state_len_max - (width - 2);
if (width < 2 || mql < 1) return HALO_NO(RAD_E_SHAPE);
return HALO_RAW(halo_gdn_conv_update_w4_h128_bf16(
x->data, (long)xpitch, w->data, b ? b->data : nullptr, cst->data,
(long)cst->stride[0], (long)cst->stride[1], (long)cst->stride[2],
(int)state_len_max, sidx->data, (long)sidx->stride[0],
nacc ? nacc->data : nullptr, q->data, k->data, v->data, cu->data,
N, (int)H, (int)Hg, (int)K, (int)V, width, (int)mql, s));
}