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

220 lines
10 KiB
Plaintext

/* Derived from libr4d/r4d_gemm_bf16_nt_tiled.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// halo_gemm_bf16_nt_tiled.hip - halo_gemm_bf16_nt_tiled: the bf16 GEMM at prefill row counts, gfx1151.
//
// C[M,N] = A[M,K] @ W[N,K]^T, bf16 in and out, fp32 accumulate: gemm_bf16_nt_m64's arithmetic in a
// block shape that reuses what it loads.
//
// The skinny kernel reads every fragment from global memory and re-reads the whole activation per
// N tile: right at decode, where the weight is the cost, but at a 2048-row prefill chunk the
// activation is read N/16 times and no configuration reaches 50 TF/s.
//
// Here a block of eight waves stages a BM x 64 activation tile and a BN x 64 weight tile through
// LDS, and each wave computes a (BM/2) x (BN/4) patch, so each staged fragment feeds TM or TN WMMA.
// The next K tile is loaded into registers while the current one is computed.
//
// Contract: byte-identical to gemm_bf16_nt_m64. Both accumulate each of SK slices from zero with
// one WMMA per 16 K in ascending order, using the same fragment layout, and sum the slices in
// ascending order from 0.f. SK comes from the same rule (cfg_for), so an op moving between the two
// rows changes no output bit.
//
// Fragment rows are clamped as in the skinny kernel: rows past M or N re-read the last valid row
// and their results are never stored.
#include "halo.h"
#include "halo_gemm_args.h"
#include "halo_common.h"
#include "halo_gdn_wmma.h"
#include <hip/hip_bf16.h>
#include <cstdint>
#define BT_BK 64 // K per staged tile: four WMMA k steps
// LDS row pitch in elements (144 bytes). A fragment read is sixteen lanes on sixteen rows, 8 bytes
// each; at 36 dwords per row they land on distinct bank pairs, and the upper half-wave's +2 dwords
// fall between them. 144 is a multiple of 16, so a staged chunk is one aligned ds_store_b128.
#define BT_PITCH (BT_BK + 8)
#define BT_WM 2 // waves over M
#define BT_WN 4 // waves over N
// TM x TN WMMA tiles a wave; BM = 2*TM*16, BN = 4*TN*16. SPLIT carries the slice sum.
template <int TM, int TN, bool SPLIT>
__global__ __launch_bounds__(256) void halo_gemm_bf16_nt_tiled_kernel(
const unsigned short* __restrict__ A, const unsigned short* __restrict__ W,
__hip_bfloat16* __restrict__ C, int M, int K, int N, int spk) {
constexpr int BM = BT_WM * TM * 16, BN = BT_WN * TN * 16;
constexpr int CPR = BT_BK / 8; // 16-byte chunks a staged row
constexpr int AL = BM * CPR / 256, WL = BN * CPR / 256;
static_assert(AL >= 1 && WL >= 1 && BM * CPR % 256 == 0 && BN * CPR % 256 == 0,
"a tile is whole 16-byte chunks for every thread");
__shared__ __attribute__((aligned(16))) unsigned short sA[BM * BT_PITCH];
__shared__ __attribute__((aligned(16))) unsigned short sW[BN * BT_PITCH];
const int tid = threadIdx.x, wave = tid >> 5, lane = tid & 31;
const int wm = wave / BT_WN, wn = wave % BT_WN;
// Blocks are remapped into groups of 8 activation bands, M fastest within a group. Blocks
// dispatch in flattened-id order; x-fastest order would read the whole weight once per 128 rows,
// which halves the rate once the weight exceeds the 64 MB MALL. A group shares weight tiles among
// blocks in flight and reads the weight once per group; with fewer bands than a group the order
// is plain M-fastest.
constexpr unsigned kGroupM = 8;
const unsigned per = kGroupM * gridDim.x, lin = blockIdx.y * gridDim.x + blockIdx.x;
const unsigned first = lin / per * kGroupM, r = lin % per;
const unsigned gm = min(gridDim.y - first, kGroupM);
const int m0 = (int)(first + r % gm) * BM, n0 = (int)(r / gm) * BN;
// Staging: chunk c = tid + 256 i is row c / CPR, elements (c % CPR) * 8. Eight threads cover a
// row's 128 bytes, so a wave's loads are four whole lines.
unsigned aoff[AL], woff[WL];
unsigned short* adst[AL];
unsigned short* wdst[WL];
#pragma unroll
for (int i = 0; i < AL; ++i) {
const int c = tid + 256 * i, r = c / CPR, e = (c % CPR) * 8;
const int gr = m0 + r < M ? m0 + r : M - 1;
aoff[i] = (unsigned)gr * (unsigned)K + (unsigned)e;
adst[i] = sA + r * BT_PITCH + e;
}
#pragma unroll
for (int i = 0; i < WL; ++i) {
const int c = tid + 256 * i, r = c / CPR, e = (c % CPR) * 8;
const int gr = n0 + r < N ? n0 + r : N - 1;
woff[i] = (unsigned)gr * (unsigned)K + (unsigned)e;
wdst[i] = sW + r * BT_PITCH + e;
}
// Fragment sources: this lane's row of each tile at its half-K offset, as in rowP.
const unsigned short* fa = sA + (wm * TM * 16 + (lane & 15)) * BT_PITCH; // no half-k term on gfx11 (halo_gdn_wmma.h)
const unsigned short* fw = sW + (wn * TN * 16 + (lane & 15)) * BT_PITCH;
v8f acc[TM][TN], tot[SPLIT ? TM : 1][SPLIT ? TN : 1];
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j) acc[i][j] = (v8f)(0.f);
if constexpr (SPLIT) {
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j) tot[i][j] = (v8f)(0.f);
}
halo_u32x4 ra[AL], rw[WL]; // registers; uint4 would sit in scratch
#pragma unroll
for (int i = 0; i < AL; ++i) ra[i] = *(const halo_u32x4*)(A + aoff[i]);
#pragma unroll
for (int i = 0; i < WL; ++i) rw[i] = *(const halo_u32x4*)(W + woff[i]);
const int nk = K / BT_BK;
int left = spk; // k steps to the end of this split slice
for (int kt = 0; kt < nk; ++kt) {
__syncthreads(); // the previous tile's fragments are all read
#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) {
const unsigned k1 = (unsigned)(kt + 1) * BT_BK;
#pragma unroll
for (int i = 0; i < AL; ++i) ra[i] = *(const halo_u32x4*)(A + aoff[i] + k1);
#pragma unroll
for (int i = 0; i < WL; ++i) rw[i] = *(const halo_u32x4*)(W + woff[i] + k1);
}
#pragma unroll
for (int s = 0; s < BT_BK / 16; ++s) {
v16bf af[TM], bf[TN];
#pragma unroll
for (int i = 0; i < TM; ++i) af[i] = fragP(fa + i * 16 * BT_PITCH + s * 16);
#pragma unroll
for (int j = 0; j < TN; ++j) bf[j] = fragP(fw + j * 16 * BT_PITCH + s * 16);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j) acc[i][j] = mma(af[i], bf[j], acc[i][j]);
/* Schedule each k step's fragment reads, then its products. Otherwise the compiler spreads
* LDS reads among the previous step's WMMAs and waits on them early; grouping is 6-9% faster
* on the 128 x 128 tile at M 384..2048, K 2560. */
__builtin_amdgcn_sched_group_barrier(0x100, TM + TN, 0); /* DS reads */
__builtin_amdgcn_sched_group_barrier(0x008, TM * TN, 0); /* WMMA */
if constexpr (SPLIT) {
if (--left == 0) {
left = spk;
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j) {
tot[i][j] += acc[i][j];
acc[i][j] = (v8f)(0.f);
}
}
}
}
}
// The skinny kernel's store: lane l holds column (l & 15) of a tile and rows accRow(l, e).
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j) {
const int n = n0 + (wn * TN + j) * 16 + (lane & 15);
const int mb = m0 + (wm * TM + i) * 16;
#pragma unroll
for (int e = 0; e < 8; ++e) {
float v = 0.f;
if constexpr (SPLIT) v = tot[i][j][e];
else v += acc[i][j][e];
const int m = mb + accRow(lane, e);
if (n < N && m < M) C[(size_t)m * N + n] = (__hip_bfloat16)v;
}
}
}
// Tile choice. A block walks all of K alone, so its time is a floor (~40 us at K = 2560), and a
// grid too small to fill the part leaves CUs idle (M = 200, N = 512 is eight 128 x 128 blocks).
// At K = 2560 the 128 x 128 tile wins once its grid reaches 32 blocks (half the CUs) and loses by
// less than the 64 x 64 tile does below that. 64 x 128 and 128 x 64 gain only a few percent.
enum { kBtLargeGrid = 32 };
int halo_gemm_bf16_nt_tiled_arm(int M, int N) {
const long long g = (long long)((N + 127) / 128) * ((M + 127) / 128);
return g >= kBtLargeGrid ? 1 : 2;
}
// `tile`: 1 = 128 x 128, 2 = 64 x 64 (M x N).
int halo_gemm_bf16_nt_tiled(long a, long w, long c, int M, int K, int N, int SK, int tile,
long stream) {
if (M < 1 || N < 1 || K < BT_BK || K % BT_BK) return -1;
if (SK < 1 || K % (SK * 16)) return -1;
if (tile != 1 && tile != 2) return -1;
// 32-bit element offsets: the last row of either operand has to be addressable.
if ((long long)M * K >= (1ll << 32) || (long long)N * K >= (1ll << 32)) return -1;
const int spk = K / SK / 16;
const int bt = tile == 1 ? 128 : 64;
dim3 grid((N + bt - 1) / bt, (M + bt - 1) / bt), block(256);
auto Ap = (const unsigned short*)a; auto Wp = (const unsigned short*)w;
auto Cp = (__hip_bfloat16*)c;
hipStream_t st = (hipStream_t)stream;
#define LAUNCH(TM, TN, S) hipLaunchKernelGGL((halo_gemm_bf16_nt_tiled_kernel<TM, TN, S>), grid, \
block, 0, st, Ap, Wp, Cp, M, K, N, spk)
const bool split = SK > 1;
if (tile == 1) { if (split) LAUNCH(4, 2, true); else LAUNCH(4, 2, false); }
else { if (split) LAUNCH(2, 1, true); else LAUNCH(2, 1, false); }
#undef LAUNCH
return (int)hipGetLastError();
}
// ============================================================== the plugin ABI entry point
extern "C" int halo_rad_gemm_bf16_tiled(const RadArgs* a, RadStream s) {
GemmOps g;
int rc = gemm_ops(a, &g, 0);
if (rc != RAD_OK) return rc;
// The skinny row's split rule with the same arguments keeps the two rows' outputs identical.
Cfg c;
rc = cfg_for(a, g.K, g.M, 16, BR_FLAT, &c, (g.N + 15) / 16);
if (rc != RAD_OK) return rc;
int tile = (int)halo_geti_or(a, "tile", 0);
if (tile == 0) tile = halo_gemm_bf16_nt_tiled_arm((int)g.M, (int)g.N);
const int e = halo_gemm_bf16_nt_tiled(halo_addr(g.a), halo_addr(g.b), halo_addr(g.y), (int)g.M,
(int)g.K, (int)g.N, c.sk, tile, (long)(uintptr_t)s);
return e == 0 ? RAD_OK : HALO_NO(e < 0 ? RAD_E_SHAPE : RAD_E_DEVICE);
}