radiance 1.7.0

- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves
  every shipped container
- Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime
- /v1/messages works with Claude Code as it ships (gearwave00001)
- a long agent conversation keeps its cached history when the client rewrites its last turn
  (manueloverride)
- a stream's first event goes out with the first token, so llama-benchy measures prefill right
- opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42)
- four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b)
- NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA
  adapters (baKon)
- many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool
  arguments and the CPU's vision and audio encoders
- plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-16 18:25:18 +02:00
1 parent b068190326
commit 1e968f6e1d
739 files changed
+128447 -84632

No files matched your search

+53 -139
View File
@@ -1,41 +1,24 @@
// W4A8 GEMM FOR PREFILL: the same packed weight and the same rotated int8 activation as
// r4d_gemm_w4a8_tiled, laid out for the regime where the matrix pipe is the answer.
// W4A8 GEMM for prefill: the packed weight and rotated int8 activation of r4d_gemm_w4a8_tiled, for
// the compute-bound regime. At decode this GEMM is a weight stream at the DRAM ceiling; at M = 4096
// (730 GFLOP against 44.6 MB) every non-matrix instruction is overhead.
//
// WHY A SECOND KERNEL. At a decode batch this GEMM is a weight stream and every question is
// bandwidth: `gate_up_proj` at M = 9 moves 47 MB and runs at the DRAM ceiling, so the inner loop's
// instruction mix does not matter because the loop is waiting on memory. At M = 4096
// the same call is 730 GFLOP against 44.6 MB, so the loop is waiting on nothing and every
// instruction that is not a matrix instruction is a tax. The two want different code.
// Ceiling: v_wmma_i32_16x16x16_iu8 occupies its SIMD for 8 cycles on gfx1201 (f16/bf16: 16), an
// int8 peak of ~375 TOPS. There is no wider iu8 form (v_wmma_i32_16x16x32_iu8 does not exist), and
// iu4 needs both operands 4-bit. The budget is 8 cycles per matrix instruction plus one per other
// VALU instruction. The group fold is the largest non-matrix item, and as v_fma_mix_f32 (VOP3P,
// never dual-issued) it costs more than as v_fmac_f32, which pairs as v_dual_fmac_f32, the only
// VOPD form this loop can use (no v_dual_and_b32 or v_dual_lshlrev_b32). So the f16 scale is
// materialised and folded with plain FMACs.
//
// THE CEILING THIS AIMS AT. `v_wmma_i32_16x16x16_iu8` occupies its SIMD for EIGHT cycles on
// gfx1201 -- half what f16/bf16 take, at 16.0-16.2 cycles -- so the int8 matrix peak is ~375 TOPS,
// not the ~187 an f16 roofline would say. gfx1201 has no wider iu8 form: `llvm-mc` rejects
// `v_wmma_i32_16x16x32_iu8`, and `v_wmma_i32_16x16x32_iu4` is exactly twice the rate but needs
// BOTH operands 4-bit, which the activation is not. So the budget is 8 cycles of matrix work per
// matrix instruction plus one cycle per other VALU instruction, and the job is to make the second
// term small. In a register-only replica of this inner loop, the matrix instructions on their own
// reach most of that peak and the weight unpack costs little on top; the GROUP FOLD is the largest
// non-matrix item in the loop, and it costs materially more as a `v_fma_mix_f32` than as a plain
// `v_fmac_f32`.
// A fragment reads: the tiled kernel stores A k-major and issues one ds_load_b64 per (row tile,
// k step). Here a row's 64 bytes are half-major, [row][lane half][k step][2], so one ds_load_b128
// fetches a lane's two consecutive k steps (TM*2 reads per 64-k block instead of TM*4). Only the LDS
// copy is permuted; a lane still consumes the k values the weight names.
//
// The reason is dual issue. `v_fma_mix_f32` is VOP3P, which can never dual-issue;
// `v_dual_fmac_f32` is the ONLY VOPD form gfx1201 offers that this loop can use
// (`v_dual_and_b32` and `v_dual_lshlrev_b32` do not exist here, so the unpack cannot be packed at
// all). This kernel therefore forces the f16 scale to materialise and folds with plain FMACs.
//
// THE OTHER HALF IS THE ACTIVATION FRAGMENT READ. The tiled kernel stores A in LDS k-major and
// reads one `ds_load_b64` per (row tile, k step): 32 reads and ~30 `s_wait_dscnt` per scale group.
// Here the row's 64 bytes are stored HALF-MAJOR -- [row][lane half][k step][2] instead of
// [row][k step][lane half][2] -- which puts a lane's two consecutive k steps side by side, so one
// `ds_load_b128` fetches them both and a whole 64-k block of A is TM*2 reads instead of TM*4.
// The permutation is inside the LDS copy only: the packed weight is untouched and the activation
// in memory is untouched, because a lane still consumes exactly the k values the weight names.
//
// The 4-dword XOR swizzle keeps that conflict-free. A row is 16 dwords and LDS has 32 banks, so
// the row index contributes only its low bit; the mask has to separate the rest and it has to
// permute whole 4-dword BLOCKS or a `ds_load_b128` lands astride two of them. ((r >> 1) & 3) << 2
// gives lanes 0..7 the start banks {0,16,4,20,8,24,12,28} -- eight distinct starts, four dwords
// each, all 32 banks once.
// A 4-dword XOR swizzle keeps it conflict-free. A row is 16 dwords across 32 banks, so the row
// index contributes only its low bit; the mask must separate the rest and permute whole 4-dword
// blocks so a ds_load_b128 never straddles two. ((r >> 1) & 3) << 2 gives lanes 0..7 start banks
// {0,16,4,20,8,24,12,28}, all 32 banks once.
#include "r4d.h"
#include "r4d_gemm_args.h"
#include "r4d_common.h"
@@ -52,9 +35,8 @@ typedef float v8f_t __attribute__((ext_vector_type(8)));
typedef unsigned u4v_t __attribute__((ext_vector_type(4)));
typedef float v4f_t __attribute__((ext_vector_type(4)));
// The unpacked byte is 16*nibble, read SIGNED, and the 16 comes back out in the store. Three
// instructions for two dwords is the floor here: gfx1201 has no `v_lshl_and_b32` and no nibble
// permute, so each output dword costs at least its own mask.
// The unpacked byte is 16*nibble, read signed; the 16 is removed at the store. Three instructions
// for two dwords is the floor: gfx1201 has no v_lshl_and_b32 and no nibble permute.
__device__ __forceinline__ v2i_t pf_dq8(unsigned w) {
v2i_t r;
r[0] = (int)((w << 4) & 0xF0F0F0F0u);
@@ -63,12 +45,14 @@ __device__ __forceinline__ v2i_t pf_dq8(unsigned w) {
}
// OPT bits
// 1 grid swizzle: walk GSW m-tiles per n-tile so the resident workgroups form a square and
// both the A row block and the W column block are shared by several of them
// 2 fold with plain `v_fmac_f32` (VOPD) instead of letting the f16 convert contract into a
// `v_fma_mix_f32`
// 4 split barrier: signal after the commit, do the k block's matrix work, wait at the top
// 8 read the A fragments one `ds_load_b64` per k step (the tiled kernel's shape), for comparison
// 1 grid swizzle: walk GSW m-tiles per n-tile so resident workgroups share A row blocks and
// W column blocks
// 2 fold with plain v_fmac_f32 (VOPD) rather than v_fma_mix_f32
// 8 read A fragments with one ds_load_b64 per k step (the tiled kernel's shape)
// 128 load a row tile's eight Ascale values as two b128: a lane's rows
// (m0 + i*16 + 8*half + e, e = 0..7) are contiguous
// 256 unpredicated stores for blocks wholly inside the matrix (all but the last row block)
// 512 non-temporal stores for C
template <int WM, int WN, int TM, int TN, int OPT, int GSW>
__global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
const signed char* __restrict__ A, const float* __restrict__ Ascale,
@@ -83,31 +67,13 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
constexpr int NT_ = WM * WN * 32;
constexpr int UNR = GRP / BK; // k blocks per scale group
constexpr bool VOPD = (OPT & 2) != 0;
constexpr bool SPLITB = (OPT & 4) != 0;
constexpr bool NARROWA = (OPT & 8) != 0;
// ABLATIONS. Both compute wrong answers on purpose and both keep every instruction that is not
// the thing being priced, so what they measure is one item and not a deleted pipeline.
// 16 every fragment read comes from buffer 0 at k = 0: the same LDS reads, the same count,
// the same banks, but no dependency on the staging that filled it
// 32 the A fragment never leaves registers: prices the A LDS read itself
constexpr bool ABSTALE = (OPT & 16) != 0;
constexpr bool ABNOA = (OPT & 32) != 0;
// 64 store one output element in eight, with the other seven folded into it so nothing in the
// k loop goes dead: prices the store epilogue, which is 64 predicated bf16 stores, 64 f32
// multiplies, 64 software bf16 roundings and 32 `Ascale` loads per lane
constexpr bool ABSTORE = (OPT & 64) != 0;
// 128 load the eight `Ascale` values of a row tile as two b128 instead of eight scalars: the
// rows a lane owns (m0 + i*16 + 8*half + e, e = 0..7) are CONTIGUOUS
constexpr bool ASCVEC = (OPT & 128) != 0;
// 256 an unpredicated store path for the blocks that are wholly inside the matrix, which at a
// prefill M is all but the last row block: 64 compares and 64 cndmasks per lane go away
// 512 non-temporal stores for C, which is written once and never read by this kernel
constexpr bool STFAST = (OPT & 256) != 0;
constexpr bool STNT = (OPT & 512) != 0;
// Both stagings move 16 bytes per thread per pass, with a predicate on the last one so a block
// shape does not have to divide the block width. AD/4 = BM*4 and WD/4 = (BN/16)*32, so at 512
// threads that is exact only for BM = 128 and BN = 256; every other tile in the sweep is ragged.
// Both stagings move 16 bytes per thread per pass, predicated on the last pass so the tile need
// not divide the thread count (exact at 512 threads only for BM = 128, BN = 256).
constexpr int AU = AD / 4, WU = WD / 4;
constexpr int APT = (AU + NT_ - 1) / NT_, WPT = (WU + NT_ - 1) / NT_;
constexpr bool APAD = (AU % NT_) != 0, WPAD = (WU % NT_) != 0;
@@ -131,8 +97,7 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
const int m0 = by * BM, n0 = bx * BN;
const int kpb = K / BK, nsz = K / GRP;
// A LDS position of (row, lane half, k step). See the header: half-major inside the row, with a
// 4-dword XOR that separates eight rows.
// A LDS position of (row, lane half, k step): half-major within the row, 4-dword XOR swizzle.
#define PF_APOS(r, h, u) ((r) * 16 + ((((h) * 8 + (u) * 2)) ^ ((((r) >> 1) & 3) << 2)))
v8f_t acc[TN][TM];
@@ -182,13 +147,9 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
}
};
auto bar = [&]() {
// THE LDS STORES IN `commit` HAVE TO LAND BEFORE THE SIGNAL, which is what the
// release fence is for. `s_barrier_signal` is not `__syncthreads()`: it emits
// the barrier alone, with no wait on the outstanding `ds_store`s. Without the
// fence a wave announces writes that are still in flight and its neighbours read
// whatever LDS held from the previous k block -- the generated code carries
// `s_wait_loadcnt` for the global loads feeding the stores and no `s_wait_dscnt`
// at all, and the kernel returns a different answer on every call.
// The release fence makes commit's LDS stores land before the signal. Unlike
// __syncthreads(), s_barrier_signal emits no s_wait_dscnt, so without the fence other
// waves would read the previous k block's LDS contents.
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup");
__builtin_amdgcn_s_barrier_signal(-1);
__builtin_amdgcn_s_barrier_wait(-1);
@@ -204,29 +165,21 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
const int k0 = kg + h * BK;
const int buf = h & 1;
if (k0 + BK < K) issue(k0 + BK);
if ((k0 % GRP) == 0) { // the group scale, one whole k block early
if ((k0 % GRP) == 0) { // the group scale, one k block early
#pragma unroll
for (int j = 0; j < TN; j++)
scv[j] = Ws[(unsigned)(((n0 / 16 + wn * TN + j) * nsz + k0 / GRP) * 16 + (lane & 15))];
}
// ---- fragments: the whole 64-k block, up front ----
// ---- fragments for the whole 64-k block, up front ----
v2i_t af[TM][BK / 16];
#pragma unroll
for (int i = 0; i < TM; i++) {
const int row = (wm * TM + i) * 16 + (lane & 15);
if (ABNOA) {
#pragma unroll
for (int u = 0; u < BK / 16; u++) {
af[i][u][0] = (int)(0x11111111u + i + u);
af[i][u][1] = (int)(0x22222222u + i);
}
continue;
}
if (NARROWA) {
#pragma unroll
for (int u = 0; u < BK / 16; u++) {
const unsigned* p = lds_a[ABSTALE ? 0 : buf] + PF_APOS(row, half, u);
const unsigned* p = lds_a[buf] + PF_APOS(row, half, u);
af[i][u][0] = (int)p[0];
af[i][u][1] = (int)p[1];
}
@@ -234,7 +187,7 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
#pragma unroll
for (int q = 0; q < BK / 32; q++) { // one b128 = two k steps
const u4v_t v =
*(const u4v_t*)(lds_a[ABSTALE ? 0 : buf] + PF_APOS(row, half, 2 * q));
*(const u4v_t*)(lds_a[buf] + PF_APOS(row, half, 2 * q));
af[i][2 * q][0] = (int)v[0];
af[i][2 * q][1] = (int)v[1];
af[i][2 * q + 1][0] = (int)v[2];
@@ -245,15 +198,12 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
u4v_t wv[TN];
#pragma unroll
for (int j = 0; j < TN; j++)
wv[j] = *(const u4v_t*)(lds_w[ABSTALE ? 0 : buf] + (size_t)(wn * TN + j) * 128 + lane * 4);
wv[j] = *(const u4v_t*)(lds_w[buf] + (size_t)(wn * TN + j) * 128 + lane * 4);
if (SPLITB) { // the next block's LDS write, then arrive
if (k0 + BK < K) commit(buf ^ 1);
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup"); // see bar()
__builtin_amdgcn_s_barrier_signal(-1);
}
// ---- the matrix work ----
// ---- matrix work ----
// A group's first WMMA seeds C with 0x4B400000 (the float 1.5 * 2^23), so the int32 sum's
// bits read as a float are 12582912 + sum while |sum| < 2^22. The fold below multiplies that
// float by the scale, and the bias 12582912 * sum(scale) is removed after the K loop.
#pragma unroll
for (int u = 0; u < BK / 16; u++) {
#pragma unroll
@@ -274,9 +224,8 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
#pragma unroll
for (int j = 0; j < TN; j++) {
sc[j] = __half2float(__ushort_as_half((unsigned short)(scv[j] & 0xFFFFu)));
// Materialise the convert. Left alone the compiler folds it into `v_fma_mix_f32`, which
// reads the f16 for free and is VOP3P -- and VOP3P never dual-issues, so the fold stays
// at one instruction per accumulator element instead of one per two.
// Materialise the convert; otherwise it contracts into v_fma_mix_f32 (VOP3P, never
// dual-issued).
if (VOPD) asm("" : "+v"(sc[j]));
csum[j] += sc[j];
}
@@ -289,12 +238,8 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
acc[j][i][e] = fmaf(__uint_as_float((unsigned)g[j][i][e]), sc[j], acc[j][i][e]);
}
if (SPLITB) {
__builtin_amdgcn_s_barrier_wait(-1);
} else {
if (k0 + BK < K) commit(buf ^ 1);
bar();
}
if (k0 + BK < K) commit(buf ^ 1);
bar();
}
}
#undef PF_APOS
@@ -308,30 +253,14 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
for (int e = 0; e < 8; e++) acc[j][i][e] += d;
}
if (ABSTORE) {
#pragma unroll
for (int j = 0; j < TN; j++)
#pragma unroll
for (int i = 0; i < TM; i++) {
float v = 0.f;
#pragma unroll
for (int e = 0; e < 8; e++) v += acc[j][i][e];
const int n = n0 + (wn * TN + j) * 16 + (lane & 15);
const int m = m0 + (wm * TM + i) * 16 + 8 * (lane >> 4);
if (n < N && m < M) C[(size_t)m * N + n] = (__hip_bfloat16)(v * Ascale[m] * 0.0625f);
}
return;
}
const bool inside = STFAST && (m0 + BM <= M);
#pragma unroll
for (int i = 0; i < TM; i++) {
float as[8];
const int mb = m0 + (wm * TM + i) * 16 + 8 * (lane >> 4);
if (ASCVEC) {
// The vector reads Ascale[mc .. mc+7], which lies inside [0, M) only when M >= 8. Below that
// it would read past the last row's scale -- past the operand, when the plane is exactly M
// long -- so a short M takes the clamped scalar read for every lane instead. M is uniform
// over the grid, so the branch costs one compare.
// The vector read Ascale[mc .. mc+7] stays inside [0, M) only when M >= 8; a short M, or a
// lane whose rows were clamped, takes the clamped scalar read instead.
const int mc = mb < M - 8 ? mb : (M > 8 ? M - 8 : 0);
if (M >= 8) {
const v4f_t* pv = (const v4f_t*)(Ascale + mc);
@@ -353,7 +282,7 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel(
const __hip_bfloat16 v =
(__hip_bfloat16)(acc[j][i][e] * (ASCVEC ? as[e] : Ascale[m]) * 0.0625f);
if (inside || (n < N && m < M)) {
// the non-temporal builtin does not take a __hip_bfloat16*, so go through its bits
// the non-temporal builtin does not take a __hip_bfloat16*, so store its bits
if (STNT)
__builtin_nontemporal_store(*(const unsigned short*)&v,
(unsigned short*)(C + (size_t)m * N + n));
@@ -380,36 +309,22 @@ static void pf_launch(long a, long ascale, long wq, long ws, long c, int M, int
void r4d_gemm_w4a8_prefill(long a, long ascale, long wq, long ws, long c, int M, int K, int N,
long stream, int variant) {
#define PF(...) pf_launch<__VA_ARGS__>(a, ascale, wq, ws, c, M, K, N, stream)
// The arms tile_bmbn in r4d_rows.cpp lists; 8, 9, 10, 13 and 14 change the tile, the rest are
// BM 128 x BN 256 with other OPT bits.
switch (variant) {
case 1: PF(2, 8, 4, 2, 0, 1); break; // no VOPD fold, b128 A
case 2: PF(2, 8, 4, 2, 8, 1); break; // b64 A, no VOPD fold
case 3: PF(2, 8, 4, 2, 10, 1); break; // b64 A, VOPD fold
case 4: PF(2, 8, 4, 2, 6, 1); break; // + split barrier
case 5: PF(2, 8, 4, 2, 3, 4); break; // + grid swizzle, GSW 4
case 6: PF(2, 8, 4, 2, 3, 8); break; // + grid swizzle, GSW 8
case 7: PF(2, 8, 4, 2, 7, 8); break; // swizzle + split barrier
case 8: PF(2, 8, 2, 2, 2, 1); break; // BM 64, BN 256
case 9: PF(2, 8, 3, 2, 2, 1); break; // BM 96, BN 256
case 10: PF(2, 8, 4, 1, 2, 1); break; // BM 128, BN 128
case 11: PF(4, 8, 4, 2, 2, 1); break; // BM 256, BN 256, 1024 threads
case 12: PF(2, 16, 4, 2, 2, 1); break; // BM 128, BN 512, 1024 threads
case 13: PF(1, 8, 4, 2, 2, 1); break; // BM 64, BN 256, 256 threads
case 14: PF(2, 8, 8, 1, 2, 1); break; // BM 256, BN 128
case 15: PF(4, 4, 4, 2, 2, 1); break; // BM 256, BN 128, 512 threads
case 20: PF(2, 8, 4, 2, 2 | 16, 1); break; // abl: fragments from a stale buffer
case 21: PF(2, 8, 4, 2, 2 | 32, 1); break; // abl: A fragment never touches LDS
case 22: PF(2, 8, 4, 2, 2 | 16 | 32, 1); break; // abl: both
case 23: PF(2, 8, 4, 2, 8 | 16, 1); break; // abl: stale, b64 A reads
case 24: PF(2, 8, 4, 2, 0, 1); break; // b128 A, fma_mix fold
case 25: PF(2, 8, 4, 2, 2 | 64, 1); break; // abl: one store in eight
case 26: PF(2, 8, 4, 2, 8 | 64, 1); break; // abl: one store in eight, b64 A
case 27: PF(2, 8, 4, 2, 2 | 128, 1); break; // vectorised Ascale
case 28: PF(2, 8, 4, 2, 8 | 128, 1); break; // vectorised Ascale, b64 A
case 29: PF(2, 8, 4, 2, 8 | 128 | 256, 1); break; // + unpredicated store
case 30: PF(2, 8, 4, 2, 8 | 128 | 256 | 512, 1); break; // + non-temporal
case 31: PF(2, 8, 4, 2, 2 | 128 | 256, 1); break; // b128 A + both
case 32: PF(2, 8, 4, 2, 8 | 128 | 256 | 1, 4); break; // + grid swizzle 4
case 33: PF(2, 8, 4, 2, 8 | 128 | 256 | 1, 8); break; // + grid swizzle 8
default: PF(2, 8, 4, 2, 2, 1); break; // b128 A + VOPD fold
}
#undef PF
@@ -417,8 +332,7 @@ void r4d_gemm_w4a8_prefill(long a, long ascale, long wq, long ws, long c, int M,
// ============================================================== the plugin ABI entry points
//
// The operand reader and the split law are in r4d_gemm_args.h. See libr4d/r4d_args.h for why there
// is no adapter directory between the core and these launches.
// The operand reader and the split law are in r4d_gemm_args.h; see libr4d/r4d_args.h.
extern "C" int r4d_rad_gemm_w4a8_prefill(const RadArgs* a, RadStream s) {
GemmOps g;
int rc = gemm_ops(a, &g, 1);