Archived
radiance 1.2.4
- EmbeddingGemma 2 (google/embeddinggemma-2): text, image, video and audio embeddings on the card (libr4d) and on the CPU (libavx); prompt_name, Matryoshka dimensions, image budgets, mixed content parts and the messages form on /v1/embeddings - #43: on serves that lend the activation arena to the experts (Qwen3.8-Flash-Next with expert_tiered), every image after the first read its rows from the wrong place -- garbage replies, or an earlier request's image. Fixed - the Qwen vision towers' attention resolves on the card again (broken on main after 1.2.3, never released) - #42: a warning at startup when amdgpu cwsr_enable is 0, and a stalled queue is reported as slow or stuck; the guide covers the GPU-hang-and-reset chain
This commit is contained in:
1 parent
05af4db458
commit
7d840861af
76 files changed
+8324
-467
No files matched your search
+481
-19
@@ -54,6 +54,9 @@
|
||||
*/
|
||||
#include "avx_vec.h"
|
||||
#include "avx_common.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include "rad_yarn.h"
|
||||
|
||||
using namespace avx;
|
||||
@@ -490,6 +493,282 @@ AVX_KERNEL(attn_paged) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ attn_dense, bf16 blocks */
|
||||
/* AN ENCODER'S ATTENTION IS ALL PREFILL, and the per-query loop below is built for a decode row:
|
||||
* it parallelises over (segment, head) -- eight units for one long input on a sixteen-core part --
|
||||
* and widens every key row to f32 again for every query that reads it, so a 2000-token segment
|
||||
* pays four million row conversions a head. Measured at 130 GFLOPS on the sliding layers.
|
||||
*
|
||||
* Where q, k and v are bf16 with unit element stride and the CPU has AVX512_BF16, this blocked
|
||||
* form runs instead. A task is (segment, KV head, 32 query positions) and holds the rows of every
|
||||
* query head that reads that KV head, so one key tile serves gqa x 32 rows. Per tile of 64 keys:
|
||||
*
|
||||
* S = Q K^T vdpbf16ps against the tile packed once as key-interleaved bf16 pairs, each
|
||||
* query pair broadcast into 16-key vectors: no score is horizontally reduced
|
||||
* the mask causal, window and segment end, on the scores
|
||||
* the softmax online, the running max moved at tile boundaries (this file's arrangement)
|
||||
* O += P V V's tile packed once for the task as key-interleaved bf16 pairs, P rounded to
|
||||
* bf16 pairs along the keys (as PyTorch's CPU flash attention does), six rows of
|
||||
* 64 output columns a block
|
||||
*
|
||||
* The key range of a task is cut to what its queries can see, so a sliding layer costs
|
||||
* O(T * window). Each output row is one task's, so the result does not depend on the thread
|
||||
* count. Same arithmetic class as the loop below: f32 scores (bf16 products are exact in f32),
|
||||
* f32 softmax and accumulator, one rounding at the store. */
|
||||
#if AVX_LEVEL == 3
|
||||
#define AVX_BF16_TARGET __attribute__((target("avx512f,avx512bw,avx512vl,avx512bf16")))
|
||||
|
||||
/* S[r][0..63] for MB_ query rows against a key tile packed as bf16 PAIRS INTERLEAVED BY KEY:
|
||||
* kp[d2 * 64 + j] holds key j's dims 2*d2 and 2*d2+1. Each step broadcasts one row's pair and
|
||||
* multiplies it into four 16-key vectors, so a score is never horizontally reduced -- with a
|
||||
* 64-wide head a reduction per score cost more than the two products it closed. */
|
||||
template <int MB_>
|
||||
AVX_BF16_TARGET static inline void qk_rows(const uint16_t* const* qr, const uint32_t* kp,
|
||||
int64_t D2, float scale, float* S, int64_t ldS) {
|
||||
__m512 acc[MB_][4];
|
||||
#pragma GCC unroll 8
|
||||
for (int i = 0; i < MB_; ++i)
|
||||
#pragma GCC unroll 4
|
||||
for (int v = 0; v < 4; ++v) acc[i][v] = _mm512_setzero_ps();
|
||||
for (int64_t d2 = 0; d2 < D2; ++d2) {
|
||||
const uint32_t* kr = kp + d2 * 64;
|
||||
const __m512bh b0 = (__m512bh)_mm512_loadu_si512((const void*)(kr));
|
||||
const __m512bh b1 = (__m512bh)_mm512_loadu_si512((const void*)(kr + 16));
|
||||
const __m512bh b2 = (__m512bh)_mm512_loadu_si512((const void*)(kr + 32));
|
||||
const __m512bh b3 = (__m512bh)_mm512_loadu_si512((const void*)(kr + 48));
|
||||
#pragma GCC unroll 8
|
||||
for (int i = 0; i < MB_; ++i) {
|
||||
uint32_t pr;
|
||||
std::memcpy(&pr, qr[i] + 2 * d2, 4);
|
||||
const __m512bh a = (__m512bh)_mm512_set1_epi32((int)pr);
|
||||
acc[i][0] = _mm512_dpbf16_ps(acc[i][0], a, b0);
|
||||
acc[i][1] = _mm512_dpbf16_ps(acc[i][1], a, b1);
|
||||
acc[i][2] = _mm512_dpbf16_ps(acc[i][2], a, b2);
|
||||
acc[i][3] = _mm512_dpbf16_ps(acc[i][3], a, b3);
|
||||
}
|
||||
}
|
||||
const __m512 vs = _mm512_set1_ps(scale);
|
||||
#pragma GCC unroll 8
|
||||
for (int i = 0; i < MB_; ++i)
|
||||
#pragma GCC unroll 4
|
||||
for (int v = 0; v < 4; ++v) _mm512_storeu_ps(S + i * ldS + 16 * v, _mm512_mul_ps(acc[i][v], vs));
|
||||
}
|
||||
|
||||
/* acc[r][c0 .. c0+63] += P[r] . V for MB_ rows, in bf16 pairs: pp[r * 32 + k2] holds row r's
|
||||
* probabilities for keys 2*k2 and 2*k2+1, vp[k2 * D + d] holds those two keys' values at column
|
||||
* d. PyTorch's CPU flash attention -- the reference's -- rounds P to bf16 for this product as
|
||||
* well; the softmax's sum is kept in f32 from the unrounded P. */
|
||||
template <int MB_>
|
||||
AVX_BF16_TARGET static inline void pv_rows(float* acc, int64_t D, const uint32_t* pp,
|
||||
const uint32_t* vp, int64_t K2, int64_t c0) {
|
||||
__m512 o[MB_][4];
|
||||
#pragma GCC unroll 8
|
||||
for (int i = 0; i < MB_; ++i)
|
||||
#pragma GCC unroll 4
|
||||
for (int v = 0; v < 4; ++v) o[i][v] = _mm512_loadu_ps(acc + i * D + c0 + 16 * v);
|
||||
for (int64_t k2 = 0; k2 < K2; ++k2) {
|
||||
const uint32_t* vr = vp + k2 * D + c0;
|
||||
const __m512bh b0 = (__m512bh)_mm512_loadu_si512((const void*)(vr));
|
||||
const __m512bh b1 = (__m512bh)_mm512_loadu_si512((const void*)(vr + 16));
|
||||
const __m512bh b2 = (__m512bh)_mm512_loadu_si512((const void*)(vr + 32));
|
||||
const __m512bh b3 = (__m512bh)_mm512_loadu_si512((const void*)(vr + 48));
|
||||
#pragma GCC unroll 8
|
||||
for (int i = 0; i < MB_; ++i) {
|
||||
const __m512bh a = (__m512bh)_mm512_set1_epi32((int)pp[i * 32 + k2]);
|
||||
o[i][0] = _mm512_dpbf16_ps(o[i][0], a, b0);
|
||||
o[i][1] = _mm512_dpbf16_ps(o[i][1], a, b1);
|
||||
o[i][2] = _mm512_dpbf16_ps(o[i][2], a, b2);
|
||||
o[i][3] = _mm512_dpbf16_ps(o[i][3], a, b3);
|
||||
}
|
||||
}
|
||||
#pragma GCC unroll 8
|
||||
for (int i = 0; i < MB_; ++i)
|
||||
#pragma GCC unroll 4
|
||||
for (int v = 0; v < 4; ++v) _mm512_storeu_ps(acc + i * D + c0 + 16 * v, o[i][v]);
|
||||
}
|
||||
|
||||
AVX_BF16_TARGET static bool attn_dense_bf16(const RadTensor* q, const RadTensor* k,
|
||||
const RadTensor* v, const RadTensor* cu,
|
||||
const RadTensor* o, int64_t D, int64_t gqa,
|
||||
int64_t kv_heads, int causal, int64_t window,
|
||||
float scale, int64_t q_rs, int64_t q_hs, int64_t q_ds,
|
||||
int64_t k_rs, int64_t k_hs, int64_t k_ds, int64_t v_rs,
|
||||
int64_t v_hs, int64_t v_ds, int64_t o_rs, int64_t o_hs,
|
||||
int64_t o_ds) {
|
||||
static const bool have = __builtin_cpu_supports("avx512bf16");
|
||||
if (!have) return false;
|
||||
if (q->dtype != RAD_BF16 || k->dtype != RAD_BF16 || v->dtype != RAD_BF16) return false;
|
||||
if (q_ds != 1 || k_ds != 1 || v_ds != 1 || o_ds != 1 || D % 64 != 0 || gqa > 16) return false;
|
||||
|
||||
constexpr int64_t QB = 32, KT = 64;
|
||||
const int64_t n_seq = numel(cu) - 1;
|
||||
/* The task list: (segment, kv head, query block), flattened through a prefix over segments. */
|
||||
std::vector<int64_t> first((size_t)n_seq + 1, 0);
|
||||
for (int64_t s = 0; s < n_seq; ++s) {
|
||||
const int64_t len = ldi(cu, offlin(cu, s + 1)) - ldi(cu, offlin(cu, s));
|
||||
first[(size_t)s + 1] = first[(size_t)s] + kv_heads * ((len + QB - 1) / QB);
|
||||
}
|
||||
const int64_t n_task = first[(size_t)n_seq];
|
||||
const uint16_t* Q = (const uint16_t*)q->data;
|
||||
const uint16_t* K = (const uint16_t*)k->data;
|
||||
const uint16_t* V = (const uint16_t*)v->data;
|
||||
const int64_t R_MAX = QB * gqa;
|
||||
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for schedule(dynamic, 1) num_threads(avx_team_size())
|
||||
#endif
|
||||
for (int64_t t = 0; t < n_task; ++t) {
|
||||
int64_t s = (int64_t)(std::upper_bound(first.begin(), first.end(), t) - first.begin()) - 1;
|
||||
const int64_t b0 = ldi(cu, offlin(cu, s)), len = ldi(cu, offlin(cu, s + 1)) - b0;
|
||||
const int64_t local = t - first[(size_t)s];
|
||||
const int64_t nqb = (len + QB - 1) / QB;
|
||||
const int64_t kh = local / nqb, q0 = (local % nqb) * QB;
|
||||
const int64_t q1 = std::min(q0 + QB, len), nq = q1 - q0, R = nq * gqa;
|
||||
|
||||
float* frame = scratch_f32((size_t)(R_MAX * D + R_MAX * KT + 2 * R_MAX + KT * D / 2 +
|
||||
KT * D / 2 + R_MAX * KT / 2) + 64);
|
||||
float* acc = frame;
|
||||
float* S = acc + R_MAX * D;
|
||||
float* m = S + R_MAX * KT;
|
||||
float* l = m + R_MAX;
|
||||
uint32_t* kpack = reinterpret_cast<uint32_t*>(l + R_MAX);
|
||||
uint32_t* vpack = kpack + KT * D / 2;
|
||||
uint32_t* ppack = vpack + KT * D / 2;
|
||||
const int64_t D2 = D / 2;
|
||||
const uint16_t* qrow[16 * QB];
|
||||
for (int64_t r = 0; r < R; ++r) {
|
||||
const int64_t qi = r / gqa, g = r % gqa;
|
||||
qrow[r] = Q + (b0 + q0 + qi) * q_rs + (kh * gqa + g) * q_hs;
|
||||
m[r] = -INFINITY;
|
||||
l[r] = 0.0f;
|
||||
}
|
||||
std::memset(acc, 0, sizeof(float) * (size_t)(R * D));
|
||||
|
||||
int64_t jbeg = 0, jend = causal ? q1 : len;
|
||||
if (window > 0) {
|
||||
jbeg = std::max<int64_t>(0, q0 - window + 1);
|
||||
if (!causal) jend = std::min<int64_t>(len, q1 - 1 + window);
|
||||
}
|
||||
for (int64_t j0 = jbeg; j0 < jend; j0 += KT) {
|
||||
const int64_t kt = std::min(KT, jend - j0);
|
||||
/* The key tile packed once for every row of the task: pairs interleaved by key, the
|
||||
* keys past the tile's end zero (their scores are never read). */
|
||||
for (int64_t d2 = 0; d2 < D2; ++d2) {
|
||||
uint32_t* dst = kpack + d2 * KT;
|
||||
for (int64_t j = 0; j < kt; ++j)
|
||||
std::memcpy(dst + j, K + (b0 + j0 + j) * k_rs + kh * k_hs + 2 * d2, 4);
|
||||
for (int64_t j = kt; j < KT; ++j) dst[j] = 0;
|
||||
}
|
||||
|
||||
/* S, six rows a block against the whole packed tile. */
|
||||
int64_t r = 0;
|
||||
for (; r + 6 <= R; r += 6) qk_rows<6>(qrow + r, kpack, D2, scale, S + r * KT, KT);
|
||||
for (; r < R; ++r) qk_rows<1>(qrow + r, kpack, D2, scale, S + r * KT, KT);
|
||||
/* The mask, by position inside the segment. */
|
||||
if (causal || window > 0) {
|
||||
for (int64_t r = 0; r < R; ++r) {
|
||||
const int64_t i = q0 + r / gqa;
|
||||
for (int64_t j = 0; j < kt; ++j) {
|
||||
const int64_t d = i - (j0 + j);
|
||||
bool ok = !causal || d >= 0;
|
||||
if (window > 0) ok = ok && (causal ? d < window : (d < window && -d < window));
|
||||
if (!ok) S[r * KT + j] = -INFINITY;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The online softmax, row by row, four vectors a row; S becomes P in place. The keys
|
||||
* past a short tile's end score -inf, so P is zero there and every row is full width. */
|
||||
if (kt < KT)
|
||||
for (int64_t r = 0; r < R; ++r)
|
||||
for (int64_t j = kt; j < KT; ++j) S[r * KT + j] = -INFINITY;
|
||||
for (int64_t r = 0; r < R; ++r) {
|
||||
float* sr = S + r * KT;
|
||||
const float tmax = _mm512_reduce_max_ps(
|
||||
_mm512_max_ps(_mm512_max_ps(_mm512_loadu_ps(sr), _mm512_loadu_ps(sr + 16)),
|
||||
_mm512_max_ps(_mm512_loadu_ps(sr + 32), _mm512_loadu_ps(sr + 48))));
|
||||
if (tmax == -INFINITY) {
|
||||
for (int64_t j = 0; j < KT; ++j) sr[j] = 0.0f;
|
||||
continue;
|
||||
}
|
||||
if (tmax > m[r]) {
|
||||
const float alpha = m[r] == -INFINITY ? 0.0f : std::exp(m[r] - tmax);
|
||||
if (alpha != 1.0f) {
|
||||
float* ar = acc + r * D;
|
||||
const __m512 va = _mm512_set1_ps(alpha);
|
||||
for (int64_t d = 0; d < D; d += 16)
|
||||
_mm512_storeu_ps(ar + d, _mm512_mul_ps(_mm512_loadu_ps(ar + d), va));
|
||||
l[r] *= alpha;
|
||||
}
|
||||
m[r] = tmax;
|
||||
}
|
||||
const vf vm = vf_set1(m[r]);
|
||||
__m512 sum = _mm512_setzero_ps();
|
||||
for (int64_t j = 0; j < KT; j += 16) {
|
||||
/* exp of -inf is the clamp's floor, ~1e-38: zero to every sum it joins. */
|
||||
const vf e = vf_exp(vf_sub(vf_loadu(sr + j), vm));
|
||||
vf_storeu(sr + j, e);
|
||||
sum = _mm512_add_ps(sum, e);
|
||||
}
|
||||
l[r] += _mm512_reduce_add_ps(sum);
|
||||
}
|
||||
/* V's tile packed once for every row of the task, consecutive keys interleaved in
|
||||
* pairs (a key past the tile's end is zero), and each row's P rounded to bf16 pairs
|
||||
* along the keys. */
|
||||
const int64_t K2 = (kt + 1) / 2;
|
||||
{
|
||||
/* Two key rows' words interleaved, 32 columns a step: lanes 0..15 of the first
|
||||
* output are (k0[d], k1[d]) for d = 0..15, the second's for d = 16..31. */
|
||||
const __m512i ilo = _mm512_set_epi16(47, 15, 46, 14, 45, 13, 44, 12, 43, 11, 42, 10,
|
||||
41, 9, 40, 8, 39, 7, 38, 6, 37, 5, 36, 4, 35, 3,
|
||||
34, 2, 33, 1, 32, 0);
|
||||
const __m512i ihi = _mm512_set_epi16(63, 31, 62, 30, 61, 29, 60, 28, 59, 27, 58, 26,
|
||||
57, 25, 56, 24, 55, 23, 54, 22, 53, 21, 52, 20,
|
||||
51, 19, 50, 18, 49, 17, 48, 16);
|
||||
for (int64_t k2 = 0; k2 < K2; ++k2) {
|
||||
const uint16_t* v0 = V + (b0 + j0 + 2 * k2) * v_rs + kh * v_hs;
|
||||
const bool two = 2 * k2 + 1 < kt;
|
||||
uint32_t* dst = vpack + k2 * D;
|
||||
for (int64_t d = 0; d < D; d += 32) {
|
||||
const __m512i a = _mm512_loadu_si512((const void*)(v0 + d));
|
||||
const __m512i bb = two ? _mm512_loadu_si512((const void*)(v0 + v_rs + d))
|
||||
: _mm512_setzero_si512();
|
||||
_mm512_storeu_si512((void*)(dst + d), _mm512_permutex2var_epi16(a, ilo, bb));
|
||||
_mm512_storeu_si512((void*)(dst + d + 16),
|
||||
_mm512_permutex2var_epi16(a, ihi, bb));
|
||||
}
|
||||
}
|
||||
}
|
||||
/* P to bf16, round to nearest even: a row's 64 values are its 32 key pairs in order. */
|
||||
for (int64_t r = 0; r < R; ++r) {
|
||||
const float* sr = S + r * KT;
|
||||
uint32_t* pr = ppack + r * 32;
|
||||
_mm512_storeu_si512((void*)pr, (__m512i)_mm512_cvtne2ps_pbh(_mm512_loadu_ps(sr + 16),
|
||||
_mm512_loadu_ps(sr)));
|
||||
_mm512_storeu_si512((void*)(pr + 16),
|
||||
(__m512i)_mm512_cvtne2ps_pbh(_mm512_loadu_ps(sr + 48),
|
||||
_mm512_loadu_ps(sr + 32)));
|
||||
}
|
||||
for (int64_t c0 = 0; c0 < D; c0 += 64) {
|
||||
int64_t r = 0;
|
||||
for (; r + 6 <= R; r += 6) pv_rows<6>(acc + r * D, D, ppack + r * 32, vpack, K2, c0);
|
||||
for (; r < R; ++r) pv_rows<1>(acc + r * D, D, ppack + r * 32, vpack, K2, c0);
|
||||
}
|
||||
}
|
||||
for (int64_t r = 0; r < R; ++r) {
|
||||
const int64_t qi = r / gqa, g = r % gqa;
|
||||
const int64_t ob = (b0 + q0 + qi) * o_rs + (kh * gqa + g) * o_hs;
|
||||
float* ar = acc + r * D;
|
||||
const float inv = (m[r] == -INFINITY || l[r] <= 0.0f) ? 0.0f : 1.0f / l[r];
|
||||
const __m512 vi = _mm512_set1_ps(inv);
|
||||
for (int64_t d = 0; d < D; d += 16)
|
||||
_mm512_storeu_ps(ar + d, _mm512_mul_ps(_mm512_loadu_ps(ar + d), vi));
|
||||
out_row(o, ob, D, ar);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ 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
|
||||
@@ -505,10 +784,15 @@ AVX_KERNEL(attn_dense) {
|
||||
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;
|
||||
/* libref's window: causal, i - j < window; non-causal, |i - j| < window; 0 none. */
|
||||
const int64_t window = p_int(a, "window", 0);
|
||||
if (head_dim <= 0 || gqa <= 0 || window < 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;
|
||||
/* A rank-2 operand is [tokens, heads * head_dim], so its head count is its width over the
|
||||
* head; reading it as one head would take every head of a multi-head slice for one long one. */
|
||||
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;
|
||||
@@ -534,6 +818,11 @@ AVX_KERNEL(attn_dense) {
|
||||
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));
|
||||
#if AVX_LEVEL == 3
|
||||
if (attn_dense_bf16(q, k, v, cu, o, head_dim, gqa, kv_heads, causal, window, scale, q_rs,
|
||||
q_hs, q_ds, k_rs, k_hs, k_ds, v_rs, v_hs, v_ds, o_rs, o_hs, o_ds))
|
||||
return RAD_OK;
|
||||
#endif
|
||||
|
||||
AVX_PARALLEL_FOR_IF(n_seq * q_heads >= 2)
|
||||
for (int64_t sh = 0; sh < n_seq * q_heads; ++sh) {
|
||||
@@ -550,7 +839,11 @@ AVX_KERNEL(attn_dense) {
|
||||
|
||||
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;
|
||||
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 + window);
|
||||
}
|
||||
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;
|
||||
@@ -589,7 +882,7 @@ AVX_KERNEL(attn_dense) {
|
||||
ntile = 0;
|
||||
};
|
||||
|
||||
for (int64_t j = b0; j < jend; ++j) {
|
||||
for (int64_t j = jbeg; 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;
|
||||
@@ -617,6 +910,92 @@ AVX_KERNEL(attn_dense) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ attn_local_rel */
|
||||
/* libref's local attention with a relative-position term (Gemma 4's audio). A query sees at most
|
||||
* left + 1 keys -- thirteen at the checkpoint's geometry -- so the work is a few dot products a
|
||||
* (row, head) and the loop is libref's, vectorised along the head and parallel over (row, head). */
|
||||
AVX_KERNEL(attn_local_rel) {
|
||||
if (!rad_args_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 > AVX_HD_MAX || L + 1 > AVX_HD_MAX) return RAD_E_UNSUPPORTED;
|
||||
const int64_t Wd = H * D;
|
||||
if (numel(pds) < D || numel(rk) < (L + 1) * Wd) return RAD_E_SHAPE;
|
||||
const int64_t S = numel(cu) - 1;
|
||||
if (S <= 0) return RAD_E_SHAPE;
|
||||
const int64_t Mq = numel(q) / Wd, Mk = numel(k) / Wd, Mv = numel(v) / Wd, Mo = numel(o) / Wd;
|
||||
const int64_t q_rs = row_stride(q, Wd), k_rs = row_stride(k, Wd), v_rs = row_stride(v, Wd);
|
||||
const int64_t r_rs = row_stride(rk, Wd), o_rs = row_stride(o, Wd);
|
||||
const int64_t q_ds = laststride(q), k_ds = laststride(k), v_ds = laststride(v);
|
||||
const int64_t r_ds = laststride(rk), o_ds = laststride(o);
|
||||
|
||||
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));
|
||||
/* softplus(pds), rounded to the weight's dtype as the reference evaluates it on the bf16
|
||||
* parameter (libref's reading), and the query's whole scale folded into one vector. */
|
||||
float* spd = (float*)scratch_raw((size_t)D * sizeof(float) + 64);
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
const float x = ldt(pds, d * laststride(pds));
|
||||
const float sp = x > 20.0f ? x : (x > 0.0f ? x + std::log1p(std::exp(-x))
|
||||
: std::log1p(std::exp(x)));
|
||||
float r = sp;
|
||||
if (pds->dtype == RAD_BF16) r = rad_bf16_to_f32(rad_f32_to_bf16(sp));
|
||||
else if (pds->dtype == RAD_F16) r = rad_f16_to_f32(rad_f32_to_f16(sp));
|
||||
spd[d] = r;
|
||||
}
|
||||
|
||||
for (int64_t s = 0; s < S; ++s) {
|
||||
const int64_t c0 = ldi(cu, offlin(cu, s)), c1 = ldi(cu, offlin(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;
|
||||
|
||||
AVX_PARALLEL_FOR_IF((t1 - t0) * H * (L + 1) * D >= AVX_PAR_MIN)
|
||||
for (int64_t ih = 0; ih < (t1 - t0) * H; ++ih) {
|
||||
const int64_t i = t0 + ih / H, h = ih % H;
|
||||
float* frame = scratch_f32((size_t)(4 * D + L + 1) + 16);
|
||||
float* qf = frame;
|
||||
float* kb = frame + D;
|
||||
float* acc = frame + 2 * D;
|
||||
float* tmp = frame + 3 * D;
|
||||
float* lg = frame + 4 * D;
|
||||
const float* qv = head_f32(q, i * q_rs + h * D * q_ds, q_ds, D, 1.0f, tmp);
|
||||
for (int64_t d = 0; d < D; ++d) qf[d] = qv[d] * 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 float* kv = head_f32(k, j * k_rs + h * D * k_ds, k_ds, D, 1.0f, tmp);
|
||||
for (int64_t d = 0; d < D; ++d) kb[d] = kv[d] * ks;
|
||||
float ac = dotf(qf, kb, D);
|
||||
const float* rv = head_f32(rk, (i - j) * r_rs + h * D * r_ds, r_ds, D, 1.0f, tmp);
|
||||
ac += dotf(qf, rv, D);
|
||||
const float x = cap * std::tanh(ac / 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] = std::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)
|
||||
axpy(acc, head_f32(v, j * v_rs + h * D * v_ds, v_ds, D, 1.0f, tmp),
|
||||
lg[j - j0] / sum, D);
|
||||
const int64_t ob = i * o_rs + h * D * o_ds;
|
||||
if (o_ds == 1) out_row(o, ob, D, acc);
|
||||
else for (int64_t d = 0; d < D; ++d) stt(o, ob + d * o_ds, acc[d]);
|
||||
}
|
||||
}
|
||||
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.
|
||||
@@ -888,27 +1267,77 @@ AVX_KERNEL(qk_norm_rope) {
|
||||
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 int v_norm = (int)p_int(a, "v_norm", 0);
|
||||
static const char* kModes[] = { "neox", "gptj", "blocks" };
|
||||
const int mode = p_enum(a, "mode", kModes, 3, ROPE_NEOX);
|
||||
if (mode < 0) return RAD_E_UNSUPPORTED;
|
||||
const bool blocks = mode == 2;
|
||||
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;
|
||||
|
||||
/* BLOCKS (libref): section c is sec[c] pairs over its own 2 * sec[c] channels, paired
|
||||
* (start + j, start + sec[c] + j), turned by position component comps[c]. */
|
||||
int64_t sec[8] = {}, comp[8] = {};
|
||||
int n_sec = 0;
|
||||
if (blocks) {
|
||||
for (const char* v = p_str(a, "sections", nullptr); v && *v && n_sec < 8; ) {
|
||||
char* end = nullptr;
|
||||
const long long x = std::strtoll(v, &end, 10);
|
||||
if (end == v) break;
|
||||
if (x <= 0) return RAD_E_SHAPE;
|
||||
comp[n_sec] = n_sec;
|
||||
sec[n_sec++] = x;
|
||||
v = end;
|
||||
}
|
||||
int64_t total = 0;
|
||||
for (int c = 0; c < n_sec; ++c) total += sec[c];
|
||||
if (n_sec <= 0 || 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 bool multi = pos->rank >= 2;
|
||||
const int64_t n_comp = multi ? pos->shape[0] : 1;
|
||||
if (multi && !blocks) return RAD_E_SHAPE;
|
||||
for (int c = 0; c < n_sec; ++c)
|
||||
if (comp[c] < 0 || comp[c] >= n_comp || n_comp > 8) return RAD_E_SHAPE;
|
||||
const int64_t pcs = multi ? pos->stride[0] : 0, pms = multi ? pos->stride[1] : 0;
|
||||
|
||||
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
|
||||
const int64_t M = numel(pos);
|
||||
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;
|
||||
|
||||
/* 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);
|
||||
/* One arena call for the launch-invariant tables: the inverse frequencies (and, for blocks,
|
||||
* each pair's component) and both widened gains. A second scratch_raw would reallocate under
|
||||
* the parallel region. */
|
||||
float* tab = (float*)scratch_raw((size_t)(2 * 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);
|
||||
float* pcomp = tab + half;
|
||||
float* gq = tab + 2 * half;
|
||||
float* gk = tab + 2 * half + head_dim;
|
||||
if (!blocks) {
|
||||
for (int64_t i = 0; i < half; ++i)
|
||||
invf[i] = 1.0f / std::pow(theta, (float)(2 * i) / (float)rot);
|
||||
} else {
|
||||
int64_t i = 0;
|
||||
for (int c = 0; c < n_sec; ++c)
|
||||
for (int64_t j = 0; j < sec[c]; ++j, ++i) {
|
||||
invf[i] = 1.0f / std::pow(theta, (float)j / (float)sec[c]);
|
||||
pcomp[i] = (float)comp[c];
|
||||
}
|
||||
}
|
||||
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;
|
||||
@@ -916,12 +1345,23 @@ AVX_KERNEL(qk_norm_rope) {
|
||||
|
||||
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 float div = rscale != 0.0f ? rscale : 1.0f;
|
||||
float pc[8];
|
||||
if (multi)
|
||||
for (int64_t c = 0; c < n_comp; ++c) pc[c] = (float)ldi(pos, c * pcs + m * pms) / div;
|
||||
else
|
||||
pc[0] = (float)ldi(pos, offlin(pos, m)) / div;
|
||||
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);
|
||||
if (!blocks) angles(cs, invf, half, pc[0]);
|
||||
else
|
||||
for (int64_t i = 0; i < half; ++i) {
|
||||
const float ang = pc[(int)pcomp[i]] * invf[i];
|
||||
cs[i] = std::cos(ang);
|
||||
cs[half + i] = std::sin(ang);
|
||||
}
|
||||
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;
|
||||
@@ -932,11 +1372,33 @@ AVX_KERNEL(qk_norm_rope) {
|
||||
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. */
|
||||
/* Written back through the operand's dtype, then re-read by the rotation -- 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);
|
||||
if (!blocks) {
|
||||
rope_head(qkv, base, ds, rot, mode, cs, half, rbuf);
|
||||
continue;
|
||||
}
|
||||
int64_t i = 0, start = 0;
|
||||
for (int c = 0; c < n_sec; ++c) {
|
||||
for (int64_t j = 0; j < sec[c]; ++j, ++i) {
|
||||
const int64_t i0 = start + j, i1 = 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[i] - x1 * cs[half + i]);
|
||||
stt(qkv, base + i1 * ds, x0 * cs[half + i] + x1 * cs[i]);
|
||||
}
|
||||
start += 2 * sec[c];
|
||||
}
|
||||
}
|
||||
/* v_norm: the v heads RMS-normed with a unit gain and not rotated (libref). */
|
||||
for (int64_t h = 0; v_norm && h < n_head_kv; ++h) {
|
||||
const int64_t base = row + (n_head + n_head_kv + 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);
|
||||
for (int64_t d = 0; d < head_dim; ++d) rbuf[d] = xv[d] * sc;
|
||||
if (ds == 1) out_row(qkv, base, head_dim, rbuf);
|
||||
else for (int64_t d = 0; d < head_dim; ++d) stt(qkv, base + d * ds, rbuf[d]);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
|
||||
Reference in new issue
Block a user