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
3158324bdb
commit
15944be7e3
9 files changed
+1030
-35
No files matched your search
+83
-2
@@ -7,8 +7,9 @@
|
||||
* scale = amax(|y|) / qmax (1 for an all-zero group), code = clamp(y / scale, +-qmax)
|
||||
*
|
||||
* qmax 448 for an E4M3 `q`, 127 (rounded to nearest even) for an i8 one; `s` holds a scale per
|
||||
* (row, part, group). One work-group a (row, part, group): the inputs in local memory, a work-item
|
||||
* an output j, its dot in double in libref's order, so the codes and scales are libref's.
|
||||
* (row, part, group). Two forms: the fast one below (a float-pair sum; the card's f64 is slow) and
|
||||
* the exact one (XE2_EXACT=1, or a rotation that is not f16): a work-item an output j, its dot in
|
||||
* double in libref's order, so the codes and scales are libref's.
|
||||
*/
|
||||
#include "xe2_common.h"
|
||||
#include "xe2_ops.h"
|
||||
@@ -59,6 +60,86 @@ int givens_dense_quant(const RadArgs* a, RadStream st) {
|
||||
const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype;
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16)) {
|
||||
/* The fast form. A work-group a (part, group) and up to kRows rows; sub-group s takes the
|
||||
* outputs j = 16 s .. 16 s + 15, and for each its lanes read row j of T in one coalesced
|
||||
* 256-byte step (8 f16 a lane). libref's double sum becomes a float pair: each lane's eight
|
||||
* products split exactly (two-product by fma) and added by two-sum, then the sixteen pairs
|
||||
* combined in a fixed tree, also by two-sum -- ~48 significant bits, rounded once to f32:
|
||||
* libref's y unless the exact sum sits within ~2^-24 ulp of an f32 rounding boundary. So
|
||||
* this is a fast kernel, held to rad-kbench's tolerance and the KL bound; XE2_EXACT=1 takes
|
||||
* the double form below. */
|
||||
constexpr int64_t kRows = 16;
|
||||
const int64_t rblocks = (rows + kRows - 1) / kRows;
|
||||
const size_t groups = (size_t)(P * ngrp * rblocks);
|
||||
if (!exact_only())
|
||||
return submit([&] {
|
||||
q->submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<float, 1> in(sycl::range<1>(kG), h);
|
||||
sycl::local_accessor<float, 1> yv(sycl::range<1>(kG), h);
|
||||
h.parallel_for(sycl::nd_range<1>{groups * kG, kG},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const int64_t gid = (int64_t)it.get_group(0);
|
||||
const int64_t rb = gid % rblocks, pg = gid / rblocks;
|
||||
const int64_t gi = pg % ngrp, p = pg / ngrp;
|
||||
const int t = (int)it.get_local_id(0);
|
||||
auto grp = it.get_group();
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int sgi = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
|
||||
const uint16_t* t0 = (const uint16_t*)tb + (p * ngrp + gi) * kG * kG;
|
||||
const int64_t c = gi * kG + t;
|
||||
const float dsc = rad_load_f32(db, ddt, p * n + c);
|
||||
auto two_sum = [](float a, float b, float& s, float& e) {
|
||||
s = a + b;
|
||||
const float bb = s - a;
|
||||
e = (a - (s - bb)) + (b - bb);
|
||||
};
|
||||
for (int64_t r = rb * kRows; r < sycl::min(rows, rb * kRows + kRows); ++r) {
|
||||
sycl::group_barrier(grp);
|
||||
in[t] = rad_load_f32(xb, xdt, r * xrs + c * xcs) / dsc;
|
||||
sycl::group_barrier(grp);
|
||||
float xin[8];
|
||||
for (int u = 0; u < 8; ++u) xin[u] = in[lane * 8 + u];
|
||||
for (int jj = 0; jj < 16; ++jj) {
|
||||
const int j = sgi * 16 + jj;
|
||||
const sycl::vec<uint16_t, 8> tv = *(const sycl::vec<uint16_t, 8>*)(t0 + j * kG + lane * 8);
|
||||
float hi = 0.0f, lo = 0.0f;
|
||||
for (int u = 0; u < 8; ++u) {
|
||||
const float tw = (float)sycl::bit_cast<sycl::half>(tv[u]);
|
||||
const float pr = tw * xin[u];
|
||||
const float pe = sycl::fma(tw, xin[u], -pr);
|
||||
float sv, se;
|
||||
two_sum(hi, pr, sv, se);
|
||||
hi = sv;
|
||||
lo += se + pe;
|
||||
}
|
||||
for (int off = 8; off > 0; off >>= 1) {
|
||||
const float h2 = sycl::shift_group_left(sg, hi, off);
|
||||
const float l2 = sycl::shift_group_left(sg, lo, off);
|
||||
float sv, se;
|
||||
two_sum(hi, h2, sv, se);
|
||||
hi = sv;
|
||||
lo = lo + l2 + se;
|
||||
}
|
||||
if (lane == 0) yv[j] = hi + lo;
|
||||
}
|
||||
sycl::group_barrier(grp);
|
||||
const float y = yv[t];
|
||||
const float amax = sycl::reduce_over_group(grp, sycl::fabs(y), sycl::maximum<float>());
|
||||
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
|
||||
const float inv = 1.0f / scl;
|
||||
float v = y * inv;
|
||||
if (v > qmax) v = qmax;
|
||||
if (v < -qmax) v = -qmax;
|
||||
if (i8) v = sycl::rint(v);
|
||||
rad_store_f32(qb, qdt, r * qrs + (p * n + c) * qcs, v);
|
||||
if (t == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
/* The exact form, libref's double sum in libref's order: a work-group a (row, part, group). */
|
||||
const size_t groups = (size_t)(rows * P * ngrp);
|
||||
return submit([&] {
|
||||
q->submit([&](sycl::handler& h) {
|
||||
|
||||
Reference in new issue
Block a user