This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gemv_esimd.cpp
T
aneinanderandClaude Opus 5.5 b52a180141 libxe2: split the ESIMD GEMV along K for wide outputs too
gemv_esimd_splits only aimed at ~8192 threads, so an output wide enough to
fill the card at one split (the lm head, N = 248320) ran S = 1: each thread
walked all of K alone and was latency-bound at ~350-530 GB/s. The split now
also keeps K a thread at ~1024 or less.

rad-kbench, B65, cold weights (us):
  logits_gemm n_vocab 248320 n_embd 5120 M=1  4710 -> 4255   M=4 4674 -> 4289
  logits_gemm n_vocab 248320 n_embd 4096 M=1  3652 -> 3418
  gemm_nt N 34816 K 5120 M=1                   621 ->  605
other gemm_nt shapes unchanged. kbench 1989/1989 replay, model checks pass;
Qwen3.8-27B greedy output unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 21:20:16 +01:00

112 lines
5.7 KiB
C++

/* xe2_gemv_esimd.cpp -- the skinny bf16 GEMM (gemm_nt and logits_gemm at M <= 16) in ESIMD.
*
* A thread owns a (16-column tile, K split): per 16 columns of K it takes W^T straight from the
* weight rows by one transposed 2D load (16 rows by 8 dwords, which is the bf16 DPAS's VNNI B
* operand as it arrives) and the activation rows by a 2D load (rows past M read as zeros), and runs
* one DPAS of RC = 1, 2, 4 or 8 rows (RC = 16: two 8-row DPAS on each weight tile). A work-group is one tile's S splits; their partial sums meet in
* local memory and are added in split order, so the result is the same run to run. S is chosen for
* ~8192 threads: narrow outputs need many splits to fill the card (probe bfg.cpp, M = 1 unless said):
*
* ssm_ab N 96 K 5120, M = 4 38.6 us (launch_dpas8) -> 4.7
* N 5120 K 6144 ~206 (the SIMT kernel) -> 110 N 5120 K 10240 ~370 -> 180
* N 12288 K 5120 ~375 -> 214 N 34816 K 5120 ~682 -> 595
*
* Not libref's order (the DPAS sums and the split tree): held to rad-kbench's gemm tolerance, as the
* SIMT forms are. */
#include "xe2_common.h"
#include <sycl/ext/intel/esimd.hpp>
#include <algorithm>
namespace xe2 {
namespace {
namespace esimd = sycl::ext::intel::esimd;
namespace xmx = sycl::ext::intel::esimd::xmx;
constexpr int kMaxS = 40;
/* RC rows a DPAS; RC = 16 is two 8-row DPAS on each weight tile, so 9-16 rows still read the weight
* once (the second band's rows past mm are clamped by the 2D load and never stored). */
template <int RC>
int launch(sycl::queue& q, const uint16_t* a, int64_t ars, int mm, const uint16_t* w, int64_t wrs, char* yb,
uint32_t ydt, int64_t yrs, int64_t N, int64_t K, int S) {
constexpr int RD = RC > 8 ? 8 : RC; /* rows a DPAS */
constexpr int NB = RC > 8 ? 2 : 1; /* DPAS bands */
const int64_t tiles = N / 16;
const int64_t kper = K / S;
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(tiles * S), (size_t)S}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
slm_init<kMaxS * RC * 16 * 4>();
const int sp = (int)it.get_local_id(0);
const int64_t n0 = (int64_t)it.get_group(0) * 16, k0 = sp * kper;
const unsigned wsw = (unsigned)(K * 2 - 1), wsh = (unsigned)(N - 1), wsp = (unsigned)(wrs * 2 - 1);
const unsigned asw = (unsigned)(K * 2 - 1), ash = (unsigned)(mm - 1), asp = (unsigned)(ars * 2 - 1);
simd<float, RD * 16> c[NB];
#pragma unroll
for (int b = 0; b < NB; ++b) c[b] = 0.0f;
for (int64_t k = k0; k < k0 + kper; k += 16) {
simd<uint32_t, 128> bt = load_2d<uint32_t, 8, 16, 1, true>((const uint32_t*)w, wsw, wsh, wsp, (int)(k / 2), (int)n0);
#pragma unroll
for (int b = 0; b < NB; ++b) {
simd<uint16_t, RD * 16> av;
if constexpr (RC == 1) av = block_load<uint16_t, 16>(a + k);
else av = load_2d<uint16_t, 16, RD, 1, false>(a, asw, ash, asp, (int)k, b * 8);
c[b] = xmx::dpas<8, RD, float, float, uint32_t, uint16_t, xmx::dpas_argument_type::bf16,
xmx::dpas_argument_type::bf16>(c[b], bt, av);
}
}
#pragma unroll
for (int b = 0; b < NB; ++b) slm_block_store<float, RD * 16>((uint32_t)((sp * RC + b * 8) * 16 * 4), c[b]);
barrier();
for (int r = sp; r < RC && r < mm; r += S) { /* row r's 16 sums, the splits in order */
simd<float, 16> s = 0.0f;
for (int u = 0; u < S; ++u) s += slm_block_load<float, 16>((uint32_t)((u * RC * 16 + r * 16) * 4));
if (ydt == RAD_F32) {
block_store<float, 16>((float*)yb + r * yrs + n0, s);
} else { /* bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */
simd<uint32_t, 16> u = s.template bit_cast_view<uint32_t>();
simd<uint32_t, 16> h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16;
h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u);
block_store<uint16_t, 16>((uint16_t*)yb + r * yrs + n0, simd<uint16_t, 16>(h));
}
}
});
});
}
} // namespace
/* The split count for N columns and K: the smallest divisor of K / 16 that gives ~8192 threads and
* at most ~1024 of K a thread, at most kMaxS; 0 when K / 16 has none worth taking (the caller then
* keeps its SIMT form). The K bound is for wide outputs that fill the card at one split: a thread
* walking all of a long K alone is latency-bound (probe bfgl.cpp, M = 1, cold weights: logits
* N 248320 K 5120 S 1 7.27 ms, 350 GB/s -> S 5 4.21 ms, 604 GB/s; K 4096 S 1 5.78 -> S 4 3.39 ms). */
int gemv_esimd_splits(int64_t N, int64_t K) {
const int64_t tiles = N / 16, steps = K / 16;
const int64_t want = std::max<int64_t>((8192 + tiles - 1) / tiles, (K + 1023) / 1024);
int best = 0;
for (int s = 1; s <= kMaxS && s <= steps; ++s) {
if (steps % s) continue;
best = s;
if (s >= want) break;
}
return best;
}
int gemv_esimd(sycl::queue& q, int rc, const uint16_t* a, int64_t ars, int mm, const uint16_t* w, int64_t wrs, char* yb,
uint32_t ydt, int64_t yrs, int64_t N, int64_t K, int S) {
switch (rc) {
case 1: return launch<1>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S);
case 2: return launch<2>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S);
case 4: return launch<4>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S);
case 8: return launch<8>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S);
default: return launch<16>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S);
}
}
} // namespace xe2