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);
|
||||
|
||||
+44
-17
@@ -64,7 +64,7 @@ inline float e4m3(uint8_t b) {
|
||||
template <int MM, int NC>
|
||||
int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* codes0,
|
||||
const uint8_t* meta0, const uint8_t* expe0, char* yb, uint32_t ydt, int64_t N, int64_t K,
|
||||
int64_t acols, int64_t brs, int64_t yrs, int64_t ycs, int mm, int64_t e1, int64_t e2) {
|
||||
int64_t acols, int64_t brs, int64_t yrs, int64_t ycs, int mm, int64_t e1, int64_t e2, bool tl) {
|
||||
const int64_t per_wg = 16 * NC;
|
||||
const size_t groups = (size_t)((N + per_wg - 1) / per_wg);
|
||||
const int64_t kg = K / 32;
|
||||
@@ -87,7 +87,11 @@ int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* code
|
||||
for (int64_t g = lane; g < kg; g += kSub) {
|
||||
for (int c = 0; c < NC; ++c) {
|
||||
const int64_t n = n0 + (c < nc ? c : 0);
|
||||
const sycl::vec<uint32_t, 4> cw = *(const sycl::vec<uint32_t, 4>*)(codes0 + n * (K / 2) + g * 16);
|
||||
sycl::vec<uint32_t, 4> cw;
|
||||
if (tl)
|
||||
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];
|
||||
H2 w2[16];
|
||||
@@ -144,7 +148,7 @@ constexpr int kKs = 4; /* 32-groups a step: one 64-byte line of a column's cod
|
||||
template <int MT>
|
||||
int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* emax,
|
||||
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) {
|
||||
float* pbuf, int64_t N, int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits, bool tl) {
|
||||
const int64_t ntile = (N + 127) / 128;
|
||||
const size_t groups = (size_t)(ntile * splits);
|
||||
const int64_t kg = K / 32;
|
||||
@@ -172,7 +176,11 @@ int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* ema
|
||||
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc[MT];
|
||||
for (int i = 0; i < MT; ++i) jm::joint_matrix_fill(sg, mc[i], 0.0f);
|
||||
for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) {
|
||||
const sycl::vec<uint32_t, 16> line = *(const sycl::vec<uint32_t, 16>*)(crow + g0 * 16);
|
||||
sycl::vec<uint32_t, 16> line;
|
||||
if (tl)
|
||||
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);
|
||||
for (int gg = 0; gg < kKs; ++gg) {
|
||||
@@ -225,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) {
|
||||
int64_t yrs, int64_t ycs, int mm, bool tl) {
|
||||
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);
|
||||
@@ -269,7 +277,11 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const
|
||||
sycl::vec<uint32_t, 16> line[NT];
|
||||
uint32_t e4[NT], m4[NT];
|
||||
for (int t = 0; t < NT; ++t) {
|
||||
line[t] = *(const sycl::vec<uint32_t, 16>*)(codes0 + nn[t] * (K / 2) + g0 * 16);
|
||||
if (tl)
|
||||
for (int w = 0; w < 16; ++w)
|
||||
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);
|
||||
}
|
||||
@@ -322,7 +334,7 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const
|
||||
int m2_esimd(sycl::queue& q, int mt, const half* ae, const uint8_t* codes0, const uint8_t* meta0,
|
||||
const uint8_t* expe0, const int32_t* emax, 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, int64_t ldp);
|
||||
int64_t yrs, int64_t ycs, int mm, int64_t ldp, bool tl);
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -332,6 +344,7 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
|
||||
int64_t ycs, int64_t e1, int64_t e2) {
|
||||
const int mt = (int)((M + 7) / 8);
|
||||
const int64_t mpad = (int64_t)mt * 8, kg = K / 32, kb = kg;
|
||||
const bool tl = m2_tiled(N, K); /* the stored form gemm_m2_layout asked for */
|
||||
/* the register form unless XE2_M2_JM=1 asks for the local-memory one */
|
||||
static const bool jm_form = std::getenv("XE2_M2_JM") != nullptr;
|
||||
const bool raw = !jm_form;
|
||||
@@ -398,15 +411,15 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
|
||||
const uint32_t ydt = Y->dtype;
|
||||
if (es)
|
||||
rc = m2_esimd(q, mt, a16, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, ydt,
|
||||
yrs, ycs, (int)M, ldp);
|
||||
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)
|
||||
rr, yb, ydt, yrs, ycs, (int)M, tl)
|
||||
: 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);
|
||||
rr, yb, ydt, yrs, ycs, (int)M, tl);
|
||||
else
|
||||
rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits)
|
||||
: launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits);
|
||||
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);
|
||||
if (rc < 0 || (raw && splits == 1)) return rc;
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> i) {
|
||||
@@ -453,8 +466,10 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
|
||||
const uint8_t* codes0 = (const uint8_t*)B->data;
|
||||
const uint8_t* meta0 = codes0 + N * (K / 2);
|
||||
const int mm = (int)M;
|
||||
/* the verify band on XMX, where its line loads fit: K a multiple of 128, aligned rows */
|
||||
if (mm >= 2 && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, 4) &&
|
||||
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) &&
|
||||
((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);
|
||||
@@ -489,7 +504,7 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
|
||||
auto go = [&](auto tag, auto cols) {
|
||||
constexpr int MM = decltype(tag)::value, NC = decltype(cols)::value;
|
||||
return launch<MM, NC>(*q, a16, rr, codes0, meta0, (const uint8_t*)BS->data, (char*)Y->data, Y->dtype,
|
||||
N, K, acols, brs, yrs, ycs, mm, e1, e2);
|
||||
N, K, acols, brs, yrs, ycs, mm, e1, e2, tl);
|
||||
};
|
||||
/* Columns a sub-group: four at decode (the activation read once for four weight rows), fewer
|
||||
* as the rows' accumulators grow. */
|
||||
@@ -585,11 +600,23 @@ 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) {
|
||||
const int64_t n = (int64_t)i[0] / kb, g = (int64_t)i[0] % kb;
|
||||
/* tiled codes: consecutive work-items the 16 columns of a (tile, group), so a word's loads
|
||||
* are one 64-byte line */
|
||||
const int64_t ii = (int64_t)i[0];
|
||||
const int64_t n = tl ? (ii / (16 * kb)) * 16 + (ii & 15) : ii / kb;
|
||||
const int64_t g = tl ? (ii >> 4) % kb : ii % kb;
|
||||
constexpr float lut[16] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
|
||||
-0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f };
|
||||
const sycl::vec<uint8_t, 16> cb = *(const sycl::vec<uint8_t, 16>*)(codes0 + n * (K / 2) + g * 16);
|
||||
sycl::vec<uint8_t, 16> cb;
|
||||
if (tl)
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const sycl::vec<uint8_t, 4> w4 = *(const sycl::vec<uint8_t, 4>*)(codes0 + m2t_off(n, g * 16 + j * 4, K));
|
||||
for (int u = 0; u < 4; ++u) cb[j * 4 + u] = w4[u];
|
||||
}
|
||||
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];
|
||||
for (int q8 = 0; q8 < 4; ++q8) { /* 8 weights: the subgroup q8 of the group */
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
*
|
||||
* Layout: a thread 16 output columns, lane = column, as the SIMT kernel's sub-group. Its codes are
|
||||
* read 16 bytes a lane at a time (LSC d32x4 gathers, [dword][lane] in registers), so a word of 8
|
||||
* nibbles is one register across the 16 columns. The activation is ae[(c / 16) * mpad + m][c % 16]
|
||||
* nibbles is one register across the 16 columns -- or, stored tiled (xe2_ops.h, m2_tiled), a step's
|
||||
* 1 KiB already in that order, four block loads. The activation is ae[(c / 16) * mpad + m][c % 16]
|
||||
* (c the permuted column of xe2_gemv_m2.cpp's prepass, m the row): a DPAS A operand of 8 rows by 16
|
||||
* k is one 256-byte block load. Results: y directly (f32 / bf16) when the whole K is one thread's,
|
||||
* else partial sums into pbuf for the caller's reduction, exactly as launch_dpas_raw does. */
|
||||
@@ -28,7 +29,7 @@ using half = sycl::half;
|
||||
|
||||
constexpr int kKs = 4; /* 32-groups a step: 64 bytes of a column's codes */
|
||||
|
||||
template <int MT>
|
||||
template <int MT, bool TL>
|
||||
int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t* meta0,
|
||||
const uint8_t* expe0, const int32_t* emax, 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,
|
||||
@@ -53,6 +54,7 @@ int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t*
|
||||
col.merge(simd<uint32_t, 16>((uint32_t)(N - 1)), col > (uint32_t)(N - 1));
|
||||
const uint8_t* cbase = codes0 + n0 * (K / 2);
|
||||
const simd<uint32_t, 16> coff = (col - (uint32_t)n0) * (uint32_t)(K / 2);
|
||||
const uint32_t* tbase = (const uint32_t*)codes0 + tile * (kg / 4) * 256; /* TL: the tile's steps */
|
||||
const simd<uint32_t, 16> eoff = col * (uint32_t)brs;
|
||||
const simd<uint32_t, 16> moff = col * (uint32_t)kg;
|
||||
/* the column's largest exponent, from the caller's prepass */
|
||||
@@ -62,9 +64,14 @@ int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t*
|
||||
/* a step: kKs groups -- the code lines, the activation blocks, the decode and DPAS */
|
||||
auto step = [&](int64_t g0, simd<uint32_t, 16> e4, simd<uint32_t, 16> m4) SYCL_ESIMD_FUNCTION {
|
||||
simd<uint32_t, 256> line; /* [word 16][lane 16] */
|
||||
_Pragma("unroll") for (int qd = 0; qd < 4; ++qd)
|
||||
line.template select<64, 1>(qd * 64) =
|
||||
gather<uint32_t, 64, 4>((const uint32_t*)(cbase + g0 * 16 + qd * 16), coff);
|
||||
if constexpr (TL) { /* tiled: the step is these 1 KiB as they lie */
|
||||
_Pragma("unroll") for (int qd = 0; qd < 4; ++qd)
|
||||
line.template select<64, 1>(qd * 64) = block_load<uint32_t, 64>(tbase + (g0 / kKs) * 256 + qd * 64);
|
||||
} else {
|
||||
_Pragma("unroll") for (int qd = 0; qd < 4; ++qd)
|
||||
line.template select<64, 1>(qd * 64) =
|
||||
gather<uint32_t, 64, 4>((const uint32_t*)(cbase + g0 * 16 + qd * 16), coff);
|
||||
}
|
||||
/* the step's activation, 8 k-blocks of [mpad rows][16 k], loaded with the codes */
|
||||
simd<half, kKs * 2 * MT * 128> As;
|
||||
_Pragma("unroll") for (int b = 0; b < kKs * 2 * MT; ++b)
|
||||
@@ -154,11 +161,16 @@ int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t*
|
||||
int m2_esimd(sycl::queue& q, int mt, const half* ae, const uint8_t* codes0, const uint8_t* meta0,
|
||||
const uint8_t* expe0, const int32_t* emax, 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, int64_t ldp) {
|
||||
return mt == 1 ? launch<1>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb,
|
||||
ydt, yrs, ycs, mm, ldp)
|
||||
: launch<2>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb,
|
||||
ydt, yrs, ycs, mm, ldp);
|
||||
int64_t yrs, int64_t ycs, int mm, int64_t ldp, bool tl) {
|
||||
auto go = [&](auto mt_, auto tl_) {
|
||||
return launch<decltype(mt_)::value, decltype(tl_)::value>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K,
|
||||
e1, e2, mpad, splits, rr, yb, ydt, yrs, ycs, mm, ldp);
|
||||
};
|
||||
using T = std::true_type;
|
||||
using F = std::false_type;
|
||||
using M1 = std::integral_constant<int, 1>;
|
||||
using M2 = std::integral_constant<int, 2>;
|
||||
return mt == 1 ? (tl ? go(M1{}, T{}) : go(M1{}, F{})) : (tl ? go(M2{}, T{}) : go(M2{}, F{}));
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
@@ -101,4 +101,13 @@ 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; }
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
Reference in new issue
Block a user