/* xe2_gemv.cpp -- Phase 4's first fast kernel: the skinny bf16 GEMM a decode step is made of * (gemm_nt and logits_gemm at M <= 16), bandwidth-bound by construction. * * One 16-lane sub-group an output column: each lane reads 8 bf16 of the weight row at a time (one * 16-byte load, the sub-group 256 contiguous bytes), multiplies them against the same 8 columns of * every activation row, and keeps one partial sum a row; the sub-group then reduces the partials and * lane 0 stores. A weight byte is read once per step whatever M is, so the cost is the weight's * bytes over the card's bandwidth. * * Not libref's order: each lane sums its own strided slice and the slices are reduced as a tree, so * the result differs from the reference's ascending sum in the last bits. It is checked against * libref at rad-kbench's gemm tolerance, the standard libr4d's split-K WMMA GEMMs are held to, and * by the engine oracle at that tolerance. XE2_EXACT=1 sends every launch to the correctness-first * kernel instead (xe2_gemm.cpp), for bisecting a difference. * * Takes: bf16 activation and weight, rows 16-byte aligned with K a multiple of 8 and a unit column * stride; anything else falls back to the correctness-first kernel, which serves every layout. */ #include "xe2_common.h" #include "xe2_ops.h" #include namespace xe2 { bool exact_only() { static const bool on = [] { const char* e = std::getenv("XE2_EXACT"); return e && e[0] == '1'; }(); return on; } namespace { constexpr int kMaxM = 16; constexpr size_t kWg = 256; /* 16 sub-groups, 16 output columns a work-group */ 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]; } inline float bf(uint16_t h) { return rad_bf16_to_f32(h); } /* Whether the fast kernel can read this launch's operands; otherwise the exact one runs. */ bool gemv_ok(const RadTensor* A, const RadTensor* B, const RadTensor* Y, int64_t M, int64_t K, int64_t ars, int64_t brs) { if (A->dtype != RAD_BF16 || B->dtype != RAD_BF16) return false; if (Y->dtype != RAD_BF16 && Y->dtype != RAD_F32) return false; if (M < 1 || M > kMaxM || K % 8) return false; if ((A->rank >= 2 && A->stride[A->rank - 1] != 1) || (B->rank >= 2 && B->stride[B->rank - 1] != 1)) return false; if (ars % 8 || brs % 8) return false; return rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16); } int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int (*exact)(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); if (!gemv_ok(A, B, Y, M, K, ars, brs)) return exact(a, st); const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1; const uint16_t* ab = (const uint16_t*)A->data; const uint16_t* bb = (const uint16_t*)B->data; char* yb = (char*)Y->data; const uint32_t ydt = Y->dtype; const int mm = (int)M; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; const size_t groups = (size_t)((N + 15) / 16); return submit([&] { syclex::nd_launch(*q, sycl::nd_range<1>{groups * kWg, kWg}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { const sycl::sub_group sg = it.get_sub_group(); const int64_t n = (int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id(); if (n >= N) return; /* uniform across the sub-group */ const int lane = (int)sg.get_local_linear_id(); float acc[kMaxM]; for (int r = 0; r < kMaxM; ++r) acc[r] = 0.0f; const uint16_t* wrow = bb + n * brs; for (int64_t k = (int64_t)lane * 8; k < K; k += 8 * kSub) { const sycl::vec w = *(const sycl::vec*)(wrow + k); float wf[8]; for (int j = 0; j < 8; ++j) wf[j] = bf(w[j]); for (int r = 0; r < kMaxM; ++r) { if (r >= mm) break; const sycl::vec x = *(const sycl::vec*)(ab + r * ars + k); float s = 0.0f; for (int j = 0; j < 8; ++j) s += bf(x[j]) * wf[j]; acc[r] += s; } } for (int r = 0; r < kMaxM; ++r) { if (r >= mm) break; const float v = sycl::reduce_over_group(sg, acc[r], sycl::plus()); if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v); } }); }); } } // namespace int gemm_nt_gemv(const RadArgs* a, RadStream s) { return gemv(a, s, "N", "K", gemm_nt); } int logits_gemm_gemv(const RadArgs* a, RadStream s) { return gemv(a, s, "n_vocab", "n_embd", logits_gemm); } } // namespace xe2