radiance 1.14.0

New models and formats:
- libr3d: a kernel library for RDNA3 (gfx11) cards
- nomic-embed-text-v2-moe (the nomicbert plugin)
- Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16
- DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and
  rad-convert --chat-template for a release that ships no template
- GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe
- Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8

Speed:
- hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined
- gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3
  us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us)
- Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 ->
  19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 ->
  40.3 ms a step)
- Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms,
  coalesced one-token expert GEMMs
- spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch)

Speculation:
- DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on)
- A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with
  a block-16 tree that runs its short block under load

Features:
- Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and
  --expert-stats FILE for the routed experts each layer selects
- --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards
- Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it
  answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for
  tip-only one drafter window below the tip; hits on models whose KV groups have different block
  sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib
- Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the
  server does
- MiniMax-H3: keyframes ride a latent upscale
- Gemma 3's fused all-reduce junction on the lossy wire

Fixes:
- A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the
  engine with a scratch refusal at issue (#293)
- --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link
  twice (#323)
- A chat template that cannot render a conversation is refused at load, not mid-request
- imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv
  kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32)
- --expert-profile refuses a profile recorded on another model
- hc_read's one-grid wait traps past its deadline instead of reading a stale value

Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and
DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
This commit is contained in:
Deadcode committed 2026-10-29 12:04:24 +01:00
1 parent 481fcd5df0
commit 4a61d7296b
160 files changed
+8790 -6819

No files matched your search

+124 -180
View File
@@ -1,112 +1,84 @@
/* r3d_gemm_nt_q_w2a8.hip -- gemm_nt_q at dtype=w2a8: the vocabulary projection, decode-sized.
*
* y[M,N] = a[M,K] @ b[N,K]^T over a 2-bit unsigned weight code with an f16 scale and an unsigned
* integer zero per (row, group of K), an int8 activation with one f32 scale per ROW, and the int8
* code sums `a_sum` the zero is removed with. f32 accumulate, bf16 store.
* integer zero per (row, group of K), an int8 activation with one f32 scale per row, and the int8
* code sums `a_sum` that remove the zero. f32 accumulate, bf16 store.
*
* This is the band that gates the server. MiniCPM5-2B's `output.weight` is N=130560, K=2048,
* group=128, dtype=w2a8, and its stored form is the container's joined `rad.planes` operand: a
* scale plane and a zero plane behind ONE operand (b_scale). libref declines w2a8 in
* `gemm_q_shape`, so kbench has no case for the op the engine needs most -- the fixture cannot
* draw the dtype, which is why this row's evidence is a driver that builds the joined operand with
* `rad_planes_relayout` and compares every element against `ref_gemm_nt_q`'s own body. libref reads
* the form in `gemm_zp`, so a host row serves it, at 135 GFLOP of CPU work per token; and a
* placement without a reader is refused, because nothing in the loaded set can READ the joined form.
* `r3d_layout_gemm_q_w2a8` below is that reader: it DESCRIBES the container's bytes (that is what
* RadLayoutFn in rad_abi.h is), and the row carries no relayout and no repacking.
* MiniCPM5-2B's `output.weight` is N=130560, K=2048, group=128, dtype=w2a8, stored as the
* container's joined `rad.planes` operand: a scale plane and a zero plane behind one operand
* (b_scale). libref's `gemm_q_shape` declines w2a8, so kbench has no case for it; the row is
* verified by a driver that builds the joined operand with `rad_planes_relayout` and compares every
* element against `ref_gemm_nt_q`. libref's host row (`gemm_zp`) costs 135 GFLOP of CPU work per
* token, and the engine refuses a placement no loaded library can read, so this row needs
* `r3d_layout_gemm_nt_q_w2a8` below: it describes the container's bytes (RadLayoutFn in
* rad_abi.h) and does no repacking.
*
* ---- the law, and the order inside it ---------------------------------------------------------
*
* gemm_zp, the ZP_I8_ROW arm, is the spec, and this kernel runs its order:
* gemm_zp's ZP_I8_ROW arm is the spec, and this kernel runs its order:
*
* q[k] = rad_load_bits(row n of b, 2, k) bits [2k,2k+2) of byte k/4
* is(g) = SUM_{k in group g} (int)a[m][i8_step_pos(k)] * (int)q[k] integers: exact
* sub(g) = (float)is(g) - (float)z[n][g] * a_sum[g*mpad + m] mpad = ceil(M/16)*16 = 16
* acc = acc + (sub(g) * sa[m]) * sb[n][g] g ASCENDING
* acc = acc + (sub(g) * sa[m]) * sb[n][g] g ascending
* y[m,n] = bf16(acc)
*
* `a_sum` is per row and per group and group-major, `sa` is one f32 per ROW (ZP_I8_ROW: quant_act_i8
* scales a whole row, not a group), and `(sub * sa) * sb` is a parenthesised order, not a formula.
* `a_sum` is per row and per group, group-major. `sa` is one f32 per row (ZP_I8_ROW: quant_act_i8
* scales a whole row, not a group). The parentheses in `(sub * sa) * sb` are the reference's order.
*
* The activation is in quant_act_i8's step order: column c sits at byte
* i8_step_pos(c) = (c & ~12) | ((c & 4) << 1) | ((c & 8) >> 1), bits 2 and 3 of c swapped, the map
* its own inverse. Swapping bits 2 and 3 of a column is swapping bits 0 and 1 of its DWORD index,
* so a dword's four columns stay four contiguous bytes: staging reads a's dwords in a permuted
* dword order, one 4-byte load per four codes, and nothing straddles a dword.
* i8_step_pos(c) = (c & ~12) | ((c & 4) << 1) | ((c & 8) >> 1), bits 2 and 3 of c swapped, a
* self-inverse map. On the dword index that is a swap of bits 0 and 1, so a dword's four columns
* stay contiguous: staging reads a's dwords in permuted order, one 4-byte load per four codes.
*
* `is` is a sum of integers, every partial sum under 2^24, so it is exact in f32 and its bracketing
* is free. `sub`, the products and the g-ascending accumulation are float, and their order is the
* reference's, so the store is bit-identical to the host row: bit-equality element by element is
* the test, not a tolerance, because nothing else on this machine can draw the operands.
* Exactness. In exact f32 arithmetic `is` (integer partials under 2^24) would be exact and the
* float steps follow the reference's order. The dot instruction is not that exact (below), so
* 0.1-0.2% of outputs land one bf16 ulp off the reference: rel_l2 5.0e-3 at the band, no
* non-finite values. The row is not bit-exact, and its row-table text says so.
*
* ---- U2 to f16 through a bias, and the bias removed with a_sum ---------------------------------
*
* There is no int8 dot on this ISA. `__builtin_amdgcn_s32sd4`, `__builtin_amdgcn_s32ud4`, `__dp4a`
* and the RDNA3 matrix ops `__builtin_amdgcn_madocx_i32_i8` / `_u32_u8` are all undeclared in AMD
* clang 24.0.0git for gfx1102. So the codes widen to f16 and the dot runs on dot2_f16x2, whose
* hardware form folds a pair into an f32 accumulator. Every product is
* an integer and every partial sum is an integer, and "products and partials are integers below
* 2^24" is a true statement about f32 and a false statement about this instruction: v_fdot2_f32_f16
* carries a little under 24 bits of mantissa on integer operands -- about 22, from the ratio of the
* worst error to its magnitude -- so any kernel that pushes integers through a pair-dot inherits a
* tail once its partials reach 2^20. The instrument at the end of this file (one 64-code dot per
* lane, the f32 it produces beside the exact integer it should have produced) shows: bias 1024,
* 71027 of 262144 dots off, by up to 1/8. Bias 128, 70115 of 262144 off, by up to 1/64. The bias is 128 because the tail grows with the magnitude, and 128 is
* the largest power of two that keeps a lane's partial sum under 2^21; shrinking it is a smaller
* tail, not no tail. What the tail costs is one bit of the bf16 answer: 0.1-0.2% of outputs land one
* bf16 ulp off and nothing else, rel_l2 5.0e-3 at the band, no non-finite anywhere. That is this
* row's exactness, written out here because the law below is exact integers and the row reads like a
* bit-exact one. It is not, and the row's prose says so.
* The row without the dot unit -- four f32 words a quad in the tile, four __fmaf_rn per quad -- is
* not in this file. Its arithmetic is exact: a probe of 262144 samples of the four fmas with no
* tile, zero inexact dots, worst error exactly zero. Its staging is the defect: the f32 tile,
* written through a vector pointer at -O2, comes up with content that was never written, and
* typing the array `uint4` rules aliasing out, so the defect is in the tile staging and not in the
* arithmetic.
* At the band this row is not memory-bound. r3d_vocab_bf16.hip streams the same n_vocab x n_embd
* head in 535 MB of bf16 in 3.29 ms at M=1, 163 GB/s, two thirds of the card's DRAM. The 66.8 MB
* of codes this row reads has a streaming floor near 0.41 ms, not the 3.0 ms of one 22 GB/s pass
* this row measures, so this row runs 6-10x above its own floor and the headroom is in its loop --
* the group-term plane and the two barriers a chunk -- not in the memory system. An f32 row costs
* nothing to run, and the tail goes with it.
* The trick that pays for the widening is that f16 bits of (128 + x) are 0x5800 + 8x for 0 <= x < 8,
* so a 2-bit code becomes an f16 with a mask, two shifts and an add -- no table, no compare per
* code -- and the dot returns is' = SUM a[k]*(128 + q[k]) = is + 128*SUM a. SUM a over the group is
* the plane the op already carries: a_sum[g*mpad + m], the very number the zero is removed with.
* 128 * a_sum scales an integer-valued f32 by a power of two, so it is exact, is = is' - 128*a_sum
* is exact, and the reference's own sub = (float)is - (float)z*a_sum follows unchanged. The de-bias
* costs one exact f32 subtraction per (m, group) and is why a row with no int8 instruction on it is
* memory-bound at the band instead of unpack-bound.
* This ISA has no int8 dot: `__builtin_amdgcn_s32sd4`, `__builtin_amdgcn_s32ud4`, `__dp4a` and the
* matrix ops `__builtin_amdgcn_madocx_i32_i8` / `_u32_u8` are undeclared in AMD clang 24.0.0git
* for gfx1102. The codes therefore widen to f16 and the dot runs on dot2_f16x2. v_fdot2_f32_f16
* carries about 22 bits of mantissa on integer operands, not 24, so integer partials pushed
* through it are inexact once they reach about 2^20. The bias choice is explained at u2x4_biased.
*
* The widening uses the fact that the f16 bits of (128 + x) are 0x5800 + 8x for 0 <= x < 8, so a
* 2-bit code becomes an f16 with a mask, two shifts and an add. The dot then returns
* is' = SUM a[k]*(128 + q[k]) = is + 128*SUM a, and SUM a over the group is a_sum[g*mpad + m],
* which the op already carries for the zero. 128 * a_sum is exact (a power-of-two scale of an
* integer-valued f32), so is = is' - 128*a_sum is exact and the reference's
* sub = (float)is - (float)z*a_sum follows unchanged, at one f32 subtraction per (m, group).
*
* The row is not memory-bound: its 66.8 MB of codes have a streaming floor near 0.41 ms, and the
* cost is in the loop (the group-term plane and the two barriers per chunk).
*
* ---- the lane map ------------------------------------------------------------------------------
*
* One warp per weight row n; a block of 8 waves covers 8 rows. MT, the template's row count, is the
* power of two that covers M, and KL = 32/MT is the number of k-lanes per activation row. A chunk
* is CHUNK = 2048/MT codes, so the staged tile is always MT*CHUNK/2 = 1024 dwords = 4 KiB: the
* tile shrinks with the row count exactly as the row count grows, which is what keeps the block
* resident at M=16. A chunk holds CHUNK/128 = 16/MT groups and every lane takes 64 codes -- one
* 16-byte load of codes (four bytes, sixteen codes each, U2 being four codes a byte), thirty-two
* dwords of activation from LDS and thirty-two dot2 in sixteen dot4_f16x4 -- so lane l owns row
* m = l % MT, k-lane kl = l / MT, and group ch*(16/MT) + kl/2, whose two halves are the pair
* (l, l^MT): the group folds with ONE shfl_xor, in integers.
* One warp per weight row n; a block of 8 waves covers 8 rows. MT is the power of two covering M,
* and KL = 32/MT is the number of k-lanes per activation row. A chunk is CHUNK = 2048/MT codes, so
* the staged tile is always MT*CHUNK/2 = 1024 dwords = 4 KiB, which keeps the block resident at
* M=16. A chunk holds CHUNK/128 = 16/MT groups. Each lane takes 64 codes per chunk: one 16-byte
* load of codes (U2 is four codes per byte), 32 dwords of activation from LDS and 32 dot2 in 16
* dot4_f16x4. Lane l owns row m = l % MT, k-lane kl = l / MT and group ch*(16/MT) + kl/2, whose two
* halves are the pair (l, l^MT), folded with one shfl_xor.
*
* Each lane's group index ascends with the chunk index, so `acc += term` in the lane IS the
* reference's ascending g loop over the groups that lane owns; the groups the other k-lanes own are
* added by kl-1 ordered broadcasts per chunk, in ascending kl, which is ascending g. At M=16, where
* a chunk is one group, there is no cross-lane float work at all. Reassociating the g sum is what
* would break bit-equality, and the map is built so that no lane ever has to.
* Group indices ascend with the chunk index, and within a chunk the row's owner lane adds the
* chunk's group terms in ascending g, so the accumulation is the reference's ascending g loop.
* Reassociating the g sum would change the result; no lane does.
*
* LDS is touched a quad at a time (two dwords, 8 bytes), swizzled as `p_ix` below; unswizzled the
* wavefront's 32 quads for one u all sit in the same bank pair.
* LDS is accessed one quad (two dwords, 8 bytes) at a time, swizzled by `p_ix` below; unswizzled,
* the wavefront's 32 quads for one u would all sit in the same bank pair.
*
* ---- refusals (codes; RAD_R3D_TRACE=1 prints the line) -----------------------------------------
*
* The shim mirrors gemm_ntq_impl's and gemm_zp's checks and declines two things the reference
* accepts, each named at its site: M > 16 (the band is 16, the tile is 16 rows, mpad is the
* reference's 16-row pad; the prefill vocab projection is `logits_gemm` and is a tiled bf16 row,
* not a wider loop bound here), and a zero plane stored sub-byte (U2/U4/U5), where the kernel reads
* a zero a byte at a time -- a sub-byte zero plane needs the bit-extract path,
* `rad_load_bits(zb, bits, g)` is ten lines. w2a8's practical form, and this container's, is U8.
* accepts, each named at its site: M > 16 (the tile is 16 rows and mpad is the reference's 16-row
* pad; the prefill vocab projection is `logits_gemm`, a tiled bf16 row), and a zero plane stored
* sub-byte (U2/U4/U5), because the kernel reads a zero one byte at a time; that would need the
* bit-extract path (`rad_load_bits(zb, bits, g)`). w2a8's practical form, and this container's,
* is U8.
*/
#include <hip/hip_runtime.h>
@@ -127,12 +99,10 @@ constexpr int kTileQuads = 512; /* MT * CHUNK/4, constant over MT: 512
constexpr int kGroup = 128; /* the block size w2a8 is stored at */
constexpr int kMpad = 16; /* the reference's (M+15)/16*16, M <= 16 */
/* Column c sits at byte step_pos(c), bits 2 and 3 of c swapped, the map its own inverse. Applying
* it to the four columns of one dword leaves them four contiguous bytes and turns the map on the
* DWORD index into a swap of bits 0 and 1 -- the middle two dwords of a 16-byte step exchanged.
* The byte a dword starts at is therefore 4 * dword_pos(d): NOT 4 * step_pos(d), which swaps the
* wrong pair of bits and reads the step's dwords unexchanged. */
[[maybe_unused]] [[maybe_unused]] __device__ __forceinline__ int64_t step_pos(int64_t c)
/* Column c sits at byte step_pos(c): bits 2 and 3 of c swapped, a self-inverse map. On the dword
* index it is a swap of bits 0 and 1 (the middle two dwords of a 16-byte step exchanged), so
* dword d starts at byte dword_pos(d). Not 4 * step_pos(d), which swaps the wrong pair of bits. */
[[maybe_unused]] __device__ __forceinline__ int64_t step_pos(int64_t c)
{
return (c & ~(int64_t)12) | ((c & 4) << 1) | ((c & 8) >> 1);
}
@@ -141,14 +111,13 @@ __device__ __forceinline__ int64_t dword_pos(int64_t d)
return 4 * ((d & ~(int64_t)3) | ((d & 1) << 1) | ((d & 2) >> 1));
}
/* The LDS swizzle, at the granularity the tile is touched: a quad of four codes, two dwords, moved
* as one 8-byte word. Quads are numbered q = r*(CHUNK/4) + c, and p_ix XORs q's low four bits with
* bits 4..7 -- a bijection on 16-quad groups, so on any tile whose quad count is a multiple of 16.
* A wavefront reads q = m*(CHUNK/4) + kl*16 + u for fixed u; CHUNK/4 is a multiple of 16 at every
* MT, so q ^ (q >> 4) mod 16 is u ^ (4m + kl) mod 16 over the 32 lanes -- 16 distinct values, each
* one 8-byte access spanning two banks: 32 banks, no conflict. The swizzle must leave bit 0 of the
* quad index alone: the two dwords of a quad are adjacent only then, and the scalar form that
* XORs bits 5..9 of the dword index does not. */
/* The LDS swizzle, at the tile's access granularity: a quad of four codes (two dwords) moved as one
* 8-byte word. Quads are numbered q = r*(CHUNK/4) + c; p_ix XORs q's low four bits with bits 4..7,
* a bijection on 16-quad groups and so on any tile whose quad count is a multiple of 16. A
* wavefront reads q = m*(CHUNK/4) + kl*16 + u for fixed u; CHUNK/4 is a multiple of 16 at every MT,
* so q ^ (q >> 4) mod 16 is u ^ (4m + kl) mod 16 over the 32 lanes: 16 distinct values, each an
* 8-byte access spanning two banks, 32 banks with no conflict. The swizzle works on quad indices
* so the two dwords of a quad stay adjacent; XORing bits 5..9 of the dword index would not. */
__device__ __forceinline__ int p_ix(int q) { return q ^ ((q >> 4) & 15); }
/* Four int8 codes -> two f16x2 dwords. Exact: an integer of |a| <= 127 needs no rounding. */
@@ -159,17 +128,15 @@ __device__ __forceinline__ void i8x4_to_f16x2(uint32_t w, uint32_t& lo, uint32_t
(float)(int32_t)(int8_t)((w >> 24) & 0xffu));
}
/* Four U2 codes (one byte) -> two f16x2 dwords, BIASED by 128: each half is the f16 bits of
* (128 + code). An f16 at exponent 2^7 steps its mantissa by 1/8, so 128 + code is 0x5800 + 8*code
* and stays an integer. The dot therefore returns is + 128 * the group's code sum, which the
* epilogue takes off with the group's a_sum.
/* Four U2 codes (one byte) -> two f16x2 dwords, biased by 128: each half is the f16 bits of
* (128 + code). An f16 at exponent 2^7 steps its mantissa by 1/8, so 128 + code is 0x5800 + 8*code.
* The dot returns is + 128 * (the activation's group sum), which the epilogue removes with a_sum.
*
* The bias is 128 and not 1024 because of where the dot unit's precision runs out. a * (B + q) is an
* integer the size of 128 * B, and a lane's 64 of them sum to 64 * 128 * (B + 3): at B = 1024 that is
* 2^23, at B = 128 it is 2^20. The unit keeps a little under 24 bits of mantissa on integers, so the
* tail scales with the magnitude -- 71027 of 262144 dots off by up to 1/8 at B = 1024, 70115 off by
* up to 1/64 at B = 128, in the probe below. 128 is the largest power of two that keeps a lane's
* partial sum under 2^21, which is as close as a pair-dot gets; the header carries the rest. */
* The bias sets the magnitude of the dot's partial sums: a * (B + q) is up to 128 * B, and a lane's
* 64 of them reach 64 * 128 * (B + 3), about 2^23 at B = 1024 and 2^20 at B = 128. The dot unit
* keeps about 22 bits of mantissa on integers, so its error grows with the magnitude: over 262144
* sampled dots (probe below), the worst error is 1/8 at B = 1024 and 1/64 at B = 128. 128 is the
* largest power of two that keeps a lane's partial sum under 2^21. */
__device__ __forceinline__ void u2x4_biased(uint32_t w, uint32_t& lo, uint32_t& hi)
{
lo = 0x58005800u + ((w & 3u) << 3) + (((w >> 2) & 3u) << 19);
@@ -188,10 +155,10 @@ gemm_w2a8_kernel(const int8_t* __restrict__ a, const float* __restrict__ sa, int
constexpr int CG = (2048 / MT) / kGroup; /* groups per chunk */
constexpr int CL = (2048 / MT) / KL; /* codes per lane per chunk: 64 */
/* The tile: one quad of four codes per slot, two f16x2 dwords, typed as the quad the staging
* writes and the dot reads. The tile is a `uint2[]` and not a `uint32_t[]` reached through a
* vector pointer: the f32 variant of this row, written through `*(uint4*)&sA[]` at -O2, comes
* up with content that was never written. */
/* The tile: one quad of four codes (two f16x2 dwords) per slot, typed as the quad the staging
* writes and the dot reads. It is a `uint2[]`, not a `uint32_t[]` written through a vector
* pointer: an f32 tile written through `*(uint4*)&sA[]` at -O2 read back content that was never
* written. */
__shared__ __align__(16) uint2 sA[kTileQuads]; /* MT rows x CHUNK/2 f16, swizzled */
__shared__ float sT[kWaves * 16]; /* each warp's chunk terms: warp*16 + m*CG + j */
__shared__ float sSa[MT];
@@ -199,12 +166,10 @@ gemm_w2a8_kernel(const int8_t* __restrict__ a, const float* __restrict__ sa, int
const int lane = threadIdx.x & (kWave - 1);
const int warp = (int)(threadIdx.x / kWave);
const int n = blockIdx.x * kWaves + warp;
/* A wavefront past the last row sits the chunk out, but it stages: the tile is block-wide, so
* the warps that have no row to serve still write the rows their lanes own. Only a block whose
* EIGHT rows are all past the last row leaves, which is a block-uniform test and cannot strand
* a barrier; a guard on the row would, because the tail block's dead warps own tile rows and
* staging them from never-written memory corrupts the tile. The store is guarded by `live`
* below, so a dead wavefront writes nothing. */
/* A wavefront past the last row skips the dot but still stages its share of the block-wide
* tile. Only a block whose eight rows are all past N returns early: that test is block-uniform
* and cannot strand a barrier, whereas a per-row return would leave tile rows unstaged. The
* store is guarded by `live`. */
const bool live = n < N;
if ((int)blockIdx.x * kWaves >= N) return;
@@ -219,8 +184,8 @@ gemm_w2a8_kernel(const int8_t* __restrict__ a, const float* __restrict__ sa, int
float acc = 0.0f;
for (int ch = 0; ch < nchunk; ++ch) {
/* Stage the chunk's activation rows, widened and step-unpermuted on the way in. Cells are
* four codes: MT * CHUNK/4 = 512 of them at every MT, two per thread. */
/* Stage the chunk's activation rows, widened and step-unpermuted. Cells are four codes:
* MT * CHUNK/4 = 512 at every MT, two per thread. */
for (int i = threadIdx.x; i < MT * (chunk >> 2); i += kBlockThreads) {
const int r = i / (chunk >> 2), c = i % (chunk >> 2);
uint32_t w = 0;
@@ -261,26 +226,17 @@ gemm_w2a8_kernel(const int8_t* __restrict__ a, const float* __restrict__ sa, int
const float iz = (float)zq[(long long)n * z_rb + g];
const float sc = f16_to_f32(sb[(long long)n * (sb_rb >> 1) + g]);
const float term = ((is - iz * as) * sSa[m]) * sc; /* gemm_zp's parentheses */
/* One slot per (row, group-in-chunk): MT*CG is 16 at every MT, so this is 64 bytes.
* The group's two half-lanes write the same value, which is why the pair fold above is
* a fold and not a broadcast. */
/* One slot per (row, group-in-chunk); MT*CG is 16 at every MT (64 bytes). Both
* half-lanes of a group write the same folded value. */
sT[warp * 16 + m * CG + (kl >> 1)] = term;
}
__syncthreads();
/* Group ch*CG + j, for j ascending, into the lane that owns the row's answer. The reference
* walks g in ascending order and f32 adds are not associative, so the order is part of the
* answer, not a detail. The group gather is the one place in the kernel that crosses
* k-lanes; it is a barrier and four LDS reads at 64 bytes, which is cheaper than a shuffle
* round-trip. */
/* Groups ch*CG + j, j ascending, into the row's owner lane: f32 adds are not associative
* and the reference walks g ascending. This gather is the only cross-k-lane step; a
* barrier and a few 64-byte LDS reads are cheaper than a shuffle round-trip. */
if (live && kl == 0) {
/* A chunk carries CG groups, and group ch*CG + j lives in the two k-lanes 2j and 2j+1:
* they hold the same group's two halves, which is what the fold above combined. Every
* lane adds all CG of them in ascending j, so each lane of a row ends on the same
* partial sum, built in the same order the reference walks its groups in -- f32 adds are
* not associative, and a lane that finished its own group last rounds the tail
* differently. Reading a lane's `acc` would not do: by j = 2 it has already added a
* group of its own. */
/* Group ch*CG + j comes from k-lanes 2j and 2j+1, already folded. */
#pragma unroll
for (int j = 0; j < CG; ++j)
if (m < M) acc += sT[warp * 16 + m * CG + j];
@@ -297,25 +253,21 @@ int mt_for(int M) { return M <= 1 ? 1 : M <= 2 ? 2 : M <= 4 ? 4 : M <= 8 ? 8 : k
/* ================================================================== the launcher
*
* Codes: 0, -1 a required pointer is null, -2 a shape it does not read, -3 a pitch it cannot read,
* -4 HIP. `nt` is the threads argument every libr3d launcher takes and this kernel ignores: the
* work is one warp per weight row, and the wavefront count is the grid.
* Codes: 0, -1 a required pointer is null, -2 an unsupported shape, -3 an unreadable pitch, -4 HIP.
* `nt` is accepted for signature uniformity and ignored: the work is one warp per weight row.
*
* `b_scale` is the joined operand's base. The launcher reads the two planes out of its own header
* (rad_planes.h's rad_planes_find) and passes each plane's base, row pitch in bytes and the row
* count down, so the kernel reads the container's bytes where they lie. */
* `b_scale` is the joined operand's base. The launcher finds the two planes through its header
* (rad_planes.h's rad_planes_find) and passes each plane's base and row pitch in bytes, so the
* kernel reads the container's bytes in place. */
/* The instrument behind the measurement in the header: the dot on its own, one 64-code sample per
* lane, the f32 it produces beside the exact integer it should have produced. The driver runs it as
* `dot [n]`; the bias is sized from it, and it shows the f32 path to be exact. Two extern "C"
* symbols, no row, no cost at serve time. */
/* A probe of the dot's exactness: one 64-code sample per lane, the f32 the dot produces beside the
* exact integer. The test driver runs it as `dot [n]`; it is what sizes the bias. Two extern "C"
* symbols, no row, nothing at serve time. */
extern "C" __global__ void __launch_bounds__(256) probe_dot_kernel(const int8_t* a,
const uint8_t* codes,
float* out, long long* exact)
{
const int g = blockIdx.x * 32 + (int)(threadIdx.x / kWave) * 4 + (int)(threadIdx.x & 3);
const int lane4 = (int)(threadIdx.x & 31) & 3;
(void)lane4;
const uint32_t* w4 = (const uint32_t*)(codes + (long long)g * 16);
float p = 0.0f;
long long ex = 0;
@@ -353,8 +305,8 @@ extern "C" int r3d_gemm_nt_q_w2a8(long a, long a_scale, long b, long b_scale, lo
(void)nt;
if (!a || !a_scale || !b || !b_scale || !a_sum || !y) return -1;
if (M < 1 || N < 1 || K < 1) return -2;
if (M > kMTMax) return -2; /* the row says the same: C_LE("M", 16) */
if (group != kGroup) return -2; /* the tile is 128-wide: chunk = 128*16/MT */
if (M > kMTMax) return -2; /* as the row's C_LE("M", 16) */
if (group != kGroup) return -2; /* chunk = 128*16/MT */
if (K % kGroup) return -2;
if (K % 16) return -2; /* the step order is a 16-byte step */
const int mt = mt_for(M);
@@ -364,7 +316,7 @@ extern "C" int r3d_gemm_nt_q_w2a8(long a, long a_scale, long b, long b_scale, lo
if (G < 1) return -2;
if (!a_ld) a_ld = K;
if (!y_ld) y_ld = N;
if (a_ld % 4) return -3; /* four codes a dword, staged whole */
if (a_ld % 4) return -3; /* four codes per dword, staged whole */
if (((uintptr_t)b & 15u) || ((uintptr_t)a & 3u)) return -3;
const RadPlanesHeader* h = rad_planes_header((const void*)(uintptr_t)b_scale);
@@ -402,8 +354,8 @@ extern "C" int r3d_gemm_nt_q_w2a8(long a, long a_scale, long b, long b_scale, lo
/* ================================================================== the shim
*
* gemm_nt_q's slots, as gemm_ntq_impl takes them: 0 a (i8, step order), 1 a_scale (f32, one per
* row), 2 b (the u2 codes), 3 b_scale (the joined scale+zero), 4 y (bf16), 5 a_sum (f32, group
* major, mpad = 16 columns). */
* row), 2 b (the u2 codes), 3 b_scale (the joined scale+zero), 4 y (bf16), 5 a_sum (f32,
* group-major, mpad = 16 columns). */
extern "C" int r3d_rad_gemm_nt_q_w2a8(const RadArgs* a, RadStream s)
{
if (!a) return R3D_NO(RAD_E_INVAL);
@@ -434,16 +386,13 @@ extern "C" int r3d_rad_gemm_nt_q_w2a8(const RadArgs* a, RadStream s)
const long long ynel = r3d_numel(Y);
if (N < 1 || ynel % N) return R3D_NO(RAD_E_SHAPE);
const long long M = ynel / N;
if (M < 1 || M > kMTMax) return R3D_NO(RAD_E_SHAPE); /* DECLINE, in the header: the band is
* 16 and the tile is 16 rows; the
* prefill vocab projection is
* logits_gemm, a tiled bf16 row. */
if (M < 1 || M > kMTMax) return R3D_NO(RAD_E_SHAPE); /* declined; see the header */
if (r3d_numel(A) < M * K || r3d_numel(B) < N * K) return R3D_NO(RAD_E_SHAPE);
if (r3d_numel(AS) < M) return R3D_NO(RAD_E_INVAL); /* gemm_zp's code for this one */
if (r3d_numel(ASUM) < G * kMpad) return R3D_NO(RAD_E_INVAL);
const long long ap = r3d_row_pitch(A);
if (ap < K || ap % 4) return R3D_NO(RAD_E_STRIDE); /* step order read four codes a dword */
if (ap < K || ap % 4) return R3D_NO(RAD_E_STRIDE); /* step order: four codes a dword */
if (!r3d_contig(B)) return R3D_NO(RAD_E_STRIDE); /* gemm_zp: contiguous codes only */
long long yrows = 0;
const long long yp = r3d_rows_pitch(Y, N, &yrows);
@@ -459,8 +408,8 @@ extern "C" int r3d_rad_gemm_nt_q_w2a8(const RadArgs* a, RadStream s)
if (!sp || !zp || !es || !ez) return R3D_NO(RAD_E_DTYPE);
if (es->dtype != RAD_F16) return R3D_NO(RAD_E_DTYPE);
if (ez->dtype != RAD_U8)
return R3D_NO(RAD_E_DTYPE); /* DECLINE: a sub-byte zero plane needs the bit-extract path;
* rad_load_bits(zb, bits, g) is 10 lines. U8 is w2a8's form. */
return R3D_NO(RAD_E_DTYPE); /* declined: a sub-byte zero plane needs the bit-extract
* path (rad_load_bits). U8 is w2a8's form. */
if (es->rows != N || es->cols != G || ez->rows != N || ez->cols != G) return R3D_NO(RAD_E_SHAPE);
if ((es->row_bytes & 1) || es->row_bytes < 2 * G || ez->row_bytes < G) return R3D_NO(RAD_E_STRIDE);
@@ -469,8 +418,7 @@ extern "C" int r3d_rad_gemm_nt_q_w2a8(const RadArgs* a, RadStream s)
const int rc = r3d_gemm_nt_q_w2a8(
r3d_addr(A), r3d_addr(AS), r3d_addr(B), r3d_addr(BS), r3d_addr(ASUM), r3d_addr(Y), (int)M,
(int)N, (int)K, (int)group, (int)ap, (int)yp, 0, (long)s);
/* The launcher's codes are its own (r3d_launch.h's set: null, shape, pitch, HIP); the row speaks
* RAD codes, and the shim is where the two are named. Same mapping as the gated row's. */
/* Launcher codes (null, shape, pitch, HIP) to RAD codes, as in the gated row. */
if (rc == 0) return RAD_OK;
return R3D_NO(rc == -1 ? RAD_E_INVAL : rc == -2 ? RAD_E_SHAPE
: rc == -3 ? RAD_E_STRIDE : RAD_E_DEVICE);
@@ -478,8 +426,8 @@ extern "C" int r3d_rad_gemm_nt_q_w2a8(const RadArgs* a, RadStream s)
/* ================================================================== the layout hook
*
* The scoped exception to libr3d's "no layout hooks, no repacking" rule, transcribed from libref's
* `ref_layout_gemm_q` with the checks and their order unchanged:
* One of libr3d's few layout hooks, transcribed from libref's `ref_layout_gemm_q` with the checks
* and their order unchanged:
*
* if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED;
* const ZpGrid* zg = zp_grid(dtype); group = param "group";
@@ -493,17 +441,14 @@ extern "C" int r3d_rad_gemm_nt_q_w2a8(const RadArgs* a, RadStream s)
*
* zp_grid, zero_dt and sel_roles are libref-internal statics, inlined below for w2a8: the grid is
* { "w2a8", RAD_U2, ZP_I8_ROW }, zero_dt is U8|U2|U4|U5, and the practical form is U8 (the kernel
* declines a sub-byte zero, at its site, with the fix named). The answer is rad_planes.h's own:
* the tag is "rad.planes", the dtype the first plane's, the shape the byte count. NOTHING HERE
* MOVES A BYTE: this hook is the reader the mapped tier needs and the row's relayout is null.
* declines a sub-byte zero at its site). The answer is rad_planes.h's: the tag "rad.planes", the
* dtype the first plane's, the shape the byte count. The hook moves no bytes.
*/
/* The writer for the joined operand this hook names: codes, scale and zero as the plan's one
* `rad.planes` weight, assembled by rad_planes_relayout -- header, entries, each plane's rows,
* copied. The ABI's contract is that a query answering RAD_OK is a promise the loader keeps on the
* row's word: "out describes the stored form, and relayout will produce it" (rad_abi.h), and a
* weight with no hook is the only kind that can be read where it lies, which is why a joined weight
* is placed in VRAM and a plain one can stay on the mapped tier. Nothing here transforms a code:
* the bytes written are the bytes stored. */
/* The writer for the joined operand this hook names: codes, scale and zero as one `rad.planes`
* weight, assembled by rad_planes_relayout (header, entries, each plane's rows, copied). A layout
* query answering RAD_OK promises "out describes the stored form, and relayout will produce it"
* (rad_abi.h). Only a weight with no hook can be read in place, so a joined weight is placed in
* VRAM while a plain one can stay on the mapped tier. No code is transformed. */
extern "C" int r3d_relayout_gemm_nt_q_w2a8(const RadParam* p, int n_p, int operand,
const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n_planes, void* dst,
@@ -514,9 +459,9 @@ extern "C" int r3d_relayout_gemm_nt_q_w2a8(const RadParam* p, int n_p, int opera
return rad_planes_relayout(enc, sel, planes, n_planes, dst, dst_bytes);
}
/* A layout query answers in the engine's own vocabulary and leaves no trace, and a model that will
* not load says only that two rows want one weight in two forms. This prints the question and the
* answer -- operand, band dtype, planes offered, code returned -- under RAD_R3D_TRACE=1. */
/* Under RAD_R3D_TRACE, prints each layout query (operand, band dtype, planes offered) and its
* return code. Otherwise a model that fails to load reports only that two rows want one weight in
* two forms. */
static int r3d_layout_trace(const char* who, int operand, const char* dt, int n_planes, int rc) {
if (r3d_trace_on())
std::fprintf(stderr, "[r3d] %s: operand %d dtype %s planes %d -> %d\n", who, operand,
@@ -530,11 +475,10 @@ extern "C" int r3d_layout_gemm_nt_q_w2a8(const RadParam* p, int n_p, int operand
{
if (n < 2 || operand != 3) return r3d_layout_trace("r3d_layout_gemm_nt_q_w2a8", operand, r3d_pgets(p, n_p, "dtype"), n, RAD_E_UNSUPPORTED);
const char* dt = r3d_pgets(p, n_p, "dtype");
/* The plan this hook describes -- the (scale, zero) pair joined on operand 3 -- is a statement
* about the CONTAINER, not about the code width, and the engine forces every row that reads one
* weight to answer with one form. So both packed grids come through, and the width a row can
* unpack is the SHIM's business: gemm_nt_q_w2a8_m16 reads U2 and nothing else,
* gemm_nt_q_w2a8_tiled reads U2 and U4, and each says so at its own line. */
/* The (scale, zero) pair joined on operand 3 describes the container, not the code width, and
* every row that reads one weight must answer with one form. So both packed grids pass here;
* the code width is checked by each shim (gemm_nt_q_w2a8_m16 reads U2 only,
* gemm_nt_q_w2a8_tiled reads U2 and U4). */
if (!dt || (std::strcmp(dt, "w2a8") != 0 && std::strcmp(dt, "w4a8_asym") != 0))
return r3d_layout_trace("r3d_layout_gemm_nt_q_w2a8", operand, r3d_pgets(p, n_p, "dtype"), n, RAD_E_DTYPE); /* the packed grids */
const long long group = r3d_pgeti_or(p, n_p, "group", 0);
@@ -551,8 +495,8 @@ extern "C" int r3d_layout_gemm_nt_q_w2a8(const RadParam* p, int n_p, int operand
if (sel[i] < 0 || sel[i] >= enc->n_planes) return r3d_layout_trace("r3d_layout_gemm_nt_q_w2a8", operand, r3d_pgets(p, n_p, "dtype"), n, RAD_E_DTYPE);
if (std::strncmp(enc->plane[sel[i]].role, want, RAD_ENC_STR) != 0) return r3d_layout_trace("r3d_layout_gemm_nt_q_w2a8", operand, r3d_pgets(p, n_p, "dtype"), n, RAD_E_DTYPE);
}
/* The codes plane is the one this operand does NOT take: the weight's role "codes", the grid's
* dtype, block {1,1}. libref finds it by role over enc->plane, not by sel. */
/* The codes plane is not part of this operand: role "codes", the grid's dtype, block {1,1}.
* libref finds it by role over enc->plane, not by sel. */
int codes = -1;
for (int i = 0; i < enc->n_planes; ++i)
if (std::strncmp(enc->plane[i].role, "codes", RAD_ENC_STR) == 0) { codes = i; break; }