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
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-23 16:09:17 +02:00
1 parent fca4cbe3b5
commit c52f76c33e
3 files changed
+97

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
/* 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
+3
View File
@@ -16,4 +16,7 @@ int embed_lookup(const RadArgs* a, RadStream s);
int gather_rows(const RadArgs* a, RadStream s);
int cast(const RadArgs* a, RadStream s);
int gemm_nt(const RadArgs* a, RadStream s);
int logits_gemm(const RadArgs* a, RadStream s);
} // namespace xe2
+16
View File
@@ -12,6 +12,8 @@
namespace {
const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj") };
/* The GEMMs here read float weights only; a quantised dtype key is gemm_nt_q's. */
const RadConstraint cFloatDtype[] = { RAD_CIN("dtype", "bf16 f16 fp16 f32 fp32") };
/* Rows by field name (C++20 designated initialisers must follow declaration order, so the compiler
* checks it), unlike a positional table where a shifted field can still compile. */
@@ -68,6 +70,20 @@ const RadKernelInfo kKernels[] = {
.shape = "contiguous operands",
.dtypes = "any float or integer pair",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::cast },
{ .name = "xe2_gemm_nt_ref", .op = "gemm_nt", .family = "gemm",
.computes = "Y = A B^T, one work-item an output, f32 over ascending k (libref's order); the "
"correctness-first form, not the fast one",
.shape = "any M, N, K; strided A",
.dtypes = "float A and W (bf16 in the recipes), float Y",
.domain = RAD_DOMAIN_DEVICE, .constraints = cFloatDtype, .n_constraints = 1,
.launch = xe2::gemm_nt },
{ .name = "xe2_logits_gemm_ref", .op = "logits_gemm", .family = "gemm",
.computes = "logits = x lm_head^T, as xe2_gemm_nt_ref",
.shape = "any M, n_vocab, n_embd",
.dtypes = "float x and head, f32 logits",
.domain = RAD_DOMAIN_DEVICE, .constraints = cFloatDtype, .n_constraints = 1,
.launch = xe2::logits_gemm },
};
const RadPluginInfo kInfo = {