Archived
975 lines
50 KiB
C++
975 lines
50 KiB
C++
/* avx_attn.cpp -- paged attention, dense attention, the KV write, and the two rotary forms.
|
|
*
|
|
* ============================== THE PAGED LAYOUT ==============================
|
|
*
|
|
* kv_cache[num_blocks, kv_heads, block_size, 2 * head_dim] K then V inside a slot
|
|
*
|
|
* libr4d's, stated once for the whole project in libref/ref_ops.h, obeyed here. A flat slot index
|
|
* s decomposes as 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 writes 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. That is what makes one
|
|
* kernel serve a decode (q_len 1), a speculative verify (q_len <= 64) and a prefill chunk (q_len
|
|
* in the thousands) with no second length parameter.
|
|
*
|
|
* 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, the
|
|
* two-sided reading libr4d's h128 kernel documents.
|
|
*
|
|
* ============================== ONE PASS OVER THE CACHE, NOT TWO ==============================
|
|
*
|
|
* This is the one place where this plugin deliberately computes the softmax in a DIFFERENT ORDER
|
|
* from the oracle, and it is worth being explicit about the trade.
|
|
*
|
|
* libref makes two passes over the whole context: one to find the maximum score, one to
|
|
* exponentiate and accumulate -- and it RECOMPUTES every dot product in the second pass, because
|
|
* keeping them would need a buffer the size of the context and "the reference has time it does not
|
|
* have memory". At a 128K context that is two full sweeps of the KV cache and twice the dot
|
|
* products, and the KV cache is the largest thing a decode step touches.
|
|
*
|
|
* Here the softmax is ONLINE and tiled, the flash-attention arrangement: keys are taken in tiles,
|
|
* a tile's scores are computed once, the running maximum is updated at the tile boundary and the
|
|
* accumulator rescaled by exp(m_old - m_new) when it moves. One pass, one dot product per key, and
|
|
* the memory is one tile of scores rather than one context of them.
|
|
*
|
|
* WHAT IT COSTS: the rescalings round. The result is mathematically identical -- the final running
|
|
* maximum IS the global maximum, and every weight ends up as exp(s_j - m_final) -- but the
|
|
* intermediate multiplications are extra roundings that libref does not perform, so this cannot be
|
|
* bit-exact against it and does not claim to be. It lands at ~1e-7 relative against a tolerance of
|
|
* 1e-4 (f32) / 2e-2 (bf16). Every other numerical choice in this file is exact by construction:
|
|
*
|
|
* THE DESCALES ARE APPLIED PER ELEMENT, NOT TO THE DOT. libref computes
|
|
* `dot += q[d] * (k[d] * kds)`, which is not the same f32 number as `dot(q, k) * kds`. The K row
|
|
* is therefore scaled by `kds` as it is widened, which reproduces the reference's arithmetic
|
|
* exactly rather than to a tolerance. Same for V and `vds`.
|
|
*
|
|
* AN EMPTY WINDOW IS A ZERO ROW, NOT A NaN. A softmax over no visible key has no defined answer
|
|
* and zero is what every kernel in this project produces. The checker compares the finiteness
|
|
* pattern before any norm, so getting this wrong is a hard failure rather than a tolerance
|
|
* question -- which is the right treatment.
|
|
*/
|
|
#include "avx_vec.h"
|
|
#include "avx_common.h"
|
|
|
|
using namespace avx;
|
|
|
|
/* The widest head this accumulates in one frame. MLA's 576 is the widest in circulation and
|
|
* libr4d's widest is 256, so 1024 is headroom rather than a limit anyone meets; above it the op
|
|
* says RAD_E_UNSUPPORTED by name instead of overflowing. Same number as libref's, so the two
|
|
* refuse the same calls. */
|
|
enum { AVX_HD_MAX = 1024 };
|
|
|
|
/* Keys per online-softmax tile. Sized so a tile of scores and the key rows behind it stay in L1
|
|
* beside the query head and the accumulator: at head_dim 128 that is 64 keys x 128 floats = 32 KiB
|
|
* of K plus the same of V, which is the working set a Zen-class L1 holds. It is a constant rather
|
|
* than a tunable because the cost curve is flat between 32 and 128 -- what matters is that it is
|
|
* not 1 (a rescale per key) and not the whole context (libref's memory problem). */
|
|
enum { AVX_KV_TILE = 64 };
|
|
|
|
struct KVView {
|
|
const RadTensor* t;
|
|
int64_t sb, sh, so, sd; /* strides: block, kv head, slot offset, element */
|
|
int64_t head_dim;
|
|
};
|
|
|
|
static 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;
|
|
}
|
|
|
|
/* A head of an operand, widened into f32 and optionally scaled as it goes -- which is what makes
|
|
* the descale exact against libref rather than close (see the header). */
|
|
static inline const float* head_f32(const RadTensor* t, int64_t base, int64_t ds, int64_t n,
|
|
float mul, float* scr) {
|
|
if (ds == 1) {
|
|
if (mul == 1.0f) return row_f32_in(t->dtype, byte_at(t->data, t->dtype, base), scr, n);
|
|
row_to_f32(t->dtype, byte_at(t->data, t->dtype, base), scr, n);
|
|
} else {
|
|
for (int64_t d = 0; d < n; ++d) scr[d] = ldt(t, base + d * ds);
|
|
}
|
|
if (mul != 1.0f) {
|
|
const vf vm = vf_set1(mul);
|
|
int64_t i = 0;
|
|
for (; i + VF_N <= n; i += VF_N) vf_storeu(scr + i, vf_mul(vf_loadu(scr + i), vm));
|
|
for (; i < n; ++i) scr[i] *= mul;
|
|
}
|
|
return scr;
|
|
}
|
|
|
|
/* A dot product with four accumulators, which is what covers the FMA latency. Shared by both
|
|
* attention forms and by nothing else, so it lives here rather than in the header. */
|
|
static inline float dotf(const float* a, const float* b, int64_t n) {
|
|
vf a0 = vf_zero(), a1 = vf_zero(), a2 = vf_zero(), a3 = vf_zero();
|
|
int64_t i = 0;
|
|
for (; i + 4 * VF_N <= n; i += 4 * VF_N) {
|
|
a0 = vf_fma(vf_loadu(a + i), vf_loadu(b + i), a0);
|
|
a1 = vf_fma(vf_loadu(a + i + VF_N), vf_loadu(b + i + VF_N), a1);
|
|
a2 = vf_fma(vf_loadu(a + i + 2 * VF_N), vf_loadu(b + i + 2 * VF_N), a2);
|
|
a3 = vf_fma(vf_loadu(a + i + 3 * VF_N), vf_loadu(b + i + 3 * VF_N), a3);
|
|
}
|
|
for (; i + VF_N <= n; i += VF_N) a0 = vf_fma(vf_loadu(a + i), vf_loadu(b + i), a0);
|
|
float s = vf_hsum(vf_add(vf_add(a0, a1), vf_add(a2, a3)));
|
|
for (; i < n; ++i) s += a[i] * b[i];
|
|
return s;
|
|
}
|
|
|
|
/* acc += w * v, vectorised. */
|
|
static inline void axpy(float* acc, const float* v, float w, int64_t n) {
|
|
const vf vw = vf_set1(w);
|
|
int64_t i = 0;
|
|
for (; i + VF_N <= n; i += VF_N)
|
|
vf_storeu(acc + i, vf_fma(vw, vf_loadu(v + i), vf_loadu(acc + i)));
|
|
for (; i < n; ++i) acc[i] += w * v[i];
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ attn_paged */
|
|
/* Prefill form, taken when the queries are uniform (!cus) and long (q_len >= 32): the
|
|
* whole context is shared across q_len rows, so widening K/V per q-row (the decode form in
|
|
* AVX_KERNEL below) re-converts the context q_len times -- 512x over at the bench shape.
|
|
* Each (seq, kv-head, q-block) unit widens the context ONCE into panels and runs its rows
|
|
* off them. The per-row math is unchanged -- dots ascending, the online softmax with the
|
|
* same rescale points, the same scalar exps -- so rows match the decode form bit for bit;
|
|
* only the loads move. q-blocks of 64 keep the units many for the engine's thread pool.
|
|
* Ragged (cus) inputs stay on the decode form, whatever their lengths. */
|
|
static int attn_paged_prefill(const RadArgs* a, [[maybe_unused]] RadStream stream) {
|
|
const RadTensor *q = &a->t[0], *kv = &a->t[1], *bt = &a->t[2], *su = &a->t[3], *o = &a->t[7];
|
|
const RadTensor* kdes = rad_arg_in(a, 4);
|
|
const RadTensor* vdes = rad_arg_in(a, 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 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) return RAD_E_SHAPE;
|
|
if (head_dim > AVX_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;
|
|
const int64_t n_seq = numel(su);
|
|
/* THE QUERY LENGTH IS PER SEQUENCE. The operand holds every sequence's rows back to back, so
|
|
* its row count is n_seq * q_len; taking the whole of it as one sequence's length puts
|
|
* sequence s at rows s * rows and writes every sequence after the first past `out`. */
|
|
const int64_t rows = numel(q) / (q_heads * head_dim);
|
|
if (n_seq <= 0 || rows <= 0 || rows % n_seq != 0) return RAD_E_SHAPE;
|
|
const int64_t q_len = rows / n_seq;
|
|
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);
|
|
/* Read as [rows, q_heads, head_dim] at whatever rank they arrived, libref's reading: a rank-2
|
|
* q is a column slice whose row stride is its own (avx_common.h: row_stride). */
|
|
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));
|
|
|
|
enum { QB = 64 };
|
|
const int64_t nqb = (q_len + QB - 1) / QB;
|
|
AVX_PARALLEL_FOR_IF(n_seq * kv_heads * nqb >= 4)
|
|
for (int64_t u = 0; u < n_seq * kv_heads * nqb; ++u) {
|
|
const int64_t qb = u % nqb, kh = (u / nqb) % kv_heads, s = u / (nqb * kv_heads);
|
|
const int64_t ctx = ldi(su, offlin(su, s));
|
|
if (ctx <= 0) continue;
|
|
const float kds = kdes ? ldt(kdes, s * kv_heads + kh) : 1.0f;
|
|
const float vds = vdes ? ldt(vdes, s * kv_heads + kh) : 1.0f;
|
|
/* Panels plus one row frame: a single arena call, partitioned by hand. */
|
|
float* mem = scratch_f32((size_t)2 * (size_t)ctx * (size_t)head_dim +
|
|
(size_t)(3 * head_dim + AVX_KV_TILE) + 16);
|
|
float* Kp = mem;
|
|
float* Vp = mem + (size_t)ctx * (size_t)head_dim;
|
|
float* frame = Vp + (size_t)ctx * (size_t)head_dim;
|
|
float* qbuf = frame;
|
|
float* kbuf = frame + head_dim;
|
|
float* acc = frame + 2 * head_dim;
|
|
float* sbuf = frame + 3 * head_dim;
|
|
int64_t* kbv = (int64_t*)scratch_raw((size_t)ctx * sizeof(int64_t) + 64);
|
|
/* Widen the context once. Rows past max_blocks stay invalid, exactly as the
|
|
* per-row loop's break would leave them. */
|
|
int64_t nkeys = 0;
|
|
for (int64_t j = 0; j < ctx; ++j) {
|
|
if (j / block_size >= max_blocks) break;
|
|
const int64_t blk = ldi(bt, s * bt_rs + (j / block_size) * bt_cs);
|
|
if (blk < 0) {
|
|
kbv[j] = -1;
|
|
continue;
|
|
}
|
|
kbv[j] = blk * kvv.sb + kh * kvv.sh + (j % block_size) * kvv.so;
|
|
const float* kr = head_f32(kv, kbv[j], kvv.sd, head_dim, kds, kbuf);
|
|
for (int64_t d = 0; d < head_dim; ++d) Kp[(size_t)j * head_dim + d] = kr[d];
|
|
const float* vr = head_f32(kv, kbv[j] + head_dim * kvv.sd, kvv.sd, head_dim,
|
|
vds, kbuf);
|
|
for (int64_t d = 0; d < head_dim; ++d) Vp[(size_t)j * head_dim + d] = vr[d];
|
|
nkeys = j + 1;
|
|
}
|
|
const int64_t i0 = qb * QB, i1 = (i0 + QB < q_len) ? i0 + QB : q_len;
|
|
/* One gqa row at a time. A joint-rows form -- each key row loaded once for all gqa
|
|
* dots -- measures identical on a long-query shape: the nest is compute-saturated
|
|
* (dots, axpy, scalar exps) rather than bound by panel traffic, so sharing the loads
|
|
* frees nothing. Do not switch to it without measuring. */
|
|
for (int64_t g = 0; g < gqa; ++g) {
|
|
const int64_t h = kh * gqa + g;
|
|
for (int64_t i = i0; i < i1; ++i) {
|
|
const int64_t tok = s * q_len + i;
|
|
const int64_t pos = ctx - q_len + i;
|
|
const int64_t qb2 = tok * q_rs + h * q_hs;
|
|
const int64_t ob = tok * o_rs + h * o_hs;
|
|
const float* qv = head_f32(q, qb2, q_ds, head_dim, 1.0f, qbuf);
|
|
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
|
|
float m = -INFINITY, l = 0.0f;
|
|
int ntile = 0;
|
|
int64_t kbase[AVX_KV_TILE];
|
|
auto flush = [&]() {
|
|
if (ntile == 0) return;
|
|
float tmax = sbuf[0];
|
|
for (int t = 1; t < ntile; ++t) if (sbuf[t] > tmax) tmax = sbuf[t];
|
|
if (tmax > m) {
|
|
const float r = (m == -INFINITY) ? 0.0f : std::exp(m - tmax);
|
|
if (r != 1.0f) {
|
|
const vf vr = vf_set1(r);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(acc + d, vf_mul(vf_loadu(acc + d), vr));
|
|
for (; d < head_dim; ++d) acc[d] *= r;
|
|
l *= r;
|
|
}
|
|
m = tmax;
|
|
}
|
|
/* Scalar exps here, deliberately: the tile-vectorised form
|
|
* (vf_exp, as in the decode flush) measures neutral-to-slower on a
|
|
* long-query shape, because the nest is bound elsewhere. The values
|
|
* are the same either way. */
|
|
for (int t = 0; t < ntile; ++t) {
|
|
const float w = std::exp(sbuf[t] - m);
|
|
l += w;
|
|
axpy(acc, Vp + (size_t)kbase[t] * head_dim, w, head_dim);
|
|
}
|
|
ntile = 0;
|
|
};
|
|
for (int64_t j = 0; j < nkeys; ++j) {
|
|
if (causal ? (j > pos) : (j >= ctx)) continue;
|
|
if (window > 0) {
|
|
const int64_t d = pos - j;
|
|
if (causal ? (d >= window) : (d >= window || d <= -window)) continue;
|
|
}
|
|
if (kbv[j] < 0) continue;
|
|
sbuf[ntile] = dotf(qv, Kp + (size_t)j * head_dim, head_dim) * scale;
|
|
kbase[ntile] = j;
|
|
if (++ntile == AVX_KV_TILE) flush();
|
|
}
|
|
flush();
|
|
if (m == -INFINITY) {
|
|
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
|
|
continue;
|
|
}
|
|
const float inv = l > 0.0f ? 1.0f / l : 0.0f;
|
|
if (o_ds == 1) {
|
|
const vf vi = vf_set1(inv);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(acc + d, vf_mul(vf_loadu(acc + d), vi));
|
|
for (; d < head_dim; ++d) acc[d] *= inv;
|
|
out_row(o, ob, head_dim, acc);
|
|
} else {
|
|
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, acc[d] * inv);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
AVX_KERNEL(attn_paged) {
|
|
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];
|
|
const RadTensor* kdes = rad_arg_in(a, 4);
|
|
const RadTensor* vdes = rad_arg_in(a, 5);
|
|
const RadTensor* cus = rad_arg_in(a, 6);
|
|
if (!q->data || !kv->data || !bt->data || !su->data || !o->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 (docs/OPS.md), so a caller who did not pin it and resolved to a
|
|
* kernel that declares no RAD_C_EQ for it arrives here without the key. The cache tensor says
|
|
* what the block size is; reading it off the operand is this plugin's rule anyway. */
|
|
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) return RAD_E_SHAPE;
|
|
if (head_dim > AVX_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;
|
|
if (q->rank >= 3 && q->shape[q->rank - 2] != 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;
|
|
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;
|
|
|
|
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 --
|
|
* libref's reading. A rank-2 operand is a column slice whose row stride is its own. */
|
|
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));
|
|
/* Uniform long queries share one context: the panel form widens it once instead of once
|
|
* per row. Ragged (cus) inputs stay below whatever their lengths. */
|
|
if (!cus && q_len >= 32) return attn_paged_prefill(a, stream);
|
|
/* Rows run as (token, kv-head) groups: the gqa query rows of a group share the key
|
|
* and value rows behind every key, so each is widened once for gqa dots/axpys rather
|
|
* than once per row. Per-row arithmetic is untouched -- rows match bit for bit. */
|
|
const int64_t total = rows * kv_heads;
|
|
|
|
AVX_PARALLEL_FOR_IF(total >= 2)
|
|
for (int64_t tidx = 0; tidx < total; ++tidx) {
|
|
const int64_t kh = tidx % kv_heads;
|
|
const int64_t tok = tidx / kv_heads;
|
|
/* Which sequence owns this token and where inside its query it sits. Linear rather
|
|
* than a binary search: n_seq is a handful. Once per group, not once per row. */
|
|
int64_t s = 0, qlen = q_len, i = 0;
|
|
if (cus) {
|
|
while (s + 1 < n_seq && ldi(cus, offlin(cus, s + 1)) <= tok) ++s;
|
|
const int64_t beg = ldi(cus, offlin(cus, s));
|
|
qlen = ldi(cus, offlin(cus, s + 1)) - beg;
|
|
i = tok - beg;
|
|
} else {
|
|
s = tok / q_len;
|
|
i = tok - s * q_len;
|
|
}
|
|
if (qlen <= 0) continue;
|
|
const int64_t ctx = ldi(su, offlin(su, s));
|
|
const int64_t pos = ctx - qlen + i;
|
|
const float kds = kdes ? ldt(kdes, s * kv_heads + kh) : 1.0f;
|
|
const float vds = vdes ? ldt(vdes, s * kv_heads + kh) : 1.0f;
|
|
|
|
/* One arena call carves the whole frame, because a second one may reallocate and
|
|
* invalidate the first pointer (avx_common.h). */
|
|
float* frame = (float*)scratch_f32((size_t)gqa * (size_t)(2 * head_dim + AVX_KV_TILE + 2) +
|
|
(size_t)(2 * head_dim) + 16);
|
|
float* qpan = frame; /* [gqa, head_dim] queries */
|
|
float* krow = qpan + (size_t)gqa * head_dim; /* one widened key row */
|
|
float* vrow = krow + head_dim; /* one widened value row */
|
|
float* accs = vrow + head_dim; /* [gqa, head_dim] */
|
|
float* sbufs = accs + (size_t)gqa * head_dim; /* [gqa, TILE] scores */
|
|
float* mm = sbufs + (size_t)gqa * AVX_KV_TILE;
|
|
float* ll = mm + gqa;
|
|
int64_t kbase[AVX_KV_TILE];
|
|
|
|
for (int64_t g = 0; g < gqa; ++g) {
|
|
const int64_t h = kh * gqa + g;
|
|
float* qd = qpan + g * head_dim;
|
|
const float* qr = head_f32(q, tok * q_rs + h * q_hs, q_ds, head_dim, 1.0f, qd);
|
|
if (qr != qd) std::memcpy(qd, qr, (size_t)head_dim * sizeof(float));
|
|
float* ac = accs + g * head_dim;
|
|
for (int64_t d = 0; d < head_dim; ++d) ac[d] = 0.0f;
|
|
mm[g] = -INFINITY;
|
|
ll[g] = 0.0f;
|
|
}
|
|
int ntile = 0;
|
|
|
|
/* Flush a tile: per row, update the running maximum, rescale what is already
|
|
* accumulated, then add this tile's weighted values in ascending key order. The
|
|
* value row is widened once per key and shared across the group's axpys. */
|
|
auto flush = [&]() {
|
|
if (ntile == 0) return;
|
|
for (int64_t g = 0; g < gqa; ++g) {
|
|
float* sb = sbufs + g * AVX_KV_TILE;
|
|
float* ac = accs + g * head_dim;
|
|
float m = mm[g], l = ll[g];
|
|
float tmax = sb[0];
|
|
for (int t = 1; t < ntile; ++t) if (sb[t] > tmax) tmax = sb[t];
|
|
if (tmax > m) {
|
|
const float r = (m == -INFINITY) ? 0.0f : std::exp(m - tmax);
|
|
if (r != 1.0f) {
|
|
const vf vr = vf_set1(r);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(ac + d, vf_mul(vf_loadu(ac + d), vr));
|
|
for (; d < head_dim; ++d) ac[d] *= r;
|
|
l *= r;
|
|
}
|
|
m = tmax;
|
|
}
|
|
/* The tile's exps, vectorised: the accumulation keeps its order. */
|
|
{
|
|
const vf vm = vf_set1(m);
|
|
int t = 0;
|
|
for (; t + VF_N <= ntile; t += VF_N)
|
|
vf_storeu(sb + t, vf_exp(vf_sub(vf_loadu(sb + t), vm)));
|
|
for (; t < ntile; ++t) sb[t] = std::exp(sb[t] - m);
|
|
}
|
|
mm[g] = m;
|
|
ll[g] = l;
|
|
}
|
|
for (int t = 0; t < ntile; ++t) {
|
|
/* One widening per key for the whole group, then gqa axpys. */
|
|
const float* vv = head_f32(kv, kbase[t] + head_dim * kvv.sd, kvv.sd, head_dim,
|
|
vds, vrow);
|
|
for (int64_t g = 0; g < gqa; ++g) {
|
|
const float w = sbufs[g * AVX_KV_TILE + t];
|
|
ll[g] += w;
|
|
axpy(accs + g * head_dim, vv, w, head_dim);
|
|
}
|
|
}
|
|
ntile = 0;
|
|
};
|
|
|
|
for (int64_t j = 0; j < ctx; ++j) {
|
|
if (causal ? (j > pos) : (j >= ctx)) continue;
|
|
if (window > 0) {
|
|
const int64_t d = pos - j;
|
|
if (causal ? (d >= window) : (d >= window || d <= -window)) continue;
|
|
}
|
|
if (j / block_size >= max_blocks) break; /* the table is only this wide */
|
|
const int64_t blk = ldi(bt, s * bt_rs + (j / block_size) * bt_cs);
|
|
if (blk < 0) continue;
|
|
const int64_t kb = blk * kvv.sb + kh * kvv.sh + (j % block_size) * kvv.so;
|
|
/* One widening per key for the whole group, then gqa dots. */
|
|
const float* kvrow = head_f32(kv, kb, kvv.sd, head_dim, kds, krow);
|
|
for (int64_t g = 0; g < gqa; ++g)
|
|
sbufs[g * AVX_KV_TILE + ntile] =
|
|
dotf(qpan + g * head_dim, kvrow, head_dim) * scale;
|
|
kbase[ntile] = kb;
|
|
if (++ntile == AVX_KV_TILE) flush();
|
|
}
|
|
flush();
|
|
|
|
for (int64_t g = 0; g < gqa; ++g) {
|
|
const int64_t h = kh * gqa + g;
|
|
const int64_t ob = tok * o_rs + h * o_hs;
|
|
float* ac = accs + g * head_dim;
|
|
if (mm[g] == -INFINITY) {
|
|
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
|
|
continue;
|
|
}
|
|
const float inv = ll[g] > 0.0f ? 1.0f / ll[g] : 0.0f;
|
|
if (o_ds == 1) {
|
|
const vf vi = vf_set1(inv);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(ac + d, vf_mul(vf_loadu(ac + d), vi));
|
|
for (; d < head_dim; ++d) ac[d] *= inv;
|
|
out_row(o, ob, head_dim, ac);
|
|
} else {
|
|
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, ac[d] * inv);
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ attn_dense */
|
|
/* The vision tower's: nothing paged, nothing quantised, `cu_seqlens` bounding one image (or one
|
|
* attention window) per entry so a whole batch is ONE launch rather than a per-segment loop and a
|
|
* concatenate. Causal is a parameter because a dense decoder-only prefill wants the same kernel.
|
|
*
|
|
* Same online softmax as the paged form, and the tile is over the segment rather than the page
|
|
* table -- so the K and V rows are consecutive in memory and the hardware prefetcher sees a single
|
|
* stream, which is the one thing the dense form has that the paged one cannot. */
|
|
AVX_KERNEL(attn_dense) {
|
|
if (!rad_args_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);
|
|
if (head_dim <= 0 || gqa <= 0) return RAD_E_SHAPE;
|
|
if (head_dim > AVX_HD_MAX) return RAD_E_UNSUPPORTED;
|
|
|
|
const int64_t q_heads = q->rank >= 3 ? q->shape[q->rank - 2] : 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 (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;
|
|
|
|
/* libref's reading of all four, at whatever rank they arrived: a rank-2 operand is a column
|
|
* slice whose row stride is its own (avx_common.h: row_stride). */
|
|
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));
|
|
|
|
AVX_PARALLEL_FOR_IF(n_seq * q_heads >= 2)
|
|
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 = ldi(cu, offlin(cu, s));
|
|
const int64_t b1 = ldi(cu, offlin(cu, s + 1));
|
|
float* frame = (float*)scratch_f32((size_t)(4 * head_dim + AVX_KV_TILE) + 16);
|
|
float* qbuf = frame;
|
|
float* kbuf = frame + head_dim;
|
|
float* vbuf = frame + 2 * head_dim;
|
|
float* acc = frame + 3 * head_dim;
|
|
float* sbuf = frame + 4 * head_dim;
|
|
int64_t vbase[AVX_KV_TILE];
|
|
|
|
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 ? (i + 1) : b1;
|
|
const float* qv = head_f32(q, qb, q_ds, head_dim, 1.0f, qbuf);
|
|
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
|
|
float m = -INFINITY, l = 0.0f;
|
|
int ntile = 0;
|
|
|
|
auto flush = [&]() {
|
|
if (ntile == 0) return;
|
|
float tmax = sbuf[0];
|
|
for (int t = 1; t < ntile; ++t) if (sbuf[t] > tmax) tmax = sbuf[t];
|
|
if (tmax > m) {
|
|
const float r = (m == -INFINITY) ? 0.0f : std::exp(m - tmax);
|
|
if (r != 1.0f) {
|
|
const vf vr = vf_set1(r);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(acc + d, vf_mul(vf_loadu(acc + d), vr));
|
|
for (; d < head_dim; ++d) acc[d] *= r;
|
|
l *= r;
|
|
}
|
|
m = tmax;
|
|
}
|
|
/* The tile's exps, vectorised (see the prefill flush above): the
|
|
* accumulation keeps its order. */
|
|
{
|
|
const vf vm = vf_set1(m);
|
|
int t = 0;
|
|
for (; t + VF_N <= ntile; t += VF_N)
|
|
vf_storeu(sbuf + t, vf_exp(vf_sub(vf_loadu(sbuf + t), vm)));
|
|
for (; t < ntile; ++t) sbuf[t] = std::exp(sbuf[t] - m);
|
|
}
|
|
for (int t = 0; t < ntile; ++t) {
|
|
const float w = sbuf[t];
|
|
l += w;
|
|
axpy(acc, head_f32(v, vbase[t], v_ds, head_dim, 1.0f, vbuf), w, head_dim);
|
|
}
|
|
ntile = 0;
|
|
};
|
|
|
|
for (int64_t j = b0; j < jend; ++j) {
|
|
const float* kvr = head_f32(k, j * k_rs + kh * k_hs, k_ds, head_dim, 1.0f, kbuf);
|
|
sbuf[ntile] = dotf(qv, kvr, head_dim) * scale;
|
|
vbase[ntile] = j * v_rs + kh * v_hs;
|
|
if (++ntile == AVX_KV_TILE) flush();
|
|
}
|
|
flush();
|
|
|
|
if (m == -INFINITY) {
|
|
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
|
|
continue;
|
|
}
|
|
const float inv = l > 0.0f ? 1.0f / l : 0.0f;
|
|
if (o_ds == 1) {
|
|
const vf vi = vf_set1(inv);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(acc + d, vf_mul(vf_loadu(acc + d), vi));
|
|
for (; d < head_dim; ++d) acc[d] *= inv;
|
|
out_row(o, ob, head_dim, acc);
|
|
} else {
|
|
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, acc[d] * inv);
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ kv_store */
|
|
/* The cache operand is INOUT and not OUT: this op touches only the mapped slots and every other
|
|
* slot in the pool must survive it, which is a liveness fact the buffer planner has to know.
|
|
*
|
|
* A NEGATIVE slot is SKIPPED, and that is a documented value rather than an accident: it is how a
|
|
* padded batch row says "this token has no cache slot". libr4d treats slot <= 0 as null because
|
|
* vLLM reserves block 0; a generic implementation must not steal a valid slot, so only the
|
|
* strictly negative is skipped -- libref's rule, and the two have to agree or a padded step writes
|
|
* different bytes under the two plugins. */
|
|
AVX_KERNEL(kv_store) {
|
|
if (!rad_args_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);
|
|
const 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;
|
|
|
|
/* `k` AND `v` ARRIVE RANK 2 FROM AN ARCHITECTURE THAT DOES NOT DE-INTERLEAVE ITS PROJECTION --
|
|
* a column slice of the fused QKV plane, whose row stride is the whole projection's width.
|
|
* Deriving the stride from the shape puts token 0 in the right slot and fills every later
|
|
* token's slot out of the previous token's query rows. libref's reading, through row_stride. */
|
|
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);
|
|
|
|
/* Validated before the loop, because an OpenMP body cannot return. */
|
|
for (int64_t t = 0; t < M; ++t) {
|
|
const int64_t slot = ldi(sm, offlin(sm, t));
|
|
if (slot >= 0 && n_blocks > 0 && slot / block_size >= n_blocks) return RAD_E_SHAPE;
|
|
}
|
|
|
|
AVX_PARALLEL_FOR_IF(M * kv_heads * head_dim >= AVX_PAR_MIN)
|
|
for (int64_t t = 0; t < M; ++t) {
|
|
const int64_t slot = ldi(sm, offlin(sm, t));
|
|
if (slot < 0) continue;
|
|
const int64_t blk = slot / block_size, off = slot % block_size;
|
|
float* buf = scratch_f32((size_t)head_dim);
|
|
for (int64_t h = 0; h < kv_heads; ++h) {
|
|
const int64_t cb = blk * kvv.sb + h * kvv.sh + off * kvv.so;
|
|
/* The head at a time, through the row converter, so a bf16 cache fed a bf16 activation
|
|
* is a widen and a narrow of a whole head rather than 2*head_dim dtype switches. The
|
|
* strided case falls back per element, which is what a non-unit cache stride means. */
|
|
if (k_ds == 1 && kvv.sd == 1) {
|
|
row_to_f32(k->dtype, byte_at(k->data, k->dtype, t * k_rs + h * k_hs), buf, head_dim);
|
|
out_row(kv, cb, head_dim, buf);
|
|
} else {
|
|
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));
|
|
}
|
|
if (v_ds == 1 && kvv.sd == 1) {
|
|
row_to_f32(v->dtype, byte_at(v->data, v->dtype, t * v_rs + h * v_hs), buf, head_dim);
|
|
out_row(kv, cb + head_dim, head_dim, buf);
|
|
} else {
|
|
for (int64_t d = 0; d < head_dim; ++d)
|
|
stt(kv, cb + (head_dim + d) * kvv.sd, ldt(v, t * v_rs + h * v_hs + d * v_ds));
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ================================================================== rotary
|
|
*
|
|
* NeoX pairs element i with i + rotary_dim/2; GPT-J (the "interleaved" style) pairs 2i with 2i+1.
|
|
* Which one a checkpoint wants is a property of the checkpoint, so it is a parameter and not a
|
|
* guess -- and getting it wrong produces fluent text with a broken sense of position, which is the
|
|
* hardest kind of wrong to notice.
|
|
*
|
|
* THE ANGLE TABLE IS THE WHOLE OPTIMISATION HERE. libref computes
|
|
* `1/pow(theta, 2i/rot)` and then a cos and a sin FOR EVERY (token, head, i) -- three libm calls
|
|
* per element, on an op that otherwise does two multiplies and an add. But the angle depends only
|
|
* on (position, i): every head of a token wants the same cos/sin. Building the row's table once
|
|
* and reusing it across all n_head + n_head_kv heads removes (heads - 1)/heads of the transcendental
|
|
* work, which at 32 heads is 97% of it, and the values are identical because they are the same
|
|
* computation performed once instead of many times -- so this is bit-identical, not merely close.
|
|
*
|
|
* The inverse frequencies depend only on `i` and are built ONCE PER LAUNCH for the same reason.
|
|
*
|
|
* What is NOT vectorised is the cos/sin themselves: a vector sincos is a polynomial with its own
|
|
* accuracy, and `rope` feeds a position signal where a fraction of a ULP is exactly the error that
|
|
* produces fluent text with wrong positions. The table makes them rare enough not to matter. */
|
|
/* The multi-component modes (docs/OPS.md, `rope`): which position component drives each of the
|
|
* rot/2 frequencies, and for `axial` a ladder per section. libref's rope_freqs states the same
|
|
* rule; the two are checked against each other by avx_check. */
|
|
enum { ROPE_NEOX = 0, ROPE_GPTJ = 1, ROPE_MROPE = 2, ROPE_IMROPE = 3, ROPE_AXIAL = 4 };
|
|
|
|
/* cos in [0, half), sin in [half, rot). Same layout `rope_table` writes, deliberately.
|
|
*
|
|
* NOT sincosf: it is bit-identical to the two separate calls over a sweep of millions of angles,
|
|
* but measures SLOWER than them, because glibc's joint wrapper costs more than two fast-path
|
|
* calls. */
|
|
static void angles(float* cs, const float* invf, int64_t half, float pos) {
|
|
for (int64_t i = 0; i < half; ++i) {
|
|
const float ang = pos * invf[i];
|
|
cs[i] = std::cos(ang);
|
|
cs[half + i] = std::sin(ang);
|
|
}
|
|
}
|
|
|
|
/* ...and with a position per component, `comp[i]` naming the one frequency i reads. */
|
|
static void angles_multi(float* cs, const float* invf, const int* comp, int64_t half,
|
|
const float* pos) {
|
|
for (int64_t i = 0; i < half; ++i) {
|
|
const float ang = pos[comp[i]] * invf[i];
|
|
cs[i] = std::cos(ang);
|
|
cs[half + i] = std::sin(ang);
|
|
}
|
|
}
|
|
|
|
static void rope_head(const RadTensor* t, int64_t base, int64_t ds, int64_t rot, int mode,
|
|
const float* cs, int64_t half, float* buf) {
|
|
if (mode == ROPE_NEOX && ds == 1) {
|
|
/* x0 and x1 are two contiguous runs of `half`, so the whole rotation is vector work. */
|
|
row_to_f32(t->dtype, byte_at(t->data, t->dtype, base), buf, rot);
|
|
int64_t i = 0;
|
|
for (; i + VF_N <= half; i += VF_N) {
|
|
const vf x0 = vf_loadu(buf + i), x1 = vf_loadu(buf + half + i);
|
|
const vf c = vf_loadu(cs + i), s = vf_loadu(cs + half + i);
|
|
vf_storeu(buf + i, vf_sub(vf_mul(x0, c), vf_mul(x1, s)));
|
|
vf_storeu(buf + half + i, vf_add(vf_mul(x0, s), vf_mul(x1, c)));
|
|
}
|
|
for (; i < half; ++i) {
|
|
const float x0 = buf[i], x1 = buf[half + i];
|
|
buf[i] = x0 * cs[i] - x1 * cs[half + i];
|
|
buf[half + i] = x0 * cs[half + i] + x1 * cs[i];
|
|
}
|
|
out_row(t, base, rot, buf);
|
|
return;
|
|
}
|
|
/* GPT-J's interleave, and every strided case. The pairs are adjacent, so a vector form needs a
|
|
* de-interleave and a re-interleave for two multiplies -- not worth the code on a layout only
|
|
* older checkpoints use. */
|
|
for (int64_t i = 0; i < half; ++i) {
|
|
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);
|
|
const float c = cs[i], s = cs[half + i];
|
|
stt(t, base + i0 * ds, x0 * c - x1 * s);
|
|
stt(t, base + i1 * ds, x0 * s + x1 * c);
|
|
}
|
|
}
|
|
|
|
static float* inv_freqs(int64_t half, int64_t rot, float theta) {
|
|
float* f = (float*)scratch_raw((size_t)half * sizeof(float));
|
|
for (int64_t i = 0; i < half; ++i)
|
|
f[i] = 1.0f / std::pow(theta, (float)(2 * i) / (float)rot);
|
|
return f;
|
|
}
|
|
|
|
AVX_KERNEL(rope) {
|
|
if (!rad_args_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;
|
|
const 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;
|
|
|
|
/* [M] one component a token, or [C, M] component-major with `sections`. */
|
|
int64_t sec[8] = {};
|
|
int n_sec = 0;
|
|
for (const char* v = p_str(a, "sections", nullptr); v && *v && n_sec < 8;) {
|
|
char* e = nullptr;
|
|
const long long x = std::strtoll(v, &e, 10);
|
|
if (e == v) break;
|
|
if (x <= 0) return RAD_E_SHAPE;
|
|
sec[n_sec++] = x;
|
|
v = e;
|
|
}
|
|
const bool multi = pos->rank >= 2;
|
|
const int64_t n_comp = multi ? pos->shape[0] : 1;
|
|
if (multi && (mode <= ROPE_GPTJ || n_sec <= 0 || n_comp > 8 ||
|
|
n_comp < (mode == ROPE_IMROPE ? 3 : n_sec)))
|
|
return RAD_E_SHAPE;
|
|
|
|
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
|
|
const int64_t M = multi ? pos->shape[1] : numel(pos);
|
|
if (M <= 0 || numel(qkv) < M * stride_row) return RAD_E_SHAPE;
|
|
const int64_t rs = qkv->rank >= 2 ? qkv->stride[qkv->rank - 2] : stride_row;
|
|
const int64_t ds = laststride(qkv);
|
|
const int64_t half = rot / 2;
|
|
float* invf = inv_freqs(half, rot, theta);
|
|
int comp[512] = {};
|
|
if (half > 512) return RAD_E_UNSUPPORTED;
|
|
if (multi) {
|
|
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) && total != half) return RAD_E_SHAPE;
|
|
for (int64_t i = 0; i < half; ++i) {
|
|
int c = 0;
|
|
if (mode == ROPE_MROPE || mode == ROPE_AXIAL) {
|
|
while (c + 1 < n_sec && i >= start[c + 1]) ++c;
|
|
if (mode == ROPE_AXIAL)
|
|
invf[i] = 1.0f / std::pow(theta, (float)(2 * (i - start[c])) /
|
|
(float)(2 * sec[c]));
|
|
} else if (mode == ROPE_IMROPE) {
|
|
if (i % 3 == 1 && i < 3 * sec[1]) c = 1;
|
|
else if (i % 3 == 2 && i < 3 * sec[2]) c = 2;
|
|
}
|
|
comp[i] = c;
|
|
}
|
|
}
|
|
const int pair_mode = mode == ROPE_GPTJ ? ROPE_GPTJ : ROPE_NEOX;
|
|
const int64_t pcs = multi ? pos->stride[0] : 0, pms = multi ? pos->stride[1] : 0;
|
|
|
|
AVX_PARALLEL_FOR_IF(M * (n_head + n_head_kv) * rot >= AVX_PAR_MIN)
|
|
for (int64_t m = 0; m < M; ++m) {
|
|
const float div = rscale != 0.0f ? rscale : 1.0f;
|
|
const int64_t row = m * rs;
|
|
float* cs = scratch_f32((size_t)rot);
|
|
float* buf = scratch_f32_b((size_t)rot);
|
|
if (multi) {
|
|
float pc[8];
|
|
for (int64_t c = 0; c < n_comp; ++c) pc[c] = (float)ldi(pos, c * pcs + m * pms) / div;
|
|
angles_multi(cs, invf, comp, half, pc);
|
|
} else {
|
|
angles(cs, invf, half, (float)ldi(pos, offlin(pos, m)) / div);
|
|
}
|
|
/* Q heads then K heads. The V heads that follow are left alone, which is the whole reason
|
|
* this op takes the fused qkv rather than two tensors. */
|
|
for (int64_t h = 0; h < n_head + n_head_kv; ++h)
|
|
rope_head(qkv, row + h * head_dim * ds, ds, rot, pair_mode, cs, half, buf);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ qk_norm_rope */
|
|
/* An RMS norm over each head against its own gain, then RoPE.
|
|
*
|
|
* THE NORM OUTPUT IS ROUNDED TO THE TENSOR'S DTYPE BEFORE THE ROTATION. That is not an accident of
|
|
* writing it back in place -- it is what the unfused pair does, because the norm stores a tensor
|
|
* and the rotation reads it, and libr4d's r4d_qk_norm_rope_gate matches the unfused reference for
|
|
* exactly this reason. A fused kernel that kept f32 across the seam would be half an ulp better
|
|
* and would fail a comparison against this. So the norm is written with out_row and the rotation
|
|
* re-reads it, rather than being carried in registers. */
|
|
AVX_KERNEL(qk_norm_rope) {
|
|
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
|
|
const RadTensor *qkv = &a->t[0], *pos = &a->t[1], *qw = &a->t[2], *kw = &a->t[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);
|
|
static const char* kModes[] = { "neox", "gptj" };
|
|
const int mode = p_enum(a, "mode", kModes, 2, ROPE_NEOX);
|
|
const 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 (numel(qw) < head_dim || numel(kw) < head_dim) return RAD_E_SHAPE;
|
|
|
|
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
|
|
const int64_t M = numel(pos);
|
|
if (M <= 0 || numel(qkv) < M * stride_row) return RAD_E_SHAPE;
|
|
const int64_t rs = qkv->rank >= 2 ? qkv->stride[qkv->rank - 2] : stride_row;
|
|
const int64_t ds = laststride(qkv);
|
|
const int64_t half = rot / 2;
|
|
|
|
/* One arena call for the launch-invariant tables: the inverse frequencies and both widened
|
|
* gains. A second scratch_raw would reallocate under the parallel region. */
|
|
float* tab = (float*)scratch_raw((size_t)(half + 2 * head_dim) * sizeof(float) + 64);
|
|
float* invf = tab;
|
|
float* gq = tab + half;
|
|
float* gk = tab + half + head_dim;
|
|
for (int64_t i = 0; i < half; ++i)
|
|
invf[i] = 1.0f / std::pow(theta, (float)(2 * i) / (float)rot);
|
|
for (int64_t d = 0; d < head_dim; ++d) {
|
|
gq[d] = ldt(qw, d * laststride(qw)) + wadd;
|
|
gk[d] = ldt(kw, d * laststride(kw)) + wadd;
|
|
}
|
|
|
|
AVX_PARALLEL_FOR_IF(M * (n_head + n_head_kv) * head_dim >= AVX_PAR_MIN)
|
|
for (int64_t m = 0; m < M; ++m) {
|
|
const float p = (float)ldi(pos, offlin(pos, m)) / (rscale != 0.0f ? rscale : 1.0f);
|
|
const int64_t row = m * rs;
|
|
float* cs = scratch_f32((size_t)rot);
|
|
float* buf = scratch_f32_b((size_t)head_dim);
|
|
float* rbuf = scratch_f32_c((size_t)head_dim);
|
|
angles(cs, invf, half, p);
|
|
for (int64_t h = 0; h < n_head + n_head_kv; ++h) {
|
|
const float* g = h < n_head ? gq : gk;
|
|
const int64_t base = row + h * head_dim * ds;
|
|
const float* xv = head_f32(qkv, base, ds, head_dim, 1.0f, buf);
|
|
const float sc = 1.0f / std::sqrt(row_sumsq(xv, head_dim) / (float)head_dim + eps);
|
|
const vf vs = vf_set1(sc);
|
|
int64_t d = 0;
|
|
for (; d + VF_N <= head_dim; d += VF_N)
|
|
vf_storeu(rbuf + d, vf_mul(vf_mul(vf_loadu(xv + d), vs), vf_loadu(g + d)));
|
|
for (; d < head_dim; ++d) rbuf[d] = xv[d] * sc * g[d];
|
|
/* Written back through the operand's dtype, then re-read by rope_head -- the rounding
|
|
* across the seam is the contract. */
|
|
if (ds == 1) out_row(qkv, base, head_dim, rbuf);
|
|
else for (int64_t i = 0; i < head_dim; ++i) stt(qkv, base + i * ds, rbuf[i]);
|
|
rope_head(qkv, base, ds, rot, mode, cs, half, rbuf);
|
|
}
|
|
}
|
|
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: `rot` values a row, cos for the first rot/2 and sin for the second.
|
|
*
|
|
* THE ANGLES ARE `rope`'s, TO THE LAST OPERATION, and that is the whole contract: a fused rotation
|
|
* reading a table built any other way would be a different model by a fraction of a ULP per
|
|
* element -- fluent text, subtly wrong positions. So the two share `inv_freqs` and `angles` rather
|
|
* than each computing the angle their own way. Only the rows `positions` names are written; a
|
|
* negative position is a padded row and writes nothing. */
|
|
AVX_KERNEL(rope_table) {
|
|
if (!rad_args_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 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;
|
|
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 float* invf = inv_freqs(half, rot, theta);
|
|
|
|
AVX_PARALLEL_FOR_IF(M * rot >= AVX_PAR_MIN)
|
|
for (int64_t m = 0; m < M; ++m) {
|
|
const int64_t pp = ldi(pos, offlin(pos, m));
|
|
if (pp < 0 || pp >= rows) continue;
|
|
const float p = (float)pp / (rscale != 0.0f ? rscale : 1.0f);
|
|
float* buf = scratch_f32((size_t)rot);
|
|
angles(buf, invf, half, p);
|
|
/* `cs` may be strided, and offlin has to be evaluated per element there -- which is what
|
|
* libref does unconditionally. The dense case is one row store. */
|
|
if (rad_tensor_is_contiguous(cs)) {
|
|
out_row(cs, pp * rot, rot, buf);
|
|
} else {
|
|
for (int64_t i = 0; i < rot; ++i) stt(cs, offlin(cs, pp * rot + i), buf[i]);
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|