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_givens.cpp
T
pascallinxweilerandClaude Opus 5.5 07cdc546a3 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
2026-10-24 03:22:48 +02:00

175 lines
9.6 KiB
C++

/* xe2_givens.cpp -- givens_dense_quant: ParoQuant's activation side for the M2XFP4 linears.
* Correctness first, libref's ref_givens_dense_quant (ref_quant.cpp) step for step:
*
* per (row, part p, 128-column group gi):
* in[i] = x[gi*128 + i] / D[p*n + gi*128 + i] f32
* y[j] = (float) sum over ascending i of (double)T[p][gi][j][i] * (double)in[i]
* 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). 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"
#include <cstring>
namespace xe2 {
namespace {
int64_t row_stride(const RadTensor* t, int64_t cols) {
if (!t || t->rank < 2) return cols;
int64_t span = 1;
for (int i = (int)t->rank - 1; i >= 0; --i) {
span *= t->shape[i];
if (span == cols) return (i > 0) ? t->stride[i - 1] : cols;
if (span > cols) break;
}
return t->stride[t->rank - 2];
}
int64_t last_stride(const RadTensor* t) { return t->rank ? t->stride[t->rank - 1] : 1; }
constexpr int kG = 128;
} // namespace
int givens_dense_quant(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *T = &a->t[1], *D = &a->t[2], *Q = &a->t[3], *S = &a->t[4];
const int64_t n = rad_args_geti_or(a, "n", 0), P = rad_args_geti_or(a, "parts", 1);
const int64_t g = rad_args_geti_or(a, "group", 0);
if (n <= 0 || P <= 0 || g != kG || n % g != 0) return RAD_E_SHAPE;
const bool i8 = Q->dtype == RAD_I8;
if (!i8 && Q->dtype != RAD_F8E4M3) return RAD_E_DTYPE;
if (std::strcmp(rad_args_gets_or(a, "out", ""), i8 ? "i8" : "fp8") != 0) return RAD_E_DTYPE;
const int64_t rows = rad_tensor_numel(X) / n, ngrp = n / g;
if (rows <= 0 || rad_tensor_numel(T) < P * n * g || rad_tensor_numel(D) < P * n ||
rad_tensor_numel(Q) < rows * P * n || rad_tensor_numel(S) < rows * P * ngrp)
return RAD_E_SHAPE;
if (!rad_tensor_is_contiguous(T) || !rad_tensor_is_contiguous(D)) return refuse("givens_dense_quant: tables not contiguous");
const float qmax = i8 ? 127.0f : 448.0f;
const int64_t xrs = row_stride(X, n), xcs = last_stride(X);
const int64_t qrs = row_stride(Q, P * n), qcs = last_stride(Q);
const int64_t srs = row_stride(S, P * ngrp), scs = last_stride(S);
const char *xb = (const char*)X->data, *tb = (const char*)T->data, *db = (const char*)D->data;
char *qb = (char*)Q->data, *sb = (char*)S->data;
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) {
sycl::local_accessor<float, 1> in(sycl::range<1>(kG), h);
h.parallel_for(sycl::nd_range<1>{groups * kG, kG}, [=](sycl::nd_item<1> it) {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P);
const int j = (int)it.get_local_id(0);
const int64_t c = gi * kG + j;
in[j] = rad_load_f32(xb, xdt, r * xrs + c * xcs) / rad_load_f32(db, ddt, p * n + c);
sycl::group_barrier(it.get_group());
const int64_t t0 = ((p * ngrp + gi) * kG + j) * kG;
double acc = 0.0;
for (int i = 0; i < kG; ++i)
acc += (double)rad_load_f32(tb, tdt, t0 + i) * (double)in[i];
const float y = (float)acc;
const float amax = sycl::reduce_over_group(it.get_group(), 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 (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl);
});
});
});
}
} // namespace xe2