libxe2: m2xfp4 meta and exponents tiled too, each column's max exponent stored at load -- gate_up 222 -> 205 us

Where N % 16 == 0 and K % 512 == 0 the meta bytes (in the codes blob) and the E8M0 plane (b_scale,
tag "xe2.m2.e8m0.t16") are stored in chunks of a tile's 16 groups, [dword][lane], so the ESIMD
kernel reads four steps of them with one block load each instead of gathers across 16 rows. The
exponent plane's stored form appends each column's largest exponent (int32), computed once at
load, so the prepass no longer scans the plane on every call; unrelayout drops it and untiles the
exponents exactly. Every m2 reader reads the tiled forms.

rad-kbench against libref: 70 checked, 70 passed (worst rel_l2 1.2e-3). M = 8: gate_up 222 -> 205,
down 119 -> 113 us; prefill shapes flat to -5%. Greedy output identical; 512-token decode 33.3 ->
33.9 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-26 02:06:52 +01:00
1 parent 68d510b1f4
commit dad30c6b59
4 files changed
+124 -47

No files matched your search

+61 -18
View File
@@ -12,9 +12,11 @@
*
* `b` reaches the kernel as the weight's codes, then its meta rows, `b_scale` as the E8M0 plane
* [N, K/32]. The layout hooks are libref's m2 arm (ref_layout_gemm_q, ref_planes.h) -- codes rows
* then meta rows, "m2.codes.meta" -- except where N % 16 == 0 and K % 128 == 0: there the codes are
* stored tiled ("xe2.m2.tiled16", xe2_ops.h), the verify GEMM's registers one block load a step
* (246 -> 229 us at gate_up's shape, probe m2d.cpp), and every kernel here reads that form.
* then meta rows, "m2.codes.meta" -- except where N % 16 == 0 and K % 512 == 0: there the codes and
* meta bytes are stored tiled ("xe2.m2.tiled16", xe2_ops.h), the verify GEMM's registers one block
* load a step (246 -> 229 us at gate_up's shape, probe m2d.cpp), and the exponent plane tiled alike
* with each column's largest exponent after it ("xe2.m2.e8m0.t16"), which the kernels otherwise
* scan for on every call. Every kernel here reads those forms.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
@@ -69,9 +71,11 @@ bool m2_enc_planes(const RadEncoding* enc) {
}
constexpr const char* kTag = "m2.codes.meta";
constexpr const char* kTagT = "xe2.m2.tiled16";
constexpr const char* kTagT = "xe2.m2.tiled16"; /* codes and meta tiled (xe2_ops.h) */
constexpr const char* kTagE = "xe2.m2.e8m0.t16"; /* exponents tiled, then each column's max */
/* Copies the codes plane from row-major [N][K/2] to the tiled form (xe2_ops.h), or back. */
/* Moves the planes between their row-major form and the tiled one (xe2_ops.h): codes [N][K/2], a
* 32-group plane (meta or exponents) [N][K/32]. */
void tile_codes(const uint8_t* src, uint8_t* dst, int64_t N, int64_t K, bool to_tiled) {
const int64_t rb = K / 2;
for (int64_t n = 0; n < N; ++n)
@@ -80,6 +84,13 @@ void tile_codes(const uint8_t* src, uint8_t* dst, int64_t N, int64_t K, bool to_
std::memcpy(dst + (to_tiled ? t : r), src + (to_tiled ? r : t), 4);
}
}
void tile_groups(const uint8_t* src, uint8_t* dst, int64_t N, int64_t kg, bool to_tiled) {
for (int64_t n = 0; n < N; ++n)
for (int64_t g = 0; g < kg; ++g) {
const int64_t t = m2e_off(n, g, kg), r = n * kg + g;
dst[to_tiled ? t : r] = src[to_tiled ? r : t];
}
}
} // namespace
@@ -108,7 +119,15 @@ int gemm_m2_layout(const RadParam* p, int n_p, int operand, const RadEncoding* e
if (n != 1 || sel[0] < 0 || sel[0] >= enc->n_planes ||
std::strncmp(enc->plane[sel[0]].role, "scale", RAD_ENC_STR) != 0)
return RAD_E_DTYPE;
return RAD_E_UNSUPPORTED; /* one plane, stored as it is */
if (operand == 6 || !m2_tiled(N, K)) return RAD_E_UNSUPPORTED; /* stored as it is */
if (planes[0].shape[0] != N || planes[0].shape[1] != K / 32) return RAD_E_SHAPE;
out->tag = kTagE;
out->dtype = RAD_E8M0;
out->rank = 1;
out->shape[0] = m2_emax_off(N, K / 32) + 4 * N;
out->align = 0;
out->bytes = out->shape[0];
return RAD_OK;
}
return RAD_E_UNSUPPORTED;
}
@@ -118,15 +137,31 @@ int gemm_m2_relayout(const RadParam* p, int n_p, int operand, const RadEncoding*
RadLayout lay{};
const int st = gemm_m2_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
const bool tiled = lay.tag && std::strcmp(lay.tag, kTagT) == 0;
if (!lay.tag || (!tiled && std::strcmp(lay.tag, kTag) != 0))
if (!lay.tag || (std::strcmp(lay.tag, kTag) && std::strcmp(lay.tag, kTagT) && std::strcmp(lay.tag, kTagE)))
return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes);
if (dst_bytes != lay.bytes) return RAD_E_SHAPE;
uint8_t* d = (uint8_t*)dst;
const int64_t N = planes[0].shape[0];
if (std::strcmp(lay.tag, kTagE) == 0) {
const int64_t kg = planes[0].shape[1];
const uint8_t* e = (const uint8_t*)planes[0].data;
std::memset(d, 0, (size_t)dst_bytes);
tile_groups(e, d, N, kg, true);
int32_t* mx = (int32_t*)(d + m2_emax_off(N, kg));
for (int64_t r = 0; r < N; ++r) {
int32_t m = 0;
for (int64_t g = 0; g < kg; ++g) m = std::max<int32_t>(m, e[r * kg + g]);
mx[r] = m;
}
return RAD_OK;
}
const bool tiled = std::strcmp(lay.tag, kTagT) == 0;
for (int i = 0; i < n; ++i) {
const int64_t b = planes[i].shape[0] * rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
if (i == 0 && tiled)
tile_codes((const uint8_t*)planes[0].data, d, planes[0].shape[0], planes[0].shape[1], true);
if (tiled && i == 0)
tile_codes((const uint8_t*)planes[0].data, d, N, planes[0].shape[1], true);
else if (tiled)
tile_groups((const uint8_t*)planes[i].data, d, N, planes[i].shape[1], true);
else
std::memcpy(d, planes[i].data, (size_t)b);
d += b;
@@ -140,15 +175,22 @@ int gemm_m2_unrelayout(const RadParam* p, int n_p, int operand, const RadEncodin
RadLayout lay{};
const int st = gemm_m2_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
const bool tiled = lay.tag && std::strcmp(lay.tag, kTagT) == 0;
if (!lay.tag || (!tiled && std::strcmp(lay.tag, kTag) != 0))
if (!lay.tag || (std::strcmp(lay.tag, kTag) && std::strcmp(lay.tag, kTagT) && std::strcmp(lay.tag, kTagE)))
return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n);
if (src_bytes != lay.bytes) return RAD_E_SHAPE;
const uint8_t* s = (const uint8_t*)src;
const int64_t N = planes[0].shape[0];
if (std::strcmp(lay.tag, kTagE) == 0) { /* the column maxima are derived: dropped */
tile_groups(s, (uint8_t*)planes[0].data, N, planes[0].shape[1], false);
return RAD_OK;
}
const bool tiled = std::strcmp(lay.tag, kTagT) == 0;
for (int i = 0; i < n; ++i) {
const int64_t b = planes[i].shape[0] * rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
if (i == 0 && tiled)
tile_codes(s, (uint8_t*)planes[0].data, planes[0].shape[0], planes[0].shape[1], false);
if (tiled && i == 0)
tile_codes(s, (uint8_t*)planes[0].data, N, planes[0].shape[1], false);
else if (tiled)
tile_groups(s, (uint8_t*)planes[i].data, N, planes[i].shape[1], false);
else
std::memcpy(planes[i].data, s, (size_t)b);
s += b;
@@ -199,6 +241,7 @@ int gemm_nt_q_m2(const RadArgs* a, RadStream st) {
char* yb = (char*)Y->data;
const uint32_t adt = A->dtype, sdt = AS->dtype, ydt = Y->dtype;
const bool tl = m2_tiled(N, K); /* the stored form gemm_m2_layout asked for */
if (tl && brs != kb) return RAD_E_SHAPE;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t total = M * N;
@@ -211,16 +254,16 @@ int gemm_nt_q_m2(const RadArgs* a, RadStream st) {
const int64_t n = g % N, m = g / N;
const int64_t part = n < e1 ? 0 : (n < e2 ? 1 : 2);
const int64_t a0 = m * ars + part * K * acs, s0 = m * srs + part * akb * scs;
const uint8_t* meta = meta0 + n * kb;
const uint8_t* expe = expe0 + n * brs;
constexpr float e2m1[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };
float acc = 0.0f;
for (int64_t k = 0; k < K; ++k) {
const uint8_t cb = tl ? codes0[m2t_off(n, k >> 1, K)] : codes0[n * (K / 2) + (k >> 1)];
const uint8_t c = (uint8_t)((cb >> (4 * (k & 1))) & 0xF);
const float cv = (c & 8) ? -e2m1[c & 7] : e2m1[c & 7];
const float ev = sycl::ldexp(1.0f, (int)expe[k >> 5] - 127);
const float km = 1.0f + 0.25f * (float)((meta[k >> 5] >> (2 * ((k & 31) >> 3))) & 3);
const int64_t gk = k >> 5;
const float ev = sycl::ldexp(1.0f, (int)expe0[tl ? m2e_off(n, gk, kb) : n * brs + gk] - 127);
const uint8_t mt = meta0[tl ? m2e_off(n, gk, kb) : n * kb + gk];
const float km = 1.0f + 0.25f * (float)((mt >> (2 * ((k & 31) >> 3))) & 3);
const float w = cv * ev * km;
acc += rad_load_f32(ab, adt, a0 + k * acs) * rad_load_f32(sb, sdt, s0 + (k >> 7) * scs) * w;
}
+26 -16
View File
@@ -92,8 +92,8 @@ int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* code
for (int j = 0; j < 4; ++j) cw[j] = *(const uint32_t*)(codes0 + m2t_off(n, g * 16 + j * 4, K));
else
cw = *(const sycl::vec<uint32_t, 4>*)(codes0 + n * (K / 2) + g * 16);
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + g] - 127);
const uint32_t mt = meta0[n * kg + g];
const float ev = sycl::ldexp(1.0f, (int)expe0[tl ? m2e_off(n, g, kg) : n * brs + g] - 127);
const uint32_t mt = meta0[tl ? m2e_off(n, g, kg) : n * kg + g];
H2 w2[16];
H2 kmh[4];
for (int j = 0; j < 4; ++j) {
@@ -181,8 +181,8 @@ int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* ema
for (int w = 0; w < 16; ++w) line[w] = *(const uint32_t*)(codes0 + m2t_off(n, g0 * 16 + w * 4, K));
else
line = *(const sycl::vec<uint32_t, 16>*)(crow + g0 * 16);
const uint32_t e4 = *(const uint32_t*)(expe0 + n * brs + g0);
const uint32_t m4 = *(const uint32_t*)(meta0 + n * kg + g0);
const uint32_t e4 = *(const uint32_t*)(expe0 + (tl ? m2e_off(n, g0, kg) : n * brs + g0));
const uint32_t m4 = *(const uint32_t*)(meta0 + (tl ? m2e_off(n, g0, kg) : n * kg + g0));
for (int gg = 0; gg < kKs; ++gg) {
const int d = (int)((e4 >> (8 * gg)) & 0xFF) - emx;
const float sc = d < -30 ? 0.0f : sycl::ldexp(16384.0f, d);
@@ -233,7 +233,7 @@ template <int MT, int NT>
int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const uint8_t* meta0,
const uint8_t* expe0, int64_t brs, float* pbuf, int64_t N, int64_t K, int64_t e1,
int64_t e2, int64_t mpad, int splits, const float* rr, char* yb, uint32_t ydt,
int64_t yrs, int64_t ycs, int mm, bool tl) {
int64_t yrs, int64_t ycs, int mm, bool tl, const int32_t* emax) {
constexpr int64_t kCols = 128 * NT; /* columns a work-group: 8 sub-groups */
const int64_t ntile = (N + kCols - 1) / kCols;
const size_t groups = (size_t)(ntile * splits);
@@ -262,6 +262,10 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const
const uint8_t* er = expe0 + nn[t] * brs;
int32_t mx = 0;
int64_t g = 0;
if (tl) { /* tiled: the stored plane carries it */
emx[t] = emax[nn[t]];
continue;
}
if (ev16)
for (; g < kg; g += 16) {
const sycl::vec<uint8_t, 16> ev = *(const sycl::vec<uint8_t, 16>*)(er + g);
@@ -282,8 +286,8 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const
line[t][w] = *(const uint32_t*)(codes0 + m2t_off(nn[t], g0 * 16 + w * 4, K));
else
line[t] = *(const sycl::vec<uint32_t, 16>*)(codes0 + nn[t] * (K / 2) + g0 * 16);
e4[t] = *(const uint32_t*)(expe0 + nn[t] * brs + g0);
m4[t] = *(const uint32_t*)(meta0 + nn[t] * kg + g0);
e4[t] = *(const uint32_t*)(expe0 + (tl ? m2e_off(nn[t], g0, kg) : nn[t] * brs + g0));
m4[t] = *(const uint32_t*)(meta0 + (tl ? m2e_off(nn[t], g0, kg) : nn[t] * kg + g0));
}
for (int gg = 0; gg < kKs; ++gg)
for (int hh = 0; hh < 2; ++hh) { /* 16 k: words 2 hh, 2 hh + 1 of the group */
@@ -360,7 +364,8 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
if (!ws || !pbuf) return RAD_E_DEVICE;
half* a16 = (half*)ws;
float* rr = (float*)((char*)ws + abytes);
int32_t* emax = (int32_t*)((char*)ws + abytes + rbytes);
/* tiled, the column maxima are stored after the exponents (gemm_m2_layout) */
int32_t* emax = tl ? (int32_t*)(expe0 + m2_emax_off(N, kg)) : (int32_t*)((char*)ws + abytes + rbytes);
const uint8_t* ab = (const uint8_t*)A->data;
const char* sab = (const char*)AS->data;
const uint32_t sadt = AS->dtype;
@@ -369,7 +374,7 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
* work-groups, 256 columns each) -- a launch of its own cost more than its work */
const int64_t chunks = raw ? (acols + 2047) / 2048 : 1;
const int64_t cspan = raw ? 2048 : acols;
const bool want_emax = es || !(raw && splits == 1);
const bool want_emax = !tl && (es || !(raw && splits == 1));
const int64_t agroups = mpad * chunks, egroups = want_emax ? (N + 255) / 256 : 0;
int rc = submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(agroups + egroups) * 256, 256}, [=](sycl::nd_item<1> it) {
@@ -414,9 +419,9 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
yrs, ycs, (int)M, ldp, tl);
else if (raw)
rc = mt == 1 ? launch_dpas_raw<1, 1>(q, a16, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits,
rr, yb, ydt, yrs, ycs, (int)M, tl)
rr, yb, ydt, yrs, ycs, (int)M, tl, emax)
: launch_dpas_raw<2, 1>(q, a16, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits,
rr, yb, ydt, yrs, ycs, (int)M, tl);
rr, yb, ydt, yrs, ycs, (int)M, tl, emax);
else
rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits, tl)
: launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits, tl);
@@ -454,6 +459,7 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
const int64_t ars = row_stride(A, acols), srs = row_stride(AS, scols), brs = row_stride(BS, kb);
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
const bool ok = blob && A->dtype == RAD_F8E4M3 && B->dtype == RAD_U8 && BS->dtype == RAD_E8M0 &&
(!m2_tiled(N, K) || brs == kb) &&
(A->rank < 2 || A->stride[A->rank - 1] == 1) &&
(AS->rank < 2 || AS->stride[AS->rank - 1] == 1) && ars % 16 == 0 &&
rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16) &&
@@ -469,7 +475,7 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
const bool tl = m2_tiled(N, K);
/* the verify band on XMX, where its line loads fit: K a multiple of 128, aligned rows; M = 1 too
* when the codes are tiled (the ESIMD kernel's form, which the lane-a-group GEMV reads poorly) */
if ((mm >= 2 || tl) && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, 4) &&
if ((mm >= 2 || tl) && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, tl ? 64 : 4) &&
((uintptr_t)meta0 & 3) == 0 && (K / 2) % 64 == 0 && rad_ptr_aligned(B->data, 64))
return m2_dpas(*q, A, AS, codes0, meta0, (const uint8_t*)BS->data, brs, Y, M, N, K, acols, scols,
ars, srs, yrs, ycs, e1, e2);
@@ -547,6 +553,7 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
const int64_t ars = row_stride(A, acols), srs = row_stride(AS, scols), brs = row_stride(BS, kb);
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
const bool ok = blob && A->dtype == RAD_F8E4M3 && B->dtype == RAD_U8 && BS->dtype == RAD_E8M0 &&
(!m2_tiled(N, K) || brs == kb) &&
(A->rank < 2 || A->stride[A->rank - 1] == 1) &&
(AS->rank < 2 || AS->stride[AS->rank - 1] == 1) &&
rad_tensor_numel(A) >= M * acols && rad_tensor_numel(AS) >= M * scols &&
@@ -589,10 +596,14 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + c]) * rad_load_f32(sab, sadt, m * srs + c / 128) * f)
: (half)0.0f;
});
const bool tl = m2_tiled(N, K);
const int32_t* smax = (const int32_t*)(expe0 + m2_emax_off(N, kb)); /* tiled: stored */
q->parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) {
const uint8_t* er = expe0 + (int64_t)i[0] * brs;
int32_t mx = 0;
for (int64_t g = 0; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]);
if (tl) mx = smax[i[0]];
else
for (int64_t g = 0; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]);
emax[i[0]] = mx;
cs[i[0]] = sycl::ldexp(1.0f, mx - 127);
});
@@ -600,7 +611,6 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
* 32 weights stored 64 bytes at a time (a work-item a code byte re-read the group's
* exponent and meta for every weight: ~3 of a 6.7 ms gate_up call) */
const int64_t ng = N * kb;
const bool tl = m2_tiled(N, K);
q->parallel_for(sycl::range<1>((size_t)ng), [=](sycl::id<1> i) {
/* tiled codes: consecutive work-items the 16 columns of a (tile, group), so a word's loads
* are one 64-byte line */
@@ -617,8 +627,8 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
}
else
cb = *(const sycl::vec<uint8_t, 16>*)(codes0 + n * (K / 2) + g * 16);
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + g] - emax[n]);
const uint32_t mt = meta0[n * kb + g];
const float ev = sycl::ldexp(1.0f, (int)expe0[tl ? m2e_off(n, g, kb) : n * brs + g] - emax[n]);
const uint32_t mt = meta0[tl ? m2e_off(n, g, kb) : n * kb + g];
for (int q8 = 0; q8 < 4; ++q8) { /* 8 weights: the subgroup q8 of the group */
const float km = 1.0f + 0.25f * (float)((mt >> (2 * q8)) & 3);
sycl::vec<half, 8> o;
+25 -8
View File
@@ -111,15 +111,32 @@ int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t*
* a step was a third of the kernel's memory messages for an eighth of its bytes); the
* tail of a split that is not 16 groups long a step at a time */
int64_t g0 = g0s;
for (; g0 + 4 * kKs <= g1s; g0 += 4 * kKs) {
simd<uint32_t, 64> E = gather<uint32_t, 64, 4>((const uint32_t*)(expe0 + g0), eoff);
simd<uint32_t, 64> Mt = gather<uint32_t, 64, 4>((const uint32_t*)(meta0 + g0), moff);
_Pragma("unroll") for (int s4 = 0; s4 < 4; ++s4)
step(g0 + kKs * s4, E.template select<16, 1>(s4 * 16), Mt.template select<16, 1>(s4 * 16));
if constexpr (TL) {
/* tiled: a chunk of 16 groups is 256 bytes [step][lane], a step's 64 one block load;
* a split's steps before its first whole chunk and after its last one at a time */
const uint32_t* et = (const uint32_t*)expe0 + tile * (kg / 16) * 64;
const uint32_t* mt = (const uint32_t*)meta0 + tile * (kg / 16) * 64;
for (; g0 < g1s && (g0 & 15); g0 += kKs)
step(g0, block_load<uint32_t, 16>(et + g0 * 4), block_load<uint32_t, 16>(mt + g0 * 4));
for (; g0 + 4 * kKs <= g1s; g0 += 4 * kKs) {
simd<uint32_t, 64> E = block_load<uint32_t, 64>(et + g0 * 4);
simd<uint32_t, 64> Mt = block_load<uint32_t, 64>(mt + g0 * 4);
_Pragma("unroll") for (int s4 = 0; s4 < 4; ++s4)
step(g0 + kKs * s4, E.template select<16, 1>(s4 * 16), Mt.template select<16, 1>(s4 * 16));
}
for (; g0 < g1s; g0 += kKs)
step(g0, block_load<uint32_t, 16>(et + g0 * 4), block_load<uint32_t, 16>(mt + g0 * 4));
} else {
for (; g0 + 4 * kKs <= g1s; g0 += 4 * kKs) {
simd<uint32_t, 64> E = gather<uint32_t, 64, 4>((const uint32_t*)(expe0 + g0), eoff);
simd<uint32_t, 64> Mt = gather<uint32_t, 64, 4>((const uint32_t*)(meta0 + g0), moff);
_Pragma("unroll") for (int s4 = 0; s4 < 4; ++s4)
step(g0 + kKs * s4, E.template select<16, 1>(s4 * 16), Mt.template select<16, 1>(s4 * 16));
}
for (; g0 < g1s; g0 += kKs)
step(g0, gather<uint32_t, 16>((const uint32_t*)(expe0 + g0), eoff),
gather<uint32_t, 16>((const uint32_t*)(meta0 + g0), moff));
}
for (; g0 < g1s; g0 += kKs)
step(g0, gather<uint32_t, 16>((const uint32_t*)(expe0 + g0), eoff),
gather<uint32_t, 16>((const uint32_t*)(meta0 + g0), moff));
const simd_mask<16> ok = (lane + (uint32_t)n0) < (uint32_t)N;
if (splits == 1) {
/* 2^(mx - 127) by bits (mx = 0: the subnormal 2^-127) */
+12 -5
View File
@@ -101,13 +101,20 @@ int gemm_m2_unrelayout(const RadParam* p, int n_p, int operand, const RadEncodin
const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes,
int n);
/* The m2xfp4 codes tiled (xe2_gemm_m2.cpp's layout, tag "xe2.m2.tiled16"), where N % 16 == 0 and
* K % 128 == 0: a 16-column tile's 128-column step is 1 KiB, dword w (codes 8w .. 8w + 7) of its
* columns side by side -- the [word][lane] registers of the verify GEMM, one block load. m2t_off is
* byte b (codes 2b, 2b + 1) of column n's row; the meta rows follow the codes as before. */
inline bool m2_tiled(int64_t N, int64_t K) { return N % 16 == 0 && K % 128 == 0; }
/* The m2xfp4 planes tiled (xe2_gemm_m2.cpp's layout hooks), where N % 16 == 0 and K % 512 == 0.
* Codes ("xe2.m2.tiled16"): a 16-column tile's 128-column step is 1 KiB, dword w (codes 8w .. 8w + 7)
* of its columns side by side -- the [word][lane] registers of the verify GEMM, one block load;
* m2t_off is byte b (codes 2b, 2b + 1) of column n's row. The meta bytes after the codes and the
* E8M0 exponents ("xe2.m2.e8m0.t16") in chunks of a tile's 16 groups, 256 bytes [dword][lane], a
* dword four consecutive groups of a column; m2e_off is group g's byte of column n. The exponent
* plane's stored form then holds each column's largest exponent, int32, at m2_emax_off. */
inline bool m2_tiled(int64_t N, int64_t K) { return N % 16 == 0 && K % 512 == 0; }
inline int64_t m2t_off(int64_t n, int64_t b, int64_t K) {
return ((((n >> 4) * (K / 128) + (b >> 6)) * 16 + ((b >> 2) & 15)) * 64) + (n & 15) * 4 + (b & 3);
}
inline int64_t m2e_off(int64_t n, int64_t g, int64_t kg) {
return ((((n >> 4) * (kg / 16) + (g >> 4)) * 4 + ((g & 15) >> 2)) * 16 + (n & 15)) * 4 + (g & 3);
}
inline int64_t m2_emax_off(int64_t N, int64_t kg) { return (N * kg + 63) / 64 * 64; }
} // namespace xe2