ops: attention sinks and LiLiCorr's correlator

For NVIDIA's LiLiCorr drafter:
- attn_paged_sink: attn_paged with a learned logit a query head beside the keys, in libref,
  libavx and libr4d (the windowed kernels at h128, gqa 8 and 16)
- sample_logprobs without params writes every row (a drafter's candidate logprobs)
- lili_cand, lili_attn, lili_factor, lili_walk: the correlator's candidates and features, its
  attention over the candidate lattice, the factor input and the greedy walk

ref_test, avx_check_hand and r4d_emu cases; docs/OPS.md.
This commit is contained in:
keptn42 committed 2026-11-04 22:08:52 +01:00
1 parent 44490c859a
commit 3b278dcec3
26 files changed
+1904 -55

No files matched your search

+2
View File
@@ -196,6 +196,8 @@ set(R4D_UNITS
"r4d_rowtopk_bf16|"
# The vocab-sharded draft head's cross-rank top-1, one id a row.
"r4d_pair_argmax_f32|"
# LiLiCorr's correlator: candidates, lattice attention, factor input, walk.
"r4d_lili|"
# Gemma 4's sandwich-norm joins (norm_add_norm, norm_add_norm_route, join_add_scale) and
# expert_scale, each one kernel for a chain of small ones.
"r4d_norm_join_bf16|"
+49 -7
View File
@@ -28,6 +28,10 @@
* a block-table entry < 0 is a missing block: its keys are not visible
* no visible key at all -> a zero row
* k_descale / v_descale per (sequence, kv head), absent = 1
* attn_paged_sink: `sinks` [query heads] f32, one logit a head in the scaled-score domain, in
* the softmax's maximum and denominator and with no value (LiLiCorr's drafter). Folded in where
* a row is normalised: the unsplit kernel's store and the merge. The WMMA kernel takes none,
* so a sink launch stays scalar.
*
* Paged layout: kv_cache[n_blocks, kv_heads, 16, 2*head_dim], K then V per slot, so one block of
* one KV head is a contiguous run of 16 * 2 * head_dim elements; a key tile is one or two runs,
@@ -194,6 +198,7 @@ struct AttnArgs {
const float* vdes;
uint16_t* out; long long o_rs, o_hs;
float* part; /* split-KV partials, or null */
const float* sinks; /* [query heads] or null */
int* tinfo; /* [tile] = (first row, positions) */
float qscale; /* softmax scale * log2(e) */
int max_blocks, n_seq, q_len, tps, kv_heads, causal, window, n_split;
@@ -484,6 +489,21 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
if (!val[i]) continue;
const int r = wave * RW + i, p = r / GQA, g = r - p * GQA;
if (A.n_split == 1) {
/* A sink joins the row's max and sum with no value (log2 domain, as m and l). */
if (A.sinks && m[i] != -__builtin_inff()) {
const float s2 = A.sinks[kh * GQA + g] * 1.4426950408889634f;
if (s2 > m[i]) {
const float corr = ex2(m[i] - s2);
#pragma unroll
for (int c = 0; c < NC; ++c)
#pragma unroll
for (int e = 0; e < 4; ++e) o[i][c][e] *= corr;
l[i] = l[i] * corr + 1.0f;
m[i] = s2;
} else {
l[i] += ex2(s2 - m[i]);
}
}
const float inv = l[i] > 0.0f ? vds / l[i] : 0.0f;
uint16_t* op = A.out + (long long)(tok0 + p) * A.o_rs + (long long)(kh * GQA + g) * A.o_hs;
#pragma unroll
@@ -922,8 +942,11 @@ __device__ __forceinline__ void merge_body(AttnArgs A) {
for (int c = 0; c < NC; ++c) *(uint2*)(op + c * 128 + lane * 4) = make_uint2(0u, 0u);
continue;
}
const float w0 = ex2(m0 - mt), w1 = ex2(m1 - mt); /* an empty split: ex2(-inf) = 0 */
const float lt = wave_sum(__builtin_fmaf(l0, w0, l1 * w1));
/* A sink joins the max and the sum with no value (log2 domain). */
const float s2 = A.sinks ? A.sinks[kh * GQA + g] * 1.4426950408889634f : ninf;
const float ms = __builtin_fmaxf(mt, s2);
const float w0 = ex2(m0 - ms), w1 = ex2(m1 - ms); /* an empty split: ex2(-inf) = 0 */
const float lt = wave_sum(__builtin_fmaf(l0, w0, l1 * w1)) + (A.sinks ? ex2(s2 - ms) : 0.0f);
const float inv = lt > 0.0f ? 1.0f / lt : 0.0f;
float acc[NC][4];
#pragma unroll
@@ -1051,9 +1074,11 @@ __global__ __launch_bounds__(kThreads) void r4dchain_merge_window(AttnArgs A) {
}
template <int HD, int GQA, bool KV8, bool TREE = false, bool SHARED = false, bool FWD = false>
static int launch_attn(const RadArgs* a, RadStream stream) {
static int launch_attn(const RadArgs* a, RadStream stream, bool sink = false) {
constexpr int R = Geo<HD>::R, BK = Geo<HD>::BK, BQ = R / GQA, ESZ = KV8 ? 1 : 2;
if (!a || a->n_t < (TREE ? 9 : 8)) return RAD_E_INVAL;
if (!a || a->n_t < (TREE || sink ? 9 : 8)) return RAD_E_INVAL;
/* attn_paged_sink keeps its output where the tree operand would be: never both. */
if (TREE && sink) return RAD_E_INVAL;
/* attn_paged_qrow_tree's ninth operand: one int32 a query row (docs/OPS.md). Causal only. */
const RadTensor* tp = TREE ? rad_arg_in(a, 8) : nullptr;
if (TREE && (!tp || !tp->data)) return RAD_E_INVAL;
@@ -1066,8 +1091,9 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
const RadTensor* kd = rad_arg_in(a, 4);
const RadTensor* vd = rad_arg_in(a, 5);
const RadTensor* cu = rad_arg_in(a, 6);
const RadTensor* o = rad_arg_in(a, 7);
if (!q || !kv || !bt || !su || !o) return RAD_E_INVAL;
const RadTensor* sk = sink ? rad_arg_in(a, 7) : nullptr;
const RadTensor* o = rad_arg_in(a, sink ? 8 : 7);
if (!q || !kv || !bt || !su || !o || (sink && !sk)) return RAD_E_INVAL;
if (rad_args_geti_or(a, "head_dim", HD) != HD || rad_args_geti_or(a, "gqa", GQA) != GQA ||
rad_args_geti_or(a, "block_size", 16) != 16)
@@ -1083,6 +1109,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
if (kv->dtype != (uint32_t)(KV8 ? RAD_F8E4M3 : RAD_BF16)) return RAD_E_DTYPE;
if (bt->dtype != RAD_I32 || su->dtype != RAD_I32 || (cu && cu->dtype != RAD_I32)) return RAD_E_DTYPE;
if ((kd && kd->dtype != RAD_F32) || (vd && vd->dtype != RAD_F32)) return RAD_E_DTYPE;
if (sk && sk->dtype != RAD_F32) return RAD_E_DTYPE;
/* the cache: [n_blocks, kv_heads, 16, 2 * head_dim], slots dense */
if (kv->rank != 4 || kv->shape[2] != 16 || kv->shape[3] != 2 * HD) return RAD_E_SHAPE;
@@ -1090,6 +1117,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
const int64_t kvh = kv->shape[1];
if (kvh <= 0 || kvh > 65535) return RAD_E_SHAPE;
const int64_t qh = kvh * GQA;
if (sk && rad_tensor_numel(sk) != qh) return RAD_E_SHAPE;
int64_t rows = 0, q_rs = 0, q_hs = 0, orows = 0, o_rs = 0, o_hs = 0;
if (!head_view(q, qh, HD, &rows, &q_rs, &q_hs) || !head_view(o, qh, HD, &orows, &o_rs, &o_hs))
@@ -1154,7 +1182,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
rows >= n_seq * (kWmmaMinQ / 2);
const bool aligned = rad_ptr_aligned(q->data, 16) && rad_ptr_aligned(o->data, 16) &&
(q_rs | q_hs | o_rs | o_hs) % 8 == 0;
if (shaped && aligned) {
if (shaped && aligned && !sk) {
constexpr int BQW = WGeo<HD>::R / GQA;
int64_t ntw = 0, tpsw = 0;
if (!cu) {
@@ -1220,6 +1248,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
A.vdes = vd ? (const float*)vd->data : nullptr;
A.out = (uint16_t*)o->data; A.o_rs = o_rs; A.o_hs = o_hs;
A.part = ns > 1 ? (float*)a->scratch : nullptr;
A.sinks = sk ? (const float*)sk->data : nullptr;
A.tinfo = ns > 1 ? (int*)((char*)a->scratch + part_bytes<HD>()) : nullptr;
const double scale = rad_args_getf_or(a, "scale", 1.0 / __builtin_sqrt((double)HD));
A.qscale = (float)(scale * 1.4426950408889634);
@@ -1311,6 +1340,19 @@ R4D_WIN_ANCHOR_DEF(256, 1) R4D_WIN_ANCHOR_DEF(256, 2) R4D_WIN_ANCHOR_DEF(256, 4)
R4D_WIN_ANCHOR_DEF(512, 1) R4D_WIN_ANCHOR_DEF(512, 2) R4D_WIN_ANCHOR_DEF(512, 4) R4D_WIN_ANCHOR_DEF(512, 8)
#undef R4D_WIN_ANCHOR_DEF
/* attn_paged_sink: LiLiCorr's drafter, 32 query heads over 2 KV heads at head_dim 128 (16 over 1
* at TP 2: gqa 16 either way; 8 over its replicated head at TP 4: gqa 8), non-causal over its
* block, a 1024-token window. */
#define R4D_WINSINK_DEF(HD, G) \
int r4d_rad_attn_window_sink_h##HD##_gqa##G##_bf16(const RadArgs* a, RadStream s) { \
return r4d_win::launch_attn<HD, G, false>(a, s, true); \
} \
int r4d_rad_attn_window_sink_h##HD##_gqa##G##_fp8(const RadArgs* a, RadStream s) { \
return r4d_win::launch_attn<HD, G, true>(a, s, true); \
}
R4D_WINSINK_DEF(128, 8) R4D_WINSINK_DEF(128, 16)
#undef R4D_WINSINK_DEF
/* Called at declare with no tensors. Partials are capped at kPartBudget (tile, head, split)
* triples for any batch, so the size is a constant. */
int64_t r4d_rad_attn_window_h128_scratch(const RadArgs*) { return r4d_win::scratch_need<128>(); }
+421
View File
@@ -0,0 +1,421 @@
/* r4d_lili.hip -- LiLiCorr's candidate correlator (libref/ref_lili.cpp has the contracts):
* lili_cand, lili_attn, lili_factor and lili_walk, at a draft step's size (seven positions of eight
* candidates a sequence, a 1024-wide lattice). Each is a few microseconds of a draft step, so the
* kernels are plain: one workgroup a row (lili_cand, lili_factor), a (sequence, head) pair
* (lili_attn) or a sequence (lili_walk), shared memory and serial loops where a step is short, f32
* arithmetic in libref's order and one rounding to bf16 at each store.
*
* Activations are bf16; lili_cand's statistics, LayerNorm and up projection are f32 and its slot and
* rank tables bf16; lili_attn's biases f32. Rows may have their own pitch, the last axis dense.
* Nothing here allocates or synchronises (docs/PLUGIN.md §5).
*/
#include <hip/hip_runtime.h>
#include <cmath>
#include <cstdint>
#include "r4d_plugin.h"
#include "r4d_launch.h"
#include "rad_plugin.h"
namespace r4d_lili {
constexpr int kThr = 256;
constexpr int kMaxK = 64; /* candidates a position (lili_cand, lili_walk) */
constexpr int kMaxW = 16; /* ranks (lili_cand) */
constexpr int kMaxL = 64; /* lattice rows a sequence (lili_attn) */
constexpr int kMaxHd = 128; /* head width (lili_attn) */
constexpr int kMaxWalk = 512; /* steps * k * k scores a sequence (lili_walk) */
__device__ __forceinline__ float bf(uint16_t v) { return __uint_as_float((uint32_t)v << 16); }
__device__ __forceinline__ uint16_t tobf(float f) {
uint32_t u = __float_as_uint(f);
if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t)((u >> 16) | 0x40u); /* NaN stays NaN */
u += 0x7fffu + ((u >> 16) & 1u);
return (uint16_t)(u >> 16);
}
/* ------------------------------------------------------------------ lili_cand */
struct CandArgs {
const float* st; long long st_rs;
const uint16_t* pas; long long pas_rs;
const float *lnw, *lnb, *upw, *upb;
const uint16_t *slot, *rank;
int* cand; long long cand_rs;
uint16_t* feat; long long feat_rs;
uint16_t* base; long long base_rs;
int k, row_k, world, steps, n;
float eps;
};
__global__ __launch_bounds__(kThr) void cand_kernel(CandArgs A) {
__shared__ float s_y[kMaxK][5];
const long long m = blockIdx.x;
const int k = A.k, w = 2 + 2 * A.row_k;
if (threadIdx.x == 0) {
const float* row = A.st + m * A.st_rs;
float mx = -__builtin_inff();
for (int r = 0; r < A.world; ++r) mx = __builtin_fmaxf(mx, row[r * w]);
float sum = 0.0f;
for (int r = 0; r < A.world; ++r)
if (row[r * w] > -__builtin_inff()) sum += row[r * w + 1] * expf(row[r * w] - mx);
const float lse = mx + logf(sum);
/* A world-way merge of lists sorted by value, ties to the lower id. */
int at[kMaxW];
for (int r = 0; r < A.world; ++r) at[r] = 0;
float lp[kMaxK];
for (int j = 0; j < k; ++j) {
int br = -1, bid = 0;
float bv = 0.0f;
for (int r = 0; r < A.world; ++r) {
while (at[r] < k && (int)row[r * w + 2 + 2 * at[r]] < 0) ++at[r];
if (at[r] >= k) continue;
const float v = row[r * w + 3 + 2 * at[r]];
const int id = (int)row[r * w + 2 + 2 * at[r]];
if (br < 0 || v > bv || (v == bv && id < bid)) { br = r; bv = v; bid = id; }
}
lp[j] = br < 0 ? -1e4f : bv - lse;
if (br >= 0) ++at[br];
A.cand[m * A.cand_rs + j] = br < 0 ? 0 : bid;
}
for (int j = 0; j < k; ++j) {
const float f[5] = { lp[j], expf(lp[j]), lp[j] - lp[0], (float)j / (float)(k - 1),
j == 0 ? 1.0f : 0.0f };
float mean = 0.0f;
for (int i = 0; i < 5; ++i) mean += f[i];
mean /= 5.0f;
float var = 0.0f;
for (int i = 0; i < 5; ++i) var += (f[i] - mean) * (f[i] - mean);
var /= 5.0f;
const float rs = 1.0f / __builtin_sqrtf(var + A.eps);
for (int i = 0; i < 5; ++i) s_y[j][i] = (f[i] - mean) * rs * A.lnw[i] + A.lnb[i];
}
}
__syncthreads();
const int s = (int)(m % A.steps);
for (int c = (int)threadIdx.x; c < A.n; c += kThr) {
const float pv = bf(A.pas[m * A.pas_rs + c]) + bf(A.slot[(long long)s * A.n + c]);
float uw[5];
for (int i = 0; i < 5; ++i) uw[i] = A.upw[(long long)c * 5 + i];
const float ub = A.upb[c];
for (int j = 0; j < k; ++j) {
float u = ub;
for (int i = 0; i < 5; ++i) u += uw[i] * s_y[j][i];
const long long row = m * k + j;
A.feat[row * A.feat_rs + c] = tobf(u / (1.0f + expf(-u)));
A.base[row * A.base_rs + c] = tobf(pv + bf(A.rank[(long long)j * A.n + c]));
}
}
}
/* ------------------------------------------------------------------ lili_attn */
struct AttnArgs {
const uint16_t* qkv; long long q_rs;
const float *rel, *same; int rel_w;
uint16_t* out; long long o_rs;
int steps, k, nh, hd;
float scale;
};
__global__ __launch_bounds__(kThr) void attn_kernel(AttnArgs A) {
__shared__ uint16_t s_k[kMaxL][kMaxHd], s_v[kMaxL][kMaxHd];
__shared__ float s_q[kMaxHd], s_sc[kMaxL], s_sum;
const int L = A.steps * A.k, hd = A.hd, n = A.nh * hd, h = (int)blockIdx.y;
const long long r0 = (long long)blockIdx.x * L;
const int tid = (int)threadIdx.x;
for (int e = tid; e < L * hd; e += kThr) {
const int j = e / hd, d = e - j * hd;
const uint16_t* row = A.qkv + (r0 + j) * A.q_rs;
s_k[j][d] = row[n + h * hd + d];
s_v[j][d] = row[2 * n + h * hd + d];
}
const float sm = A.same[h];
for (int i = 0; i < L; ++i) {
__syncthreads();
if (tid < hd) s_q[tid] = bf(A.qkv[(r0 + i) * A.q_rs + h * hd + tid]);
__syncthreads();
const int si = i / A.k;
if (tid < L) {
float dot = 0.0f;
for (int d = 0; d < hd; ++d) dot += s_q[d] * bf(s_k[tid][d]);
const int sj = tid / A.k;
float b = A.rel[(long long)h * A.rel_w + (si - sj + A.steps)];
if (si == sj) b += sm;
s_sc[tid] = dot * A.scale + b;
}
__syncthreads();
if (tid == 0) {
float mx = s_sc[0];
for (int j = 1; j < L; ++j) mx = __builtin_fmaxf(mx, s_sc[j]);
float sum = 0.0f;
for (int j = 0; j < L; ++j) { s_sc[j] = expf(s_sc[j] - mx); sum += s_sc[j]; }
s_sum = sum;
}
__syncthreads();
if (tid < hd) {
float acc = 0.0f;
for (int j = 0; j < L; ++j) acc += s_sc[j] * bf(s_v[j][tid]);
A.out[(r0 + i) * A.o_rs + h * hd + tid] = tobf(acc / s_sum);
}
}
}
/* ------------------------------------------------------------------ lili_factor */
struct FactorArgs {
const uint16_t* h; long long h_rs;
const uint16_t* a; long long a_rs;
uint16_t* y; long long y_rs;
int L, n;
};
__global__ __launch_bounds__(kThr) void factor_kernel(FactorArgs A) {
const long long i = blockIdx.x;
const uint16_t* hr = A.h + i * A.h_rs;
const uint16_t* ar = A.a + (i / A.L) * A.a_rs;
uint16_t* yr = A.y + i * A.y_rs;
for (int c = (int)threadIdx.x; c < A.n; c += kThr) {
const float hv = bf(hr[c]), av = bf(ar[c]);
yr[c] = hr[c];
yr[A.n + c] = ar[c];
yr[2 * A.n + c] = tobf(hv * av);
}
}
/* ------------------------------------------------------------------ lili_walk */
struct WalkArgs {
const uint16_t* ed; long long e_rs;
const uint16_t* ao; long long a_rs;
const int* cand; long long c_rs;
int* tok; long long t_rs;
float* scores; /* [M, steps, k, k] dense, or null */
int steps, k, n;
float eps, scale;
};
__global__ __launch_bounds__(kThr) void walk_kernel(WalkArgs A) {
/* norms: [0, R) outgoing, [R, 2R) incoming, 2R the anchor's */
__shared__ float s_nrm[2 * kMaxK * 8 + 1];
__shared__ float s_sc[kMaxWalk];
const long long m = blockIdx.x;
const int k = A.k, R = A.steps * k, n = A.n, tid = (int)threadIdx.x;
const uint16_t* E = A.ed + m * R * A.e_rs;
const uint16_t* an = A.ao + m * A.a_rs;
for (int v = tid; v < 2 * R + 1; v += kThr) {
const uint16_t* p = v < 2 * R ? E + (long long)(v % R) * A.e_rs + (v < R ? 0 : n) : an;
float ss = 0.0f;
for (int c = 0; c < n; ++c) { const float x = bf(p[c]); ss += x * x; }
s_nrm[v] = __builtin_fmaxf(__builtin_sqrtf(ss), A.eps);
}
__syncthreads();
/* start (k entries, shared by every i) and the pairs: dots of the raw vectors over their norms */
const int npair = (A.steps - 1) * k * k;
for (int e = tid; e < k + npair; e += kThr) {
const uint16_t *x, *y;
float nx, ny;
if (e < k) {
x = an; nx = s_nrm[2 * R];
y = E + (long long)e * A.e_rs + n; ny = s_nrm[R + e];
} else {
const int q = e - k, s = q / (k * k) + 1, i = (q / k) % k, j = q % k;
x = E + (long long)((s - 1) * k + i) * A.e_rs; nx = s_nrm[(s - 1) * k + i];
y = E + (long long)(s * k + j) * A.e_rs + n; ny = s_nrm[R + s * k + j];
}
float d = 0.0f;
for (int c = 0; c < n; ++c) d += bf(x[c]) * bf(y[c]);
const float v = A.scale * (d / nx / ny);
if (e < k) {
for (int i = 0; i < k; ++i) s_sc[i * k + e] = v;
} else {
s_sc[k * k + (e - k)] = v;
}
}
__syncthreads();
if (tid == 0) {
int prev = 0;
for (int s = 0; s < A.steps; ++s) {
const float* row = s_sc + (s * k + prev) * k;
int best = 0;
for (int j = 1; j < k; ++j) if (row[j] > row[best]) best = j;
A.tok[m * A.t_rs + s] = A.cand[(m * A.steps + s) * A.c_rs + best];
prev = best;
}
}
if (A.scores)
for (int e = tid; e < R * k; e += kThr) A.scores[m * R * k + e] = s_sc[e];
}
/* A tensor read as [rows, cols] with a row pitch, last axis dense: the pitch, and the rows in *M. */
static long long pitch_of(const RadTensor* t, long long cols, long long* M) {
if (!t || cols <= 0) return -1;
if (t->rank == 1) {
if (t->stride[0] != 1 || t->shape[0] % cols) return -1;
*M = t->shape[0] / cols;
return cols;
}
const uint32_t r = t->rank;
if (r < 2 || t->stride[r - 1] != 1) return -1;
long long rows = 1;
for (uint32_t i = 0; i + 1 < r; ++i) rows *= t->shape[i];
if (t->shape[r - 1] == cols) {
for (uint32_t i = 0; i + 2 < r; ++i)
if (t->shape[i] > 1 && t->stride[i] != t->stride[i + 1] * t->shape[i + 1]) return -1;
*M = rows;
return t->stride[r - 2];
}
/* A flat [rows, a * cols] read at a narrower width is not a pitch. */
if (r == 2 && t->stride[0] == t->shape[1] && t->shape[1] % cols == 0) {
*M = rows * (t->shape[1] / cols);
return cols;
}
return -1;
}
static bool dense(const RadTensor* t) {
int64_t s = 1;
for (int i = (int)t->rank - 1; i >= 0; --i) {
if (t->shape[i] != 1 && t->stride[i] != s) return false;
s *= t->shape[i];
}
return true;
}
} /* namespace r4d_lili */
using namespace r4d_lili;
extern "C" {
int r4d_rad_lili_cand(const RadArgs* a, RadStream stream) {
if (!a || a->n_t < 11) return RAD_E_INVAL;
const RadTensor* t[11];
for (int i = 0; i < 11; ++i) if (!(t[i] = rad_arg_in(a, i))) return RAD_E_INVAL;
const long long k = rad_args_geti_or(a, "k", 0), world = rad_args_geti_or(a, "world", 1);
const long long steps = rad_args_geti_or(a, "steps", 0), n = rad_args_geti_or(a, "n", 0);
const long long rk = rad_args_geti_or(a, "row_k", k);
if (k < 2 || k > kMaxK || rk < k || world < 1 || world > kMaxW || steps < 1 || n <= 0)
return RAD_E_SHAPE;
if (t[0]->dtype != RAD_F32 || t[1]->dtype != RAD_BF16 || t[8]->dtype != RAD_I32 ||
t[9]->dtype != RAD_BF16 || t[10]->dtype != RAD_BF16 || t[6]->dtype != RAD_BF16 ||
t[7]->dtype != RAD_BF16)
return RAD_E_DTYPE;
for (int i = 2; i <= 5; ++i) if (t[i]->dtype != RAD_F32) return RAD_E_DTYPE;
for (int i = 2; i <= 7; ++i) if (!dense(t[i])) return RAD_E_STRIDE;
if (rad_tensor_numel(t[2]) < 5 || rad_tensor_numel(t[3]) < 5 || rad_tensor_numel(t[4]) < n * 5 ||
rad_tensor_numel(t[5]) < n || rad_tensor_numel(t[6]) < steps * n ||
rad_tensor_numel(t[7]) < k * n)
return RAD_E_SHAPE;
long long M = 0, Mp = 0, Mc = 0, Mf = 0, Mb = 0;
CandArgs A{};
A.st_rs = pitch_of(t[0], world * (2 + 2 * rk), &M);
A.pas_rs = pitch_of(t[1], n, &Mp);
A.cand_rs = pitch_of(t[8], k, &Mc);
A.feat_rs = pitch_of(t[9], n, &Mf);
A.base_rs = pitch_of(t[10], n, &Mb);
if (A.st_rs < 0 || A.pas_rs < 0 || A.cand_rs < 0 || A.feat_rs < 0 || A.base_rs < 0)
return RAD_E_STRIDE;
if (M <= 0 || Mp < M || Mc < M || Mf < M * k || Mb < M * k) return RAD_E_SHAPE;
A.st = (const float*)t[0]->data; A.pas = (const uint16_t*)t[1]->data;
A.lnw = (const float*)t[2]->data; A.lnb = (const float*)t[3]->data;
A.upw = (const float*)t[4]->data; A.upb = (const float*)t[5]->data;
A.slot = (const uint16_t*)t[6]->data; A.rank = (const uint16_t*)t[7]->data;
A.cand = (int*)t[8]->data; A.feat = (uint16_t*)t[9]->data;
A.base = (uint16_t*)t[10]->data;
A.k = (int)k; A.row_k = (int)rk; A.world = (int)world; A.steps = (int)steps; A.n = (int)n;
A.eps = (float)rad_args_getf_or(a, "eps", 1e-5);
R4D_KLAUNCH(R4D_KNAME(cand_kernel), dim3((unsigned)M), dim3(kThr), (hipStream_t)stream, A);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
int r4d_rad_lili_attn(const RadArgs* a, RadStream stream) {
if (!a || a->n_t < 4) return RAD_E_INVAL;
const RadTensor* q = rad_arg_in(a, 0);
const RadTensor* rel = rad_arg_in(a, 1);
const RadTensor* same = rad_arg_in(a, 2);
const RadTensor* o = rad_arg_in(a, 3);
if (!q || !rel || !same || !o) return RAD_E_INVAL;
const long long steps = rad_args_geti_or(a, "steps", 0), k = rad_args_geti_or(a, "k", 0);
const long long nh = rad_args_geti_or(a, "n_head", 0), hd = rad_args_geti_or(a, "head_dim", 0);
if (steps < 1 || k < 1 || nh < 1 || hd < 1 || hd > kMaxHd || steps * k > kMaxL)
return RAD_E_SHAPE;
if (q->dtype != RAD_BF16 || o->dtype != RAD_BF16 || rel->dtype != RAD_F32 ||
same->dtype != RAD_F32)
return RAD_E_DTYPE;
if (!dense(rel) || !dense(same)) return RAD_E_STRIDE;
const long long rw = rel->rank ? rel->shape[rel->rank - 1] : 0;
if (rw < 2 * steps - 1 || rad_tensor_numel(rel) < nh * rw || rad_tensor_numel(same) < nh)
return RAD_E_SHAPE;
const long long L = steps * k, n = nh * hd;
long long M = 0, Mo = 0;
AttnArgs A{};
A.q_rs = pitch_of(q, 3 * n, &M);
A.o_rs = pitch_of(o, n, &Mo);
if (A.q_rs < 0 || A.o_rs < 0) return RAD_E_STRIDE;
if (M <= 0 || M % L || Mo < M) return RAD_E_SHAPE;
A.qkv = (const uint16_t*)q->data; A.out = (uint16_t*)o->data;
A.rel = (const float*)rel->data; A.same = (const float*)same->data; A.rel_w = (int)rw;
A.steps = (int)steps; A.k = (int)k; A.nh = (int)nh; A.hd = (int)hd;
A.scale = (float)rad_args_getf_or(a, "scale", 1.0 / __builtin_sqrt((double)hd));
R4D_KLAUNCH(R4D_KNAME(attn_kernel), dim3((unsigned)(M / L), (unsigned)nh), dim3(kThr),
(hipStream_t)stream, A);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
int r4d_rad_lili_factor(const RadArgs* a, RadStream stream) {
if (!a || a->n_t < 3) return RAD_E_INVAL;
const RadTensor* h = rad_arg_in(a, 0);
const RadTensor* an = rad_arg_in(a, 1);
const RadTensor* y = rad_arg_in(a, 2);
if (!h || !an || !y) return RAD_E_INVAL;
if (h->dtype != RAD_BF16 || an->dtype != RAD_BF16 || y->dtype != RAD_BF16) return RAD_E_DTYPE;
const long long L = rad_args_geti_or(a, "L", 0), n = rad_args_geti_or(a, "n", 0);
if (L < 1 || n < 1) return RAD_E_SHAPE;
long long M = 0, Ma = 0, My = 0;
FactorArgs A{};
A.h_rs = pitch_of(h, n, &M);
A.a_rs = pitch_of(an, n, &Ma);
A.y_rs = pitch_of(y, 3 * n, &My);
if (A.h_rs < 0 || A.a_rs < 0 || A.y_rs < 0) return RAD_E_STRIDE;
if (M <= 0 || M % L || Ma < M / L || My < M) return RAD_E_SHAPE;
A.h = (const uint16_t*)h->data; A.a = (const uint16_t*)an->data; A.y = (uint16_t*)y->data;
A.L = (int)L; A.n = (int)n;
R4D_KLAUNCH(R4D_KNAME(factor_kernel), dim3((unsigned)M), dim3(kThr), (hipStream_t)stream, A);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
int r4d_rad_lili_walk(const RadArgs* a, RadStream stream) {
if (!a || a->n_t < 4) return RAD_E_INVAL;
const RadTensor* ed = rad_arg_in(a, 0);
const RadTensor* ao = rad_arg_in(a, 1);
const RadTensor* cd = rad_arg_in(a, 2);
const RadTensor* tk = rad_arg_in(a, 3);
const RadTensor* sc = a->n_t > 4 ? rad_arg_in(a, 4) : nullptr;
if (!ed || !ao || !cd || !tk) return RAD_E_INVAL;
const long long steps = rad_args_geti_or(a, "steps", 0), k = rad_args_geti_or(a, "k", 0);
const long long n = rad_args_geti_or(a, "n", 0);
if (steps < 1 || k < 1 || k > kMaxK || n < 1 || steps * k * k > kMaxWalk ||
steps * k > kMaxK * 8)
return RAD_E_SHAPE;
if (ed->dtype != RAD_BF16 || ao->dtype != RAD_BF16 || cd->dtype != RAD_I32 ||
tk->dtype != RAD_I32 || (sc && sc->dtype != RAD_F32))
return RAD_E_DTYPE;
if (sc && !dense(sc)) return RAD_E_STRIDE;
long long M = 0, Me = 0, Mc = 0, Mt = 0;
WalkArgs A{};
A.a_rs = pitch_of(ao, n, &M);
A.e_rs = pitch_of(ed, 2 * n, &Me);
A.c_rs = pitch_of(cd, k, &Mc);
A.t_rs = pitch_of(tk, steps, &Mt);
if (A.a_rs < 0 || A.e_rs < 0 || A.c_rs < 0 || A.t_rs < 0) return RAD_E_STRIDE;
if (M <= 0 || Me < M * steps * k || Mc < M * steps || Mt < M) return RAD_E_SHAPE;
if (sc && rad_tensor_numel(sc) < M * steps * k * k) return RAD_E_SHAPE;
A.ed = (const uint16_t*)ed->data; A.ao = (const uint16_t*)ao->data;
A.cand = (const int*)cd->data; A.tok = (int*)tk->data;
A.scores = sc ? (float*)sc->data : nullptr;
A.steps = (int)steps; A.k = (int)k; A.n = (int)n;
A.eps = (float)rad_args_getf_or(a, "eps", 1e-4);
A.scale = (float)rad_args_getf_or(a, "scale", 1.0);
R4D_KLAUNCH(R4D_KNAME(walk_kernel), dim3((unsigned)M), dim3(kThr), (hipStream_t)stream, A);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
} /* extern "C" */
+15
View File
@@ -169,6 +169,11 @@ int64_t r4d_rad_attn_qrow_h128_gqa8_scratch(const RadArgs*);
* row pitch, so they serve both attn_paged and attn_paged_qrow. */
int r4d_rad_attn_window_h128_gqa1_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h128_gqa1_fp8 (const RadArgs*, RadStream);
/* The same kernels under attn_paged_sink (a sink logit a query head). */
int r4d_rad_attn_window_sink_h128_gqa8_bf16 (const RadArgs*, RadStream);
int r4d_rad_attn_window_sink_h128_gqa8_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_window_sink_h128_gqa16_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_sink_h128_gqa16_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_window_h256_gqa1_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h256_gqa1_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_window_h256_gqa2_bf16(const RadArgs*, RadStream);
@@ -223,6 +228,11 @@ int r4d_rad_dflash_tree(const RadArgs*, RadStream);
/* The top-1 of a row of all-gathered (id, value) pairs, written one id a row: the vocab-sharded
* draft head's merge (r4d_pair_argmax_f32.hip). */
int r4d_rad_pair_argmax_f32(const RadArgs*, RadStream);
/* LiLiCorr's correlator (r4d_lili.hip). */
int r4d_rad_lili_cand (const RadArgs*, RadStream);
int r4d_rad_lili_attn (const RadArgs*, RadStream);
int r4d_rad_lili_factor(const RadArgs*, RadStream);
int r4d_rad_lili_walk (const RadArgs*, RadStream);
/* Gemma 4's two sandwich-norm joins, each one kernel for a chain of rmsnorm / add / mul_weight
* launches (r4d_norm_join_bf16.hip), and expert_scale, the embed_lookup + mul pair that scales
* each routing weight by its expert's scale (r4d_expert_scale_bf16.hip). */
@@ -977,6 +987,7 @@ int r4d_shape_sample_pick (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_sample_chain (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_attn_paged (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_attn_paged_sink (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_attn_paged_gate_quant(const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_qk_norm_rope_gate (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_qk_norm_rope_gate_kv_store(const RadParam*, int, int operand, RadOpdDesc*);
@@ -1013,6 +1024,10 @@ int r4d_shape_scale_rows (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_scale (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_row_topk_merge (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_pair_argmax (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_lili_cand (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_lili_attn (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_lili_factor (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_lili_walk (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_quant_act_i8 (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_had_quant_act_i8 (const RadParam*, int, int operand, RadOpdDesc*);
+122 -1
View File
@@ -137,6 +137,19 @@ static const RadOperandSpec oAttnPagedTree[] = {
{ "spec_parent", RAD_OPD_IN, 0 },
};
/* attn_paged with a sink logit a query head, its own name so no kernel ignorant of it is chosen. */
static const RadOperandSpec oAttnPagedSink[] = {
{ "q", RAD_OPD_IN, 0 },
{ "kv_cache", RAD_OPD_IN, 0 },
{ "block_table", RAD_OPD_IN, 0 },
{ "seqused", RAD_OPD_IN, 0 },
{ "k_descale", RAD_OPD_IN, 1 },
{ "v_descale", RAD_OPD_IN, 1 },
{ "cu_seqlens", RAD_OPD_IN, 1 },
{ "sinks", RAD_OPD_WEIGHT, 0 }, /* [n_head] f32 */
{ "out", RAD_OPD_OUT, 0 },
};
/* attn_paged with gate_quant_fp8 folded into its merge: attn_paged's parameters plus the two of
* gate_quant_fp8's that are not implied (its `n` is `head_dim`, its rows attn_paged's (token,
* head) pairs), then attn_paged's operands, the gate, the codes and the scales. `out` holds the
@@ -2477,7 +2490,7 @@ static const RadParamSpec pLogprobs[] = {
};
static const RadOperandSpec oLogprobs[] = {
{ "logits", RAD_OPD_IN, 0 },
{ "params", RAD_OPD_IN, 0 },
{ "params", RAD_OPD_IN, 1 }, /* absent: every row */
{ "stats", RAD_OPD_OUT, 0 },
};
static const RadOperandSpec oNarrow[] = {
@@ -2514,6 +2527,38 @@ static const RadOperandSpec oPairArgmax[] = {
{ "idx", RAD_OPD_OUT, 0 },
{ "val", RAD_OPD_OUT, 1 },
};
/* LiLiCorr's correlator (r4d_lili.hip; libref/ref_lili.cpp has the contracts). */
static const RadParamSpec pLiliCand[] = {
P_INT("M"), P_INT("k"), P_INT("world"), P_INT("steps"), P_INT("n"), P_STR("dtype"),
P_F64O("eps"), P_INTO("row_k"),
};
static const RadOperandSpec oLiliCand[] = {
{ "stats", RAD_OPD_IN, 0 }, { "pas", RAD_OPD_IN, 0 },
{ "ln_w", RAD_OPD_WEIGHT, 0 }, { "ln_b", RAD_OPD_WEIGHT, 0 },
{ "up_w", RAD_OPD_WEIGHT, 0 }, { "up_b", RAD_OPD_WEIGHT, 0 },
{ "slot", RAD_OPD_WEIGHT, 0 }, { "rank", RAD_OPD_WEIGHT, 0 },
{ "cand", RAD_OPD_OUT, 0 }, { "feat", RAD_OPD_OUT, 0 }, { "base", RAD_OPD_OUT, 0 },
};
static const RadParamSpec pLiliAttn[] = {
P_INT("M"), P_INT("steps"), P_INT("k"), P_INT("n_head"), P_INT("head_dim"), P_STR("dtype"),
P_F64O("scale"),
};
static const RadOperandSpec oLiliAttn[] = {
{ "qkv", RAD_OPD_IN, 0 }, { "rel", RAD_OPD_WEIGHT, 0 }, { "same", RAD_OPD_WEIGHT, 0 },
{ "out", RAD_OPD_OUT, 0 },
};
static const RadParamSpec pLiliFactor[] = { P_INT("M"), P_INT("L"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oLiliFactor[] = {
{ "h", RAD_OPD_IN, 0 }, { "a", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 },
};
static const RadParamSpec pLiliWalk[] = {
P_INT("M"), P_INT("steps"), P_INT("k"), P_INT("n"), P_STR("dtype"), P_F64O("eps"),
P_F64O("scale"),
};
static const RadOperandSpec oLiliWalk[] = {
{ "edges", RAD_OPD_IN, 0 }, { "aout", RAD_OPD_IN, 0 }, { "cand", RAD_OPD_IN, 0 },
{ "tokens", RAD_OPD_OUT, 0 }, { "scores", RAD_OPD_OUT, 1 },
};
static const RadParamSpec pNormJoin[] = {
P_INT("M"),
P_INT("n"),
@@ -3345,6 +3390,9 @@ static const RadOpSchema kSchemas[] = {
* attn_paged kernels read q dense and refuse a slice (RAD_E_STRIDE). Under this name the op
* resolves to a kernel that takes the slice or to nothing, and in the latter case the
* architecture lifts q into a dense block for attn_paged. */
SCHEMA("attn_paged_sink", pAttnPaged, oAttnPagedSink,
"attn_paged with `sinks` [n_head] f32: one logit a query head in the scaled-score "
"domain, in the softmax's maximum and denominator, with no value."),
SCHEMA("attn_paged_qrow", pAttnPaged, oAttnPaged,
"attn_paged, parameter for parameter and value for value, with q read at its own row "
"pitch: a column slice of a wider row -- the q part of a fused [q|k|v] projection -- read "
@@ -3819,6 +3867,16 @@ static const RadOpSchema kSchemas[] = {
"token order (with the shared arm folded when present) ARE the all-reduce's message, "
"and the gated residual's write follows the reduction. Both wires at two ranks, the "
"exact one at four."),
SCHEMA("lili_cand", pLiliCand, oLiliCand,
"LiLiCorr's candidates from the ranks' sample_logprobs rows, their rank features through "
"layernorm, the up projection and silu, and the lattice rows' additive part."),
SCHEMA("lili_attn", pLiliAttn, oLiliAttn,
"attention over each sequence's candidate lattice with a learned bias on slot distance "
"and the same slot."),
SCHEMA("lili_factor", pLiliFactor, oLiliFactor,
"[h | a | h * a] a lattice row, a its sequence's anchor."),
SCHEMA("lili_walk", pLiliWalk, oLiliWalk,
"LiLiCorr's normalised edge scores and their greedy walk."),
SCHEMA("pair_argmax", pPairArgmax, oPairArgmax,
"idx[m] = the id of the largest value among pairs[m, 0..n), each pair an int32 id in "
"the first f32 slot and its value in the second (row_topk's `pairs`, all-gathered): ties "
@@ -4304,6 +4362,8 @@ static const RadConstraint cAttnPrefillH64G4Bf16[] = {
C_GE("window", WMIN), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", KV), \
};
R4D_WIN_C(cWinH128G1Bf16, 128, 1, "bf16", 0) R4D_WIN_C(cWinH128G1Fp8, 128, 1, "fp8_e4m3", 0)
R4D_WIN_C(cWinH128G8Bf16, 128, 8, "bf16", 0) R4D_WIN_C(cWinH128G8Fp8, 128, 8, "fp8_e4m3", 0)
R4D_WIN_C(cWinH128G16Bf16, 128, 16, "bf16", 0) R4D_WIN_C(cWinH128G16Fp8, 128, 16, "fp8_e4m3", 0)
R4D_WIN_C(cWinH256G1Bf16, 256, 1, "bf16", 0) R4D_WIN_C(cWinH256G1Fp8, 256, 1, "fp8_e4m3", 0)
R4D_WIN_C(cWinH256G2Bf16, 256, 2, "bf16", 0) R4D_WIN_C(cWinH256G2Fp8, 256, 2, "fp8_e4m3", 0)
R4D_WIN_C(cWinH256G4Bf16, 256, 4, "bf16", 1) R4D_WIN_C(cWinH256G4Fp8, 256, 4, "fp8_e4m3", 1)
@@ -4786,6 +4846,14 @@ static const RadConstraint cNormJoinRoute[] = {
static const RadConstraint cPairArgmax[] = {
C_GE("n", 1), C_IN("dtype", "f32 fp32"),
};
static const RadConstraint cLiliCand[] = {
C_GE("k", 2), C_LE("k", 64), C_GE("world", 1), C_LE("world", 16), C_IN("dtype", "bf16"),
};
static const RadConstraint cLiliAttn[] = {
C_GE("head_dim", 1), C_LE("head_dim", 128), C_IN("dtype", "bf16"),
};
static const RadConstraint cLiliFactor[] = { C_IN("dtype", "bf16") };
static const RadConstraint cLiliWalk[] = { C_GE("k", 1), C_LE("k", 64), C_IN("dtype", "bf16") };
static const RadConstraint cExpertScale[] = {
C_GE("top_k", 1), C_LE("top_k", 64), C_GE("n_expert", 1), C_IN("dtype", "bf16"),
};
@@ -7371,6 +7439,24 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
R4D_WIN_PAIR(512, 16)
#undef R4D_WIN_PAIR
#undef R4D_WIN_ROW
/* attn_paged_sink: the windowed kernels with the sink folded in (LiLiCorr's drafter, h128 at gqa 16
* to TP 2, gqa 8 at TP 4). */
#define R4D_WINSINK_ROW(HD, G, KVN, KVS, C, KVDOC) \
{ "attn_window_sink_h" #HD "_gqa" #G "_" #KVN, "attn_paged_sink", "attn", \
"paged attention with a sink logit a head, any window, causal or not; split-KV with a merge", \
"head_dim " #HD ", " #G " queries per kv head, paged block 16, any q_len, varlen", \
"bf16 query, " KVDOC " kv cache, f32 sinks, bf16 out, f32 partials", \
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, \
ROW(C), NOTUNE, \
nullptr, nullptr, r4d_rad_attn_window_sink_h##HD##_gqa##G##_##KVS, \
r4d_rad_attn_window_h##HD##_scratch, \
nullptr, nullptr, \
r4d_shape_attn_paged_sink },
R4D_WINSINK_ROW(128, 8, bf16kv, bf16, cWinH128G8Bf16, "bf16")
R4D_WINSINK_ROW(128, 8, fp8kv, fp8, cWinH128G8Fp8, "fp8-e4m3 (per-(seq,head) descales)")
R4D_WINSINK_ROW(128, 16, bf16kv, bf16, cWinH128G16Bf16, "bf16")
R4D_WINSINK_ROW(128, 16, fp8kv, fp8, cWinH128G16Fp8, "fp8-e4m3 (per-(seq,head) descales)")
#undef R4D_WINSINK_ROW
/* DSpark's shared-anchor mask is intentionally a distinct op/kernel family, not an accepted
* `window` variation of attn_paged's per-query visibility. Its base/upper key bounds are tied to
@@ -9886,6 +9972,41 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
/* pair_argmax, the sandwich-norm joins and expert_scale. The joins and expert_scale have no
* opd_shape: libref has no row for them, and tests/r4d_emu holds their source to the replaced chain
* bit for bit on the host. pair_argmax is described as libref describes it. */
/* LiLiCorr's correlator: a few microseconds of a draft step each (r4d_lili.hip). */
{ "lili_cand_bf16", "lili_cand", "draft",
"LiLiCorr candidates: the ranks' logprob rows merged, five rank features through layernorm, "
"the up projection and silu, and the lattice rows' additive part",
"k <= 64, world <= 16; one workgroup a draft position",
"f32 stats, layernorm and up projection; bf16 activations, slot and rank tables; i32 cand",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cLiliCand), NOTUNE,
nullptr, nullptr, r4d_rad_lili_cand, nullptr, nullptr, nullptr,
r4d_shape_lili_cand },
{ "lili_attn_bf16", "lili_attn", "draft",
"LiLiCorr lattice attention with learned slot biases, no mask",
"steps * k <= 64, head_dim <= 128; one workgroup a (sequence, head)",
"bf16 qkv and out, f32 biases",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cLiliAttn), NOTUNE,
nullptr, nullptr, r4d_rad_lili_attn, nullptr, nullptr, nullptr,
r4d_shape_lili_attn },
{ "lili_factor_bf16", "lili_factor", "draft",
"LiLiCorr factor input [h | a | h * a]",
"any M and n; one workgroup a row",
"bf16",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cLiliFactor), NOTUNE,
nullptr, nullptr, r4d_rad_lili_factor, nullptr, nullptr, nullptr,
r4d_shape_lili_factor },
{ "lili_walk_bf16", "lili_walk", "draft",
"LiLiCorr normalised edge scores and their greedy walk",
"k <= 64, steps * k * k <= 512; one workgroup a sequence",
"bf16 edges and anchor, i32 cand and tokens, f32 scores",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cLiliWalk), NOTUNE,
nullptr, nullptr, r4d_rad_lili_walk, nullptr, nullptr, nullptr,
r4d_shape_lili_walk },
{ "pair_argmax_f32", "pair_argmax", "select",
"the top-1 of a row of (id, value) pairs, ties to the lower id, negative ids skipped",
"any M and n; one wave a row; the outputs one element a row at their own pitch",
+4 -3
View File
@@ -687,7 +687,7 @@ __global__ __launch_bounds__(kTopkThreads) void logprobs_kernel(
__shared__ float red[kTopkWaves];
const uint32_t m = blockIdx.x;
if (!(sp[m].flags & SP_LOGPROBS)) return;
if (sp && !(sp[m].flags & SP_LOGPROBS)) return; /* no params: every row */
const float* __restrict__ row = lg + (size_t)m * lg_ld;
float* __restrict__ out = st + (size_t)m * st_ld;
const uint32_t kk = k < n_vocab ? k : n_vocab;
@@ -1165,7 +1165,7 @@ static int r4d_sample_argmax_f32(long lg, long tok, int M, int n_vocab, int lg_l
static int r4d_sample_logprobs_f32(long lg, long params, long stats, int M, int n_vocab, int k,
int lg_ld, int st_ld, int vocab_off, long stream) {
if (!lg || !params || !stats) return R4D_NO(RAD_E_INVAL);
if (!lg || !stats) return R4D_NO(RAD_E_INVAL);
if (M <= 0 || n_vocab <= 0 || k <= 0) return R4D_NO(RAD_E_SHAPE);
if ((uint32_t)k > kMaxLogprobs) return R4D_NO(RAD_E_SHAPE);
if (!lg_ld) lg_ld = n_vocab;
@@ -1389,8 +1389,9 @@ extern "C" int r4d_rad_sample_logprobs(const RadArgs* a, RadStream s) {
const RadTensor* lg;
long long M = 0, nv = 0, ld = 0;
R4D_NEED(logits_of(a, &lg, &M, &nv, &ld));
/* Absent params: every row (a drafter's candidate logprobs). */
const RadTensor* pr = r4d_opt(a, V_PARAMS);
R4D_NEED(params_ok(pr, M));
if (pr) R4D_NEED(params_ok(pr, M));
const RadTensor* st = r4d_opt(a, 2);
if (!st) return R4D_NO(RAD_E_INVAL);
R4D_NEED(dt_f32(st));
+57
View File
@@ -1062,6 +1062,20 @@ extern "C" int r4d_shape_attn_paged(const RadParam* p, int n_p, int operand,
R4D_PICK(l);
}
/* attn_paged_sink -- attn_paged's operands with `sinks` [n_head] f32 before `out`. */
extern "C" int r4d_shape_attn_paged_sink(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
if (operand < 7) return r4d_shape_attn_paged(p, n_p, operand, out);
if (operand == 8) return r4d_shape_attn_paged(p, n_p, 7, out);
const int64_t nh = pi(p, n_p, "n_head");
if (nh <= 0) return RAD_E_UNSUPPORTED;
const RadOpdDesc l[] = {
opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd_none(),
opd((uint32_t)RAD_F32, nh),
};
R4D_PICK(l);
}
/* attn_paged_gate_quant -- attn_paged's operands, then gate_quant_fp8's gate, codes and scales,
* flat as gate_quant_fp8 describes them: one row per (token, head) pair, `head_dim` columns. */
extern "C" int r4d_shape_attn_paged_gate_quant(const RadParam* p, int n_p, int operand,
@@ -1757,6 +1771,49 @@ extern "C" int r4d_shape_pair_argmax(const RadParam* p, int n_p, int operand, Ra
R4D_PICK(l);
}
/* LiLiCorr's ops, described as libref describes them; lili_cand declines (its stats rows are no
* draw; avx_check_hand.cpp and tests/r4d_emu build them). */
extern "C" int r4d_shape_lili_cand(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED;
}
extern "C" int r4d_shape_lili_attn(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), S = pi(p, n_p, "steps"), k = pi(p, n_p, "k");
const int64_t nh = pi(p, n_p, "n_head"), hd = pi(p, n_p, "head_dim");
if (M <= 0 || S <= 0 || k <= 0 || nh <= 0 || hd <= 0) return RAD_E_UNSUPPORTED;
const int64_t rows = (M + S * k - 1) / (S * k) * (S * k);
const RadOpdDesc l[] = {
opd((uint32_t)RAD_BF16, rows, 3 * nh * hd),
opd((uint32_t)RAD_F32, nh, 2 * S + 1),
opd((uint32_t)RAD_F32, nh),
opd((uint32_t)RAD_BF16, rows, nh * hd),
};
R4D_PICK(l);
}
extern "C" int r4d_shape_lili_factor(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), L = pi(p, n_p, "L"), n = pi(p, n_p, "n");
if (M <= 0 || L <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
const int64_t rows = (M + L - 1) / L * L;
const RadOpdDesc l[] = {
opd((uint32_t)RAD_BF16, rows, n),
opd((uint32_t)RAD_BF16, rows / L, n),
opd((uint32_t)RAD_BF16, rows, 3 * n),
};
R4D_PICK(l);
}
extern "C" int r4d_shape_lili_walk(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), S = pi(p, n_p, "steps"), k = pi(p, n_p, "k");
const int64_t n = pi(p, n_p, "n");
if (M <= 0 || S <= 0 || k <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
const RadOpdDesc l[] = {
opd((uint32_t)RAD_BF16, M * S * k, 2 * n),
opd((uint32_t)RAD_BF16, M, n),
opd_idx(M * S, k, 1000),
opd((uint32_t)RAD_I32, M, S),
opd((uint32_t)RAD_F32, M, S, k, k),
};
R4D_PICK(l);
}
/* ==========================================================================================
* Quantisation, the norms and the fusions (shims beside their kernels).
*