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

+23 -9
View File
@@ -75,20 +75,24 @@ struct KvDyn {
} /* namespace */
/* ------------------------------------------------------------------ attn_paged */
/* attn_paged, and attn_paged_sink with `sinks` (one logit a query head, beside the keys in the
* softmax's denominator and nowhere else): out at operand `oi`. */
/* attn_paged_tree's ninth operand, `spec_parent` (RadBatch::spec_parent, rad_drafter_tree), is
* read here too: a row whose entry is -2 is an ordinary causal row; any other row is a node of its
* sequence's verify block, whose entry is its parent's row within the sequence's rows (-1 for the
* root, the block's first row). Inside the block such a row sees itself and its ancestors only,
* and its window is measured from the block's first position plus its depth -- the position its
* token has if its path is committed -- rather than from its row. */
static int ref_attn_paged_impl(const RadArgs* a, RadStream, bool shared_anchor_window) {
if (a->n_t < 8) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *kv = &a->t[1], *bt = &a->t[2], *su = &a->t[3], *o = &a->t[7];
static int attn_paged_impl(const RadArgs* a, const RadTensor* sinks, int oi, bool shared_anchor_window) {
if (a->n_t < oi + 1) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *kv = &a->t[1], *bt = &a->t[2], *su = &a->t[3], *o = &a->t[oi];
const RadTensor* kdes = t_in(a, 4);
const RadTensor* vdes = t_in(a, 5);
const RadTensor* cus = t_in(a, 6);
const RadTensor* tpar = a->n_t > 8 ? t_in(a, 8) : nullptr;
/* The tree operand sits where attn_paged_sink keeps its output; a sinked call has none. */
const RadTensor* tpar = !sinks && a->n_t > 8 ? t_in(a, 8) : nullptr;
if (!q->data || !kv->data || !bt->data || !su->data || !o->data) return RAD_E_INVAL;
if (sinks && !sinks->data) return RAD_E_INVAL;
const int64_t head_dim = p_int(a, "head_dim", q->rank >= 3 ? q->shape[q->rank - 1] : 0);
const int64_t gqa = p_int(a, "gqa", 1);
@@ -107,6 +111,7 @@ static int ref_attn_paged_impl(const RadArgs* a, RadStream, bool shared_anchor_w
/* gqa and the cache's head count fix the query head count; a rank-3 query that disagrees would
* stride through the wrong rows. */
if (q->rank >= 3 && q->shape[q->rank - 2] != q_heads) return RAD_E_SHAPE;
if (sinks && numel(sinks) != q_heads) return RAD_E_SHAPE;
const int64_t n_seq = numel(su);
const int64_t rows = numel(q) / (q_heads * head_dim);
if (n_seq <= 0 || rows <= 0) return RAD_E_SHAPE;
@@ -255,6 +260,11 @@ static int ref_attn_paged_impl(const RadArgs* a, RadStream, bool shared_anchor_w
return;
}
/* A sink is one more logit in the softmax with no value: the maximum takes it,
* the sum adds it last, and the output stays the keys' weighted values. */
const float sk = sinks ? ldt(sinks, h) : -INFINITY;
if (sinks && sk > mx) mx = sk;
/* Pass two: exponentiate and accumulate in ascending key order. */
float acc[REF_HD_MAX];
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
@@ -268,6 +278,7 @@ static int ref_attn_paged_impl(const RadArgs* a, RadStream, bool shared_anchor_w
for (int64_t d = 0; d < head_dim; ++d)
acc[d] += w * (kl(kv->data, vb + d * kvv.sd) * vds);
}
if (sinks) sum += ref_exp(sk - mx);
const float inv = sum > 0.0f ? 1.0f / sum : 0.0f;
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, acc[d] * inv);
};
@@ -282,14 +293,17 @@ static int ref_attn_paged_impl(const RadArgs* a, RadStream, bool shared_anchor_w
return RAD_OK;
}
int ref_attn_paged(const RadArgs* a, RadStream s) {
return ref_attn_paged_impl(a, s, false);
}
int ref_attn_paged(const RadArgs* a, RadStream) { return attn_paged_impl(a, nullptr, 7, false); }
int ref_attn_paged_anchor_window(const RadArgs* a, RadStream s) {
int ref_attn_paged_anchor_window(const RadArgs* a, RadStream) {
if (!a || a->n_t < 8) return RAD_E_INVAL;
if (a->n_t > 8 && t_in(a, 8)) return RAD_E_UNSUPPORTED;
return ref_attn_paged_impl(a, s, true);
return attn_paged_impl(a, nullptr, 7, true);
}
int ref_attn_paged_sink(const RadArgs* a, RadStream) {
if (a->n_t < 9 || !a->t[7].data) return RAD_E_INVAL;
return attn_paged_impl(a, &a->t[7], 8, false);
}
/* attn_paged with its tree operand present: ref_attn_paged reads it (see there). Refused without
+260
View File
@@ -0,0 +1,260 @@
/* ref_lili.cpp -- LiLiCorr's candidate correlator (NVIDIA's Nemotron-3.5-Bolt drafter; vLLM's
* model_executor/models/lilicorr.py), the four ops a block drafter's query pass ends in:
*
* lili_cand the candidates of every draft position from the target head's top-k, their five
* rank features through LayerNorm, the up projection and SiLU, and the lattice row's
* additive part: the position's own state plus a slot and a rank embedding
* lili_attn attention over each sequence's lattice of steps x k rows, with a learned bias per
* head on the slot distance and on the same slot
* lili_factor [h | a | h * a] a row, a = its sequence's anchor
* lili_walk the factored edge scores and the greedy walk over them
*
* A sequence's lattice is `steps` draft positions of `k` candidates, row s * k + j for position s's
* j-th candidate, the sequences one after another. Everything runs in f32 and rounds once to the
* output's dtype.
*/
#include "ref_common.h"
#include "ref_ops.h"
#include <algorithm>
#include <cmath>
#include <vector>
using namespace ref;
/* ------------------------------------------------------------------ lili_cand */
/* stats [M, world * (2 + 2 row_k)] f32: each rank's sample_logprobs row (max, sum of exp(l - max),
* row_k >= k (id, logit) pairs descending), all-gathered row-interleaved; row_k defaults to k (a
* caller widens it so a row is whole 16-byte words for the all-gather). The ranks merge as one row would:
* the maximum and the rescaled sum give the log-normaliser, the pairs the k largest by value, ties
* to the lower id. For candidate j of row m, with lp_j = logit_j - lse:
*
* feat = [lp_j, exp(lp_j), lp_j - lp_0, j / (k - 1), j == 0]
* f = silu(up_w . layernorm(feat; ln_w, ln_b, eps) + up_b) -> feat out, row m*k+j
* base = pas[m] + slot[m % steps] + rank[j] -> base out, row m*k+j
* cand = id_j -> cand [M, k] i32
*/
int ref_lili_cand(const RadArgs* a, RadStream) {
if (a->n_t < 11) return RAD_E_INVAL;
for (int i = 0; i < 11; ++i) if (!a->t[i].data) return RAD_E_INVAL;
const RadTensor *st = &a->t[0], *pas = &a->t[1], *lnw = &a->t[2], *lnb = &a->t[3];
const RadTensor *upw = &a->t[4], *upb = &a->t[5], *slot = &a->t[6], *rank = &a->t[7];
const RadTensor *cand = &a->t[8], *fo = &a->t[9], *bo = &a->t[10];
const int64_t k = p_int(a, "k", 0), world = p_int(a, "world", 1), steps = p_int(a, "steps", 0);
const int64_t n = p_int(a, "n", 0);
const float eps = p_f32(a, "eps", 1e-5f);
const int64_t rk = p_int(a, "row_k", k);
if (k < 2 || k > 64 || rk < k || world < 1 || steps < 1 || n <= 0) return RAD_E_SHAPE;
const int64_t w = 2 + 2 * rk;
const int64_t M = rows_of(st, world * w);
if (M <= 0 || rows_of(pas, n) < M || numel(cand) < M * k || rows_of(fo, n) < M * k ||
rows_of(bo, n) < M * k)
return RAD_E_SHAPE;
if (numel(lnw) < 5 || numel(lnb) < 5 || numel(upw) < n * 5 || numel(upb) < n ||
rows_of(slot, n) < steps || rows_of(rank, n) < k)
return RAD_E_SHAPE;
if (cand->dtype != RAD_I32) return RAD_E_DTYPE;
REF_PAR_FOR(M * k * n * 8)
for (int64_t m = 0; m < M; ++m) {
const int64_t sb = rowoff(st, m, world * w), ss = laststride(st);
auto S = [&](int64_t r, int64_t i) { return ldt(st, sb + (r * w + i) * ss); };
/* The log-normaliser over every rank's shard. */
float mx = -INFINITY;
for (int64_t r = 0; r < world; ++r) mx = std::max(mx, S(r, 0));
double sum = 0.0;
for (int64_t r = 0; r < world; ++r)
if (S(r, 0) > -INFINITY) sum += (double)S(r, 1) * std::exp((double)S(r, 0) - mx);
const double lse = (double)mx + std::log(sum);
/* The k largest pairs of the world * k, by value, ties to the lower id. */
std::vector<std::pair<float, int64_t>> all;
all.reserve((size_t)(world * k));
for (int64_t r = 0; r < world; ++r)
for (int64_t j = 0; j < k; ++j) {
const int64_t id = (int64_t)S(r, 2 + 2 * j);
if (id >= 0) all.push_back({ S(r, 3 + 2 * j), id });
}
std::sort(all.begin(), all.end(), [](const auto& x, const auto& y) {
return x.first > y.first || (x.first == y.first && x.second < y.second);
});
float lp[64];
int64_t ids[64];
for (int64_t j = 0; j < k; ++j) {
const bool have_j = j < (int64_t)all.size();
ids[j] = have_j ? all[(size_t)j].second : 0;
lp[j] = have_j ? (float)((double)all[(size_t)j].first - lse) : -1e4f;
((int32_t*)cand->data)[rowoff(cand, m, k) + j * laststride(cand)] = (int32_t)ids[j];
}
const int64_t s = m % steps;
for (int64_t j = 0; j < k; ++j) {
const float f[5] = { lp[j], std::exp(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 / std::sqrt(var + eps);
float y[5];
for (int i = 0; i < 5; ++i) y[i] = (f[i] - mean) * rs * ldt(lnw, i) + ldt(lnb, i);
const int64_t row = m * k + j;
const int64_t fb = rowoff(fo, row, n), fs = laststride(fo);
const int64_t bb = rowoff(bo, row, n), bs = laststride(bo);
for (int64_t c = 0; c < n; ++c) {
float u = ldt(upb, c);
for (int i = 0; i < 5; ++i) u += ldt(upw, c * 5 + i) * y[i];
stt(fo, fb + c * fs, u / (1.0f + std::exp(-u)));
const float b = ldt(pas, rowoff(pas, m, n) + c * laststride(pas)) +
ldt(slot, rowoff(slot, s, n) + c * laststride(slot)) +
ldt(rank, rowoff(rank, j, n) + c * laststride(rank));
stt(bo, bb + c * bs, b);
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ lili_attn */
/* qkv [M, 3 * n_head * head_dim] (q | k | v, heads contiguous within each), M = sequences * L with
* L = steps * k. Row i of a lattice sits at slot s_i = i / k. Per sequence and head:
*
* score(i, j) = q_i . k_j * scale + rel[h][s_i - s_j + steps] + (s_i == s_j ? same[h] : 0)
* out_i = softmax_j(score(i, .)) . v
*
* Every row sees every row of its lattice: the bias is a preference, not a mask. */
int ref_lili_attn(const RadArgs* a, RadStream) {
if (!have(a, 4)) return RAD_E_INVAL;
const RadTensor *qkv = &a->t[0], *rel = &a->t[1], *same = &a->t[2], *o = &a->t[3];
const int64_t steps = p_int(a, "steps", 0), k = p_int(a, "k", 0);
const int64_t nh = p_int(a, "n_head", 0), hd = p_int(a, "head_dim", 0);
if (steps < 1 || k < 1 || nh < 1 || hd < 1) return RAD_E_SHAPE;
const int64_t L = steps * k, n = nh * hd;
const int64_t M = rows_of(qkv, 3 * n);
if (M <= 0 || M % L || rows_of(o, n) < M) return RAD_E_SHAPE;
const int64_t rw = rel->rank ? rel->shape[rel->rank - 1] : 0;
if (rw < 2 * steps - 1 || rows_of(rel, rw) < nh || numel(same) < nh) return RAD_E_SHAPE;
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)hd));
const int64_t qs = laststride(qkv), os = laststride(o);
REF_PAR_FOR(M * nh * L * hd * 2)
for (int64_t u = 0; u < M * nh; ++u) {
const int64_t i = u / nh, h = u % nh;
const int64_t q0 = i / L * L, si = (i - q0) / k;
const int64_t qb = rowoff(qkv, i, 3 * n) + h * hd * qs;
std::vector<float> sc((size_t)L);
float mx = -INFINITY;
for (int64_t j = 0; j < L; ++j) {
const int64_t kb = rowoff(qkv, q0 + j, 3 * n) + (n + h * hd) * qs;
float dot = 0.0f;
for (int64_t d = 0; d < hd; ++d) dot += ldt(qkv, qb + d * qs) * ldt(qkv, kb + d * qs);
const int64_t sj = j / k;
float b = ldt(rel, rowoff(rel, h, rw) + (si - sj + steps) * laststride(rel));
if (si == sj) b += ldt(same, h);
sc[(size_t)j] = dot * scale + b;
mx = std::max(mx, sc[(size_t)j]);
}
float sum = 0.0f;
for (int64_t j = 0; j < L; ++j) { sc[(size_t)j] = std::exp(sc[(size_t)j] - mx); sum += sc[(size_t)j]; }
const int64_t ob = rowoff(o, i, n) + h * hd * os;
for (int64_t d = 0; d < hd; ++d) {
float acc = 0.0f;
for (int64_t j = 0; j < L; ++j)
acc += sc[(size_t)j] * ldt(qkv, rowoff(qkv, q0 + j, 3 * n) + (2 * n + h * hd + d) * qs);
stt(o, ob + d * os, acc / sum);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ lili_factor */
/* y[i] = [h_i | a_s | h_i * a_s] with s = i / L, the factor projection's input. */
int ref_lili_factor(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
const RadTensor *h = &a->t[0], *an = &a->t[1], *y = &a->t[2];
const int64_t L = p_int(a, "L", 0), n = p_int(a, "n", 0);
if (L < 1 || n < 1) return RAD_E_SHAPE;
const int64_t M = rows_of(h, n);
if (M <= 0 || M % L || rows_of(an, n) < M / L || rows_of(y, 3 * n) < M) return RAD_E_SHAPE;
REF_PAR_FOR(M * n * 3)
for (int64_t i = 0; i < M; ++i) {
const int64_t hb = rowoff(h, i, n), ab = rowoff(an, i / L, n), yb = rowoff(y, i, 3 * n);
const int64_t hs = laststride(h), as = laststride(an), ys = laststride(y);
for (int64_t c = 0; c < n; ++c) {
const float hv = ldt(h, hb + c * hs), av = ldt(an, ab + c * as);
stt(y, yb + c * ys, hv);
stt(y, yb + (n + c) * ys, av);
stt(y, yb + (2 * n + c) * ys, hv * av);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ lili_walk */
/* edges [M * steps * k, 2n]: each candidate's outgoing | incoming vector, aout [M, n] the anchor's
* outgoing one, cand [M * steps, k] i32. Vectors are normalised as F.normalize (v / max(|v|, eps)).
*
* start[j] = scale * aout . in[0][j]
* pair[s][i][j] = scale * out[s][i] . in[s + 1][j]
* walk p_0 = argmax start, p_s = argmax pair[s - 1][p_{s-1}] (first index on ties)
* tokens[m][s] = cand[m * steps + s][p_s]
*
* `scores` [M, steps, k, k], when present, is vLLM's tensor: row 0 is start for every i. */
int ref_lili_walk(const RadArgs* a, RadStream) {
if (!have(a, 4)) return RAD_E_INVAL;
const RadTensor *ed = &a->t[0], *ao = &a->t[1], *cd = &a->t[2], *tk = &a->t[3];
const RadTensor* sco = a->n_t > 4 && a->t[4].data ? &a->t[4] : nullptr;
const int64_t steps = p_int(a, "steps", 0), k = p_int(a, "k", 0), n = p_int(a, "n", 0);
const float eps = p_f32(a, "eps", 1e-4f), scale = p_f32(a, "scale", 1.0f);
if (steps < 1 || k < 1 || n < 1) return RAD_E_SHAPE;
const int64_t M = rows_of(ao, n);
if (M <= 0 || rows_of(ed, 2 * n) < M * steps * k || numel(cd) < M * steps * k ||
numel(tk) < M * steps)
return RAD_E_SHAPE;
if (cd->dtype != RAD_I32 || tk->dtype != RAD_I32) return RAD_E_DTYPE;
if (sco && numel(sco) < M * steps * k * k) return RAD_E_SHAPE;
REF_PAR_FOR(M * steps * k * k * n)
for (int64_t m = 0; m < M; ++m) {
auto vec = [&](const RadTensor* t, int64_t row, int64_t pitch, int64_t col0, float* v) {
const int64_t b = rowoff(t, row, pitch) + col0 * laststride(t);
double ss = 0.0;
for (int64_t c = 0; c < n; ++c) { v[c] = ldt(t, b + c * laststride(t)); ss += (double)v[c] * v[c]; }
const float inv = 1.0f / std::max((float)std::sqrt(ss), eps);
for (int64_t c = 0; c < n; ++c) v[c] *= inv;
};
std::vector<float> va((size_t)n), vo((size_t)(steps * k * n)), vi((size_t)(steps * k * n));
vec(ao, m, n, 0, va.data());
for (int64_t r = 0; r < steps * k; ++r) {
vec(ed, m * steps * k + r, 2 * n, 0, &vo[(size_t)(r * n)]);
vec(ed, m * steps * k + r, 2 * n, n, &vi[(size_t)(r * n)]);
}
auto dot = [&](const float* x, const float* y) {
float s = 0.0f;
for (int64_t c = 0; c < n; ++c) s += x[c] * y[c];
return s * scale;
};
std::vector<float> sc((size_t)(steps * k * k));
for (int64_t j = 0; j < k; ++j) {
const float v = dot(va.data(), &vi[(size_t)(j * n)]);
for (int64_t i = 0; i < k; ++i) sc[(size_t)(i * k + j)] = v;
}
for (int64_t s = 1; s < steps; ++s)
for (int64_t i = 0; i < k; ++i)
for (int64_t j = 0; j < k; ++j)
sc[(size_t)((s * k + i) * k + j)] =
dot(&vo[(size_t)(((s - 1) * k + i) * n)], &vi[(size_t)((s * k + j) * n)]);
int64_t prev = 0;
for (int64_t s = 0; s < steps; ++s) {
int64_t best = 0;
for (int64_t j = 1; j < k; ++j)
if (sc[(size_t)((s * k + prev) * k + j)] > sc[(size_t)((s * k + prev) * k + best)]) best = j;
const int64_t id = ld_int(cd->dtype, cd->data, rowoff(cd, m * steps + s, k) + best * laststride(cd));
((int32_t*)tk->data)[rowoff(tk, m, steps) + s * laststride(tk)] = (int32_t)id;
prev = best;
}
if (sco)
for (int64_t e = 0; e < steps * k * k; ++e) stt(sco, m * steps * k * k + e, sc[(size_t)e]);
}
return RAD_OK;
}
+10
View File
@@ -72,6 +72,10 @@ int ref_mtp_enter (const RadArgs*, RadStream);
int ref_layernorm (const RadArgs*, RadStream);
int ref_grid_embed (const RadArgs*, RadStream);
int ref_grid_embed_aa (const RadArgs*, RadStream);
int ref_lili_cand (const RadArgs*, RadStream);
int ref_lili_attn (const RadArgs*, RadStream);
int ref_lili_factor (const RadArgs*, RadStream);
int ref_lili_walk (const RadArgs*, RadStream);
int ref_add (const RadArgs*, RadStream);
int ref_mul (const RadArgs*, RadStream);
int ref_silu_mul (const RadArgs*, RadStream);
@@ -157,6 +161,7 @@ int ref_logits_gemm (const RadArgs*, RadStream);
int ref_attn_paged (const RadArgs*, RadStream);
int ref_attn_paged_anchor_window(const RadArgs*, RadStream);
int ref_attn_paged_tree (const RadArgs*, RadStream);
int ref_attn_paged_sink (const RadArgs*, RadStream);
int ref_attn_dense (const RadArgs*, RadStream);
int ref_attn_cross (const RadArgs*, RadStream);
int ref_table_add (const RadArgs*, RadStream);
@@ -356,6 +361,10 @@ REF_SHAPE_FN(ref_mtp_enter);
REF_SHAPE_FN(ref_layernorm);
REF_SHAPE_FN(ref_grid_embed);
REF_SHAPE_FN(ref_grid_embed_aa);
REF_SHAPE_FN(ref_lili_cand);
REF_SHAPE_FN(ref_lili_attn);
REF_SHAPE_FN(ref_lili_factor);
REF_SHAPE_FN(ref_lili_walk);
REF_SHAPE_FN(ref_add);
REF_SHAPE_FN(ref_mul);
REF_SHAPE_FN(ref_silu_mul);
@@ -437,6 +446,7 @@ REF_SHAPE_FN(ref_logits_gemm);
REF_SHAPE_FN(ref_attn_paged);
REF_SHAPE_FN(ref_attn_paged_anchor_window);
REF_SHAPE_FN(ref_attn_paged_tree);
REF_SHAPE_FN(ref_attn_paged_sink);
REF_SHAPE_FN(ref_attn_dense);
REF_SHAPE_FN(ref_attn_cross);
REF_SHAPE_FN(ref_table_add);
+61 -1
View File
@@ -162,6 +162,22 @@ static const RadOperandSpec oLayernorm[] = { OPD("x"), WGT("w"), WGT_O("b"), OUT
/* A learned position table resampled to each row's grid and added in (a vision tower's). */
static const RadParamSpec pGridEmbed[] = { P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype") };
static const RadOperandSpec oGridEmbed[] = { INOUT("x"), WGT("table"), OPD("coord") };
/* LiLiCorr's correlator (ref_lili.cpp). */
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[] = { OPD("stats"), OPD("pas"), WGT("ln_w"), WGT("ln_b"),
WGT("up_w"), WGT("up_b"), WGT("slot"), WGT("rank"),
OUT("cand"), OUT("feat"), OUT("base") };
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[] = { OPD("qkv"), WGT("rel"), WGT("same"), OUT("out") };
static const RadParamSpec pLiliFactor[] = { P_INT("M"), P_INT("L"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oLiliFactor[] = { OPD("h"), OPD("a"), OUT("y") };
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[] = { OPD("edges"), OPD("aout"), OPD("cand"), OUT("tokens"),
OUT_O("scores") };
static const RadParamSpec pSwigluClamped[] = { P_INT("M"), P_INT("n"), P_F64("limit"), P_STR("dtype") };
static const RadParamSpec pDsaPool[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_STR("dtype"), P_INTO("block_size") };
@@ -399,6 +415,13 @@ static const RadOperandSpec oAttnPagedTree[] = {
OPD("q"), OPD("kv_cache"), OPD("block_table"), OPD("seqused"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("cu_seqlens"), OUT("out"), OPD("spec_parent")
};
/* attn_paged with a learned sink logit a query head (gpt-oss, LiLiCorr's drafter). A name of its
* own, so no attn_paged kernel that does not know the operand can be chosen for it. */
static const RadOperandSpec oAttnPagedSink[] = {
OPD("q"), OPD("kv_cache"), OPD("block_table"), OPD("seqused"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("cu_seqlens"), WGT("sinks"), OUT("out")
};
/* Effective on kv_store, where `block_size` is RAD_REQUIRED: admits every value and refuses only a
* query that omits it. On attn_paged the key is RAD_DERIVED and the constraint is skipped
* (core/plugin/select.cpp). See the file header for the spec §7.2 consequence. */
@@ -803,7 +826,7 @@ static const RadOperandSpec oArgmax[] = { OPD("logits"), OPD("params"), OUT("
* sample_topk. */
static const RadParamSpec pLogprobs[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"),
P_INTO("vocab_off") };
static const RadOperandSpec oLogprobs[] = { OPD("logits"), OPD("params"), OUT("stats") };
static const RadOperandSpec oLogprobs[] = { OPD("logits"), OPD_O("params"), OUT("stats") };
static const RadOperandSpec oNarrow[] = { INOUT("cand_idx"), INOUT("cand_val"), OPD("params") };
static const RadOperandSpec oPick[] = { OPD("cand_idx"), OPD("cand_val"), OPD("params"),
OUT("token") };
@@ -1178,6 +1201,26 @@ static const RadOpSchema kSchemas[] = {
"antialias=True) resamples it to the row's grid -- SigLIP2's position embedding: per axis a "
"triangle filter on half-pixel centres, widened by the scale when shrinking, its in-table taps "
"normalised. Separable, f32, rounded once before the add; coordinates clamp into their grid." },
{ "lili_cand", ARR(pLiliCand), ARR(oLiliCand),
"LiLiCorr's candidates. `stats` [M, world * (2 + 2k)] f32 is each rank's sample_logprobs row, "
"row-interleaved; the ranks merge to the log-normaliser and the k largest pairs by value, ties to "
"the lower id. cand [M, k] i32 = those ids. For candidate j, lp_j = logit_j - lse: feat row m*k+j "
"= silu(up_w . layernorm([lp_j, exp lp_j, lp_j - lp_0, j/(k-1), j == 0]; ln_w, ln_b, eps) + "
"up_b); base row m*k+j = pas[m] + slot[m % steps] + rank[j]. f32, rounded once to the outputs." },
{ "lili_attn", ARR(pLiliAttn), ARR(oLiliAttn),
"Attention over each sequence's lattice of L = steps * k rows (row i at slot i / k): qkv [M, 3 * "
"n_head * head_dim] as q | k | v; score(i, j) = q_i . k_j * scale + rel[h][s_i - s_j + steps] + "
"(s_i == s_j ? same[h] : 0), softmax over the lattice's rows, out [M, n_head * head_dim]. No "
"mask." },
{ "lili_factor", ARR(pLiliFactor), ARR(oLiliFactor),
"y [M, 3n] = [h_i | a_s | h_i * a_s] with s = i / L: each lattice row beside its sequence's anchor "
"and their product, the factor projection's input." },
{ "lili_walk", ARR(pLiliWalk), ARR(oLiliWalk),
"LiLiCorr's walk. edges [M * steps * k, 2n] each candidate's outgoing | incoming vector, aout "
"[M, n] the anchor's; each normalised as v / max(|v|, eps). start[j] = scale * aout . in[0][j], "
"pair[s][i][j] = scale * out[s][i] . in[s+1][j]; greedy p_0 = argmax start, p_s = argmax "
"pair[s-1][p_{s-1}], first index on ties; tokens [M, steps] i32 = cand[m * steps + s][p_s]. "
"`scores` [M, steps, k, k], when passed: row 0 start for every i, row s pair[s - 1]." },
{ "add", ARR(pAdd), ARR(oBinary),
"y = a + b. An operand carrying exactly n elements against a taller output broadcasts along "
"rows; nothing else broadcasts. With `div`, y = rd(rd(a + b) / div), rd y's dtype: the last "
@@ -1489,6 +1532,10 @@ static const RadOpSchema kSchemas[] = {
"causal row, else the row's parent's index within its sequence's rows (-1 for the block's "
"first row). Inside the block such a row sees itself and its ancestors only, and its window is "
"measured from the block's first position plus its depth." },
{ "attn_paged_sink", ARR(pAttnPaged), ARR(oAttnPagedSink),
"attn_paged with `sinks` [n_head] f32, one logit a query head in the scaled-score domain: it "
"joins the softmax's maximum and denominator, contributes no value, and so only shrinks the "
"keys' weights (out = sum_j p_j v_j with p_j = exp(s_j) / (sum_k exp(s_k) + exp(sink)))." },
{ "attn_dense", ARR(pAttnDense), ARR(oAttnDense),
"Dense varlen attention over cu_seqlens segments (rows cu/cu_div). Nothing paged. `window` is "
"attn_paged's rule over a segment's positions: causal, i - j < window; non-causal, |i - j| < "
@@ -2290,6 +2337,14 @@ SIMPLE("ref_grid_embed", "grid_embed", "elem",
SIMPLE("ref_grid_embed_aa", "grid_embed_aa", "elem",
"antialiased half-pixel bilinear resample of a position grid, added in", DT_FLOAT,
ref_grid_embed_aa),
SIMPLE("ref_lili_cand", "lili_cand", "draft", "LiLiCorr candidates and their features",
DT_FLOAT, ref_lili_cand),
SIMPLE("ref_lili_attn", "lili_attn", "draft", "LiLiCorr lattice attention with slot biases",
DT_FLOAT, ref_lili_attn),
SIMPLE("ref_lili_factor", "lili_factor", "draft", "LiLiCorr factor input [h | a | h * a]",
DT_FLOAT, ref_lili_factor),
SIMPLE("ref_lili_walk", "lili_walk", "draft", "LiLiCorr edge scores and greedy walk", DT_FLOAT,
ref_lili_walk),
SIMPLE("ref_add", "add", "elem", "elementwise sum", DT_FLOAT, ref_add),
SIMPLE("ref_mul", "mul", "elem", "elementwise product", DT_FLOAT, ref_mul),
SIMPLE("ref_mla_attn", "mla_attn", "attn", "absorbed latent MQA attention", DT_FLOAT,
@@ -2476,6 +2531,11 @@ ROW("ref_attn_paged_tree", "attn_paged_tree", "attn",
"any q and out dtype; bf16 or fp8 kv cache with optional per-(seq,head) descales; i32 parents",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged_tree, nullptr),
ROW("ref_attn_paged_sink", "attn_paged_sink", "attn",
"paged causal / sliding-window attention with a sink logit a head, any query length",
"any q and out dtype; bf16 or fp8 kv cache with optional per-(seq,head) descales; f32 sinks",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged_sink, nullptr),
SIMPLE("ref_attn_dense", "attn_dense", "attn", "dense varlen attention over cu_seqlens", DT_FLOAT,
ref_attn_dense),
SIMPLE("ref_attn_cross", "attn_cross", "attn",
+4 -4
View File
@@ -674,9 +674,9 @@ int ref_sample_argmax(const RadArgs* a, RadStream) {
* floats (exact below 2^24). Read before any sampling stage, as OpenAI's and llama-server's
* logprobs report. Rows without the flag are not written. Under a split vocabulary ids are global
* (+ `vocab_off`) and the host merges ranks; an unfilled slot is id -1, logit -infinity. The sum is
* in double. */
* in double. Without `params` every row is written (a drafter's candidate logprobs). */
int ref_sample_logprobs(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
if (a->n_t < 3 || !a->t[0].data || !a->t[2].data) return RAD_E_INVAL;
const RadTensor *lg = &a->t[0], *pr = &a->t[1], *st = &a->t[2];
const int64_t n_vocab = p_int(a, "n_vocab", lg->rank >= 2 ? lg->shape[lg->rank - 1] : 0);
const int64_t k = p_int(a, "k", 0);
@@ -686,11 +686,11 @@ int ref_sample_logprobs(const RadArgs* a, RadStream) {
const int64_t w = 2 + 2 * k;
if (M <= 0 || numel(st) < M * w) return RAD_E_SHAPE;
Rows R;
RAD_REF_TRY(param_rows(pr, M, &R));
if (pr->data) RAD_REF_TRY(param_rows(pr, M, &R));
REF_PAR_FOR(M * n_vocab * (k + 2))
for (int64_t m = 0; m < M; ++m) {
if (!(R.p[m].flags & RAD_SP_LOGPROBS)) continue;
if (pr->data && !(R.p[m].flags & RAD_SP_LOGPROBS)) continue;
const int64_t lb = rowoff(lg, m, n_vocab), ls = laststride(lg);
const int64_t sb = rowoff(st, m, w), ss = laststride(st);
float mx = -INFINITY;
+62
View File
@@ -488,6 +488,50 @@ int ref_grid_embed_aa_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
return o.finish("grid_embed_aa", operand, out);
}
/* LiLiCorr's ops. lili_cand declines: its stats rows (sample_logprobs' sorted pairs and positive
* sums) are no draw; avx_check_hand.cpp builds them. The others draw freely. */
int ref_lili_cand_shape(const RadParam*, int, int, RadOpdDesc*) { return RAD_E_UNSUPPORTED; }
int ref_lili_attn_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), S = gi(p, n_p, "steps", 0), k = gi(p, n_p, "k", 0);
const int64_t nh = gi(p, n_p, "n_head", 0), hd = gi(p, n_p, "head_dim", 0);
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); /* whole lattices */
Opd o;
o.t(D, { rows, 3 * nh * hd }); /* qkv */
o.t((uint32_t)RAD_F32, { nh, 2 * S + 1 }); /* rel */
o.t((uint32_t)RAD_F32, { nh }); /* same */
o.t(D, { rows, nh * hd }); /* out */
return o.finish("lili_attn", operand, out);
}
int ref_lili_factor_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), L = gi(p, n_p, "L", 0), n = gi(p, n_p, "n", 0);
if (M <= 0 || L <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
const int64_t rows = (M + L - 1) / L * L;
Opd o;
o.t(D, { rows, n }); /* h */
o.t(D, { rows / L, n }); /* a */
o.t(D, { rows, 3 * n }); /* y */
return o.finish("lili_factor", operand, out);
}
int ref_lili_walk_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), S = gi(p, n_p, "steps", 0), k = gi(p, n_p, "k", 0);
const int64_t n = gi(p, n_p, "n", 0);
if (M <= 0 || S <= 0 || k <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
Opd o;
o.t(D, { M * S * k, 2 * n }); /* edges */
o.t(D, { M, n }); /* aout */
o.t_idx({ M * S, k }, 1000, -1, 0); /* cand: ids, copied */
o.t((uint32_t)RAD_I32, { M, S }); /* tokens */
o.t((uint32_t)RAD_F32, { M, S, k, k }); /* scores */
return o.finish("lili_walk", operand, out);
}
int ref_layernorm_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
@@ -1467,6 +1511,24 @@ int ref_attn_paged_tree_shape(const RadParam* p, int n_p, int operand, RadOpdDes
return o.finish("attn_paged_tree", operand, out);
}
/* attn_paged's operands with the sinks before `out`: a learned logit a query head. */
int ref_attn_paged_sink_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int nbase = 8;
if (operand < 7) return ref_attn_paged_shape(p, n_p, operand, out);
if (operand == 8) return ref_attn_paged_shape(p, n_p, nbase - 1, out);
if (operand != 7 || !out) return RAD_E_SHAPE;
const int64_t gqa = gi(p, n_p, "gqa", 1);
const int64_t nh = gi(p, n_p, "n_head", gqa);
if (nh <= 0) return RAD_E_UNSUPPORTED;
std::memset(out, 0, sizeof *out);
out->fill = (uint32_t)RAD_FILL_NORMAL;
out->idx_const = -1;
out->dtype = (uint32_t)RAD_F32;
out->rank = 1;
out->shape[0] = nh;
return RAD_OK;
}
int ref_attn_dense_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0);
const int64_t hd = gi(p, n_p, "head_dim", 0);