Archived
libxe2: the w4a8i8 prefill GEMM tiled through local memory -- 27 -> 62-78 int8 TOPS, prefill 291 -> 378 tok/s
The first form gave each thread 32 rows by 32 columns in 256 registers and its own 2D loads of the activation: 23-29 TOPS at M = 2048 (probe w4p.cpp), and the probe's breakdown put the time in the activation loads, then the group epilogue (DPAS alone ran 120 TOPS). Now a work-group of 8 threads shares 16 rows: each group's activation is staged in local memory once, double-buffered, a step a 2D load; a thread owns 16 rows by 32 columns in 128 registers (8 threads an EU) and runs the group's epilogue on whole [8 rows][16] blocks against factors broadcast once; for outputs wider than 8192 the work-groups go rows fastest, so one 256-column slice of the weight stays in L2 while every row block uses it. TOPS at M = 2048: gate_up 27 -> 78, N 16384 K 5120 27 -> 75, down 24 -> 62, N 5120 K 6144 29 -> 78. The chain is unchanged: still libref's result bit for bit (XE2_W4_CHECK on the 27B: 0 of 349568, 327680 and 445696 outputs differ). quant_act_i8g for bf16 rows in groups of 128 reads a lane's 8 values in one 16-byte load and stores its 8 codes in one 8-byte store, typed (677 us a call at 2048 x 5120 through the generic accessors); the same floats and rounding: rad-kbench fixture 153/153 and the 27B 96/96, rel_l2 0. Prefill, 2234 tokens: 7.68 -> 5.90 s (378 tok/s). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e85124c0f0
commit
2365a2ae51
2 files changed
+107
-36
No files matched your search
@@ -15,10 +15,10 @@
|
||||
* Narrow outputs split K (the caller's choice); partials go to pbuf for its reduction.
|
||||
*
|
||||
* 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). A thread owns 32
|
||||
* rows by 32 columns, four 8-row blocks by two tiles: a group is four K-32 steps of eight DPAS into
|
||||
* int32 -- exact -- and then each output's term ((sum - zero * asum) * a_scale) * scale is added to
|
||||
* its f32 total in group order: libref's result bit for bit. */
|
||||
* the prepass's activation (bytes of each 8 evens first, the codes' nibble order), staged in local
|
||||
* memory a work-group (launch_i8). A group is four K-32 steps of DPAS into int32 -- exact -- and then
|
||||
* each output's term ((sum - zero * asum) * a_scale) * scale is added to its f32 total in group
|
||||
* order: libref's result bit for bit. */
|
||||
#include "xe2_common.h"
|
||||
|
||||
#include <sycl/ext/intel/esimd.hpp>
|
||||
@@ -139,54 +139,74 @@ int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t*
|
||||
});
|
||||
}
|
||||
|
||||
/* The prefill GEMM. A work-group of 8 threads shares 16 rows: each group's activation ([step][row][32
|
||||
* bytes], 2 KiB) is staged in local memory once, double-buffered, threads 0-3 a step each by one 2D
|
||||
* load, and every thread reads its A operands from there. A thread owns 16 rows by 32 columns (two
|
||||
* 8-row blocks by two tiles): 128 registers, so 8 threads an EU, and the group's epilogue on whole
|
||||
* [8 rows][16] blocks against the factors broadcast once. RF orders the work-groups rows fastest, so
|
||||
* those over one 256-column slice of the weight run together and it stays in L2 (the wide outputs).
|
||||
* Probe w4p.cpp at M = 2048, int8 TOPS: gate_up 27 -> 78, inz 27 -> 75, down 24 -> 62 (66 without
|
||||
* RF), N = 5120 K = 6144 29 -> 78 (without RF); every output identical to the first form's. */
|
||||
template <bool RF>
|
||||
int launch_i8(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) {
|
||||
const int64_t G = K / 128, RG = mpad / 32, CG = N / 32;
|
||||
const int64_t nthr = RG * CG;
|
||||
constexpr int kWg = 8;
|
||||
const int64_t ng = (nthr + kWg - 1) / kWg;
|
||||
constexpr int RB = 2, R = RB * 8; /* rows a work-group */
|
||||
constexpr int kWg = 8; /* threads a work-group, two tiles each */
|
||||
constexpr int kStep = R * 32, kBuf = 4 * kStep;
|
||||
const int64_t G = K / 128, RG = (M + R - 1) / R, CG = N / 32;
|
||||
const int64_t cgw = (CG + kWg - 1) / kWg;
|
||||
namespace intelex = sycl::ext::intel::experimental;
|
||||
syclex::properties props{intelex::grf_size<256>};
|
||||
syclex::properties props{intelex::grf_size<128>};
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(ng * kWg), (size_t)kWg}, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(RG * cgw * kWg), (size_t)kWg}, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
using namespace esimd;
|
||||
const int64_t t = (int64_t)it.get_global_id(0);
|
||||
if (t >= nthr) return;
|
||||
const int64_t rg = t % RG, cg = t / RG;
|
||||
const int64_t r0 = rg * 32, tile0 = cg * 2;
|
||||
slm_init<2 * kBuf>();
|
||||
const int tl = (int)it.get_local_id(0);
|
||||
const int64_t wg = (int64_t)it.get_group(0);
|
||||
const int64_t rg = RF ? wg % RG : wg / cgw, cg = (RF ? wg / RG : wg % cgw) * kWg + tl;
|
||||
const bool live = cg < CG;
|
||||
const int64_t r0 = rg * R, tile0 = (live ? cg : 0) * 2;
|
||||
const unsigned sw = (unsigned)(K - 1), sh = (unsigned)(M - 1);
|
||||
simd<float, 128> acc[4][2];
|
||||
simd<float, 128> acc[RB][2];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int i = 0; i < RB; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j) acc[i][j] = 0.0f;
|
||||
auto stage = [&](int64_t g, int buf) SYCL_ESIMD_FUNCTION {
|
||||
if (tl < 4) { /* rows past M load as zeros (the 2D load's bounds) */
|
||||
simd<uint32_t, R * 8> a = load_2d<uint32_t, 8, R, 1, false>((const uint32_t*)ap, sw, sh, sw,
|
||||
(int)(g * 32 + tl * 8), (int)r0);
|
||||
slm_block_store<uint32_t, 128>((uint32_t)(buf * kBuf + tl * kStep), a);
|
||||
}
|
||||
};
|
||||
stage(0, 0);
|
||||
barrier();
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
simd<int32_t, 128> c[4][2];
|
||||
const int buf = (int)(g & 1);
|
||||
if (g + 1 < G) stage(g + 1, buf ^ 1);
|
||||
simd<int32_t, 128> c[RB][2];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int i = 0; i < RB; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j) c[i][j] = 0;
|
||||
#pragma unroll
|
||||
for (int ks = 0; ks < 4; ++ks) {
|
||||
/* 32 rows of the step's 32 bytes: [row][8 dwords], row block i its 256 bytes */
|
||||
simd<uint32_t, 256> A4 = load_2d<uint32_t, 8, 32, 1, false>((const uint32_t*)ap, sw, sh, sw,
|
||||
(int)((g * 128 + ks * 32) / 4), (int)r0);
|
||||
simd<uint32_t, 64> B[2];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j)
|
||||
B[j] = block_load<uint32_t, 64>(ct + ((tile0 + j) * (K / 8) + g * 16 + ks * 4) * 16);
|
||||
for (int j = 0; j < 2; ++j) B[j] = block_load<uint32_t, 64>(ct + ((tile0 + j) * (K / 8) + g * 16 + ks * 4) * 16);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const simd<uint32_t, 64> Ai = A4.template select<64, 1>(i * 64);
|
||||
for (int i = 0; i < RB; ++i) {
|
||||
simd<uint32_t, 64> Ai = slm_block_load<uint32_t, 64>((uint32_t)(buf * kBuf + ks * kStep + i * 256));
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j)
|
||||
c[i][j] = xmx::dpas<8, 8, int32_t, int32_t, uint32_t, uint32_t, xmx::dpas_argument_type::u4,
|
||||
xmx::dpas_argument_type::s8>(c[i][j], B[j], Ai);
|
||||
}
|
||||
}
|
||||
simd<int32_t, 32> as = block_load<int32_t, 32>(asumt + g * mpad + r0);
|
||||
simd<float, 32> sv = block_load<float, 32>(sat + g * mpad + r0);
|
||||
/* the group's terms ((sum - zero * asum) * a_scale) * scale, libref's chain */
|
||||
simd<int32_t, R> as = block_load<int32_t, R>(asumt + g * mpad + r0);
|
||||
simd<float, R> sv = block_load<float, R>(sat + g * mpad + r0);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
simd<uint32_t, 16> w = block_load<uint32_t, 16>(sz + ((tile0 + j) * G + g) * 16);
|
||||
@@ -194,21 +214,34 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo
|
||||
simd<uint16_t, 16> s16 = lo, z16 = hi;
|
||||
const simd<float, 16> sb = simd<half, 16>(s16.template bit_cast_view<half>());
|
||||
const simd<int32_t, 16> z = simd<int32_t, 16>(simd<float, 16>(simd<half, 16>(z16.template bit_cast_view<half>()))) - 1024;
|
||||
simd<int32_t, 128> zz;
|
||||
simd<float, 128> sbb;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int m = 0; m < 8; ++m) {
|
||||
zz.template select<16, 1>(m * 16) = z;
|
||||
sbb.template select<16, 1>(m * 16) = sb;
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < RB; ++i) {
|
||||
simd<int32_t, 128> asr;
|
||||
simd<float, 128> sar;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < 8; ++m) {
|
||||
const int r = i * 8 + m;
|
||||
const simd<int32_t, 16> is = c[i][j].template select<16, 1>(m * 16) - z * (int32_t)as[r];
|
||||
const simd<float, 16> sub = is;
|
||||
simd<float, 16> tt = sub * (float)sv[r];
|
||||
tt = tt * sb;
|
||||
acc[i][j].template select<16, 1>(m * 16) += tt;
|
||||
asr.template select<16, 1>(m * 16) = (int32_t)as[i * 8 + m];
|
||||
sar.template select<16, 1>(m * 16) = (float)sv[i * 8 + m];
|
||||
}
|
||||
const simd<int32_t, 128> is = c[i][j] - zz * asr;
|
||||
const simd<float, 128> sub = is;
|
||||
simd<float, 128> tt = sub * sar;
|
||||
tt = tt * sbb;
|
||||
acc[i][j] += tt;
|
||||
}
|
||||
}
|
||||
barrier();
|
||||
}
|
||||
if (!live) return;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int i = 0; i < RB; ++i)
|
||||
#pragma unroll
|
||||
for (int m = 0; m < 8; ++m) {
|
||||
const int64_t row = r0 + i * 8 + m;
|
||||
@@ -241,7 +274,8 @@ int w4a16_esimd(sycl::queue& q, int rc, const half* a, int64_t ars, int mm, cons
|
||||
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) {
|
||||
return launch_i8(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs);
|
||||
if (N > 8192) return launch_i8<true>(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs);
|
||||
return launch_i8<false>(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs);
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
@@ -203,6 +203,43 @@ int quant_act_groups(const RadArgs* a, RadStream st, float top, const char* what
|
||||
/* a sub-group a (row, group): the groups are independent, and a row's work-group walking its
|
||||
* groups in turn ran ~5 us a group (48 us at n = 17408) */
|
||||
const int64_t wpr = (ngrp + kSubs - 1) / kSubs; /* work-groups a row */
|
||||
/* bf16 in, int8 out, groups of 128 (the int4 release's prefill quantiser): a lane's 8 values one
|
||||
* 16-byte load and its 8 codes one 8-byte store, typed -- the generic accessors per element
|
||||
* ran 677 us a call at 2048 rows of 5120. The same floats, amax, scale and rounding (round to
|
||||
* nearest even and saturate, NaN to 0, rad_store_f32's rule). */
|
||||
if (top == 127.0f && x.dtype == RAD_BF16 && qv.dtype == RAD_I8 && g == 128 && n % 128 == 0 &&
|
||||
x.pitch % 8 == 0 && qv.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(qv.base, 8)) {
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wpr) * kWg, kWg},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const int64_t r = (int64_t)it.get_group(0) / wpr;
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int64_t gi = ((int64_t)it.get_group(0) % wpr) * kSubs + (int64_t)sg.get_group_linear_id();
|
||||
if (gi >= ngrp) return; /* uniform over the sub-group */
|
||||
const int lane = (int)sg.get_local_linear_id();
|
||||
const int64_t k0 = gi * 128 + lane * 8;
|
||||
const sycl::vec<uint16_t, 8> w = *(const sycl::vec<uint16_t, 8>*)((const uint16_t*)x.base + r * x.pitch + k0);
|
||||
float c[8];
|
||||
float amax = 0.0f;
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
c[j] = rad_bf16_to_f32(w[j]);
|
||||
amax = sycl::fmax(amax, sycl::fabs(c[j]));
|
||||
}
|
||||
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
|
||||
const float sc = amax > 0.0f ? amax * (1.0f / 127.0f) : 1.0f;
|
||||
const float inv = 1.0f / sc;
|
||||
sycl::vec<int8_t, 8> o;
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
float v = c[j] * inv;
|
||||
if (v > 127.0f) v = 127.0f;
|
||||
if (v < -127.0f) v = -127.0f;
|
||||
o[j] = v != v ? (int8_t)0 : (int8_t)sycl::rint(v);
|
||||
}
|
||||
*(sycl::vec<int8_t, 8>*)((int8_t*)qv.base + r * qv.pitch + k0) = o;
|
||||
if (lane == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
|
||||
});
|
||||
});
|
||||
}
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wpr) * kWg, kWg},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
|
||||
Reference in new issue
Block a user