This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gemm.cpp
T
pascallinxweilerandClaude Opus 5.5 c52f76c33e libxe2: gemm_nt and logits_gemm, correctness first
One work-item an output, f32 over ascending k from zero with no fused multiply-add: libref's
gemm_plain order, so MiniCPM5-2B's projections and its 130560-row head agree with the reference
exactly. Slow on purpose; the XMX kernels of Phase 4 are checked against this and libref. The rows
take float weights only (a dtype constraint); a quantised weight is gemm_nt_q's.

rad-kbench against libref: fixture 276/276, MiniCPM5-2B 164/164 (rel_l2 0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-23 16:09:17 +02:00

79 lines
3.5 KiB
C++

/* xe2_gemm.cpp -- the unquantised GEMMs: gemm_nt (Y = A B^T) and logits_gemm, its lm_head twin.
*
* Correctness first (docs/SYCL_PORTING.md §4.8, Phase 3): one work-item an output, accumulating
* a * b in f32 over ascending k from zero, no fused multiply-add, then one store: libref's
* gemm_plain exactly. It reads each weight row once per output row, so it is slow; the XMX kernels
* of Phase 4 are checked against it and against libref.
*
* A is read through its row and column strides (an activation may be a slice of a wider arena
* buffer); B is the weight, [N, K]. A joined head (codes with a scale plane) and a non-float
* weight are refused: those are gemm_nt_q's.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
namespace xe2 {
namespace {
/* libref's row_stride: fold trailing axes until they span `cols`. */
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];
}
bool is_float(uint32_t dt) {
return dt == RAD_F32 || dt == RAD_F16 || dt == RAD_BF16 || dt == RAD_F8E4M3 || dt == RAD_F8E5M2;
}
int gemm_plain(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, const char* op) {
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) return RAD_E_SHAPE;
if (!is_float(B->dtype) || !is_float(A->dtype))
return refuse("%s: dtype pair %u x %u is not served (a quantised weight is gemm_nt_q's)", op,
A->dtype, B->dtype);
if (rad_tensor_numel(A) < M * K || rad_tensor_numel(B) < N * K) return RAD_E_SHAPE;
const int64_t ars = row_stride(A, K), acs = A->rank >= 2 ? A->stride[A->rank - 1] : 1;
const int64_t brs = row_stride(B, K), bcs = B->rank >= 2 ? B->stride[B->rank - 1] : 1;
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
const char *ab = (const char*)A->data, *bb = (const char*)B->data;
char* yb = (char*)Y->data;
const uint32_t adt = A->dtype, bdt = B->dtype, ydt = Y->dtype;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const size_t wn = 64;
const size_t nn = (size_t)((N + (int64_t)wn - 1) / (int64_t)wn) * wn;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<2>{{(size_t)M, nn}, {1, wn}},
[=](sycl::nd_item<2> it) {
const int64_t m = (int64_t)it.get_global_id(0), n = (int64_t)it.get_global_id(1);
if (n >= N) return;
float acc = 0.0f;
for (int64_t k = 0; k < K; ++k)
acc += rad_load_f32(ab, adt, m * ars + k * acs) * rad_load_f32(bb, bdt, n * brs + k * bcs);
rad_store_f32(yb, ydt, m * yrs + n * ycs, acc);
});
});
}
} // namespace
int gemm_nt(const RadArgs* a, RadStream s) { return gemm_plain(a, s, "N", "K", "gemm_nt"); }
int logits_gemm(const RadArgs* a, RadStream s) {
return gemm_plain(a, s, "n_vocab", "n_embd", "logits_gemm");
}
} // namespace xe2