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

173 lines
9.1 KiB
Plaintext

/* Derived from libr4d/r4d_givens_rotate.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// ParoQuant's rotation to f16: the channel scale and Givens stages applied to a bf16 activation,
// for the 16-bit-activation GEMMs (w5a16 / w4a16_asym, rows up to 64, i.e. decode). The weight is
// stored as v = w . D . G_1 ... G_S (abi/rad_encoding.h, "givens"), so with
// x' = x . D^-1 . G_1 ... G_S, <v, x'> = <w, x>: the activation is rotated once a token and the
// GEMM reads the weight unchanged. The quantising form, for prefill-sized rows and MXFP4, is
// halo_givens_dense.hip.
//
// A group at a time: ParoQuant's pairs never leave a group of 128 columns (the checkpoint is
// trained that way, and the converter and loader, halo_rad_relayout_givens_pairs, refuse one that
// does), so a group's 128 values are rotated by S stages of 64 disjoint pairs. A wave owns a group,
// held in LDS (128 floats a wave). No block barrier is needed: a wave's LDS accesses are in order
// and a stage's pairs are disjoint, so no two lanes touch one address. Each lane takes two pairs
// per stage.
//
// Parts: a fused projection (gate|up, q|k|v) is rotated per piece, so a row yields `parts` rotated
// copies side by side: y is [M, parts * K]. A (part, group) is one wave's work (a row spans several
// blocks, grid.y). The tables stack the parts as the "givens" encoding does: `stages` rows each of
// pairs and angle, one row of channel scales.
//
// The channel scale is a true IEEE divide: t.scale is the factor D the stored row was multiplied
// by, and libref also divides in f32, so the two agree exactly on this step. Cos and sin are
// computed here in f32 from the stored angle (sincosf, an ulp or two, as libref's libm).
//
// Returns a negative code instead of throwing, for the reason given in halo_gemm_fp8a8.hip.
#include "halo.h"
#include "halo_emit.h"
#include "halo_fp8_args.h"
#include <hip/hip_fp16.h>
namespace {
constexpr uint32_t kWave = 32;
constexpr uint32_t kThreads = 256;
constexpr uint32_t kWaves = kThreads / kWave;
constexpr uint32_t kGroup = 128;
constexpr uint32_t kPairsPerLane = kGroup / 2 / kWave; // 2
// kPre stages' tables are loaded and their sines and cosines taken before the first is applied, so
// each stage's dependent chain (table load, LDS read, rotate, LDS write) overlaps its neighbours'
// loads. At decode it is latency bound: a row is 40 groups of 128, each eight serial stages.
constexpr uint32_t kPre = 8;
__device__ __forceinline__ void wave_sync() {
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "wavefront");
__builtin_amdgcn_wave_barrier();
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "wavefront");
}
// One wave rotates one (part, group) of one row: grid.x is the row and grid.y spreads the row's
// `parts * nkb` groups over blocks of kWaves waves, so a decode step of a few rows still fills the
// part. A wave with no group returns at once; there is no block barrier. The rotated row is
// written as f16 into `y`, y_ld elements a row.
__global__ __launch_bounds__(kThreads) void givens_rotate_kernel(
const uint16_t* __restrict__ x, const int32_t* __restrict__ pairs,
const float* __restrict__ angle, const float* __restrict__ rscale, uint16_t* __restrict__ y,
uint32_t K, uint32_t nkb, uint32_t parts, uint32_t stages, uint32_t x_ld, uint32_t y_ld) {
__shared__ float sbuf[kWaves][kGroup];
const uint32_t row = blockIdx.x;
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
const uint32_t item = blockIdx.y * kWaves + wave;
if (item >= parts * nkb) return;
const uint32_t part = item / nkb, g = item % nkb;
const uint32_t c0 = g * kGroup;
const size_t xrow = (size_t)row * x_ld;
float* buf = sbuf[wave];
// channel scale: v = x / D, four consecutive columns a lane
{
const float4 d = *(const float4*)(rscale + (size_t)part * K + c0 + lane * 4u);
const uint32_t xc = c0 + lane * 4u;
const float v0 = __uint_as_float((uint32_t)x[xrow + xc + 0] << 16) / d.x;
const float v1 = __uint_as_float((uint32_t)x[xrow + xc + 1] << 16) / d.y;
const float v2 = __uint_as_float((uint32_t)x[xrow + xc + 2] << 16) / d.z;
const float v3 = __uint_as_float((uint32_t)x[xrow + xc + 3] << 16) / d.w;
*(float4*)(buf + lane * 4u) = make_float4(v0, v1, v2, v3);
}
for (uint32_t st0 = 0; st0 < stages; st0 += kPre) {
const uint32_t nst = (stages - st0) < kPre ? (stages - st0) : kPre;
int2 ab[kPre][kPairsPerLane];
float th[kPre][kPairsPerLane], cs[kPre][kPairsPerLane], sn[kPre][kPairsPerLane];
#pragma unroll
for (uint32_t k = 0; k < kPre; ++k)
if (k < nst) {
const size_t trow = (size_t)part * stages + st0 + k;
const int32_t* pr = pairs + trow * K + c0;
const float* an = angle + trow * (K / 2u) + g * (kGroup / 2u);
#pragma unroll
for (uint32_t u = 0; u < kPairsPerLane; ++u) {
const uint32_t j = lane + u * kWave;
ab[k][u] = *(const int2*)(pr + 2u * j);
th[k][u] = an[j];
}
}
#pragma unroll
for (uint32_t k = 0; k < kPre; ++k)
if (k < nst)
#pragma unroll
for (uint32_t u = 0; u < kPairsPerLane; ++u) sincosf(th[k][u], &sn[k][u], &cs[k][u]);
wave_sync(); // the scaled row (or the previous chunk's last stage) is in LDS
#pragma unroll
for (uint32_t k = 0; k < kPre; ++k)
if (k < nst) {
#pragma unroll
for (uint32_t u = 0; u < kPairsPerLane; ++u) {
const int ia = ab[k][u].x - (int)c0, ib = ab[k][u].y - (int)c0;
const float va = buf[ia], vb = buf[ib];
buf[ia] = fmaf(cs[k][u], va, sn[k][u] * vb);
buf[ib] = fmaf(cs[k][u], vb, -sn[k][u] * va);
}
wave_sync();
}
}
const float4 r = *(const float4*)(buf + lane * 4u);
const float v[4] = { r.x, r.y, r.z, r.w };
// f16 holds +-65504; a rotated post-norm row is far below that, and the clamp keeps an outlier
// finite.
union { __half h[4]; uint2 u; } o;
#pragma unroll
for (int j = 0; j < 4; ++j) o.h[j] = __float2half(fminf(fmaxf(v[j], -65504.0f), 65504.0f));
*(uint2*)(y + (size_t)row * y_ld + (size_t)part * K + c0 + lane * 4u) = o.u;
}
} // namespace
// x [M][K] bf16, `x_ld` elements a row (0 = K)
// pairs [parts*stages][K] int32 column indices; (pairs[2j], pairs[2j+1]) is pair j
// angle [parts*stages][K/2] f32
// rscale [parts][K] f32, the factor the stored row was multiplied by
// y [M][parts*K] f16
enum { GV_X = 0, GV_PAIRS = 1, GV_ANGLE = 2, GV_RSCALE = 3, GV_Y = 4 };
extern "C" int halo_rad_givens_rotate(const RadArgs* a, RadStream stream) {
const RadTensor* x = halo_opt(a, GV_X);
const RadTensor* pr = halo_opt(a, GV_PAIRS);
const RadTensor* an = halo_opt(a, GV_ANGLE);
const RadTensor* rs = halo_opt(a, GV_RSCALE);
const RadTensor* y = halo_opt(a, GV_Y);
if (!x || !pr || !an || !rs || !y) return HALO_NO(RAD_E_INVAL);
if (x->rank < 2 || y->rank != 2 || pr->rank != 2 || an->rank != 2 || rs->rank != 2)
return HALO_NO(RAD_E_SHAPE);
if (y->dtype != RAD_F16) return HALO_NO(RAD_E_DTYPE);
if (pr->dtype != RAD_I32 || an->dtype != RAD_F32 || rs->dtype != RAD_F32)
return HALO_NO(RAD_E_DTYPE);
// rows are dim 0 and everything after it is the row
const long long M = x->shape[0], K = halo_numel(x) / (x->shape[0] > 0 ? x->shape[0] : 1);
const long long grp = (long long)kGroup, nkb = K / grp;
const long long parts = halo_geti_or(a, "parts", 1), stages = halo_geti_or(a, "stages", 0);
if (M <= 0 || halo_geti_or(a, "group", grp) != grp || K % grp || parts < 1 || stages < 1)
return HALO_NO(RAD_E_SHAPE);
if (y->shape[0] != M || y->shape[1] != parts * K) return HALO_NO(RAD_E_SHAPE);
// the tables are the weight's, contiguous: parts * stages rows of K pairs and K/2 angles
if (pr->shape[0] != parts * stages || pr->shape[1] != K) return HALO_NO(RAD_E_SHAPE);
if (an->shape[0] != parts * stages || an->shape[1] != K / 2) return HALO_NO(RAD_E_SHAPE);
if (rs->shape[0] != parts || rs->shape[1] != K) return HALO_NO(RAD_E_SHAPE);
// A lane stores four f16 (8 bytes) at a time, so y's base and row pitch keep 8-byte alignment.
if (!halo_aligned(rs->data, 16) || !halo_aligned(pr->data, 8) || !halo_aligned(y->data, 8))
return HALO_NO(RAD_E_ALIGN);
const long long xp = halo_row_pitch(x), yp = halo_row_pitch(y);
if (xp < 0 || yp < 0) return HALO_NO(RAD_E_STRIDE);
if (yp % 4) return HALO_NO(RAD_E_ALIGN);
const uint32_t gy = ((uint32_t)(parts * nkb) + kWaves - 1) / kWaves;
return HALO_EMIT(stream, givens_rotate_kernel, dim3((uint32_t)M, gy), dim3(kThreads), 0,
(const uint16_t*)x->data, (const int32_t*)pr->data, (const float*)an->data,
(const float*)rs->data, (uint16_t*)y->data, (uint32_t)K, (uint32_t)nkb,
(uint32_t)parts, (uint32_t)stages, (uint32_t)(xp ? xp : K),
(uint32_t)(yp ? yp : parts * K));
}