libxe2: qk_norm_rope, conv1d_dw, conv2d, attn_local_rel rows

The encoder towers' ops that had only a host kernel: on the SYCL backend every op runs on the card,
so a container carrying Gemma 4's vision tower (qk_norm_rope, blocks mode, v_norm) or its audio
conformer (conv2d subsampler, conv1d_dw, attn_local_rel) refused to start. qk_norm_rope also serves
GLM's vision tower (axial) and any QK-norm model that declares the fused op.

Each is libref's loop, a work-item an output (a (row, head) for qk_norm_rope and attn_local_rel),
for passes that run once a picture or clip. cu_seqlens stays on the card, so each work-item finds
its own segment.

rad-kbench, Gemma 4 E2B container: qk_norm_rope 80/80, conv1d_dw 64/64, conv2d 34/34,
attn_local_rel 64/64; default fixture 2144/2144. Gemma 4 E2B now serves on Intel: text and an
image request answer correctly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-08 14:28:38 +01:00
1 parent ff4b7c9fb4
commit 25c8fbd136
4 files changed
+441

No files matched your search

+274
View File
@@ -0,0 +1,274 @@
/* xe2_media.cpp -- the encoder towers' ops that had no device row (Gemma 4's audio conformer, the
* Qwen3-ASR and Whisper-style subsamplers): libref's loops, a work-item an output, for a pass that
* runs once a clip. On the SYCL backend every op runs on the card, so without these rows a container
* carrying such a tower does not start.
*
* cu_seqlens lives on the card, so each work-item finds its own segment (a short scan of cu) and a
* row no segment covers is left untouched, as libref leaves it. libref's host-side checks of cu
* (divisibility by cu_div, rows in range) become a skip of the bad segment's rows.
*
* conv1d_dw: y[t, c] = sum_j w[c, j] x[t - (k-1) + j, c] over the segment's rows (earlier ones zero),
* plus b[c], f32 in j order, one rounding.
* conv2d: libref's 2-D convolution over (time, width), channels innermost, segment by segment, zero
* padded, the optional frame mask, bias, and gelu on the rounded value.
* attn_local_rel: Gemma 4's audio attention, query i over keys i - left .. i of its segment, the
* relative-position term and the tanh softcap, softmax and the weighted v sum in f32; transcendentals
* in double rounded once.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include <algorithm>
#include <cmath>
#include <cstring>
namespace xe2 {
namespace {
size_t groups_of(int64_t n, size_t wg) { return (size_t)((n + (int64_t)wg - 1) / (int64_t)wg); }
struct Cu {
const char* p = nullptr;
uint32_t dt = 0;
Lin l;
int64_t S = 0;
int64_t at(int64_t i) const { return ld_index(dt, p, l.off(i)); }
/* the segment holding row r of a pass whose row bounds are cu / div, or -1; its start in *t0 */
int64_t find(int64_t r, int64_t div, int64_t* t0, int64_t* t1) const {
for (int64_t s = 0; s < S; ++s) {
const int64_t c0 = at(s), c1 = at(s + 1);
if (c0 % div || c1 % div || c1 < c0) continue;
if (r >= c0 / div && r < c1 / div) { *t0 = c0 / div; *t1 = c1 / div; return s; }
}
return -1;
}
};
Cu cu_of(const RadTensor* t) {
Cu c;
c.p = (const char*)t->data;
c.dt = t->dtype;
c.l = Lin::of(t);
c.S = rad_tensor_numel(t) - 1;
return c;
}
} // namespace
int conv1d_dw(const RadArgs* a, RadStream s) {
if (!rad_args_have(a, 4)) return refuse("conv1d_dw: missing operand");
const RadTensor *X = &a->t[0], *Wt = &a->t[1], *CU = &a->t[2], *Y = &a->t[3];
const RadTensor* B = rad_arg_in(a, 4);
const int64_t C = rad_args_geti_or(a, "n", 0), K = rad_args_geti_or(a, "k", 0), cdiv = rad_args_geti_or(a, "cu_div", 1);
if (C <= 0 || K <= 0 || cdiv <= 0 || rad_tensor_numel(Wt) < C * K || (B && rad_tensor_numel(B) < C)) return RAD_E_SHAPE;
const Cu cu = cu_of(CU);
if (cu.S <= 0) return RAD_E_SHAPE;
Rows x, y;
if (int rc = rows_view(X, C, &x, "conv1d_dw x"); rc < 0) return rc;
if (int rc = rows_view(Y, C, &y, "conv1d_dw y"); rc < 0) return rc;
const int64_t Mx = x.rows, My = y.rows;
if (Mx <= 0 || My <= 0) return RAD_E_SHAPE;
const char* wb = (const char*)Wt->data;
const uint32_t wdt = Wt->dtype;
const Lin wl = Lin::of(Wt);
const char* bb = B ? (const char*)B->data : nullptr;
const uint32_t bdt = B ? B->dtype : 0;
const Lin bl = B ? Lin::of(B) : Lin{};
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
const int64_t total = My * C;
const size_t wg = 256;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t t = g / C, c = g % C;
int64_t t0 = 0, t1 = 0;
if (cu.find(t, cdiv, &t0, &t1) < 0 || t1 > Mx || t1 > My) return;
float acc = 0.0f;
for (int64_t j = 0; j < K; ++j) {
const int64_t tt = t - (K - 1) + j;
if (tt < t0) continue;
acc += rad_load_f32(x.base, x.dtype, tt * x.pitch + c) * rad_load_f32(wb, wdt, wl.off(c * K + j));
}
if (bb) acc += rad_load_f32(bb, bdt, bl.off(c));
rad_store_f32(y.base, y.dtype, t * y.pitch + c, acc);
});
});
}
int conv2d(const RadArgs* a, RadStream s) {
if (!a || a->n_t < 5 || !a->t[0].data || !a->t[1].data || !a->t[2].data || !a->t[4].data) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *Wt = &a->t[1], *CU = &a->t[2], *Y = &a->t[4];
const RadTensor* V = a->t[3].data ? &a->t[3] : nullptr;
const RadTensor* B = a->n_t > 5 && a->t[5].data ? &a->t[5] : nullptr;
const int64_t W = rad_args_geti_or(a, "width", 0), cin = rad_args_geti_or(a, "cin", 0), cout = rad_args_geti_or(a, "cout", 0);
const int64_t kh = rad_args_geti_or(a, "kh", 0), kw = rad_args_geti_or(a, "kw", 0);
const int64_t st = rad_args_geti_or(a, "stride", 1), pad = rad_args_geti_or(a, "pad", 0);
const int64_t cdiv = rad_args_geti_or(a, "cu_div", 1), vst = rad_args_geti_or(a, "valid_stride", 1);
const bool tlast = rad_args_geti_or(a, "t_last", 0) != 0;
const char* act = rad_args_gets_or(a, "act", "none");
const bool gelu = !std::strcmp(act, "gelu");
if (!gelu && std::strcmp(act, "none")) return RAD_E_UNSUPPORTED;
if (W <= 0 || cin <= 0 || cout <= 0 || kh <= 0 || kw <= 0 || st <= 0 || pad < 0 || cdiv <= 0 || vst <= 0)
return RAD_E_SHAPE;
const int64_t Wo = (W + 2 * pad - kw) / st + 1;
if (Wo <= 0 || rad_tensor_numel(Wt) < cout * cin * kh * kw) return RAD_E_SHAPE;
if (B && rad_tensor_numel(B) < cout) return RAD_E_SHAPE;
const Cu cu = cu_of(CU);
if (cu.S <= 0) return RAD_E_SHAPE;
const int64_t rin = W * cin, rout = Wo * cout;
Rows x, y;
if (int rc = rows_view(X, rin, &x, "conv2d x"); rc < 0) return rc;
if (int rc = rows_view(Y, rout, &y, "conv2d y"); rc < 0) return rc;
const int64_t Min = x.rows, Mout = y.rows;
if (Min <= 0 || Mout <= 0) return RAD_E_SHAPE;
const char* wb = (const char*)Wt->data;
const uint32_t wdt = Wt->dtype;
const Lin wl = Lin::of(Wt);
const char* bb = B ? (const char*)B->data : nullptr;
const uint32_t bdt = B ? B->dtype : 0;
const Lin bl = B ? Lin::of(B) : Lin{};
const char* vb = V ? (const char*)V->data : nullptr;
const uint32_t vdt = V ? V->dtype : 0;
const Lin vl = V ? Lin::of(V) : Lin{};
const int64_t vn = V ? rad_tensor_numel(V) : 0;
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
const int64_t total = Mout * rout;
const size_t wg = 256;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t orow = g / rout, wo = (g % rout) / cout, co = g % cout;
/* the segment whose output rows [t0 / st, t0 / st + To) hold orow */
int64_t t0 = -1, T = 0, To = 0;
for (int64_t sgi = 0; sgi < cu.S; ++sgi) {
const int64_t c0 = cu.at(sgi), c1 = cu.at(sgi + 1);
if (c0 % cdiv || c1 % cdiv || (c0 / cdiv) % st || c1 < c0) continue;
const int64_t s0 = c0 / cdiv, sT = c1 / cdiv - s0;
if (sT <= 0) continue;
const int64_t sTo = (sT + 2 * pad - kh) / st + 1, o0 = s0 / st;
if (sTo <= 0) continue;
if (orow >= o0 && orow < o0 + sTo) { t0 = s0; T = sT; To = sTo; break; }
}
if (t0 < 0 || t0 + T > Min) return;
const int64_t to = orow - t0 / st;
(void)To;
float acc = 0.0f;
for (int64_t i = 0; i < kh; ++i) {
const int64_t ti = to * st - pad + i;
if (ti < 0 || ti >= T) continue;
const int64_t tg = t0 + ti;
if (vb) {
if (tg * vst >= vn) return;
if (ld_index(vdt, vb, vl.off(tg * vst)) == 0) continue;
}
for (int64_t j = 0; j < kw; ++j) {
const int64_t wi = wo * st - pad + j;
if (wi < 0 || wi >= W) continue;
for (int64_t ci = 0; ci < cin; ++ci) {
const int64_t widx = tlast ? ((co * cin + ci) * kw + j) * kh + i : ((co * cin + ci) * kh + i) * kw + j;
acc += rad_load_f32(x.base, x.dtype, tg * x.pitch + wi * cin + ci) * rad_load_f32(wb, wdt, wl.off(widx));
}
}
}
if (bb) acc += rad_load_f32(bb, bdt, bl.off(co));
const int64_t yo = orow * y.pitch + wo * cout + co;
rad_store_f32(y.base, y.dtype, yo, acc);
if (gelu) {
const float v = rad_load_f32(y.base, y.dtype, yo);
rad_store_f32(y.base, y.dtype, yo, 0.5f * v * (1.0f + (float)sycl::erf((double)(v * 0.70710678118654752f))));
}
});
});
}
namespace {
constexpr int kLrMaxD = 256; /* head_dim */
constexpr int kLrMaxL = 64; /* left + 1 */
}
int attn_local_rel(const RadArgs* a, RadStream s) {
if (!rad_args_have(a, 7)) return refuse("attn_local_rel: missing operand");
const RadTensor *Q = &a->t[0], *Kt = &a->t[1], *Vt = &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 = rad_args_geti_or(a, "head_dim", 0), H = rad_args_geti_or(a, "n_head", 0);
const int64_t L = rad_args_geti_or(a, "left", 0), R = rad_args_geti_or(a, "right", 0);
const int64_t cdiv = rad_args_geti_or(a, "cu_div", 1);
const float cap = (float)rad_args_getf_or(a, "softcap", 0.0);
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 > kLrMaxD || L + 1 > kLrMaxL)
return refuse("attn_local_rel: head_dim %lld / left %lld over %d / %d", (long long)D, (long long)L, kLrMaxD,
kLrMaxL - 1);
const int64_t Wd = H * D;
if (rad_tensor_numel(PDS) < D || rad_tensor_numel(RK) < (L + 1) * Wd) return RAD_E_SHAPE;
const Cu cu = cu_of(CU);
if (cu.S <= 0) return RAD_E_SHAPE;
Rows q, k, v, rk, o;
if (int rc = rows_view(Q, Wd, &q, "attn_local_rel q"); rc < 0) return rc;
if (int rc = rows_view(Kt, Wd, &k, "attn_local_rel k"); rc < 0) return rc;
if (int rc = rows_view(Vt, Wd, &v, "attn_local_rel v"); rc < 0) return rc;
if (int rc = rows_view(RK, Wd, &rk, "attn_local_rel rel_k"); rc < 0) return rc;
if (int rc = rows_view(O, Wd, &o, "attn_local_rel o"); rc < 0) return rc;
const int64_t Mmin = std::min(std::min(q.rows, k.rows), std::min(v.rows, o.rows));
if (q.rows <= 0 || o.rows <= 0) return RAD_E_SHAPE;
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));
const char* pb = (const char*)PDS->data;
const uint32_t pdt = PDS->dtype;
const Lin pl = Lin::of(PDS);
const int64_t ps = PDS->rank ? PDS->stride[PDS->rank - 1] : 1;
(void)pl;
sycl::queue* qu = queue_of(s);
if (!qu) return RAD_E_DEVICE;
const int64_t total = q.rows * H;
const size_t wg = 64;
return submit([&] {
syclex::nd_launch(*qu, sycl::nd_range<1>{groups_of(total, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t i = g / H, h = g % H;
int64_t t0 = 0, t1 = 0;
if (cu.find(i, cdiv, &t0, &t1) < 0 || t1 > Mmin) return;
auto softplus = [](float x) {
if (x > 20.0f) return x;
return x > 0.0f ? x + (float)sycl::log1p((double)(float)sycl::exp((double)-x))
: (float)sycl::log1p((double)(float)sycl::exp((double)x));
};
float qf[kLrMaxD], acc[kLrMaxD], lg[kLrMaxL];
for (int64_t d = 0; d < D; ++d) {
const float spd = round_dt(pdt, softplus(rad_load_f32(pb, pdt, d * ps)));
qf[d] = rad_load_f32(q.base, q.dtype, i * q.pitch + h * D + d) * qs * spd;
}
const int64_t j0 = i - L > t0 ? i - L : t0;
float mx = -INFINITY;
for (int64_t j = j0; j <= i; ++j) {
float ac = 0.0f, bd = 0.0f;
for (int64_t d = 0; d < D; ++d) {
ac += qf[d] * (rad_load_f32(k.base, k.dtype, j * k.pitch + h * D + d) * ks);
bd += qf[d] * rad_load_f32(rk.base, rk.dtype, (i - j) * rk.pitch + h * D + d);
}
const float x = cap * (float)sycl::tanh((double)((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] = (float)sycl::exp((double)(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;
for (int64_t d = 0; d < D; ++d) acc[d] += p * rad_load_f32(v.base, v.dtype, j * v.pitch + h * D + d);
}
for (int64_t d = 0; d < D; ++d) rad_store_f32(o.base, o.dtype, i * o.pitch + h * D + d, acc[d]);
});
});
}
} // namespace xe2
+4
View File
@@ -27,6 +27,10 @@ int relu(const RadArgs* a, RadStream s);
int logit_softcap(const RadArgs* a, RadStream s);
int rel_sinusoid(const RadArgs* a, RadStream s);
int pool_rows(const RadArgs* a, RadStream s);
int qk_norm_rope(const RadArgs* a, RadStream s);
int conv1d_dw(const RadArgs* a, RadStream s);
int conv2d(const RadArgs* a, RadStream s);
int attn_local_rel(const RadArgs* a, RadStream s);
int scatter_rows(const RadArgs* a, RadStream s);
int layernorm(const RadArgs* a, RadStream s);
int grid_embed(const RadArgs* a, RadStream s);
+147
View File
@@ -201,6 +201,153 @@ int rope(const RadArgs* a, RadStream st) {
});
}
/* qk_norm_rope: libref's ref_qk_norm_rope. A work-item a (row, head): the head's RMS norm with
* libref's ordered sum and its gain (+ wadd; absent: unit), rounded to the tensor's dtype, then the
* rotation: neox / gptj / imrope / axial from rope's frequency table (measured by rot), blocks and
* gptj_blocks section by section with inv = 1 / pow(theta, j / sec) in double rounded once. With
* v_norm the v heads are normed at unit gain and not turned. For the vision towers (once a picture),
* so the plain form. */
int qk_norm_rope(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 4 || !a->t[0].data) return RAD_E_INVAL;
const RadTensor *QKV = &a->t[0], *POS = rad_arg_in(a, 1), *QW = rad_arg_in(a, 2), *KW = rad_arg_in(a, 3);
const int64_t head_dim = rad_args_geti_or(a, "head_dim", 0);
const int64_t n_head = rad_args_geti_or(a, "n_head", 0);
const int64_t n_head_kv = rad_args_geti_or(a, "n_head_kv", 0);
const float theta = (float)rad_args_getf_or(a, "theta", 10000.0);
const float eps = (float)rad_args_getf_or(a, "eps", 1e-6);
const float rscale = (float)rad_args_getf_or(a, "scale", 1.0);
const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0);
const int v_norm = (int)rad_args_geti_or(a, "v_norm", 0);
static const char* kM[] = { "neox", "gptj", "blocks", "gptj_blocks", "imrope", "axial" };
const char* ms = rad_args_gets_or(a, "mode", "neox");
int mode = -1;
for (int i = 0; i < 6; ++i)
if (!std::strcmp(ms, kM[i])) mode = i;
if (mode < 0) return RAD_E_UNSUPPORTED;
const bool blocks = mode == 2 || mode == 3, adjacent = mode == 3, im = mode == 4, axial = mode == 5;
int64_t rot = rad_args_geti_or(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 && rad_tensor_numel(QW) < head_dim) || (KW && rad_tensor_numel(KW) < head_dim)) return RAD_E_SHAPE;
const bool turn = rot > 0;
if (turn && !POS) return RAD_E_INVAL;
int64_t sec[8] = {}, comp[8] = {};
int n_sec = 0;
if (turn && (im || axial || blocks)) {
n_sec = sections(a, sec);
if (n_sec < (im ? 3 : 1)) return RAD_E_SHAPE;
}
if (turn && blocks) {
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 = rad_args_gets_or(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 ? rad_tensor_numel(QKV) / stride_row : multi ? POS->shape[1] : rad_tensor_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;
if (n_comp > 8) 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;
}
if (M <= 0 || rad_tensor_numel(QKV) < M * stride_row) return RAD_E_SHAPE;
const int64_t rs = row_stride(QKV, stride_row);
const int64_t ds = QKV->rank ? QKV->stride[QKV->rank - 1] : 1;
Freqs f{};
if (turn && !blocks) {
const int fm = im ? kImrope : axial ? kAxial : mode; /* neox 0, gptj 1 */
if (int rc = freqs(fm, rot, rot, theta, sec, (im && !multi) ? 0 : (im || axial ? n_sec : 0), &f); rc < 0)
return rc;
}
const bool gptj_pairs = mode == 1;
const int64_t pcs = multi ? POS->stride[0] : 0, pms = multi ? POS->stride[1] : 0;
const Lin pl = POS ? Lin::of(POS) : Lin{};
char* base = (char*)QKV->data;
const uint32_t dt = QKV->dtype;
const char* pbase = POS ? (const char*)POS->data : nullptr;
const uint32_t pdt = POS ? POS->dtype : 0;
const char* qwb = QW ? (const char*)QW->data : nullptr;
const char* kwb = KW ? (const char*)KW->data : nullptr;
const uint32_t qwdt = QW ? QW->dtype : 0, kwdt = KW ? KW->dtype : 0;
const int64_t qws = QW && QW->rank ? QW->stride[QW->rank - 1] : 1, kws = KW && KW->rank ? KW->stride[KW->rank - 1] : 1;
const float div = rscale != 0.0f ? rscale : 1.0f;
const int64_t heads = n_head + n_head_kv + (v_norm ? n_head_kv : 0);
const int64_t total = M * heads;
struct Sec { int64_t sec[8], comp[8]; } sc{};
for (int c = 0; c < 8; ++c) { sc.sec[c] = sec[c]; sc.comp[c] = comp[c]; }
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const size_t wg = 64;
const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg);
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t m = g / heads, h = g % heads;
const bool vh = h >= n_head + n_head_kv; /* a v head under v_norm */
const int64_t hb = m * rs + (vh ? n_head + n_head_kv + (h - n_head - n_head_kv) : h) * head_dim * ds;
const char* wb = vh ? nullptr : (h < n_head ? qwb : kwb);
const uint32_t wdt = h < n_head ? qwdt : kwdt;
const int64_t wst = h < n_head ? qws : kws;
float ss = 0.0f;
for (int64_t d = 0; d < head_dim; ++d) {
const float x = rad_load_f32(base, dt, hb + d * ds);
ss += x * x;
}
const float s = 1.0f / sycl::sqrt(ss / (float)head_dim + eps);
for (int64_t d = 0; d < head_dim; ++d) {
const float x = rad_load_f32(base, dt, hb + d * ds) * s;
rad_store_f32(base, dt, hb + d * ds, wb ? x * (rad_load_f32(wb, wdt, d * wst) + wadd) : x);
}
if (!turn || vh) return;
auto P = [&](int64_t c) {
return multi ? pos_at(pbase, pdt, c * pcs + m * pms, div) : pos_at(pbase, pdt, pl.off(m), div);
};
if (!blocks) {
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 cs = (float)sycl::cos((double)ang), sn = (float)sycl::sin((double)ang);
const int64_t i0 = gptj_pairs ? 2 * i : i, i1 = gptj_pairs ? 2 * i + 1 : i + half;
const float x0 = rad_load_f32(base, dt, hb + i0 * ds), x1 = rad_load_f32(base, dt, hb + i1 * ds);
rad_store_f32(base, dt, hb + i0 * ds, x0 * cs - x1 * sn);
rad_store_f32(base, dt, hb + i1 * ds, x0 * sn + x1 * cs);
}
return;
}
int64_t start = 0;
for (int c = 0; c < n_sec; ++c) {
const float pc = P(sc.comp[c]);
for (int64_t j = 0; j < sc.sec[c]; ++j) {
const float inv = 1.0f / (float)sycl::pow((double)theta, (double)((float)j / (float)sc.sec[c]));
const float ang = pc * inv;
const float cs = (float)sycl::cos((double)ang), sn = (float)sycl::sin((double)ang);
const int64_t i0 = adjacent ? start + 2 * j : start + j;
const int64_t i1 = adjacent ? start + 2 * j + 1 : start + sc.sec[c] + j;
const float x0 = rad_load_f32(base, dt, hb + i0 * ds), x1 = rad_load_f32(base, dt, hb + i1 * ds);
rad_store_f32(base, dt, hb + i0 * ds, x0 * cs - x1 * sn);
rad_store_f32(base, dt, hb + i1 * ds, x0 * sn + x1 * cs);
}
start += 2 * sc.sec[c];
}
});
});
}
/* rope_table: cos and sin rows for each position, cs[pos * rot + i] and cs[pos * rot + half + i],
* as libref's ref_rope_table (a position outside the table is skipped). */
int rope_table(const RadArgs* a, RadStream st) {
+16
View File
@@ -120,6 +120,22 @@ const RadKernelInfo kKernels[] = {
.computes = "the mean of k consecutive rows, f32 in order, rounded, times mult, rounded",
.shape = "rows of n, M a multiple of k", .dtypes = "any float dtype",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::pool_rows },
{ .name = "xe2_qk_norm_rope", .op = "qk_norm_rope", .family = "rope",
.computes = "libref's per-head RMS norm (gain + wadd, or unit), rounded, then rotation: neox, gptj, blocks, gptj_blocks, imrope, axial; v_norm",
.shape = "head_dim, rotary_dim <= 512; a work-item a (row, head)", .dtypes = "any float qkv, gains",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::qk_norm_rope },
{ .name = "xe2_conv1d_dw", .op = "conv1d_dw", .family = "conv",
.computes = "causal depthwise conv over time within cu segments, f32 in tap order, + bias, one rounding",
.shape = "rows of n, k taps", .dtypes = "any float dtype",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::conv1d_dw },
{ .name = "xe2_conv2d", .op = "conv2d", .family = "conv",
.computes = "libref's segmented 2-D conv over (time, width), zero pad, frame mask, bias, gelu on the rounded value",
.shape = "channels innermost, any stride / pad", .dtypes = "any float dtype",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::conv2d },
{ .name = "xe2_attn_local_rel", .op = "attn_local_rel", .family = "attn",
.computes = "Gemma 4 audio local attention with the relative term and tanh softcap, f32, transcendentals in double",
.shape = "head_dim <= 256, left <= 63, right 0", .dtypes = "any float q, k, v, rel_k, o",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::attn_local_rel },
{ .name = "xe2_scatter_rows", .op = "scatter_rows", .family = "gather",
.computes = "row m of v to row idx[m] of x; a negative index skipped, one past x refused",
.shape = "any n and row counts; rows may be padded, the last axis dense",