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
+26
-16
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user