Archived
libxe2: the w4a16 decode GEMM a tile's K splits a work-group, reduced in local memory -- decode proxy 46.3 -> 48.5 tok/s
The first form ran a thread a (tile, split) and a separate reduction launch over partials in global memory, and kept narrow-N shapes on one split (too few threads to fill the card). Now a tile's splits are one work-group whose partial sums meet in local memory, added in split order, and S is the divisor of the group count closest to 8 groups a thread. Probe w4d.cpp at M = 4: gate_up 190-208 -> 162 us, N 16384 125-147 -> 79, N 12288 108-117 -> 61, down 148 -> 83, N 5120 K 6144 35 (+ reduce) -> 32, N 1024 10 (+) -> 7.4 -- the weights at the card's ~560 GB/s. XE2_W4_CHECK on the 27B (M = 5): 10 of 81920, 6 of 25600 and 14 of 174080 bf16 outputs differ from libref's loop (ties; rel_l2 <= 1.8e-5); greedy text unchanged; dec_quick.py 46.3 -> 48.5 tok/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d63d1bc8a5
commit
5ed4ff847e
2 files changed
+62
-98
No files matched your search
+16
-36
@@ -41,8 +41,7 @@ namespace xe2 {
|
||||
|
||||
/* xe2_gemm_w4_esimd.cpp */
|
||||
int w4a16_esimd(sycl::queue& q, int rc, const sycl::half* a, int64_t ars, int mm, const uint32_t* ct,
|
||||
const uint32_t* sz, int64_t N, int64_t K, int splits, float* pbuf, int64_t prow, int64_t pm,
|
||||
char* yb, uint32_t ydt, int64_t yrs);
|
||||
const uint32_t* sz, int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs);
|
||||
int w4a8i8_esimd(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad,
|
||||
const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt,
|
||||
int64_t yrs);
|
||||
@@ -351,28 +350,18 @@ void check_call(sycl::queue& q, const W4Call& c, const char* form, bool exact, s
|
||||
exact ? " (an exact form: must be 0)" : "", r2 > 0 ? std::sqrt(e2 / r2) : 0.0, emax, rmax);
|
||||
}
|
||||
|
||||
/* The split-K partials [splits][pm][N] summed in split order into y. */
|
||||
int reduce_splits(sycl::queue& q, const float* pbuf, int splits, int64_t M, int64_t N, char* yb, uint32_t ydt,
|
||||
int64_t ys_r, int64_t ys_c) {
|
||||
return submit([&] {
|
||||
syclex::nd_launch(q, sycl::nd_range<1>{(size_t)((M * N + 255) / 256 * 256), 256}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t i = (int64_t)it.get_global_id(0);
|
||||
if (i >= M * N) return;
|
||||
const int64_t m = i / N, n = i % N;
|
||||
float s = 0.0f;
|
||||
for (int sp = 0; sp < splits; ++sp) s += pbuf[(sp * M + m) * N + n];
|
||||
rad_store_f32(yb, ydt, m * ys_r + n * ys_c, s);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* Threads a 16-column tile for the decode kernel: enough tiles alone fill the card's 1280 thread
|
||||
* slots; narrower outputs split K (probe w4d.cpp: N = 5120 best at two, N = 1024 at ten). */
|
||||
int w4_splits(int64_t N, int64_t G) {
|
||||
if (const char* e = std::getenv("XE2_W4_SPLITS")) return (int)std::clamp<int64_t>(std::atoi(e), 1, G);
|
||||
const int64_t ntile = N / 16;
|
||||
int64_t s = ntile >= 640 ? 1 : (ntile >= 256 ? 2 : std::min<int64_t>(10, (1280 + ntile - 1) / ntile));
|
||||
return (int)std::clamp<int64_t>(s, 1, G);
|
||||
/* The decode kernel's splits of K a tile: the divisor of G closest to G / 8, about 8 groups a thread
|
||||
* (probe w4d.cpp: G = 40 best at 5, G = 136 at 8-17, G = 48 at 4-8); a tile's splits are one
|
||||
* work-group, at most 32. */
|
||||
int w4_splits(int64_t G) {
|
||||
if (const char* e = std::getenv("XE2_W4_SPLITS")) {
|
||||
const int64_t s = std::clamp<int64_t>(std::atoi(e), 1, G);
|
||||
if (G % s == 0 && s <= 32) return (int)s;
|
||||
}
|
||||
int best = 1;
|
||||
for (int64_t s = 1; s <= 32 && s <= G; ++s)
|
||||
if (G % s == 0 && std::llabs(G / s - 8) <= std::llabs(G / best - 8)) best = (int)s;
|
||||
return best;
|
||||
}
|
||||
|
||||
std::atomic<int> g_check_exact{0}, g_check_a16{0}, g_check_a8{0};
|
||||
@@ -410,24 +399,15 @@ int gemm_nt_q_w4a16(const RadArgs* a, RadStream st) {
|
||||
return gemm_nt_q_w4(a, st);
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const int splits = w4_splits(c.N, c.G);
|
||||
float* pbuf = nullptr;
|
||||
if (splits > 1) {
|
||||
pbuf = workspace(*q, (size_t)(splits * c.M * c.N), 3);
|
||||
if (!pbuf) return RAD_E_DEVICE;
|
||||
}
|
||||
const int splits = w4_splits(c.G);
|
||||
const sycl::half* a16 = (const sycl::half*)c.ab;
|
||||
for (int64_t m0 = 0; m0 < c.M; m0 += 8) {
|
||||
const int mm = (int)std::min<int64_t>(8, c.M - m0);
|
||||
const int rcount = mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8));
|
||||
const int rc = w4a16_esimd(*q, rcount, a16 + m0 * c.ars, c.ars, mm, (const uint32_t*)c.bb,
|
||||
(const uint32_t*)c.bsb, c.N, c.K, splits, pbuf, m0, c.M,
|
||||
c.yb + m0 * c.ys_r * (c.ydt == RAD_F32 ? 4 : 2), c.ydt, c.ys_r);
|
||||
const int rc = w4a16_esimd(*q, rcount, a16 + m0 * c.ars, c.ars, mm, (const uint32_t*)c.bb, (const uint32_t*)c.bsb,
|
||||
c.N, c.K, splits, c.yb + m0 * c.ys_r * (c.ydt == RAD_F32 ? 4 : 2), c.ydt, c.ys_r);
|
||||
if (rc != RAD_OK) return rc;
|
||||
}
|
||||
if (splits > 1) {
|
||||
if (int rc = reduce_splits(*q, pbuf, splits, c.M, c.N, c.yb, c.ydt, c.ys_r, c.ys_c); rc != RAD_OK) return rc;
|
||||
}
|
||||
check_call(*q, c, "w4a16 fp16 DPAS", false, g_check_a16);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
/* xe2_gemm_w4_esimd.cpp -- the 4-bit zero-point GEMMs in ESIMD, over the planes stored tiled
|
||||
* (xe2_ops.h, w4_tiled; xe2_gemm_w4.cpp has the forms and their checks).
|
||||
*
|
||||
* w4a16_esimd, M <= 64 in blocks of 8 rows: a thread owns a 16-column tile and walks its split of K
|
||||
* a group (128 columns) at a time -- the group's codes are 1 KiB, four block loads. Each dword w of
|
||||
* eight codes decodes into four VNNI pairs, ((w >> 4s) & 0x000F000F) | 0x64006400 being the pair
|
||||
* (1024 + code) in fp16, and one 32-wide fp16 subtract of the column's 1024 + zero leaves
|
||||
* code - zero, exact. Eight fp16 DPAS a group, RC rows of the activation each (RC = 1, 2, 4 or 8:
|
||||
* the DPAS repeat count, so a decode row costs one row of the systolic array, not eight), sum the
|
||||
* group in f32, which is scaled by the column's scale once and added to the total. The activation
|
||||
* comes from row loads for RC <= 4 and from 2D block loads at RC = 8, which return the DPAS A
|
||||
* operand as it lies (probe w4d.cpp: M = 8 at gate_up's shape 466 -> 210 us; at M = 4 row loads
|
||||
* were faster). At RC = 1 a group's codes are loaded before the previous group is computed (narrow
|
||||
* outputs: N = 5120, K = 6144 82 -> 35 us; at RC > 1 the extra registers cost more than it hid).
|
||||
* Narrow outputs split K (the caller's choice); partials go to pbuf for its reduction.
|
||||
* w4a16_esimd, M <= 64 in blocks of 8 rows: a thread owns a 16-column tile and a split of K, walked a
|
||||
* group (128 columns) at a time -- the group's codes are 1 KiB, four block loads. Each dword w of eight
|
||||
* codes decodes into four VNNI pairs, ((w >> 4s) & 0x000F000F) | 0x64006400 being the pair (1024 +
|
||||
* code) in fp16, and one 32-wide fp16 subtract of the column's 1024 + zero leaves code - zero, exact.
|
||||
* Eight fp16 DPAS of RC rows of the activation each (RC = 1, 2, 4 or 8: the DPAS repeat count, so a
|
||||
* decode row costs one row of the systolic array, not eight) sum the group in f32, which is scaled
|
||||
* by the column's scale once and added to the split's total; a tile's splits are one work-group and
|
||||
* meet in local memory (launch16). The activation comes from row loads for RC <= 4 and from 2D block
|
||||
* loads at RC = 8, which return the DPAS A operand as it lies (probe w4d.cpp: M = 8 at gate_up's
|
||||
* shape 466 -> 210 us; at M = 4 row loads were faster).
|
||||
*
|
||||
* w4a8i8_esimd, any M: int8 DPAS with the tiled codes as its u4 B operand, 8 codes a dword, against
|
||||
* the prepass's activation (bytes of each 8 evens first, the codes' nibble order), staged in local
|
||||
@@ -40,39 +38,35 @@ inline esimd::simd<uint16_t, 16> to_bf16(esimd::simd<float, 16> v) SYCL_ESIMD_FU
|
||||
return simd<uint16_t, 16>(h);
|
||||
}
|
||||
|
||||
template <int RC, bool PF, bool A2D>
|
||||
/* A work-group is one tile's S splits of K: each thread sums its groups, the S partial sums meet in
|
||||
* local memory and are added in split order, and the group's first threads store y. S is the caller's
|
||||
* (w4_splits: about 8 groups a thread). Against the first form -- a thread a (tile, split) and a
|
||||
* separate reduction launch over partials in global memory -- probe w4d.cpp at M = 4 (us):
|
||||
* gate_up 190-208 -> 162, N 16384 125-147 -> 79, N 12288 108-117 -> 61, down 148 -> 83, N 5120
|
||||
* K 6144 35 (+ the reduction) -> 32, N 1024 10 (+) -> 7.4: the weight's bytes at the card's ~560 GB/s. */
|
||||
constexpr int kMaxSplits = 32;
|
||||
|
||||
template <int RC, bool A2D>
|
||||
int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, int64_t N,
|
||||
int64_t K, int splits, float* pbuf, int64_t prow, int64_t pm, char* yb, uint32_t ydt, int64_t yrs) {
|
||||
int64_t K, int S, char* yb, uint32_t ydt, int64_t yrs) {
|
||||
const int64_t ntile = N / 16, G = K / 128;
|
||||
const int64_t per = (G + splits - 1) / splits;
|
||||
const int64_t nthr = ntile * splits;
|
||||
constexpr int kWg = 8;
|
||||
const int64_t ng = (nthr + kWg - 1) / kWg;
|
||||
const int64_t per = G / S;
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(ng * kWg), (size_t)kWg}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(ntile * S), (size_t)S}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
using namespace esimd;
|
||||
const int64_t tid = (int64_t)it.get_global_id(0);
|
||||
if (tid >= nthr) return;
|
||||
const int64_t tile = tid % ntile, sp = tid / ntile;
|
||||
const int64_t g0 = sp * per, g1 = G < g0 + per ? G : g0 + per;
|
||||
slm_init<kMaxSplits * RC * 16 * 4>();
|
||||
const int sp = (int)it.get_local_id(0);
|
||||
const int64_t tile = (int64_t)it.get_group(0);
|
||||
const int64_t g0 = sp * per, g1 = g0 + per;
|
||||
const uint32_t* cb = ct + tile * (K / 8) * 16;
|
||||
const uint32_t* sb = sz + tile * G * 16;
|
||||
const unsigned sw = (unsigned)(K * 2 - 1), sh = (unsigned)(mm - 1), spt = (unsigned)(ars * 2 - 1);
|
||||
auto ldc = [&](int64_t g, simd<uint32_t, 256>& cw, simd<uint32_t, 16>& szw) SYCL_ESIMD_FUNCTION {
|
||||
simd<float, RC * 16> tot = 0.0f;
|
||||
for (int64_t g = g0; g < g1; ++g) {
|
||||
simd<uint32_t, 256> cw;
|
||||
#pragma unroll
|
||||
for (int qd = 0; qd < 4; ++qd) cw.template select<64, 1>(qd * 64) = block_load<uint32_t, 64>(cb + g * 256 + qd * 64);
|
||||
szw = block_load<uint32_t, 16>(sb + g * 16);
|
||||
};
|
||||
simd<float, RC * 16> tot = 0.0f;
|
||||
simd<uint32_t, 256> cw;
|
||||
simd<uint32_t, 16> szw;
|
||||
if (g0 < g1) ldc(g0, cw, szw);
|
||||
for (int64_t g = g0; g < g1; ++g) {
|
||||
simd<uint32_t, 256> cwn;
|
||||
simd<uint32_t, 16> szn;
|
||||
if constexpr (PF) {
|
||||
if (g + 1 < g1) ldc(g + 1, cwn, szn);
|
||||
}
|
||||
simd<uint32_t, 16> szw = block_load<uint32_t, 16>(sb + g * 16);
|
||||
simd<half, RC * 128> Ar; /* A2D: [pair of steps][step][row][16]; else [row][128] */
|
||||
if constexpr (A2D) {
|
||||
#pragma unroll
|
||||
@@ -96,9 +90,9 @@ int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t*
|
||||
for (int t = 0; t < 2; ++t) {
|
||||
const simd<uint32_t, 16> w = cw.template select<16, 1>((2 * j + t) * 16);
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 4; ++s) {
|
||||
simd<uint32_t, 16> d = ((w >> (4 * s)) & 0x000F000Fu) | 0x64006400u;
|
||||
B.template select<32, 1>((4 * t + s) * 32) = d.template bit_cast_view<half>() - zz;
|
||||
for (int s4 = 0; s4 < 4; ++s4) {
|
||||
simd<uint32_t, 16> d = ((w >> (4 * s4)) & 0x000F000Fu) | 0x64006400u;
|
||||
B.template select<32, 1>((4 * t + s4) * 32) = d.template bit_cast_view<half>() - zz;
|
||||
}
|
||||
}
|
||||
simd<half, RC * 16> A;
|
||||
@@ -115,25 +109,15 @@ int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t*
|
||||
const simd<float, 16> sc = simd<half, 16>(s16.template bit_cast_view<half>());
|
||||
#pragma unroll
|
||||
for (int m = 0; m < RC; ++m) tot.template select<16, 1>(m * 16) += cg.template select<16, 1>(m * 16) * sc;
|
||||
if constexpr (PF) {
|
||||
cw = cwn;
|
||||
szw = szn;
|
||||
} else if (g + 1 < g1) {
|
||||
ldc(g + 1, cw, szw);
|
||||
}
|
||||
}
|
||||
slm_block_store<float, RC * 16>((uint32_t)(sp * RC * 16 * 4), tot);
|
||||
barrier();
|
||||
const int64_t n0 = tile * 16;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < RC; ++m) {
|
||||
if (m >= mm) break;
|
||||
const simd<float, 16> v = tot.template select<16, 1>(m * 16);
|
||||
if (splits > 1) {
|
||||
block_store<float, 16>(pbuf + ((sp * pm) + prow + m) * N + n0, v);
|
||||
} else if (ydt == RAD_F32) {
|
||||
block_store<float, 16>((float*)yb + m * yrs + n0, v);
|
||||
} else {
|
||||
block_store<uint16_t, 16>((uint16_t*)yb + m * yrs + n0, to_bf16(v));
|
||||
}
|
||||
for (int r = sp; r < RC && r < mm; r += S) { /* row r: the splits in order */
|
||||
simd<float, 16> v = 0.0f;
|
||||
for (int u = 0; u < S; ++u) v += slm_block_load<float, 16>((uint32_t)((u * RC * 16 + r * 16) * 4));
|
||||
if (ydt == RAD_F32) block_store<float, 16>((float*)yb + r * yrs + n0, v);
|
||||
else block_store<uint16_t, 16>((uint16_t*)yb + r * yrs + n0, to_bf16(v));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -261,13 +245,13 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo
|
||||
} // namespace
|
||||
|
||||
int w4a16_esimd(sycl::queue& q, int rc, const half* a, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz,
|
||||
int64_t N, int64_t K, int splits, float* pbuf, int64_t prow, int64_t pm, char* yb, uint32_t ydt,
|
||||
int64_t yrs) {
|
||||
int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs) {
|
||||
if (splits < 1 || splits > kMaxSplits || (K / 128) % splits) return RAD_E_UNSUPPORTED;
|
||||
switch (rc) {
|
||||
case 1: return launch16<1, true, false>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
|
||||
case 2: return launch16<2, false, false>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
|
||||
case 4: return launch16<4, false, false>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
|
||||
default: return launch16<8, false, true>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
|
||||
case 1: return launch16<1, false>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs);
|
||||
case 2: return launch16<2, false>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs);
|
||||
case 4: return launch16<4, false>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs);
|
||||
default: return launch16<8, true>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user