radiance 2.0.0

Radiance becomes a multi-model engine: one process serves several models behind one port, loads
each when a request names it, parks it in host memory or restores it from its container, and
shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the
command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP).

Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an
int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block
FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such
weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5,
Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B,
the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0
serves all of them.

Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new
Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill
reworked, and a reworked adaptive draft window.

Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its
configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool
in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server,
each accelerator's memory by model, model states over time.

Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by
AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests
keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410);
eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d
large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi
(#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon
(#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and
multi-model serving as asked for in #367 and #109.

Plugin ABI 39: out-of-tree plugins need to be rebuilt.

Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
This commit is contained in:
Deadcode committed 2026-11-14 22:32:28 +01:00
1 parent b4c04b6042
commit ddcb9c69bb
696 files changed
+120770 -56596

No files matched your search

+344 -169
View File
@@ -351,10 +351,12 @@ __global__ __launch_bounds__(kThreads) void rmsnorm_p_kernel(
// `b_step` 0 reads one element per row; with b_ld 0 too, a scalar for the whole op. `div` (add
// only) divides the sum after its rounding and rounds again, the last step of a mean summed
// pairwise; 1 is the plain sum.
__global__ __launch_bounds__(kThreads) void binary_kernel(
const uint16_t* __restrict__ a, const uint16_t* __restrict__ b, uint16_t* __restrict__ y,
uint32_t n, uint32_t a_ld, uint32_t b_ld, uint32_t y_ld, int mul, uint32_t b_step,
float div) {
//
// r4dchain_binary is the chained form (r4d_chain.h), taken at decode heights.
#define R4D_BIN_PARAMS \
const uint16_t* __restrict__ a, const uint16_t* __restrict__ b, uint16_t* __restrict__ y, \
uint32_t n, uint32_t a_ld, uint32_t b_ld, uint32_t y_ld, int mul, uint32_t b_step, float div
__device__ __forceinline__ void binary_body(R4D_BIN_PARAMS) {
const uint32_t m = blockIdx.x;
const uint16_t* __restrict__ ar = a + (size_t)m * a_ld;
const uint16_t* __restrict__ br = b + (size_t)m * b_ld;
@@ -366,6 +368,15 @@ __global__ __launch_bounds__(kThreads) void binary_kernel(
yr[i] = f32_to_bf16(r);
}
}
__global__ __launch_bounds__(kThreads) void binary_kernel(R4D_BIN_PARAMS) {
binary_body(a, b, y, n, a_ld, b_ld, y_ld, mul, b_step, div);
}
__global__ __launch_bounds__(kThreads) void r4dchain_binary(R4D_BIN_PARAMS) {
r4d_chain_wait();
binary_body(a, b, y, n, a_ld, b_ld, y_ld, mul, b_step, div);
r4d_chain_done();
}
#undef R4D_BIN_PARAMS
// The f32 form (a video decoder's residual stream, a vocoder's branch mean), with the same
// broadcast rule and `div`. Flat over the M x n extent: a vocoder's last stages are a few channels
@@ -417,15 +428,27 @@ __global__ __launch_bounds__(kThreads) void mul_weight_f32_kernel(
//
// Row-wise with a pitch because the operand is a column slice: the gate is the second half of each
// head of an interleaved [tokens, heads, 2*head_dim] projection, `n` wide inside a stride of 2n.
__global__ __launch_bounds__(kThreads) void sigmoid_kernel(
const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
uint32_t n, uint32_t x_ld, uint32_t y_ld) {
//
// r4dchain_sigmoid is the chained form (r4d_chain.h), taken at decode heights.
#define R4D_SIG_PARAMS \
const uint16_t* __restrict__ x, uint16_t* __restrict__ y, uint32_t n, uint32_t x_ld, \
uint32_t y_ld
__device__ __forceinline__ void sigmoid_body(R4D_SIG_PARAMS) {
const uint32_t m = blockIdx.x;
const uint16_t* __restrict__ xr = x + (size_t)m * x_ld;
uint16_t* __restrict__ yr = y + (size_t)m * y_ld;
for (uint32_t i = threadIdx.x; i < n; i += kThreads)
yr[i] = f32_to_bf16(1.0f / (1.0f + expf(-bf16_to_f32(xr[i]))));
}
__global__ __launch_bounds__(kThreads) void sigmoid_kernel(R4D_SIG_PARAMS) {
sigmoid_body(x, y, n, x_ld, y_ld);
}
__global__ __launch_bounds__(kThreads) void r4dchain_sigmoid(R4D_SIG_PARAMS) {
r4d_chain_wait();
sigmoid_body(x, y, n, x_ld, y_ld);
r4d_chain_done();
}
#undef R4D_SIG_PARAMS
// ---------------------------------------------------------------------------- logit_softcap
// ref_logit_softcap: y = cap * tanh(x / cap), the final-logit softcap. Row-wise with a pitch like
@@ -507,10 +530,13 @@ __global__ __launch_bounds__(kThreads) void scale_kernel(
// A row is split over grid.y, kSiluPer elements per thread, so decode's few rows use more than a
// few workgroups. A thread's loads are unconditional (a column past the row reads the last element
// and its store is dropped), so all are in flight at once.
//
// r4dchain_silu_mul is the chained form (r4d_chain.h), taken up to a decode step's routed slots.
constexpr uint32_t kSiluPer = 4;
__global__ __launch_bounds__(kThreads) void silu_mul_kernel(
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y,
uint32_t n, uint32_t gu_ld, uint32_t y_ld) {
#define R4D_SM_PARAMS \
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y, uint32_t n, uint32_t gu_ld, \
uint32_t y_ld
__device__ __forceinline__ void silu_mul_body(R4D_SM_PARAMS) {
const uint32_t m = blockIdx.x;
const uint16_t* __restrict__ gr = gu + (size_t)m * gu_ld;
uint16_t* __restrict__ yr = y + (size_t)m * y_ld;
@@ -529,14 +555,25 @@ __global__ __launch_bounds__(kThreads) void silu_mul_kernel(
if (i < n) yr[i] = f32_to_bf16((g / (1.0f + expf(-g))) * bf16_to_f32(uv[u]));
}
}
__global__ __launch_bounds__(kThreads) void silu_mul_kernel(R4D_SM_PARAMS) {
silu_mul_body(gu, y, n, gu_ld, y_ld);
}
__global__ __launch_bounds__(kThreads) void r4dchain_silu_mul(R4D_SM_PARAMS) {
r4d_chain_wait();
silu_mul_body(gu, y, n, gu_ld, y_ld);
r4d_chain_done();
}
#undef R4D_SM_PARAMS
// ---------------------------------------------------------------------------- gelu_mul
// ref_gelu_mul: y[.., i] = gelu_tanh(gu[.., i]) * gu[.., n + i], gate first as in silu_mul. The tanh
// GELU (gemma's gelu_pytorch_tanh), not erf, with the same constants as the gemm_nt_bias
// "gelu_tanh" epilogue so the two agree bit for bit.
__global__ __launch_bounds__(kThreads) void gelu_mul_kernel(
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y,
uint32_t n, uint32_t gu_ld, uint32_t y_ld) {
// r4dchain_gelu_mul is the chained form (r4d_chain.h), taken at decode heights.
#define R4D_GM_PARAMS \
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y, uint32_t n, uint32_t gu_ld, \
uint32_t y_ld
__device__ __forceinline__ void gelu_mul_body(R4D_GM_PARAMS) {
const uint32_t m = blockIdx.x;
const uint16_t* __restrict__ gr = gu + (size_t)m * gu_ld;
uint16_t* __restrict__ yr = y + (size_t)m * y_ld;
@@ -547,6 +584,15 @@ __global__ __launch_bounds__(kThreads) void gelu_mul_kernel(
yr[i] = f32_to_bf16(gelu * bf16_to_f32(gr[n + i]));
}
}
__global__ __launch_bounds__(kThreads) void gelu_mul_kernel(R4D_GM_PARAMS) {
gelu_mul_body(gu, y, n, gu_ld, y_ld);
}
__global__ __launch_bounds__(kThreads) void r4dchain_gelu_mul(R4D_GM_PARAMS) {
r4d_chain_wait();
gelu_mul_body(gu, y, n, gu_ld, y_ld);
r4d_chain_done();
}
#undef R4D_GM_PARAMS
// ---------------------------------------------------------------------------- rope
// ref_rope, in place over a fused [n_head + 2*n_head_kv] head row; the trailing V heads are left
@@ -579,11 +625,16 @@ __device__ __forceinline__ int rope_comp(int mode, uint32_t i, const int* sec, i
return c;
}
__global__ __launch_bounds__(kThreads) void rope_kernel(
uint16_t* __restrict__ qkv, const int32_t* __restrict__ pos,
uint32_t head_dim, uint32_t n_rope_heads, uint32_t rot, uint32_t nfreq, int den, int gptj,
float theta, float rscale, uint32_t row_pitch,
int mode, int s0, int s1, int s2, int n_sec, uint32_t plane, RadYarn yarn, RadLlama3 l3) {
// r4dchain_rope is the chained form (r4d_chain.h), taken at decode heights.
#define R4D_ROPE_PARAMS \
uint16_t* __restrict__ qkv, const int32_t* __restrict__ pos, uint32_t head_dim, \
uint32_t n_rope_heads, uint32_t rot, uint32_t nfreq, int den, int gptj, float theta, \
float rscale, uint32_t row_pitch, int mode, int s0, int s1, int s2, int n_sec, \
uint32_t plane, RadYarn yarn, RadLlama3 l3
#define R4D_ROPE_ARGS \
qkv, pos, head_dim, n_rope_heads, rot, nfreq, den, gptj, theta, rscale, row_pitch, mode, s0, \
s1, s2, n_sec, plane, yarn, l3
__device__ __forceinline__ void rope_body(R4D_ROPE_PARAMS) {
const uint32_t m = blockIdx.x;
const uint32_t half = rot / 2;
const float div = rscale != 0.0f ? rscale : 1.0f;
@@ -620,6 +671,16 @@ __global__ __launch_bounds__(kThreads) void rope_kernel(
hd[i1] = f32_to_bf16(x0 * s + x1 * c);
}
}
__global__ __launch_bounds__(kThreads) void rope_kernel(R4D_ROPE_PARAMS) {
rope_body(R4D_ROPE_ARGS);
}
__global__ __launch_bounds__(kThreads) void r4dchain_rope(R4D_ROPE_PARAMS) {
r4d_chain_wait();
rope_body(R4D_ROPE_ARGS);
r4d_chain_done();
}
#undef R4D_ROPE_ARGS
#undef R4D_ROPE_PARAMS
// The same rotary at prefill row counts with each (token, pair) angle computed once. rope_kernel
// gives every (head, pair) its own thread, which recomputes powf and the accurate sincosf for every
@@ -675,11 +736,13 @@ __global__ __launch_bounds__(kThreads) void rope_rows_kernel(
// K then V inside a slot: key element d at [..., d], value element d at [..., head_dim + d]. A flat
// slot index (`slot_mapping`) splits into block = slot / block_size, offset = slot % block_size. A
// negative slot is padding and stores nothing.
__global__ __launch_bounds__(kThreads) void kv_store_kernel(
const uint16_t* __restrict__ k, const uint16_t* __restrict__ v,
const int32_t* __restrict__ slot, uint16_t* __restrict__ kv,
uint32_t head_dim, uint32_t kv_heads, uint32_t block_size, uint32_t n_blocks,
uint32_t k_ld, uint32_t v_ld) {
// The body: a padding row's block returns early, so the chained form (r4dchain_kv_store,
// r4d_chain.h; taken at decode heights) wraps it.
#define R4D_KVS_PARAMS \
const uint16_t* __restrict__ k, const uint16_t* __restrict__ v, \
const int32_t* __restrict__ slot, uint16_t* __restrict__ kv, uint32_t head_dim, \
uint32_t kv_heads, uint32_t block_size, uint32_t n_blocks, uint32_t k_ld, uint32_t v_ld
__device__ __forceinline__ void kv_store_body(R4D_KVS_PARAMS) {
const uint32_t t = blockIdx.x;
const int32_t s = slot[t];
if (s < 0 || (uint32_t)s / block_size >= n_blocks) return;
@@ -694,6 +757,15 @@ __global__ __launch_bounds__(kThreads) void kv_store_kernel(
c[head_dim + d] = v[(size_t)t * v_ld + j];
}
}
__global__ __launch_bounds__(kThreads) void kv_store_kernel(R4D_KVS_PARAMS) {
kv_store_body(k, v, slot, kv, head_dim, kv_heads, block_size, n_blocks, k_ld, v_ld);
}
__global__ __launch_bounds__(kThreads) void r4dchain_kv_store(R4D_KVS_PARAMS) {
r4d_chain_wait();
kv_store_body(k, v, slot, kv, head_dim, kv_heads, block_size, n_blocks, k_ld, v_ld);
r4d_chain_done();
}
#undef R4D_KVS_PARAMS
// The same scatter into an E4M3 cache: the write side of the fp8kv attention family and its
// `k_descale` / `v_descale` operands.
@@ -814,84 +886,106 @@ __global__ __launch_bounds__(kThreads) void kv_store_streamed_kernel(
}
}
// The fp8_v4 store: K as kv_store_fp8 writes it, V as int4 in groups of 32 with an f16 scale
// (absmax / 7). Four columns a thread as above, so a group of 32 is 8 lanes; head_dim is a multiple
// of 32, so a group never straddles two heads. A token's row is `row` bytes. K6 is the k6_v4 store:
// K as int6 in groups of 32 (absmax / 31) in the row layout of abi/rad_builder.h's RAD_KVQ_K6V4, V
// as above after it.
template <int K6>
__global__ __launch_bounds__(kThreads) void kv_store_fp8v4_kernel(
// The int8 store (abi/rad_builder.h): a head's K and its V each as int8 codes against one f16
// scale, the head's absmax / 127, written after the codes. One workgroup a (token, head) and one
// dim a lane, so the absmax is a block reduction through LDS. The scale is rounded to f16 first
// and the codes are taken against the rounded value, which is what a reader multiplies by; it is
// clamped so 127 times it stays an f16. An all-zero head stores scale 0 and codes 0.
//
// kv_i8_quant is a lane's part of that: its K and V codes and the head's two scales. It holds the
// block's barrier, so every lane of the block calls it, a padding row's included.
struct KvI8 { uint8_t k, v; uint16_t ks, vs; };
__device__ __forceinline__ KvI8 kv_i8_quant(const uint16_t* __restrict__ k,
const uint16_t* __restrict__ v, uint32_t head_dim,
uint32_t k_ld, uint32_t v_ld, float (*s_m)[32]) {
const uint32_t t = blockIdx.x, h = blockIdx.y, d = threadIdx.x;
const bool in = d < head_dim;
const float kf = in ? bf16_to_f32(k[(size_t)t * k_ld + (size_t)h * head_dim + d]) : 0.0f;
const float vf = in ? bf16_to_f32(v[(size_t)t * v_ld + (size_t)h * head_dim + d]) : 0.0f;
float km = r4d_wave_max(fabsf(kf)), vm = r4d_wave_max(fabsf(vf));
if ((d & 31u) == 0u) { s_m[0][d >> 5] = km; s_m[1][d >> 5] = vm; }
__syncthreads();
km = 0.0f; vm = 0.0f;
for (uint32_t w = 0; w < blockDim.x / 32u; ++w) {
km = fmaxf(km, s_m[0][w]);
vm = fmaxf(vm, s_m[1][w]);
}
const __fp16 k16 = (__fp16)fminf(km / 127.0f, 515.0f);
const __fp16 v16 = (__fp16)fminf(vm / 127.0f, 515.0f);
const float ksc = (float)k16, vsc = (float)v16;
const float kr = ksc > 0.0f ? rintf(kf / ksc) : 0.0f;
const float vr = vsc > 0.0f ? rintf(vf / vsc) : 0.0f;
KvI8 q;
q.k = (uint8_t)(int8_t)(int)fminf(fmaxf(kr, -127.0f), 127.0f);
q.v = (uint8_t)(int8_t)(int)fminf(fmaxf(vr, -127.0f), 127.0f);
q.ks = __builtin_bit_cast(uint16_t, k16);
q.vs = __builtin_bit_cast(uint16_t, v16);
return q;
}
// A lane's bytes into a token's row of a head.
__device__ __forceinline__ void kv_i8_put(uint8_t* __restrict__ c, uint32_t head_dim, KvI8 q) {
const uint32_t d = threadIdx.x;
c[d] = q.k;
c[head_dim + d] = q.v;
if (d == 0u) {
*(uint16_t*)(c + 2 * head_dim) = q.ks;
*(uint16_t*)(c + 2 * head_dim + 2) = q.vs;
}
}
// The body takes the block's reduction table from the kernel that calls it; it is a function
// because a padding row's block returns early and the chained form (r4dchain_kv_store_i8,
// r4d_chain.h, taken at decode heights) must still add to its queue's counter.
#define R4D_KVI_PARAMS \
const uint16_t* __restrict__ k, const uint16_t* __restrict__ v, \
const int32_t* __restrict__ slot, uint8_t* __restrict__ kv, uint32_t head_dim, \
uint32_t kv_heads, uint32_t block_size, uint32_t n_blocks, uint32_t k_ld, uint32_t v_ld
#define R4D_KVI_ARGS k, v, slot, kv, head_dim, kv_heads, block_size, n_blocks, k_ld, v_ld
__device__ __forceinline__ void kv_store_i8_body(R4D_KVI_PARAMS, float (*s_m)[32]) {
const uint32_t t = blockIdx.x, h = blockIdx.y, d = threadIdx.x;
const KvI8 q = kv_i8_quant(k, v, head_dim, k_ld, v_ld, s_m);
const int32_t s = slot[t];
if (d >= head_dim || s < 0 || (uint32_t)s / block_size >= n_blocks) return;
const uint32_t blk = (uint32_t)s / block_size, off = (uint32_t)s % block_size;
const size_t row = 2 * (size_t)head_dim + 4;
kv_i8_put(kv + (((size_t)blk * kv_heads + h) * block_size + off) * row, head_dim, q);
}
__global__ __launch_bounds__(1024) void kv_store_i8_kernel(R4D_KVI_PARAMS) {
__shared__ float s_m[2][32];
kv_store_i8_body(R4D_KVI_ARGS, s_m);
}
__global__ __launch_bounds__(1024) void r4dchain_kv_store_i8(R4D_KVI_PARAMS) {
__shared__ float s_m[2][32];
r4d_chain_wait();
kv_store_i8_body(R4D_KVI_ARGS, s_m);
r4d_chain_done();
}
#undef R4D_KVI_ARGS
#undef R4D_KVI_PARAMS
// The int8 store of a streamed group: kv_store_i8's bytes to the token's row of its block in the
// host mirror always, and to the ring at `res[block]` when that block is resident. A slot past the
// id space stores nothing and a map entry outside [-1, slots) reads as absent, as in
// kv_store_streamed_kernel.
__global__ __launch_bounds__(1024) void kv_store_streamed_i8_kernel(
const uint16_t* __restrict__ k, const uint16_t* __restrict__ v,
const int32_t* __restrict__ slot, uint8_t* __restrict__ kv,
uint32_t head_dim, uint32_t kv_heads, uint32_t block_size, uint32_t n_blocks,
uint32_t k_ld, uint32_t v_ld, float k_inv, float v_inv, uint32_t row) {
const uint32_t t = blockIdx.x;
const int32_t* __restrict__ res, uint8_t* __restrict__ host,
uint32_t head_dim, uint32_t kv_heads, uint32_t block_size, uint32_t blocks, uint32_t slots,
uint32_t k_ld, uint32_t v_ld) {
__shared__ float s_m[2][32];
const uint32_t t = blockIdx.x, h = blockIdx.y, d = threadIdx.x;
const KvI8 q = kv_i8_quant(k, v, head_dim, k_ld, v_ld, s_m);
const int32_t s = slot[t];
if (s < 0 || (uint32_t)s / block_size >= n_blocks) return;
if (d >= head_dim || s < 0) return;
const uint32_t blk = (uint32_t)s / block_size, off = (uint32_t)s % block_size;
const size_t slot_base = ((size_t)blk * kv_heads * block_size + (size_t)off) * row;
const size_t head_step = (size_t)block_size * row;
for (uint32_t j = threadIdx.x * 4u; j < kv_heads * head_dim; j += kThreads * 4u) {
const uint32_t h = j / head_dim, d = j % head_dim;
uint8_t* __restrict__ c = kv + slot_base + (size_t)h * head_step;
float kf[4], vf[4];
#pragma unroll
for (int e = 0; e < 4; ++e) {
kf[e] = bf16_to_f32(k[(size_t)t * k_ld + j + (uint32_t)e]) * k_inv;
vf[e] = bf16_to_f32(v[(size_t)t * v_ld + j + (uint32_t)e]) * v_inv;
}
const uint32_t voff = K6 ? head_dim * 3 / 4 : head_dim;
const uint32_t vsoff = K6 ? head_dim * 21 / 16 : head_dim * 3 / 2;
if constexpr (K6) {
// A group of 32 is 8 lanes; its scale absmax / 31 in f16, codes 6-bit two's complement.
float kmax = fmaxf(fmaxf(fabsf(kf[0]), fabsf(kf[1])), fmaxf(fabsf(kf[2]), fabsf(kf[3])));
for (int m = 4; m >= 1; m >>= 1) kmax = fmaxf(kmax, __shfl_xor(kmax, m, 32));
// Clamped so 31 times it stays an f16 (65504): a group gone inf
// or NaN decodes finite, as the E4M3 form's clamp to 448 does.
const __fp16 k16 = (__fp16)fminf(kmax / 31.0f, 2112.0f);
const float ksc = (float)k16;
uint32_t q[4];
#pragma unroll
for (int e = 0; e < 4; ++e) {
const float r = ksc > 0.0f ? rintf(kf[e] / ksc) : 0.0f;
q[e] = (uint32_t)((int)fminf(fmaxf(r, -31.0f), 31.0f)) & 0x3Fu;
}
*(uint16_t*)(c + d / 2) = (uint16_t)((q[0] & 15u) | ((q[1] & 15u) << 4) |
((q[2] & 15u) << 8) | ((q[3] & 15u) << 12));
// The high bits of dims x..x+3 of a 16-dim chunk (x = d % 16 in 0, 4, 8, 12): bits
// 0-3 of bytes x/2 and x/2+1 for x < 8, bits 4-7 of bytes (x-8)/2 and (x-8)/2+1 for
// x >= 8. The lane two over holds the dims eight on, so the lower lane writes both.
const uint32_t hb = ((q[0] >> 4) | ((q[1] >> 4) << 2)) | ((((q[2] >> 4) | ((q[3] >> 4) << 2))) << 8);
const uint32_t up = (uint32_t)__shfl_xor((int)hb, 2, 32);
if ((d & 15u) < 8u)
*(uint16_t*)(c + head_dim / 2 + (d / 16) * 4 + (d & 7u) / 2) = (uint16_t)(hb | (up << 4));
if ((d & 31u) == 0)
*(uint16_t*)(c + head_dim * 5 / 4 + (d / 32) * 2) = __builtin_bit_cast(uint16_t, k16);
} else {
int pk = 0;
pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(kf[0], -448.0f), 448.0f),
fminf(fmaxf(kf[1], -448.0f), 448.0f), pk, false);
pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(kf[2], -448.0f), 448.0f),
fminf(fmaxf(kf[3], -448.0f), 448.0f), pk, true);
*(uint32_t*)(c + d) = (uint32_t)pk;
}
float amax = fmaxf(fmaxf(fabsf(vf[0]), fabsf(vf[1])), fmaxf(fabsf(vf[2]), fabsf(vf[3])));
for (int m = 4; m >= 1; m >>= 1) amax = fmaxf(amax, __shfl_xor(amax, m, 32));
// Clamped so 7 times it stays an f16: see the K scale above.
const __fp16 s16 = (__fp16)fminf(amax / 7.0f, 8188.0f);
const float sc = (float)s16;
uint32_t q[4];
#pragma unroll
for (int e = 0; e < 4; ++e) {
const float r = sc > 0.0f ? rintf(vf[e] / sc) : 0.0f;
q[e] = (uint32_t)((int)fminf(fmaxf(r, -7.0f), 7.0f)) & 0xFu;
}
*(uint16_t*)(c + voff + d / 2) = (uint16_t)(q[0] | (q[1] << 4) | (q[2] << 8) | (q[3] << 12));
if ((d & 31u) == 0)
*(uint16_t*)(c + vsoff + (d / 32) * 2) = __builtin_bit_cast(uint16_t, s16);
}
if (blk >= blocks) return;
const size_t row = 2 * (size_t)head_dim + 4;
const size_t brow = (size_t)kv_heads * block_size * row; // one block of every head
const size_t o = ((size_t)h * block_size + off) * row;
kv_i8_put(host + (size_t)blk * brow + o, head_dim, q);
const int32_t sl = res[blk];
if (sl >= 0 && (uint32_t)sl < slots) kv_i8_put(kv + (size_t)sl * brow + o, head_dim, q);
}
} // namespace
@@ -1132,31 +1226,48 @@ extern "C" int r4d_rmsnorm_p_bf16(long x, long y, int M, int n, float eps,
// `op` is 0 for a sum, 1 for a product. `b_ld` 0 broadcasts one row of n down the rows; -1 is a
// one-element b, broadcast as a scalar.
extern "C" int r4d_binary_bf16(long a, long b, long y, int M, int n, int op,
int a_ld, int b_ld, int y_ld, long stream) {
static int binary_bf16_emit(long a, long b, long y, int M, int n, int op, int a_ld, int b_ld,
int y_ld, long stream, bool chain) {
if (!a || !b || !y) return -1;
if (M <= 0 || n <= 0) return -2;
if (!a_ld) a_ld = n;
if (!y_ld) y_ld = n;
const uint32_t b_step = b_ld < 0 ? 0u : 1u;
if (b_ld < 0) b_ld = 0;
hipLaunchKernelGGL(binary_kernel, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream,
(const uint16_t*)a, (const uint16_t*)b, (uint16_t*)y, (uint32_t)n,
(uint32_t)a_ld, (uint32_t)b_ld, (uint32_t)y_ld, op, b_step, 1.0f);
#define R4D_BIN(K_) \
hipLaunchKernelGGL(K_, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream, \
(const uint16_t*)a, (const uint16_t*)b, (uint16_t*)y, (uint32_t)n, \
(uint32_t)a_ld, (uint32_t)b_ld, (uint32_t)y_ld, op, b_step, 1.0f)
if (chain) R4D_BIN(r4dchain_binary);
else R4D_BIN(binary_kernel);
#undef R4D_BIN
return hipGetLastError() == hipSuccess ? 0 : -4;
}
extern "C" int r4d_sigmoid_bf16(long x, long y, int M, int n, int x_ld, int y_ld, long stream) {
extern "C" int r4d_binary_bf16(long a, long b, long y, int M, int n, int op,
int a_ld, int b_ld, int y_ld, long stream) {
return binary_bf16_emit(a, b, y, M, n, op, a_ld, b_ld, y_ld, stream, false);
}
static int sigmoid_emit(long x, long y, int M, int n, int x_ld, int y_ld, long stream, bool chain) {
if (!x || !y) return -1;
if (M <= 0 || n <= 0) return -2;
if (!x_ld) x_ld = n;
if (!y_ld) y_ld = n;
hipLaunchKernelGGL(sigmoid_kernel, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream,
(const uint16_t*)x, (uint16_t*)y, (uint32_t)n,
(uint32_t)x_ld, (uint32_t)y_ld);
#define R4D_SIG(K_) \
hipLaunchKernelGGL(K_, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream, \
(const uint16_t*)x, (uint16_t*)y, (uint32_t)n, (uint32_t)x_ld, \
(uint32_t)y_ld)
if (chain) R4D_SIG(r4dchain_sigmoid);
else R4D_SIG(sigmoid_kernel);
#undef R4D_SIG
return hipGetLastError() == hipSuccess ? 0 : -4;
}
extern "C" int r4d_sigmoid_bf16(long x, long y, int M, int n, int x_ld, int y_ld, long stream) {
return sigmoid_emit(x, y, M, n, x_ld, y_ld, stream, false);
}
// ---------------------------------------------------------------------------- fill
// y[m, i] = v over an [M, n] extent with a row pitch, for a rank with nothing to compute whose
// downstream reader (an all-reduce, a residual) still needs a defined buffer. A memset cannot
@@ -1254,31 +1365,49 @@ extern "C" int r4d_scale_bf16(long x, long s, uint32_t s_dt, long y, int M, int
return hipGetLastError() == hipSuccess ? 0 : -4;
}
extern "C" int r4d_silu_mul_bf16(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream) {
static int silu_mul_emit(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream,
bool chain) {
if (!gu || !y) return -1;
if (M <= 0 || n <= 0) return -2;
if (!gu_ld) gu_ld = 2 * n;
if (!y_ld) y_ld = n;
const uint32_t chunks = ((uint32_t)n + kSiluPer * kThreads - 1u) / (kSiluPer * kThreads);
hipLaunchKernelGGL(silu_mul_kernel, dim3((uint32_t)M, chunks), dim3(kThreads), 0,
(hipStream_t)stream, (const uint16_t*)gu, (uint16_t*)y, (uint32_t)n,
(uint32_t)gu_ld, (uint32_t)y_ld);
#define R4D_SM(K_) \
hipLaunchKernelGGL(K_, dim3((uint32_t)M, chunks), dim3(kThreads), 0, (hipStream_t)stream, \
(const uint16_t*)gu, (uint16_t*)y, (uint32_t)n, (uint32_t)gu_ld, \
(uint32_t)y_ld)
if (chain) R4D_SM(r4dchain_silu_mul);
else R4D_SM(silu_mul_kernel);
#undef R4D_SM
return hipGetLastError() == hipSuccess ? 0 : -4;
}
extern "C" int r4d_silu_mul_bf16(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream) {
return silu_mul_emit(gu, y, M, n, gu_ld, y_ld, stream, false);
}
// GELU-gated product, the gemma FFN activation. Same shape and gate order as silu_mul; tanh GELU to
// match ref_gelu_mul's act_gelu_tanh.
extern "C" int r4d_gelu_mul_bf16(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream) {
static int gelu_mul_emit(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream,
bool chain) {
if (!gu || !y) return -1;
if (M <= 0 || n <= 0) return -2;
if (!gu_ld) gu_ld = 2 * n;
if (!y_ld) y_ld = n;
hipLaunchKernelGGL(gelu_mul_kernel, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream,
(const uint16_t*)gu, (uint16_t*)y, (uint32_t)n,
(uint32_t)gu_ld, (uint32_t)y_ld);
#define R4D_GM(K_) \
hipLaunchKernelGGL(K_, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream, \
(const uint16_t*)gu, (uint16_t*)y, (uint32_t)n, (uint32_t)gu_ld, \
(uint32_t)y_ld)
if (chain) R4D_GM(r4dchain_gelu_mul);
else R4D_GM(gelu_mul_kernel);
#undef R4D_GM
return hipGetLastError() == hipSuccess ? 0 : -4;
}
extern "C" int r4d_gelu_mul_bf16(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream) {
return gelu_mul_emit(gu, y, M, n, gu_ld, y_ld, stream, false);
}
// `n_rope_heads` is n_head + n_head_kv: the Q heads and the K heads of a fused projection, or just
// the one buffer's heads when q and k are separate tensors (which is what the gated block does).
extern "C" int r4d_rope_bf16(long qkv, long pos, int M, int head_dim, int n_rope_heads,
@@ -1295,7 +1424,8 @@ extern "C" int r4d_rope_bf16(long qkv, long pos, int M, int head_dim, int n_rope
static int rope_launch(long qkv, long pos, int M, int head_dim, int n_rope_heads, int rot, int den,
int gptj, float theta, float rscale, int row_pitch, int mode,
const int* sec, int n_sec, int plane, RadYarn yarn, long stream,
int nfreq = -1, RadLlama3 l3 = RadLlama3{}, bool rows_form = false) {
int nfreq = -1, RadLlama3 l3 = RadLlama3{}, bool rows_form = false,
bool chain = false) {
if (!qkv || !pos) return -1;
if (M <= 0 || head_dim <= 0 || n_rope_heads <= 0) return -2;
if (rot <= 0 || rot > head_dim || (rot & 1)) return -3;
@@ -1318,11 +1448,15 @@ static int rope_launch(long qkv, long pos, int M, int head_dim, int n_rope_heads
s0, s1, s2, n_sec, (uint32_t)plane, yarn, l3);
return hipGetLastError() == hipSuccess ? 0 : -4;
}
hipLaunchKernelGGL(rope_kernel, dim3((uint32_t)M, gy), dim3(kThreads), 0, (hipStream_t)stream,
(uint16_t*)qkv, (const int32_t*)pos, (uint32_t)head_dim,
(uint32_t)n_rope_heads, (uint32_t)rot, (uint32_t)nfreq, (int)den, gptj, theta,
rscale,
(uint32_t)row_pitch, mode, s0, s1, s2, n_sec, (uint32_t)plane, yarn, l3);
#define R4D_ROPE(K_) \
hipLaunchKernelGGL(K_, dim3((uint32_t)M, gy), dim3(kThreads), 0, (hipStream_t)stream, \
(uint16_t*)qkv, (const int32_t*)pos, (uint32_t)head_dim, \
(uint32_t)n_rope_heads, (uint32_t)rot, (uint32_t)nfreq, (int)den, gptj, \
theta, rscale, (uint32_t)row_pitch, mode, s0, s1, s2, n_sec, \
(uint32_t)plane, yarn, l3)
if (chain) R4D_ROPE(r4dchain_rope);
else R4D_ROPE(rope_kernel);
#undef R4D_ROPE
return hipGetLastError() == hipSuccess ? 0 : -4;
}
@@ -1336,22 +1470,32 @@ extern "C" int r4d_rope_mc_bf16(long qkv, long pos, int M, int head_dim, int n_r
row_pitch, mode, sec, n_sec, plane, RadYarn{}, stream);
}
extern "C" int r4d_kv_store_bf16(long k, long v, long slot, long kv, int M, int head_dim,
int kv_heads, int block_size, int n_blocks,
int k_ld, int v_ld, long stream) {
static int kv_store_emit(long k, long v, long slot, long kv, int M, int head_dim, int kv_heads,
int block_size, int n_blocks, int k_ld, int v_ld, long stream,
bool chain) {
if (!k || !v || !slot || !kv) return -1;
if (M <= 0 || head_dim <= 0 || kv_heads <= 0 || block_size <= 0 || n_blocks <= 0) return -2;
if (!k_ld) k_ld = kv_heads * head_dim;
if (!v_ld) v_ld = kv_heads * head_dim;
/* The fp8 cache is a separate entry point (r4d_kv_store_fp8), selected by --kv-cache-dtype. */
hipLaunchKernelGGL(kv_store_kernel, dim3((uint32_t)M), dim3(kThreads), 0,
(hipStream_t)stream,
(const uint16_t*)k, (const uint16_t*)v, (const int32_t*)slot,
(uint16_t*)kv, (uint32_t)head_dim, (uint32_t)kv_heads,
(uint32_t)block_size, (uint32_t)n_blocks, (uint32_t)k_ld, (uint32_t)v_ld);
#define R4D_KVS(K_) \
hipLaunchKernelGGL(K_, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream, \
(const uint16_t*)k, (const uint16_t*)v, (const int32_t*)slot, \
(uint16_t*)kv, (uint32_t)head_dim, (uint32_t)kv_heads, \
(uint32_t)block_size, (uint32_t)n_blocks, (uint32_t)k_ld, (uint32_t)v_ld)
if (chain) R4D_KVS(r4dchain_kv_store);
else R4D_KVS(kv_store_kernel);
#undef R4D_KVS
return hipGetLastError() == hipSuccess ? 0 : -4;
}
extern "C" int r4d_kv_store_bf16(long k, long v, long slot, long kv, int M, int head_dim,
int kv_heads, int block_size, int n_blocks,
int k_ld, int v_ld, long stream) {
return kv_store_emit(k, v, slot, kv, M, head_dim, kv_heads, block_size, n_blocks, k_ld, v_ld,
stream, false);
}
// bf16 k and v in, E4M3 cache out. `k_scale` and `v_scale` are the reader's descales, so this
// divides by them; 1.0 is the vLLM default and what the attention kernels assume when their
// descale pointers are null.
@@ -2023,9 +2167,10 @@ static int binary(const RadArgs* a, int mul, RadStream s) {
(float)div);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
return r4d_binary_bf16(r4d_addr(A), r4d_addr(B), r4d_addr(Y), (int)M, (int)n, mul,
(int)a_ld, (int)b_ld, (int)y_ld,
(long)(uintptr_t)s) == 0 ? RAD_OK : RAD_E_DEVICE;
return binary_bf16_emit(r4d_addr(A), r4d_addr(B), r4d_addr(Y), (int)M, (int)n, mul,
(int)a_ld, (int)b_ld, (int)y_ld, (long)(uintptr_t)s,
r4d_geti_or(a, "chain", 0) != 0 && M <= 64) == 0 ? RAD_OK
: RAD_E_DEVICE;
}
extern "C" int r4d_rad_add(const RadArgs* a, RadStream s) { return binary(a, 0, s); }
@@ -2103,8 +2248,10 @@ extern "C" int r4d_rad_sigmoid(const RadArgs* a, RadStream s) {
if (x_ld < 0 || y_ld < 0) return R4D_NO(RAD_E_STRIDE);
if (M <= 0 || My < M) return R4D_NO(RAD_E_SHAPE);
return r4d_sigmoid_bf16(r4d_addr(x), r4d_addr(y), (int)M, (int)n, (int)x_ld, (int)y_ld,
(long)(uintptr_t)s) == 0 ? RAD_OK : RAD_E_DEVICE;
return sigmoid_emit(r4d_addr(x), r4d_addr(y), (int)M, (int)n, (int)x_ld, (int)y_ld,
(long)(uintptr_t)s, r4d_geti_or(a, "chain", 0) != 0 && M <= 64) == 0
? RAD_OK
: RAD_E_DEVICE;
}
/* ------------------------------------------------------------------ fill */
@@ -2206,8 +2353,10 @@ extern "C" int r4d_rad_silu_mul(const RadArgs* a, RadStream s) {
if (y_ld < 0 || gu_ld < 0) return R4D_NO(RAD_E_STRIDE);
if (M <= 0 || Mg < M) return R4D_NO(RAD_E_SHAPE);
return r4d_silu_mul_bf16(r4d_addr(gu), r4d_addr(y), (int)M, (int)n, (int)gu_ld, (int)y_ld,
(long)(uintptr_t)s) == 0 ? RAD_OK : RAD_E_DEVICE;
return silu_mul_emit(r4d_addr(gu), r4d_addr(y), (int)M, (int)n, (int)gu_ld, (int)y_ld,
(long)(uintptr_t)s,
r4d_geti_or(a, "chain", 0) != 0 && M <= 1024) == 0 ? RAD_OK
: RAD_E_DEVICE;
}
/* ------------------------------------------------------------------ gelu_mul */
@@ -2227,8 +2376,8 @@ extern "C" int r4d_rad_gelu_mul(const RadArgs* a, RadStream s) {
if (y_ld < 0 || gu_ld < 0) return R4D_NO(RAD_E_STRIDE);
if (M <= 0 || Mg < M) return R4D_NO(RAD_E_SHAPE);
const int rc = r4d_gelu_mul_bf16(r4d_addr(gu), r4d_addr(y), (int)M, (int)n, (int)gu_ld, (int)y_ld,
(long)(uintptr_t)s);
const int rc = gelu_mul_emit(r4d_addr(gu), r4d_addr(y), (int)M, (int)n, (int)gu_ld, (int)y_ld,
(long)(uintptr_t)s, r4d_geti_or(a, "chain", 0) != 0 && M <= 64);
return rc == 0 ? RAD_OK : RAD_E_DEVICE;
}
@@ -2324,7 +2473,7 @@ static int rope_emit(const RadArgs* a, RadStream s, bool rows_form) {
return rope_launch(r4d_addr(qkv), r4d_addr(pos), (int)Mp, (int)head_dim,
(int)(n_head + n_head_kv), (int)rot_l, (int)den, gptj, theta, rscale,
(int)pitch, mc, sec, n_sec, plane, yarn, (long)(uintptr_t)s,
(int)nfreq, l3, rows_form) == 0
(int)nfreq, l3, rows_form, r4d_geti_or(a, "chain", 0) != 0 && Mp <= 64) == 0
? RAD_OK
: RAD_E_DEVICE;
}
@@ -2350,7 +2499,9 @@ struct KvStoreGeom {
long long head_dim, block_size, kv_heads, n_blocks, k_ld, v_ld, T;
};
static int kv_store_geom(const RadArgs* a, KvStoreGeom* g, long long row_per_hd16 = 32) {
/* A token's row in a (block, head) page is head_dim * row_per_hd16 / 16 + row_extra elements. */
static int kv_store_geom(const RadArgs* a, KvStoreGeom* g, long long row_per_hd16 = 32,
long long row_extra = 0) {
g->k = r4d_opt(a, KS_K);
g->v = r4d_opt(a, KS_V);
g->sm = r4d_opt(a, KS_SLOT);
@@ -2369,7 +2520,7 @@ static int kv_store_geom(const RadArgs* a, KvStoreGeom* g, long long row_per_hd1
* disagrees. */
if (g->kv->rank != 4) return R4D_NO(RAD_E_SHAPE);
if (g->kv->shape[1] != g->kv_heads || g->kv->shape[2] != g->block_size ||
g->kv->shape[3] != g->head_dim * row_per_hd16 / 16) return R4D_NO(RAD_E_SHAPE);
g->kv->shape[3] != g->head_dim * row_per_hd16 / 16 + row_extra) return R4D_NO(RAD_E_SHAPE);
g->n_blocks = g->kv->shape[0];
const long long lane = g->kv_heads * g->head_dim;
@@ -2388,10 +2539,11 @@ extern "C" int r4d_rad_kv_store(const RadArgs* a, RadStream s) {
if (rc != RAD_OK) return rc;
R4D_NEED(dt_bf16(g.kv));
return r4d_kv_store_bf16(r4d_addr(g.k), r4d_addr(g.v), r4d_addr(g.sm), r4d_addr(g.kv),
(int)g.T, (int)g.head_dim, (int)g.kv_heads, (int)g.block_size,
(int)g.n_blocks, (int)g.k_ld, (int)g.v_ld,
(long)(uintptr_t)s) == 0 ? RAD_OK : RAD_E_DEVICE;
return kv_store_emit(r4d_addr(g.k), r4d_addr(g.v), r4d_addr(g.sm), r4d_addr(g.kv),
(int)g.T, (int)g.head_dim, (int)g.kv_heads, (int)g.block_size,
(int)g.n_blocks, (int)g.k_ld, (int)g.v_ld, (long)(uintptr_t)s,
r4d_geti_or(a, "chain", 0) != 0 && g.T <= 64) == 0 ? RAD_OK
: RAD_E_DEVICE;
}
/* The E4M3 store. `k_scale` and `v_scale` are the attention side's descales (one per sequence and
@@ -2415,42 +2567,35 @@ extern "C" int r4d_rad_kv_store_fp8(const RadArgs* a, RadStream stream) {
(float)ks, (float)vs, stream) == 0 ? RAD_OK : RAD_E_DEVICE;
}
/* The fp8_v4 and k6_v4 stores: the cache's last extent is a token's row, 25 or 22 * head_dim / 16
* bytes. */
static int kv_store_q4(const RadArgs* a, RadStream stream, int k6) {
const long long r16 = k6 ? 22 : 25;
/* The int8 store: the cache's last extent is a token's row, 2 * head_dim + 4 bytes. A workgroup is
* a head's dims rounded up to whole waves. */
extern "C" int r4d_rad_kv_store_i8(const RadArgs* a, RadStream stream) {
KvStoreGeom g{};
const int rc = kv_store_geom(a, &g, r16);
const int rc = kv_store_geom(a, &g, 32, 4);
if (rc != RAD_OK) return rc;
if (g.kv->dtype != RAD_F8E4M3) return R4D_NO(RAD_E_DTYPE);
if (g.head_dim % 32) return R4D_NO(RAD_E_SHAPE);
const double ks = r4d_getf(a, "k_scale", 1.0);
const double vs = r4d_getf(a, "v_scale", 1.0);
if (!(ks > 0.0) || !(vs > 0.0)) return R4D_NO(RAD_E_INVAL);
long long k_ld = g.k_ld ? g.k_ld : g.kv_heads * g.head_dim;
long long v_ld = g.v_ld ? g.v_ld : g.kv_heads * g.head_dim;
if (k6)
return R4D_EMIT(stream, kv_store_fp8v4_kernel<1>, dim3((uint32_t)g.T), dim3(kThreads), 0,
(const uint16_t*)r4d_addr(g.k), (const uint16_t*)r4d_addr(g.v),
(const int32_t*)r4d_addr(g.sm), (uint8_t*)r4d_addr(g.kv),
(uint32_t)g.head_dim, (uint32_t)g.kv_heads, (uint32_t)g.block_size,
(uint32_t)g.n_blocks, (uint32_t)k_ld, (uint32_t)v_ld, (float)(1.0 / ks),
(float)(1.0 / vs), (uint32_t)(g.head_dim * r16 / 16)) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
return R4D_EMIT(stream, kv_store_fp8v4_kernel<0>, dim3((uint32_t)g.T), dim3(kThreads), 0,
(const uint16_t*)r4d_addr(g.k), (const uint16_t*)r4d_addr(g.v),
if (g.kv->dtype != RAD_I8) return R4D_NO(RAD_E_DTYPE);
if (g.head_dim % 4 || g.head_dim > 1024) return R4D_NO(RAD_E_SHAPE);
const long long k_ld = g.k_ld ? g.k_ld : g.kv_heads * g.head_dim;
const long long v_ld = g.v_ld ? g.v_ld : g.kv_heads * g.head_dim;
const uint32_t thr = (uint32_t)((g.head_dim + 31) / 32 * 32);
const bool chain = r4d_geti_or(a, "chain", 0) != 0 && g.T <= 64;
return R4D_EMIT(stream, (chain ? r4dchain_kv_store_i8 : kv_store_i8_kernel),
dim3((uint32_t)g.T, (uint32_t)g.kv_heads), dim3(thr),
0, (const uint16_t*)r4d_addr(g.k), (const uint16_t*)r4d_addr(g.v),
(const int32_t*)r4d_addr(g.sm), (uint8_t*)r4d_addr(g.kv),
(uint32_t)g.head_dim, (uint32_t)g.kv_heads, (uint32_t)g.block_size,
(uint32_t)g.n_blocks, (uint32_t)k_ld, (uint32_t)v_ld, (float)(1.0 / ks),
(float)(1.0 / vs), (uint32_t)(g.head_dim * r16 / 16)) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
(uint32_t)g.n_blocks, (uint32_t)k_ld, (uint32_t)v_ld) == RAD_OK ? RAD_OK
: RAD_E_DEVICE;
}
extern "C" int r4d_rad_kv_store_fp8v4(const RadArgs* a, RadStream stream) { return kv_store_q4(a, stream, 0); }
extern "C" int r4d_rad_kv_store_k6v4(const RadArgs* a, RadStream stream) { return kv_store_q4(a, stream, 1); }
/* The store of a streamed group: kv_store's geometry and conversion (E4M3, or bf16 with no
* descales), plus the residency map `res` (read, never written: residency is the resolve's) and the
* host mirror `host`. Both are one block a row of the same byte pitch. State faults are guarded,
* not refused (see kv_store_streamed_kernel). */
static int kv_store_streamed_i8(const RadArgs* a, RadStream stream);
extern "C" int r4d_rad_kv_store_streamed(const RadArgs* a, RadStream stream) {
const RadTensor* kvt = r4d_opt(a, 3);
if (kvt && kvt->dtype == RAD_I8) return kv_store_streamed_i8(a, stream);
KvStoreGeom g{};
const int rc = kv_store_geom(a, &g);
if (rc != RAD_OK) return rc;
@@ -2501,6 +2646,36 @@ extern "C" int r4d_rad_kv_store_streamed(const RadArgs* a, RadStream stream) {
k_inv, v_inv) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
}
/* The same into the int8 cache: kv_store_i8's workgroup, a head's dims one a lane, and a row of
* 2 * head_dim + 4 bytes in both planes. */
static int kv_store_streamed_i8(const RadArgs* a, RadStream stream) {
KvStoreGeom g{};
const int rc = kv_store_geom(a, &g, 32, 4);
if (rc != RAD_OK) return rc;
if (g.head_dim % 4 || g.head_dim > 1024) return R4D_NO(RAD_E_SHAPE);
const RadTensor* res = r4d_opt(a, 4);
const RadTensor* host = r4d_opt(a, 5);
if (!res || !host) return R4D_NO(RAD_E_INVAL);
if (res->dtype != RAD_I32 || !r4d_contig(res)) return R4D_NO(RAD_E_STRIDE);
if (host->dtype != RAD_U8 || !r4d_contig(host) || host->rank != 2)
return R4D_NO(RAD_E_STRIDE);
const long long row = g.kv_heads * g.block_size * (2 * g.head_dim + 4);
const long long blocks = host->shape[0], slots = g.kv->shape[0];
if (blocks <= 0 || slots <= 0 || row <= 0) return R4D_NO(RAD_E_SHAPE);
if (host->shape[1] != row || host->stride[0] != row) return R4D_NO(RAD_E_STRIDE);
if (r4d_numel(res) < blocks) return R4D_NO(RAD_E_SHAPE);
const long long k_ld = g.k_ld ? g.k_ld : g.kv_heads * g.head_dim;
const long long v_ld = g.v_ld ? g.v_ld : g.kv_heads * g.head_dim;
return R4D_EMIT(stream, kv_store_streamed_i8_kernel, dim3((uint32_t)g.T, (uint32_t)g.kv_heads),
dim3((uint32_t)((g.head_dim + 31) / 32 * 32)), 0,
(const uint16_t*)r4d_addr(g.k), (const uint16_t*)r4d_addr(g.v),
(const int32_t*)r4d_addr(g.sm), (uint8_t*)r4d_addr(g.kv),
(const int32_t*)r4d_addr(res), (uint8_t*)r4d_addr(host),
(uint32_t)g.head_dim, (uint32_t)g.kv_heads, (uint32_t)g.block_size,
(uint32_t)blocks, (uint32_t)slots, (uint32_t)k_ld,
(uint32_t)v_ld) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
}
/* ------------------------------------------------------------------ logits_gemm
*
* gemm_nt's arithmetic with `n_vocab`/`n_embd` for N/K, but writing f32 (see the kernel above).