diff --git a/libxe2/xe2_gemv.cpp b/libxe2/xe2_gemv.cpp index 5ad68e2f..0c4ec10c 100644 --- a/libxe2/xe2_gemv.cpp +++ b/libxe2/xe2_gemv.cpp @@ -25,6 +25,11 @@ namespace xe2 { +/* xe2_gemv_esimd.cpp */ +int gemv_esimd_splits(int64_t N, int64_t K); +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); + bool exact_only() { static const bool on = [] { const char* e = std::getenv("XE2_EXACT"); @@ -253,6 +258,17 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int const int mm = (int)M; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + static const bool simt = std::getenv("XE2_GEMV_SIMT") != nullptr; + /* M <= 8 in ESIMD (xe2_gemv_esimd.cpp) where its 2D loads can read the operands: a thread a + * (tile, K split), W^T by transposed loads, the splits summed in order in local memory. */ + if (mm <= 8 && N % 16 == 0 && K % 16 == 0 && ycs == 1 && !simt && rad_ptr_aligned(ab, 64) && + rad_ptr_aligned(bb, 64) && rad_ptr_aligned(yb, 64) && (brs * 2) % 64 == 0 && (ars * 2) % 64 == 0 && + (yrs * (ydt == RAD_F32 ? 4 : 2)) % 64 == 0) { + const int S = gemv_esimd_splits(N, K); + if (S > 0) + return gemv_esimd(*q, mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8)), ab, ars, mm, bb, brs, yb, ydt, yrs, + N, K, S); + } /* Decode (M = 1) takes the lean form with kCols columns a sub-group; M up to 16 one column a * sub-group with a partial sum a row. Sizing the accumulators by the band keeps them in * registers (four columns at sixteen rows spilled and ran slower than one). */ @@ -265,7 +281,6 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int if (S > 1) return launch_split(*q, ab, bb, yb, ydt, N, K, brs, ycs, S); return launch(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, 1); } - static const bool simt = std::getenv("XE2_GEMV_SIMT") != nullptr; if (mm <= 8 && N % 16 == 0 && K % 16 == 0 && K <= 6144 && !simt) return launch_dpas8(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm); return launch<1, kMaxM>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm); diff --git a/libxe2/xe2_gemv_esimd.cpp b/libxe2/xe2_gemv_esimd.cpp new file mode 100644 index 00000000..353c7e15 --- /dev/null +++ b/libxe2/xe2_gemv_esimd.cpp @@ -0,0 +1,95 @@ +/* xe2_gemv_esimd.cpp -- the skinny bf16 GEMM (gemm_nt and logits_gemm at M <= 8) 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. 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 + +namespace xe2 { + +namespace { + +namespace esimd = sycl::ext::intel::esimd; +namespace xmx = sycl::ext::intel::esimd::xmx; + +constexpr int kMaxS = 40; + +template +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) { + 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(); + 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 c = 0.0f; + for (int64_t k = k0; k < k0 + kper; k += 16) { + simd bt = load_2d((const uint32_t*)w, wsw, wsh, wsp, (int)(k / 2), (int)n0); + simd av; + if constexpr (RC == 1) av = block_load(a + k); + else av = load_2d(a, asw, ash, asp, (int)k, 0); + c = xmx::dpas<8, RC, float, float, uint32_t, uint16_t, xmx::dpas_argument_type::bf16, + xmx::dpas_argument_type::bf16>(c, bt, av); + } + slm_block_store((uint32_t)(sp * RC * 16 * 4), c); + barrier(); + for (int r = sp; r < RC && r < mm; r += S) { /* row r's 16 sums, the splits in order */ + simd s = 0.0f; + for (int u = 0; u < S; ++u) s += slm_block_load((uint32_t)((u * RC * 16 + r * 16) * 4)); + if (ydt == RAD_F32) { + block_store((float*)yb + r * yrs + n0, s); + } else { /* bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */ + simd u = s.template bit_cast_view(); + simd h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16; + h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u); + block_store((uint16_t*)yb + r * yrs + n0, simd(h)); + } + } + }); + }); +} + +} // namespace + +/* The split count for N columns and K: the smallest divisor of K / 16 that gives ~8192 threads, at + * most kMaxS; 0 when K / 16 has none worth taking (the caller then keeps its SIMT form). */ +int gemv_esimd_splits(int64_t N, int64_t K) { + const int64_t tiles = N / 16, steps = K / 16; + const int64_t want = (8192 + tiles - 1) / tiles; + 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); + default: return launch<8>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); + } +} + +} // namespace xe2