diff --git a/libxe2/xe2_gemm_zp.cpp b/libxe2/xe2_gemm_zp.cpp index 745c9fd9..30397ebf 100644 --- a/libxe2/xe2_gemm_zp.cpp +++ b/libxe2/xe2_gemm_zp.cpp @@ -96,13 +96,53 @@ int64_t row_stride(const RadTensor* t, int64_t cols) { return t->stride[t->rank - 2]; } +constexpr const char* kTagU2 = "xe2.zp.u2.t16"; + +/* Tiling the u2 codes plane into its stored form and back (zp_tiled, xe2_ops.h). */ +void tile_u2(const uint8_t* rows, uint32_t* t, int64_t N, int64_t K, bool to_tiled) { + const int64_t rb = K / 4; + for (int64_t n = 0; n < N; ++n) + for (int64_t s = 0; s < K / 32; ++s) + for (int r = 0; r < 2; ++r) { + uint32_t& w = t[zpt_dword(n, s, r, K)]; + if (to_tiled) w = 0; + for (int u = 0; u < 16; ++u) { + const int pos = 16 * r + u; + const int64_t k = s * 32 + ((pos & ~12) | ((pos & 4) << 1) | ((pos & 8) >> 1)); + if (to_tiled) w |= rad_load_bits(rows + n * rb, 2, k) << (2 * u); + else rad_store_bits(const_cast(rows) + n * rb, 2, k, (w >> (2 * u)) & 3u); + } + } +} + } // namespace /* ------------------------------------------------------------------ the layout hooks */ /* libref's ref_layout_gemm_q, line for line: `b_scale` (operand 3) joined as "scale" and "zero", - * one each per (row, `group` of K), over codes of the grid's width. */ + * one each per (row, `group` of K), over codes of the grid's width. One addition: the u2 codes + * (operand 2) stored tiled where zp_tiled holds ("xe2.zp.u2.t16", xe2_ops.h), the int8 DPAS's u2 + * B operand as it lies; libref reads them as they are, so a weight libxe2 claims this way is + * libxe2's alone. */ int gemm_zp_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, RadLayout* out) { + if (operand == 2) { + const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr)); + if (!zg) return RAD_E_DTYPE; + if (zg->codes != RAD_U2 || n != 1 || !enc || sel[0] < 0 || sel[0] >= enc->n_planes || + std::strncmp(enc->plane[sel[0]].role, "codes", RAD_ENC_STR) != 0) + return RAD_E_UNSUPPORTED; + const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0); + const long long group = rad_param_geti(p, n_p, "group", 0); + if (!zp_tiled(2, N, K, group)) return RAD_E_UNSUPPORTED; + if (planes[0].shape[0] != N || planes[0].shape[1] != K) return RAD_E_SHAPE; + out->tag = kTagU2; + out->dtype = RAD_U2; + out->rank = 1; + out->shape[0] = N * K; + out->align = 0; + out->bytes = N * K / 4; + return RAD_OK; + } if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED; const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr)); const long long group = rad_param_geti(p, n_p, "group", 0); @@ -127,6 +167,11 @@ int gemm_zp_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* RadLayout lay{}; const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, &lay); if (st != RAD_OK) return st; + if (operand == 2) { + if (dst_bytes != lay.bytes) return RAD_E_SHAPE; + tile_u2((const uint8_t*)planes[0].data, (uint32_t*)dst, planes[0].shape[0], planes[0].shape[1], true); + return RAD_OK; + } return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes); } @@ -136,6 +181,12 @@ int gemm_zp_unrelayout(const RadParam* p, int n_p, int operand, const RadEncodin RadLayout lay{}; const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, &lay); if (st != RAD_OK) return st; + if (operand == 2) { + if (src_bytes != lay.bytes) return RAD_E_SHAPE; + tile_u2((const uint8_t*)planes[0].data, (uint32_t*)const_cast(src), planes[0].shape[0], + planes[0].shape[1], false); + return RAD_OK; + } return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n); } @@ -177,6 +228,7 @@ int gemm_nt_q_zp(const RadArgs* a, RadStream st) { const char *sab = (const char*)AS->data, *sumb = (const char*)ASUM->data; const uint32_t sadt = AS->dtype, sumdt = ASUM->dtype, ydt = Y->dtype; char* yb = (char*)Y->data; + const bool tl = zp_tiled(cbits, N, K, group); /* the stored form gemm_zp_layout asked for */ sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; const size_t wn = 64; @@ -197,7 +249,8 @@ int gemm_nt_q_zp(const RadArgs* a, RadStream st) { int64_t is = 0; for (int64_t k = g * group; k < (g + 1) * group; ++k) { const int64_t pos = (k & ~(int64_t)12) | ((k & 4) << 1) | ((k & 8) >> 1); - is += (int64_t)ar[pos] * (int32_t)rad_load_bits(crow, cbits, k); + const uint32_t code = tl ? zpt_code((const uint32_t*)bb, n, k, K) : rad_load_bits(crow, cbits, k); + is += (int64_t)ar[pos] * (int32_t)code; } const float sub = (float)is - (float)z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s); acc += sub * sa * sb; @@ -231,6 +284,7 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint int64_t K, int64_t group, int mm) { const int64_t G = K / group; const size_t groups = (size_t)((N + 15) / 16); + const bool tl = zp_tiled(cbits, N, K, group); return submit([&] { syclex::nd_launch(q, sycl::nd_range<1>{groups * 256, 256}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { @@ -251,7 +305,8 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint for (int m = 0; m < MM; ++m) is[m] = 0.0f; for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 16) { float cf[16]; - for (int j = 0; j < 16; ++j) cf[j] = (float)rad_load_bits(crow, cbits, k0 + j); + for (int j = 0; j < 16; ++j) + cf[j] = (float)(tl ? zpt_code((const uint32_t*)bb, n, k0 + j, K) : rad_load_bits(crow, cbits, k0 + j)); for (int m = 0; m < MM; ++m) { if (m >= mm) break; /* the step order: logical column j of the step sits at byte @@ -425,6 +480,12 @@ int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint } // namespace +/* xe2_gemm_zp_esimd.cpp */ +constexpr int64_t kZpEsimdMaxK = 8192; +int zp_esimd(sycl::queue& q, const int8_t* ab, int64_t ars, const float* sa, int64_t sa_rs, const float* asum, + int64_t mpad, const uint32_t* bt, const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, + int64_t N, int64_t K, int64_t group, int mm); + int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) { if (exact_only()) return gemm_nt_q_zp(a, st); if (!a || a->n_t < 5 || !a->t[0].data || !a->t[2].data || !a->t[4].data) return RAD_E_INVAL; @@ -457,7 +518,14 @@ int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) { const int64_t sa_rs = row_stride(AS, 1); const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1; const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1; - if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 2 == 0 && + const bool tl = zp_tiled(cbits, N, K, group); + /* tiled u2 codes: int8 DPAS with the codes as its u2 B operand (xe2_gemm_zp_esimd.cpp) */ + if (tl && M <= 8 && K <= kZpEsimdMaxK && AS->dtype == RAD_F32 && ASUM->dtype == RAD_F32 && asum_s == 1 && + (Y->dtype == RAD_F32 || Y->dtype == RAD_BF16) && rad_ptr_aligned(B->data, 64) && !std::getenv("XE2_ZP_GEMV")) + return zp_esimd(*q, (const int8_t*)A->data, ars, (const float*)AS->data, sa_rs, (const float*)ASUM->data, + mpad, (const uint32_t*)B->data, (const char*)BS->data, (char*)Y->data, Y->dtype, ys_r, + ys_c, N, K, group, (int)M); + if (!tl && M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 2 == 0 && brow % 16 == 0 && rad_ptr_aligned(B->data, 16) && !std::getenv("XE2_ZP_GEMV")) { auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>; diff --git a/libxe2/xe2_gemm_zp_esimd.cpp b/libxe2/xe2_gemm_zp_esimd.cpp new file mode 100644 index 00000000..4692fd24 --- /dev/null +++ b/libxe2/xe2_gemm_zp_esimd.cpp @@ -0,0 +1,115 @@ +/* xe2_gemm_zp_esimd.cpp -- the w2a8 head (the DFlash2 drafter's 2-bit vocabulary head, N = 248320) + * in ESIMD, over the u2 codes stored tiled (xe2_ops.h, zp_tiled). Int8 DPAS takes 2-bit B operands + * on this card, 16 codes a dword from the low end (probe dpas_u2.cpp), so the tiled codes are the B + * operand as they lie: no decode. A thread owns 16 columns; a group is group / 32 DPAS steps of one + * 128-byte block load each, against the activation staged once a work-group in local memory as the + * A operand's [step][row][32 bytes]. Each group's integer sum is exact in int32, and its term + * ((is - zero * a_sum) * a_scale) * scale is added in group order: zp_dpas's (and libref's) chain, + * so the result is the same bit for bit. Rows past M are staged as zeros and not stored. */ +#include "xe2_common.h" +#include "xe2_ops.h" + +#include "rad_planes.h" + +#include + +namespace xe2 { + +namespace { + +namespace esimd = sycl::ext::intel::esimd; +namespace xmx = sycl::ext::intel::esimd::xmx; + +constexpr int kWg = 16; /* threads a work-group */ +constexpr int kSlm = 8 * 8192; /* the staged activation: 8 rows of up to 8192 */ + +} // namespace + +int zp_esimd(sycl::queue& q, const int8_t* ab, int64_t ars, const float* sa, int64_t sa_rs, const float* asum, + int64_t mpad, const uint32_t* bt, const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, + int64_t N, int64_t K, int64_t group, int mm) { + const int64_t ntile = N / 16, ng = (ntile + kWg - 1) / kWg; + const int64_t steps = K / 32, gs = group / 32, G = K / group; + 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; + slm_init(); + const int tl = (int)it.get_local_id(0); + const int64_t tile = (int64_t)it.get_group(0) * kWg + tl; + /* stage the activation: thread tl the 256-byte runs tl, tl + 16, ... of the rows, + * each run eight steps of one row, scattered to [step][row][32 bytes] */ + const int64_t runs = K / 256; + const simd di(0, 1); + for (int64_t j = tl; j < 8 * runs; j += kWg) { + const int64_t m = j % 8, rn = j / 8; + simd v = 0; + if (m < mm) v = block_load((const uint32_t*)(ab + m * ars + rn * 256)); + const simd off = (((uint32_t)(rn * 8) + (di >> 3)) * 8u + (uint32_t)m) * 32u + (di & 7u) * 4u; + slm_scatter(off, v); + } + barrier(); + if (tile >= ntile) return; + /* the joined b_scale: entry 0 the f16 scales, entry 1 the zeros (gemm_zp_layout) */ + const RadPlanesEntry* e = (const RadPlanesEntry*)(bsb + sizeof(RadPlanesHeader)); + const int64_t s_off = e[0].offset, s_rb = e[0].row_bytes; + const int64_t z_off = e[1].offset, z_rb = e[1].row_bytes; + const int zbits = e[1].dtype == RAD_U8 ? 8 : e[1].dtype == RAD_U5 ? 5 : e[1].dtype == RAD_U4 ? 4 : 2; + const simd lane(0, 1); + const uint8_t* sbase = (const uint8_t*)bsb + s_off + tile * 16 * s_rb; + const uint8_t* zbase = (const uint8_t*)bsb + z_off + tile * 16 * z_rb; + const simd soff = lane * (uint32_t)s_rb, zoff = lane * (uint32_t)z_rb; + float sam[8]; + _Pragma("unroll") for (int m = 0; m < 8; ++m) sam[m] = m < mm ? sa[m * sa_rs] : 0.0f; + simd acc = 0.0f; /* [row][column] */ + const uint32_t* tb = bt + tile * steps * 32; + for (int64_t g = 0; g < G; ++g) { + simd c = 0; + for (int64_t s = 0; s < gs; ++s) { + const int64_t st = g * gs + s; + const simd B = block_load(tb + st * 32); + const simd A = slm_block_load((uint32_t)(st * 256)); + c = xmx::dpas<8, 8, int32_t, int32_t, uint32_t, int32_t, xmx::dpas_argument_type::u2, + xmx::dpas_argument_type::s8>(c, B, A); + } + simd sh = gather((const uint16_t*)(sbase + g * 2), soff); + const simd sb = simd(sh.template bit_cast_view()); + simd zf; + if (zbits == 8) { + zf = simd(gather(zbase + g, zoff)); + } else { + const int64_t o = g * zbits; + const int sh2 = (int)(o & 7); + simd w = simd(gather(zbase + (o >> 3), zoff)); + if (sh2 + zbits > 8) w |= simd(gather(zbase + (o >> 3) + 1, zoff)) << 8; + zf = simd((w >> sh2) & ((1u << zbits) - 1u)); + } + _Pragma("unroll") for (int m = 0; m < 8; ++m) { + if (m >= mm) break; + const float as = asum[g * mpad + m]; + const simd cf = c.template select<16, 1>(m * 16); + const simd sub = cf - zf * as; + const simd term = sub * sam[m] * sb; + acc.template select<16, 1>(m * 16) += term; + } + } + const int64_t n0 = tile * 16; + const uint32_t esz = ydt == RAD_F32 ? 4u : 2u; + const simd yoff = lane * (uint32_t)(ys_c * esz); + _Pragma("unroll") for (int m = 0; m < 8; ++m) { + if (m >= mm) break; + simd v = acc.template select<16, 1>(m * 16); + char* yrow = yb + (m * ys_r + n0 * ys_c) * (int64_t)esz; + if (ydt == RAD_F32) { + scatter((float*)yrow, yoff, v); + } else { /* bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */ + const simd u = v.template bit_cast_view(); + simd h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16; + h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u); + scatter((uint16_t*)yrow, yoff, simd(h)); + } + } + }); + }); +} + +} // namespace xe2 diff --git a/libxe2/xe2_ops.h b/libxe2/xe2_ops.h index 544cff29..355e7d72 100644 --- a/libxe2/xe2_ops.h +++ b/libxe2/xe2_ops.h @@ -117,4 +117,22 @@ inline int64_t m2e_off(int64_t n, int64_t g, int64_t kg) { } inline int64_t m2_emax_off(int64_t N, int64_t kg) { return (N * kg + 63) / 64 * 64; } +/* The zero-point GEMM's u2 codes tiled (xe2_gemm_zp.cpp, tag "xe2.zp.u2.t16") where N % 16 == 0, + * K % 32 == 0 and the group a multiple of 32: a 16-column tile's 32-column step is 128 bytes, + * dword [r][lane] holding the column's codes for the activation's stored positions 16 r .. 16 r + + * 15, two bits each from the low end -- the u2 B operand of an int8 DPAS as it lies. The + * activation is quant_act_i8's step order (position p holds column p with bits 2 and 3 swapped), + * so dword r's code u is logical column 32 s + swap23(16 r + u). zpt_code reads logical column k. */ +inline bool zp_tiled(int cbits, int64_t N, int64_t K, int64_t group) { + return cbits == 2 && N > 0 && N % 16 == 0 && K > 0 && K % 32 == 0 && group > 0 && group % 32 == 0 && K % group == 0; +} +inline int64_t zpt_dword(int64_t n, int64_t s, int r, int64_t K) { + return (((n >> 4) * (K / 32) + s) * 2 + r) * 16 + (n & 15); +} +inline uint32_t zpt_code(const uint32_t* t, int64_t n, int64_t k, int64_t K) { + const int x = (int)(k & 31); + const int p = (x & ~12) | ((x & 4) << 1) | ((x & 8) >> 1); + return (t[zpt_dword(n, k >> 5, p >> 4, K)] >> (2 * (p & 15))) & 3u; +} + } // namespace xe2