libavx: swiglu_step, router_sigmoid_group_topk and mla_attn_paged, held to libref by avx_check

This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-11-03 19:07:34 +01:00
1 parent 7169c9da0a
commit e4ca20ddf8
5 files changed
+205

No files matched your search

+92
View File
@@ -55,6 +55,98 @@ AVX_KERNEL(mla_project) {
return RAD_OK;
}
/* libref's mla_attn_paged (libref/ref_mla.cpp): dense causal latent attention over a cache row of
* the latent then the rotated key part, one sequence's page table, an optional head gate. */
AVX_KERNEL(mla_attn_paged) {
if (a->n_t < 8 || !rad_arg_in(a, 0) || !rad_arg_in(a, 2) || !rad_arg_in(a, 3) ||
!rad_arg_in(a, 4) || !rad_arg_in(a, 5) || !rad_arg_in(a, 7))
return RAD_E_INVAL;
RadTensor q = a->t[0], qr = a->t[1], o = a->t[7];
const auto &kv = a->t[2], &bt = a->t[3], &su = a->t[4], &cu = a->t[5];
const RadTensor* gate = rad_arg_in(a, 6);
const int64_t h = p_int(a, "heads", 0), L = p_int(a, "latent_dim", 0),
R = p_int(a, "rope_dim", 0), bs = p_int(a, "block_size", 0);
const float scale = p_f32(a, "scale", 0);
auto heads_view = [&](RadTensor& t, int64_t width) {
if (h > 0 && width > 0 && t.rank == 2 && t.shape[1] == h * width) {
t.rank = 3;
t.shape[1] = h;
t.shape[2] = width;
t.stride[2] = t.stride[1];
t.stride[1] *= width;
}
};
heads_view(q, L);
heads_view(qr, R);
heads_view(o, L);
if (h < 1 || L < 1 || L > 2048 || R < 0 || R > 2048 || bs < 1 || !std::isfinite(scale) ||
scale <= 0 || q.rank != 3 || q.shape[0] < 1 || q.shape[1] != h || q.shape[2] != L ||
(R && (!qr.data || qr.rank != 3 || qr.shape[0] != q.shape[0] || qr.shape[1] != h ||
qr.shape[2] != R)) ||
kv.rank != 4 || kv.shape[1] != 1 || kv.shape[2] != bs || kv.shape[3] < L + R ||
bt.rank != 2 || su.rank != 1 || cu.rank != 1 || numel(&cu) != numel(&su) + 1 ||
o.rank != 3 || o.shape[0] != q.shape[0] || o.shape[1] != h || o.shape[2] != L ||
(gate && (gate->rank != 2 || gate->shape[0] != q.shape[0] || gate->shape[1] < h)))
return RAD_E_SHAPE;
const int64_t n_seq = numel(&su), rows = q.shape[0];
auto cu_at = [&](int64_t i) { return ld_int_dt(cu.dtype, cu.data, i * laststride(&cu)); };
if (cu_at(0) != 0 || cu_at(n_seq) != rows) return RAD_E_SHAPE;
AVX_PARALLEL_FOR_IF(rows * h * (L + R) * bt.shape[1] * bs >= AVX_PAR_MIN)
for (int64_t job = 0; job < rows * h; ++job) {
const int64_t r = job / h, head = job % h;
int64_t s = 0;
while (s + 1 < n_seq && cu_at(s + 1) <= r) ++s;
const int64_t qlen = cu_at(s + 1) - cu_at(s), i = r - cu_at(s);
const int64_t ctx = ld_int_dt(su.dtype, su.data, s * su.stride[0]);
const int64_t live = std::min(ctx - qlen + i + 1, bt.shape[1] * bs);
float *tmp = scratch_f32((size_t)(3 * L + 2 * R)), *query = tmp, *key = tmp + L,
*value = tmp + 2 * L, *qrot = tmp + 3 * L, *krot = qrot + R;
for (int64_t c = 0; c < L; ++c) {
query[c] = ldt(&q, r * q.stride[0] + head * q.stride[1] + c * q.stride[2]);
value[c] = 0;
}
for (int64_t c = 0; c < R; ++c)
qrot[c] = ldt(&qr, r * qr.stride[0] + head * qr.stride[1] + c * qr.stride[2]);
float max = -INFINITY, den = 0;
for (int64_t j = 0; j < live; ++j) {
const int64_t page =
ld_int_dt(bt.dtype, bt.data, s * bt.stride[0] + (j / bs) * bt.stride[1]);
if (page < 0 || page >= kv.shape[0]) continue;
const int64_t base = page * kv.stride[0] + (j % bs) * kv.stride[2];
for (int64_t c = 0; c < L; ++c) key[c] = ldt(&kv, base + c * kv.stride[3]);
for (int64_t c = 0; c < R; ++c) krot[c] = ldt(&kv, base + (L + c) * kv.stride[3]);
vf acc = vf_zero();
int64_t c = 0;
for (; c + VF_N <= L; c += VF_N)
acc = vf_fma(vf_loadu(query + c), vf_loadu(key + c), acc);
float score = vf_hsum(acc);
for (; c < L; ++c) score += query[c] * key[c];
for (c = 0; c < R; ++c) score += qrot[c] * krot[c];
score *= scale;
const float next = std::max(max, score), old = std::exp(max - next),
w = std::exp(score - next);
den = den * old + w;
max = next;
const vf a = vf_set1(old), b = vf_set1(w);
c = 0;
for (; c + VF_N <= L; c += VF_N)
vf_storeu(value + c, vf_fma(vf_loadu(key + c), b, vf_mul(vf_loadu(value + c), a)));
for (; c < L; ++c) value[c] = value[c] * old + key[c] * w;
}
float g = 1.0f;
if (gate) {
const float x = ldt(gate, r * gate->stride[0] + head * gate->stride[1]);
g = narrow1(RAD_BF16, 1.0f / (1.0f + std::exp(-x)));
}
for (int64_t c = 0; c < L; ++c) {
float v = den > 0 ? value[c] / den : 0;
if (gate) v = narrow1(RAD_BF16, v) * g;
stt(&o, r * o.stride[0] + head * o.stride[1] + c * o.stride[2], v);
}
}
return RAD_OK;
}
AVX_KERNEL(mla_attn) {
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
const auto &q = a->t[0], &kv = a->t[1], &bt = a->t[2], &len = a->t[3], &out = a->t[4];