This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gemv_m2_esimd.cpp
T
claudeandClaude Opus 5.5 340231d007 libxe2: verify GEMM loads exponents and metadata four steps a gather -- down 142 -> 131 us
EU stall sampling (unitrace --stall-sampling) put 43% of the kernel's samples on send stalls, the
memory request queue full, not on waits for data: 4-byte gathers of the exponents and metadata
were a third of its cache-line requests for an eighth of its bytes. 16 bytes a lane now cover
four steps; a split's tail keeps the per-step gathers, so the arithmetic and splits are as
before. rad-kbench against libref 50/50; greedy output identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-25 14:21:03 +01:00

165 lines
10 KiB
C++

/* xe2_gemv_m2_esimd.cpp -- the m2xfp4 verify GEMM (2 <= M <= 16) in ESIMD: the register-DPAS form
* of xe2_gemv_m2.cpp (launch_dpas_raw) with the same arithmetic, written per hardware thread.
*
* Why ESIMD: in SYCL a lane's half2 scale multiply compiles to two SIMD16 fp16 multiplies plus four
* moves splitting and re-packing the pair, ~10 instructions a weight pair, and that ALU work -- not
* the weight stream -- set the kernel's time (probe m2v.cpp: 333 us at gate_up's shape, 193 us with
* the decode removed). Here a thread holds its 16 columns' code words as [word][lane] registers, a
* decoded word pair is one 32-wide fp16 multiply, and the loop body is ~4 integer ops a weight pair:
* 240 us at the same shape, bit-identical (probe m2e.cpp).
*
* Layout: a thread 16 output columns, lane = column, as the SIMT kernel's sub-group. Its codes are
* read 16 bytes a lane at a time (LSC d32x4 gathers, [dword][lane] in registers), so a word of 8
* nibbles is one register across the 16 columns. The activation is ae[(c / 16) * mpad + m][c % 16]
* (c the permuted column of xe2_gemv_m2.cpp's prepass, m the row): a DPAS A operand of 8 rows by 16
* k is one 256-byte block load. Results: y directly (f32 / bf16) when the whole K is one thread's,
* else partial sums into pbuf for the caller's reduction, exactly as launch_dpas_raw does. */
#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;
constexpr int kKs = 4; /* 32-groups a step: 64 bytes of a column's codes */
template <int MT>
int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t* meta0,
const uint8_t* expe0, const int32_t* emax, int64_t brs, float* pbuf, int64_t N, int64_t K,
int64_t e1, int64_t e2, int64_t mpad, int splits, const float* rr, char* yb, uint32_t ydt,
int64_t yrs, int64_t ycs, int mm, int64_t ldp) {
constexpr int kWg = 8; /* threads a work-group */
const int64_t ntile = (N + 15) / 16;
const int64_t nthr = ntile * splits, ng = (nthr + kWg - 1) / kWg;
const int64_t kg = K / 32;
const int64_t per = ((kg + splits - 1) / splits + kKs - 1) / kKs * kKs;
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 n0 = tile * 16;
const int64_t g0s = sp * per, g1s = kg < g0s + per ? kg : g0s + per;
const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2);
const half* abase = ae + part * K * mpad;
const simd<uint32_t, 16> lane(0, 1);
simd<uint32_t, 16> col = lane + (uint32_t)n0;
col.merge(simd<uint32_t, 16>((uint32_t)(N - 1)), col > (uint32_t)(N - 1));
const uint8_t* cbase = codes0 + n0 * (K / 2);
const simd<uint32_t, 16> coff = (col - (uint32_t)n0) * (uint32_t)(K / 2);
const simd<uint32_t, 16> eoff = col * (uint32_t)brs;
const simd<uint32_t, 16> moff = col * (uint32_t)kg;
/* the column's largest exponent, from the caller's prepass */
const simd<int32_t, 16> mx = gather<int32_t, 16>(emax, col * 4u);
simd<float, 128> c[MT];
_Pragma("unroll") for (int i = 0; i < MT; ++i) c[i] = 0.0f;
/* a step: kKs groups -- the code lines, the activation blocks, the decode and DPAS */
auto step = [&](int64_t g0, simd<uint32_t, 16> e4, simd<uint32_t, 16> m4) SYCL_ESIMD_FUNCTION {
simd<uint32_t, 256> line; /* [word 16][lane 16] */
_Pragma("unroll") for (int qd = 0; qd < 4; ++qd)
line.template select<64, 1>(qd * 64) =
gather<uint32_t, 64, 4>((const uint32_t*)(cbase + g0 * 16 + qd * 16), coff);
/* the step's activation, 8 k-blocks of [mpad rows][16 k], loaded with the codes */
simd<half, kKs * 2 * MT * 128> As;
_Pragma("unroll") for (int b = 0; b < kKs * 2 * MT; ++b)
As.template select<128, 1>(b * 128) = block_load<half, 128>(abase + (g0 * 2 * mpad + b * 8) * 16);
_Pragma("unroll") for (int gg = 0; gg < kKs; ++gg) {
const simd<int32_t, 16> d = simd<int32_t, 16>((e4 >> (8 * gg)) & 0xFF) - mx;
_Pragma("unroll") for (int hh = 0; hh < 2; ++hh) { /* 16 k: words 2 hh, 2 hh + 1 */
simd<half, 256> B;
_Pragma("unroll") for (int w = 0; w < 2; ++w) {
const int j = 2 * hh + w;
/* (1 + k/4) 2^(14 + d) by bits, exact in f32, then to fp16: the SIMT
* kernel's (half)((1 + 0.25 k) * ldexp(16384, d)), 0 below d = -30 */
const simd<uint32_t, 16> kf = (m4 >> (8 * gg + 2 * j)) & 3;
simd<uint32_t, 16> fb = (simd<uint32_t, 16>(d + 141) << 23) | (kf << 21);
simd<float, 16> khf = fb.template bit_cast_view<float>();
khf.merge(simd<float, 16>(0.0f), d < -30);
const simd<half, 16> kh = khf;
simd<half, 32> kh2;
kh2.template select<16, 2>(0) = kh;
kh2.template select<16, 2>(1) = kh;
const simd<uint32_t, 16> wv = line.template select<16, 1>((gg * 4 + j) * 16);
_Pragma("unroll") for (int u = 0; u < 4; ++u) {
simd<uint32_t, 16> dw = u < 3 ? ((wv << (9 - 4 * u)) & 0x0E000E00u) : ((wv >> 3) & 0x0E000E00u);
dw |= (wv << (12 - 4 * u)) & 0x80008000u;
const simd<half, 32> hv = dw.template bit_cast_view<half>();
B.template select<32, 1>((w * 4 + u) * 32) = hv * kh2;
}
}
_Pragma("unroll") for (int i = 0; i < MT; ++i) {
const simd<half, 128> A = As.template select<128, 1>(((gg * 2 + hh) * MT + i) * 128);
c[i] = xmx::dpas<8, 8, float>(c[i], B, A);
}
}
}
};
/* the exponents and metadata 16 bytes a lane, four steps' worth a gather (a 4-byte gather
* a step was a third of the kernel's memory messages for an eighth of its bytes); the
* tail of a split that is not 16 groups long a step at a time */
int64_t g0 = g0s;
for (; g0 + 4 * kKs <= g1s; g0 += 4 * kKs) {
simd<uint32_t, 64> E = gather<uint32_t, 64, 4>((const uint32_t*)(expe0 + g0), eoff);
simd<uint32_t, 64> Mt = gather<uint32_t, 64, 4>((const uint32_t*)(meta0 + g0), moff);
_Pragma("unroll") for (int s4 = 0; s4 < 4; ++s4)
step(g0 + kKs * s4, E.template select<16, 1>(s4 * 16), Mt.template select<16, 1>(s4 * 16));
}
for (; g0 < g1s; g0 += kKs)
step(g0, gather<uint32_t, 16>((const uint32_t*)(expe0 + g0), eoff),
gather<uint32_t, 16>((const uint32_t*)(meta0 + g0), moff));
const simd_mask<16> ok = (lane + (uint32_t)n0) < (uint32_t)N;
if (splits == 1) {
/* 2^(mx - 127) by bits (mx = 0: the subnormal 2^-127) */
simd<uint32_t, 16> sb = simd<uint32_t, 16>(mx) << 23;
sb.merge(simd<uint32_t, 16>(0x00400000u), mx == 0);
const simd<float, 16> sc = sb.template bit_cast_view<float>();
const uint32_t esz = ydt == RAD_F32 ? 4u : 2u;
const simd<uint32_t, 16> yoff = lane * (uint32_t)(ycs * esz);
_Pragma("unroll") for (int i = 0; i < MT; ++i)
_Pragma("unroll") for (int m = 0; m < 8; ++m) {
const int r = i * 8 + m;
if (r >= mm) continue;
simd<float, 16> v = c[i].template select<16, 1>(m * 16) * rr[r] * sc;
char* yrow = yb + (r * yrs + n0 * ycs) * (int64_t)esz;
if (ydt == RAD_F32) {
scatter<float, 16>((float*)yrow, yoff, v, ok);
} 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);
const simd<uint16_t, 16> h16 = h;
scatter<uint16_t, 16>((uint16_t*)yrow, yoff, h16, ok);
}
}
} else {
_Pragma("unroll") for (int i = 0; i < MT; ++i)
_Pragma("unroll") for (int m = 0; m < 8; ++m) {
const int64_t r = (int64_t)i * 8 + m;
scatter<float, 16>(pbuf + (sp * mpad + r) * ldp + n0, lane * 4u,
c[i].template select<16, 1>(m * 16), ok);
}
}
});
});
}
} // namespace
int m2_esimd(sycl::queue& q, int mt, const half* ae, const uint8_t* codes0, const uint8_t* meta0,
const uint8_t* expe0, const int32_t* emax, int64_t brs, float* pbuf, int64_t N, int64_t K,
int64_t e1, int64_t e2, int64_t mpad, int splits, const float* rr, char* yb, uint32_t ydt,
int64_t yrs, int64_t ycs, int mm, int64_t ldp) {
return mt == 1 ? launch<1>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb,
ydt, yrs, ycs, mm, ldp)
: launch<2>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb,
ydt, yrs, ycs, mm, ldp);
}
} // namespace xe2