From 3efdb43a3de83370c6fa69978a1bb658b459ecb8 Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 14:59:05 +0200 Subject: [PATCH] libxe2: gdn_conv_update with typed loads for bf16 operands -- the fused update 272 -> 238 us a verify call The conv's per-element generic accessors (a dtype switch each) cost it ~60 us at 8 tokens; the engine's operands are all bf16, which now take a typed copy of the same loop (libref's double exp in silu kept). Bit-identical: 252/252 live gdn_conv_recurrent_update and 48/48 gdn_conv_prep oracle issues at tolerance 1e-9, fixture 90/90. XE2_GDN_GENERIC=1 keeps the generic forms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_gdn.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 77e02b68..39f358b2 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -305,6 +305,56 @@ int gdn_conv_update(const RadArgs* a, RadStream st) { sycl::queue* qu = queue_of(st); if (!qu) return RAD_E_DEVICE; const size_t wg = 128; + const auto bf = [](const DT& d) { return d.dt == RAD_BF16; }; + if (bf(io.x) && bf(io.w) && bf(css) && bf(io.q) && bf(io.k) && bf(io.v) && xcol == 1 && + (!io.has_bias || bf(io.bias)) && io.wv.s1 == 1 && !std::getenv("XE2_GDN_GENERIC")) { + /* bf16 throughout (the engine's case): the same loop with typed loads and stores -- the + * generic accessors' dtype switch an element kept the loads from issuing together */ + return submit([&] { + 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; + const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos; + if (slen <= 0) return; + const int64_t slot = siv.on ? siv.at(n) : n; + if (slot < 0) return; + const int64_t off = nav.on ? nav.at(n) - 1 : 0; + if (off < 0 || off + width - 1 > state_len || (width > 1 && (width - 2) + slen > state_len)) return; + const uint16_t* xp = (const uint16_t*)xs.p; + uint16_t* sp = (uint16_t*)css.p; + const uint16_t* wp = (const uint16_t*)io.w.p + c * io.wv.s0; + float hist[kHistMax]; + for (int64_t i = 0; i < width - 1; ++i) hist[i] = rad_bf16_to_f32(sp[at3(cvs, slot, c, off + i)]); + float wv[kHistMax + 1]; + for (int64_t j = 0; j < width; ++j) wv[j] = rad_bf16_to_f32(wp[j]); + uint16_t* outp; + V3 ov; + int64_t oh, od; + if (c < io.Hg * io.K) { + outp = (uint16_t*)io.q.p; ov = io.qv; oh = c / io.K; od = c % io.K; + } else if (c < 2 * io.Hg * io.K) { + outp = (uint16_t*)io.k.p; ov = io.kv; oh = (c - io.Hg * io.K) / io.K; od = (c - io.Hg * io.K) % io.K; + } else { + outp = (uint16_t*)io.v.p; ov = io.vv; oh = (c - 2 * io.Hg * io.K) / io.V; od = (c - 2 * io.Hg * io.K) % io.V; + } + const float bs = io.has_bias ? rad_bf16_to_f32(((const uint16_t*)io.bias.p)[c * io.bias_s]) : 0.0f; + for (int64_t t = 0; t < slen; ++t) { + float acc = bs; + 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) * xrow + c]) : hist[src + width - 1]; + acc += val * wv[j]; + } + outp[at3(ov, bos + t, oh, od)] = rad_f32_to_bf16(silu(acc)); + } + if (width > 1) { + for (int64_t i = 0; i + 1 < width - 1; ++i) sp[at3(cvs, slot, c, i)] = rad_f32_to_bf16(hist[i + 1]); + for (int64_t t = 0; t < slen; ++t) sp[at3(cvs, slot, c, t + width - 2)] = xp[(bos + t) * xrow + c]; + } + }); + }); + } return submit([&] { 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);