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

203 lines
9.3 KiB
C++

/* Derived from libr4d/r4d_moe_sort.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_moe_sort.h -- the routed slots' stable counting sort in one workgroup, shared by every
// kernel that ends in it: moe_scatter's small form and router_topk_scatter (halo_moe.hip), and
// GLM's router_sigmoid_topk_scatter (halo_glm.hip). One definition, so a fused selection and the
// standalone sort place every slot alike; tests/halo_emu runs it from here.
#pragma once
#include <hip/hip_runtime.h>
#include <cstdint>
#include <cstdlib>
#include "halo_args.h"
#include "halo_common.h"
// Internal linkage, as each unit's own definitions had: the same code generation in every kernel
// that takes the sort.
namespace halo_moesort {
namespace {
constexpr uint32_t kSortWave = 32;
/* Expert-count ceiling, a hard limit of these kernels: the router keeps a lane's logits in
* registers (MAXOWN) and both scatter forms keep n_expert + 1 counters in LDS. 512 covers
* Qwen3.8-Flash-Next (512 experts of 640, top-10) for 4 KiB of scatter LDS and a second router
* instantiation. */
constexpr uint32_t kMaxExpert = 512;
/* Protected experts of a slice, sorted last. A layer may keep a few experts (those whose output
* dwarfs the rest) at higher precision, and one grouped GEMM takes one weight format, so the sort
* places their runs after all others, in the listed order, and two GEMMs serve [0, n - p) and
* [n - p, n) of the offsets. Local indices, strictly ascending; p <= kMaxProtect. With none, an
* expert's place is itself. */
constexpr uint32_t kMaxProtect = 8;
struct Protect {
uint32_t n;
uint16_t e[kMaxProtect];
};
/* Local expert `e`'s place in the sort: the protected ones last, the rest closed up in order. */
__device__ __forceinline__ int32_t sort_place(int32_t e, uint32_t n_local, const Protect& p) {
int32_t below = 0;
for (uint32_t j = 0; j < p.n; ++j) {
if ((int32_t)p.e[j] == e) return (int32_t)(n_local - p.n + j);
below += (int32_t)p.e[j] < e;
}
return e - below;
}
/* `protect`, a comma list of local expert indices: strictly ascending, inside the slice, at most
* kMaxProtect. Absent or empty is none. */
inline int protect_of(const RadArgs* a, long long n_local, Protect* p) {
*p = Protect{};
const char* s = halo_gets(a, "protect");
if (!s) return RAD_OK;
long long prev = -1;
while (*s) {
char* end = nullptr;
const long long v = std::strtoll(s, &end, 10);
if (end == s || v <= prev || v >= n_local || p->n >= kMaxProtect)
return HALO_NO(RAD_E_SHAPE);
p->e[p->n++] = (uint16_t)v;
prev = v;
s = end;
if (*s == ',') ++s;
else if (*s) return HALO_NO(RAD_E_SHAPE);
}
return RAD_OK;
}
/* ================================================================== the small sort
*
* One workgroup does the whole sort, for T <= kScatterSmall, which covers every decode and verify
* step (eight sequences at depth 3 and top_k 10 is T = 320), in one launch instead of three.
* Histogram in LDS, offsets an exclusive prefix over n_expert <= kMaxExpert counts, and each slot
* placed by its rank inside its expert's run.
*
* On one workgroup the critical path (barriers, serial LDS chains) is the cost, not the work:
*
* - The prefix is a DPP scan inside each wave plus one exchange of wave totals through LDS: one
* barrier per tile of 32 counts per wave (at most two tiles at eight waves for 512 experts).
* - Placement groups first and orders after. The histogram atomic hands each slot a ticket in its
* expert's run (arrival order); slots are gathered into runs in ticket order, then each slot's
* place is the number of the run's slots with a lower index. That count is as long as the run
* (a row picks an expert at most once), far shorter than counting all earlier slots.
*
* Stable and byte-identical: a slot's place is its position among the same expert's slots in
* ascending i, as in the reference, whatever the ticket order. Every barrier orders LDS only, so
* none waits on or invalidates the vector memory path. */
constexpr uint32_t kScatterSmall = 1024;
/* The small sort's LDS (about 16 KiB), defined once for the standalone and fused kernels. NW is the
* workgroup's wave count, which sizes the scan's exchange. */
template <uint32_t NW>
struct ScatterLds {
static constexpr uint32_t kTiles = (kMaxExpert + NW * kSortWave - 1) / (NW * kSortWave);
int32_t e[kScatterSmall]; /* the slot list: expert in the slice, or -1 */
int32_t tk[kScatterSmall]; /* the slot's ticket inside its expert's run */
int32_t run[kScatterSmall]; /* slots grouped by expert, tickets' order */
int32_t cnt[kMaxExpert + 1];
int32_t off[kMaxExpert + 1];
int32_t wt[kTiles][NW]; /* each wave's total, a slot per tile */
};
/* Inclusive prefix sum over a wave32 with DPP row shifts: four steps within each 16-lane row, then
* the upper row adds the lower row's total. Lanes shifted in from outside the row read zero
* (bound_ctrl). */
__device__ __forceinline__ int32_t wave_incl_scan_i32(int32_t x, uint32_t lane) {
x += __builtin_amdgcn_update_dpp(0, x, 0x111, 0xF, 0xF, true); /* row_shr:1 */
x += __builtin_amdgcn_update_dpp(0, x, 0x112, 0xF, 0xF, true); /* row_shr:2 */
x += __builtin_amdgcn_update_dpp(0, x, 0x114, 0xF, 0xF, true); /* row_shr:4 */
x += __builtin_amdgcn_update_dpp(0, x, 0x118, 0xF, 0xF, true); /* row_shr:8 */
const int32_t lo = __builtin_amdgcn_readlane(x, 15);
return lane >= 16u ? x + lo : x;
}
/* The sort, from a filled slot list on: histogram, offsets and counts, placement. The caller has
* written `sl.e[0, T)` and zeroed `sl.cnt[0, n_expert)`, followed by a barrier. Shared by the
* standalone sort and the fused kernels. The workgroup is exactly NW waves.
*
* INV also writes the inverse: `inv[i]` is slot i's row in `sorted`, -1 for a dropped slot, the
* table moe_gather would otherwise build from `sorted`. */
template <uint32_t NW, bool INV = false>
__device__ __forceinline__ void scatter_small_place(
int32_t* __restrict__ sorted, int32_t* __restrict__ off, int32_t* __restrict__ cnt,
uint32_t T, uint32_t n_expert, ScatterLds<NW>& sl, int32_t* __restrict__ inv = nullptr) {
const uint32_t tid = threadIdx.x, lane = tid % kSortWave, wave = tid / kSortWave;
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = sl.e[i];
if (e >= 0) sl.tk[i] = atomicAdd(&sl.cnt[e], 1);
}
lds_barrier();
/* Every thread sums all NW wave totals, so `carry` (finally the live slot count) is the same
* in every thread. */
int32_t carry = 0;
for (uint32_t b = 0, t = 0; b < n_expert; b += NW * kSortWave, ++t) {
const uint32_t e = b + tid;
const int32_t v = (e < n_expert) ? sl.cnt[e] : 0;
const int32_t x = wave_incl_scan_i32(v, lane);
if (lane == kSortWave - 1) sl.wt[t][wave] = x;
lds_barrier();
int32_t below = 0, tot = 0;
#pragma unroll
for (uint32_t k = 0; k < NW; ++k) {
const int32_t c = sl.wt[t][k];
below += (k < wave) ? c : 0;
tot += c;
}
if (e < n_expert) {
const int32_t o = carry + below + x - v; /* inclusive -> exclusive */
sl.off[e] = o;
off[e] = o;
cnt[e] = v;
}
carry += tot;
}
if (tid == 0) off[n_expert] = carry;
lds_barrier();
for (uint32_t i = (uint32_t)carry + tid; i < T; i += blockDim.x) sorted[i] = -1;
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = sl.e[i];
if (e >= 0) sl.run[sl.off[e] + sl.tk[i]] = (int32_t)i;
}
lds_barrier();
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = sl.e[i];
if (e < 0) {
if constexpr (INV) inv[i] = -1;
continue;
}
const int32_t o = sl.off[e], n = sl.cnt[e];
int32_t rank = 0;
for (int32_t k = 0; k < n; ++k) rank += sl.run[o + k] < (int32_t)i;
sorted[o + rank] = (int32_t)i;
if constexpr (INV) inv[i] = o + rank;
}
}
/* moe_scatter's small form: the slot list from `ids`, then the sort. A template only so a unit that
* includes this header and does not launch it compiles no kernel. */
constexpr uint32_t kScatWaves = 8; /* 256 threads */
template <bool INV>
__global__ __launch_bounds__(kScatWaves * kSortWave) void scatter_small_kernel(
const int32_t* __restrict__ ids, int32_t* __restrict__ sorted, int32_t* __restrict__ off,
int32_t* __restrict__ cnt, uint32_t T, uint32_t n_expert, int32_t base, Protect prot,
int32_t* __restrict__ inv) {
__shared__ ScatterLds<kScatWaves> sl;
const uint32_t tid = threadIdx.x;
for (uint32_t e = tid; e < n_expert; e += blockDim.x) sl.cnt[e] = 0;
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = ids[i] - base;
sl.e[i] = (e >= 0 && e < (int32_t)n_expert) ? sort_place(e, n_expert, prot) : -1;
}
lds_barrier();
scatter_small_place<kScatWaves, INV>(sorted, off, cnt, T, n_expert, sl, inv);
}
} // namespace
} // namespace halo_moesort