/* 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" #include 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 /* ------------------------------------------------------------------ the fp16 form, scaled */ /* The same tiling over fp16 operands with a per-row and per-column factor on the way out: * y[m][n] = acc * rs[m] * cs[n]. For the m2xfp4 prefill (xe2_gemv_m2.cpp), whose weight is exact * in fp16 once scaled by its column's largest exponent and whose activation is fp16(a / R): fp16's * 2^-11 step beside bf16's 2^-8. A is [mpad, K] with mpad a multiple of 8 (padding rows zero); * 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]); }); }); } /* The same tiling, batched and unscaled: batch b multiplies A + b * a_bs by B + b * b_bs into the * f32 C + b * c_bs, [M, N] row-major (ldc = N). N a multiple of 128, K of 32. */ int xmx_f16_batched(sycl::queue& q, const sycl::half* ab, int64_t ars, int64_t a_bs, const sycl::half* bb, int64_t brs, int64_t b_bs, float* cb, int64_t c_bs, int64_t M, int64_t N, int64_t K, int64_t batch) { 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>}; return 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<3>{{(size_t)batch, gm, gn * 128}, {1, 1, 128}}, props, [=](sycl::nd_item<3> 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(2); const int sm = s / 2, sn = s % 2; const int64_t bt = (int64_t)it.get_group(0); const hf* a0 = ab + bt * a_bs; const hf* b0 = bb + bt * b_bs; float* c0 = cb + bt * c_bs; const int64_t mb0 = (int64_t)it.get_group(1) * BM, nb0 = (int64_t)it.get_group(2) * 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*)(a0 + 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*)(b0 + (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(c0 + m0 * N + n0); jm::joint_matrix_store(sg, mc[i][j], gc, (size_t)N, jm::layout::row_major); } }); }); }); } 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)); return 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 >= mpad) return; 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, m = m0 + r, n = n0 + t * kTn + c; if (m < M) rad_store_f32(yb, ydt, m * yrs + n * ycs, mine[e] * rs[m] * cs[n]); } sycl::group_barrier(sg); } }); }); }); } } // namespace xe2