libxe2: Phase 4 step 2 -- bf16 XMX GEMM for prefill and a one-pass paged attention

xe2_gemm_nt_xmx / xe2_logits_gemm_xmx (M >= 17, bf16): joint_matrix M8 N16 K16 with use::b loaded
column-major straight from the [N, K] weight, eight sub-groups a work-group on one 64-column slice
so a B tile is in cache for all of them; tail rows go to the GEMV and shapes the tiles cannot read
to the exact kernel. xe2_attn_paged: one pass with an online softmax, keys over eight sub-groups,
head_dim over lanes, libref's masks, windows, ragged batches and fp8 descales. Both keep their
exact twins under XE2_EXACT=1.

rad-kbench: fixture 499/499, MiniCPM5-2B 482/482 (worst rel_l2 1.7e-4). KL against the libref
reference over 1914 positions: mean 5.8e-4, p99 3.2e-3, top-1 98.7% -- inside the bound.
Prefill of a 1795-token prompt: 4.57 s, 393 tok/s (was ~17); decode 33.2 tok/s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-23 20:34:02 +02:00
1 parent a65ab54213
commit 96b3e1be83
5 files changed
+381

No files matched your search

+23
View File
@@ -16,6 +16,8 @@ const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj") };
const RadConstraint cFloatDtype[] = { RAD_CIN("dtype", "bf16 f16 fp16 f32 fp32") };
/* 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). */
const RadConstraint cXmxBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CGE("M", 17) };
/* The block-scaled pairs xe2_gemm_q.cpp serves; the zero-point grids (w2a8, w4*_asym) and mxfp4 are
* other rows. */
const RadConstraint cFp8Dtype[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16") };
@@ -92,6 +94,19 @@ const RadKernelInfo kKernels[] = {
.dtypes = "bf16 x and head, f32 logits",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cGemvBf16, .n_constraints = 2,
.launch = xe2::logits_gemm_gemv },
{ .name = "xe2_gemm_nt_xmx", .op = "gemm_nt", .family = "gemm",
.computes = "Y = A B^T on XMX (joint_matrix M8 N16 K16), B read column-major from the "
"[N, K] weight; tail rows by the GEMV. XE2_EXACT=1 runs xe2_gemm_nt_ref instead",
.shape = "M >= 17; N a multiple of 64, K of 16, bf16 rows 16-byte aligned (else the exact "
"kernel)",
.dtypes = "bf16 A and W, bf16 or f32 Y, f32 accumulate",
.domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cXmxBf16, .n_constraints = 2,
.launch = xe2::gemm_nt_xmx },
{ .name = "xe2_logits_gemm_xmx", .op = "logits_gemm", .family = "gemm",
.computes = "logits at prefill sizes, as xe2_gemm_nt_xmx", .shape = "M >= 17",
.dtypes = "bf16 x and head, f32 logits",
.domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cXmxBf16, .n_constraints = 2,
.launch = xe2::logits_gemm_xmx },
{ .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",
@@ -111,6 +126,14 @@ const RadKernelInfo kKernels[] = {
.shape = "any head_dim, kv heads and block size; a negative or out-of-range slot is skipped",
.dtypes = "bf16 k and v, bf16 or E4M3 cache",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::kv_store },
{ .name = "xe2_attn_paged", .op = "attn_paged", .family = "attn",
.computes = "paged attention in one pass with an online softmax: a work-group a (row, head), "
"keys over eight sub-groups, head_dim over lanes (not libref's order). "
"XE2_EXACT=1 runs xe2_attn_paged_ref instead",
.shape = "head_dim a multiple of 16 up to 256 (else the exact kernel); any gqa, causal or "
"not, sliding window, ragged batches",
.dtypes = "bf16 query, bf16 or E4M3 cache with per-(seq, head) descales, bf16 out",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .launch = xe2::attn_paged_fast },
{ .name = "xe2_attn_paged_ref", .op = "attn_paged", .family = "attn",
.computes = "paged attention, one work-group a (row, head), libref's order: serial dots, "
"keys summed in order; the correctness-first form",