/* xe2_gemm_xmx.cpp -- the bf16 GEMM for prefill-sized M on Xe2's XMX engines (joint_matrix, * M8 x N16 x K16, bf16 in, f32 accumulate; verified exact on the B65 in Phase 0). * * The weight is [N, K] row-major, which is B = W^T column-major: use::b is loaded column-major * straight from it (checked on the card: scripts/sycl's probe pattern, exact). A sub-group owns an * 8-row tile and 64 columns (four accumulators); the eight sub-groups of a work-group own eight * different row tiles of the same 64 columns, so a B tile one of them loads is in cache for the * others and the weight streams from memory once per 64 rows rather than once per 8. Each * accumulator goes through local memory to the output, so Y may be bf16 or f32 at any row stride. * * Rows past the last whole 8-row tile (at most 7) go to the decode GEMV, and a launch whose * operands the tiles cannot read -- N not a multiple of 64, K not a multiple of 16, unaligned or * strided rows, a non-bf16 operand -- to the correctness-first kernel. Not libref's summation * order (XMX accumulates K in blocks of 16): held to rad-kbench's gemm tolerance and the KL bound * (docs/SYCL_PROGRESS.md D13). XE2_EXACT=1 sends every launch to the exact kernel. */ #include "xe2_common.h" #include "xe2_ops.h" namespace xe2 { namespace { namespace jm = sycl::ext::oneapi::experimental::matrix; using bf16 = sycl::ext::oneapi::bfloat16; constexpr int kTm = 8, kTn = 16, kTk = 16; constexpr int kTiles = 4; /* accumulators a sub-group: 64 columns */ constexpr int kSgs = 8; /* sub-groups a work-group: 64 rows */ constexpr size_t kWg = kSgs * kSub; int64_t row_stride(const RadTensor* t, int64_t cols) { if (!t || t->rank < 2) return cols; int64_t span = 1; for (int i = (int)t->rank - 1; i >= 0; --i) { span *= t->shape[i]; if (span == cols) return (i > 0) ? t->stride[i - 1] : cols; if (span > cols) break; } return t->stride[t->rank - 2]; } /* The launch's operands with the first `rows` rows dropped: the tail the GEMV takes. */ RadArgs tail_args(const RadArgs* a, RadTensor* t, int64_t rows, int64_t ars, int64_t yrs) { RadArgs b = *a; for (int i = 0; i < a->n_t && i < 3; ++i) t[i] = a->t[i]; t[0].data = (char*)a->t[0].data + rows * ars * 2; t[2].data = (char*)a->t[2].data + rows * yrs * (int64_t)((rad_dtype_bits(a->t[2].dtype) + 7) / 8); for (int i : { 0, 2 }) { if (t[i].rank >= 2) { /* Leading axes collapse to rows; the tail is a [rows', cols] view. */ const int64_t cols = t[i].shape[t[i].rank - 1]; const int64_t total = rad_tensor_numel(&a->t[i]) / cols; const int64_t stride = i == 0 ? ars : yrs; const int64_t cs = t[i].stride[t[i].rank - 1]; t[i].rank = 2; t[i].shape[0] = total - rows; t[i].shape[1] = cols; t[i].stride[0] = stride; t[i].stride[1] = cs; } } b.t = t; return b; } int xmx(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int (*exact)(const RadArgs*, RadStream), int (*skinny)(const RadArgs*, RadStream)) { if (exact_only()) return exact(a, st); if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL; const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[2]; const int64_t K = rad_args_geti_or(a, kkey, B->rank >= 2 ? B->shape[B->rank - 1] : 0); const int64_t N = rad_args_geti_or(a, nkey, B->rank >= 2 ? B->shape[B->rank - 2] : 0); if (K <= 0 || N <= 0) return RAD_E_SHAPE; const int64_t M = rad_tensor_numel(Y) / N; if (M <= 0 || rad_tensor_numel(A) < M * K || rad_tensor_numel(B) < N * K) return RAD_E_SHAPE; const int64_t ars = row_stride(A, K), brs = row_stride(B, K); const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1; const bool ok = A->dtype == RAD_BF16 && B->dtype == RAD_BF16 && (Y->dtype == RAD_BF16 || Y->dtype == RAD_F32) && N % (kTn * kTiles) == 0 && K % kTk == 0 && ars % 8 == 0 && brs % 8 == 0 && (A->rank < 2 || A->stride[A->rank - 1] == 1) && (B->rank < 2 || B->stride[B->rank - 1] == 1) && rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16); const int64_t M8 = M / kTm * kTm; if (!ok || M8 == 0) return exact(a, st); const bf16* ab = (const bf16*)A->data; const bf16* bb = (const bf16*)B->data; char* yb = (char*)Y->data; const uint32_t ydt = Y->dtype; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; const size_t gm = (size_t)((M8 + kTm * kSgs - 1) / (kTm * kSgs)); const size_t gn = (size_t)(N / (kTn * kTiles)); int rc = submit([&] { q->submit([&](sycl::handler& h) { sycl::local_accessor stage(sycl::range<1>((size_t)kSgs * kTm * kTn), h); h.parallel_for(sycl::nd_range<2>{{gm, gn * kWg}, {1, kWg}}, [=](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(); const int64_t m0 = ((int64_t)it.get_group(0) * kSgs + s) * kTm; const int64_t n0 = (int64_t)it.get_group(1) * kTn * kTiles; if (m0 >= M8) return; /* uniform across the sub-group */ auto ga = sycl::address_space_cast(ab + m0 * ars); jm::joint_matrix ma; jm::joint_matrix mb; jm::joint_matrix mc[kTiles]; for (int t = 0; t < kTiles; ++t) jm::joint_matrix_fill(sg, mc[t], 0.0f); for (int64_t k = 0; k < K; k += kTk) { jm::joint_matrix_load(sg, ma, ga + k, (size_t)ars); for (int t = 0; t < kTiles; ++t) { auto gb = sycl::address_space_cast( bb + (n0 + t * kTn) * brs + k); jm::joint_matrix_load(sg, mb, gb, (size_t)brs); jm::joint_matrix_mad(sg, mc[t], ma, mb, mc[t]); } } float* mine = &stage[(size_t)s * kTm * kTn]; auto ls = sycl::address_space_cast(mine); const int lane = (int)sg.get_local_linear_id(); for (int t = 0; t < kTiles; ++t) { jm::joint_matrix_store(sg, mc[t], ls, kTn, jm::layout::row_major); sycl::group_barrier(sg); for (int e = lane; e < kTm * kTn; e += kSub) { const int64_t r = e / kTn, c = e % kTn; rad_store_f32(yb, ydt, (m0 + r) * yrs + (n0 + t * kTn + c) * ycs, mine[e]); } sycl::group_barrier(sg); } }); }); }); if (rc < 0 || M8 == M) return rc; RadTensor t[3]; const RadArgs b = tail_args(a, t, M8, ars, yrs); return skinny(&b, st); } } // namespace int gemm_nt_xmx(const RadArgs* a, RadStream s) { return xmx(a, s, "N", "K", gemm_nt, gemm_nt_gemv); } int logits_gemm_xmx(const RadArgs* a, RadStream s) { return xmx(a, s, "n_vocab", "n_embd", logits_gemm, logits_gemm_gemv); } } // namespace xe2