This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gemm_xmx.cpp
T
claudeandClaude Opus 5.5 e9b2714081 libxe2: chain-parallel gdn_chunk_scan and an fp16 m2xfp4 prefill -- 27B prefill 68 -> 91 tok/s
- gdn_chunk_scan for head_k = 128, chunk 64: a work-group of 256 a (sequence, head, block of 32
  value columns), the chunk's k, q (rows padded), the block's slices of v, S, W, V' and the sA rows
  in local memory (~124 KB); every phase of a chunk is thousands of independent chains -- W[j][e],
  V'[i][e], R[i][j], O[i][e], S[d][e] -- each one work-item's, in libref's order, A staged in local
  memory, the decays e^{g_i - g_j} (libref's double exp) once a call. Still bit-identical (rel_l2 0,
  the 27B's geometry, 1..32 rows and multi-chunk). 233 -> 96 ms a call at 1792 tokens.
- m2xfp4 at M >= 17 now fp16 on XMX: the weight times 2^(127 - e_max(n)), exact in fp16; the
  activation fp16(a * a_scale / R_m); y = acc * R_m * 2^(e_max - 127) in a new scaled fp16 XMX
  kernel (xmx_f16_scaled). fp16's 2^-11 step instead of bf16's 2^-8.

KL on the 27B against an XE2_EXACT=1 reference (every kernel exact, each matched to libref bit for
bit; 250 positions, full vocabulary): mean 0.0041 -> 0.0035, p99 0.027 -> 0.021, top-1 96.8% ->
95.6%, PPL ratio 0.998. The fast set's KL is dominated by the model's own fp8 activation quantiser
re-rounding small upstream differences.

27B prefill of 1792 tokens: 26.3 -> 19.7 s (68 -> 91 tok/s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 05:33:08 +02:00

213 lines
11 KiB
C++

/* 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<float, 1> 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<sycl::access::address_space::global_space,
sycl::access::decorated::no>(ab + m0 * ars);
jm::joint_matrix<sycl::sub_group, bf16, jm::use::a, kTm, kTk, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, bf16, jm::use::b, kTk, kTn, jm::layout::col_major> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, kTm, kTn> 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<sycl::access::address_space::global_space,
sycl::access::decorated::no>(
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<sycl::access::address_space::local_space,
sycl::access::decorated::no>(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
/* ------------------------------------------------------------------ the fp16 form, scaled */
/* The same tiling over fp16 operands with a per-row and per-column factor on the way out:
* y[m][n] = acc * rs[m] * cs[n]. For the m2xfp4 prefill (xe2_gemv_m2.cpp), whose weight is exact
* in fp16 once scaled by its column's largest exponent and whose activation is fp16(a / R): fp16's
* 2^-11 step beside bf16's 2^-8. A is [mpad, K] with mpad a multiple of 8 (padding rows zero);
* rows past M are not written. N a multiple of 64, K of 16. */
namespace xe2 {
int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs,
char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N,
int64_t K, const float* rs, const float* cs) {
using hf = sycl::half;
const size_t gm = (size_t)((mpad + kTm * kSgs - 1) / (kTm * kSgs));
const size_t gn = (size_t)(N / (kTn * kTiles));
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> 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 >= mpad) return;
auto ga = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(ab + m0 * ars);
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, kTm, kTk, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, kTk, kTn, jm::layout::col_major> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, kTm, kTn> 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<sycl::access::address_space::global_space,
sycl::access::decorated::no>(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<sycl::access::address_space::local_space,
sycl::access::decorated::no>(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, m = m0 + r, n = n0 + t * kTn + c;
if (m < M) rad_store_f32(yb, ydt, m * yrs + n * ycs, mine[e] * rs[m] * cs[n]);
}
sycl::group_barrier(sg);
}
});
});
});
}
} // namespace xe2