From 96b3e1be8358c8fe420b98aaa063f5ca8881e7ba Mon Sep 17 00:00:00 2001 From: claude Date: Fri, 23 Oct 2026 20:34:02 +0200 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- docs/SYCL_PROGRESS.md | 17 ++++ libxe2/xe2_attn_fast.cpp | 185 +++++++++++++++++++++++++++++++++++++++ libxe2/xe2_gemm_xmx.cpp | 153 ++++++++++++++++++++++++++++++++ libxe2/xe2_ops.h | 3 + libxe2/xe2_rows.cpp | 23 +++++ 5 files changed, 381 insertions(+) create mode 100644 libxe2/xe2_attn_fast.cpp create mode 100644 libxe2/xe2_gemm_xmx.cpp diff --git a/docs/SYCL_PROGRESS.md b/docs/SYCL_PROGRESS.md index a7387255..ea243731 100644 --- a/docs/SYCL_PROGRESS.md +++ b/docs/SYCL_PROGRESS.md @@ -382,6 +382,23 @@ the rest under 1.5% each. The B65's fast path is as far from libref as the production CPU library is. Greedy against libref: 7/8 prompts identical, the eighth diverging at token 18 on a 0.008-nat near-tie. +## Phase 4, step 2 (2026-10-23): XMX GEMM and one-pass attention + +- `xe2_gemm_nt_xmx` / `xe2_logits_gemm_xmx` (priority 5, `M >= 17`, bf16): joint_matrix M8 N16 K16, + use::b loaded column-major straight from the [N, K] weight (probed exact on the card), eight + sub-groups a work-group sharing one 64-column slice; tail rows by the GEMV, odd shapes to the exact + kernel. +- `xe2_attn_paged` (priority 10): one pass, online softmax, keys over eight sub-groups, head_dim over + lanes; the exact kernel underneath. +- rad-kbench: fixture 499/499, MiniCPM5 482/482 (worst rel_l2 1.7e-4); every new row reached. +- KL against the libref reference (same corpus as D13): normal batching (XMX + fast attention) + mean 5.8e-4, p99 3.2e-3, top-1 98.69%, PPL x1.0001; 16-token chunks (GEMV + fast attention) + 5.8e-4 / 2.9e-3 / 98.59%. Inside the bound. +- **Prefill 1795 tokens: TTFT 4.57 s = 393 tok/s (was ~17).** Decode 33.2 tok/s over 512 tokens, but + 16.2 tok/s right after a 1.8k-token prompt: single-row decode attention runs 16 work-groups (one a + head) on a 160-engine card, and each kv head is read by 8 query heads. Next: split the keys across + work-groups and share a kv head's reads across its query heads. + ## Next steps 1. ~~Milestone B, part 2: `minicpm5-2b-fp8.rad`~~ **Dropped by the user (2026-10-23): "instead of the diff --git a/libxe2/xe2_attn_fast.cpp b/libxe2/xe2_attn_fast.cpp new file mode 100644 index 00000000..4020ef5f --- /dev/null +++ b/libxe2/xe2_attn_fast.cpp @@ -0,0 +1,185 @@ +/* xe2_attn_fast.cpp -- Phase 4's paged attention: one pass over the keys with an online softmax, + * the keys spread over the work-group, for decode and prefill alike. + * + * One work-group a (query row, head), eight 16-lane sub-groups. Sub-group s takes keys s, s + 8, + * s + 16, ... of the row's visible span; its lanes split head_dim (head_dim / 16 elements each), so + * a key's score is a sub-group reduction of q . (k * k_descale), scaled. Each sub-group keeps the + * flash-attention running state -- max m, sum l, and its lanes' slice of the weighted value sum -- + * rescaling by exp(m_old - m_new) as the max grows; at the end the eight states are merged through + * local memory and lane d's slice is divided by the merged sum. + * + * Same mask, window, ragged batches (cu_seqlens), fp8 descales and zero row for no visible key as + * libref's attn_paged and xe2_attn_paged_ref. Not libref's order (it takes the max first and sums + * in key order): held to rad-kbench's attention tolerance and the KL bound (D13). XE2_EXACT=1, or a + * head_dim that is not a multiple of 16 up to 256, sends the launch to xe2_attn_paged_ref. + */ +#include "xe2_common.h" +#include "xe2_ops.h" + +#include + +namespace xe2 { + +namespace { + +constexpr int kSgs = 8; +constexpr size_t kWg = kSgs * kSub; +constexpr int kMaxPer = 16; /* head_dim / 16, up to head_dim 256 */ + +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]; +} + +} // namespace + +int attn_paged_fast(const RadArgs* a, RadStream st) { + if (exact_only()) return attn_paged(a, st); + if (!a || a->n_t < 8) return RAD_E_INVAL; + const RadTensor *Q = &a->t[0], *KV = &a->t[1], *BT = &a->t[2], *SU = &a->t[3], *O = &a->t[7]; + const RadTensor* KD = rad_arg_in(a, 4); + const RadTensor* VD = rad_arg_in(a, 5); + const RadTensor* CU = rad_arg_in(a, 6); + if (!Q->data || !KV->data || !BT->data || !SU->data || !O->data) return RAD_E_INVAL; + const int64_t head_dim = rad_args_geti_or(a, "head_dim", Q->rank >= 3 ? Q->shape[Q->rank - 1] : 0); + if (head_dim <= 0 || head_dim % kSub || head_dim / kSub > kMaxPer) return attn_paged(a, st); + const int64_t gqa = rad_args_geti_or(a, "gqa", 1); + const int64_t block_size = rad_args_geti_or(a, "block_size", KV->rank == 4 ? KV->shape[2] : 0); + const int causal = (int)rad_args_geti_or(a, "causal", 1); + const int64_t window = rad_args_geti_or(a, "window", 0); + if (gqa <= 0 || block_size <= 0) return RAD_E_SHAPE; + const int64_t kv_heads = KV->rank == 4 ? KV->shape[1] : 0; + if (kv_heads <= 0 || KV->rank != 4) return RAD_E_SHAPE; + const int64_t q_heads = gqa * kv_heads; + if (Q->rank >= 3 && Q->shape[Q->rank - 2] != q_heads) return RAD_E_SHAPE; + const int64_t n_seq = rad_tensor_numel(SU); + const int64_t rows = rad_tensor_numel(Q) / (q_heads * head_dim); + if (n_seq <= 0 || rows <= 0) return RAD_E_SHAPE; + if (!CU && rows % n_seq != 0) return RAD_E_SHAPE; + if (CU && rad_tensor_numel(CU) != n_seq + 1) return RAD_E_SHAPE; + const int64_t q_len_all = CU ? 0 : rows / n_seq; + if (rad_tensor_numel(O) < rows * q_heads * head_dim) return RAD_E_SHAPE; + if (KV->shape[2] != block_size || KV->shape[3] < 2 * head_dim) return RAD_E_SHAPE; + if (!rad_tensor_is_contiguous(SU) || (CU && !rad_tensor_is_contiguous(CU)) || + (KD && !rad_tensor_is_contiguous(KD)) || (VD && !rad_tensor_is_contiguous(VD))) + return attn_paged(a, st); + + const int64_t max_blocks = BT->rank >= 2 ? BT->shape[BT->rank - 1] : rad_tensor_numel(BT) / n_seq; + const int64_t bt_rs = BT->rank >= 2 ? BT->stride[BT->rank - 2] : max_blocks; + const int64_t bt_cs = BT->rank >= 2 ? BT->stride[BT->rank - 1] : 1; + const int64_t sb = KV->stride[0], sh = KV->stride[1], so = KV->stride[2], sd = KV->stride[3]; + const int64_t q_ds = Q->rank >= 2 ? Q->stride[Q->rank - 1] : 1; + const int64_t q_hs = Q->rank >= 3 ? Q->stride[Q->rank - 2] : head_dim * q_ds; + const int64_t q_rs = row_stride(Q, q_heads * head_dim); + const int64_t o_ds = O->rank >= 2 ? O->stride[O->rank - 1] : 1; + const int64_t o_hs = O->rank >= 3 ? O->stride[O->rank - 2] : head_dim * o_ds; + const int64_t o_rs = row_stride(O, q_heads * head_dim); + const float scale = (float)rad_args_getf_or(a, "scale", (double)(1.0f / std::sqrt((float)head_dim))); + const int64_t jcap = max_blocks * block_size; + const int per = (int)(head_dim / kSub); + + const char *qb = (const char*)Q->data, *cb = (const char*)KV->data, *btb = (const char*)BT->data; + const char *sub = (const char*)SU->data, *cub = CU ? (const char*)CU->data : nullptr; + const char *kdb = KD ? (const char*)KD->data : nullptr, *vdb = VD ? (const char*)VD->data : nullptr; + char* ob = (char*)O->data; + const uint32_t qdt = Q->dtype, cdt = KV->dtype, btdt = BT->dtype, sudt = SU->dtype; + const uint32_t cudt = CU ? CU->dtype : 0, kddt = KD ? KD->dtype : 0, vddt = VD ? VD->dtype : 0; + const uint32_t odt = O->dtype; + const int64_t total = rows * q_heads; + + sycl::queue* q = queue_of(st); + if (!q) return RAD_E_DEVICE; + return submit([&] { + q->submit([&](sycl::handler& h) { + /* Each sub-group's final state: m, l, and its head_dim-wide accumulator. */ + sycl::local_accessor sm(sycl::range<1>(kSgs), h); + sycl::local_accessor sl(sycl::range<1>(kSgs), h); + sycl::local_accessor sacc(sycl::range<1>((size_t)kSgs * kSub * kMaxPer), h); + h.parallel_for(sycl::nd_range<1>{(size_t)total * kWg, kWg}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const int64_t idx = (int64_t)it.get_group(0); + const sycl::sub_group sg = it.get_sub_group(); + const int s = (int)sg.get_group_linear_id(); + const int lane = (int)sg.get_local_linear_id(); + const int64_t hq = idx % q_heads, tok = idx / q_heads; + int64_t sq = 0, qlen = q_len_all, i = 0; + if (cub) { + while (sq + 1 < n_seq && ld_index(cudt, cub, sq + 1) <= tok) ++sq; + qlen = ld_index(cudt, cub, sq + 1) - ld_index(cudt, cub, sq); + i = tok - ld_index(cudt, cub, sq); + } else { + sq = tok / q_len_all; + i = tok - sq * q_len_all; + } + if (qlen <= 0) return; /* uniform across the group */ + const int64_t kh = hq / gqa; + const int64_t ctx = ld_index(sudt, sub, sq); + const int64_t pos = ctx - qlen + i; + const float kds = kdb ? rad_load_f32(kdb, kddt, sq * kv_heads + kh) : 1.0f; + const float vds = vdb ? rad_load_f32(vdb, vddt, sq * kv_heads + kh) : 1.0f; + const int64_t jend = sycl::min(ctx, jcap); + /* The keys this row sees: [jlo, jhi) before the block table is consulted. */ + int64_t jlo = 0, jhi = causal ? sycl::min(jend, pos + 1) : jend; + if (window > 0) { + jlo = sycl::max((int64_t)0, pos - window + 1); + if (!causal) jhi = sycl::min(jend, pos + window); + } + const int64_t qoff = tok * q_rs + hq * q_hs, ooff = tok * o_rs + hq * o_hs; + float qv[kMaxPer], acc[kMaxPer]; + for (int e = 0; e < kMaxPer; ++e) { + qv[e] = e < per ? rad_load_f32(qb, qdt, qoff + (lane * per + e) * q_ds) : 0.0f; + acc[e] = 0.0f; + } + float m = -INFINITY, l = 0.0f; + for (int64_t j = jlo + s; j < jhi; j += kSgs) { + const int64_t blk = ld_index(btdt, btb, sq * bt_rs + (j / block_size) * bt_cs); + if (blk < 0) continue; /* uniform across the sub-group */ + const int64_t kb = blk * sb + kh * sh + (j % block_size) * so; + float part = 0.0f; + for (int e = 0; e < per; ++e) + part += qv[e] * (rad_load_f32(cb, cdt, kb + (lane * per + e) * sd) * kds); + const float sc = sycl::reduce_over_group(sg, part, sycl::plus()) * scale; + const float mn = sycl::fmax(m, sc); + const float corr = sycl::exp(m - mn), w = sycl::exp(sc - mn); + l = l * corr + w; + for (int e = 0; e < per; ++e) + acc[e] = acc[e] * corr + + w * (rad_load_f32(cb, cdt, kb + (head_dim + lane * per + e) * sd) * vds); + m = mn; + } + if (lane == 0) { + sm[s] = m; + sl[s] = l; + } + for (int e = 0; e < per; ++e) sacc[((size_t)s * kSub + lane) * kMaxPer + e] = acc[e]; + sycl::group_barrier(it.get_group()); + if (s != 0) return; /* sub-group 0 merges and stores */ + float M = -INFINITY; + for (int t = 0; t < kSgs; ++t) M = sycl::fmax(M, sm[t]); + if (M == -INFINITY) { + for (int e = 0; e < per; ++e) rad_store_f32(ob, odt, ooff + (lane * per + e) * o_ds, 0.0f); + return; + } + float L = 0.0f, out[kMaxPer]; + for (int e = 0; e < kMaxPer; ++e) out[e] = 0.0f; + for (int t = 0; t < kSgs; ++t) { + if (sm[t] == -INFINITY) continue; + const float f = sycl::exp(sm[t] - M); + L += sl[t] * f; + for (int e = 0; e < per; ++e) out[e] += sacc[((size_t)t * kSub + lane) * kMaxPer + e] * f; + } + const float inv = L > 0.0f ? 1.0f / L : 0.0f; + for (int e = 0; e < per; ++e) + rad_store_f32(ob, odt, ooff + (lane * per + e) * o_ds, out[e] * inv); + }); + }); + }); +} + +} // namespace xe2 diff --git a/libxe2/xe2_gemm_xmx.cpp b/libxe2/xe2_gemm_xmx.cpp new file mode 100644 index 00000000..94f1357b --- /dev/null +++ b/libxe2/xe2_gemm_xmx.cpp @@ -0,0 +1,153 @@ +/* xe2_gemm_xmx.cpp -- the bf16 GEMM for prefill-sized M on Xe2's XMX engines (joint_matrix, + * M8 x N16 x K16, bf16 in, f32 accumulate; verified exact on the B65 in Phase 0). + * + * The weight is [N, K] row-major, which is B = W^T column-major: use::b is loaded column-major + * straight from it (checked on the card: scripts/sycl's probe pattern, exact). A sub-group owns an + * 8-row tile and 64 columns (four accumulators); the eight sub-groups of a work-group own eight + * different row tiles of the same 64 columns, so a B tile one of them loads is in cache for the + * others and the weight streams from memory once per 64 rows rather than once per 8. Each + * accumulator goes through local memory to the output, so Y may be bf16 or f32 at any row stride. + * + * Rows past the last whole 8-row tile (at most 7) go to the decode GEMV, and a launch whose + * operands the tiles cannot read -- N not a multiple of 64, K not a multiple of 16, unaligned or + * strided rows, a non-bf16 operand -- to the correctness-first kernel. Not libref's summation + * order (XMX accumulates K in blocks of 16): held to rad-kbench's gemm tolerance and the KL bound + * (docs/SYCL_PROGRESS.md D13). XE2_EXACT=1 sends every launch to the exact kernel. + */ +#include "xe2_common.h" +#include "xe2_ops.h" + +namespace xe2 { + +namespace { + +namespace jm = sycl::ext::oneapi::experimental::matrix; +using bf16 = sycl::ext::oneapi::bfloat16; + +constexpr int kTm = 8, kTn = 16, kTk = 16; +constexpr int kTiles = 4; /* accumulators a sub-group: 64 columns */ +constexpr int kSgs = 8; /* sub-groups a work-group: 64 rows */ +constexpr size_t kWg = kSgs * kSub; + +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]; +} + +/* The launch's operands with the first `rows` rows dropped: the tail the GEMV takes. */ +RadArgs tail_args(const RadArgs* a, RadTensor* t, int64_t rows, int64_t ars, int64_t yrs) { + RadArgs b = *a; + for (int i = 0; i < a->n_t && i < 3; ++i) t[i] = a->t[i]; + t[0].data = (char*)a->t[0].data + rows * ars * 2; + t[2].data = (char*)a->t[2].data + rows * yrs * (int64_t)((rad_dtype_bits(a->t[2].dtype) + 7) / 8); + for (int i : { 0, 2 }) { + if (t[i].rank >= 2) { + /* Leading axes collapse to rows; the tail is a [rows', cols] view. */ + const int64_t cols = t[i].shape[t[i].rank - 1]; + const int64_t total = rad_tensor_numel(&a->t[i]) / cols; + const int64_t stride = i == 0 ? ars : yrs; + const int64_t cs = t[i].stride[t[i].rank - 1]; + t[i].rank = 2; + t[i].shape[0] = total - rows; + t[i].shape[1] = cols; + t[i].stride[0] = stride; + t[i].stride[1] = cs; + } + } + b.t = t; + return b; +} + +int xmx(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, + int (*exact)(const RadArgs*, RadStream), int (*skinny)(const RadArgs*, RadStream)) { + if (exact_only()) return exact(a, st); + 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 || rad_tensor_numel(A) < M * K || rad_tensor_numel(B) < N * K) return RAD_E_SHAPE; + const int64_t ars = row_stride(A, K), brs = row_stride(B, K); + const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1; + const bool ok = A->dtype == RAD_BF16 && B->dtype == RAD_BF16 && + (Y->dtype == RAD_BF16 || Y->dtype == RAD_F32) && N % (kTn * kTiles) == 0 && + K % kTk == 0 && ars % 8 == 0 && brs % 8 == 0 && + (A->rank < 2 || A->stride[A->rank - 1] == 1) && + (B->rank < 2 || B->stride[B->rank - 1] == 1) && rad_ptr_aligned(A->data, 16) && + rad_ptr_aligned(B->data, 16); + const int64_t M8 = M / kTm * kTm; + if (!ok || M8 == 0) return exact(a, st); + + const bf16* ab = (const bf16*)A->data; + const bf16* bb = (const bf16*)B->data; + char* yb = (char*)Y->data; + const uint32_t ydt = Y->dtype; + sycl::queue* q = queue_of(st); + if (!q) return RAD_E_DEVICE; + const size_t gm = (size_t)((M8 + kTm * kSgs - 1) / (kTm * kSgs)); + const size_t gn = (size_t)(N / (kTn * kTiles)); + int rc = submit([&] { + q->submit([&](sycl::handler& h) { + sycl::local_accessor stage(sycl::range<1>((size_t)kSgs * kTm * kTn), h); + h.parallel_for(sycl::nd_range<2>{{gm, gn * kWg}, {1, kWg}}, + [=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int s = (int)sg.get_group_linear_id(); + const int64_t m0 = ((int64_t)it.get_group(0) * kSgs + s) * kTm; + const int64_t n0 = (int64_t)it.get_group(1) * kTn * kTiles; + if (m0 >= M8) return; /* uniform across the sub-group */ + auto ga = sycl::address_space_cast(ab + m0 * ars); + jm::joint_matrix ma; + jm::joint_matrix mb; + jm::joint_matrix mc[kTiles]; + for (int t = 0; t < kTiles; ++t) jm::joint_matrix_fill(sg, mc[t], 0.0f); + for (int64_t k = 0; k < K; k += kTk) { + jm::joint_matrix_load(sg, ma, ga + k, (size_t)ars); + for (int t = 0; t < kTiles; ++t) { + auto gb = sycl::address_space_cast( + bb + (n0 + t * kTn) * brs + k); + jm::joint_matrix_load(sg, mb, gb, (size_t)brs); + jm::joint_matrix_mad(sg, mc[t], ma, mb, mc[t]); + } + } + float* mine = &stage[(size_t)s * kTm * kTn]; + auto ls = sycl::address_space_cast(mine); + const int lane = (int)sg.get_local_linear_id(); + for (int t = 0; t < kTiles; ++t) { + jm::joint_matrix_store(sg, mc[t], ls, kTn, jm::layout::row_major); + sycl::group_barrier(sg); + for (int e = lane; e < kTm * kTn; e += kSub) { + const int64_t r = e / kTn, c = e % kTn; + rad_store_f32(yb, ydt, (m0 + r) * yrs + (n0 + t * kTn + c) * ycs, mine[e]); + } + sycl::group_barrier(sg); + } + }); + }); + }); + if (rc < 0 || M8 == M) return rc; + RadTensor t[3]; + const RadArgs b = tail_args(a, t, M8, ars, yrs); + return skinny(&b, st); +} + +} // namespace + +int gemm_nt_xmx(const RadArgs* a, RadStream s) { + return xmx(a, s, "N", "K", gemm_nt, gemm_nt_gemv); +} +int logits_gemm_xmx(const RadArgs* a, RadStream s) { + return xmx(a, s, "n_vocab", "n_embd", logits_gemm, logits_gemm_gemv); +} + +} // namespace xe2 diff --git a/libxe2/xe2_ops.h b/libxe2/xe2_ops.h index d3108101..48b24d46 100644 --- a/libxe2/xe2_ops.h +++ b/libxe2/xe2_ops.h @@ -50,6 +50,9 @@ int gemm_nt_q_zp(const RadArgs* a, RadStream s); bool exact_only(); int gemm_nt_gemv(const RadArgs* a, RadStream s); int logits_gemm_gemv(const RadArgs* a, RadStream s); +int gemm_nt_xmx(const RadArgs* a, RadStream s); +int logits_gemm_xmx(const RadArgs* a, RadStream s); +int attn_paged_fast(const RadArgs* a, RadStream s); int gemm_zp_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, RadLayout* out); int gemm_zp_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, diff --git a/libxe2/xe2_rows.cpp b/libxe2/xe2_rows.cpp index aadfb355..dc4a1a30 100644 --- a/libxe2/xe2_rows.cpp +++ b/libxe2/xe2_rows.cpp @@ -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",