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

+60 -317
View File
@@ -1,70 +1,43 @@
// r4d_gemm_w4a8_nt_m64.hip - r4d_gemm_w4a8_nt_m64: 4-bit-weight, 8-bit-activation skinny GEMM for
// M up to 64, gfx1201. C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T with A quantised per row.
//
// WHY THIS EXISTS ALONGSIDE r4d_gemm_w4a16_nt_m64. The f16 kernel is at the DRAM roofline at M=8
// (620-648 GB/s) and there is nothing left to win there. At M=64 -- the drafter's batch at eight
// concurrent requests -- it is not near memory at all: 300-350 GB/s and 72-78 TF/s, against a
// v_wmma_f32_16x16x16_f16 issue ceiling of 207 TF/s, and removing the unpack entirely barely
// changes that. The wall is the f16 matrix instruction, and on this part it is the slow one:
// Complements r4d_gemm_w4a16_nt_m64, which is at the DRAM roofline at M=8 but capped by the f16
// matrix instruction at M=64 (72-78 TF/s against a 207 TF/s issue ceiling; removing the unpack
// barely helps). Every 8-bit form runs at twice the f16 rate:
//
// v_wmma_f32_16x16x16_f16 207 TF/s 5.95 cyc/instr
// v_wmma_f32_16x16x16_fp8_fp8 412 2.99
// v_wmma_i32_16x16x16_iu8 407 3.03
// v_wmma_i32_16x16x32_iu4 830 2.97
//
// So the widening that made the f16 kernel cheap is what caps it: every 8-bit form runs at twice
// the rate. This kernel takes the int8 one, which needs no exponent games at all.
//
// THE UNPACK IS THREE INSTRUCTIONS AND NEEDS NO ZERO POINT. Store the weight as a 4-bit TWO'S
// COMPLEMENT code and put the nibble in the HIGH half of its byte: for q in -8..7 the byte 16*q is
// exactly the signed int8 of 16*q, so
// The unpack is three instructions with no zero point. The weight is a 4-bit two's complement code
// placed in the high half of its byte: for q in -8..7 the byte 16*q is the signed int8 of 16*q:
//
// lo = (w << 4) & 0xF0F0F0F0 bytes <- nibbles 0,2,4,6 (elements 0..3)
// hi = w & 0xF0F0F0F0 bytes <- nibbles 1,3,5,7 (elements 4..7)
//
// is the whole dequant: two v_and_b32 and one v_lshlrev_b32 per eight weights, against eleven for
// the f16 route, and the factor of 16 folds into the group scale offline. A symmetric grid also
// deletes the zero point, which in an integer kernel would otherwise cost a per-group row-sum of A
// and a second correction term. A symmetric 4-bit grid is the best of the 4-bit options on
// quality anyway, so this costs nothing to pick.
// Two v_and_b32 and one v_lshlrev_b32 per eight weights (eleven on the f16 route); the factor 16 is
// removed at the store. A symmetric grid needs no per-group row sum of A for a zero point.
//
// The nibble ORDER is the one r4d_gemm_w4a16_nt_m64 already uses: its f16 unpack pairs nibble 2b
// into element b and nibble 2b+1 into element 4+b, which is exactly what the two masks above
// produce. One packed layout, two kernels.
// The weight is shared byte for byte with r4d_gemm_w4a16_nt_m64. Its f16 unpack pairs nibble 2b
// into element b and 2b+1 into element 4+b, the order the two masks above produce, and with a
// constant zero point 8 it computes the same n-8. Only the low half (the scale) of the
// (scale, -(1024+zero)) dword is read here. Small M stays on the f16 kernel, which needs no
// activation-quantiser launch.
//
// IT SHARES ITS WEIGHT WITH r4d_gemm_w4a16_nt_m64, BYTE FOR BYTE. The two kernels want the same
// nibbles in the same order, and a signed 4-bit code is what the f16 kernel computes anyway when
// its integer zero point is the constant 8: it builds 1024+n and adds -(1024+8), leaving n-8. So
// one quantised weight feeds both, and the scale array is the f16 kernel's (scale, -(1024+zero))
// dword with only the low half read here. The stored scale multiplies q-8 while the WMMA here
// sees 16*(q-8), so the factor of 1/16 rides along with the per-row activation scale at the store,
// where it costs nothing. That matters because the dispatch is per call: below M=16 the f16
// kernel is already at the DRAM roofline and there is nothing for an int8 one to win, while the
// activation quantiser costs a launch -- so the small-M path stays on f16 and the same bytes serve
// both.
// The activation is shuffled within each k step. Fragment element e of lane l is
// A[row][16s + 8*(e>>2) + 4*(l>>4) + (e&3)], two dwords eight bytes apart in a row-major A. The
// quantiser writes each step's sixteen bytes as [half 0's eight][half 1's eight], so a fragment is
// one global_load_b64 (32 loads per group instead of 64).
//
// THE ACTIVATION IS PRE-SHUFFLED TOO, BUT ONLY INSIDE A K STEP. A fragment element e of lane l is
// A[row][16s + 8*(e>>2) + 4*(l>>4) + (e&3)], so read out of a row-major A a lane's eight bytes are
// two dwords eight bytes apart: 64 global_load_b32 against 64 WMMA for one group, and 32
// s_wait_loadcnt with them. The activation layout is this library's to choose -- the quantiser
// writes it -- so it is written with the sixteen bytes of each k step reordered to
// [half 0's eight][half 1's eight]. A lane's fragment is then one global_load_b64 and the group
// costs 32 loads instead of 64.
// Do not lay A out as two planes ([2][M][K/2]): it allows global_load_b128 but is slower, because
// one instruction then spans two distant regions and touches twice the cache lines. The shuffle
// keeps a wave within the same sixteen rows.
//
// DO NOT LAY A OUT AS TWO PLANES. [2][M][K/2] makes a lane's four k steps contiguous and the
// group eight global_load_b128, and it is SLOWER than the shuffle below. Sixteen lanes of a wave
// then address one plane and sixteen the other, Mp*K/2 bytes away, so a single instruction spans
// two distant regions and doubles the cache lines it touches. The shuffle keeps every lane of a
// wave inside the same sixteen rows the row-major version touches, and only makes each lane's own
// eight bytes adjacent.
//
// THE ACTIVATION SCALE COMES OUT OF THE GROUP LOOP. Per-row symmetric quantisation makes the row
// scale a constant in n and in the group index, so sum_g s_g * <a_row, w_g> * ar = ar * sum_g ...:
// it is applied once at the store and never inside the k loop. What does stay in the loop is one
// v_cvt_f32_i32 per accumulator element per group, because group scales differ -- eight of them
// against eight WMMA. That argued for a WIDE group, and this kernel alone would take 256; the
// group is 128 because the f16 kernel at M=8 needs the split-K that a wider group takes away
// (`K % (SK*GROUP) == 0` halves the legal split count), and one artifact feeds every W4 kernel.
// The per-row activation scale is constant over n and groups, so it is applied once at the store.
// The loop keeps one v_cvt_f32_i32 per accumulator element per group. A wider group would suit this
// kernel alone; 128 keeps enough legal split-K counts for the f16 kernel at M=8 (one artifact
// feeds every W4 kernel).
#include "r4d.h"
#include "r4d_gemm_args.h"
#include "r4d_common.h"
@@ -74,13 +47,10 @@
#include <cstdint>
#include <stdexcept>
// M is not structural here -- `mt0 = blockIdx.y * MT` tiles it and the row index is clamped into
// 0..M-1 -- so this is a guard against a garbage shape, not the band the kernel is good at. It is
// set wide enough to reach a prefill chunk as well as the decode band, where at eight concurrent
// sequences with spec width 8 the target verifies 8 x 9 = 72 rows. Whether the tuned geometry
// above M = 64 is still the right one is a separate question and is NOT answered by this bound.
// M is not structural (grid.y tiles it, rows are clamped); this is a sanity bound wide enough for a
// prefill chunk and the decode band (e.g. 8 sequences x 9 verify rows = 72), not a tuning band.
#define R4D_GEMM_W4A8_MAX_M 8192
// R4D_GEMM_W4A8_GROUP comes from r4d_common.h -- see the note there.
// R4D_GEMM_W4A8_GROUP comes from r4d_common.h; see the note there.
#define R4D_GEMM_W4A8_KPB 64 // K per packed block: 4 k steps, 16 bytes per lane
#define WAVE32 32
@@ -90,91 +60,11 @@ typedef int v2i __attribute__((ext_vector_type(2)));
typedef unsigned uint4v __attribute__((ext_vector_type(4)));
typedef __attribute__((address_space(1))) const uint4v* r4d_gw4;
typedef __attribute__((address_space(1))) const unsigned* r4d_gu1;
typedef __attribute__((address_space(1))) const unsigned* r4d_gh1;
typedef __attribute__((address_space(1))) const unsigned short* r4d_gh2;
typedef unsigned uint2v __attribute__((ext_vector_type(2)));
typedef __attribute__((address_space(1))) const uint2v* r4d_ga2;
// A fragment out of the pre-permuted plane: eight contiguous bytes per k step, so a uint4 is two
// steps and the whole group is eight global_load_b128 per row tile.
#ifndef R4D_W4A8_APERM
#define R4D_W4A8_APERM 1 // 0 reads A row-major, for a harness with an unpermuted buffer
#endif
// THE SCALE DWORD IS HALF DEAD ON A SYMMETRIC GRID, AND AT THE DECODE BATCH THAT IS PURE COST.
// The shared (f16 scale, f16 of -(1024 + zero)) dword exists for r4d_gemm_w4a16_nt_m64, which
// builds its value as 1024 + n and needs the second half to subtract. A symz grid only ever
// writes the constant 8 there, and THIS kernel never reads it: the ISA shows one
// `global_load_b32` per group whose high sixteen bits are dropped by the `op_sel_hi` on the
// v_fma_mix that consumes it.
//
// At group 128 the scale array is 4 bytes per 16 columns per group against 1024 bytes of nibbles,
// so the dead half is 32 of every 1088 bytes -- 2.94% of everything the kernel reads. That is not
// an instruction count, it is DRAM traffic, and at M <= 16 this kernel runs at 97-98% of the part's
// 639 GB/s read ceiling, where traffic is the ONLY thing that sets the time. WS16 reads
// an f16-only scale plane instead. Sixteen lanes then take 32 contiguous bytes and consecutive
// groups are adjacent, so the 64-byte line is still fully used -- the halving is real and not an
// artefact of counting bytes the cache would have fetched anyway.
//
// The asymmetric form keeps the dword: its whole reason for existing is the zero point.
#ifndef R4D_GEMM_W4A8_WS16
#define R4D_GEMM_W4A8_WS16 0
#endif
// COST ABLATIONS. Wrong answers by construction; they exist to say what the kernel's time is made
// of. WS16 above deletes 2.94% of the bytes read and does not move the time, so byte counts alone
// do not explain it.
// 1 no scale load (sc = 1)
// 2 no activation load (the A fragment is a constant)
// 4 no weight load (the B fragment is a constant; leaves the A stream and the WMMA)
#ifndef R4D_W4A8_ABL
#define R4D_W4A8_ABL 0
#endif
// SOFTWARE-PIPELINE THE WEIGHT. The ablations above say the weight stream IS the kernel at a
// decode M: deleting the scale load or the activation loads barely moves the time, while deleting
// the weight load removes most of it, and that stream runs at 587 GB/s against the part's
// 639.6 GB/s ceiling. The loop as written issues its two `global_load_b128`, waits on them, then
// spends ~80 cycles in VALU and WMMA during which the wave issues no memory at all. PF holds the
// NEXT group's weight in registers so the load is issued a whole group ahead of the wait.
// Costs bpg*NPW*4 more VGPRs (16 instead of 8 at NPW=1, against 58 used).
#ifndef R4D_W4A8_PF
#define R4D_W4A8_PF 0
#endif
// STAGGER THE GROUP ORDER PER WAVE, so the block's waves stop marching in lockstep across the
// memory channels.
//
// Wave (col_local, ksplit) reads n-tile plane `nt0+col_local` starting at byte offset
// `ksplit * (Ks/KPB) * 512` and advances 512 bytes per k-block. Every plane pitch here is
// kpb*512 -- 40960 at K=5120, 69632 at 8704, 24576 at 3072 -- and every one of those is an exact
// multiple of 4096. A 256-bit GDDR6 part interleaves 16 channels at 256 B, so the address period
// that sweeps every channel is 4096 B, which means at time t EVERY wave in the grid is at the same
// address modulo 4096 and therefore aimed at the same channel. The block issues 20 concurrent
// streams and they all camp one channel group at a time.
//
// The fix is hipBLASLt's `StaggerU`: start each wave at a different group inside its OWN k-slice
// and wrap. The slice, the split-K partition and the bytes read are all unchanged -- only the
// order is -- so the only cost is that `pa` must be computed from g instead of incremented.
//
// NOTE this reorders an fp32 accumulation, so results move by ~1 ULP against a non-staggered build.
#ifndef R4D_W4A8_STAGGER
#define R4D_W4A8_STAGGER 0
#endif
// Under STAGGER the group loop no longer visits groups in order, so the activation pointer is
// indexed rather than bumped; without it the bump stays and this is zero.
#if R4D_W4A8_STAGGER
#if R4D_W4A8_APERM
#define AOFF(g) ((g) * (R4D_GEMM_W4A8_GROUP / 8))
#else
#define AOFF(g) ((g) * (R4D_GEMM_W4A8_GROUP / 4))
#endif
#else
#define AOFF(g) 0
#endif
// A fragments from the pre-permuted plane: eight contiguous bytes per k step.
__device__ __forceinline__ v2i fragA8(r4d_ga2 p, int u) {
const uint2v w = p[2 * u];
v2i r;
@@ -183,15 +73,7 @@ __device__ __forceinline__ v2i fragA8(r4d_ga2 p, int u) {
return r;
}
__device__ __forceinline__ v2i fragA8row(r4d_gu1 p, int u) {
v2i r;
r[0] = p[4 * u];
r[1] = p[4 * u + 2];
return r;
}
// The whole dequant: two ands and a shift, no zero point. It is a few percent of the kernel at
// M=64 and under a fifth of a percent at M=8, so there is nothing here worth a knob.
// The whole dequant: two ands and a shift.
__device__ __forceinline__ v2i dequant8(unsigned w) {
v2i r;
r[0] = (int)((w << 4) & 0xF0F0F0F0u);
@@ -199,31 +81,19 @@ __device__ __forceinline__ v2i dequant8(unsigned w) {
return r;
}
// BSIGNED=false reads the unpacked byte as 16*nibble (0..240) rather than 16*twos(nibble)
// (-128..112). That is what makes an ASYMMETRIC grid expressible: 16*nibble is affine in the
// nibble, so sum_k a_k (nibble_k - z) = sum_k a_k nibble_k - z sum_k a_k, whereas twos() is affine
// on each half of the range and not across it. The flag's meaning on gfx1201: a B byte of 0x81
// gives -2032 signed against 2064 unsigned, a ratio of -127/129 exactly.
// BSIGNED=false reads the unpacked byte as 16*nibble (0..240) instead of 16*twos(nibble)
// (-128..112), which makes an asymmetric grid expressible: 16*nibble is affine in the nibble, so
// sum_k a_k (nibble_k - z) = sum_k a_k nibble_k - z sum_k a_k.
template <bool BSIGNED>
__device__ __forceinline__ v8i mmai(v2i a, v2i b, v8i c) {
return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a, BSIGNED, b, c, false);
}
// __launch_bounds__ IS PER NPW, AND A FLAT 1024 COSTS THE WIDE ONES 192 REGISTERS.
// A block cannot exceed 1024 threads, but it also cannot exceed 64 KB of LDS, and the split-K
// buffer is WV*NPW*SK*1024 bytes -- so WV*NPW*SK <= 64 is structural and threads = 32*WV*SK is
// therefore capped at 32*64/NPW. Telling the compiler 1024 for every NPW makes it budget for 32
// waves on one workgroup, which on this part is 1536/8 = 192 VGPRs per wave, and NPW=4 and NPW=8
// then SPILL against that cap (up to 1875 bytes at MT=4, NPW=8). Those configs can never run more
// than 8 or 16 waves, so that cap is imaginary and the spill is not.
#ifndef R4D_GEMM_W4A8_LB
#define R4D_GEMM_W4A8_LB 1
#endif
#if R4D_GEMM_W4A8_LB
// __launch_bounds__ depends on NPW. The split-K buffer (WV*NPW*SK KiB) must fit 64 KiB of LDS, so
// threads = 32*WV*SK <= 32*64/NPW. A flat 1024 would budget 32 waves per workgroup and make NPW=4
// and NPW=8 spill, though they can never run more than 16 or 8 waves. The bound is a hard launch
// limit, so the host checks WV*SK against the same expression.
#define R4D_W4A8_BOUNDS(NPW) ((NPW) >= 8 ? 256 : ((NPW) >= 4 ? 512 : 1024))
#else
#define R4D_W4A8_BOUNDS(NPW) 1024
#endif
template <int MT, int NPW, bool NT, bool ASYM>
__global__ __launch_bounds__(R4D_W4A8_BOUNDS(NPW)) void r4d_gemm_w4a8_nt_m64_kernel(
@@ -247,138 +117,65 @@ __global__ __launch_bounds__(R4D_W4A8_BOUNDS(NPW)) void r4d_gemm_w4a8_nt_m64_ker
#pragma unroll
for (int i = 0; i < MT; i++) acc[j][i] = (v8f)(0.f);
// TWO THINGS THAT DO NOT WORK ON THE ASYMMETRIC CORRECTION'S ACTIVATION SUMS.
//
// Loading the sums just before the epilogue instead of at the top of the group, to cut
// asum[MT][8]'s 32-VGPR live range at MT=4, is SLOWER. At the top of the group the load is
// already hidden behind the WMMA block, and the register pressure costs less than exposing that
// latency does.
//
// Staging the sums in LDS is also SLOWER, at both ends of the M range. Every wave of the block
// re-reads a group's sums, and the staging can alias the split-K buffer for free since `red` is
// only touched in the store loop and that loop opens with a barrier -- but the staging prologue
// is (K/GROUP)*(MT*16) loads plus a __syncthreads() on a kernel that runs a few microseconds at a
// decode M, and the sums are not the latency problem.
// The asymmetric activation sums are loaded at the top of each group, hidden behind the WMMA
// block. Loading them later (shorter live range) or staging them in LDS were both slower.
if (n0 < N) {
// Rows clamped, never masked, as in the f16 kernel: a tile past M recomputes the last valid
// row and the store discards it. A is int8 so the row stride is K bytes, not 2K.
// Plane (lane >> 4), row clamped into 0..M-1, and the group's byte offset is K/2 per row.
#if R4D_W4A8_APERM
// Rows clamped into 0..M-1, never masked; the store discards rows past M. A is int8, so the
// row stride is K bytes.
r4d_ga2 pa[MT];
#else
r4d_gu1 pa[MT];
#endif
#pragma unroll
for (int i = 0; i < MT; i++) {
int r = (mt0 + i) * 16 + (lane & 15);
r = r < M - 1 ? r : M - 1;
#if R4D_W4A8_APERM
const size_t off = (size_t)r * K + kbase + 8 * (lane >> 4);
pa[i] = (r4d_ga2)(const uint2v*)(A + off);
#else
const size_t off = (size_t)r * K + kbase + 4 * (lane >> 4);
pa[i] = (r4d_gu1)(const unsigned*)(A + off);
#endif
}
const int kpb = K / R4D_GEMM_W4A8_KPB;
const int nsz = K / R4D_GEMM_W4A8_GROUP;
r4d_gw4 pw[NPW];
#if R4D_GEMM_W4A8_WS16
r4d_gh2 ps[NPW];
#else
r4d_gh1 ps[NPW];
#endif
const int ntlast = N / 16 - 1;
#pragma unroll
for (int j = 0; j < NPW; j++) {
const int t = (nt0 + j) < ntlast ? (nt0 + j) : ntlast;
pw[j] = (r4d_gw4)(const uint4v*)(Wq) +
(size_t)(t * kpb + kbase / R4D_GEMM_W4A8_KPB) * WAVE32 + lane;
#if R4D_GEMM_W4A8_WS16
ps[j] = (r4d_gh2)(const unsigned short*)(Ws) +
(size_t)(t * nsz + kbase / R4D_GEMM_W4A8_GROUP) * 16 + (lane & 15);
#else
ps[j] = (r4d_gh1)(const unsigned*)(Ws) +
(size_t)(t * nsz + kbase / R4D_GEMM_W4A8_GROUP) * 16 + (lane & 15);
#endif
}
const int bpg = R4D_GEMM_W4A8_GROUP / R4D_GEMM_W4A8_KPB;
#if R4D_W4A8_PF
uint4v nx[NPW][bpg];
#pragma unroll
for (int j = 0; j < NPW; j++)
#pragma unroll
for (int b = 0; b < bpg; b++)
nx[j][b] = NT ? __builtin_nontemporal_load(&pw[j][(size_t)b * WAVE32])
: pw[j][(size_t)b * WAVE32];
#endif
#if R4D_W4A8_STAGGER
const int gstag = groups > 1 ? (wave * R4D_W4A8_STAGGER) % groups : 0;
#endif
for (int gi = 0; gi < groups; gi++) {
#if R4D_W4A8_STAGGER
const int g = gi + gstag < groups ? gi + gstag : gi + gstag - groups;
#else
const int g = gi;
#endif
for (int g = 0; g < groups; g++) {
float sc[NPW];
#pragma unroll
for (int j = 0; j < NPW; j++)
#if R4D_W4A8_ABL & 1
sc[j] = 1.f;
#elif R4D_GEMM_W4A8_WS16
sc[j] = __half2float(__ushort_as_half(ps[j][(size_t)g * 16]));
#else
sc[j] = __half2float(__ushort_as_half(
(unsigned short)(ps[j][(size_t)g * 16] & 0xFFFFu)));
#endif
// The zero point lives in the SAME dword's high half, as the f16 of -(1024 + zero) -- the
// encoding the f16 kernel already needs, and exact because f16 has exactly 1024 steps between
// 1024 and 2048. The symz grid only ever writes the constant 8 there.
// The zero point is the dword's high half, the f16 of -(1024 + zero) (exact: f16 has unit
// steps between 1024 and 2048). The symmetric grid always stores 8 there.
float z16[NPW];
float asum[MT][8];
if (ASYM) {
#if R4D_W4A8_ASUM_ABLATE
// Cost ablation only -- same arithmetic, no global loads. Wrong answers by construction;
// it exists to say whether the asymmetric path costs its FMAs or its memory.
#pragma unroll
for (int j = 0; j < NPW; j++) z16[j] = 16.f;
#pragma unroll
for (int i = 0; i < MT; i++)
#pragma unroll
for (int e = 0; e < 8; e++) asum[i][e] = 1.f;
#else
#pragma unroll
for (int j = 0; j < NPW; j++) {
const float nz = __half2float(__ushort_as_half(
(unsigned short)(ps[j][(size_t)g * 16] >> 16)));
z16[j] = 16.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] and
// not [M][G].
//
// TWO v4f LOADS, NOT EIGHT SCALARS. These loads, and not the correction's arithmetic, are
// most of what an asymmetric grid costs over a symmetric one.
// They vectorise only if the base is 32-byte aligned, which is why the row stride is padded
// to a multiple of 16 (also making the last tile's rb+7 in bounds, since rb reaches
// (mtiles-1)*16 + 8).
// 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]. Two v4f loads need a
// 32-byte aligned base, which Mpad (a multiple of 16) provides; it also keeps the last
// tile's rb+7 in bounds. These loads are most of the asymmetric grid's extra cost.
const int Mpad = ((M + 15) / 16) * 16;
const size_t gi = (size_t)(kbase / R4D_GEMM_W4A8_GROUP + g) * Mpad;
#pragma unroll
for (int i = 0; i < MT; i++) {
// CLAMPED, and it has to be. When MB exceeds the number of M tiles -- MB=4 with M=9 is a
// legal config the tuner will pick -- this loop still runs MT tiles, and rb reaches
// (MT-1)*16+8 = 56 against an Mpad of 16. The symz path survives that because its A
// pointer clamps and the store discards rows past M; an unclamped Asum read is a page
// fault. Rows past M produce garbage that is discarded, so clamping the base is enough,
// and it stays a multiple of 8 so the v4f loads stay aligned.
// Clamped: MB may exceed the number of M tiles (MB=4 with M=9 is legal), and an unclamped
// Asum read could fault. The clamped base stays a multiple of 8, keeping v4f alignment.
int rb = (mt0 + i) * 16 + 8 * (lane >> 4);
rb = rb < Mpad - 8 ? rb : Mpad - 8;
const v4f* pv = (const v4f*)(Asum + gi + rb);
@@ -389,57 +186,23 @@ __global__ __launch_bounds__(R4D_W4A8_BOUNDS(NPW)) void r4d_gemm_w4a8_nt_m64_ker
asum[i][e + 4] = v1[e];
}
}
#endif
}
v8i g_acc[NPW][MT];
#if R4D_W4A8_PF
// Take this group's weight out of the registers the last iteration filled, and issue the
// next group's loads BEFORE any of it is consumed, so the wait sits a full group away.
uint4v cw[NPW][bpg];
#pragma unroll
for (int j = 0; j < NPW; j++)
#pragma unroll
for (int b = 0; b < bpg; b++) cw[j][b] = nx[j][b];
if (g + 1 < groups) {
#pragma unroll
for (int j = 0; j < NPW; j++)
#pragma unroll
for (int b = 0; b < bpg; b++) {
const size_t o = (size_t)((g + 1) * bpg + b) * WAVE32;
nx[j][b] = NT ? __builtin_nontemporal_load(&pw[j][o]) : pw[j][o];
}
}
#endif
#pragma unroll
for (int b = 0; b < bpg; b++) {
uint4v wv[NPW];
#pragma unroll
for (int j = 0; j < NPW; j++) {
#if R4D_W4A8_PF
wv[j] = cw[j][b];
#else
const size_t o = (size_t)(g * bpg + b) * WAVE32;
#if R4D_W4A8_ABL & 4
(void)o;
wv[j] = (uint4v)(0x21212121u);
#else
wv[j] = NT ? __builtin_nontemporal_load(&pw[j][o]) : pw[j][o];
#endif
#endif
}
#pragma unroll
for (int s = 0; s < 4; s++) {
const int u = b * 4 + s;
v2i afrag[MT];
#pragma unroll
#if R4D_W4A8_ABL & 2
for (int i = 0; i < MT; i++) { afrag[i][0] = 0x01010101; afrag[i][1] = 0x01010101; }
#elif R4D_W4A8_APERM
for (int i = 0; i < MT; i++) afrag[i] = fragA8(pa[i] + AOFF(g), u);
#else
for (int i = 0; i < MT; i++) afrag[i] = fragA8row(pa[i] + AOFF(g), u);
#endif
for (int i = 0; i < MT; i++) afrag[i] = fragA8(pa[i], u);
#pragma unroll
for (int j = 0; j < NPW; j++) {
const v2i bfrag = dequant8(wv[j][s]);
@@ -460,20 +223,12 @@ __global__ __launch_bounds__(R4D_W4A8_BOUNDS(NPW)) void r4d_gemm_w4a8_nt_m64_ker
if (ASYM) t -= z16[j] * asum[i][e];
acc[j][i][e] = fmaf(t, sc[j], acc[j][i][e]);
}
#if !R4D_W4A8_STAGGER
#pragma unroll
#if R4D_W4A8_APERM
for (int i = 0; i < MT; i++) pa[i] += R4D_GEMM_W4A8_GROUP / 8;
#else
for (int i = 0; i < MT; i++) pa[i] += R4D_GEMM_W4A8_GROUP / 4;
#endif
#endif
}
}
// Split-K reduction and store, one row tile at a time. The per-row activation scale is applied
// here and only here: it is constant over n and over the group index, so it never entered the
// k loop.
// Split-K reduction and store, one row tile at a time, applying the per-row activation scale.
const int ncols = WV * NPW;
const int nthreads = WV * SK * WAVE32;
#pragma unroll
@@ -493,7 +248,7 @@ __global__ __launch_bounds__(R4D_W4A8_BOUNDS(NPW)) void r4d_gemm_w4a8_nt_m64_ker
const int l = off & 31;
const int n = (blockIdx.x * ncols + col) * 16 + (l & 15);
const int m = (mt0 + i) * 16 + 8 * (l >> 4) + (off >> 5);
// 0.0625 is the 1/16 the unpack introduced by putting the nibble in the byte's high half.
// 0.0625 undoes the 16 from placing the nibble in the byte's high half.
if (n < N && m < M) C[(size_t)m * N + n] = (__hip_bfloat16)(v * Ascale[m] * 0.0625f);
}
}
@@ -507,7 +262,8 @@ static void r4d_gemm_w4a8_impl(long a, long ascale, long wq, long ws, long asum,
if (N % 16) throw std::runtime_error("r4d gemm_w4a8_nt_m64: N must be a multiple of 16");
if (K % (SK * R4D_GEMM_W4A8_GROUP))
throw std::runtime_error("r4d gemm_w4a8_nt_m64: K must be divisible by SK*group");
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_w4a8_nt_m64: threads/block > 1024");
if (WV * SK * WAVE32 > R4D_W4A8_BOUNDS(NPW))
throw std::runtime_error("r4d gemm_w4a8_nt_m64: WV*SK*32 exceeds this tile's launch bound");
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_w4a8_nt_m64: MB must be 1..4");
if (NPW != 1 && NPW != 2 && NPW != 4 && NPW != 8)
throw std::runtime_error("r4d gemm_w4a8_nt_m64: NPW must be 1, 2, 4 or 8");
@@ -534,8 +290,9 @@ static void r4d_gemm_w4a8_impl(long a, long ascale, long wq, long ws, long asum,
#define LAUNCH_NPW(TT, AS) switch (NPW) { \
case 1: LAUNCH_MB(1, TT, AS); break; \
case 2: LAUNCH_MB(2, TT, AS); break; \
case 4: LAUNCH_MB(4, TT, AS); break; \
case 8: LAUNCH_MB(8, TT, AS); break; \
default: LAUNCH_MB(4, TT, AS); break; }
default: throw std::runtime_error("r4d gemm_w4a8_nt_m64: NPW must be 1, 2, 4 or 8"); }
if (asym) {
if (NT) { LAUNCH_NPW(true, true); } else { LAUNCH_NPW(false, true); }
} else {
@@ -551,39 +308,25 @@ void r4d_gemm_w4a8_nt_m64(long a, long ascale, long wq, long ws, long c, int M,
r4d_gemm_w4a8_impl(a, ascale, wq, ws, 0, c, M, K, N, WV, SK, MB, NPW, NT, false, stream);
}
// Asymmetric grid: the weight nibble is read UNSIGNED and the stored zero point is corrected out
// with a per-row per-group sum of the activation, which r4d_quant_act_i8_asum emits. `asum` is
// [K/group][Mpad] float with Mpad = ceil(M/16)*16 -- group-major, because a lane's eight
// accumulator elements are eight CONTIGUOUS output rows at one group, and padded so that base is
// 32-byte aligned for a v4f load and the last tile stays in bounds. Allocate it ZEROED.
// Asymmetric grid: the nibble is read unsigned and the zero point is corrected with the per-row
// per-group activation sums from r4d_quant_act_i8_asum. `asum` is [K/group][Mpad] float,
// Mpad = ceil(M/16)*16 (see the kernel). Allocate it zeroed.
void r4d_gemm_w4a8_asym_nt_m64(long a, long ascale, long wq, long ws, long asum, long c,
int M, int K, int N, int WV, int SK, int MB, int NPW, int NT,
long stream) {
if (!asum) throw std::runtime_error("r4d gemm_w4a8_asym_nt_m64: asum is required");
#if R4D_GEMM_W4A8_WS16
// The zero point is the only thing in the dword's high half, and WS16 is the build that does not
// store it. The two are mutually exclusive by construction, so say so instead of reading zero.
throw std::runtime_error("r4d gemm_w4a8_asym_nt_m64: built with WS16, which has no zero point");
#endif
r4d_gemm_w4a8_impl(a, ascale, wq, ws, asum, c, M, K, N, WV, SK, MB, NPW, NT, true, stream);
}
int r4d_gemm_w4a8_nt_m64_max_m(void) { return R4D_GEMM_W4A8_MAX_M; }
int r4d_gemm_w4a8_nt_m64_group(void) { return R4D_GEMM_W4A8_GROUP; }
int r4d_gemm_w4a8_nt_m64_aperm(void) { return R4D_W4A8_APERM; }
int r4d_gemm_w4a8_nt_m64_ws16(void) { return R4D_GEMM_W4A8_WS16; }
// ============================================================== 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.
/* ------------------------------------------------------------------ int8-activation grids */
/* The asymmetric grid is a different WEIGHT FORMAT, not a different call: the nibble is read
* UNSIGNED and the stored per-group zero is corrected out with the activation's per-group row
* sums. So it is selected by `dtype`, which is what the weight file was written for, and never by
* whether a_sum happens to be present -- guessing from an operand would make a caller that forgot
* to pass the sums silently compute the symmetric answer against asymmetric bytes. */
/* The asymmetric grid is selected by `dtype`, not by the presence of a_sum (see is_asym). */
extern "C" int r4d_rad_gemm_w4a8_m64(const RadArgs* a, RadStream s) {
GemmOps g;