diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index bcbf8013..02053525 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -223,7 +223,83 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) { 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) { + /* the 4-tap form of vec8 a block of 8 tokens a work-item: the channels' taps loaded once and + * the window slid a row a token within a sequence, where vec8 reloaded both every token + * (434 us a 2048-token chunk of Qwen3.8-27B's 10240 channels, against ~150 of traffic). + * Each output is vec8's, tap for tap. */ + if (vec8 && width == 4 && !std::getenv("XE2_GDN_CONV_TOK1")) { + constexpr int W = 4, TB = 8; + const int c8n = (int)(conv_dim / 8); + const size_t wg8 = 64; + const int64_t nb = (T + TB - 1) / TB; + qu->parallel_for(sycl::nd_range<2>{{(size_t)nb, 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 tok0 = (int)it.get_global_id(0) * TB; + const int c = c8 * 8, HK = (int)(io.Hg * io.K), Ki = (int)io.K, Vi = (int)io.V; + 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; + float wt[W][8], b0[8]; +#pragma unroll + for (int e = 0; e < 8; ++e) { + b0[e] = io.has_bias ? io.bias.ld((int64_t)(c + e) * io.bias_s) : 0.0f; +#pragma unroll + for (int j = 0; j < W; ++j) wt[j][e] = rad_bf16_to_f32(wp[(int64_t)(c + e) * io.wv.s0 + j]); + } + float win[W][8]; + int64_t pn = -1, pt = -2; + for (int b = 0; b < TB; ++b) { + const int tok = tok0 + b; + if (tok >= T) break; + 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) { pn = -1; continue; } + 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; + auto row = [&](int src, float* v) { + if (src >= 0) { + const sycl::vec u = *(const sycl::vec*)(xp + (bos + src) * io.xrow + c); +#pragma unroll + for (int e = 0; e < 8; ++e) v[e] = rad_bf16_to_f32(u[e]); + } else { +#pragma unroll + for (int e = 0; e < 8; ++e) + v[e] = warm && has_cs && slot >= 0 ? rad_bf16_to_f32(sp[at3(cvs, slot, c + e, src + W - 1)]) : 0.0f; + } + }; + if (n == pn && t == pt + 1) { +#pragma unroll + for (int j = 0; j < W - 1; ++j) +#pragma unroll + for (int e = 0; e < 8; ++e) win[j][e] = win[j + 1][e]; + row(t, win[W - 1]); + } else { +#pragma unroll + for (int j = 0; j < W; ++j) row(t - (W - 1) + j, win[j]); + } + pn = n; + pt = t; + 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; + sycl::vec o; +#pragma unroll + for (int e = 0; e < 8; ++e) { + float acc = b0[e]; +#pragma unroll + for (int j = 0; j < W; ++j) acc += win[j][e] * wt[j][e]; + float sv = acc * sigmoid_fexp(acc); + if (near_bf16_tie(sv)) sv = silu(acc); + o[e] = rad_f32_to_bf16(sv); + } + *(sycl::vec*)op = o; + } + }); + } else 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) {