libxe2: gemm_nt_bias on the fast GEMMs, the epilogue in libref's order

gemm_nt_bias (Y = A B^T + bias, then gelu / gelu_tanh and a residual) had only the
correctness-first row: one work-item an output over ascending k. 22 architectures use it
(the ViT, SigLIP2, GLM-vision, SAM and Gemma4-audio blocks, Qwen3-ASR, nomic-bert, the
diffusion VAEs and UNets). The new row forms A B^T on the existing bf16 kernels (the skinny
GEMV at M <= 16, XMX above; each falls back to the exact kernel where its layout is refused)
into an f32 workspace, and gemm_plain runs only its epilogue on it, so the bias, activation,
residual and their roundings are the reference row's. XE2_EXACT=1 keeps the ref row.

rad-kbench: fixture 210/210 (worst rel_l2 1.7e-4), full fixture 2144/2144; the
Qwen3.5-9B vision tower's shapes all agree with libref. N = K = 4608 + gelu on the B65:
M = 1 1,627 -> 81 us, M = 16 8,221 -> 104, M = 128 74,573 -> 253, M = 512 302,924 -> 721.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-08 12:54:54 +01:00
1 parent 6abca01f17
commit d04a27fad3
3 files changed
+50 -2

No files matched your search

+39 -2
View File
@@ -82,8 +82,10 @@ inline float gelu_tanh(float x) {
* on the stored value and the residual added to it, each stored (gemm_epilogue). `joined`: B is a
* head of E4M3 / int8 codes and a scale plane, each element decoded to code * scale in f32 first
* (gemm_scaled_rows). */
/* `pre`: A B^T already formed, f32 [M, N] contiguous (gemm_nt_bias_fast); the kernel then runs only
* the epilogue, in libref's order, on it. */
int gemm_plain(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, const char* op,
bool bias_form = false, bool joined = false) {
bool bias_form = false, bool joined = false, const float* pre = nullptr) {
const int nt = bias_form ? 5 : 3;
if (!a || a->n_t < nt || !a->t[0].data || !a->t[1].data || !a->t[nt - 1].data) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[nt - 1];
@@ -139,7 +141,9 @@ int gemm_plain(const RadArgs* a, RadStream st, const char* nkey, const char* kke
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;
if (joined) {
if (pre) {
acc = pre[m * N + n];
} else if (joined) {
const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale");
if (!cp.p || !sp.p) return; /* the layout hook guarantees a joined head */
for (int64_t k = 0; k < K; ++k)
@@ -168,6 +172,39 @@ int gemm_nt(const RadArgs* a, RadStream s) { return gemm_plain(a, s, "N", "K", "
int gemm_nt_bias(const RadArgs* a, RadStream s) {
return gemm_plain(a, s, "N", "K", "gemm_nt_bias", true);
}
/* Y = A B^T + bias (gelu, residual) with the product on the fast GEMMs: the skinny GEMV at M <= 16,
* XMX above (each falls back to the exact kernel where its layout is refused), into an f32
* workspace; then the epilogue above on it, as xe2_gemm_nt_bias_ref runs it. The product's order
* is the GEMMs' (the gemm tolerance), the bias, activation and residual libref's. bf16 A and W. */
int gemm_nt_bias_fast(const RadArgs* a, RadStream st) {
if (exact_only()) return gemm_nt_bias(a, st);
if (!a || a->n_t < 5 || !a->t[0].data || !a->t[1].data || !a->t[4].data) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[4];
if (A->dtype != RAD_BF16 || B->dtype != RAD_BF16) return gemm_nt_bias(a, st);
const int64_t K = rad_args_geti_or(a, "K", B->rank >= 2 ? B->shape[B->rank - 1] : 0);
const int64_t N = rad_args_geti_or(a, "N", 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;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
float* ws = workspace(*q, (size_t)(M * N), 11);
if (!ws) return RAD_E_DEVICE;
RadTensor t[3] = { *A, *B, {} };
t[2].data = ws;
t[2].dtype = RAD_F32;
t[2].rank = 2;
t[2].shape[0] = M;
t[2].shape[1] = N;
t[2].stride[0] = N;
t[2].stride[1] = 1;
RadArgs sub = *a;
sub.t = t;
sub.n_t = 3;
const int rc = M <= 16 ? gemm_nt_gemv(&sub, st) : gemm_nt_xmx(&sub, st);
if (rc < 0) return rc;
return gemm_plain(a, st, "N", "K", "gemm_nt_bias", true, false, ws);
}
int logits_gemm(const RadArgs* a, RadStream s) {
return gemm_plain(a, s, "n_vocab", "n_embd", "logits_gemm");
}
+1
View File
@@ -81,6 +81,7 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream s);
int givens_dense_quant(const RadArgs* a, RadStream s);
int gemm_nt_bias(const RadArgs* a, RadStream s);
int gemm_nt_bias_fast(const RadArgs* a, RadStream s);
int dflash_conv(const RadArgs* a, RadStream s);
int attn_dense(const RadArgs* a, RadStream s);
int logit_rerank(const RadArgs* a, RadStream s);
+10
View File
@@ -15,6 +15,8 @@ namespace {
const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj mrope imrope axial") };
/* 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") };
/* bf16 operands, any M (xe2_gemm_nt_bias picks its GEMM by M). */
const RadConstraint cBf16[] = { RAD_CIN("dtype", "bf16") };
/* The decode band of the skinny bf16 GEMV (xe2_gemv.cpp). */
const RadConstraint cGemvBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CLE("M", 16) };
/* The prefill band of the bf16 XMX GEMM (xe2_gemm_xmx.cpp). */
@@ -386,6 +388,14 @@ const RadKernelInfo kKernels[] = {
.shape = "any rows and R", .dtypes = "float x; E4M3 or i8 codes with a float scale",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::logit_rerank_head, .layout = xe2::head_layout,
.relayout = xe2::head_relayout, .unrelayout = xe2::head_unrelayout },
{ .name = "xe2_gemm_nt_bias", .op = "gemm_nt_bias", .family = "gemm",
.computes = "Y = A B^T + bias, then gelu / gelu_tanh and a residual: the product on the fast "
"GEMMs (the GEMV at M <= 16, XMX above) into f32, the epilogue in libref's order. "
"XE2_EXACT=1 runs xe2_gemm_nt_bias_ref instead",
.shape = "any M, N, K (each GEMM falls back to the exact kernel where its layout is refused)",
.dtypes = "bf16 A and W, float bias, residual and Y",
.domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cBf16, .n_constraints = 1,
.launch = xe2::gemm_nt_bias_fast },
{ .name = "xe2_gemm_nt_bias_ref", .op = "gemm_nt_bias", .family = "gemm",
.computes = "Y = A B^T + bias, then gelu / gelu_tanh and a residual; libref's order",
.shape = "any M, N, K", .dtypes = "float A and B",