This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libref/ref_attn.cpp
T
Deadcode ddcb9c69bb 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.
2026-11-14 22:32:28 +01:00

1360 lines
70 KiB
C++

/* ref_attn.cpp -- paged attention, dense attention, the KV write, and the two rotary forms.
*
* These ops are the easiest to get subtly wrong, so they favour legibility: the mask is an explicit
* predicate per key, the softmax is two plain passes with the max subtracted, and nothing is fused.
*
* ============================== The paged layout ==============================
*
* kv_cache[num_blocks, kv_heads, block_size, 2 * head_dim] K then V inside a slot
*
* The layout every kernel must match (ref_ops.h; libr4d's `R4DArgs::kv`). A flat slot index s is
* block = s / block_size, offset = s % block_size; `block_table[seq, i]` holds the block id of the
* sequence's i-th block and `slot_mapping[token]` the flat slot a token's KV goes to.
*
* ============================== The mask ==============================
*
* `seqused[s]` is the sequence's total key count including this step's query tokens, so query row
* i of a q_len-row step sits at absolute position seqused[s] - q_len + i. One kernel thus serves
* decode (q_len 1), speculative verify (q_len <= 64) and prefill chunks (q_len in the thousands).
*
* causal = 1 key j is visible when j <= pos
* causal = 0 key j is visible when j < seqused[s]
* window > 0 causal: additionally pos - j < window; non-causal: |pos - j| < window, which is
* the two-sided reading libr4d's h128 kernel documents.
*/
#include "ref_common.h"
#include "rad_yarn.h"
/* `row_stride`: every q/k/v/out row stride below goes through it rather than a rank-based
* shortcut; see kv_store for where the shortcut is wrong. */
#include "ref_gemm.h"
#include "ref_ops.h"
#include <algorithm>
#include <cstring>
using namespace ref;
namespace {
/* The largest head this reference accumulates in one stack frame. The widest in use is an image
* VAE's single-head mid-block attention at 1152 (Qwen-Image 2.1); above 2048 the op returns
* RAD_E_UNSUPPORTED instead of overflowing the frame. */
enum { REF_HD_MAX = 2048 };
struct KVView {
const RadTensor* t;
int64_t sb, sh, so, sd; /* strides: block, kv head, slot offset, element */
int64_t head_dim;
};
static inline KVView kv_view(const RadTensor* c, int64_t head_dim, int64_t block_size,
int64_t kv_heads) {
KVView v{};
v.t = c;
v.head_dim = head_dim;
if (c->rank == 4) {
v.sb = c->stride[0]; v.sh = c->stride[1]; v.so = c->stride[2]; v.sd = c->stride[3];
} else {
v.sd = 1;
v.so = 2 * head_dim;
v.sh = block_size * v.so;
v.sb = kv_heads * v.sh;
}
return v;
}
/* Cache element loaders with the dtype resolved once per launch: the same floats as ldt, and the
* per-element switch out of the inner loops. */
template <uint32_t DT> struct KvFix {
float operator()(const void* p, int64_t i) const { return Cvt<DT>::ld(p, i); }
};
struct KvDyn {
uint32_t dt;
float operator()(const void* p, int64_t i) const { return ld_dt(dt, p, i); }
};
/* The int8 cache (abi/rad_builder.h): a token's row in a head is head_dim K codes, head_dim V
* codes and the two f16 scales, K's then V's. `row` is the row's byte offset in the cache; `half`
* 0 reads K's scale, 1 V's. A value is its code times its scale. */
static inline float kv_i8_scale(const void* cache, int64_t row, int64_t head_dim, int half) {
uint16_t h;
std::memcpy(&h, (const uint8_t*)cache + row + 2 * head_dim + 2 * half, sizeof h);
return f16_to_f32(h);
}
/* One head's K or V into its row of the int8 cache: the scale is the head's absmax / 127 rounded
* to f16 (clamped at 515 so 127 times it stays an f16), the codes round(x / scale) against the
* rounded scale, clamped to +-127. An all-zero head stores scale 0 and codes 0. */
static inline void kv_i8_put(void* cache, int64_t row, int64_t head_dim, int half,
const RadTensor* x, int64_t xb, int64_t x_ds) {
float am = 0.0f;
for (int64_t d = 0; d < head_dim; ++d) am = std::fmax(am, std::fabs(ldt(x, xb + d * x_ds)));
const uint16_t h = f32_to_f16(std::fmin(am / 127.0f, 515.0f));
const float sc = f16_to_f32(h);
int8_t* const c = (int8_t*)cache + row + half * head_dim;
for (int64_t d = 0; d < head_dim; ++d) {
const float r = sc > 0.0f ? std::nearbyint(ldt(x, xb + d * x_ds) / sc) : 0.0f;
c[d] = (int8_t)std::fmin(std::fmax(r, -127.0f), 127.0f);
}
std::memcpy((uint8_t*)cache + row + 2 * head_dim + 2 * half, &h, sizeof h);
}
} /* 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 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);
/* 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);
/* `block_size` is RAD_DERIVED here (docs/OPS.md), so a call resolved to ref's row may arrive
* without it. Read it off the cache tensor, and cross-check below when the caller names one. */
const int64_t block_size = p_int(a, "block_size", kv->rank == 4 ? kv->shape[2] : 0);
const int causal = (int)p_int(a, "causal", 1);
const int64_t window = p_int(a, "window", 0);
if (head_dim <= 0 || gqa <= 0 || block_size <= 0 ||
(shared_anchor_window && window <= 0)) return RAD_E_SHAPE;
if (head_dim > REF_HD_MAX) return RAD_E_UNSUPPORTED;
const int64_t kv_heads = kv->rank == 4 ? kv->shape[1] : 0;
if (kv_heads <= 0) return RAD_E_SHAPE;
const int64_t q_heads = gqa * kv_heads;
/* 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;
/* Ragged batches. cu_seqlens is optional; absent, every sequence has the same query length.
* Present, it is [n_seq + 1] with cu[0] == 0 and cu[n_seq] == rows, and sequence s owns query
* rows [cu[s], cu[s+1]). Serving mixes a prefill chunk with decode rows in one step, so ragged
* is the ordinary case. */
if (!cus && rows % n_seq != 0) return RAD_E_SHAPE;
if (cus && numel(cus) != n_seq + 1) return RAD_E_SHAPE;
const int64_t q_len = cus ? 0 : rows / n_seq;
if (numel(o) < rows * q_heads * head_dim) return RAD_E_SHAPE;
if (kv->rank == 4 && kv->shape[2] != block_size) return RAD_E_SHAPE;
if (kv->rank == 4 && kv->shape[3] < 2 * head_dim) return RAD_E_SHAPE;
/* The int8 cache: dense rows of 2 * head_dim + 4 bytes, each key's scales in its own row, and
* no descales. */
const bool i8 = kv->dtype == RAD_I8;
if (i8 && (kv->rank != 4 || kv->shape[3] != 2 * head_dim + 4 || kv->stride[3] != 1))
return RAD_E_SHAPE;
const int64_t max_blocks = bt->rank >= 2 ? bt->shape[bt->rank - 1] : numel(bt) / n_seq;
const int64_t bt_rs = bt->rank >= 2 ? bt->stride[bt->rank - 2] : max_blocks;
const int64_t bt_cs = bt->rank >= 2 ? bt->stride[bt->rank - 1] : 1;
const KVView kvv = kv_view(kv, head_dim, block_size, kv_heads);
/* Row strides of q and out read as [rows, q_heads, head_dim], at whatever rank they arrived. */
const int64_t q_ds = q->rank >= 2 ? q->stride[q->rank - 1] : 1;
const int64_t q_hs = q->rank >= 3 ? q->stride[q->rank - 2] : head_dim * q_ds;
const int64_t q_rs = row_stride(q, q_heads * head_dim);
const int64_t o_ds = o->rank >= 2 ? o->stride[o->rank - 1] : 1;
const int64_t o_hs = o->rank >= 3 ? o->stride[o->rank - 2] : head_dim * o_ds;
const int64_t o_rs = row_stride(o, q_heads * head_dim);
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)head_dim));
const int64_t total = rows * q_heads;
const int64_t jcap = max_blocks * block_size;
/* Every (query row, head) is one output row, computed as below in a fixed order: per visible
* key, dot = sum over d of q[d] * (k[d] * k_descale), score = dot * scale; then the max over the
* scores in key order; then sum and acc[d] accumulate exp(score - max) and
* exp(...) * (v[d] * v_descale) in key order. At the int8 cache k[d] and v[d] are codes and
* the key's own K and V scales stand where the descales do. The query row is widened once, each score is kept
* for the second pass instead of recomputed, and four keys' dot chains run side by side; none
* of that changes a value. */
REF_PAR_REGION(total * head_dim * 64)
{
float* const sc = ws_get((size_t)std::max<int64_t>(jcap, 1));
REF_FOR_DYN
for (int64_t idx = 0; idx < total; ++idx) {
auto unit = [&](auto kl) {
const int64_t h = idx % q_heads;
const int64_t tok = idx / q_heads; /* the dense query row */
/* The owning sequence and the row's index within its query. */
int64_t s = 0, qlen = q_len, i = 0;
if (cus) {
s = seq_of(cus, n_seq, tok);
qlen = cu_at(cus, s + 1) - cu_at(cus, s);
i = tok - cu_at(cus, s);
} else {
s = tok / q_len;
i = tok - s * q_len;
}
if (qlen <= 0) return;
const int64_t kh = h / gqa;
const int64_t ctx = ld_int(su->dtype, su->data, offlin(su, s));
const int64_t pos = ctx - qlen + i;
const int64_t qb = tok * q_rs + h * q_hs;
const int64_t ob = tok * o_rs + h * o_hs;
/* A tree row: its ancestors inside the block (a bit per row) and its depth. */
const int64_t base = ctx - qlen;
bool tree = false;
uint64_t tmask = 0;
int64_t tdep = 0;
if (tpar) {
const int64_t first = tok - i;
int64_t pj = ld_int(tpar->dtype, tpar->data, offlin(tpar, tok));
if (pj != -2 && i < 64) {
tree = true;
tmask = 1ull << i;
for (int64_t at = i; pj >= 0 && pj < at; ) {
tmask |= 1ull << pj;
++tdep;
at = pj;
pj = ld_int(tpar->dtype, tpar->data, offlin(tpar, first + pj));
}
}
}
const int64_t wpos = tree ? base + tdep : pos;
const float kds = i8 ? 1.0f : kdes ? ldt(kdes, s * kv_heads + kh) : 1.0f;
const float vds = i8 ? 1.0f : vdes ? ldt(vdes, s * kv_heads + kh) : 1.0f;
/* A key's K (half 0) or V (half 1) multiplier. */
auto mul_of = [&](int64_t kb, int half) -> float {
return i8 ? kv_i8_scale(kv->data, kb, head_dim, half) : half ? vds : kds;
};
/* Keys past the block table's width are never read. */
const int64_t jend = std::min(ctx, jcap);
/* Key j's slot in the cache, or -1 when the mask hides it or its block is
* unmapped. */
auto key_at = [&](int64_t j) -> int64_t {
const int64_t mask_pos = shared_anchor_window ? base : pos;
if (causal && j > mask_pos) return -1;
if (tree && j >= base && !((tmask >> (j - base)) & 1)) return -1;
if (window > 0) {
const int64_t d = (shared_anchor_window ? base : wpos) - j;
if (shared_anchor_window) {
if (d >= window) return -1;
} else if (causal ? (d >= window) : (d >= window || d <= -window)) {
return -1;
}
}
const int64_t blk =
ld_int(bt->dtype, bt->data, s * bt_rs + (j / block_size) * bt_cs);
return blk < 0 ? -1 : blk * kvv.sb + kh * kvv.sh + (j % block_size) * kvv.so;
};
float qf[REF_HD_MAX];
for (int64_t d = 0; d < head_dim; ++d) qf[d] = ldt(q, qb + d * q_ds);
/* Pass one: every visible key's score, four keys at a time, and the maximum. */
float mx = -INFINITY;
for (int64_t j0 = 0; j0 < jend; j0 += 4) {
int64_t kb[4];
float dot[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
float km[4] = { kds, kds, kds, kds };
const int nk = (int)std::min<int64_t>(4, jend - j0);
for (int u = 0; u < 4; ++u) kb[u] = u < nk ? key_at(j0 + u) : -1;
if (i8)
for (int u = 0; u < nk; ++u) if (kb[u] >= 0) km[u] = mul_of(kb[u], 0);
if (kb[0] >= 0 && kb[1] >= 0 && kb[2] >= 0 && kb[3] >= 0) {
for (int64_t d = 0; d < head_dim; ++d) {
const float qd = qf[d];
dot[0] += qd * (kl(kv->data, kb[0] + d * kvv.sd) * km[0]);
dot[1] += qd * (kl(kv->data, kb[1] + d * kvv.sd) * km[1]);
dot[2] += qd * (kl(kv->data, kb[2] + d * kvv.sd) * km[2]);
dot[3] += qd * (kl(kv->data, kb[3] + d * kvv.sd) * km[3]);
}
} else {
for (int u = 0; u < nk; ++u) {
if (kb[u] < 0) continue;
for (int64_t d = 0; d < head_dim; ++d)
dot[u] += qf[d] * (kl(kv->data, kb[u] + d * kvv.sd) * km[u]);
}
}
for (int u = 0; u < nk; ++u) {
if (kb[u] < 0) continue;
const float sj = dot[u] * scale;
sc[j0 + u] = sj;
if (sj > mx) mx = sj;
}
}
/* No visible key (an empty sliding window) gives a zero row, not NaN, matching
* every kernel in the project. */
if (mx == -INFINITY) {
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
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;
float sum = 0.0f;
for (int64_t j = 0; j < jend; ++j) {
const int64_t kb = key_at(j);
if (kb < 0) continue;
const float w = ref_exp(sc[j] - mx);
sum += w;
const int64_t vb = kb + head_dim * kvv.sd;
const float vm = mul_of(kb, 1);
for (int64_t d = 0; d < head_dim; ++d)
acc[d] += w * (kl(kv->data, vb + d * kvv.sd) * vm);
}
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);
};
switch (kv->dtype) {
case RAD_F32: unit(KvFix<RAD_F32>{}); break;
case RAD_BF16: unit(KvFix<RAD_BF16>{}); break;
case RAD_I8: unit(KvFix<RAD_I8>{}); break;
default: unit(KvDyn{ kv->dtype }); break;
}
}
}
return RAD_OK;
}
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) {
if (!a || a->n_t < 8) return RAD_E_INVAL;
if (a->n_t > 8 && t_in(a, 8)) return RAD_E_UNSUPPORTED;
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
* one, and for a non-causal layer, whose two-sided window has no tree reading here. */
int ref_attn_paged_tree(const RadArgs* a, RadStream s) {
if (a->n_t < 9 || !t_in(a, 8)) return RAD_E_INVAL;
if (p_int(a, "causal", 1) != 1) return RAD_E_UNSUPPORTED;
return ref_attn_paged(a, s);
}
/* ------------------------------------------------------------------ attn_dense */
/* For the vision tower: unpaged, unquantised, `cu_seqlens` bounding one image (or attention window)
* per entry so a whole batch is one launch. Causal is a parameter so a dense decoder-only prefill
* can use the same kernel. */
int ref_attn_dense(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *cu = &a->t[3], *o = &a->t[4];
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);
const int causal = (int)p_int(a, "causal", 0);
/* `window` follows attn_paged's rule over a segment's own positions: causal, key j is visible
* to query i when i - j < window; non-causal, when |i - j| < window. 0 is no window. `block`
* cuts a segment into blocks of that many rows from its start, each attending only within
* itself (a chunked encoder's windows). `cu_div` reads cu_seqlens in units of that many rows:
* a segment is rows [cu[s] / cu_div, cu[s+1] / cu_div). */
const int64_t window = p_int(a, "window", 0);
/* Non-causal only: the window's right side, key j visible when j - i < window_right; 0 is
* `window`'s. A sliding encoder layer that sees `l` keys behind and `r` ahead is window l + 1,
* window_right r + 1. */
const int64_t wright = p_int(a, "window_right", 0);
const int64_t block = p_int(a, "block", 0);
const int64_t cdiv = p_int(a, "cu_div", 1);
if (head_dim <= 0 || gqa <= 0 || window < 0 || block < 0 || cdiv <= 0 || wright < 0 ||
(wright > 0 && (causal || window == 0)))
return RAD_E_SHAPE;
if (head_dim > REF_HD_MAX) return RAD_E_UNSUPPORTED;
/* [tokens, heads, head_dim], or [tokens, heads * head_dim] as a column slice of a fused
* projection with the projection's row pitch. */
const int64_t q_heads = q->rank >= 3 ? q->shape[q->rank - 2]
: (q->rank == 2 ? q->shape[1] / head_dim : 1);
const int64_t kv_heads = q_heads / gqa;
if (kv_heads <= 0 || q_heads % gqa != 0) return RAD_E_SHAPE;
const int64_t n_seq = numel(cu) - 1;
if (n_seq <= 0) return RAD_E_SHAPE;
const int64_t total_q = numel(q) / (q_heads * head_dim);
if (total_q <= 0 || cu_at(cu, n_seq) <= cu_at(cu, 0)) return RAD_E_SHAPE; /* zero rows */
if (numel(o) < total_q * q_heads * head_dim) return RAD_E_SHAPE;
if (numel(k) < total_q * kv_heads * head_dim) return RAD_E_SHAPE;
if (numel(v) < total_q * kv_heads * head_dim) return RAD_E_SHAPE;
const int64_t q_ds = q->rank >= 2 ? q->stride[q->rank - 1] : 1;
const int64_t q_hs = q->rank >= 3 ? q->stride[q->rank - 2] : head_dim * q_ds;
const int64_t q_rs = row_stride(q, q_heads * head_dim);
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1;
const int64_t k_hs = k->rank >= 3 ? k->stride[k->rank - 2] : head_dim * k_ds;
const int64_t k_rs = row_stride(k, kv_heads * head_dim);
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1;
const int64_t v_hs = v->rank >= 3 ? v->stride[v->rank - 2] : head_dim * v_ds;
const int64_t v_rs = row_stride(v, kv_heads * head_dim);
const int64_t o_ds = o->rank >= 2 ? o->stride[o->rank - 1] : 1;
const int64_t o_hs = o->rank >= 3 ? o->stride[o->rank - 2] : head_dim * o_ds;
const int64_t o_rs = row_stride(o, q_heads * head_dim);
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)head_dim));
REF_PAR_FOR(total_q * q_heads * head_dim * 64)
for (int64_t sh = 0; sh < n_seq * q_heads; ++sh) {
const int64_t s = sh / q_heads, h = sh % q_heads, kh = h / gqa;
const int64_t b0 = cu_at(cu, s) / cdiv;
const int64_t b1 = cu_at(cu, s + 1) / cdiv;
for (int64_t i = b0; i < b1; ++i) {
const int64_t qb = i * q_rs + h * q_hs, ob = i * o_rs + h * o_hs;
int64_t jbeg = b0, jend = causal ? (i + 1) : b1;
if (window > 0) {
jbeg = std::max<int64_t>(b0, i - window + 1);
if (!causal) jend = std::min<int64_t>(b1, i + (wright > 0 ? wright : window));
}
if (block > 0) {
const int64_t blk = b0 + (i - b0) / block * block;
jbeg = std::max<int64_t>(jbeg, blk);
jend = std::min<int64_t>(jend, blk + block);
}
float mx = -INFINITY;
for (int64_t j = jbeg; j < jend; ++j) {
const int64_t kb = j * k_rs + kh * k_hs;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d)
dot += ldt(q, qb + d * q_ds) * ldt(k, kb + d * k_ds);
dot *= scale;
if (dot > mx) mx = dot;
}
float acc[REF_HD_MAX];
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
if (mx == -INFINITY) {
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
continue;
}
float sum = 0.0f;
for (int64_t j = jbeg; j < jend; ++j) {
const int64_t kb = j * k_rs + kh * k_hs, vb = j * v_rs + kh * v_hs;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d)
dot += ldt(q, qb + d * q_ds) * ldt(k, kb + d * k_ds);
const float w = ref_exp(dot * scale - mx);
sum += w;
for (int64_t d = 0; d < head_dim; ++d) acc[d] += w * ldt(v, vb + d * v_ds);
}
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);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ attn_cross */
/* attn_dense with each entry's keys a run of their own: entry `s`'s queries are rows
* [cu[s], cu[s+1]) of `q` and its keys and values rows [cu_k[s], cu_k[s+1]) of `k` and `v`. With
* `causal` a query sees key j of its run when j <= its own index in its entry, both counted from
* the start: a video's rows reading its prompt (cross attention), or a padded prompt's rows of which
* the padding sees only the prompt. A query that sees no key is zero. */
int ref_attn_cross(const RadArgs* a, RadStream) {
if (!have(a, 6)) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *cu = &a->t[3], *cuk = &a->t[4];
const RadTensor *o = &a->t[5];
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);
const int causal = (int)p_int(a, "causal", 0);
if (head_dim <= 0 || gqa <= 0) return RAD_E_SHAPE;
if (head_dim > REF_HD_MAX) return RAD_E_UNSUPPORTED;
const int64_t q_heads = q->rank >= 3 ? q->shape[q->rank - 2]
: (q->rank == 2 ? q->shape[1] / head_dim : 1);
const int64_t kv_heads = q_heads / gqa;
if (kv_heads <= 0 || q_heads % gqa != 0) return RAD_E_SHAPE;
const int64_t n_seq = numel(cu) - 1;
if (n_seq <= 0 || numel(cuk) < n_seq + 1) return RAD_E_SHAPE;
const int64_t total_q = numel(q) / (q_heads * head_dim);
const int64_t total_k = numel(k) / (kv_heads * head_dim);
if (total_q <= 0 || cu_at(cu, n_seq) <= cu_at(cu, 0)) return RAD_E_SHAPE; /* zero rows */
if (numel(o) < total_q * q_heads * head_dim) return RAD_E_SHAPE;
for (int64_t s = 0; s < n_seq; ++s)
if (cu_at(cu, s + 1) < cu_at(cu, s) || cu_at(cuk, s + 1) < cu_at(cuk, s) ||
cu_at(cu, s + 1) > total_q || cu_at(cuk, s + 1) > total_k)
return RAD_E_SHAPE;
const int64_t q_ds = q->rank >= 2 ? q->stride[q->rank - 1] : 1;
const int64_t q_hs = q->rank >= 3 ? q->stride[q->rank - 2] : head_dim * q_ds;
const int64_t q_rs = row_stride(q, q_heads * head_dim);
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1;
const int64_t k_hs = k->rank >= 3 ? k->stride[k->rank - 2] : head_dim * k_ds;
const int64_t k_rs = row_stride(k, kv_heads * head_dim);
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1;
const int64_t v_hs = v->rank >= 3 ? v->stride[v->rank - 2] : head_dim * v_ds;
const int64_t v_rs = row_stride(v, kv_heads * head_dim);
const int64_t o_ds = o->rank >= 2 ? o->stride[o->rank - 1] : 1;
const int64_t o_hs = o->rank >= 3 ? o->stride[o->rank - 2] : head_dim * o_ds;
const int64_t o_rs = row_stride(o, q_heads * head_dim);
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)head_dim));
REF_PAR_FOR(total_q * q_heads * head_dim * 64)
for (int64_t sh = 0; sh < n_seq * q_heads; ++sh) {
const int64_t s = sh / q_heads, h = sh % q_heads, kh = h / gqa;
const int64_t b0 = cu_at(cu, s), b1 = cu_at(cu, s + 1);
const int64_t k0 = cu_at(cuk, s), k1 = cu_at(cuk, s + 1);
for (int64_t i = b0; i < b1; ++i) {
const int64_t qb = i * q_rs + h * q_hs, ob = i * o_rs + h * o_hs;
const int64_t jend = causal ? std::min<int64_t>(k1, k0 + (i - b0) + 1) : k1;
float mx = -INFINITY;
for (int64_t j = k0; j < jend; ++j) {
const int64_t kb = j * k_rs + kh * k_hs;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d)
dot += ldt(q, qb + d * q_ds) * ldt(k, kb + d * k_ds);
dot *= scale;
if (dot > mx) mx = dot;
}
if (mx == -INFINITY) {
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
continue;
}
float acc[REF_HD_MAX];
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
float sum = 0.0f;
for (int64_t j = k0; j < jend; ++j) {
const int64_t kb = j * k_rs + kh * k_hs, vb = j * v_rs + kh * v_hs;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d)
dot += ldt(q, qb + d * q_ds) * ldt(k, kb + d * k_ds);
const float w = ref_exp(dot * scale - mx);
sum += w;
for (int64_t d = 0; d < head_dim; ++d) acc[d] += w * ldt(v, vb + d * v_ds);
}
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);
}
}
return RAD_OK;
}
/* attn_na3d: 3-D neighborhood attention, NATTEN's na3d (LTX-2.5's diffusion decoder). The rows of
* q, k, v and out are the voxels of the volume geom = (T, H, W), i32 on the device, (t, i, j)
* row-major. A query attends the keys of a window `kt` x `kh` x `kw` (each extent at most its
* axis) centred on it and shifted inward at the border: along an axis of L and window n the
* window starts at clamp(c - n / 2, 0, L - n). Per head, in f32: each score the dot over d
* ascending times `scale` (default 1 / sqrt(head_dim)), the window's keys in raster order, the
* softmax as attn_cross takes it (the maximum, then the exponentials' sum), the output rounded to
* its dtype. Rows past T H W are not written. */
int ref_attn_na3d(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *G = &a->t[3], *o = &a->t[4];
const int64_t head_dim = p_int(a, "head_dim", 0);
const int64_t kt = p_int(a, "kt", 0), kh = p_int(a, "kh", 0), kw = p_int(a, "kw", 0);
if (head_dim <= 0 || kt <= 0 || kh <= 0 || kw <= 0 || numel(G) < 3 || q->rank != 2)
return RAD_E_SHAPE;
if (head_dim > REF_HD_MAX) return RAD_E_UNSUPPORTED;
const int64_t heads = q->shape[1] / head_dim;
const int64_t T = cu_at(G, 0), H = cu_at(G, 1), W = cu_at(G, 2);
if (heads <= 0 || q->shape[1] % head_dim || T <= 0 || H <= 0 || W <= 0) return RAD_E_SHAPE;
const int64_t N = std::min(q->shape[0], T * H * W);
if (N <= 0 || numel(k) < N * heads * head_dim || numel(v) < N * heads * head_dim ||
numel(o) < N * heads * head_dim)
return RAD_E_SHAPE;
const int64_t nt = std::min(kt, T), nh = std::min(kh, H), nw = std::min(kw, W);
const int64_t q_ds = q->stride[1], q_rs = row_stride(q, heads * head_dim);
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1, k_rs = row_stride(k, heads * head_dim);
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1, v_rs = row_stride(v, heads * head_dim);
const int64_t o_ds = o->rank >= 2 ? o->stride[o->rank - 1] : 1, o_rs = row_stride(o, heads * head_dim);
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)head_dim));
auto start = [](int64_t c, int64_t n, int64_t L) {
return std::min(std::max<int64_t>(c - n / 2, 0), L - n);
};
REF_PAR_FOR(N * heads * nt * nh * nw * head_dim)
for (int64_t rh = 0; rh < N * heads; ++rh) {
const int64_t r = rh / heads, h = rh % heads;
const int64_t t = r / (H * W), i = (r / W) % H, j = r % W;
const int64_t t0 = start(t, nt, T), i0 = start(i, nh, H), j0 = start(j, nw, W);
const int64_t qb = r * q_rs + h * head_dim * q_ds, ob = r * o_rs + h * head_dim * o_ds;
auto score = [&](int64_t key) {
const int64_t kb = key * k_rs + h * head_dim * k_ds;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d) dot += ldt(q, qb + d * q_ds) * ldt(k, kb + d * k_ds);
return dot * scale;
};
float mx = -INFINITY;
for (int64_t a1 = t0; a1 < t0 + nt; ++a1)
for (int64_t b1 = i0; b1 < i0 + nh; ++b1)
for (int64_t c1 = j0; c1 < j0 + nw; ++c1) mx = std::max(mx, score((a1 * H + b1) * W + c1));
float acc[REF_HD_MAX];
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
float sum = 0.0f;
for (int64_t a1 = t0; a1 < t0 + nt; ++a1)
for (int64_t b1 = i0; b1 < i0 + nh; ++b1)
for (int64_t c1 = j0; c1 < j0 + nw; ++c1) {
const int64_t key = (a1 * H + b1) * W + c1;
const float w = ref_exp(score(key) - mx);
sum += w;
const int64_t vb = key * v_rs + h * head_dim * v_ds;
for (int64_t d = 0; d < head_dim; ++d) acc[d] += w * ldt(v, vb + d * v_ds);
}
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);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ kv_store */
/* Writes this step's keys and values into the paged cache at `slot_mapping`. The cache is INOUT,
* not OUT: only mapped slots are touched and every other slot must survive, which the buffer
* planner needs to know (docs/OPS.md draws it as an output; this plugin deliberately differs).
*
* A negative slot is skipped: it marks a padded row with no cache slot. libr4d treats slot <= 0 as
* null because vLLM reserves block 0, but a generic reference must not steal a valid slot.
*
* `k` and `v` rows are not dense. A de-interleaved projection gives [tokens, kv_heads, head_dim];
* otherwise they are a column slice of the fused QKV plane, [tokens, kv_heads * head_dim] with the
* projection's row stride (2560 vs 256 for 16 heads and 2 kv heads). Deriving the stride from the
* shape would write every token after the first from the wrong rows, so `row_stride` reads the
* operand. */
int ref_kv_store(const RadArgs* a, RadStream) {
if (!have(a, 4)) return RAD_E_INVAL;
const RadTensor *k = &a->t[0], *v = &a->t[1], *sm = &a->t[2], *kv = &a->t[3];
const int64_t head_dim = p_int(a, "head_dim", k->rank >= 3 ? k->shape[k->rank - 1] : 0);
const int64_t block_size = p_int(a, "block_size", 0);
int64_t kv_heads = p_int(a, "n_head_kv", kv->rank == 4 ? kv->shape[1] : 0);
if (head_dim <= 0 || block_size <= 0 || kv_heads <= 0) return RAD_E_SHAPE;
if (kv->rank == 4 && (kv->shape[1] != kv_heads || kv->shape[2] != block_size ||
kv->shape[3] < 2 * head_dim)) return RAD_E_SHAPE;
const int64_t M = numel(sm);
if (M <= 0 || numel(k) < M * kv_heads * head_dim) return RAD_E_SHAPE;
const int64_t n_blocks = kv->rank == 4 ? kv->shape[0] : 0;
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1;
const int64_t k_hs = k->rank >= 3 ? k->stride[k->rank - 2] : head_dim * k_ds;
const int64_t k_rs = row_stride(k, kv_heads * head_dim);
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1;
const int64_t v_hs = v->rank >= 3 ? v->stride[v->rank - 2] : head_dim * v_ds;
const int64_t v_rs = row_stride(v, kv_heads * head_dim);
const KVView kvv = kv_view(kv, head_dim, block_size, kv_heads);
/* The int8 cache stores each head's codes and scale (kv_i8_put). An fp8 cache (GLM-5.3's
* latent attention) stores k / k_scale, where the scales are the reader's descales (default
* 1.0). A wider cache stores the value itself. */
const bool i8 = kv->dtype == RAD_I8;
if (i8 && (kv->rank != 4 || kv->shape[3] != 2 * head_dim + 4 || kv->stride[3] != 1))
return RAD_E_SHAPE;
const bool fp8 = kv->dtype == RAD_F8E4M3 || kv->dtype == RAD_F8E5M2;
const float ks = fp8 ? 1.0f / p_f32(a, "k_scale", 1.0f) : 1.0f;
const float vs = fp8 ? 1.0f / p_f32(a, "v_scale", 1.0f) : 1.0f;
for (int64_t t = 0; t < M; ++t) {
const int64_t slot = ld_int(sm->dtype, sm->data, offlin(sm, t));
if (slot >= 0 && n_blocks > 0 && slot / block_size >= n_blocks) return RAD_E_SHAPE;
}
REF_PAR_FOR(M * kv_heads * head_dim * 2)
for (int64_t t = 0; t < M; ++t) {
const int64_t slot = ld_int(sm->dtype, sm->data, offlin(sm, t));
if (slot < 0) continue;
const int64_t blk = slot / block_size, off = slot % block_size;
for (int64_t h = 0; h < kv_heads; ++h) {
const int64_t cb = blk * kvv.sb + h * kvv.sh + off * kvv.so;
if (i8) {
kv_i8_put(kv->data, cb, head_dim, 0, k, t * k_rs + h * k_hs, k_ds);
kv_i8_put(kv->data, cb, head_dim, 1, v, t * v_rs + h * v_hs, v_ds);
continue;
}
for (int64_t d = 0; d < head_dim; ++d) {
stt(kv, cb + d * kvv.sd, ldt(k, t * k_rs + h * k_hs + d * k_ds) * ks);
stt(kv, cb + (head_dim + d) * kvv.sd, ldt(v, t * v_rs + h * v_hs + d * v_ds) * vs);
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ kv_store_streamed */
/* The store of a streamed group (docs/OPS.md "kv_store_streamed"): `kv_store` into both of the
* group's copies. host[b], the authoritative mirror, is written always; kv_cache[res[b]] only when
* the block is resident. The writer must test residency: at decode the block a step writes is
* usually the one the previous step made resident, so a mirror-only writer would leave the ring's
* copy missing this step's tokens, and the resolve, seeing it resident, would not copy it. The
* caller's order is store, resolve, attention. The planes are the same paged layout at the same
* pitch, so one conversion writes both. `res` is read, never written; an entry outside
* [-1, slots) for a touched block, or a slot past the id space, is refused before any write. */
int ref_kv_store_streamed(const RadArgs* a, RadStream) {
if (!have(a, 6)) return RAD_E_INVAL;
const RadTensor *k = &a->t[0], *v = &a->t[1], *sm = &a->t[2], *kv = &a->t[3];
const RadTensor *res = &a->t[4], *host = &a->t[5];
const int64_t head_dim = p_int(a, "head_dim", k->rank >= 3 ? k->shape[k->rank - 1] : 0);
const int64_t block_size = p_int(a, "block_size", 0);
const int64_t kv_heads = p_int(a, "n_head_kv", 0);
if (head_dim <= 0 || block_size <= 0 || kv_heads <= 0) return RAD_E_SHAPE;
if (k->dtype != RAD_BF16 || v->dtype != RAD_BF16) return RAD_E_DTYPE;
if (sm->dtype != RAD_I32 || res->dtype != RAD_I32) return RAD_E_DTYPE;
if (host->dtype != RAD_U8) return RAD_E_DTYPE;
/* The int8 cache, bf16 or E4M3: a bf16 group stores the value itself, as kv_store_bf16 does,
* the int8 one each head's codes and scale, an E4M3 one (GLM-5.3's latent attention) the
* value over the reader's descale. */
const bool i8 = kv->dtype == RAD_I8, fp8 = kv->dtype == RAD_F8E4M3;
if (!i8 && !fp8 && kv->dtype != RAD_BF16) return RAD_E_UNSUPPORTED;
const float ks = fp8 ? 1.0f / p_f32(a, "k_scale", 1.0f) : 1.0f;
const float vs = fp8 ? 1.0f / p_f32(a, "v_scale", 1.0f) : 1.0f;
if (kv->rank != 4) return RAD_E_SHAPE;
const int64_t rowe = i8 ? 2 * head_dim + 4 : 2 * head_dim; /* a token's row in a head */
if (kv->shape[1] != kv_heads || kv->shape[2] != block_size || kv->shape[3] != rowe ||
kv->stride[3] != 1) return RAD_E_SHAPE;
if (host->rank != 2) return RAD_E_SHAPE;
/* Both planes are dense rows of one block of one layer: a padded mirror row would copy over its
* neighbour, and the two copies must share offsets. */
const int64_t row = kv_heads * block_size * rowe * (kv->dtype == RAD_BF16 ? 2 : 1);
const int64_t slots = kv->shape[0], blocks = host->shape[0];
if (host->shape[1] != row || host->stride[0] * rad_dtype_bytes(host->dtype, 1) != row)
return RAD_E_STRIDE;
if (kv->stride[0] * rad_dtype_bytes(kv->dtype, 1) != row) return RAD_E_STRIDE;
if (slots <= 0 || blocks <= 0) return RAD_E_SHAPE;
if (numel(res) < blocks || res->stride[0] != 1) return RAD_E_SHAPE;
const int64_t M = numel(sm);
if (M <= 0 || numel(k) < M * kv_heads * head_dim || numel(v) < M * kv_heads * head_dim)
return RAD_E_SHAPE;
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1;
const int64_t k_hs = k->rank >= 3 ? k->stride[k->rank - 2] : head_dim * k_ds;
const int64_t k_rs = row_stride(k, kv_heads * head_dim);
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1;
const int64_t v_hs = v->rank >= 3 ? v->stride[v->rank - 2] : head_dim * v_ds;
const int64_t v_rs = row_stride(v, kv_heads * head_dim);
/* Everything that can refuse is checked before the first write: a half-written mirror would
* make later copies plausible and wrong. */
const int32_t* const R = (int32_t*)res->data;
for (int64_t t = 0; t < M; ++t) {
const int64_t s = ld_int(sm->dtype, sm->data, offlin(sm, t));
if (s < 0) continue; /* padded row, no cache slot */
const int64_t blk = s / block_size;
if (blk >= blocks) return RAD_E_SHAPE;
if (R[blk] < -1 || R[blk] >= slots) return RAD_E_INVAL;
}
/* The mirror seen as the plane it holds, so both copies round the same. A view's strides are
* in the plane's elements: the mirror's row pitch is `row` bytes. */
RadTensor hkv = *kv;
hkv.data = host->data;
hkv.shape[0] = blocks;
const KVView kvv = kv_view(kv, head_dim, block_size, kv_heads);
const KVView hv = kv_view(&hkv, head_dim, block_size, kv_heads);
REF_PAR_FOR(M * kv_heads * head_dim * 2)
for (int64_t t = 0; t < M; ++t) {
const int64_t s = ld_int(sm->dtype, sm->data, offlin(sm, t));
if (s < 0) continue;
const int64_t blk = s / block_size, off = s % block_size;
const int64_t hb = blk * hv.sb + off * hv.so;
/* A non-resident block is written to the mirror alone; the resolve copies it in whole. */
const int64_t cb = R[blk] >= 0 ? R[blk] * kvv.sb + off * kvv.so : -1;
for (int64_t h = 0; h < kv_heads; ++h) {
const int64_t kx = t * k_rs + h * k_hs, vx = t * v_rs + h * v_hs;
if (i8) {
kv_i8_put(hkv.data, hb + h * hv.sh, head_dim, 0, k, kx, k_ds);
kv_i8_put(hkv.data, hb + h * hv.sh, head_dim, 1, v, vx, v_ds);
if (cb >= 0) {
kv_i8_put(kv->data, cb + h * kvv.sh, head_dim, 0, k, kx, k_ds);
kv_i8_put(kv->data, cb + h * kvv.sh, head_dim, 1, v, vx, v_ds);
}
continue;
}
for (int64_t d = 0; d < head_dim; ++d) {
const float xk = ldt(k, kx + d * k_ds) * ks;
const float xv = ldt(v, vx + d * v_ds) * vs;
stt(&hkv, hb + h * hv.sh + d * hv.sd, xk);
stt(&hkv, hb + h * hv.sh + (head_dim + d) * hv.sd, xv);
if (cb >= 0) {
stt(kv, cb + h * kvv.sh + d * kvv.sd, xk);
stt(kv, cb + h * kvv.sh + (head_dim + d) * kvv.sd, xv);
}
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ rope */
namespace {
/* NeoX pairs element i with i + rotary_dim/2; GPT-J ("interleaved") pairs 2i with 2i+1. The
* checkpoint decides, so it is a parameter: the wrong one yields fluent text with broken position
* sense, which is hard to notice.
*
* The multi-component modes pair like NeoX and differ in which position component drives each
* frequency (i indexes the rot/2 frequencies):
* mrope contiguous sections: frequencies [0, s0) take component 0, the next s1 component 1...
* (Qwen2-VL), all on one ladder theta^(-2i/rot).
* imrope interleaved (Qwen3-VL): component 1 where i % 3 == 1 and i < 3*s1, component 2 where
* i % 3 == 2 and i < 3*s2, component 0 everywhere else; one ladder.
* axial contiguous sections, each on its own ladder theta^(-2j/(2*s_c)) for its j-th
* frequency: the vision tower's 2-D rotary embedding over (row, column).
* With one position component per token (rank-1 `positions`) every component is the same, and
* mrope and imrope reduce to neox. */
enum { ROPE_NEOX = 0, ROPE_GPTJ = 1, ROPE_MROPE = 2, ROPE_IMROPE = 3, ROPE_AXIAL = 4 };
struct RopeFreq {
int comp[REF_HD_MAX / 2];
float inv[REF_HD_MAX / 2];
};
/* The component and inverse frequency of each of the rot/2 frequencies. `den` is the width the base
* ladder is measured in: `rot` for a `rotary` ladder (a partial rotary is a full rotation of its
* own width), `head_dim` for the proportional `head` variant. The loop count and pairing still come
* from `rot`; `axial` overrides `inv`. */
static int rope_freqs(int mode, int64_t rot, int64_t den, float theta, const int64_t* sec,
int n_sec, RopeFreq* f) {
const int64_t half = rot / 2;
if (half > REF_HD_MAX / 2) return RAD_E_UNSUPPORTED;
int64_t start[8] = {}, total = 0;
for (int c = 0; c < n_sec; ++c) { start[c] = total; total += sec[c]; }
if ((mode == ROPE_MROPE || mode == ROPE_AXIAL) && n_sec > 0 && total != half) return RAD_E_SHAPE;
for (int64_t i = 0; i < half; ++i) {
int c = 0;
float inv = 1.0f / ref_pow(theta, (float)(2 * i) / (float)den);
if (n_sec > 0 && (mode == ROPE_MROPE || mode == ROPE_AXIAL)) {
while (c + 1 < n_sec && i >= start[c + 1]) ++c;
if (mode == ROPE_AXIAL) {
const int64_t j = i - start[c];
inv = 1.0f / ref_pow(theta, (float)(2 * j) / (float)(2 * sec[c]));
}
} else if (n_sec >= 3 && mode == ROPE_IMROPE) {
if (i % 3 == 1 && i < 3 * sec[1]) c = 1;
else if (i % 3 == 2 && i < 3 * sec[2]) c = 2;
}
f->comp[i] = c;
f->inv[i] = inv;
}
return RAD_OK;
}
static void rope_head(const RadTensor* t, int64_t base, int64_t ds, int64_t rot, int mode,
const RopeFreq& f, const float* pos, float magnitude = 1.0f) {
const int64_t half = rot / 2;
for (int64_t i = 0; i < half; ++i) {
const float ang = pos[f.comp[i]] * f.inv[i];
const float c = ref_cos(ang) * magnitude, s = ref_sin(ang) * magnitude;
const int64_t i0 = (mode == ROPE_GPTJ) ? 2 * i : i;
const int64_t i1 = (mode == ROPE_GPTJ) ? 2 * i + 1 : i + half;
const float x0 = ldt(t, base + i0 * ds), x1 = ldt(t, base + i1 * ds);
stt(t, base + i0 * ds, x0 * c - x1 * s);
stt(t, base + i1 * ds, x0 * s + x1 * c);
}
}
/* A position as the rotation reads it, divided by the position scale in f32: an integer operand's
* value, or a float operand's own value (a fractional coordinate, such as a video decoder's
* normalised grid already multiplied by 2 pi). */
static inline float rope_pos(const RadTensor* pos, int64_t off, float div) {
const uint32_t dt = pos->dtype;
const bool flt = dt == RAD_F32 || dt == RAD_BF16 || dt == RAD_F16;
return (flt ? ld_dt(dt, pos->data, off) : (float)ld_int(dt, pos->data, off)) / div;
}
/* "11 11 10" -> {11, 11, 10}. Absent is no sections. */
static int rope_sections(const RadArgs* a, int64_t* sec) {
const char* v = p_str(a, "sections", nullptr);
int n = 0;
while (v && *v && n < 8) {
char* end = nullptr;
const long long x = std::strtoll(v, &end, 10);
if (end == v) break;
if (x <= 0) return -1;
sec[n++] = x;
v = end;
}
return n;
}
/* `ladder` names the width the base frequency ladder is measured in (see rope_freqs): absent or
* `rotary` divides by `rot`, `head` by `head_dim`. An unknown value is refused, since a mistyped
* ladder would silently give wrong angles. */
static int rope_ladder(const RadArgs* a, int64_t rot, int64_t head_dim, int64_t* den) {
const char* lad = p_str(a, "ladder", nullptr);
if (!lad) { *den = rot; return RAD_OK; }
if (!std::strcmp(lad, "rotary")) { *den = rot; return RAD_OK; }
if (!std::strcmp(lad, "head")) {
if (head_dim <= 0) return RAD_E_SHAPE;
*den = head_dim;
return RAD_OK;
}
return RAD_E_UNSUPPORTED;
}
} /* namespace */
/* Ministral 3's query multiplier, applied after rotary and rounded to the activation dtype
* before the product. Only the supplied query slice is changed; keys and values stay intact. */
int ref_position_scale(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *pos = &a->t[1];
// M in the resolved geometry is a capacity band; issued views carry the active rows.
const int64_t M = x->rank == 2 ? x->shape[0] : 0;
const int64_t n = p_int(a, "n", 0), orig = p_int(a, "original", 0);
const float beta = p_f32(a, "beta", -1);
if (M <= 0 || M > p_int(a, "M", 0) || n <= 0 || orig <= 0 || !std::isfinite(beta) || beta < 0 ||
x->rank != 2 || x->shape[0] < M || x->shape[1] != n || pos->rank != 1 || numel(pos) < M)
return RAD_E_SHAPE;
if (pos->dtype != RAD_I32 || (x->dtype != RAD_BF16 && x->dtype != RAD_F32)) return RAD_E_DTYPE;
/* Validate all positions before writing: an invalid batch must not leave a partial result. */
for (int64_t r = 0; r < M; ++r)
if (ld_int(pos->dtype, pos->data, offlin(pos, r)) < 0) return RAD_E_INVAL;
REF_PAR_FOR(M * n)
for (int64_t r = 0; r < M; ++r) {
const int64_t p = ld_int(pos->dtype, pos->data, offlin(pos, r));
const float scale = round_dt(x->dtype, 1.0f + beta * std::log(1.0f + (float)(p / orig)));
for (int64_t j = 0; j < n; ++j) {
const int64_t at = rowoff(x, r, n) + j * laststride(x);
stt(x, at, ldt(x, at) * scale);
}
}
return RAD_OK;
}
int ref_rope(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *qkv = &a->t[0], *pos = &a->t[1];
const int64_t head_dim = p_int(a, "head_dim", 0);
const int64_t n_head = p_int(a, "n_head", 0);
const int64_t n_head_kv = p_int(a, "n_head_kv", 0);
const float theta = p_f32(a, "theta", 10000.0f);
const float rscale = p_f32(a, "scale", 1.0f);
static const char* kModes[] = { "neox", "gptj", "mrope", "imrope", "axial" };
const int mode = p_enum(a, "mode", kModes, 5, -1);
if (mode < 0) return RAD_E_UNSUPPORTED;
int64_t rot = p_int(a, "rotary_dim", head_dim);
if (head_dim <= 0 || n_head <= 0 || rot <= 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE;
int64_t den;
RAD_REF_TRY(rope_ladder(a, rot, head_dim, &den));
int64_t sec[8] = {};
const int n_sec = rope_sections(a, sec);
if (n_sec < 0) return RAD_E_SHAPE;
/* Positions: [M] is one component per token; [C, M] is C components, component-major. */
const bool multi = pos->rank >= 2;
const int64_t n_comp = multi ? pos->shape[0] : 1;
const int64_t M = multi ? pos->shape[1] : numel(pos);
if (multi) {
if (mode == ROPE_NEOX || mode == ROPE_GPTJ || n_sec <= 0) return RAD_E_SHAPE;
const int64_t need = mode == ROPE_IMROPE ? 3 : n_sec;
if (n_comp < need || n_comp > 8) return RAD_E_SHAPE;
}
/* The proportional ladder (`head`) is HF's: the rotation spans the whole head, pair i being
* (i, i + head_dim/2), and frequencies past rotary_dim/2 are zero so those pairs do not turn.
* A rotary of rotary_dim's own width, pairing (i, i + rot/2), would turn other channels. */
const int64_t nfreq = rot / 2;
if (den == head_dim && rot < head_dim && !multi && mode == ROPE_NEOX) rot = head_dim;
RopeFreq f;
RAD_REF_TRY(rope_freqs(mode, rot, den, theta, sec, multi ? n_sec : 0, &f));
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn)) return RAD_E_INVAL;
if (yarn.factor != 0.0f) {
if (mode == ROPE_AXIAL || rscale != 1.0f) return RAD_E_UNSUPPORTED;
for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i);
}
const RadLlama3 l3 = rad_llama3_args(a);
if (!rad_llama3_valid(l3) || (l3.factor != 0.0f && yarn.factor != 0.0f)) return RAD_E_INVAL;
if (l3.factor != 0.0f) {
if (mode == ROPE_AXIAL || multi) return RAD_E_UNSUPPORTED;
for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = l3.frequency(f.inv[i]);
}
for (int64_t i = nfreq; i < rot / 2; ++i) f.inv[i] = 0.0f;
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
if (M <= 0 || numel(qkv) < M * stride_row) return RAD_E_SHAPE;
const int64_t rs = row_stride(qkv, stride_row);
const int64_t ds = laststride(qkv);
const int64_t pcs = multi ? pos->stride[0] : 0, pms = multi ? pos->stride[1] : 0;
REF_PAR_FOR(M * stride_row)
for (int64_t m = 0; m < M; ++m) {
float p[8];
const float div = rscale != 0.0f ? rscale : 1.0f;
if (multi) {
for (int64_t c = 0; c < n_comp; ++c) p[c] = rope_pos(pos, c * pcs + m * pms, div);
} else {
const float one = rope_pos(pos, offlin(pos, m), div);
for (int c = 0; c < 8; ++c) p[c] = one;
}
const int64_t row = m * rs;
/* Q heads then K heads; the V heads after them are left alone. */
for (int64_t h = 0; h < n_head + n_head_kv; ++h)
rope_head(qkv, row + h * head_dim * ds, ds, rot, mode, f, p, yarn.magnitude);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ qk_norm_rope */
/* QK-norm and rotation in one op: an RMS norm over each head_dim-wide q and k head against its own
* gain vector, then RoPE. `v_norm` 1 also RMS-norms each v head with unit gain and no rotation
* (Gemma 4's weightless value norm); otherwise v heads are untouched. An absent gain is a unit
* gain, without `wadd` (a video decoder's weightless q and k norms); `rotary_dim` 0 is the norm
* alone, and then `positions` may be absent.
*
* The norm output is rounded to the tensor's dtype before the rotation, as the unfused pair does;
* libr4d's r4d_qk_norm_rope_gate matches this. A fused kernel keeping f32 across the seam would be
* half a ULP better and fail the comparison. */
int ref_qk_norm_rope(const RadArgs* a, RadStream) {
if (a->n_t < 4 || !t_in(a, 0)) return RAD_E_INVAL;
const RadTensor *qkv = &a->t[0], *pos = t_in(a, 1), *qw = t_in(a, 2), *kw = t_in(a, 3);
const int64_t head_dim = p_int(a, "head_dim", 0);
const int64_t n_head = p_int(a, "n_head", 0);
const int64_t n_head_kv = p_int(a, "n_head_kv", 0);
const float theta = p_f32(a, "theta", 10000.0f);
const float eps = p_f32(a, "eps", 1e-6f);
const float rscale = p_f32(a, "scale", 1.0f);
const float wadd = p_f32(a, "wadd", 0.0f);
const int v_norm = (int)p_int(a, "v_norm", 0);
/* `gptj_blocks` is `blocks` with each section's pairs adjacent, (start + 2j, start + 2j + 1):
* the complex-number form of Qwen-Image's 3-axis rotary. `imrope` is `rope`'s interleaved
* M-RoPE over NeoX pairs, after the norm (Qwen3-VL text attention). `axial` is `rope`'s axial
* form (a 3-axis video rotary over NeoX pairs), its section ladders applied with rank-1
* positions too, every component then being that one position. */
static const char* kModes[] = { "neox", "gptj", "blocks", "gptj_blocks", "imrope", "axial" };
const int mode = p_enum(a, "mode", kModes, 6, 0);
if (mode < 0) return RAD_E_UNSUPPORTED;
const bool blocks = mode == 2 || mode == 3;
const bool adjacent = mode == 3;
const bool im = mode == 4;
const bool axial = mode == 5;
int64_t rot = p_int(a, "rotary_dim", head_dim);
if (head_dim <= 0 || n_head <= 0 || rot < 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE;
if ((qw && numel(qw) < head_dim) || (kw && numel(kw) < head_dim)) return RAD_E_SHAPE;
const bool turn = rot > 0;
if (turn && !pos) return RAD_E_INVAL;
/* Blocks: section c is `sec[c]` rotary pairs over its own run of 2 * sec[c] channels, paired
* (start + j, start + sec[c] + j) and turned by position component comps[c] at
* theta^(-j / sec[c]), i.e. a complete neox rotary of its own width. The sections fill the
* rotated prefix exactly. Positions are [C, M], component-major. */
int64_t sec[8] = {}, comp[8] = {};
int n_sec = 0;
if (turn && (im || axial)) {
n_sec = rope_sections(a, sec);
if (n_sec < (im ? 3 : 1)) return RAD_E_SHAPE;
}
if (turn && blocks) {
n_sec = rope_sections(a, sec);
if (n_sec <= 0) return RAD_E_SHAPE;
int64_t total = 0;
for (int c = 0; c < n_sec; ++c) { total += sec[c]; comp[c] = c; }
if (2 * total != rot) return RAD_E_SHAPE;
if (const char* cs = p_str(a, "comps", nullptr)) {
int k = 0;
for (const char* v = cs; *v && k < 8; ) {
char* end = nullptr;
const long long x = std::strtoll(v, &end, 10);
if (end == v) break;
comp[k++] = x;
v = end;
}
if (k != n_sec) return RAD_E_SHAPE;
}
}
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
const bool multi = pos && pos->rank >= 2;
const int64_t n_comp = multi ? pos->shape[0] : 1;
const int64_t M = !pos ? rows_of(qkv, stride_row) : multi ? pos->shape[1] : numel(pos);
if (turn) {
if (multi && !blocks && !im && !axial) return RAD_E_SHAPE;
if (multi && im && n_comp < 3) return RAD_E_SHAPE;
if (multi && axial && n_comp < n_sec) return RAD_E_SHAPE;
for (int c = 0; blocks && c < n_sec; ++c)
if (comp[c] < 0 || comp[c] >= n_comp) return RAD_E_SHAPE;
}
const int64_t pcs = multi ? pos->stride[0] : 0, pms = multi ? pos->stride[1] : 0;
if (M <= 0 || numel(qkv) < M * stride_row) return RAD_E_SHAPE;
const int64_t rs = row_stride(qkv, stride_row);
const int64_t ds = laststride(qkv);
RopeFreq f;
/* No `ladder` parameter here: the fused norm+rope measures by `rot`. */
if (turn && im)
RAD_REF_TRY(rope_freqs(ROPE_IMROPE, rot, rot, theta, sec, multi ? n_sec : 0, &f));
else if (turn && axial)
RAD_REF_TRY(rope_freqs(ROPE_AXIAL, rot, rot, theta, sec, n_sec, &f));
else if (turn)
RAD_REF_TRY(rope_freqs(blocks ? 0 : mode, rot, rot, theta, nullptr, 0, &f));
const int pair_mode = im ? (int)ROPE_IMROPE : axial ? (int)ROPE_AXIAL : mode;
/* RMS-norms one head in place, against gain `w` (+ wadd), or unit gain without one. */
auto norm_head = [&](int64_t base, const RadTensor* w) {
float ss = 0.0f;
for (int64_t d = 0; d < head_dim; ++d) {
const float x = ldt(qkv, base + d * ds);
ss += x * x;
}
const float sc = 1.0f / std::sqrt(ss / (float)head_dim + eps);
for (int64_t d = 0; d < head_dim; ++d) {
const float x = ldt(qkv, base + d * ds) * sc;
stt(qkv, base + d * ds, w ? x * (ldt(w, d * laststride(w)) + wadd) : x);
}
};
REF_PAR_FOR(M * stride_row)
for (int64_t m = 0; m < M; ++m) {
float p[8];
const float div = rscale != 0.0f ? rscale : 1.0f;
if (turn && multi) {
for (int64_t c = 0; c < n_comp && c < 8; ++c)
p[c] = rope_pos(pos, c * pcs + m * pms, div);
} else if (turn) {
const float one = rope_pos(pos, offlin(pos, m), div);
for (int c = 0; c < 8; ++c) p[c] = one;
}
const int64_t row = m * rs;
for (int64_t h = 0; h < n_head + n_head_kv; ++h) {
const int64_t base = row + h * head_dim * ds;
norm_head(base, h < n_head ? qw : kw);
if (!turn) continue;
if (!blocks) {
rope_head(qkv, base, ds, rot, pair_mode, f, p);
continue;
}
int64_t start = 0;
for (int c = 0; c < n_sec; ++c) {
for (int64_t j = 0; j < sec[c]; ++j) {
const float inv = 1.0f / ref_pow(theta, (float)j / (float)sec[c]);
const float ang = p[comp[c]] * inv;
const float cs = ref_cos(ang), sn = ref_sin(ang);
const int64_t i0 = adjacent ? start + 2 * j : start + j;
const int64_t i1 = adjacent ? start + 2 * j + 1 : start + sec[c] + j;
const float x0 = ldt(qkv, base + i0 * ds), x1 = ldt(qkv, base + i1 * ds);
stt(qkv, base + i0 * ds, x0 * cs - x1 * sn);
stt(qkv, base + i1 * ds, x0 * sn + x1 * cs);
}
start += 2 * sec[c];
}
}
for (int64_t h = 0; v_norm && h < n_head_kv; ++h)
norm_head(row + (n_head + n_head_kv + h) * head_dim * ds, nullptr);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ attn_local_rel */
/* Local attention with a relative-position term: Gemma 4's audio (USM conformer) attention.
*
* Query row i of a cu_seqlens segment sees the key rows j of the same segment with
* 0 <= i - j <= left (`right` must be 0). Per head:
*
* qf[d] = q[i,d] * (head_dim^-0.5 / ln 2) * softplus(pds[d])
* logit = sum_d qf[d] * k[j,d] * (ln(1 + e) / ln 2) + sum_d qf[d] * relk[i - j, d]
* logit = softcap * tanh(logit / softcap)
*
* then a softmax over those keys and the weighted sum of v, all in f32 and rounded once. `relk`
* is [left + 1, n_head * head_dim]: row d is the projected relative position for distance d.
*
* The upstream model computes this in 12-query blocks over 24-key windows with Transformer-XL's
* rel_shift; keys outside 0 <= i - j <= left get a -1e9 logit and zero weight there, so this
* sliding form is the same function. softplus(pds) is rounded to the weight's dtype before use,
* as upstream evaluates it on the bf16 parameter. */
int ref_attn_local_rel(const RadArgs* a, RadStream) {
if (!have(a, 7)) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *rk = &a->t[3];
const RadTensor *pds = &a->t[4], *cu = &a->t[5], *o = &a->t[6];
const int64_t D = p_int(a, "head_dim", 0), H = p_int(a, "n_head", 0);
const int64_t L = p_int(a, "left", 0), R = p_int(a, "right", 0);
const int64_t cdiv = p_int(a, "cu_div", 1);
const float cap = p_f32(a, "softcap", 0.0f);
if (D <= 0 || H <= 0 || L < 0 || cdiv <= 0 || !(cap > 0.0f)) return RAD_E_SHAPE;
if (R != 0) return RAD_E_UNSUPPORTED;
if (D > REF_HD_MAX || L + 1 > REF_HD_MAX) return RAD_E_UNSUPPORTED;
const int64_t W = H * D;
if (numel(pds) < D || numel(rk) < (L + 1) * W) return RAD_E_SHAPE;
const int64_t S = numel(cu) - 1;
if (S <= 0) return RAD_E_SHAPE;
const int64_t Mq = rows_of(q, W), Mk = rows_of(k, W), Mv = rows_of(v, W), Mo = rows_of(o, W);
if (Mq <= 0 || Mo <= 0 || cu_at(cu, S) <= cu_at(cu, 0)) return RAD_E_SHAPE; /* zero rows */
const float qs = (float)(1.0 / std::sqrt((double)D) / std::log(2.0));
const float ks = (float)(std::log(1.0 + std::exp(1.0)) / std::log(2.0));
float spd[REF_HD_MAX];
for (int64_t d = 0; d < D; ++d)
spd[d] = round_dt(pds->dtype, act_softplus(ldt(pds, d * laststride(pds)), 20.0f));
for (int64_t s = 0; s < S; ++s) {
const int64_t c0 = cu_at(cu, s);
const int64_t c1 = cu_at(cu, s + 1);
if (c0 % cdiv || c1 % cdiv || c1 < c0) return RAD_E_SHAPE;
const int64_t t0 = c0 / cdiv, t1 = c1 / cdiv;
if (t1 > Mq || t1 > Mk || t1 > Mv || t1 > Mo) return RAD_E_SHAPE;
REF_PAR_FOR((t1 - t0) * H * D * (L + R + 1))
for (int64_t ih = 0; ih < (t1 - t0) * H; ++ih) {
const int64_t i = t0 + ih / H, h = ih % H;
float qf[REF_HD_MAX], lg[REF_HD_MAX], acc[REF_HD_MAX];
const int64_t qb = rowoff(q, i, W) + h * D * laststride(q);
for (int64_t d = 0; d < D; ++d) qf[d] = ldt(q, qb + d * laststride(q)) * qs * spd[d];
const int64_t j0 = i - L > t0 ? i - L : t0;
float mx = -INFINITY;
for (int64_t j = j0; j <= i; ++j) {
const int64_t kb = rowoff(k, j, W) + h * D * laststride(k);
const int64_t rb = rowoff(rk, i - j, W) + h * D * laststride(rk);
float ac = 0.0f, bd = 0.0f;
for (int64_t d = 0; d < D; ++d) {
ac += qf[d] * (ldt(k, kb + d * laststride(k)) * ks);
bd += qf[d] * ldt(rk, rb + d * laststride(rk));
}
const float x = cap * ref_tanh((ac + bd) / cap);
lg[j - j0] = x;
mx = x > mx ? x : mx;
}
float sum = 0.0f;
for (int64_t j = j0; j <= i; ++j) {
lg[j - j0] = ref_exp(lg[j - j0] - mx);
sum += lg[j - j0];
}
for (int64_t d = 0; d < D; ++d) acc[d] = 0.0f;
for (int64_t j = j0; j <= i; ++j) {
const float p = lg[j - j0] / sum;
const int64_t vb = rowoff(v, j, W) + h * D * laststride(v);
for (int64_t d = 0; d < D; ++d) acc[d] += p * ldt(v, vb + d * laststride(v));
}
const int64_t ob = rowoff(o, i, W) + h * D * laststride(o);
for (int64_t d = 0; d < D; ++d) stt(o, ob + d * laststride(o), acc[d]);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ rope_table */
/* The cos/sin plane a fused prologue reads instead of theta, written at each position's own row of
* a [max_ctx, rot] table: cos in the first rot/2 values of a row, sin in the second.
*
* The angles must match `rope`'s to the last operation; any other construction gives subtly wrong
* positions. Only rows named by `positions` are written; a negative position writes nothing. */
int ref_rope_table(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *pos = &a->t[0], *cs = &a->t[1];
const int64_t rot = p_int(a, "rot", 0);
const int64_t head_dim = p_int(a, "head_dim", 0);
const float theta = p_f32(a, "theta", 10000.0f);
const float rscale = p_f32(a, "scale", 1.0f);
if (rot < 2 || (rot & 1)) return RAD_E_SHAPE;
int64_t den;
RAD_REF_TRY(rope_ladder(a, rot, head_dim, &den));
const int64_t M = numel(pos);
if (M <= 0 || numel(cs) % rot) return RAD_E_SHAPE;
const int64_t rows = numel(cs) / rot;
const int64_t half = rot / 2;
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && rscale != 1.0f)) return RAD_E_INVAL;
REF_PAR_FOR(M * rot)
for (int64_t m = 0; m < M; ++m) {
const int64_t pp = ld_int(pos->dtype, pos->data, offlin(pos, m));
if (pp < 0 || pp >= rows) continue;
const float p = (float)pp / (rscale != 0.0f ? rscale : 1.0f);
for (int64_t i = 0; i < half; ++i) {
const float inv = yarn.frequency(1.0f / ref_pow(theta, (float)(2 * i) / (float)den), (int)i);
const float ang = p * inv;
stt(cs, offlin(cs, pp * rot + i), ref_cos(ang) * yarn.magnitude);
stt(cs, offlin(cs, pp * rot + half + i), ref_sin(ang) * yarn.magnitude);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ qsa_block_key */
/* The QSA indexer's compressed key (docs/OPS.md, docs/QSA.md): for each of M blocks,
*
* pooled = mean of the block's `ratio` rows of k, accumulated in f32 and rounded to k's dtype
* normed = pooled * rsqrt(mean(pooled^2) + eps) * (wadd + w)
* out = NeoX rotary of normed's first `rotary_dim` lanes at the block's position, the rest
* passed through, rounded once at the store
*
* The rounding after the mean is the upstream model's (`.float().mean(dim=1).to(dtype)`). The
* block's position is `pos[b]` (a [>= M, 1 or 3] list holds a block's components side by side,
* read under `mode` mrope/imrope and `sections`), or `pos0 + b*ratio` without one. `page[b]`, when
* given, is the output row; a negative entry writes nothing. The rotary is `rope`'s ladder, with
* YaRN's blend and magnitude when its coefficients are given. */
int ref_qsa_block_key(const RadArgs* a, RadStream) {
const RadTensor* k = t_in(a, 0);
const RadTensor* w = t_in(a, 1);
const RadTensor* page = t_in(a, 2);
const RadTensor* pos = t_in(a, 3);
const RadTensor* out = t_in(a, 4);
if (!k || !w || !out) return RAD_E_INVAL;
const int64_t n = p_int(a, "n", 0), ratio = p_int(a, "ratio", 0);
const int64_t rot = p_int(a, "rotary_dim", 0), pos0 = p_int(a, "pos0", -1);
if (n <= 0 || n > REF_HD_MAX || ratio <= 0 || (pos0 < 0 && !pos)) return RAD_E_INVAL;
if (rot < 0 || rot > n || (rot & 1)) return RAD_E_SHAPE;
if (p_int(a, "block_size", ratio) != ratio) return RAD_E_SHAPE;
if (k->rank != 2 || out->rank < 2 || out->shape[0] < 1) return RAD_E_SHAPE;
const int64_t M = page ? numel(page) : out->shape[0];
if (M < 1 || numel(out) / out->shape[0] != n) return RAD_E_SHAPE;
if (k->shape[0] != M * ratio || k->shape[1] != n || numel(w) < n) return RAD_E_SHAPE;
const bool pos2 = pos && pos->rank == 2;
if (pos && (pos2 ? pos->shape[0] < M : numel(pos) < M)) return RAD_E_SHAPE;
const float eps = p_f32(a, "eps", 1e-6f), wadd = p_f32(a, "wadd", 1.0f);
const float theta = p_f32(a, "theta", 10000.0f), rscale = p_f32(a, "scale", 1.0f);
static const char* kModes[] = { "neox", "mrope", "imrope" };
const int mi = p_enum(a, "mode", kModes, 3, 0);
if (mi < 0) return RAD_E_UNSUPPORTED;
const int mode = mi == 1 ? ROPE_MROPE : mi == 2 ? ROPE_IMROPE : ROPE_NEOX;
int64_t sec[8] = {};
const int n_sec = rope_sections(a, sec);
if (n_sec < 0 || n_sec > 3) return RAD_E_SHAPE;
/* Multiple components need a mode, its sections and three columns of `pos`; otherwise every
* frequency reads the block's single position, which reduces every mode to NeoX. */
const bool mc = mode != ROPE_NEOX && n_sec > 0 && pos2 && pos->shape[1] >= 3;
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && (rot <= 0 || rscale != 1.0f)))
return RAD_E_INVAL;
RopeFreq f;
RAD_REF_TRY(rope_freqs(mc ? mode : ROPE_NEOX, rot, rot, theta, sec, mc ? n_sec : 0, &f));
for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i);
REF_PAR_FOR(M * n * ratio)
for (int64_t b = 0; b < M; ++b) {
const int64_t orow = page ? ld_int(page->dtype, page->data, offlin(page, b)) : b;
if (orow < 0) continue;
float x[REF_HD_MAX], y[REF_HD_MAX];
double ss = 0.0;
for (int64_t t = 0; t < n; ++t) {
float acc = 0.0f;
for (int64_t j = 0; j < ratio; ++j) acc += ldt(k, offlin(k, (b * ratio + j) * n + t));
x[t] = round_dt(k->dtype, acc / (float)ratio);
ss += (double)x[t] * x[t];
}
const float inv = 1.0f / std::sqrt((float)(ss / (double)n) + eps);
for (int64_t t = 0; t < n; ++t) y[t] = x[t] * inv * (wadd + ldt(w, offlin(w, t)));
float p[3] = {};
for (int c = 0; c < (mc ? 3 : 1); ++c) {
const int64_t bp = !pos ? pos0 + b * ratio
: ld_int(pos->dtype, pos->data,
offlin(pos, pos2 ? b * pos->shape[1] + c : b));
p[c] = (float)bp / (rscale != 0.0f ? rscale : 1.0f);
}
const int64_t half = rot / 2;
for (int64_t i = 0; i < half; ++i) {
const float ang = p[f.comp[i]] * f.inv[i];
const float c = ref_cos(ang) * yarn.magnitude, s = ref_sin(ang) * yarn.magnitude;
const float x0 = y[i], x1 = y[half + i];
y[i] = x0 * c - x1 * s;
y[half + i] = x0 * s + x1 * c;
}
for (int64_t t = 0; t < n; ++t) stt(out, offlin(out, orow * n + t), y[t]);
}
return RAD_OK;
}