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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user