Archived
libxe2: m2xfp4 verify GEMM on register DPAS -- no local-memory round trip
SPV_INTEL_subgroup_matrix_multiply_accumulate gives DPAS on operands in registers (dpas_f16 in xe2_common.h; libxe2's link enables the extension). Its B operand is a lane's column as dwords of fp16 pairs, which is what a lane decodes from its codes, so the 2 <= M <= 16 m2xfp4 GEMM no longer writes the decoded weights to local memory and reads them back as a joint_matrix tile; the activation is transposed once so a lane's A column is one 16-byte load. 15-20% a call at M = 8 (N=34816 K=5120 518 -> 463 us, N=5120 K=17408 312 -> 249 us). Same products: rel_l2 at most 1.46e-3 as before (185/185 model cases). XE2_M2_JM=1 keeps the local-memory form. 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
84732432c0
commit
b0e34c6324
3 files changed
+114
-5
No files matched your search
+95
-4
@@ -18,6 +18,7 @@
|
||||
#include "xe2_ops.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace xe2 {
|
||||
|
||||
@@ -213,12 +214,97 @@ int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* ema
|
||||
});
|
||||
}
|
||||
|
||||
/* The same products with DPAS on registers (dpas_f16): a lane owns one output column of each of
|
||||
* the sub-group's NT 16-column tiles and decodes its codes straight into the B operand -- a word's
|
||||
* four (u, u + 4) pairs are B's dwords in the activation's order -- so nothing goes through local
|
||||
* memory and no barrier is taken (NT = 2, a lane two columns, measured slower: 463 -> 639 us at
|
||||
* N = 34816). The activation is transposed for it, at[(part K + k) * mpad + m],
|
||||
* so a lane's A column (k = k0 + lane, the tile's rows) is one 16-byte load, shared by the NT
|
||||
* tiles. K split as above. */
|
||||
template <int MT, int NT>
|
||||
int launch_dpas_raw(sycl::queue& q, const half* at, 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) {
|
||||
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);
|
||||
const int64_t kg = K / 32;
|
||||
const int64_t per = ((kg + splits - 1) / splits + kKs - 1) / kKs * kKs;
|
||||
const int64_t ldp = ntile * kCols;
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{groups * 128, 128}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
using H2 = sycl::vec<half, 2>;
|
||||
using A8 = sycl::vec<uint16_t, 8>;
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
|
||||
const int64_t tile = (int64_t)it.get_group(0) % ntile, sp = (int64_t)it.get_group(0) / ntile;
|
||||
const int64_t g0s = sp * per, g1s = sycl::min(kg, g0s + per);
|
||||
const int64_t n0 = tile * kCols + s * 16 * NT;
|
||||
if (n0 >= N) return; /* uniform */
|
||||
/* the tiles of one sub-group share a part: part bounds are multiples of 16 * NT */
|
||||
const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2);
|
||||
const half* abase = at + (part * K + lane) * mpad;
|
||||
int64_t nn[NT];
|
||||
int emx[NT];
|
||||
for (int t = 0; t < NT; ++t) {
|
||||
nn[t] = sycl::min(n0 + t * 16 + lane, N - 1);
|
||||
emx[t] = emax[nn[t]];
|
||||
}
|
||||
xe2_float8 c[NT][MT];
|
||||
for (int t = 0; t < NT; ++t)
|
||||
for (int i = 0; i < MT; ++i) c[t][i] = xe2_float8(0.0f);
|
||||
for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) {
|
||||
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);
|
||||
e4[t] = *(const uint32_t*)(expe0 + nn[t] * brs + g0);
|
||||
m4[t] = *(const uint32_t*)(meta0 + 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 */
|
||||
xe2_short8 a[MT];
|
||||
for (int i = 0; i < MT; ++i) {
|
||||
const A8 av = *(const A8*)(abase + (g0 * 32 + gg * 32 + hh * 16) * mpad + i * 8);
|
||||
for (int m = 0; m < 8; ++m) a[i][m] = (short)av[m];
|
||||
}
|
||||
for (int t = 0; t < NT; ++t) {
|
||||
const int d = (int)((e4[t] >> (8 * gg)) & 0xFF) - emx[t];
|
||||
const float sc = d < -30 ? 0.0f : sycl::ldexp(16384.0f, d);
|
||||
const uint32_t mt = (m4[t] >> (8 * gg)) & 0xFF;
|
||||
xe2_int8 b;
|
||||
for (int w = 0; w < 2; ++w) {
|
||||
const int j = 2 * hh + w;
|
||||
const uint32_t word = line[t][gg * 4 + j];
|
||||
const half kh = (half)((1.0f + 0.25f * (float)((mt >> (2 * j)) & 3)) * sc);
|
||||
const H2 k2(kh, kh);
|
||||
for (int u = 0; u < 4; ++u) {
|
||||
const uint32_t mb2 = u < 3 ? (word << (9 - 4 * u)) & 0x0E000E00u : (word >> 3) & 0x0E000E00u;
|
||||
const uint32_t sb2 = (word << (12 - 4 * u)) & 0x80008000u;
|
||||
b[w * 4 + u] = sycl::bit_cast<int>(sycl::bit_cast<H2>(mb2 | sb2) * k2);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < MT; ++i) c[t][i] = dpas_f16(a[i], b, c[t][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int t = 0; t < NT; ++t)
|
||||
for (int i = 0; i < MT; ++i)
|
||||
for (int m = 0; m < 8; ++m)
|
||||
pbuf[(sp * mpad + (int64_t)i * 8 + m) * ldp + n0 + t * 16 + lane] = c[t][i][m];
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8_t* codes0,
|
||||
const uint8_t* meta0, const uint8_t* expe0, int64_t brs, const RadTensor* Y, int64_t M,
|
||||
int64_t N, int64_t K, int64_t acols, int64_t scols, int64_t ars, int64_t srs, int64_t yrs,
|
||||
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;
|
||||
/* 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;
|
||||
const int64_t ntile = (N + 127) / 128, ldp = ntile * 128;
|
||||
constexpr int64_t kFill = 160;
|
||||
const int splits = (int)std::min<int64_t>(std::max<int64_t>(1, (kFill + ntile - 1) / ntile), std::max<int64_t>(1, kg / kKs));
|
||||
@@ -244,8 +330,9 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
|
||||
const float f = 1.0f / R;
|
||||
for (int64_t c = t; c < acols; c += 256) {
|
||||
const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1);
|
||||
a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 128) * f)
|
||||
: (half)0.0f;
|
||||
a16[raw ? c * mpad + m : m * acols + c] =
|
||||
m < M ? (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 128) * f)
|
||||
: (half)0.0f;
|
||||
}
|
||||
});
|
||||
q.parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) {
|
||||
@@ -262,8 +349,12 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
|
||||
});
|
||||
});
|
||||
if (rc < 0) return rc;
|
||||
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);
|
||||
if (raw)
|
||||
rc = mt == 1 ? launch_dpas_raw<1, 1>(q, a16, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits)
|
||||
: launch_dpas_raw<2, 1>(q, a16, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits);
|
||||
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);
|
||||
if (rc < 0) return rc;
|
||||
char* yb = (char*)Y->data;
|
||||
const uint32_t ydt = Y->dtype;
|
||||
|
||||
Reference in new issue
Block a user