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

+33 -59
View File
@@ -1,39 +1,31 @@
// r4d_gemm_w2a8_nt.hip - r4d_gemm_w2a8_nt: 2-bit-weight, 8-bit-activation skinny GEMM, gfx1201.
// C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T with A quantised per row and W asymmetric per (row, group).
//
// WHAT IT IS FOR. The DFlash2 draft head is `x[M,5120] @ W[124160,5120]^T` at two bits, run once
// per decode step. Its 178.8 MB weight puts a one-pass memory floor near 280 us; at M = 64 its
// 64 x 124160 x 5120 MACs cost around 815 us on the f16 WMMA at 207 TF/s, which is the rate a bf16
// dequant form (`0x3F80 | v<<5`) runs at. So a bf16 form is COMPUTE bound once several sequences
// are in flight, not memory bound. The same codes on v_wmma_i32_16x16x16_iu8 at 407 TF/s put the
// compute floor near 200 us, under the memory floor, so this kernel is a stream at both ends of
// the M range.
// Used for the DFlash2 draft head, `x[M,5120] @ W[124160,5120]^T` at two bits, once per decode
// step. Its 178.8 MB weight gives a memory floor near 280 us. A bf16 dequant form runs on the f16
// WMMA (207 TF/s) and is compute bound at M = 64 (~815 us); v_wmma_i32_16x16x16_iu8 at 407 TF/s
// puts the compute floor near 200 us, under the memory floor, across the M range.
//
// STRUCTURE AND ACTIVATION ARE r4d_gemm_w4a8_nt_m64's: same 512-byte
// packed block (32 lanes x 16 bytes), same permuted int8 activation from r4d_quant_act_i8_asum,
// same [K/group][Mpad] group-major activation sums, same split-K reduction, same per-row scale
// applied once at the store. A packed block covers 128 K here instead of 64, because the code is
// half as wide.
// Structure and activation as r4d_gemm_w4a8_nt_m64: 512-byte packed block (32 lanes x 16 bytes),
// permuted int8 activation from r4d_quant_act_i8_asum, [K/group][Mpad] group-major activation sums,
// split-K reduction, per-row scale at the store. A packed block covers 128 K (64 for 4-bit).
//
// THE UNPACK PUTS THE CODE IN THE BYTE'S HIGH TWO BITS. For q in 0..3 the byte 64*q is exactly
// the UNSIGNED int8 of 64*q, so
// The unpack puts the code in the byte's high two bits; for q in 0..3 the byte 64*q is the
// unsigned int8 of 64*q:
//
// t0 = (w << 6) & 0xC0C0C0C0 bytes <- fields 0, 4, 8, 12
// t1 = (w << 4) & 0xC0C0C0C0 bytes <- fields 1, 5, 9, 13
// t2 = (w << 2) & 0xC0C0C0C0 bytes <- fields 2, 6, 10, 14
// t3 = w & 0xC0C0C0C0 bytes <- fields 3, 7, 11, 15
//
// is the whole dequant: seven instructions per dword, which is two 16x16x16 fragments, and the
// factor of 64 folds into the group scale at the store. Fragment 2d+0 is (t0, t1) and 2d+1 is
// (t2, t3), so element e of fragment h reads field 4*(e&3) + (e>>2) + 2h of dword d -- which is
// what the packer writes and nothing else has to know.
// Seven instructions per dword (two 16x16x16 fragments); the factor 64 is removed at the store.
// Fragment 2d+0 is (t0, t1) and 2d+1 is (t2, t3), so element e of fragment h reads field
// 4*(e&3) + (e>>2) + 2h of dword d, as the packer writes it.
//
// WHY UNSIGNED, AND WHAT IT COSTS. Four levels are not enough for a symmetric grid: the draft
// head's quantiser is asymmetric per (row, group of 128), w ~= sc*(q - zp), and 16*nibble-style
// offset binary is affine in the code where two's complement is affine only on each half of the
// range. So the WMMA reads B unsigned and the zero point comes out afterwards as
// sc*(<a,q> - zp*sum a), with the group's activation sum loaded from Asum. That is the same
// arrangement r4d_gemm_w4a8_asym_nt_m64 uses, and the same quantiser produces both.
// Unsigned because four levels are too few for a symmetric grid: the quantiser is asymmetric per
// (row, group of 128), w ~= sc*(q - zp). The WMMA reads B unsigned and the zero point is removed
// as sc*(<a,q> - zp*sum a) with the group's activation sum from Asum, as in
// r4d_gemm_w4a8_asym_nt_m64.
#include "r4d.h"
#include "r4d_gemm_args.h"
#include "r4d_common.h"
@@ -43,9 +35,6 @@
#include <cstdint>
#include <stdexcept>
#ifndef R4D_GEMM_W2A8_GROUP
#define R4D_GEMM_W2A8_GROUP 128 // K per (scale, zero) pair
#endif
#define R4D_GEMM_W2A8_KPB 128 // K per packed block: 8 k steps, 16 bytes per lane
#define WAVE32 32
@@ -81,13 +70,10 @@ __device__ __forceinline__ v8i w2_mmai(v2i a, v2i b, v8i c) {
return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a, false, b, c, false);
}
// __launch_bounds__ HAS TO SEE MT, NOT JUST NPW. A block cannot exceed 1024 threads, but this
// kernel's accumulators scale with MT * NPW: at MT=4 the two arrays alone are 8*MT*NPW VGPRs and
// the group's activation sums are another 8*MT. Telling the compiler 1024 makes it budget for 32
// waves on ONE workgroup, which on this part is 1536/32 = 48 VGPRs per wave, so every MT=4 config
// spills hard enough that MB=2 -- which reads the WHOLE WEIGHT TWICE at M=64 -- is faster than any
// of them. The bound has to be what the config can actually run, and an MT=4 config can never run
// 32 waves. The same rule, on NPW alone, is in r4d_gemm_w4a8_nt_m64.
// __launch_bounds__ depends on MT as well as NPW. Accumulators are 8*MT*NPW VGPRs per array plus
// 8*MT for the activation sums; a bound of 1024 budgets 1536/32 = 48 VGPRs per wave, and every MT=4
// config then spills badly. The bound must be what the config can run. r4d_gemm_w4a8_nt_m64 has
// the same rule on NPW alone.
#define R4D_W2A8_BOUNDS(MT, NPW) \
((MT) >= 4 ? 256 : (((MT) >= 2 || (NPW) >= 4) ? 512 : 1024))
@@ -137,18 +123,12 @@ __global__ __launch_bounds__(R4D_W2A8_BOUNDS(MT, NPW)) void r4d_gemm_w2a8_nt_ker
(size_t)(t * nsz + kbase / R4D_GEMM_W2A8_GROUP) * 16 + (lane & 15);
}
// PREFETCHING THE NEXT GROUP'S WEIGHT IS A LOSS, and the MB=4 configs it would most help --
// they read the weight once where MB=2 reads it twice -- lose the most by it. One more uint4v
// a column tile is four more VGPRs on a kernel already at 214, and a wave fewer per SIMD costs
// more latency cover than the prefetch buys. The same holds in r4d_gemm_w4a8_nt_m64.
// No next-group weight prefetch: four more VGPRs per column tile on a kernel at 214 costs a wave
// per SIMD, which loses more latency cover than the prefetch gains (likewise in w4a8_nt_m64).
for (int g = 0; g < groups; g++) {
// TWO FMAs, NOT A MUL-SUB-FMA. Written as fmaf(g_acc - z64*asum, sc, acc) the compiler emits
// v_mul + v_sub + v_fma_mix per accumulator element -- three instructions on top of the
// v_cvt, 238 VALU a group against 512 cycles of WMMA. Splitting the group's contribution
// into sc*g_acc and (-sc*z64)*asum makes both halves plain f32 FMAs into the same
// accumulator, which is one instruction less and a VOPD pair rather than a mixed-precision
// op. The scale is converted to f32 once a group here instead of riding into every
// v_fma_mix as an f16.
// Two f32 FMAs, sc*g_acc and (-sc*z64)*asum, rather than fmaf(g_acc - z64*asum, sc, acc),
// which compiles to v_mul + v_sub + v_fma_mix per element. This saves an instruction and
// pairs as VOPD; the scale is converted to f32 once per group.
float sc[NPW], scz[NPW];
#pragma unroll
for (int j = 0; j < NPW; j++) {
@@ -157,15 +137,14 @@ __global__ __launch_bounds__(R4D_W2A8_BOUNDS(MT, NPW)) void r4d_gemm_w2a8_nt_ker
const float nz = __half2float(__ushort_as_half((unsigned short)(sz >> 16)));
scz[j] = -sc[j] * (64.f * (-nz - 1024.f));
}
// Element e of tile i at this lane is output row (mt0+i)*16 + 8*(lane>>4) + e, so the eight
// rows a lane needs are contiguous at a fixed group -- which is why Asum is [G][Mpad].
// Element e of tile i at this lane is output row (mt0+i)*16 + 8*(lane>>4) + e, so a lane's
// eight rows are contiguous at a fixed group; hence Asum is [G][Mpad].
float asum[MT][8];
{
const size_t gi = (size_t)(kbase / R4D_GEMM_W2A8_GROUP + g) * Mpad;
#pragma unroll
for (int i = 0; i < MT; i++) {
// Clamped: MB past the number of row tiles is a legal config, and rows past M produce
// results the store discards -- but an unclamped read here is a page fault, not garbage.
// Clamped: MB may exceed the row tiles, and an unclamped read could fault.
int rb = (mt0 + i) * 16 + 8 * (lane >> 4);
rb = rb < Mpad - 8 ? rb : Mpad - 8;
const v4f* pv = (const v4f*)(Asum + gi + rb);
@@ -251,13 +230,9 @@ void r4d_gemm_w2a8_nt(long a, long ascale, long wq, long ws, long asum, long c,
if (K % (SK * R4D_GEMM_W2A8_GROUP))
throw std::runtime_error("r4d gemm_w2a8_nt: K must be divisible by SK*group");
if (!asum) throw std::runtime_error("r4d gemm_w2a8_nt: asum is required");
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_w2a8_nt: threads/block > 1024");
// __launch_bounds__ is a MAXIMUM BLOCK SIZE, not a hint: a block wider than the bound the
// instantiation was compiled with does not run slowly, it fails to launch -- and on this runtime
// that surfaces as `hipErrorLaunchFailure` from whatever unrelated call happens to synchronise
// next. So the launcher has to know the same rule the macro does.
const int bound = (MB >= 4) ? 256 : (((MB >= 2) || (NPW >= 4)) ? 512 : 1024);
if (WV * SK * WAVE32 > bound)
// __launch_bounds__ is a hard maximum: a wider block fails to launch, surfacing as
// hipErrorLaunchFailure at the next sync.
if (WV * SK * WAVE32 > R4D_W2A8_BOUNDS(MB, NPW))
throw std::runtime_error("r4d gemm_w2a8_nt: block wider than this MB/NPW's launch bound");
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_w2a8_nt: MB must be 1..4");
if (NPW != 1 && NPW != 2 && NPW != 4)
@@ -297,15 +272,14 @@ int r4d_gemm_w2a8_nt_group(void) { return R4D_GEMM_W2A8_GROUP; }
// ============================================================== 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_w2a8(const RadArgs* a, RadStream s) {
GemmOps g;
int rc = gemm_ops(a, &g, 1);
if (rc != RAD_OK) return rc;
if (g.N % 16) return R4D_NO(RAD_E_SHAPE);
if (!g.ascale) return R4D_NO(RAD_E_INVAL);
/* The 2-bit grid is asymmetric by construction, so the group sums are not optional. */
/* The 2-bit grid is always asymmetric, so the group sums are required. */
if (!g.asum) return R4D_NO(RAD_E_INVAL);
const int group = r4d_gemm_w2a8_nt_group();
Cfg c;