Archived
libxe2: fast decode kernels for Qwen3.8-27B-PARO-M2XFP4 -- 0.1 -> 12.1 tok/s on the B65
- m2xfp4 GEMV (M <= 16): e2m1 decoded to fp16 by bits alone ((mag << 9) | sign << 15 is exactly e2m1 * 2^-14; the card keeps fp16 subnormals), K permuted (u, u + 4) inside each 8 so a code pair is one shift and mask, the activation as fp16(a * a_scale / R * 2^14) in that order, half2 subgroup and group sums, f32 across groups; four columns a sub-group at decode. The scalar form was ALU-bound (~10 ops a weight, 80 ms a token); this one runs at 415-525 GB/s on the 27B's shapes in a standalone probe. rad-kbench against libref on the container: rel_l2 ~5e-5 to ~3e-3, tolerance 4e-2. - m2xfp4 prefill (M >= 17): the weight decoded to bf16 (exact) and the activation to bf16(e4m3 * scale), then the bf16 XMX GEMM per part. - givens_dense_quant: a sub-group an output, the rotation row read coalesced, libref's double sum as a float pair (two-product and two-sum, ~48 bits); the double form stays for XE2_EXACT=1. 146 -> 5.4 ms a token. - the joined fp8 head (logits_gemm, M <= 16): E4M3 to fp16 by bits ((b & 0x7F) << 7 is E4M3 * 2^-8), bytes (0, 2), (1, 3) as half pairs; an i8 head or a narrow scale block element by element. 43 -> 3.9 ms. - gated delta net, still bit-identical to libref: the decode step split over 16-column blocks (384 work-groups, not 48) with the gated norm in a second kernel and the anchor replay its own launch (block 0 rewrites the factors other blocks read); the chunked scan and the kkt solve work-group parallel; persistent staging (workspace slots) instead of a malloc and a wait a call. - rmsnorm_quant_fp8: the row in local memory, loads issued before stores, the ordered sum fed by sub-group broadcasts -- still libref's chain (a probe puts that chain's floor at ~32 us for 5120). - round_dt converts the float dtypes directly. rad-kbench: 1410/1410 on the fixture; the 27B container's M <= 32 bands 77/77. Decode (greedy, no speculation) 12.1 tok/s, coherent text; with the DFlash2 drafter 5.3 tok/s and the same text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
1 parent
4921641578
commit
07cdc546a3
9 files changed
+1030
-35
No files matched your search
+77
-7
@@ -28,6 +28,7 @@ namespace xe2 {
|
||||
namespace {
|
||||
|
||||
constexpr size_t kWg = 256; /* work-items a row */
|
||||
constexpr int64_t kRowMax = 8192; /* a row rmsnorm_quant_fp8 keeps in local memory */
|
||||
|
||||
/* sigmoid exactly as libref's act_sigmoid: exp in double, rounded once to f32 (ref_exp). An f32
|
||||
* exp is within an ulp, inside rad-kbench's tolerance, but an ulp in silu now and then moves the
|
||||
@@ -55,14 +56,16 @@ inline float ordered_sumsq(const sycl::nd_item<1>& it, const F& src, int64_t n)
|
||||
sq[i] = v * v;
|
||||
}
|
||||
sycl::group_barrier(grp);
|
||||
if (t == 0) {
|
||||
/* Eight independent loads a step, then the eight adds in index order: the same sum,
|
||||
* without waiting on one local-memory load per addition. */
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
if (sg.get_group_linear_id() == 0) {
|
||||
/* Sub-group 0 takes the squares sixteen at a time, one a lane, and every lane adds them
|
||||
* in index order from the lanes' registers: the same chain of additions as libref's
|
||||
* serial loop, without a local-memory load per addition. */
|
||||
const int lane = (int)sg.get_local_linear_id();
|
||||
int64_t i = 0;
|
||||
for (; i + 8 <= cn; i += 8) {
|
||||
float b[8];
|
||||
for (int j = 0; j < 8; ++j) b[j] = sq[i + j];
|
||||
for (int j = 0; j < 8; ++j) ss += b[j];
|
||||
for (; i + kSub <= cn; i += kSub) {
|
||||
const float v = sq[i + lane];
|
||||
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
|
||||
}
|
||||
for (; i < cn; ++i) ss += sq[i];
|
||||
}
|
||||
@@ -245,6 +248,73 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
constexpr int kSubs = (int)(kWg / kSub);
|
||||
if (n <= kRowMax) {
|
||||
/* A row up to kRowMax in local memory: the (narrowed) residual sum and its squares are
|
||||
* formed once from one read of x and the residual, the sum stored once; sub-group 0 adds
|
||||
* the squares in index order through register broadcasts (libref's chain); the groups
|
||||
* read their values from local memory. The same arithmetic as the general form below,
|
||||
* without re-reading what the work-group just stored. */
|
||||
return submit([&] {
|
||||
q->submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<float, 1> sv_(sycl::range<1>((size_t)n), h);
|
||||
sycl::local_accessor<float, 1> sq(sycl::range<1>((size_t)n), h);
|
||||
sycl::local_accessor<float, 1> wl(sycl::range<1>((size_t)n), h);
|
||||
h.parallel_for(sycl::nd_range<1>{(size_t)rows * kWg, kWg},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const int64_t r = (int64_t)it.get_group(0);
|
||||
const int t = (int)it.get_local_id(0);
|
||||
auto grp = it.get_group();
|
||||
/* Every load of the work-item first, then the stores: interleaved, each load would
|
||||
* wait for the store before it (the residual may alias x as far as the compiler
|
||||
* knows), a memory round trip an element. */
|
||||
constexpr int kPer = (int)(kRowMax / kWg);
|
||||
float xv[kPer], rv[kPer], wv[kPer];
|
||||
for (int j = 0; j < kPer; ++j) {
|
||||
const int64_t i = t + (int64_t)j * kWg;
|
||||
if (i >= n) break;
|
||||
xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
|
||||
rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + i) : 0.0f;
|
||||
wv[j] = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd;
|
||||
}
|
||||
for (int j = 0; j < kPer; ++j) {
|
||||
const int64_t i = t + (int64_t)j * kWg;
|
||||
if (i >= n) break;
|
||||
float v = xv[j];
|
||||
if (has_res) {
|
||||
const float s0 = v + rv[j];
|
||||
rad_store_f32(res.base, res.dtype, r * res.pitch + i, s0);
|
||||
v = round_dt(res.dtype, s0); /* what reading it back gives */
|
||||
}
|
||||
sv_[i] = v;
|
||||
sq[i] = v * v;
|
||||
wl[i] = wv[j];
|
||||
}
|
||||
sycl::group_barrier(grp);
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
float ss = 0.0f;
|
||||
if (sg.get_group_linear_id() == 0) {
|
||||
const int lane = (int)sg.get_local_linear_id();
|
||||
int64_t i = 0;
|
||||
for (; i + kSub <= n; i += kSub) {
|
||||
const float v = sq[i + lane];
|
||||
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
|
||||
}
|
||||
for (; i < n; ++i) ss += sq[i];
|
||||
}
|
||||
ss = sycl::group_broadcast(grp, ss, 0);
|
||||
const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
||||
auto v = [&](int64_t i) { return rad_bf16_to_f32(rad_f32_to_bf16(sv_[i] * rs * wl[i])); };
|
||||
const int sgi = (int)sg.get_group_linear_id();
|
||||
for (int64_t gi = sgi; gi < ngrp; gi += kSubs) {
|
||||
const int64_t k0 = gi * g, len = sycl::min(g, n - k0);
|
||||
const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch,
|
||||
has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch);
|
||||
if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
|
||||
Reference in new issue
Block a user