diff --git a/libxe2/xe2_gemm_xmx.cpp b/libxe2/xe2_gemm_xmx.cpp index 7de58fd5..4c8d950b 100644 --- a/libxe2/xe2_gemm_xmx.cpp +++ b/libxe2/xe2_gemm_xmx.cpp @@ -17,6 +17,8 @@ #include "xe2_common.h" #include "xe2_ops.h" +#include + namespace xe2 { namespace { @@ -160,9 +162,100 @@ int logits_gemm_xmx(const RadArgs* a, RadStream s) { * rows past M are not written. N a multiple of 64, K of 16. */ namespace xe2 { +/* The local-memory tiling: a 128 x 128 work-group tile (4 x 2 sub-groups of 32 x 64, 16 + * accumulators each, the large register file), K in steps of 32 with A [128][32] and B VNNI + * [16 pairs][128] staged in local memory, double buffered; each sub-group's A tiles loaded once + * a K16 step. ~30 TFLOPS on the 27B's prefill shapes (gemm.cpp in scratch; the per-sub-group form + * below ~11-18, the card's DPAS peak ~98). N a multiple of 128, K of 32. */ +int xmx_f16_tiled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs, + char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t N, int64_t K, + const float* rs, const float* cs) { + using hf = sycl::half; + constexpr int BM = 128, BN = 128, BK = 32, SGM = 32, SGN = 64; + const size_t gm = (size_t)((M + BM - 1) / BM), gn = (size_t)(N / BN); + namespace intelex = sycl::ext::intel::experimental; + syclex::properties props{intelex::grf_size<256>}; + /* The accumulators go straight to an f32 [M, N] scratch (a generic store in this kernel's + * epilogue spills its 16 accumulators); a second kernel applies the factors and y's dtype. */ + const int64_t mp = (int64_t)gm * BM; + float* cbuf = workspace(q, (size_t)(mp * N), 6); + if (!cbuf) return RAD_E_DEVICE; + int rc = submit([&] { + q.submit([&](sycl::handler& h) { + sycl::local_accessor As(sycl::range<1>(2 * BM * BK), h); + sycl::local_accessor Bs(sycl::range<1>(2 * (BK / 2) * BN), h); + h.parallel_for(sycl::nd_range<2>{{gm, gn * 128}, {1, 128}}, props, + [=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int s = (int)sg.get_group_linear_id(), t = (int)it.get_local_id(1); + const int sm = s / 2, sn = s % 2; + const int64_t mb0 = (int64_t)it.get_group(0) * BM, nb0 = (int64_t)it.get_group(1) * BN; + jm::joint_matrix ma[4]; + jm::joint_matrix mb; + jm::joint_matrix mc[4][4]; + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) jm::joint_matrix_fill(sg, mc[i][j], 0.0f); + auto stage_in = [&](int buf, int64_t k0) { + for (int r = 0; r < 4; ++r) { + const int idx = t + r * 128; + const int row = idx / 4, kc = (idx % 4) * 8; + const int64_t gm_ = mb0 + row; + sycl::vec v(0u, 0u, 0u, 0u); + if (gm_ < M) v = *(const sycl::vec*)(ab + gm_ * ars + k0 + kc); + *(sycl::vec*)&As[buf * BM * BK + row * BK + kc] = v; + } + for (int r = 0; r < 2; ++r) { + const int idx = t + r * 128; + const int n = idx / 2, kc = (idx % 2) * 16; + const sycl::vec v = *(const sycl::vec*)(bb + (nb0 + n) * brs + k0 + kc); + for (int u = 0; u < 8; ++u) Bs[buf * (BK / 2) * BN + (kc / 2 + u) * BN + n] = v[u]; + } + }; + stage_in(0, 0); + int buf = 0; + for (int64_t k0 = 0; k0 < K; k0 += BK) { + sycl::group_barrier(it.get_group()); + if (k0 + BK < K) stage_in(buf ^ 1, k0 + BK); + for (int kk = 0; kk < BK; kk += 16) { + for (int i = 0; i < 4; ++i) { + auto la = sycl::address_space_cast(&As[buf * BM * BK + (sm * SGM + i * 8) * BK + kk]); + jm::joint_matrix_load(sg, ma[i], la, BK); + } + for (int j = 0; j < 4; ++j) { + auto lb = sycl::address_space_cast( + (hf*)&Bs[buf * (BK / 2) * BN + (kk / 2) * BN] + (sn * SGN + j * 16) * 2); + jm::joint_matrix_load(sg, mb, lb, BN * 2); + for (int i = 0; i < 4; ++i) jm::joint_matrix_mad(sg, mc[i][j], ma[i], mb, mc[i][j]); + } + } + buf ^= 1; + } + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) { + const int64_t m0 = mb0 + sm * SGM + i * 8, n0 = nb0 + sn * SGN + j * 16; + auto gc = sycl::address_space_cast(cbuf + m0 * N + n0); + jm::joint_matrix_store(sg, mc[i][j], gc, (size_t)N, jm::layout::row_major); + } + }); + }); + }); + if (rc < 0) return rc; + return submit([&] { + q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> id) { + const int64_t m = (int64_t)id[0] / N, n = (int64_t)id[0] % N; + rad_store_f32(yb, ydt, m * yrs + n * ycs, cbuf[id[0]] * rs[m] * cs[n]); + }); + }); +} + int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs, char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N, int64_t K, const float* rs, const float* cs) { + if (N % 128 == 0 && K % 32 == 0 && ars % 8 == 0 && brs % 16 == 0 && !std::getenv("XE2_XMX_PLAIN")) + return xmx_f16_tiled(q, ab, ars, bb, brs, yb, ydt, yrs, ycs, M, N, K, rs, cs); using hf = sycl::half; const size_t gm = (size_t)((mpad + kTm * kSgs - 1) / (kTm * kSgs)); const size_t gn = (size_t)(N / (kTn * kTiles));