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_gemm_zp.cpp
T
claudeandClaude Opus 5.5 5da5c1ba05 libxe2: the DSpark drafter's ops -- speculative decoding on the B65, greedy-identical
Every op MiniCPM5-2B bf16's DSpark drafter issues now has a libxe2 row, each libref's arithmetic in
libref's order: gemm_nt_q over fp8a8 block-scaled codes (quant_dot per output, the scale grids
worked out on the host as make_scales does), gemm_nt_q_gated with the gated fp8 epilogue, the w2a8
vocabulary head (gemm_zp; b_scale in libref's joined planes form through a copy of libref's layout
hook, so both libraries ask for the same stored bytes, its planes found on the device),
quant_act_i8, silu_mul, row_topk and dflash_select.

rad-kbench: MiniCPM5-2B 191/191 at rel_l2 0. The fixture found the gated GEMM refusing a 27B
geometry its row accepted (it held the whole N-wide row in local memory); it now keeps the n gated
values and declares N <= 57344. The engine oracle with speculation on: 1500/1500 issues
bit-identical, the w2a8 head included. Greedy with speculation on matches libref's greedy without
it on all eight prompts.

Install and oot: a SYCL build installs librad_sycl.so and libxe2.so, its executables find the
backend through $ORIGIN/../lib, and tests/oot_plugins.sh rebuilds libxe2 out of tree with icpx when
RAD_OOT_ICPX is set; it passes in build-sycl.

scripts/sycl/bench_report.sh: the report's screenshot is a JPEG, rendered with fonts (this host has
none, and the first post had no text). docs/SYCL_PROGRESS.md: the first quick benchmark (4.2 tok/s
decode, 17.3 tok/s prefill at 2000 tokens), Milestone B part 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-23 19:31:29 +02:00

210 lines
9.6 KiB
C++

/* xe2_gemm_zp.cpp -- gemm_nt_q over the zero-point grids whose activation is quant_act_i8's
* (w2a8, w4a8_asym): the DSpark drafter's 2-bit vocabulary head. Correctness first: libref's
* gemm_zp (ref_gemm.cpp) per output --
*
* per group g of K: is = sum over ascending k of a[k] * code[n][k] (exact integers)
* sub = (float)is - (float)zero[n][g] * a_sum[g][m]
* acc = acc + (sub * a_scale[m]) * scale[n][g]
*
* -- with `a` read back from quant_act_i8's step order into column order, as libref does.
*
* `b_scale` reaches the kernel in libref's joined form (abi/rad_planes.h): a header, an entry per
* plane, then the "scale" and "zero" planes. The layout hook below is libref's
* (ref_layout_gemm_q), so both libraries ask for the same stored bytes and the builder can resolve
* either for a band. The header lives in device memory, so the kernel finds its planes itself: a
* launch must not read device memory from the host.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include "rad_planes.h"
#include <cstring>
namespace xe2 {
namespace {
struct ZpGrid {
const char* name;
uint32_t codes;
bool i8_row; /* the activation is quant_act_i8's (served here) */
};
const ZpGrid* zp_grid(const char* dtype) {
static const ZpGrid kGrids[] = {
{ "w2a8", RAD_U2, true }, { "w4a8_asym", RAD_U4, true },
{ "w4a16_asym", RAD_U4, false }, { "w5a16", RAD_U5, false },
{ "w4a8i8_asym", RAD_U4, false }, { "w5a8i8", RAD_U5, false },
};
if (!dtype) return nullptr;
for (const ZpGrid& g : kGrids)
if (std::strcmp(g.name, dtype) == 0) return &g;
return nullptr;
}
bool zero_dt(uint32_t dt) { return dt == RAD_U8 || dt == RAD_U2 || dt == RAD_U4 || dt == RAD_U5; }
bool sel_roles(const RadEncoding* enc, const int* sel, int n, const char* r0, const char* r1) {
if (!enc || !sel || n != 2) return false;
for (int i = 0; i < 2; ++i)
if (sel[i] < 0 || sel[i] >= enc->n_planes) return false;
return std::strncmp(enc->plane[sel[0]].role, r0, RAD_ENC_STR) == 0 &&
std::strncmp(enc->plane[sel[1]].role, r1, RAD_ENC_STR) == 0;
}
/* One plane of the joined operand, found on the device. */
struct DPlane {
const char* p = nullptr;
uint32_t dt = 0;
int64_t row_bytes = 0;
float at(int64_t i, int64_t j) const { return rad_load_f32(p + i * row_bytes, dt, j); }
};
inline bool role_is(const char* role, const char* want) {
for (int i = 0; i < RAD_ENC_STR; ++i) {
if (role[i] != want[i]) return false;
if (!want[i]) return true;
}
return true;
}
inline DPlane find_plane(const char* base, const char* role) {
DPlane 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_is(e[i].role, role)) {
v.p = base + e[i].offset;
v.dt = e[i].dtype;
v.row_bytes = e[i].row_bytes;
return v;
}
return v;
}
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];
}
} // namespace
/* ------------------------------------------------------------------ the layout hooks */
/* libref's ref_layout_gemm_q, line for line: `b_scale` (operand 3) joined as "scale" and "zero",
* one each per (row, `group` of K), over codes of the grid's width. */
int gemm_zp_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out) {
if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED;
const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr));
const long long group = rad_param_geti(p, n_p, "group", 0);
if (!zg || group <= 0 || !rad_enc_is(enc, "affine")) return RAD_E_DTYPE;
if (enc->transform[0] && std::strncmp(enc->transform, "givens", RAD_ENC_STR) != 0)
return RAD_E_DTYPE;
if (!sel_roles(enc, sel, n, "scale", "zero") || enc->n_planes < 3) return RAD_E_DTYPE;
const RadEncPlane& c = enc->plane[0];
const RadEncPlane& s = enc->plane[sel[0]];
const RadEncPlane& z = enc->plane[sel[1]];
if (std::strncmp(c.role, "codes", RAD_ENC_STR) != 0 || c.dtype != zg->codes ||
c.block[0] != 1 || c.block[1] != 1)
return RAD_E_DTYPE;
if (s.kind != RAD_PLANE_TILED || s.dtype != RAD_F16 || s.block[0] != 1 || s.block[1] != group ||
z.kind != RAD_PLANE_TILED || !zero_dt(z.dtype) || z.block[0] != 1 || z.block[1] != group)
return RAD_E_DTYPE;
return rad_planes_layout(enc, sel, planes, n, out);
}
int gemm_zp_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
const int* sel, const RadTensor* planes, int n, void* dst, int64_t dst_bytes) {
RadLayout lay{};
const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes);
}
int gemm_zp_unrelayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes,
int n) {
RadLayout lay{};
const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n);
}
/* ------------------------------------------------------------------ the launch */
int gemm_nt_q_zp(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 5 || !a->t[0].data || !a->t[2].data || !a->t[4].data) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4];
const RadTensor* AS = rad_arg_in(a, 1);
const RadTensor* BS = rad_arg_in(a, 3);
const RadTensor* ASUM = rad_arg_in(a, 5);
if (rad_arg_in(a, 6)) return refuse("gemm_nt_q (zero-point): b_ref is mxfp4's");
const ZpGrid* zg = zp_grid(rad_args_gets_or(a, "dtype", nullptr));
if (!zg || !zg->i8_row)
return refuse("gemm_nt_q (zero-point): dtype '%s' is not served (w2a8, w4a8_asym are)",
rad_args_gets_or(a, "dtype", ""));
if (rad_args_geti_or(a, "pb1", 0) > 0 || rad_args_geti_or(a, "pb2", 0) > 0) return RAD_E_UNSUPPORTED;
const int64_t K = rad_args_geti_or(a, "K", B->rank >= 2 ? B->shape[B->rank - 1] : 0);
const int64_t N = rad_args_geti_or(a, "N", B->rank >= 2 ? B->shape[B->rank - 2] : 0);
const int64_t group = rad_args_geti_or(a, "group", 0);
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
const int64_t M = rad_tensor_numel(Y) / N;
if (M <= 0 || rad_tensor_numel(A) < M * K || rad_tensor_numel(B) < N * K) return RAD_E_SHAPE;
if (!BS) return RAD_E_INVAL;
if (B->dtype != zg->codes) return RAD_E_DTYPE;
if (group <= 0 || K % group || K % 16) return RAD_E_SHAPE;
if (!rad_tensor_is_contiguous(B)) return RAD_E_STRIDE;
if (A->dtype != RAD_I8) return RAD_E_DTYPE;
if (A->rank && A->stride[A->rank - 1] != 1) return RAD_E_STRIDE;
const int64_t G = K / group, mpad = (M + 15) / 16 * 16;
if (!AS || rad_tensor_numel(AS) < M) return RAD_E_INVAL;
if (!ASUM || rad_tensor_numel(ASUM) < G * mpad) return RAD_E_INVAL;
const int64_t brow = rad_dtype_bytes(B->dtype, K);
const int cbits = rad_dtype_bits(B->dtype);
const int64_t ars = row_stride(A, K);
const int64_t sa_rs = row_stride(AS, 1);
const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1;
const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1;
const char *ab = (const char*)A->data, *bb = (const char*)B->data, *bsb = (const char*)BS->data;
const char *sab = (const char*)AS->data, *sumb = (const char*)ASUM->data;
const uint32_t sadt = AS->dtype, sumdt = ASUM->dtype, ydt = Y->dtype;
char* yb = (char*)Y->data;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const size_t wn = 64;
const size_t nn = (size_t)((N + (int64_t)wn - 1) / (int64_t)wn) * wn;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<2>{{(size_t)M, nn}, {1, wn}}, [=](sycl::nd_item<2> it) {
const int64_t m = (int64_t)it.get_global_id(0), n = (int64_t)it.get_global_id(1);
if (n >= N) return;
const DPlane sc = find_plane(bsb, "scale"), zp = find_plane(bsb, "zero");
if (!sc.p || !zp.p) return; /* not a joined b_scale: the layout hook guarantees one */
const uint8_t* crow = (const uint8_t*)bb + n * brow;
const int8_t* ar = (const int8_t*)ab + m * ars;
const float sa = rad_load_f32(sab, sadt, m * sa_rs);
float acc = 0.0f;
for (int64_t g = 0; g < G; ++g) {
const float sb = sc.at(n, g);
const int32_t z = (int32_t)zp.at(n, g);
int64_t is = 0;
for (int64_t k = g * group; k < (g + 1) * group; ++k) {
const int64_t pos = (k & ~(int64_t)12) | ((k & 4) << 1) | ((k & 8) >> 1);
is += (int64_t)ar[pos] * (int32_t)rad_load_bits(crow, cbits, k);
}
const float sub = (float)is - (float)z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s);
acc += sub * sa * sb;
}
rad_store_f32(yb, ydt, m * ys_r + n * ys_c, acc);
});
});
}
} // namespace xe2