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

+59 -118
View File
@@ -1,34 +1,22 @@
// Per-(row, 128-column) symmetric E4M3 quantisation of a bf16 activation -- the gate on the fp8a8
// GEMMs. `quantization_config.activation_scheme` on the target checkpoint is "dynamic", which means
// the file ships no activation scale and one is computed every step; and a GEMM cannot compute its
// own without a reduction across every M block, so it does not try.
// Per-(row, 128-column) symmetric E4M3 quantisation of a bf16 activation, the input to the fp8a8
// GEMMs. The checkpoint's activation_scheme is "dynamic" (no stored activation scale), and a GEMM
// cannot compute one without a reduction across every M block.
//
// UNLIKE r4d_quant_act_i8, THE OUTPUT IS PLAIN ROW-MAJOR. That kernel writes the byte order
// gemm_w4a8_nt_m64's A fragment reads, because that GEMM loads A straight to registers. The fp8a8
// GEMMs stage BOTH operands through the same `stage8` byte copy and read both through the same XOR
// swizzle, so the permutation would have to be undone. Row-major is also what makes the activation
// a legal B operand of the narrow instantiation, where it is the sixteen-wide side.
// Output is plain row-major, unlike r4d_quant_act_i8 (which writes gemm_w4a8_nt_m64's register
// fragment order): the fp8a8 GEMMs stage both operands through the same `stage8` copy and fragment
// read, and row-major lets the activation be the B operand of the narrow instantiation.
//
// THE GROUP IS 128 AND IS NOT A BUILD FLAG. It is the weight scale's block width -- a property of
// the checkpoint on disk, not a choice this library's quantiser makes, unlike R4D_GEMM_W4A8_GROUP.
// A different group would make the GEMM's fold read the wrong scale for part of a block.
// The group is fixed at 128, the checkpoint's weight-scale block width (unlike
// R4D_GEMM_W4A8_GROUP); another group would make the GEMM's fold read the wrong scale. No LDS: the
// input is already row-major per token.
//
// NO LDS. Every activation in this library is already row-major per token, the layout the codes
// are written in, so there is no transpose to do and staging through LDS would buy nothing.
//
// THE THREE THINGS THAT ARE NOT OBVIOUS:
//
// 448 is E4M3's largest finite value, and the scale is `amax / 448` -- a DEQUANT multiplier, the
// same sense as the checkpoint's `weight_scale_inv`, so the GEMM's fold multiplies both scales
// and never divides.
//
// A ZERO BLOCK gets a scale of 1 and quantises to zeros, because `amax / 448` would be zero and
// its reciprocal infinite.
//
// THE CLAMP IS NOT DEFENSIVE. `amax * (1/scale)` is 448 in exact arithmetic but can land an ulp
// above it in float, and v_cvt_pk_fp8_f32 answers an out-of-range input with NaN rather than
// saturating -- so without the clamp EVERY block containing its own maximum produces a NaN. One
// missing fminf is a whole-model failure.
// Not obvious:
// - The scale is `amax / 448` (E4M3's largest finite value), a dequant multiplier like the
// checkpoint's `weight_scale_inv`, so the GEMM's fold only multiplies.
// - A zero block gets scale 1 and quantises to zeros.
// - The clamp is required: `amax * (1/scale)` can land an ulp above 448, and v_cvt_pk_fp8_f32
// returns NaN for out-of-range input instead of saturating, so every block's maximum would
// become NaN.
//
// Returns a negative code rather than throwing, for the reason given in r4d_gemm_fp8a8.hip.
@@ -45,29 +33,16 @@ constexpr uint32_t kThreads = 256;
constexpr uint32_t kWaves = kThreads / kWave;
constexpr uint32_t kGroup = 128;
__device__ __forceinline__ float wave_max(float v) {
v = r4d_wave_max(v);
return v;
}
// One block a row; a wave owns one 128-column group, a lane four consecutive columns of it. Four is
// what makes the store a single dword and the wave's store 128 contiguous bytes, and it is also
// exactly the two v_cvt_pk_fp8_f32 the packing needs.
// One block per row; a wave owns one 128-column group, a lane four consecutive columns (one dword
// store, 128 contiguous bytes per wave, two v_cvt_pk_fp8_f32).
//
// ROT MAKES IT THE ROTATED QUANTISER AND NEEDS NO EXTRA MACHINERY, because that mapping -- one
// wave a 128-wide group, four consecutive values a lane -- is exactly the one r4d_fwht_wave<128> wants
// (VPL = 128/32 = 4). The transform runs in registers between the load and the absmax, with no
// LDS and no barrier, and the absmax then sees the ROTATED values -- which is the entire point,
// since the whole benefit of a rotation is that the maximum the grid is scaled by gets smaller.
// ROT applies r4d_fwht_wave<128> in registers between the load and the absmax; the mapping (four
// values per lane) is the one it expects, so no LDS or barrier is needed, and the absmax sees the
// rotated values. The sqrt(128) factor of the unnormalised transform is not removed here:
// <Hw, Ha> = 128 <w, a>, and r4d_layout.cpp folds that into the stored weight scale at convert.
//
// NOTHING NORMALISES sqrt(128) HERE. The unnormalised transform gives sqrt(128) times the
// orthonormal values, the scale is amax/448 over exactly those, and <Hw, Ha> = 128 <w, a> -- so
// the whole residual factor is one number, and r4d_layout.cpp folds it into the STORED WEIGHT
// SCALE at convert, where it is an exponent shift and costs nothing. A pass here to divide by
// sqrt(128) would be a second touch of every element for a constant the other side carries free.
// I8 IS THE SAME QUANTISER AT INT8 CODES (quant_act_i8g, had_quant_act_i8g): the scale is amax/127
// and a code is the value over it rounded to nearest-even, -127..127, one byte -- for the int8 trunk
// GEMMs, which read the same plain row-major bytes and per-(row, 128) scales the fp8 ones do.
// I8 is the same quantiser at int8 codes (quant_act_i8g, had_quant_act_i8g): scale amax/127,
// round-to-nearest-even into -127..127, same row-major layout and per-(row, 128) scales.
template <bool ROT, bool I8 = false>
__global__ __launch_bounds__(kThreads) void quant_act_fp8_kernel(
const uint16_t* __restrict__ x, uint8_t* __restrict__ q, float* __restrict__ s,
@@ -86,13 +61,12 @@ __global__ __launch_bounds__(kThreads) void quant_act_fp8_kernel(
const uint32_t c = c0 + (uint32_t)j;
v[j] = c < K ? __uint_as_float((uint32_t)x[xrow + c] << 16) : 0.0f;
}
// The whole wave has to be inside the row for this, which is why the rotated launcher
// refuses a K that is not a multiple of the group: a partial block would rotate real
// values against padding zeros, and the weight side has no matching block to undo it.
// The rotated launcher requires K % 128 == 0: a partial block would rotate real values
// with padding zeros, and the weight has no matching block.
if constexpr (ROT) r4d_fwht_wave<(int)kGroup>(v, (int)lane);
#pragma unroll
for (int j = 0; j < 4; ++j) m = fmaxf(m, fabsf(v[j]));
m = wave_max(m);
m = r4d_wave_max(m);
constexpr float kTop = I8 ? 127.0f : 448.0f;
const float sc = m > 0.0f ? m * (1.0f / kTop) : 1.0f;
if (lane == 0) s[(size_t)row * s_ld + kb] = sc;
@@ -114,10 +88,8 @@ __global__ __launch_bounds__(kThreads) void quant_act_fp8_kernel(
fminf(fmaxf(v[3] * inv, -448.0f), 448.0f), pk,
true);
}
// A ragged last group is written zero-padded rather than partially: the padding lanes hold
// zeros, which quantise to zeros, and the GEMM's own bounds guard is what stops them
// reaching an accumulator. A partial store would be a second code path for a case no dense
// tensor in the target has -- but the caller must own K rounded up to 4 bytes of `q`.
// A ragged last group is written zero-padded; the GEMM's bounds guard keeps the padding out
// of the accumulators. The caller must own K rounded up to 4 bytes of `q`.
if (c0 < K) *(uint32_t*)(q + qrow + c0) = (uint32_t)pk;
}
}
@@ -130,12 +102,9 @@ extern "C" int r4d_quant_act_fp8_group(void) { return (int)kGroup; }
// q [M][K] E4M3, `q_ld` bytes a row (0 = K), rounded up to 4
// s [M][ceil(K/128)] f32, `s_ld` elements a row (0 = ceil(K/128))
//
// `rot` runs the 128-wide Hadamard first. It is a STRICTER call, not a flag on the same one: a
// ragged last group is legal unrotated (it is written zero-padded and the GEMM's bounds guard
// stops the padding) and is not legal rotated, because a block that mixes real values with
// padding has no matching weight block on the other side.
// `rot` runs the 128-wide Hadamard first and additionally requires K % 128 == 0 (see the kernel).
static int quant_act_fp8_launch(long a, long q, long s, int M, int K,
int x_ld, int q_ld, int s_ld, const R4dSink& sk, bool rot,
int x_ld, int q_ld, int s_ld, RadStream stream, bool rot,
bool i8 = false) {
int emit_rc = RAD_OK;
if (!a || !q || !s) return -1;
@@ -147,103 +116,75 @@ static int quant_act_fp8_launch(long a, long q, long s, int M, int K,
const int nkb = (K + (int)kGroup - 1) / (int)kGroup;
if (!s_ld) s_ld = nkb;
#define R4D_QA(ROT_, I8_) \
R4D_EMIT(sk, (quant_act_fp8_kernel<ROT_, I8_>), dim3((uint32_t)M), dim3(kThreads), 0, \
R4D_EMIT(stream, (quant_act_fp8_kernel<ROT_, I8_>), dim3((uint32_t)M), dim3(kThreads), 0, \
(const uint16_t*)a, (uint8_t*)q, (float*)s, (uint32_t)K, (uint32_t)nkb, \
(uint32_t)x_ld, (uint32_t)q_ld, (uint32_t)s_ld)
if (i8)
emit_rc = rot ? R4D_QA(true, true) : R4D_QA(false, true);
else if (rot)
emit_rc = R4D_EMIT(sk, quant_act_fp8_kernel<true>, dim3((uint32_t)M), dim3(kThreads), 0,
(const uint16_t*)a, (uint8_t*)q, (float*)s,
(uint32_t)K, (uint32_t)nkb, (uint32_t)x_ld, (uint32_t)q_ld,
(uint32_t)s_ld);
else
emit_rc = R4D_EMIT(sk, quant_act_fp8_kernel<false>, dim3((uint32_t)M), dim3(kThreads), 0,
(const uint16_t*)a, (uint8_t*)q, (float*)s,
(uint32_t)K, (uint32_t)nkb, (uint32_t)x_ld, (uint32_t)q_ld,
(uint32_t)s_ld);
if (i8) emit_rc = rot ? R4D_QA(true, true) : R4D_QA(false, true);
else emit_rc = rot ? R4D_QA(true, false) : R4D_QA(false, false);
#undef R4D_QA
if (emit_rc != RAD_OK) return -4;
return (!sk.desc && hipGetLastError() != hipSuccess) ? -4 : 0;
return (hipGetLastError() != hipSuccess) ? -4 : 0;
}
extern "C" int r4d_quant_act_fp8(long a, long q, long s, int M, int K,
int x_ld, int q_ld, int s_ld, long stream) {
return quant_act_fp8_launch(a, q, s, M, K, x_ld, q_ld, s_ld,
r4d_stream_sink((RadStream)(uintptr_t)stream), false);
(RadStream)(uintptr_t)stream, false);
}
extern "C" int r4d_had_quant_act_fp8(long a, long q, long s, int M, int K,
int x_ld, int q_ld, int s_ld, long stream) {
return quant_act_fp8_launch(a, q, s, M, K, x_ld, q_ld, s_ld,
r4d_stream_sink((RadStream)(uintptr_t)stream), true);
(RadStream)(uintptr_t)stream, true);
}
// ============================================================== the plugin ABI entry points
//
// The shared operand reader is r4d_fp8_args.h. See libr4d/r4d_args.h for why there is no adapter
// directory between the core and these launches.
// The shared operand reader is r4d_fp8_args.h; see libr4d/r4d_args.h.
/* ================================================================== the fp8 activation quantiser */
static int rad_quant_act_fp8(const RadArgs* a, const R4dSink& sk, bool rot, bool i8 = false) {
static int rad_quant_act_fp8(const RadArgs* a, RadStream stream, bool rot, bool i8 = false) {
const RadTensor* x = r4d_opt(a, QF_X);
const RadTensor* q = r4d_opt(a, QF_Q);
const RadTensor* sc = r4d_opt(a, QF_S);
if (!x || !q || !sc) return R4D_NO(RAD_E_INVAL);
/* ROWS ARE DIM 0 AND EVERYTHING AFTER IT IS THE ROW, which is the same reading r4d_row_pitch
* already takes -- it folds every trailing extent into one pitch and returns stride[0]. So a
* rank-3 activation is accepted and is one row per token: a GDN block's out-projection input is
* declared [tokens, heads, width] because libr4d's scan kernels require that rank, and its K is
* heads*width. Flattening it here rather than making the caller declare a second buffer is the
* only way to quantise it at all, and reading it as one row per HEAD would put a scale on a
* grid the consuming GEMM does not read.
*
* `q` and `sc` stay rank 2 because this kernel WRITES them and they exist for the GEMM. */
/* Rows are dim 0 and everything after is the row, as r4d_row_pitch reads it. A GDN block's
* out-projection input is [tokens, heads, width] (the scan kernels require that rank) and its
* K is heads*width; one row per head would put scales on a grid the GEMM does not read.
* `q` and `sc` are written for the GEMM and stay rank 2. */
if (x->rank < 2 || q->rank != 2 || sc->rank != 2) return R4D_NO(RAD_E_SHAPE);
const long long M = x->shape[0], K = r4d_numel(x) / (x->shape[0] > 0 ? x->shape[0] : 1);
const long long grp = r4d_quant_act_fp8_group();
const long long nkb = (K + grp - 1) / grp;
/* The group is a property of the checkpoint's scale grid, not a knob. A geometry that declares
* a different one is asking for a format the GEMM's fold does not read, and the answer is a
* refusal by name rather than a silent 128. */
/* The group is fixed by the checkpoint's scale grid; a different declared group is refused. */
if (r4d_geti_or(a, "group", grp) != grp) return R4D_NO(RAD_E_SHAPE);
if (q->shape[0] != M || q->shape[1] != K) return R4D_NO(RAD_E_SHAPE);
if (sc->shape[0] != M || sc->shape[1] != nkb) return R4D_NO(RAD_E_SHAPE);
const long long xp = r4d_row_pitch(x), qp = r4d_row_pitch(q), sp = r4d_row_pitch(sc);
if (xp < 0 || qp < 0 || sp < 0) return R4D_NO(RAD_E_STRIDE);
const int e = quant_act_fp8_launch(r4d_addr(x), r4d_addr(q), r4d_addr(sc), (int)M, (int)K,
(int)xp, (int)qp, (int)sp, sk, rot, i8);
(int)xp, (int)qp, (int)sp, stream, rot, i8);
return e == 0 ? RAD_OK : RAD_E_SHAPE;
}
static int rad_quant_act_fp8_body(const RadArgs* a, const R4dSink& sk) {
return rad_quant_act_fp8(a, sk, false);
extern "C" int r4d_rad_quant_act_fp8(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, false);
}
R4D_ENTRY(r4d_rad_quant_act_fp8, rad_quant_act_fp8_body)
/* THE ROTATED FORM, and it is a DIFFERENT OP rather than a parameter on that one, for the reason
* quant_act_i8 and had_quant_act_i8 are two ops: the number it writes is not the same number, so
* a graph in which one was substituted for the other would be a different model that still ran.
* The op name is what says which, and it is checked against the weight's layout tag by the pair
* being declared together in arch/common/rad_block_moe_fp8.h -- nothing here can see the weight.
*
* It pairs with the `w4a8h` expert plane and with NOTHING ELSE: any consumer of this activation
* that was not rotated at convert reads it as noise. */
static int rad_had_quant_act_fp8_body(const RadArgs* a, const R4dSink& sk) {
return rad_quant_act_fp8(a, sk, true);
/* The rotated form is a separate op, not a parameter: it writes different numbers, so a swapped
* graph would still run as a different model. The op name pairs with the weight's layout tag in
* arch/common/rad_block_moe_fp8.h. It pairs only with the `w4a8h` expert plane; any consumer not
* rotated at convert reads it as noise. */
extern "C" int r4d_rad_had_quant_act_fp8(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, true);
}
R4D_ENTRY(r4d_rad_had_quant_act_fp8, rad_had_quant_act_fp8_body)
/* THE INT8 PAIR: the same rows at int8 codes, for the int8 trunk GEMMs (gemm_i8a8_*). Ops of their
* own for the reason the rotated form is one: they write other numbers into a buffer of the same
* shape, and the name is what says which. */
static int rad_quant_act_i8g_body(const RadArgs* a, const R4dSink& sk) {
return rad_quant_act_fp8(a, sk, false, true);
/* The int8 pair, for the int8 trunk GEMMs (gemm_i8a8_*). Separate ops for the same reason as the
* rotated form: different numbers in a buffer of the same shape. */
extern "C" int r4d_rad_quant_act_i8g(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, false, true);
}
R4D_ENTRY(r4d_rad_quant_act_i8g, rad_quant_act_i8g_body)
static int rad_had_quant_act_i8g_body(const RadArgs* a, const R4dSink& sk) {
return rad_quant_act_fp8(a, sk, true, true);
extern "C" int r4d_rad_had_quant_act_i8g(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, true, true);
}
R4D_ENTRY(r4d_rad_had_quant_act_i8g, rad_had_quant_act_i8g_body)