Archived
For qwen3.8-27b-w4a16-rtn (u4 codes, f16 scale and u4 zero per 128 of K): the engine casts a linear's input to f16 for gemm_nt_q at w4a16_asym up to 64 rows a step, and quantises it with quant_act_i8g for w4a8i8_asym above. None had a device kernel. - The planes are stored tiled at load (xe2_ops.h, w4_tiled): a 16-column tile's codes side by side, the even codes of each dword in its low half so (w >> 4s) & 0x000F000F is a VNNI pair, and the scale with f16(1024 + zero) one dword a (column, group). unrelayout inverts both exactly. - gemm_nt_q_w4: libref's gemm_zp loop per output, the exact form (XE2_EXACT=1, XE2_W4_EXACT=1). - w4a16_asym, M <= 64: ESIMD, a thread a tile; (code | 0x6400) less f16(1024 + zero) is code - zero exactly; eight fp16 DPAS of RC = 1, 2, 4 or 8 rows a group, scaled in f32 a group. Narrow outputs split K. Probe w4d.cpp, incompressible codes: gate_up (N 34816, K 5120) 173 us at M = 1 (546 GB/s), 190 us at M = 4. - w4a8i8_asym: int8 DPAS takes the tiled codes as its u4 B operand (nibble i meets A's byte i), so a prepass reorders the activation's bytes of each 8 evens first and forms each group's sum of codes; the group's integer sum is exact and its term ((sum - zero * asum) * a_scale) * scale is added in group order. - quant_act_i8g: quant_act_fp8's form at libref's int8 rule (top 127). rad-kbench and the op oracle cannot draw the zero-point grids, so XE2_W4_CHECK=n checks the first n calls of each form against libref's loop on the host, on the engine's operands. On the 27B: the exact form 0 of 81920 and 0 of 25600 outputs differ; w4a8i8 (M = 2048) 0 of 349568 and 0 of 327680; w4a16 (M = 4) 0 of 65536 and 2 of 20480 (bf16 ties, rel_l2 5e-9). quant_act_i8g: rad-kbench fixture 153/153 (quant_act*), the 27B 96/96, rel_l2 0. ctest -L sycl 3/3; the host tier's five failures are the baseline's (FFmpeg, CPU budgets). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
13 KiB
C++
248 lines
13 KiB
C++
/* 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.
|
|
*
|
|
* 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. */
|
|
#include "xe2_common.h"
|
|
|
|
#include <sycl/ext/intel/esimd.hpp>
|
|
|
|
namespace xe2 {
|
|
|
|
namespace {
|
|
|
|
namespace esimd = sycl::ext::intel::esimd;
|
|
namespace xmx = sycl::ext::intel::esimd::xmx;
|
|
using half = sycl::half;
|
|
|
|
/* f32 to bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */
|
|
inline esimd::simd<uint16_t, 16> to_bf16(esimd::simd<float, 16> v) SYCL_ESIMD_FUNCTION {
|
|
using namespace esimd;
|
|
simd<uint32_t, 16> u = v.template bit_cast_view<uint32_t>();
|
|
simd<uint32_t, 16> h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16;
|
|
h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u);
|
|
return simd<uint16_t, 16>(h);
|
|
}
|
|
|
|
template <int RC, bool PF, 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) {
|
|
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;
|
|
return submit([&] {
|
|
q.parallel_for(sycl::nd_range<1>{(size_t)(ng * kWg), (size_t)kWg}, [=](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;
|
|
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 {
|
|
#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<half, RC * 128> Ar; /* A2D: [pair of steps][step][row][16]; else [row][128] */
|
|
if constexpr (A2D) {
|
|
#pragma unroll
|
|
for (int j2 = 0; j2 < 4; ++j2)
|
|
Ar.template select<RC * 32, 1>(j2 * RC * 32) =
|
|
load_2d<half, 16, RC, 2, false>(a, sw, sh, spt, (int)(g * 128 + j2 * 32), 0);
|
|
} else {
|
|
#pragma unroll
|
|
for (int m = 0; m < RC; ++m) {
|
|
if (m < mm) Ar.template select<128, 1>(m * 128) = block_load<half, 128>(a + m * ars + g * 128);
|
|
else Ar.template select<128, 1>(m * 128) = 0;
|
|
}
|
|
}
|
|
simd<uint32_t, 16> zzd = (szw >> 16) | (szw & 0xFFFF0000u);
|
|
const simd<half, 32> zz = zzd.template bit_cast_view<half>();
|
|
simd<float, RC * 16> cg = 0.0f;
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
simd<half, 256> B;
|
|
#pragma unroll
|
|
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;
|
|
}
|
|
}
|
|
simd<half, RC * 16> A;
|
|
if constexpr (A2D) {
|
|
A = Ar.template select<RC * 16, 1>((j / 2) * RC * 32 + (j & 1) * RC * 16);
|
|
} else {
|
|
#pragma unroll
|
|
for (int m = 0; m < RC; ++m) A.template select<16, 1>(m * 16) = Ar.template select<16, 1>(m * 128 + 16 * j);
|
|
}
|
|
cg = xmx::dpas<8, RC, float>(cg, B, A);
|
|
}
|
|
simd<uint32_t, 16> lo = szw & 0xFFFFu;
|
|
simd<uint16_t, 16> s16 = lo;
|
|
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);
|
|
}
|
|
}
|
|
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));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
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;
|
|
namespace intelex = sycl::ext::intel::experimental;
|
|
syclex::properties props{intelex::grf_size<256>};
|
|
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 {
|
|
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;
|
|
const unsigned sw = (unsigned)(K - 1), sh = (unsigned)(M - 1);
|
|
simd<float, 128> acc[4][2];
|
|
#pragma unroll
|
|
for (int i = 0; i < 4; ++i)
|
|
#pragma unroll
|
|
for (int j = 0; j < 2; ++j) acc[i][j] = 0.0f;
|
|
for (int64_t g = 0; g < G; ++g) {
|
|
simd<int32_t, 128> c[4][2];
|
|
#pragma unroll
|
|
for (int i = 0; i < 4; ++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);
|
|
#pragma unroll
|
|
for (int i = 0; i < 4; ++i) {
|
|
const simd<uint32_t, 64> Ai = A4.template select<64, 1>(i * 64);
|
|
#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);
|
|
#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);
|
|
simd<uint32_t, 16> lo = w & 0xFFFFu, hi = w >> 16;
|
|
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;
|
|
#pragma unroll
|
|
for (int i = 0; i < 4; ++i)
|
|
#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;
|
|
}
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (int i = 0; i < 4; ++i)
|
|
#pragma unroll
|
|
for (int m = 0; m < 8; ++m) {
|
|
const int64_t row = r0 + i * 8 + m;
|
|
if (row >= M) continue;
|
|
#pragma unroll
|
|
for (int j = 0; j < 2; ++j) {
|
|
const int64_t n0 = (tile0 + j) * 16;
|
|
const simd<float, 16> v = acc[i][j].template select<16, 1>(m * 16);
|
|
if (ydt == RAD_F32) block_store<float, 16>((float*)yb + row * yrs + n0, v);
|
|
else block_store<uint16_t, 16>((uint16_t*)yb + row * yrs + n0, to_bf16(v));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
} // 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) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
} // namespace xe2
|