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

+157 -253
View File
@@ -1,86 +1,64 @@
// r4d_rowtopk_bf16.hip - r4d_rowtopk_bf16: exact per-row top-R of a wide bf16 matrix.
//
// WHAT IT IS FOR. A quantised draft head produces coarse logits over a whole vocabulary shard and
// only the best few dozen of them are ever rescored exactly. Selecting those is not a GEMM
// problem, but doing it with a general top-k costs more than the GEMM saves: torch.topk over a
// 15520-wide candidate array measures 132 us at M=64 in three kernels (digit counts, within-K
// counts, gather), and over a full 124160-wide row it is several times that.
// Purpose: a quantised draft head produces coarse logits over a vocabulary shard, and only the
// best few dozen are rescored exactly. A general top-k costs more than that saves: torch.topk over
// a 15520-wide array takes 132 us at M=64 in three kernels, and several times that over a full
// 124160-wide row.
//
// THE SHAPE IS TWO STAGES BECAUSE THE SCAN AND THE SELECTION WANT OPPOSITE THINGS. Reading the
// matrix wants thousands of waves in flight; the selection is a chain of dependent cross-lane
// steps, ~300 cycles a round, and running that chain thousands of times costs more than the read.
// Two simpler shapes are both far off that on a 15.9 MB matrix whose roofline is 25 us:
// Two stages, because scanning and selecting want opposite shapes. Reading the matrix wants
// thousands of waves in flight; selection is a chain of dependent cross-lane steps (~300 cycles a
// round), too costly to run thousands of times. On a 15.9 MB matrix with a 25 us roofline:
//
// 256-thread blocks, R=32 rounds, two barriers a round 75.6 us (chain, plus barriers)
// one wave per chunk, R=32 rounds, no barrier 68.5 us (chain, or too few waves)
//
// Neither is bandwidth-bound. So stage 1 runs MANY waves and a SHORT chain -- one wave per (row,
// chunk), keeping only the chunk's top-RC -- and stage 2 runs one wave per row over the nch*RC
// survivors with the full R-round chain, where there are only M chains in the whole launch.
// So stage 1 runs many waves with a short chain (one wave per (row, chunk), keeping the chunk's
// top-RC), and stage 2 runs one wave per row over the nch*RC survivors with the full R-round chain.
//
// * A LANE KEEPS ITS OWN TOP-PT IN REGISTERS, SORTED. Insertion is guarded by a single compare
// against the tail, so the common element costs one instruction; only an element that actually
// places pays the branch-free shift. There is no LDS array of candidates.
// * A ROUND ONLY EVER REMOVES A LIST HEAD. The round winner is the head of exactly one lane's
// sorted list, so "remove it" is that lane shifting its own registers. A mask-and-rescan over
// a shared candidate array -- the obvious form -- re-reads every candidate each round; this
// reads none.
// * ONE WAVE PER BLOCK, so a round is five xor-butterfly steps and NO barrier.
// * A lane keeps its own top-PT in registers, sorted. Insertion is guarded by one compare
// against the tail, so most elements cost one instruction; only a placing element pays the
// branch-free shift. No LDS candidate array.
// * A round only removes a list head: the winner is the head of exactly one lane's list, so that
// lane shifts its own registers. Nothing is re-read per round.
// * One wave per block, so a round is five xor-butterfly steps and no barrier.
//
// THE KEY IS ONE 64-BIT INTEGER, which is what makes a round a plain max. The high 32 bits are the
// float's order-preserving image (invert everything if negative, set the sign bit if not), the low
// 32 are ~index, so a larger key means a larger value and, on an exact tie, the LOWER column --
// the tie rule a dense top-k over the row would give. The index in the low bits also makes every
// key distinct, which is what lets a round identify its winner by equality instead of carrying a
// position alongside the value.
// The key is one 64-bit integer, so a round is a plain max. The high 32 bits are the float's
// order-preserving image (invert all bits if negative, set the sign bit if not), the low 32 are
// ~index, so a larger key is a larger value and, on a tie, the lower column (a dense top-k's tie
// rule). The index also makes every key distinct, so a round identifies its winner by equality.
//
// WHERE PT HAS TO BE BIG ENOUGH, AND WHERE IT BIT. A lane's top-PT filter loses a candidate when
// more than PT of the ones that matter land in that lane's slice, so PT is a statement about how
// the slice is chosen:
// PT bounds how many relevant candidates one lane's slice can hold:
//
// * Stage 1 gives a lane every 32nd eight-column group, and PT must cover a whole VECTOR LOAD --
// eight bf16 arrive at once, so PT=4 can lose a candidate to a single load. That is a real
// failure and not a conservative bound: PT=4 disagrees with torch.topk on smooth data while
// passing on tied data. PT=8 makes one load placeable in full, and beyond that a chunk would
// need nine of its own top-RC in one of 32 slices.
// * Stage 2's slice is NOT random. Stage 1 writes chunk-major, so at stride 32 with RC=32 lane
// 0 would receive the maximum of EVERY chunk -- exactly where the row's top-R concentrates --
// and PT=8 then drops all but eight of them. That is the second way the filter can lose. Stage 2
// therefore carries PT = 32 >= R: a lane can hold the entire answer, so no distribution of the
// candidates over lanes can lose one. It is exact by construction, not by argument.
// * Stage 1 gives a lane every 32nd eight-column group, and PT must cover a whole vector load:
// eight bf16 arrive at once, so PT=4 can lose a candidate to a single load (and does disagree
// with torch.topk on smooth data). PT=8 places one load in full; beyond that a chunk would need
// nine of its own top-RC in one of 32 slices.
// * Stage 2's slice is not random: stage 1 writes chunk-major, so at stride 32 with RC=32 lane 0
// receives every chunk's maximum, exactly where the row's top-R concentrates. Stage 2 therefore
// uses PT = 32 >= R: one lane can hold the entire answer, so it is exact for any distribution.
//
// RC IS THE ONE PLACE THE ANSWER CAN BE APPROXIMATE, and it is the caller's choice. A chunk hands
// on its own top-RC, so the row top-R survives unless MORE THAN RC of it falls in a single chunk.
// RC=R is exact for any data; smaller RC shortens stage 1's chain, which is what stage 1 costs, at
// a risk that falls off fast in the chunk count -- at R=32, RC=8 and 64 chunks it takes nine of
// the thirty-two in one sixty-fourth of the row. The caller that rescores its candidates exactly
// loses a candidate, not correctness, when that happens.
// RC is the only possible approximation, and the caller's choice. A chunk hands on its top-RC, so
// the row's top-R survives unless more than RC of it falls in one chunk. RC=R is exact for any
// data; a smaller RC shortens stage 1's chain at a risk that falls fast with the chunk count (at
// R=32, RC=8 and 64 chunks it takes nine of the 32 in one sixty-fourth of the row). A caller that
// rescores exactly then loses a candidate, not correctness.
#include "r4d.h"
#include "r4d_emit.h"
#include "r4d_emit.h"
#include "r4d_gemm_args.h"
#include "r4d_common.h"
#include "r4d_fp8_wmma.h"
#include "r4d_fp8_lm.h"
#include <hip/hip_bf16.h>
#include <cstdint>
#include <stdexcept>
#define R4D_TOPK_WAVE 32
#ifndef R4D_TOPK_PT1
#define R4D_TOPK_PT1 8 // stage 1: >= the eight bf16 a lane loads in one step
#endif
#define R4D_TOPK_PT2 32 // stage 2: >= R, so a lane can hold the whole answer
typedef unsigned u4v_tk __attribute__((ext_vector_type(4)));
typedef __attribute__((address_space(1))) const u4v_tk* r4d_tk_gv4;
#ifndef R4D_TOPK_WPB
#define R4D_TOPK_WPB 8 // independent waves per block: one chunk each, no barrier between
#endif
#ifndef R4D_TOPK_UNROLL
#define R4D_TOPK_UNROLL 4 // vector loads issued before any of them is consumed
#endif
__device__ __forceinline__ unsigned long long tk_key(unsigned short bf, unsigned idx) {
unsigned u = (unsigned)bf << 16;
@@ -98,21 +76,19 @@ __device__ __forceinline__ int tk_idx(unsigned long long k) {
return (int)(0xFFFFFFFFu - (unsigned)(k & 0xFFFFFFFFull));
}
// The larger of this lane's key and its partner's at lane ^ N, the partner read through DPP or
// permlanex16 rather than `__shfl_xor` -- see r4d_xor_lane.
// The larger of this lane's key and its partner's at lane ^ N, read through DPP or permlanex16
// (see r4d_xor_lane).
template <int N>
__device__ __forceinline__ unsigned long long tk_xor_max(unsigned long long m) {
const unsigned long long p = r4d_xor_lane_u64<N>(m);
return p > m ? p : m;
}
// THE ONLY GUARD IN FRONT OF THE INSERT IS ITS OWN, ON THE KEY. A float guard -- one v_cmp_f32
// against the list tail's value -- is a loss in stage 1, where making it a branch breaks the
// unrolled straight-line body that eight elements per vector load produce and costs about 18% at
// M=64 even on tied data. And it is wrong anywhere a NaN can arrive: the key orders a positive NaN
// above +inf and a negative one below -inf, while a float compare is false against everything, so
// a float guard drops what the key would keep and, once a NaN is the tail, drops every candidate
// after it. Every stage here and the merge rank on the key alone, so they agree on one order.
// The insert's only guard is its own key compare. A float guard against the tail's value costs
// ~18% at M=64 in stage 1 (a branch breaks the unrolled straight-line body), and it is wrong with
// NaN: the key orders a positive NaN above +inf and a negative one below -inf, while a float
// compare is false, so once a NaN is the tail every later candidate would be dropped. Every stage
// and the merge rank on the key alone, so they agree on one order.
template <int PT>
__device__ __forceinline__ void tk_insert(unsigned long long* t, unsigned long long k) {
if (k > t[PT - 1]) {
@@ -126,8 +102,8 @@ __device__ __forceinline__ void tk_insert(unsigned long long* t, unsigned long l
}
}
// One reduction round over the wave. Every lane gets the winner; the one lane that owns it drops
// its head. No LDS and no barrier -- that is what one wave per block buys.
// One reduction round over the wave: every lane gets the winner and its owner drops its head. No
// LDS and no barrier.
template <int PT>
__device__ __forceinline__ unsigned long long tk_round(unsigned long long* t) {
unsigned long long m = t[0];
@@ -148,9 +124,8 @@ template <int PT>
__global__ __launch_bounds__(R4D_TOPK_WAVE * R4D_TOPK_WPB) void r4d_rowtopk_stage1_kernel(
const unsigned short* __restrict__ Y, unsigned long long* __restrict__ P,
int N, int RC, int NCH, int CH) {
// WPB independent waves in one block, each on its own chunk. They share nothing -- no LDS, no
// barrier -- and the only reason to group them is that a block of 32 threads spends the CU's
// workgroup slots, not its wave slots, and runs out of the former first.
// WPB independent waves per block, each on its own chunk, sharing nothing. Grouping them only
// matters because 32-thread blocks exhaust the CU's workgroup slots before its wave slots.
const int m = blockIdx.y;
const int c = blockIdx.x * R4D_TOPK_WPB + (int)(threadIdx.x >> 5);
if (c >= NCH) return;
@@ -164,10 +139,9 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE * R4D_TOPK_WPB) void r4d_rowtopk_stag
for (int q = 0; q < PT; q++) t[q] = 0ull;
const unsigned short* row = Y + (size_t)m * N;
// Eight bf16 a lane a step: the wave then moves 512 contiguous bytes per issue, where a scalar
// ushort load would move 64 for the same instruction. UNROLL of them are issued before the
// first is consumed -- the insert chain is a register dependency and would otherwise let one
// memory latency stand between every load and the next.
// Eight bf16 per lane per load, so the wave moves 512 contiguous bytes per issue. UNROLL loads
// are issued before the first is consumed, since the insert chain would otherwise put a memory
// latency between consecutive loads.
const int v0 = c0 >> 3, v1 = c1 >> 3;
const int step = R4D_TOPK_WAVE * R4D_TOPK_UNROLL;
int v = v0 + lane;
@@ -206,17 +180,14 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE * R4D_TOPK_WPB) void r4d_rowtopk_stag
}
}
// `off` is the first GLOBAL column this rank's plane stands for, and `OP` is the optional
// (id, value) pair plane an all-gather moves -- eight bytes an entry, an int32 id riding in the
// first float slot, which is the layout core/sample/sampler.h holds as buf_pairs_ and the
// same one the sampler's own vocab-parallel path gathers. Both are inert at off = 0 with OP null,
// and that is the single-rank launch unchanged to the bit.
// `off` is the first global column this rank's plane stands for, and `OP` the optional (id, value)
// pair plane an all-gather moves: eight bytes an entry, an int32 id in the first float slot, the
// layout core/sample/sampler.h holds as buf_pairs_. Both are inert at off = 0 with OP null, which
// is the single-rank launch bit for bit.
//
// AN EMPTY SLOT IS NOT TOKEN `off`. A chunk that ran out of candidates leaves a zero key; OI has
// always written column 0 for it and keeps doing so, because adding the offset there would make a
// pad indistinguishable from this rank's first real token. The pair plane says -1 instead, which
// is the sentinel the merge drops -- and a sentinel rather than -INFINITY, because a masked logit
// legitimately takes that value.
// An empty slot is not token `off`. A chunk that ran out of candidates leaves a zero key; OI gets
// column 0 for it, without the offset, so a pad cannot look like this rank's first real token. The
// pair plane writes -1, the sentinel the merge drops (not -INFINITY, a valid masked logit).
__global__ __launch_bounds__(R4D_TOPK_WAVE) void r4d_rowtopk_stage2_kernel(
const unsigned long long* __restrict__ P, float* __restrict__ OV, int* __restrict__ OI,
float* __restrict__ OP, int off, int R, int n) {
@@ -227,18 +198,16 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE) void r4d_rowtopk_stage2_kernel(
#pragma unroll
for (int q = 0; q < R4D_TOPK_PT2; q++) t[q] = 0ull;
// THE KEY DECIDES, NOT THE FLOAT, as in stage 1 -- see tk_insert. A chunk that handed on a NaN
// among its top-RC handed on one fewer real candidate, so dropping the NaN here can leave the row
// short of R, and the pad that fills the gap is what the merge reports as id -1. An empty slot's
// key is 0 and never places.
// The key decides, as in stage 1 (see tk_insert). A NaN a chunk handed on displaced a real
// candidate, so dropping it here can leave the row short of R; the pad filling the gap is what
// the merge reports as id -1. An empty slot's key is 0 and never places.
const unsigned long long* p = P + (size_t)m * n;
for (int i = lane; i < n; i += R4D_TOPK_WAVE) tk_insert<R4D_TOPK_PT2>(t, p[i]);
for (int r = 0; r < R; r++) {
const unsigned long long w = tk_round<R4D_TOPK_PT2>(t);
if (lane == 0) {
// A chunk that ran out of candidates wrote a zero key, and a zero key must not read back as
// a real column: it is an empty slot and says so.
// A zero key is an empty slot, not a real column.
const float v = w ? tk_val(w) : -INFINITY;
OV[(size_t)m * R + r] = v;
OI[(size_t)m * R + r] = w ? (int)tk_idx(w) + off : 0;
@@ -252,43 +221,34 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE) void r4d_rowtopk_stage2_kernel(
}
}
// Chunks are a multiple of eight columns so stage 1 stays on the vector load, and the last one is
// clamped to N rather than padded -- a padded tail would need a mask on every element. The caller
// sizes `partial` from this, so the rounding lives in one place.
// Chunks are a multiple of eight columns so stage 1 stays on the vector load; the last one is
// clamped to N rather than padded (a padded tail would need a mask per element). The caller sizes
// `partial` from r4d_rowtopk_bf16_chunks, so the rounding lives here only.
static int rowtopk_chunk_width(int N, int NCH) {
const int CH = ((N + NCH - 1) / NCH + 7) & ~7;
return CH < 8 ? 8 : CH;
}
int r4d_rowtopk_bf16_chunks(int N, int NCH) {
int CH = ((N + NCH - 1) / NCH + 7) & ~7;
if (CH < 8) CH = 8;
const int CH = rowtopk_chunk_width(N, NCH);
return (N + CH - 1) / CH;
}
void r4d_rowtopk_bf16(long y, long partial, long outv, long outi,
int M, int N, int R, int RC, int NCH, int PT1, long stream) {
r4d_rowtopk_bf16_sharded(y, partial, outv, outi, 0, M, N, R, RC, NCH, PT1, 0, stream);
}
// The same top-R over a plane that is one rank's SLICE of a wider one: `off` is the first global
// column the slice stands for, and `outp` -- optional, 0 for none -- is the M*R (int32 id, float
// value) pair plane an all-gather moves so the ranks can agree on a global top-R without ever
// gathering the plane itself. It is a separate entry point, so the unsharded one keeps its
// signature.
/* The body takes a sink and RETURNS; the exported thrower keeps its signature. */
// The top-R over one rank's slice of a wider plane: `off` is the slice's first global column, and
// `outp` (optional, 0 for none) is the M*R (int32 id, float value) pair plane an all-gather moves
// so ranks can agree on a global top-R without gathering the plane.
static int rowtopk_bf16_sharded_emit(long y, long partial, long outv, long outi, long outp,
int M, int N, int R, int RC, int NCH, int PT1, int off,
const R4dSink& sk) {
RadStream stream) {
int emit_rc = RAD_OK;
if (M < 1 || N < 1) return -1;
if (N % 8) return -2;
// R <= PT2 is what makes stage 2 exact whatever the candidates look like; it is a real limit,
// not a tuning choice, so it is checked rather than clamped.
if (R < 1 || R > R4D_TOPK_PT2)
return -3;
if (RC < 1 || RC > PT1 * R4D_TOPK_WAVE)
return -4;
if (NCH < 1) return -5;
int CH = ((N + NCH - 1) / NCH + 7) & ~7;
if (CH < 8) CH = 8;
// R <= PT2 is what makes stage 2 exact for any candidates; a real limit, so checked, not
// clamped.
if (M < 1 || N < 1 || N % 8 || R < 1 || R > R4D_TOPK_PT2 || RC < 1 ||
RC > PT1 * R4D_TOPK_WAVE || NCH < 1)
return R4D_NO(RAD_E_SHAPE);
const int CH = rowtopk_chunk_width(N, NCH);
const int nch = (N + CH - 1) / CH;
#define R4D_TK_S1(PT) emit_rc = R4D_EMIT(sk, (r4d_rowtopk_stage1_kernel<PT>), \
#define R4D_TK_S1(PT) emit_rc = R4D_EMIT(stream, (r4d_rowtopk_stage1_kernel<PT>), \
dim3((nch + R4D_TOPK_WPB - 1) / R4D_TOPK_WPB, M), \
dim3(R4D_TOPK_WAVE * R4D_TOPK_WPB), 0, \
(const unsigned short*)y, (unsigned long long*)partial, N, RC, nch, CH)
@@ -297,38 +257,27 @@ static int rowtopk_bf16_sharded_emit(long y, long partial, long outv, long outi,
case 4: R4D_TK_S1(4); break;
case 8: R4D_TK_S1(8); break;
case 16: R4D_TK_S1(16); break;
default: return -6;
default: return R4D_NO(RAD_E_INVAL);
}
#undef R4D_TK_S1
if (emit_rc != RAD_OK) return emit_rc;
return R4D_EMIT(sk, r4d_rowtopk_stage2_kernel, dim3(M), dim3(R4D_TOPK_WAVE), 0,
return R4D_EMIT(stream, r4d_rowtopk_stage2_kernel, dim3(M), dim3(R4D_TOPK_WAVE), 0,
(const unsigned long long*)partial, (float*)outv, (int*)outi,
(float*)outp, off, R, nch * RC);
}
void r4d_rowtopk_bf16_sharded(long y, long partial, long outv, long outi, long outp,
int M, int N, int R, int RC, int NCH, int PT1, int off,
long stream) {
const int rc = rowtopk_bf16_sharded_emit(y, partial, outv, outi, outp, M, N, R, RC, NCH, PT1,
off, r4d_stream_sink((RadStream)(uintptr_t)stream));
if (rc != RAD_OK) throw std::runtime_error("r4d rowtopk_bf16: refused or failed to launch");
}
// ============================================================ the vocab-parallel merge
//
// The other half of a sharded top-R. Each rank ran the two stages above over ITS OWN slice of the
// plane and wrote an [M, R, 2] pair plane -- an int32 global id beside a float value; an all-gather
// concatenated those into [M, W*R, 2] with rank r's at [r*R, (r+1)*R). This picks the R largest of
// the W*R and writes them in the same (idx, val) form stage 2 writes, so nothing downstream can
// tell which path produced them.
// The other half of a sharded top-R. Each rank ran the two stages over its own slice and wrote an
// [M, R, 2] pair plane (int32 global id, float value); an all-gather concatenated those into
// [M, W*R, 2] with rank r's at [r*R, (r+1)*R). This picks the R largest of the W*R and writes them
// in stage 2's (idx, val) form.
//
// THE KEY IS THE SAME 64-BIT INTEGER, built from the float's bits rather than a bf16's, so a round
// is still a plain max and ties still break on the LOWER id -- which is what makes this equal to a
// top-R over the unsharded plane rather than merely close to it. A pad carries id -1 and is
// skipped: a sentinel, because -INFINITY is a value a masked logit legitimately takes.
// The key is the same 64-bit integer, built from the float's bits, so a round is a plain max and
// ties break on the lower id, which makes this equal to a top-R over the unsharded plane. A pad
// carries id -1 and is skipped (a sentinel, since -INFINITY is a legitimate masked logit).
//
// One wave a row, W*R entries -- 32 at world 2 and R 16 -- so this is a launch with a rounding
// error attached, and its only real requirement is that it be exact.
// One wave per row over W*R entries (32 at world 2, R 16); its cost is its launch.
__device__ __forceinline__ unsigned long long tk_key_f32(float v, unsigned id) {
unsigned u = __float_as_uint(v);
u = (u & 0x80000000u) ? ~u : (u | 0x80000000u);
@@ -361,46 +310,29 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE) void r4d_rowtopk_merge_kernel(
}
}
void r4d_rowtopk_merge(long gathered, long outv, long outi, int M, int R, int W, long stream) {
if (M < 1 || R < 1 || W < 1) throw std::runtime_error("r4d rowtopk_merge: M, R and W must be >= 1");
// R <= PT2 is what makes this exact whatever the candidates look like -- a lane can hold the
// whole answer -- so it is checked rather than clamped, the same as stage 2's.
if (R > R4D_TOPK_PT2) throw std::runtime_error("r4d rowtopk_merge: R must be 1..32");
hipLaunchKernelGGL(r4d_rowtopk_merge_kernel, dim3(M), dim3(R4D_TOPK_WAVE), 0,
(hipStream_t)stream, (const float*)gathered, (float*)outv, (int*)outi,
R, W * R);
}
// ============================================================== logit_rerank
//
// THE EXACT SECOND HALF OF A COARSE TOP-R, which is what this file's header already says the top-R
// is for: "only the best few dozen of them are ever rescored exactly". The coarse head proposes R
// candidates a row and this scores those R -- and only those R -- against the FULL-PRECISION head,
// then re-sorts on the exact values. A draft round reads R rows of the weight instead of all of
// it: 40 KB a row against 318 MB. On Qwen3.8-Flash-Next the 2-bit head accepts 4 to 8 points
// below the exact one, and that gap is what this closes.
// The exact second half of a coarse top-R: the coarse head proposes R candidates per row, and this
// scores only those against the full-precision head, then re-sorts on the exact values. A draft
// round reads R weight rows instead of the whole head (40 KB a row against 318 MB). On
// Qwen3.8-Flash-Next the 2-bit head's acceptance is 4 to 8 points below the exact head's, and this
// closes that gap.
//
// ONE BLOCK A ROW, THE WAVES SPREAD OVER THE CANDIDATES. A wave walks K with a lane stride and
// folds with the same five xor-butterfly steps the selection uses, so the reduction order is a fixed
// function of the shape and the score is reproducible. Then wave 0 runs the ORDINARY top-R chain
// over the R scores -- one lane a candidate, R <= PT2 -- so the tie rule is tk_key_f32's and is the
// same rule row_topk and row_topk_merge break on. A tie inside a shard and a tie between shards
// therefore resolve alike, which is the property that makes the merged answer exact.
// One block per row, waves spread over the candidates. A wave walks K with a lane stride and folds
// with the same five xor-butterfly steps, so the reduction order is a fixed function of the shape.
// Wave 0 then runs the ordinary top-R chain over the R scores (one lane a candidate, R <= PT2),
// so ties break by tk_key_f32's rule, as in row_topk and row_topk_merge; ties within and between
// shards resolve alike, which keeps the merged answer exact.
//
// A CANDIDATE OUTSIDE THIS RANK'S ROWS IS A PAD. `voff` is the shard's first column and the ids
// are GLOBAL, because that is what row_topk reports; under vocab-parallel drafting every id a rank
// is handed came from its own plane, and the guard is for a caller that mixes them.
// A candidate outside this rank's rows is a pad. `voff` is the shard's first column and ids are
// global, as row_topk reports them; under vocab-parallel drafting a rank only gets its own ids, and
// the guard is for a caller that mixes them.
//
// I8 READS THE INT8 HEAD logits_gemm_i8 stores (r4d.i8lm.r128): the same row -- K codes, then the
// row's own bf16 scale per 128 columns in its tail -- with each code an int8 value rather than an
// E4M3 one. The drafter's rescoring reads whichever head the trunk's logits GEMM reads, so a model
// with an int8 head needs this form or its lm_head has no device reader here.
// I8 reads the int8 head logits_gemm_i8 stores (r4d.i8lm.r128): the same row (K codes, then the
// row's bf16 scale per 128 columns in its tail) with int8 codes instead of E4M3. The drafter
// rescores against whichever head the trunk's logits GEMM reads, so an int8 head needs this form.
#define R4D_RERANK_WPB 8 // waves a block: 256 threads, and R <= 32 so a wave takes <= 4
__device__ __forceinline__ float tk_bf2f(unsigned short b) {
return __uint_as_float((unsigned)b << 16);
}
template <bool I8>
__global__ __launch_bounds__(R4D_TOPK_WAVE * R4D_RERANK_WPB) void r4d_logit_rerank_kernel(
const unsigned short* __restrict__ X, const int* __restrict__ IN,
@@ -420,15 +352,15 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE * R4D_RERANK_WPB) void r4d_logit_rera
const bool own = gid >= 0 && loc >= 0 && loc < nv;
float acc = 0.0f;
if (own) {
/* THE STORED ROW, exactly as logits_gemm_fp8 reads it (r4d_fp8_lm.h): K E4M3 codes then
* K/128 bf16 block scales in the row's own tail. A lane owns a strided subset of the
* columns and applies each column's own block scale locally, so the fold below is a plain
* sum and the order is fixed by the lane count alone. */
/* The stored row, as logits_gemm_fp8 reads it (r4d_fp8_lm.h): K E4M3 codes then K/128 bf16
* block scales in the row's tail. A lane owns a strided subset of columns and applies each
* column's block scale locally, so the fold is a plain sum whose order depends only on the
* lane count. */
const unsigned char* const wr = W + (size_t)loc * w_ld;
const unsigned short* const sr = (const unsigned short*)(wr + K);
for (long k = lane; k < K; k += R4D_TOPK_WAVE) {
const float c = I8 ? (float)(int8_t)wr[k] : r4d_fp8::e4m3((uint32_t)wr[k]);
acc = fmaf(c * r4d_fp8::bscale(sr[k / 128]), tk_bf2f(xr[k]), acc);
acc = fmaf(c * r4d_fp8::bscale(sr[k / 128]), bf16_to_f32(xr[k]), acc);
}
acc = r4d_wave_sum(acc);
}
@@ -460,53 +392,41 @@ __global__ __launch_bounds__(R4D_TOPK_WAVE * R4D_RERANK_WPB) void r4d_logit_rera
}
static int logit_rerank_emit(long x, long idx_in, long head, long outi, long outv, long outp,
int M, int R, long K, long voff, long nv, const R4dSink& sk,
bool i8 = false) {
int M, int R, long K, long voff, long nv, RadStream stream,
bool i8) {
const long w_ld = r4d_fp8::lm_row_bytes(K);
if (M < 1 || R < 1 || K < 1 || nv < 1) return -1;
// R <= PT2 is what makes the re-sort exact whatever the scores look like -- one lane holds the
// whole answer -- so it is checked rather than clamped, the same as the merge's.
if (R > R4D_TOPK_PT2) return -2;
// R <= PT2 makes the re-sort exact for any scores (one lane holds the whole answer), so it is
// checked, not clamped.
if (M < 1 || R < 1 || K < 1 || nv < 1 || R > R4D_TOPK_PT2) return R4D_NO(RAD_E_SHAPE);
#define R4D_RERANK_EMIT(I8) \
R4D_EMIT(sk, r4d_logit_rerank_kernel<I8>, dim3(M), dim3(R4D_TOPK_WAVE * R4D_RERANK_WPB), 0, \
(const unsigned short*)x, (const int*)idx_in, (const unsigned char*)head, (int*)outi, \
R4D_EMIT(stream, r4d_logit_rerank_kernel<I8>, dim3(M), dim3(R4D_TOPK_WAVE * R4D_RERANK_WPB), \
0, (const unsigned short*)x, (const int*)idx_in, (const unsigned char*)head, \
(int*)outi, \
(float*)outv, (float*)outp, R, K, voff, nv, w_ld)
const int rc = i8 ? R4D_RERANK_EMIT(true) : R4D_RERANK_EMIT(false);
#undef R4D_RERANK_EMIT
return rc;
}
void r4d_logit_rerank(long x, long idx_in, long head, long outi, long outv, long outp,
int M, int R, long K, long voff, long nv, long stream) {
if (logit_rerank_emit(x, idx_in, head, outi, outv, outp, M, R, K, voff, nv,
r4d_stream_sink((RadStream)(uintptr_t)stream)) != RAD_OK)
throw std::runtime_error("r4d logit_rerank: refused or failed to launch");
}
// ============================================================== the plugin ABI entry points
//
// The operand reader and the split law are 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 on why there
// is no adapter layer.
/* ------------------------------------------------------------------ row top-k */
enum { T_X = 0, T_IDX, T_VAL, T_PAIRS };
/* R > 32 is refused here, not by a constraint: libr4d's row says R <= 1024 in both its predicate
* and its prose, and the entry point throws above R4D_TOPK_PT2 == 32, calling that "a real limit,
* not a tuning choice". The constraint table transcribes what libr4d wrote and this is the
* per-call half that makes the pair sound. */
/* Also the scratch sizer's reader, which runs at declare with no tensors and no constraint check, so
* it repeats the row's R <= 32. */
static int topk_params(const RadArgs* a,
const RadTensor** x, const RadTensor** idx, const RadTensor** val,
long long* M, long long* N, long long* R, int* RC, int* NCH, int* PT1) {
*x = r4d_opt(a, T_X);
*idx = r4d_opt(a, T_IDX);
*val = r4d_opt(a, T_VAL);
/* THE SCRATCH SIZER RUNS AT DECLARE, WHERE THERE ARE NO TENSORS. It is the same entry point as
* the launch, so reading the geometry out of `x` alone would make sizing the partial plane
* ahead of a step fail with "invalid argument" -- which reads as a refusal by the kernel and
* is actually an absent operand. `M` and `N` are declared parameters of this op for exactly
* this reason (spec §2.1), so they are the fallback; when the tensor IS present it still wins,
* because a band ceiling is an upper bound and the tensor is the row count of the call. */
/* The scratch sizer runs at declare, with no tensors, through this same function. `M` and `N`
* are declared parameters of this op for that reason (spec §2.1) and serve as the fallback;
* when the tensor is present it wins, since a band ceiling is only an upper bound. */
if (*x) {
if ((*x)->rank != 2 || !r4d_contig(*x)) return R4D_NO(RAD_E_STRIDE);
*M = (*x)->shape[0];
@@ -528,8 +448,8 @@ static int topk_params(const RadArgs* a,
*RC = c->rc > 0 ? c->rc : (int)*R;
if (*RC < (int)*R) return R4D_NO(RAD_E_INVAL); /* a chunk must keep at least R */
if (*RC > *PT1 * 32) return R4D_NO(RAD_E_SHAPE);
/* NCH is a REQUEST for a chunk COUNT. 0 means "one chunk per 2048 columns, at least one",
* which is the shape the MoE router this kernel serves actually runs. */
/* NCH is a requested chunk count. 0 means one chunk per 2048 columns, at least one, the shape
* the MoE router this kernel serves runs. */
*NCH = c->nch > 0 ? c->nch : (int)((*N + 2047) / 2048);
if (*NCH < 1) *NCH = 1;
return RAD_OK;
@@ -541,14 +461,12 @@ extern "C" int64_t r4d_rad_rowtopk_scratch(const RadArgs* a) {
int RC, NCH, PT1;
int rc = topk_params(a, &x, &idx, &val, &M, &N, &R, &RC, &NCH, &PT1);
if (rc != RAD_OK) return rc;
/* The chunk width is rounded up to a multiple of eight columns inside libr4d, so the count is
* asked for rather than recomputed -- "the caller sizes `partial` from this, so the rounding
* lives in one place". */
/* The chunk width is rounded inside libr4d, so the count is asked for, not recomputed. */
const int nch = r4d_rowtopk_bf16_chunks((int)N, NCH);
return (int64_t)M * nch * RC * (int64_t)sizeof(unsigned long long);
}
static int rad_rowtopk_body(const RadArgs* a, const R4dSink& sk) {
extern "C" int r4d_rad_rowtopk(const RadArgs* a, RadStream stream) {
const RadTensor *x, *idx, *val;
long long M, N, R;
int RC, NCH, PT1;
@@ -561,9 +479,9 @@ static int rad_rowtopk_body(const RadArgs* a, const R4dSink& sk) {
const int64_t need = (int64_t)M * nch * RC * (int64_t)sizeof(unsigned long long);
if (!a->scratch || a->scratch_bytes < need) return R4D_NO(RAD_E_SCRATCH);
/* The vocab-parallel form, and both halves of it are optional: `vocab_off` shifts the columns
* reported so a rank's slice reports global ids, and `pairs` is the [M, R, 2] plane an
* all_gather moves. Absent and zero is the single-rank launch, bit for bit. */
/* The vocab-parallel form; both halves optional. `vocab_off` shifts the reported columns to
* global ids, and `pairs` is the [M, R, 2] plane an all_gather moves. Absent and zero is the
* single-rank launch, bit for bit. */
const RadTensor* pairs = r4d_opt(a, T_PAIRS);
const long long off = r4d_geti_or(a, "vocab_off", 0);
if (off < 0) return R4D_NO(RAD_E_SHAPE);
@@ -572,19 +490,15 @@ static int rad_rowtopk_body(const RadArgs* a, const R4dSink& sk) {
if (r4d_numel(pairs) < M * R * 2) return R4D_NO(RAD_E_SHAPE);
}
if (rowtopk_bf16_sharded_emit(r4d_addr(x), (long)(uintptr_t)a->scratch, r4d_addr(val),
r4d_addr(idx), pairs ? r4d_addr(pairs) : 0,
(int)M, (int)N, (int)R, RC, NCH, PT1, (int)off, sk) != RAD_OK)
return R4D_NO(RAD_E_DEVICE);
return RAD_OK;
return rowtopk_bf16_sharded_emit(r4d_addr(x), (long)(uintptr_t)a->scratch, r4d_addr(val),
r4d_addr(idx), pairs ? r4d_addr(pairs) : 0,
(int)M, (int)N, (int)R, RC, NCH, PT1, (int)off, stream);
}
R4D_ENTRY(r4d_rad_rowtopk, rad_rowtopk_body)
/* ------------------------------------------------------------------ row_topk_merge
*
* The R largest of the all-gathered per-rank candidate sets. No variants and no scratch: it is one
* wave a row over world_size * R pairs -- 32 of them at world 2 and R 16 -- so what it costs is its
* launch, and the only thing that matters about it is that it be exact. */
* The R largest of the all-gathered per-rank candidate sets. No variants and no scratch: one wave
* per row over world_size * R pairs, so its cost is its launch; it only has to be exact. */
extern "C" int r4d_rad_rowtopk_merge(const RadArgs* a, RadStream s) {
const RadTensor* g = r4d_opt(a, 0);
const RadTensor* idx = r4d_opt(a, 1);
@@ -593,12 +507,9 @@ extern "C" int r4d_rad_rowtopk_merge(const RadArgs* a, RadStream s) {
if (g->dtype != RAD_F32 || !r4d_contig(g)) return R4D_NO(RAD_E_STRIDE);
if (!r4d_contig(idx) || !r4d_contig(val)) return R4D_NO(RAD_E_STRIDE);
/* M COMES OFF THE OPERAND, not off the parameter. `M` is declared as a RANGE -- a band ceiling
* -- and what the parameter table carries is that ceiling, not the row count of this call; the
* gathered plane is row-narrowed to the rows the step actually has. Reading the parameter
* sized the checks below at the band and refused every real issue, which is the same trap
* `topk_params` above documents for the top-R's own scratch hook. The parameter is still the
* fallback, for a sizing query made before there are tensors. */
/* M comes from the operand: the `M` parameter is a band ceiling, and the gathered plane is
* narrowed to the step's rows, so sizing the checks by the parameter would refuse real calls.
* The parameter is the fallback for a sizing query made before there are tensors. */
const long long M = (g->rank >= 1) ? g->shape[0] : r4d_geti_or(a, "M", 0);
const long long R = r4d_geti_or(a, "R", 0);
const long long W = r4d_geti_or(a, "world_size", 0);
@@ -607,16 +518,15 @@ extern "C" int r4d_rad_rowtopk_merge(const RadArgs* a, RadStream s) {
if (r4d_numel(g) < M * W * R * 2) return R4D_NO(RAD_E_SHAPE);
if (r4d_numel(idx) < M * R || r4d_numel(val) < M * R) return R4D_NO(RAD_E_SHAPE);
R4D_TRY("rowtopk_merge",
r4d_rowtopk_merge(r4d_addr(g), r4d_addr(val), r4d_addr(idx), (int)M, (int)R, (int)W,
(long)(uintptr_t)s));
return RAD_OK;
/* R <= PT2 makes the merge exact for any candidates (one lane can hold the whole answer). */
return R4D_EMIT(s, r4d_rowtopk_merge_kernel, dim3((unsigned)M),
dim3(R4D_TOPK_WAVE), 0, (const float*)g->data, (float*)val->data,
(int*)idx->data, (int)R, (int)(W * R));
}
/* `M` and the candidate count come off the OPERANDS for the reason rowtopk_merge's do: `M` is a
* band ceiling and a step narrows it. `n_vocab` is THIS RANK'S rows of the head, which is what
* makes the ownership guard a shard test rather than a vocabulary one. */
static int rad_logit_rerank(const RadArgs* a, const R4dSink& sk, bool i8) {
/* `M` and the candidate count come from the operands, as in rowtopk_merge: `M` is a band ceiling.
* `n_vocab` is this rank's rows of the head, so the ownership guard is a shard test. */
static int rad_logit_rerank(const RadArgs* a, RadStream stream, bool i8) {
const RadTensor* x = r4d_opt(a, 0);
const RadTensor* w = r4d_opt(a, 1);
const RadTensor* in = r4d_opt(a, 2);
@@ -625,13 +535,11 @@ static int rad_logit_rerank(const RadArgs* a, const R4dSink& sk, bool i8) {
const RadTensor* pr = r4d_opt(a, 5);
if (!x || !in || !w || !idx || !val) return R4D_NO(RAD_E_INVAL);
if (x->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
/* THE DECLARED DTYPE IS WHAT ARRIVES, and for this weight it is the bf16 the trunk
* declared -- the E4M3 plane is the STORED form. logits_gemm_fp8 says the same thing
* about the stored WIDTH: it cannot be checked here and is derived by the same rule
* the layout hook used, and what guarantees the two agree is the engine's layout-tag
* check, which refuses a container laid out for any other row before a byte of it
* moves. So the head is accepted at either spelling and read as r4d.fp8lm.b128 -- or, for
* the int8 form, as r4d.i8lm.r128. */
/* The declared dtype arrives, which for this weight is the trunk's bf16; the E4M3 (or int8)
* plane is the stored form. Its stored width cannot be checked here and follows the layout
* hook's rule; the engine's layout-tag check guarantees agreement by refusing a container laid
* out for another row. So the head is accepted at either spelling and read as r4d.fp8lm.b128,
* or r4d.i8lm.r128 for the int8 form. */
if (w->dtype != (i8 ? RAD_I8 : RAD_F8E4M3) && w->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
if (in->dtype != RAD_I32 || idx->dtype != RAD_I32 || val->dtype != RAD_F32)
return R4D_NO(RAD_E_DTYPE);
@@ -652,18 +560,14 @@ static int rad_logit_rerank(const RadArgs* a, const R4dSink& sk, bool i8) {
if (pr && r4d_numel(pr) < M * R * 2) return R4D_NO(RAD_E_SHAPE);
if (K % 128) return R4D_NO(RAD_E_SHAPE); /* the scale block; lm_row_bytes assumes it */
if (logit_rerank_emit(r4d_addr(x), r4d_addr(in), r4d_addr(w), r4d_addr(idx), r4d_addr(val),
pr ? r4d_addr(pr) : 0, (int)M, (int)R, (long)K, (long)voff, (long)NV,
sk, i8) != RAD_OK)
return R4D_NO(RAD_E_DEVICE);
return RAD_OK;
return logit_rerank_emit(r4d_addr(x), r4d_addr(in), r4d_addr(w), r4d_addr(idx), r4d_addr(val),
pr ? r4d_addr(pr) : 0, (int)M, (int)R, (long)K, (long)voff, (long)NV,
stream, i8);
}
static int rad_logit_rerank_body(const RadArgs* a, const R4dSink& sk) {
return rad_logit_rerank(a, sk, false);
extern "C" int r4d_rad_logit_rerank(const RadArgs* a, RadStream stream) {
return rad_logit_rerank(a, stream, false);
}
R4D_ENTRY(r4d_rad_logit_rerank, rad_logit_rerank_body)
static int rad_logit_rerank_i8_body(const RadArgs* a, const R4dSink& sk) {
return rad_logit_rerank(a, sk, true);
extern "C" int r4d_rad_logit_rerank_i8(const RadArgs* a, RadStream stream) {
return rad_logit_rerank(a, stream, true);
}
R4D_ENTRY(r4d_rad_logit_rerank_i8, rad_logit_rerank_i8_body)