/* 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 (a * group of 128 one 512-byte block load), against the activation staged once a work-group of 64 * threads 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 kSlm = 8 * 8192; /* the staged activation: 8 rows of up to 8192 */ template int launch(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; if constexpr (G4) { /* group = 128: its four steps' codes one 512-byte load */ simd B4 = block_load(tb + g * 128); _Pragma("unroll") for (int s = 0; s < 4; ++s) { const simd A = slm_block_load((uint32_t)((g * 4 + s) * 256)); const simd B = B4.template select<32, 1>(s * 32); 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); } } else 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 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) { /* 64 threads a work-group share one staging of the activation (16: 1.66 ms a call, 32: 0.96, * and with a group's codes one 512-byte load, 32: 0.77, 64: 0.74) */ if (group == 128) return launch<64, true>(q, ab, ars, sa, sa_rs, asum, mpad, bt, bsb, yb, ydt, ys_r, ys_c, N, K, group, mm); return launch<64, false>(q, ab, ars, sa, sa_rs, asum, mpad, bt, bsb, yb, ydt, ys_r, ys_c, N, K, group, mm); } } // namespace xe2