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/PORTING.md
T
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

151 lines
9.1 KiB
Markdown

# Porting a unit from libr4d to libhalo (gfx1151)
`libhalo` is radiance's kernel library for Strix Halo (gfx1151, RDNA 3.5, the Radeon 8060S in the
Ryzen AI Max+ 395): HIP kernels behind the plain C ABI `halo.h`, plus the plugin rows the engine
selects from (`halo_rows.cpp`), loaded as `radiance_home/kernels/libhalo.so`.
`libr4d/` in the same tree is the **shipped** library for gfx1201 (RDNA4, R9700) and is the source
every unit is ported *from*. Shared by design and never to be changed by a port: op schemas, row
names, parameter keys, layout tags and the stored weight bytes (`libhalo/README.md` §"What is
shared"). Everything measured is libhalo's own and must be re-measured here: priorities,
constraint bands, tunable defaults, launch geometry, kernel bodies. The shape of the part drives
most of it: ~256 GB/s of LPDDR5X shared with the CPU, 40 CUs, wave32, unified memory.
Entry points come in two flavours and both must exist before a row can select a unit:
`halo_rad_<row>(const RadArgs*, RadStream)` shims declared in `halo_plugin.h` (the row table takes
their addresses) and the plain C entries of `halo.h` the engine dlsyms. Unimplemented ones are
weak stubs in `halo_stubs.cpp` returning `RAD_E_UNSUPPORTED` (-18); see §5.
## 1. Start a unit: the rename transform (one command)
```sh
# from the repository root. u = the libr4d unit's name without the r4d_ prefix.
u=attn_paged_gqa8
sed -e 's/libr4d/libhalo/g' -e 's/r4dchain/halo_r4dchain/g' \
-e 's/R4D_/HALO_/g' -e 's/R4d/Halo/g' -e 's/R4D/Halo/g' \
-e 's/r4d_/halo_/g' -e 's/"r4d\.h"/"halo.h"/g' \
-e 's/__builtin_amdgcn_cvt_pk_fp8_f32/halo_fp8::cvt_pk_fp8_f32/g' \
libr4d/r4d_$u.hip > libhalo/halo_$u.hip
grep -nE '\br4d\b|R4d|R4D' libhalo/halo_$u.hip # leftovers must all be inside message strings
```
Order matters: `libr4d` and `r4dchain` first, then `R4D_` before `R4D`, then `r4d_`. A chained
kernel keeps `r4dchain` inside its name (`halo_r4dchain_rmsnorm`): it is the ABI's marker, not a
library prefix -- the AQL backend gives a chain record only to a kernel whose name contains it
(`core/device/aql.cpp`, RadKernelChainFn in `abi/rad_abi.h`), so a chained form renamed to
`halochain_*` would silently run unchained. Quoted
includes (`#include "r4d_dt16.h"`) are renamed by the `r4d_` rule; the bare `"r4d.h"` needs its
own rule, which is why it is listed. The remaining `r4d` spellings are error-message strings; the
ported units keep them (`halo_dequant_w4_bf16.hip` says `"r4d dequant_w4_bf16: ..."`) — just make
sure no *identifier* was missed. Replace the file's leading comment with the two lines every ported
unit carries:
```
/* Derived from libr4d/r4d_<unit>.hip by the libhalo rename transform (<date>),
* then ported to gfx1151; see libhalo/README.md. */
```
A rename is not a port: the file now compiles for gfx1151 or it does not, and either way it has
not been checked. §2-§4 are the actual work.
## 2. gfx1151 fragment rules: `libhalo/halo_dt16.h` states them
Read the header comment of `libhalo/halo_dt16.h` before touching an attention body; it is the whole
story, summarised here so you know what to look for:
| | a lane's A/B fragment | the k its element `e` holds |
|---|---|---|
| gfx1201 | 8 x 16-bit (4 dwords) | `k = 8*(lane>>4) + e` |
| gfx1151 | 16 x 16-bit (8 dwords) | `k = e` (lanes 16-31 replicate) |
libr4d's attention kernels keep the score matrix transposed (`S^T = K.Q^T`) so the D column — the
query — stays on `lane & 15` on **both** architectures; that is why they port at all. What changes
is the accumulator's ROW: element `e` is key `8*(lane>>4) + e` on gfx12, key `2*e + (lane>>4)` on
gfx1151. Both spread the tile's sixteen keys over the half-pair, so the kernels' existing
`swap16` (`lane ^ 16`) row-max / row-sum reductions keep their meaning: a port changes the *key
index* and the *fragment width*, never the softmax algebra. Per body that means
- `mk()` takes the lane's **eight** dwords (16 values), not four;
- the fragment gather reads **32 bytes** at the un-split k offset (drop libr4d's `+ 8*h` term) and
masks on key `2*e + (lane>>4)`;
- the P fragment is assembled across the half-pair (`p16w`): a lane holds one parity, keys
`2*e + (lane>>4)`;
- the store scatters at head dim `2*e + (lane>>4)`, not at a contiguous run.
`halo_wmma.h` has the measured WMMA layouts and `halo::mma`; `halo_fp8_wmma.h` has `e4m3_pk` (an
exact e4m3 widen) and `cvt_pk_fp8_f32`. gfx1151 has **no** fp8 convert instruction
(`v_cvt_pk_fp8_f32`), **no** `v_cvt_pk_bf16_f32` (use `halo::pk_bf16`, software RTNE) and **no**
`global_load_tr_b128` (the shuffle transpose in `halo_common.h` stands in). fp8 KV forms therefore
take `halo_fp8::e4m3_pk`; bf16/f16 forms take `halo::mma` and `DT16<0>` / `DT16<1>`.
## 3. Wrapper units: `#define` + `#include`
A family body is parameterised by macros and reused by small wrapper files, as in libr4d. The
wrapper is the translation unit (it is what `HALO_UNITS` lists and compiles); the body is text:
```c
#define HALO_ATTN_GQA 1
#define HALO_ATTN_H128 1
#include "halo_attn_paged_gqa8.hip"
```
The body builds its entry names by token pasting (`GQ_SYM(halo_rad_attn_prefill_h256_gqa, _fp8)`),
so no text scan can see which symbols it defines. That is exactly why the stubs are weak (§5): a
macro-built definition overrides its stub at link time with nothing to regenerate.
## 4. Done means numerically checked
A unit is done when a numeric harness says so, never because the build passes: with weak stubs a
row whose entry is missing answers -18 and a row whose entry exists but is wrong computes garbage,
and both leave the build green.
- Row smoke/numeric checks: `halo_selftest <plugin.so> --case <row>` (row names are shared with
libr4d, so the case names are too). Add a case to `libhalo/halo_selftest.cpp` when the row has
none — an unwritten case is a row nobody has measured.
- A row with no case, and every kernel with a new fragment path: write a float64 reference checker
modelled on `.../scratch/pad/attn_dense_check.cpp` — `dlopen` the `.so`, `dlsym` the C entry,
fill small exactly-representable inputs, compute a straightforward `double` reference on the
host, print the worst relative L2. 16-bit and fp8 paths land in the 1e-3 class; byte-identical
contracts (see `STRIX_HALOD_RADIANCE.md` §6.3) must compare bytes, not rel_l2. Build the checker
on the node with
`/opt/rocm/lib/llvm/bin/clang++ -x hip --offload-arch=gfx1151 -std=c++17 -O2 check.cpp -o check`
and run `./check <plugin.so> 3`.
- Then `rad-kbench --kernels libhalo,libref` for the row set, and time it against the shape's
roofline on the card (`--perfmxd`, `--perfw8`, `--perfq`, ... in `halo_selftest`).
## 5. Stubs and `HALO_UNITS` are generated now
`python3 libhalo/gen_stubs.py` rewrites `halo_stubs.cpp` and the `HALO_UNITS` list from the tree;
`--check` fails instead of writing (use it before committing). What it derives: the entries from
every `halo_*` declaration in `halo_plugin.h` and `halo.h` plus any line already in the file whose
symbol neither header declares; what is implemented from definitions found in the sources
`CMakeLists.txt` compiles; the unit list from the `halo_*.hip` files present, minus pure bodies
(§3). So a new unit needs no edit to either file — add `libhalo/halo_<unit>.hip` and run the
generator. Never edit `halo_stubs.cpp` or the `HALO_UNITS` list by hand, and never delete a stub
line to "make room": every line is `__attribute__((weak))`, so your definition wins anyway.
## 6. Lane workflow
One lane = one git worktree on its own branch outside the shared tree, rsynced to one node;
builds and selftests run **on the node**, detached, never in the shared tree. The recipe, the node
list (node2; never node1), the per-node configure flags and the merge rules
live in the project notebook note `halo-port-lane-workflow`; the current state of the port and
what is already proven are in `strix-halo-libhalo-task` and `STRIX_HALOD_RADIANCE.md`.
## 7. Gotchas already paid for in shared code
- **`mx_frag16x` takes `exp` and `ref` and folds `exp - ref`**, not the e4m3 table's
`d = ref - exp`. Passing the table's sign flips every staged weight exponent (~1e4 relative error, see
`halo_gemm_mxfp4a8_tiled.hip`'s note and `halo_gemm_mxfp4a8_nt_m64.hip`'s `mx_frag16x`).
- **`load_tr_b128` in `halo_common.h` was wrong** until recently: a shuffle's argument is evaluated
at the SOURCE lane, so the dword it fetched was the wrong one. It is fixed; re-check any kernel
that uses the transposed load and recompile any probe you copy from an older session.
- **`core/build/` is checked-in SOURCE** — never delete it, never regenerate it, never treat it as
build output.
- Each unit compiles `-ffp-contract=off` (see `CMakeLists.txt`): the quantised all-reduce's byte
contracts depend on it. Do not add fast-math or let the port re-associate a reduction.
- Two int8 planes exist and are not interchangeable: `quant_act_i8`'s shuffled plane (the skinny
w4a8/w8a8 GEMMs) and `quant_act_i8g`'s natural row-major plane (the i8a8 twins, `wpa8i8`).
- fp8 `deq16_e4m3` underflows values near 2⁻¹²⁰; the `HALO_FP8_FIX` compensation applies to the
**weight only** (both operands would underflow) — `STRIX_HALOD_RADIANCE.md` §6.3.