libxe2: gdn_conv_prep a work-item a (token, channel) and gdn_gated_rmsnorm's row staged -- 0.54 -> 0.46 and 0.38 -> 0.20 s a 1882-token prefill

conv_prep walked each channel through every token (10240 chains of 1882) and the gate cumsum on 48
work-items; every (token, channel) output is independent and the cumsum restarts each chunk, so
they now take a work-item a (token, channel) and a (head, chunk). The gated RMS norm's work-item 0
summed its row from global memory one load at a time; the row is staged in local memory first.
Same expressions and order: fixture 90/90 at rel_l2 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-24 18:51:28 +02:00
1 parent e69626332c
commit ea38ec68a2
1 file changed
+70 -15
+70 -15
View File
@@ -199,19 +199,39 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
if (!qu) return RAD_E_DEVICE;
const size_t wg = 128;
return submit([&] {
/* phase 1: the convolution, straight into q / k / v */
qu->parallel_for(sycl::nd_range<1>{groups_for(N * conv_dim, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t nc = (int64_t)it.get_global_id(0);
if (nc >= N * conv_dim) return;
const int64_t n = nc / conv_dim, c = nc % conv_dim;
/* phase 1: the convolution, straight into q / k / v: a work-item a (token, channel) --
* each output is conv_channel's for that token, its window from x or the conv state
* (a channel walked its tokens in turn here: 10240 chains of 1882 at a prefill) */
qu->parallel_for(sycl::nd_range<1>{groups_for(T * conv_dim, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t tc = (int64_t)it.get_global_id(0);
if (tc >= T * conv_dim) return;
const int64_t tok = tc / conv_dim, c = tc % conv_dim;
int64_t n = 0;
while (n + 1 < N && cuv.at(n + 1) <= tok) ++n;
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
if (slen <= 0) return;
if (slen <= 0 || tok >= bos + slen) return;
const int64_t t = tok - bos;
const int64_t slot = siv.on ? siv.at(n) : n;
const bool warm = !hiv.on || hiv.at(n) != 0;
float hist[kHistMax];
for (int64_t i = 0; i < width - 1; ++i)
hist[i] = warm && has_cs && slot >= 0 ? css.ld(at3(cvs, slot, c, i)) : 0.0f;
conv_channel(io, c, bos, slen, hist);
const int64_t Hg_ = io.Hg, K_ = io.K, V_ = io.V;
DT out;
V3 ov;
int64_t oh, od;
if (c < Hg_ * K_) {
out = io.q; ov = io.qv; oh = c / K_; od = c % K_;
} else if (c < 2 * Hg_ * K_) {
out = io.k; ov = io.kv; oh = (c - Hg_ * K_) / K_; od = (c - Hg_ * K_) % K_;
} else {
out = io.v; ov = io.vv; oh = (c - 2 * Hg_ * K_) / V_; od = (c - 2 * Hg_ * K_) % V_;
}
float acc = io.has_bias ? io.bias.ld(c * io.bias_s) : 0.0f;
for (int64_t j = 0; j < width; ++j) {
const int64_t src = t - (width - 1) + j;
const float val = src >= 0 ? io.x.ld((bos + src) * io.xrow + c * io.xcol)
: (warm && has_cs && slot >= 0 ? css.ld(at3(cvs, slot, c, src + width - 1)) : 0.0f);
acc += val * io.w.ld(at2(io.wv, c, j));
}
out.st(at3(ov, bos + t, oh, od), silu(acc));
});
/* phase 2: the l2 norm over each q and k head, reading the rounded conv output */
qu->parallel_for(sycl::nd_range<1>{groups_for(T * Hg * 2, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
@@ -226,16 +246,20 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
const float inv = 1.0f / sycl::sqrt(ss + l2eps);
for (int64_t d = 0; d < K; ++d) z.st(at3(zv, t, h, d), z.ld(at3(zv, t, h, d)) * inv);
});
/* phase 3: the gate, its per-chunk cumsum, and beta */
qu->parallel_for(sycl::nd_range<1>{groups_for(N * H, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t nh = (int64_t)it.get_global_id(0);
if (nh >= N * H) return;
/* phase 3: the gate, its per-chunk cumsum, and beta: a work-item a (sequence, head,
* chunk), since the sum restarts at every chunk (48 work-items walked 1882 tokens) */
const int64_t cmax = (T + chunk - 1) / chunk;
qu->parallel_for(sycl::nd_range<1>{groups_for(N * H * cmax, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t gid = (int64_t)it.get_global_id(0);
if (gid >= N * H * cmax) return;
const int64_t ci = gid % cmax, nh = gid / cmax;
const int64_t n = nh / H, h = nh % H;
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
if (ci * chunk >= slen) return;
const float alog = alg.p ? alg.ld(h * alg_s) : 0.0f;
const float db = dtb.p ? dtb.ld(h * dtb_s) : 0.0f;
float run = 0.0f;
for (int64_t t = 0; t < slen; ++t) {
for (int64_t t = ci * chunk; t < sycl::min(slen, (ci + 1) * chunk); ++t) {
if (t % chunk == 0) run = 0.0f;
const float av_ = has_ta ? tas.ld(at2(av, bos + t, h)) : xs.ld((bos + t) * xrow + (conv_dim + h) * xcol);
const float bvl = has_ta ? tbs.ld(at2(bgv, bos + t, h)) : xs.ld((bos + t) * xrow + (conv_dim + H + h) * xcol);
@@ -1747,6 +1771,37 @@ int gdn_gated_rmsnorm(const RadArgs* a, RadStream st) {
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
constexpr size_t wg = 128;
if (ch <= 1024) {
/* the row in local memory, read once by all work-items, then work-item 0's ordered sum
* from there (its serial walk over global memory was the kernel's time) */
return submit([&] {
qu->submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> xl(sycl::range<1>((size_t)ch), h);
h.parallel_for(sycl::nd_range<1>{(size_t)rows * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t r = (int64_t)it.get_group(0);
const int64_t t = (int64_t)it.get_local_id(0);
for (int64_t i = t; i < ch; i += (int64_t)wg) xl[i] = rad_load_f32(xr.base, xr.dtype, r * xr.pitch + i);
sycl::group_barrier(it.get_group());
float sc = 0.0f;
if (t == 0) {
float ss = 0.0f;
for (int64_t i = 0; i < ch; ++i) {
const float u = xl[i];
ss += u * u;
}
sc = 1.0f / sycl::sqrt(ss / (float)ch + eps);
}
sc = sycl::group_broadcast(it.get_group(), sc, 0);
const int64_t zb = zslice ? (r / zheads) * zs0 + (r % zheads) * ch * zs1 : r * zpitch;
for (int64_t i = t; i < ch; i += (int64_t)wg) {
const float zz = zd.ld(zb + i * zs);
rad_store_f32(orr.base, orr.dtype, r * orr.pitch + i,
xl[i] * sc * wd.ld(i * ws) * (act == 1 ? sigm(zz) : silu(zz)));
}
});
});
});
}
return submit([&] {
syclex::nd_launch(*qu, sycl::nd_range<1>{(size_t)rows * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t r = (int64_t)it.get_group(0);