libxe2: 8-row bf16 GEMMs and the E4M3 logits head on XMX

At M <= 8 the bf16 GEMV re-read the activation for every column; it now stages 8 rows in local
memory and runs bf16 DPAS against the weight, K split to fill the card: N=96 164 -> 20 us. The
joined E4M3 head decodes its codes by bits into a packed fp16 tile and runs fp16 DPAS, a scale
block's sum scaled per column in f32: 9.7 -> 3.5 ms at M = 8, and 3.9 -> 3.4 ms at M = 1, so decode
takes it too. Held to the gemm tolerance (rel_l2 1.6e-3, as before).

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-24 12:01:34 +02:00
1 parent 99fd4ea59a
commit ea25b98e23
2 files changed
+173 -3

No files matched your search

+105 -3
View File
@@ -15,6 +15,7 @@
#include "rad_planes.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
namespace xe2 {
@@ -260,6 +261,97 @@ int head_gemv(sycl::queue& q, const sycl::half* a16, const float* rr, const char
});
}
/* M <= 8 on XMX (decode too: 3.4 ms against the SIMT form's 3.9 at M = 1): a sub-group 16 vocab columns, a lane one column's 64 codes a step decoded
* by the bits above into its slice of a packed fp16 B tile in local memory (the pairs (0, 2) and
* (1, 3) of each word are the activation's pair order already), four fp16 DPAS K16 against the
* activation; a scale block's sum is taken out of the accumulator and scaled in f32 per column,
* so the products are summed in f32 rather than half2. Needs the scale block a multiple of 64
* columns (or one block); else the SIMT form. */
int head_dpas(sycl::queue& q, const sycl::half* a16, const float* rr, const char* bb, char* yb, uint32_t ydt,
int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm) {
namespace jm = sycl::ext::oneapi::experimental::matrix;
using hf = sycl::half;
constexpr int kSgs = 16;
const int64_t tiles = (N + 15) / 16;
const size_t groups = (size_t)((tiles + kSgs - 1) / kSgs);
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>((size_t)kSgs * 32 * 16), h);
sycl::local_accessor<float, 1> Cs(sycl::range<1>((size_t)kSgs * 8 * 16), h);
h.parallel_for(sycl::nd_range<1>{groups * kSgs * kSub, (size_t)kSgs * kSub},
[=](sycl::nd_item<1> 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(), lane = (int)sg.get_local_linear_id();
const int64_t n0 = ((int64_t)it.get_group(0) * kSgs + s) * 16;
if (n0 >= N) return; /* uniform */
const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale");
if (!cp.p || !sp.p) return;
const int64_t n = sycl::min(n0 + lane, N - 1);
const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes;
if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 64 == 0) || cp.row_bytes % 16 ||
((uintptr_t)cp.p & 15)) {
/* not this form's head (the layout is only known here): a lane its column in
* f32, a16's permutation undone per element */
float accg[8];
for (int m = 0; m < 8; ++m) accg[m] = 0.0f;
for (int64_t k = 0; k < K; ++k) {
const float w = rad_load_f32(crow, cp.dt, k) * sp.cover(n, k);
const int64_t pos = (k & ~(int64_t)3) + ((k & 3) == 1 ? 2 : (k & 3) == 2 ? 1 : (k & 3));
for (int m = 0; m < 8; ++m) accg[m] += (float)a16[m * K + pos] * w;
}
if (n0 + lane < N)
for (int m = 0; m < mm; ++m)
rad_store_f32(yb, ydt, m * yrs + n * ycs, accg[m] * rr[m] * (1.0f / 256.0f));
return;
}
const int64_t blk = sp.bc > 0 ? sp.bc : K;
uint32_t* bt = &Bs[(size_t)s * 32 * 16];
float* ct = &Cs[(size_t)s * 8 * 16];
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(ct);
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, 8, 16, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, 16, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc;
float acc[8];
for (int m = 0; m < 8; ++m) acc[m] = 0.0f;
for (int64_t b0 = 0; b0 < K; b0 += blk) {
jm::joint_matrix_fill(sg, mc, 0.0f);
for (int64_t k0 = b0; k0 < b0 + blk; k0 += 64) {
uint32_t c[16];
for (int v = 0; v < 4; ++v) {
const sycl::vec<uint32_t, 4> c4 = *(const sycl::vec<uint32_t, 4>*)(crow + k0 + 16 * v);
for (int u = 0; u < 4; ++u) c[4 * v + u] = c4[u];
}
for (int j = 0; j < 16; ++j) {
const uint32_t x = c[j] & 0x00FF00FFu, y = (c[j] >> 8) & 0x00FF00FFu;
bt[(2 * j) * 16 + lane] = ((x & 0x007F007Fu) << 7) | ((x & 0x00800080u) << 8);
bt[(2 * j + 1) * 16 + lane] = ((y & 0x007F007Fu) << 7) | ((y & 0x00800080u) << 8);
}
sycl::group_barrier(sg);
for (int kk = 0; kk < 64; kk += 16) {
auto la = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(a16 + k0 + kk);
jm::joint_matrix_load(sg, ma, la, (size_t)K);
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>((hf*)&bt[(kk / 2) * 16]);
jm::joint_matrix_load(sg, mb, lb, 32);
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
}
sycl::group_barrier(sg);
}
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
sycl::group_barrier(sg);
const float sc = sp.cover(n, b0);
for (int m = 0; m < 8; ++m) acc[m] += ct[m * 16 + lane] * sc;
sycl::group_barrier(sg);
}
if (n0 + lane < N)
for (int m = 0; m < mm; ++m) rad_store_f32(yb, ydt, m * yrs + n * ycs, acc[m] * rr[m]);
});
});
});
}
int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
if (exact_only()) return logits_gemm_head(a, st);
if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL;
@@ -273,17 +365,26 @@ int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t abytes = (M * K * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(abytes / 4 + M + 64));
/* the XMX form reads 8 activation rows, the ones past M zero */
static const bool simt = std::getenv("XE2_HEAD_SIMT") != nullptr;
const bool dpas = M <= 8 && K % 64 == 0 && !simt;
const int64_t mrows = dpas ? 8 : M;
const int64_t abytes = (mrows * K * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(abytes / 4 + mrows + 64));
if (!ws) return RAD_E_DEVICE;
sycl::half* a16 = (sycl::half*)ws;
float* rr = (float*)((char*)ws + abytes);
const char* ab = (const char*)A->data;
const uint32_t adt = A->dtype;
int rc = submit([&] {
q->parallel_for(sycl::nd_range<1>{(size_t)M * 256, 256}, [=](sycl::nd_item<1> it) {
q->parallel_for(sycl::nd_range<1>{(size_t)mrows * 256, 256}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
const int64_t t = (int64_t)it.get_local_id(0);
if (m >= M) { /* uniform: the XMX form's padding rows */
for (int64_t k = t; k < K; k += 256) a16[m * K + k] = (sycl::half)0.0f;
if (t == 0) rr[m] = 1.0f;
return;
}
float mx = 0.0f;
for (int64_t k = t; k < K; k += 256) mx = sycl::fmax(mx, sycl::fabs(rad_load_f32(ab, adt, m * ars + k * acs)));
mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum<float>());
@@ -301,6 +402,7 @@ int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
const char* bb = (const char*)B->data;
char* yb = (char*)Y->data;
const int mm = (int)M;
if (dpas) return head_dpas(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
if (mm == 1) return head_gemv<1>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
if (mm <= 8) return head_gemv<8>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
return head_gemv<16>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
+68
View File
@@ -169,6 +169,71 @@ int launch_split(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* y
});
}
/* 2 <= M <= 8 (a speculative verify) on XMX: the activation staged in local memory as 8 rows
* (the missing ones zero), a sub-group 16 columns and a K split of the work-group's 16
* sub-groups, bf16 DPAS 8 x 16 x 16 against B read column-major from the weight; the splits'
* partials are added in split order. The SIMT form above re-reads the activation for every
* column, which at 8 rows costs more than the weight. */
int launch_dpas8(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uint32_t ydt, int64_t N,
int64_t K, int64_t ars, int64_t brs, int64_t yrs, int64_t ycs, int mm) {
namespace jm = sycl::ext::oneapi::experimental::matrix;
using bf16 = sycl::ext::oneapi::bfloat16;
const int64_t tiles = N / 16;
int S = 1; /* K splits: enough sub-groups to fill the card, whole 16-element steps each */
while (S < 16 && tiles * S < kFill) S *= 2;
const int ct = 16 / S;
const size_t groups = (size_t)((tiles + ct - 1) / ct);
const int64_t ksteps = K / 16, kper = (ksteps + S - 1) / S;
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<uint16_t, 1> As(sycl::range<1>((size_t)(8 * K)), h);
sycl::local_accessor<float, 1> Cs(sycl::range<1>((size_t)16 * 8 * 16), h);
h.parallel_for(sycl::nd_range<1>{groups * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int g = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
const int t = (int)it.get_local_id(0);
for (int64_t i = t; i < K; i += kWg) { /* 8 bf16 at a time */
const int64_t m = i / (K / 8), c = (i % (K / 8)) * 8;
sycl::vec<uint16_t, 8> v(0);
if (m < mm) v = *(const sycl::vec<uint16_t, 8>*)(ab + m * ars + c);
*(sycl::vec<uint16_t, 8>*)&As[m * K + c] = v;
}
sycl::group_barrier(it.get_group());
const int sp = g % S;
const int64_t tile = (int64_t)it.get_group(0) * ct + g / S;
const bool live = tile < tiles;
const int64_t n0 = (live ? tile : 0) * 16;
jm::joint_matrix<sycl::sub_group, bf16, jm::use::a, 8, 16, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, bf16, jm::use::b, 16, 16, jm::layout::col_major> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc;
jm::joint_matrix_fill(sg, mc, 0.0f);
const int64_t klo = sp * kper * 16, khi = sycl::min(K, klo + kper * 16);
if (live)
for (int64_t k = klo; k < khi; k += 16) {
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>((bf16*)&As[k]);
jm::joint_matrix_load(sg, ma, la, (size_t)K);
auto gb = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>((const bf16*)bb + n0 * brs + k);
jm::joint_matrix_load(sg, mb, gb, (size_t)brs);
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
}
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(&Cs[(size_t)g * 128]);
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
sycl::group_barrier(it.get_group());
if (live && sp == 0)
for (int m = 0; m < mm; ++m) {
float y = 0.0f;
for (int u = 0; u < S; ++u) y += Cs[(size_t)(g + u) * 128 + m * 16 + lane];
rad_store_f32(yb, ydt, m * yrs + (n0 + lane) * ycs, y);
}
});
});
});
}
int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int (*exact)(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;
@@ -200,6 +265,9 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int
if (S > 1) return launch_split<kCols>(*q, ab, bb, yb, ydt, N, K, brs, ycs, S);
return launch<kCols, 1>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, 1);
}
static const bool simt = std::getenv("XE2_GEMV_SIMT") != nullptr;
if (mm <= 8 && N % 16 == 0 && K % 16 == 0 && K <= 6144 && !simt)
return launch_dpas8(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm);
return launch<1, kMaxM>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm);
}