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

302 lines
15 KiB
Plaintext

/* Derived from libr4d/r4d_gen.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// halo_conv2d_nhwc.hip -- the image decoder's convolution, gfx1151: an implicit GEMM over
// (tap, channel) with the weight stored tap-major, plus the direct form for an input whose channel
// count is not a multiple of eight.
//
// The implicit GEMM is libr4d's, with the two gfx11 fragment changes halo_gdn_wmma.h states:
//
// * the fragment pointer loses `4 * (lane >> 4)`: on RDNA 3.5 a lane holds the WHOLE 16-wide k
// run (32 bytes), on RDNA4 only its half's 8 values 16 bytes apart. `fragP` is therefore one
// dwordx4 pair at the un-split k offset.
// * the accumulator's element e is D[2e + (lane>>4)][lane & 15], not D[8*(lane>>4) + e][lane&15].
// The blocked store below is unchanged in SHAPE because `accPack` exchanges the two halves'
// rows first (four v_permlanex16), putting the tile's sixteen consecutive channels back in one
// lane -- so the fail-safe is 16 bytes a lane, as libr4d writes it.
//
// Arithmetic is untouched: f32 accumulation, one WMMA per 16 K in ascending order, bias added
// after the product, RTNE bf16 stores (libref/ref_gen.cpp's conv2d_nhwc).
#include "halo.h"
#include "halo_args.h"
#include "halo_common.h"
#include "halo_gdn_wmma.h"
#include <cstdint>
#include <cstring>
namespace {
constexpr int kCvT = 256;
inline int cv_st_of(hipError_t e) { return e == hipSuccess ? RAD_OK : HALO_NO(RAD_E_DEVICE); }
inline unsigned cv_blocks_for(int64_t n) { return (unsigned)((n + kCvT - 1) / kCvT); }
inline bool cv_is_bf16(const RadTensor* t) { return t && t->dtype == RAD_BF16; }
// A dense [H, W * c] image operand; `pitched` allows a row pitch wider than the row.
inline bool cv_image_of(const RadTensor* t, long long c, long long* H, long long* W,
bool pitched = false) {
if (!t || t->rank != 2 || c <= 0 || t->shape[1] % c || t->stride[1] != 1 ||
(pitched ? t->stride[0] < t->shape[1] : t->stride[0] != t->shape[1]))
return false;
*H = t->shape[0];
*W = t->shape[1] / c;
return true;
}
// ---------------------------------------------------------------------------- conv2d_nhwc
// An implicit GEMM: y[pixel][cout] = sum over (tap, cin) of x_virtual[pixel's tap][cin] *
// w[cout][tap][cin]. The weight is stored tap-major (halo_rows' relayout turns PyTorch's
// [cout][cin][k][k] into it at load), so K runs over taps then channels and a 64-wide K tile of the
// image is eight 16-byte reads of NHWC rows; no im2col buffer.
//
// The block is halo_gemm_bf16_nt_tiled's: eight waves, an activation tile of BP pixels and a weight
// tile of BC output channels staged through LDS 64 K at a time, with the next tile's loads in
// flight under the current WMMA. The weight is the WMMA's A operand and the pixels its B, so a
// lane's accumulator is one pixel and sixteen output channels of it.
//
// An eight-channel input chunk never straddles a tap because cin is a multiple of eight (the shim
// sends anything else to the direct kernel); a chunk past the image, K or the last pixel is zeros.
//
// Kernel size and upsampling are template parameters (KS, UP; 0 reads the argument): at runtime
// they cost two integer divisions per staged chunk, at 3 and 2 a multiply-shift and a shift.
#define CV_BK 64
#define CV_PITCH (CV_BK + 8)
template <int TP, int TC, int KS, int UP>
__global__ __launch_bounds__(256) void conv_igemm_kernel(
const uint16_t* __restrict__ x, const uint16_t* __restrict__ w,
const uint16_t* __restrict__ bias, uint16_t* __restrict__ y, int Hv, int Wv, int cin, int Wo,
int cout, int k, int stride, int up, int pt, int pl, int x_ld, int y_ld, int Mpx, int K) {
constexpr int BP = 2 * TP * 16, BC = 4 * TC * 16;
constexpr int CPR = CV_BK / 8;
constexpr int AL = BP * CPR / 256, WL = BC * CPR / 256;
static_assert(AL >= 1 && WL >= 1, "every thread stages whole chunks");
__shared__ __attribute__((aligned(16))) uint16_t sA[BP * CV_PITCH];
__shared__ __attribute__((aligned(16))) uint16_t sW[BC * CV_PITCH];
const int tid = threadIdx.x, wave = tid >> 5, lane = tid & 31;
const int wp = wave / 4, wc = wave % 4;
const int p0 = blockIdx.x * BP, c0 = blockIdx.y * BC;
const int kq = KS ? KS : k, kk2 = kq * kq;
int a_oy[AL], a_ox[AL], a_t[AL], a_ci[AL];
bool a_ok[AL];
uint16_t* adst[AL];
#pragma unroll
for (int i = 0; i < AL; ++i) {
const int c = tid + 256 * i, r = c / CPR, e = (c % CPR) * 8;
const int m = p0 + r;
a_ok[i] = m < Mpx;
const int mm = a_ok[i] ? m : 0;
a_oy[i] = mm / Wo;
a_ox[i] = mm % Wo;
a_t[i] = e / cin;
a_ci[i] = e % cin;
adst[i] = sA + r * CV_PITCH + e;
}
size_t w_off[WL];
int w_e[WL];
bool w_ok[WL];
uint16_t* wdst[WL];
#pragma unroll
for (int i = 0; i < WL; ++i) {
const int c = tid + 256 * i, r = c / CPR, e = (c % CPR) * 8;
const int co = c0 + r;
w_ok[i] = co < cout;
w_off[i] = (size_t)(w_ok[i] ? co : 0) * K + e;
w_e[i] = e;
wdst[i] = sW + r * CV_PITCH + e;
}
const halo_u32x4 zero = {0u, 0u, 0u, 0u};
auto loadA = [&](int i) -> halo_u32x4 {
if (!a_ok[i] || a_t[i] >= kk2) return zero;
const int kh = a_t[i] / kq, kw = a_t[i] - kh * kq;
const int iy = a_oy[i] * stride + kh - pt, ix = a_ox[i] * stride + kw - pl;
if (iy < 0 || iy >= Hv || ix < 0 || ix >= Wv) return zero;
/* Both are non-negative here, so a halving is a shift. */
const int sy = UP == 1 ? iy : UP == 2 ? (iy >> 1) : iy / up;
const int sx = UP == 1 ? ix : UP == 2 ? (ix >> 1) : ix / up;
return *(const halo_u32x4*)(x + (size_t)sy * x_ld + (size_t)sx * cin + a_ci[i]);
};
auto loadW = [&](int i, int kt) -> halo_u32x4 {
if (!w_ok[i] || kt * CV_BK + w_e[i] >= K) return zero;
return *(const halo_u32x4*)(w + w_off[i] + (size_t)kt * CV_BK);
};
/* This lane's fragment row of each tile, at the un-split k offset: gfx11 has no half-wave k
* term (halo_gdn_wmma.h's rowP). */
const uint16_t* fa = sA + (wp * TP * 16 + (lane & 15)) * CV_PITCH;
const uint16_t* fw = sW + (wc * TC * 16 + (lane & 15)) * CV_PITCH;
v8f acc[TC][TP];
#pragma unroll
for (int j = 0; j < TC; ++j)
#pragma unroll
for (int i = 0; i < TP; ++i) acc[j][i] = (v8f)(0.f);
halo_u32x4 ra[AL], rw[WL];
#pragma unroll
for (int i = 0; i < AL; ++i) ra[i] = loadA(i);
#pragma unroll
for (int i = 0; i < WL; ++i) rw[i] = loadW(i, 0);
const int nk = (K + CV_BK - 1) / CV_BK;
for (int kt = 0; kt < nk; ++kt) {
__syncthreads();
#pragma unroll
for (int i = 0; i < AL; ++i) *(halo_u32x4*)adst[i] = ra[i];
#pragma unroll
for (int i = 0; i < WL; ++i) *(halo_u32x4*)wdst[i] = rw[i];
__syncthreads();
if (kt + 1 < nk) {
#pragma unroll
for (int i = 0; i < AL; ++i) {
a_ci[i] += CV_BK;
while (a_ci[i] >= cin) { a_ci[i] -= cin; ++a_t[i]; }
ra[i] = loadA(i);
}
#pragma unroll
for (int i = 0; i < WL; ++i) rw[i] = loadW(i, kt + 1);
}
#pragma unroll
for (int s = 0; s < CV_BK / 16; ++s) {
v16bf af[TP], wf[TC];
#pragma unroll
for (int i = 0; i < TP; ++i) af[i] = fragP(fa + i * 16 * CV_PITCH + s * 16);
#pragma unroll
for (int j = 0; j < TC; ++j) wf[j] = fragP(fw + j * 16 * CV_PITCH + s * 16);
#pragma unroll
for (int j = 0; j < TC; ++j)
#pragma unroll
for (int i = 0; i < TP; ++i) acc[j][i] = mma(wf[j], af[i], acc[j][i]);
}
}
/* The store. Lane l's element e is channel cb + accRow(l, e) of pixel accCol(l), so the bias is
* added per element and accPack then hands each lane its own eight consecutive channels back. */
#pragma unroll
for (int j = 0; j < TC; ++j)
#pragma unroll
for (int i = 0; i < TP; ++i) {
const int px = p0 + (wp * TP + i) * 16 + accCol(lane);
if (px >= Mpx) continue;
const int cb = c0 + (wc * TC + j) * 16;
const int oy = px / Wo, ox = px % Wo;
uint16_t* dst = y + (size_t)oy * y_ld + (size_t)ox * cout;
if (cb + 16 <= cout && (cout & 7) == 0) {
float f[8];
#pragma unroll
for (int e = 0; e < 8; ++e)
f[e] = acc[j][i][e] + bf16_to_f32(bias[cb + accRow(lane, e)]);
*(uint4*)(dst + cb + accPackRow(lane)) = accPack(f, lane);
} else {
#pragma unroll
for (int e = 0; e < 8; ++e) {
const int co = cb + accRow(lane, e);
if (co < cout) dst[co] = f32_to_bf16(acc[j][i][e] + bf16_to_f32(bias[co]));
}
}
}
}
// The direct form, for an input whose channel count is not a multiple of eight (an RGBA image into
// the first layer): a thread an output element, f32 over (tap, cin), the same tap-major weight.
__global__ __launch_bounds__(kCvT) void conv_direct_kernel(
const uint16_t* __restrict__ x, const uint16_t* __restrict__ w,
const uint16_t* __restrict__ bias, uint16_t* __restrict__ y, int Hv, int Wv, int cin, int Wo,
int cout, int k, int stride, int up, int pt, int pl, int x_ld, int y_ld, int64_t total) {
const int64_t t = (int64_t)blockIdx.x * kCvT + threadIdx.x;
if (t >= total) return;
const int co = (int)(t % cout);
const int64_t px = t / cout;
const int oy = (int)(px / Wo), ox = (int)(px % Wo);
const int K = k * k * cin;
float acc = 0.0f;
for (int kh = 0; kh < k; ++kh) {
const int iy = oy * stride + kh - pt;
if (iy < 0 || iy >= Hv) continue;
for (int kw = 0; kw < k; ++kw) {
const int ix = ox * stride + kw - pl;
if (ix < 0 || ix >= Wv) continue;
const uint16_t* xp = x + (size_t)(iy / up) * x_ld + (size_t)(ix / up) * cin;
const uint16_t* wp = w + (size_t)co * K + (size_t)(kh * k + kw) * cin;
for (int ci = 0; ci < cin; ++ci) acc += bf16_to_f32(wp[ci]) * bf16_to_f32(xp[ci]);
}
}
y[(size_t)oy * y_ld + (size_t)ox * cout + co] = f32_to_bf16(acc + bf16_to_f32(bias[co]));
}
} // namespace
// ============================================================== the plugin ABI entry point
extern "C" int halo_rad_conv2d_nhwc(const RadArgs* a, RadStream s) {
const RadTensor *x = halo_opt(a, 0), *w = halo_opt(a, 1), *b = halo_opt(a, 2),
*y = halo_opt(a, 3);
if (!x || !w || !b || !y) return HALO_NO(RAD_E_INVAL);
if (!cv_is_bf16(x) || !cv_is_bf16(w) || !cv_is_bf16(b) || !cv_is_bf16(y))
return HALO_NO(RAD_E_DTYPE);
const long long cin = halo_geti_or(a, "cin", 0), cout = halo_geti_or(a, "cout", 0);
const long long k = halo_geti_or(a, "k", 0), stride = halo_geti_or(a, "stride", 1);
const long long up = halo_geti_or(a, "up", 1);
const char* pad = halo_gets(a, "pad");
const bool down = pad && !std::strcmp(pad, "down");
const bool causal = pad && !std::strcmp(pad, "causal");
if (pad && !down && !causal && std::strcmp(pad, "same")) return HALO_NO(RAD_E_INVAL);
if (cin <= 0 || cout <= 0 || k <= 0 || stride <= 0 || up <= 0) return HALO_NO(RAD_E_SHAPE);
long long Hin = 0, Win = 0, Ho = 0, Wo = 0;
/* y may be a column slice of wider rows (a channel written into its own columns): both kernels
* store at its row pitch. */
if (!cv_image_of(x, cin, &Hin, &Win) || !cv_image_of(y, cout, &Ho, &Wo, true))
return HALO_NO(RAD_E_SHAPE);
const long long Hv = Hin * up, Wv = Win * up;
/* Rows padded above `pt` and below `pb`, columns `pl` left and `pr` right. */
const long long pt = down ? 0 : causal ? k - 1 : k / 2, pb = down ? 1 : causal ? 0 : k / 2;
const long long pl = down ? 0 : k / 2, pr = down ? 1 : k / 2;
if ((Hv + pt + pb - k) / stride + 1 != Ho || (Wv + pl + pr - k) / stride + 1 != Wo)
return HALO_NO(RAD_E_SHAPE);
const long long K = k * k * cin;
if (halo_numel(w) < cout * K || halo_numel(b) < cout) return HALO_NO(RAD_E_SHAPE);
const long long Mpx = Ho * Wo;
if (Mpx <= 0 || Mpx >= (1ll << 31) || (long long)Ho * Wo * cout >= (1ll << 40))
return HALO_NO(RAD_E_SHAPE);
hipStream_t st = (hipStream_t)s;
if (cin % 8 || ((uintptr_t)x->data & 15) || ((uintptr_t)w->data & 15)) {
const long long total = Mpx * cout;
hipLaunchKernelGGL(conv_direct_kernel, dim3(cv_blocks_for(total)), dim3(kCvT), 0, st,
(const uint16_t*)x->data, (const uint16_t*)w->data,
(const uint16_t*)b->data, (uint16_t*)y->data, (int)Hv, (int)Wv,
(int)cin, (int)Wo, (int)cout, (int)k, (int)stride, (int)up, (int)pt,
(int)pl, (int)x->stride[0], (int)y->stride[0], (int64_t)total);
return cv_st_of(hipGetLastError());
}
// 128 pixels a block, and the widest channel tile (192, 128, then 64) that leaves under a third
// of its last block idle. A wider tile does two or three times the WMMA per K step for the same
// two barriers, which pays even with the last block partly empty. The rule is libr4d's; on
// gfx1151 a lane's fragment is twice as wide, so a wave's WMMA count per K step is unchanged
// (TP x TC accumulators) and the choice was not re-measured here.
auto fits = [cout](long long bc) {
const long long padded = (cout + bc - 1) / bc * bc;
return (padded - cout) * 3 < padded;
};
#define HALO_CONV(TP, TC, KS, UP) \
hipLaunchKernelGGL((conv_igemm_kernel<TP, TC, KS, UP>), \
dim3((unsigned)((Mpx + 2 * TP * 16 - 1) / (2 * TP * 16)), \
(unsigned)((cout + 4 * TC * 16 - 1) / (4 * TC * 16))), \
dim3(256), 0, st, (const uint16_t*)x->data, (const uint16_t*)w->data, \
(const uint16_t*)b->data, (uint16_t*)y->data, (int)Hv, (int)Wv, (int)cin, \
(int)Wo, (int)cout, (int)k, (int)stride, (int)up, (int)pt, (int)pl, \
(int)x->stride[0], (int)y->stride[0], (int)Mpx, (int)K)
/* The decoder's two kernel sizes at its two sampling rates, and the generic form for the rest. */
#define HALO_CONV_K(TP, TC) \
if (k == 3 && up == 1) HALO_CONV(TP, TC, 3, 1); \
else if (k == 3 && up == 2) HALO_CONV(TP, TC, 3, 2); \
else if (k == 1 && up == 1) HALO_CONV(TP, TC, 1, 1); \
else HALO_CONV(TP, TC, 0, 0)
if (fits(192)) { HALO_CONV_K(4, 3); }
else if (fits(128)) { HALO_CONV_K(4, 2); }
else { HALO_CONV_K(4, 1); }
#undef HALO_CONV_K
#undef HALO_CONV
return cv_st_of(hipGetLastError());
}