Archived
libxe2: m2xfp4 codes stored tiled at load -- the verify GEMM's step one block load, gate_up 240 -> 222 us
Where N % 16 == 0 and K % 128 == 0 the layout hook asks for "xe2.m2.tiled16": a 16-column tile's 128-column step is 1 KiB, dword w of its columns side by side, the ESIMD kernel's [word][lane] registers as they lie (four block loads in place of four d32x4 gathers). M = 1 goes to the same kernel when tiled. Every m2 reader in libxe2 (exact, SIMT GEMV, jm and SIMT DPAS forms, prefill decode) reads the tiled form; unrelayout inverts it for the oracle. rad-kbench against libref: 70 checked, 70 passed (worst rel_l2 1.2e-3). M = 8: gate_up 240 -> 222, down 140 -> 119, N=1024 50 -> 25 us. Greedy output identical (8 prompts x 64 tokens); 512-token decode 31.8 -> 33.3 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
2a764d2d38
commit
68d510b1f4
4 files changed
+107
-37
No files matched your search
+32
-10
@@ -10,9 +10,11 @@
|
||||
* rotated per piece): weight rows [0, e1), [e1, e2), [e2, N) read copies 0, 1, 2 of the activation
|
||||
* and its scale, side by side, as libref's gemm_m2 and libr4d's mx_parts do.
|
||||
*
|
||||
* `b` reaches the kernel as the weight's codes rows, then its meta rows ("m2.codes.meta"), `b_scale`
|
||||
* as the E8M0 plane [N, K/32]. The layout hooks are libref's m2 arm (ref_layout_gemm_q,
|
||||
* ref_planes.h) line for line, so both libraries ask for the same stored bytes.
|
||||
* `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.
|
||||
*/
|
||||
#include "xe2_common.h"
|
||||
#include "xe2_ops.h"
|
||||
@@ -67,6 +69,17 @@ bool m2_enc_planes(const RadEncoding* enc) {
|
||||
}
|
||||
|
||||
constexpr const char* kTag = "m2.codes.meta";
|
||||
constexpr const char* kTagT = "xe2.m2.tiled16";
|
||||
|
||||
/* Copies the codes plane from row-major [N][K/2] to the tiled form (xe2_ops.h), or back. */
|
||||
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)
|
||||
for (int64_t b = 0; b < rb; b += 4) {
|
||||
const int64_t t = m2t_off(n, b, K), r = n * rb + b;
|
||||
std::memcpy(dst + (to_tiled ? t : r), src + (to_tiled ? r : t), 4);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -83,7 +96,7 @@ int gemm_m2_layout(const RadParam* p, int n_p, int operand, const RadEncoding* e
|
||||
if (planes[0].shape[0] != N || planes[0].shape[1] != K || planes[1].shape[0] != N ||
|
||||
planes[1].shape[1] != K / 32)
|
||||
return RAD_E_SHAPE;
|
||||
out->tag = kTag;
|
||||
out->tag = m2_tiled(N, K) ? kTagT : kTag;
|
||||
out->dtype = RAD_U8;
|
||||
out->rank = 1;
|
||||
out->shape[0] = N * (K / 2 + K / 32);
|
||||
@@ -105,13 +118,17 @@ 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;
|
||||
if (!lay.tag || std::strcmp(lay.tag, kTag) != 0)
|
||||
const bool tiled = lay.tag && std::strcmp(lay.tag, kTagT) == 0;
|
||||
if (!lay.tag || (!tiled && std::strcmp(lay.tag, kTag) != 0))
|
||||
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;
|
||||
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]);
|
||||
std::memcpy(d, planes[i].data, (size_t)b);
|
||||
if (i == 0 && tiled)
|
||||
tile_codes((const uint8_t*)planes[0].data, d, planes[0].shape[0], planes[0].shape[1], true);
|
||||
else
|
||||
std::memcpy(d, planes[i].data, (size_t)b);
|
||||
d += b;
|
||||
}
|
||||
return RAD_OK;
|
||||
@@ -123,13 +140,17 @@ 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;
|
||||
if (!lay.tag || std::strcmp(lay.tag, kTag) != 0)
|
||||
const bool tiled = lay.tag && std::strcmp(lay.tag, kTagT) == 0;
|
||||
if (!lay.tag || (!tiled && std::strcmp(lay.tag, kTag) != 0))
|
||||
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;
|
||||
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]);
|
||||
std::memcpy(planes[i].data, s, (size_t)b);
|
||||
if (i == 0 && tiled)
|
||||
tile_codes(s, (uint8_t*)planes[0].data, planes[0].shape[0], planes[0].shape[1], false);
|
||||
else
|
||||
std::memcpy(planes[i].data, s, (size_t)b);
|
||||
s += b;
|
||||
}
|
||||
return RAD_OK;
|
||||
@@ -177,6 +198,7 @@ int gemm_nt_q_m2(const RadArgs* a, RadStream st) {
|
||||
const uint8_t* expe0 = (const uint8_t*)BS->data;
|
||||
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 */
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const int64_t total = M * N;
|
||||
@@ -189,13 +211,13 @@ 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* codes = codes0 + n * (K / 2);
|
||||
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 c = (uint8_t)((codes[k >> 1] >> (4 * (k & 1))) & 0xF);
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user