Archived
libxe2: gdn_conv_prep's conv and l2 norm typed for bf16 -- 0.46 -> 0.26 s a 1882-token prefill
The conv takes typed loads and stores when its operands are bf16 (the engine's), and the l2 norm over each 128-wide q/k head is a sub-group's: its values loaded 8 a lane, the squares added in d order by lane broadcasts -- libref's chain -- instead of a work-item walking the head twice through the generic accessors. Fixture 90/90 at rel_l2 0. Prefill 336 tok/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
1 parent
5d11a79dca
commit
4675fd17cd
1 file changed
+44
-1
+44
-1
@@ -199,6 +199,9 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
|
||||
if (!qu) return RAD_E_DEVICE;
|
||||
const size_t wg = 128;
|
||||
return submit([&] {
|
||||
const bool allbf = io.x.dt == RAD_BF16 && io.w.dt == RAD_BF16 && io.q.dt == RAD_BF16 && io.k.dt == RAD_BF16 &&
|
||||
io.v.dt == RAD_BF16 && (!has_cs || css.dt == RAD_BF16) && io.xcol == 1 && io.wv.s1 == 1 &&
|
||||
!std::getenv("XE2_GDN_GENERIC");
|
||||
/* 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) */
|
||||
@@ -225,6 +228,19 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
|
||||
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;
|
||||
if (allbf) { /* the engine's bf16 operands, typed */
|
||||
const uint16_t* xp = (const uint16_t*)io.x.p;
|
||||
const uint16_t* wp = (const uint16_t*)io.w.p + c * io.wv.s0;
|
||||
const uint16_t* sp = (const uint16_t*)css.p;
|
||||
for (int64_t j = 0; j < width; ++j) {
|
||||
const int64_t src = t - (width - 1) + j;
|
||||
const float val = src >= 0 ? rad_bf16_to_f32(xp[(bos + src) * io.xrow + c])
|
||||
: (warm && has_cs && slot >= 0 ? rad_bf16_to_f32(sp[at3(cvs, slot, c, src + width - 1)]) : 0.0f);
|
||||
acc += val * rad_bf16_to_f32(wp[j]);
|
||||
}
|
||||
((uint16_t*)out.p)[at3(ov, bos + t, oh, od)] = rad_f32_to_bf16(silu(acc));
|
||||
return;
|
||||
}
|
||||
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)
|
||||
@@ -233,7 +249,34 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
|
||||
}
|
||||
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 */
|
||||
/* phase 2: the l2 norm over each q and k head, reading the rounded conv output: a sub-group
|
||||
* a (token, head) when the head is 128 bf16 contiguous -- its values loaded 8 a lane, the
|
||||
* squares added in d order by lane broadcasts (libref's chain) -- else a work-item a head */
|
||||
const bool l2v = K == 128 && io.q.dt == RAD_BF16 && io.k.dt == RAD_BF16 && qv.s2 == 1 && kv.s2 == 1 &&
|
||||
!std::getenv("XE2_GDN_GENERIC");
|
||||
if (l2v)
|
||||
qu->parallel_for(sycl::nd_range<1>{(size_t)(T * Hg * 2) * kSub, (size_t)kSub},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int lane = (int)sg.get_local_linear_id();
|
||||
const int64_t th = (int64_t)it.get_group(0);
|
||||
const int64_t which = th / (T * Hg), rest = th % (T * Hg);
|
||||
const int64_t t = rest / Hg, h = rest % Hg;
|
||||
uint16_t* zp = (uint16_t*)(which ? io.k.p : io.q.p) + at3(which ? kv : qv, t, h, 0);
|
||||
sycl::vec<uint16_t, 8> u8 = *(const sycl::vec<uint16_t, 8>*)(zp + lane * 8);
|
||||
float u[8];
|
||||
for (int e = 0; e < 8; ++e) u[e] = rad_bf16_to_f32(u8[e]);
|
||||
float ss = 0.0f;
|
||||
for (int l = 0; l < kSub; ++l)
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const float w = sycl::select_from_group(sg, u[e], l);
|
||||
ss += w * w;
|
||||
}
|
||||
const float inv = 1.0f / sycl::sqrt(ss + l2eps);
|
||||
for (int e = 0; e < 8; ++e) u8[e] = rad_f32_to_bf16(u[e] * inv);
|
||||
*(sycl::vec<uint16_t, 8>*)(zp + lane * 8) = u8;
|
||||
});
|
||||
else
|
||||
qu->parallel_for(sycl::nd_range<1>{groups_for(T * Hg * 2, wg) * wg, wg}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t th = (int64_t)it.get_global_id(0);
|
||||
if (th >= T * Hg * 2) return;
|
||||
|
||||
Reference in new issue
Block a user