Archived
Work-groups of 16 threads staged the 40 KB activation into local memory for 16 tiles each; 64 threads amortise it over four times the columns (16: 1.66 ms, 32: 0.96), and a group of 128 reads its four steps' codes as one 512-byte block load (32: 0.77, 64: 0.74 ms in the round, unitrace -d). Checked as before: 20 calls on the engine's inputs against the SIMT GEMV, 0 of 1738240 outputs differ each; greedy output identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
134 lines
7.8 KiB
C++
134 lines
7.8 KiB
C++
/* 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 <sycl/ext/intel/esimd.hpp>
|
|
|
|
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 kWg, bool G4>
|
|
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<kSlm>();
|
|
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<uint32_t, 64> di(0, 1);
|
|
for (int64_t j = tl; j < 8 * runs; j += kWg) {
|
|
const int64_t m = j % 8, rn = j / 8;
|
|
simd<uint32_t, 64> v = 0;
|
|
if (m < mm) v = block_load<uint32_t, 64>((const uint32_t*)(ab + m * ars + rn * 256));
|
|
const simd<uint32_t, 64> off = (((uint32_t)(rn * 8) + (di >> 3)) * 8u + (uint32_t)m) * 32u + (di & 7u) * 4u;
|
|
slm_scatter<uint32_t, 64>(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<uint32_t, 16> 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<uint32_t, 16> 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<float, 128> acc = 0.0f; /* [row][column] */
|
|
const uint32_t* tb = bt + tile * steps * 32;
|
|
for (int64_t g = 0; g < G; ++g) {
|
|
simd<int32_t, 128> c = 0;
|
|
if constexpr (G4) { /* group = 128: its four steps' codes one 512-byte load */
|
|
simd<uint32_t, 128> B4 = block_load<uint32_t, 128>(tb + g * 128);
|
|
_Pragma("unroll") for (int s = 0; s < 4; ++s) {
|
|
const simd<int32_t, 64> A = slm_block_load<int32_t, 64>((uint32_t)((g * 4 + s) * 256));
|
|
const simd<uint32_t, 32> 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<uint32_t, 32> B = block_load<uint32_t, 32>(tb + st * 32);
|
|
const simd<int32_t, 64> A = slm_block_load<int32_t, 64>((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<uint16_t, 16> sh = gather<uint16_t, 16>((const uint16_t*)(sbase + g * 2), soff);
|
|
const simd<float, 16> sb = simd<sycl::half, 16>(sh.template bit_cast_view<sycl::half>());
|
|
simd<float, 16> zf;
|
|
if (zbits == 8) {
|
|
zf = simd<float, 16>(gather<uint8_t, 16>(zbase + g, zoff));
|
|
} else {
|
|
const int64_t o = g * zbits;
|
|
const int sh2 = (int)(o & 7);
|
|
simd<uint32_t, 16> w = simd<uint32_t, 16>(gather<uint8_t, 16>(zbase + (o >> 3), zoff));
|
|
if (sh2 + zbits > 8) w |= simd<uint32_t, 16>(gather<uint8_t, 16>(zbase + (o >> 3) + 1, zoff)) << 8;
|
|
zf = simd<float, 16>((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<float, 16> cf = c.template select<16, 1>(m * 16);
|
|
const simd<float, 16> sub = cf - zf * as;
|
|
const simd<float, 16> 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<uint32_t, 16> yoff = lane * (uint32_t)(ys_c * esz);
|
|
_Pragma("unroll") for (int m = 0; m < 8; ++m) {
|
|
if (m >= mm) break;
|
|
simd<float, 16> 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, 16>((float*)yrow, yoff, v);
|
|
} else { /* bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */
|
|
const 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);
|
|
scatter<uint16_t, 16>((uint16_t*)yrow, yoff, simd<uint16_t, 16>(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
|