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

+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",