Archived
libxe2: the int8 lm_head in ESIMD -- 96 ms -> ~2.5 ms a call at N = 248320, K = 5120
An int4 release's head is int8 codes with a bf16 scale per (row, 128 of K), joined. head_dpas reads E4M3 only, and its fallback walked a column a lane in f32: 96 ms a call, 35 of a decode token's 61 ms (profile_decode.sh). The ESIMD form reads 16 columns' rows 16 bytes a lane (d32x4 gathers), turns a byte into fp16 exactly as (b ^ 0x80) | 0x6400 less 1152, paired (0, 2) and (1, 3) as the fp8 path pairs its bytes so the prepass's activation serves both, and sums a 128-block on fp16 DPAS scaled once. head_dpas leaves exactly this form to it (head_i8_fast); both read the planes on the card. rad-kbench on qwen3.8-27b-w4a16-rtn: logits_gemm 28/28 (worst rel_l2 1.7e-3, tolerance 8e-3). Decode, story prompt with the MTP drafter: 15.0 -> 43.2 tok/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
efdfc4a570
commit
e85124c0f0
2 files changed
+168
-1
No files matched your search
+12
-1
@@ -286,6 +286,7 @@ int head_dpas(sycl::queue& q, const sycl::half* a16, const float* rr, const char
|
||||
if (n0 >= N) return; /* uniform */
|
||||
const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale");
|
||||
if (!cp.p || !sp.p) return;
|
||||
if (head_i8_fast(cp.dt, cp.row_bytes, sp.br, sp.bc, sp.dt, K)) return; /* xe2_head_esimd.cpp's */
|
||||
const int64_t n = sycl::min(n0 + lane, N - 1);
|
||||
const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes;
|
||||
if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 64 == 0) || cp.row_bytes % 16 ||
|
||||
@@ -352,6 +353,10 @@ int head_dpas(sycl::queue& q, const sycl::half* a16, const float* rr, const char
|
||||
});
|
||||
}
|
||||
|
||||
/* xe2_head_esimd.cpp */
|
||||
int head_i8_esimd(sycl::queue& q, int rc, const sycl::half* a16, const float* rr, const char* bb, char* yb,
|
||||
uint32_t ydt, int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm);
|
||||
|
||||
int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
|
||||
if (exact_only()) return logits_gemm_head(a, st);
|
||||
if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL;
|
||||
@@ -402,7 +407,13 @@ int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
|
||||
const char* bb = (const char*)B->data;
|
||||
char* yb = (char*)Y->data;
|
||||
const int mm = (int)M;
|
||||
if (dpas) return head_dpas(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
if (dpas) {
|
||||
/* head_dpas serves the E4M3 head and every form but the int8 one, which it leaves to the
|
||||
* ESIMD kernel (each reads the planes on the card; a form not its own returns at once) */
|
||||
if (int r = head_dpas(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm); r != RAD_OK) return r;
|
||||
return head_i8_esimd(*q, mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8)), a16, rr, bb, yb, Y->dtype, N, K, yrs,
|
||||
ycs, mm);
|
||||
}
|
||||
if (mm == 1) return head_gemv<1>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
if (mm <= 8) return head_gemv<8>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
return head_gemv<16>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
/* xe2_head_esimd.cpp -- the lm_head of an int4 release, int8 codes with one float scale per (row,
|
||||
* 128 of K) joined (head_layout), at M <= 8, in ESIMD; logits_gemm_head_gemv launches it after
|
||||
* head_dpas, which leaves this form to it (head_i8_fast, xe2_ops.h). head_dpas's slow fallback
|
||||
* walked a column a lane in f32: 96 ms a call at N = 248320, K = 5120.
|
||||
*
|
||||
* A thread owns 16 vocab columns. Their rows are read 16 bytes a lane at a time (LSC d32x4
|
||||
* gathers), so a dword of four codes is one register across the 16 columns; an int8 byte becomes
|
||||
* fp16 exactly as (b ^ 0x80) | 0x6400 = 1152 + b less 1152, the bytes paired (0, 2) and (1, 3) as
|
||||
* head_dpas pairs its E4M3 bytes -- the prepass's activation a16 is permuted to match. Eight fp16
|
||||
* DPAS of RC rows a 128-block sum into f32, scaled by the block's scale once; the row factor of the
|
||||
* prepass (a16 = x * 256 / R) comes off at the store. Not libref's order (it scales each element):
|
||||
* the gemm tolerance, as head_dpas. */
|
||||
#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;
|
||||
using half = sycl::half;
|
||||
|
||||
struct HeadPlane {
|
||||
int64_t off = -1, row_bytes = 0, br = 0, bc = 0;
|
||||
uint32_t dt = 0;
|
||||
};
|
||||
|
||||
inline bool role_eq(const char* r, const char* want) {
|
||||
for (int i = 0; i < RAD_ENC_STR; ++i) {
|
||||
if (r[i] != want[i]) return false;
|
||||
if (!want[i]) return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline HeadPlane head_plane(const char* base, const char* role) {
|
||||
HeadPlane v;
|
||||
const RadPlanesHeader* h = (const RadPlanesHeader*)base;
|
||||
if (h->magic != RAD_PLANES_MAGIC) return v;
|
||||
const RadPlanesEntry* e = (const RadPlanesEntry*)(base + sizeof(RadPlanesHeader));
|
||||
for (int i = 0; i < h->n_planes && i < RAD_ENC_MAX_PLANES; ++i)
|
||||
if (role_eq(e[i].role, role)) {
|
||||
v.off = e[i].offset;
|
||||
v.dt = e[i].dtype;
|
||||
v.row_bytes = e[i].row_bytes;
|
||||
v.br = e[i].block[0];
|
||||
v.bc = e[i].block[1];
|
||||
return v;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <int RC>
|
||||
int launch(sycl::queue& q, const half* a16, const float* rr, const char* bb, char* yb, uint32_t ydt, int64_t N,
|
||||
int64_t K, int64_t yrs, int64_t ycs, int mm) {
|
||||
const int64_t ntile = (N + 15) / 16;
|
||||
constexpr int kWg = 8;
|
||||
const int64_t ng = (ntile + 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 tile = (int64_t)it.get_global_id(0);
|
||||
if (tile >= ntile) return;
|
||||
const HeadPlane cp = head_plane(bb, "codes"), sp = head_plane(bb, "scale");
|
||||
if (cp.off < 0 || sp.off < 0 || !head_i8_fast(cp.dt, cp.row_bytes, sp.br, sp.bc, sp.dt, K)) return;
|
||||
const int64_t n0 = tile * 16;
|
||||
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 char* cbase = bb + cp.off;
|
||||
const simd<uint32_t, 16> coff = col * (uint32_t)cp.row_bytes;
|
||||
const simd<uint32_t, 16> soff = col * (uint32_t)sp.row_bytes;
|
||||
const char* sbase = bb + sp.off;
|
||||
const uint32_t sesz = sp.dt == RAD_F32 ? 4u : 2u;
|
||||
simd<float, RC * 16> tot = 0.0f;
|
||||
const simd<half, 32> bias = 1152.0f;
|
||||
for (int64_t k0 = 0; k0 < K; k0 += 128) {
|
||||
simd<uint32_t, 512> line; /* [dword 32][lane 16]: the block's 128 codes a column */
|
||||
#pragma unroll
|
||||
for (int qd = 0; qd < 8; ++qd)
|
||||
line.template select<64, 1>(qd * 64) = gather<uint32_t, 64, 4>((const uint32_t*)(cbase + k0 + qd * 16), coff);
|
||||
simd<half, RC * 128> Ar;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < RC; ++m) Ar.template select<128, 1>(m * 128) = block_load<half, 128>(a16 + m * K + k0);
|
||||
simd<float, 16> sc;
|
||||
if (sesz == 4) {
|
||||
sc = gather<float, 16>((const float*)(sbase + (k0 / 128) * 4), soff);
|
||||
} else {
|
||||
simd<uint16_t, 16> s16 = gather<uint16_t, 16>((const uint16_t*)(sbase + (k0 / 128) * 2), soff);
|
||||
if (sp.dt == RAD_F16) {
|
||||
sc = simd<half, 16>(s16.template bit_cast_view<half>());
|
||||
} else {
|
||||
simd<uint32_t, 16> s32 = simd<uint32_t, 16>(s16) << 16;
|
||||
sc = s32.template bit_cast_view<float>();
|
||||
}
|
||||
}
|
||||
simd<float, RC * 16> cg = 0.0f;
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
simd<half, 256> B;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const simd<uint32_t, 16> c = line.template select<16, 1>((4 * s + j) * 16);
|
||||
simd<uint32_t, 16> x = ((c & 0x00FF00FFu) ^ 0x00800080u) | 0x64006400u;
|
||||
simd<uint32_t, 16> y = (((c >> 8) & 0x00FF00FFu) ^ 0x00800080u) | 0x64006400u;
|
||||
B.template select<32, 1>((2 * j) * 32) = x.template bit_cast_view<half>() - bias;
|
||||
B.template select<32, 1>((2 * j + 1) * 32) = y.template bit_cast_view<half>() - bias;
|
||||
}
|
||||
simd<half, RC * 16> A;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < RC; ++m) A.template select<16, 1>(m * 16) = Ar.template select<16, 1>(m * 128 + 16 * s);
|
||||
cg = xmx::dpas<8, RC, float>(cg, B, A);
|
||||
}
|
||||
#pragma unroll
|
||||
for (int m = 0; m < RC; ++m) tot.template select<16, 1>(m * 16) += cg.template select<16, 1>(m * 16) * sc;
|
||||
}
|
||||
const simd_mask<16> ok = (lane + (uint32_t)n0) < (uint32_t)N;
|
||||
const uint32_t esz = ydt == RAD_F32 ? 4u : 2u;
|
||||
const simd<uint32_t, 16> yoff = lane * (uint32_t)(ycs * esz);
|
||||
#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) * (rr[m] * (1.0f / 256.0f));
|
||||
char* yrow = yb + (m * 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 */
|
||||
simd<float, 16> vv = v;
|
||||
simd<uint32_t, 16> u = vv.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), ok);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int head_i8_esimd(sycl::queue& q, int rc, const half* a16, const float* rr, const char* bb, char* yb, uint32_t ydt,
|
||||
int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm) {
|
||||
switch (rc) {
|
||||
case 1: return launch<1>(q, a16, rr, bb, yb, ydt, N, K, yrs, ycs, mm);
|
||||
case 2: return launch<2>(q, a16, rr, bb, yb, ydt, N, K, yrs, ycs, mm);
|
||||
case 4: return launch<4>(q, a16, rr, bb, yb, ydt, N, K, yrs, ycs, mm);
|
||||
default: return launch<8>(q, a16, rr, bb, yb, ydt, N, K, yrs, ycs, mm);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
Reference in new issue
Block a user