Archived
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:
1 parent
68d510b1f4
commit
dad30c6b59
4 files changed
+124
-47
No files matched your search
+61
-18
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user