Archived
libxe2: gdn_conv_prep's convolution eight channels a work-item
The (token, channel) form spent ~1.8 ms a 2048-token chunk in int64 divides and per-channel branches. A work-item now takes eight channels of one head in 32-bit index math with 16-byte loads and one 16-byte store, each output computed tap for tap as before; the scalar form remains for other layouts (XE2_GDN_SCALAR_CONV=1 forces it). 2234-token prompt 877 -> 960 tok/s, its 96-token greedy continuation identical to the scalar form's; greedy set 8/8 identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
49d7d0b238
commit
e6f4862c7e
1 file changed
+61
@@ -213,6 +213,67 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
|
||||
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, vectorised: a work-item a (token, eight channels) in 32-bit index math, the
|
||||
* eight in one head (K and V multiples of 8) and stored as one 16-byte write. Each output
|
||||
* is the scalar form's, tap for tap (the int64 divides and the per-channel branches of
|
||||
* the form below made it ~1.8 ms a 2048-token chunk, all ALU) */
|
||||
const bool vec8 = allbf && K % 8 == 0 && V % 8 == 0 && conv_dim % 8 == 0 && io.xrow % 8 == 0 &&
|
||||
io.qv.s2 == 1 && io.kv.s2 == 1 && io.vv.s2 == 1 && io.qv.s0 % 8 == 0 && io.qv.s1 % 8 == 0 &&
|
||||
io.kv.s0 % 8 == 0 && io.kv.s1 % 8 == 0 && io.vv.s0 % 8 == 0 && io.vv.s1 % 8 == 0 &&
|
||||
rad_ptr_aligned(io.x.p, 16) && rad_ptr_aligned(io.q.p, 16) && rad_ptr_aligned(io.k.p, 16) &&
|
||||
rad_ptr_aligned(io.v.p, 16) && T * conv_dim < (int64_t)1 << 31 && width <= 8 &&
|
||||
!std::getenv("XE2_GDN_SCALAR_CONV");
|
||||
if (vec8) {
|
||||
const int c8n = (int)(conv_dim / 8);
|
||||
const size_t wg8 = 64;
|
||||
qu->parallel_for(sycl::nd_range<2>{{(size_t)T, groups_for(c8n, wg8) * wg8}, {1, wg8}}, [=](sycl::nd_item<2> it) {
|
||||
const int c8 = (int)it.get_global_id(1);
|
||||
if (c8 >= c8n) return;
|
||||
const int tok = (int)it.get_global_id(0);
|
||||
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 || tok >= bos + slen) return;
|
||||
const int t = (int)(tok - bos);
|
||||
const int64_t slot = siv.on ? siv.at(n) : n;
|
||||
const bool warm = !hiv.on || hiv.at(n) != 0;
|
||||
const int c = c8 * 8, HK = (int)(io.Hg * io.K), Ki = (int)io.K, Vi = (int)io.V;
|
||||
uint16_t* op;
|
||||
if (c < HK) op = (uint16_t*)io.q.p + (int64_t)tok * io.qv.s0 + (c / Ki) * io.qv.s1 + c % Ki;
|
||||
else if (c < 2 * HK) op = (uint16_t*)io.k.p + (int64_t)tok * io.kv.s0 + ((c - HK) / Ki) * io.kv.s1 + (c - HK) % Ki;
|
||||
else op = (uint16_t*)io.v.p + (int64_t)tok * io.vv.s0 + ((c - 2 * HK) / Vi) * io.vv.s1 + (c - 2 * HK) % Vi;
|
||||
float acc[8];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) acc[e] = io.has_bias ? io.bias.ld((int64_t)(c + e) * io.bias_s) : 0.0f;
|
||||
const uint16_t* xp = (const uint16_t*)io.x.p;
|
||||
const uint16_t* wp = (const uint16_t*)io.w.p;
|
||||
const uint16_t* sp = (const uint16_t*)css.p;
|
||||
const int wd = (int)width;
|
||||
for (int j = 0; j < wd; ++j) {
|
||||
const int src = t - (wd - 1) + j;
|
||||
float val[8];
|
||||
if (src >= 0) {
|
||||
const sycl::vec<uint16_t, 8> u = *(const sycl::vec<uint16_t, 8>*)(xp + (bos + src) * io.xrow + c);
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) val[e] = rad_bf16_to_f32(u[e]);
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e)
|
||||
val[e] = warm && has_cs && slot >= 0 ? rad_bf16_to_f32(sp[at3(cvs, slot, c + e, src + wd - 1)]) : 0.0f;
|
||||
}
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) acc[e] += val[e] * rad_bf16_to_f32(wp[(int64_t)(c + e) * io.wv.s0 + j]);
|
||||
}
|
||||
sycl::vec<uint16_t, 8> o;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
float s = acc[e] * sigmoid_fexp(acc[e]);
|
||||
if (near_bf16_tie(s)) s = silu(acc[e]);
|
||||
o[e] = rad_f32_to_bf16(s);
|
||||
}
|
||||
*(sycl::vec<uint16_t, 8>*)op = o;
|
||||
});
|
||||
} else
|
||||
/* 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) */
|
||||
|
||||
Reference in new issue
Block a user