libxe2: every op Qwen3.8-27B-PARO-M2XFP4 needs, correctness first -- the 27B serves on the B65

Kernels, each libref's arithmetic in libref's order:
- gemm_nt_q at m2xfp4 (xe2_gemm_m2.cpp) with libref's m2 layout hooks, and givens_dense_quant
  (xe2_givens.cpp, the rotation in double as libref's)
- the gated delta net (xe2_gdn.cpp): gdn_conv_prep, gdn_conv_update, gdn_kkt_solve, gdn_chunk_scan,
  gdn_recurrent_update (per-candidate and anchor state forms), gdn_gated_rmsnorm,
  gdn_conv_recurrent_update; each work-item one iteration of libref's parallel loop, the
  per-(sequence, head) workspaces in a shared device workspace (xe2_common: workspace())
- rope's multi-component modes (mrope, imrope, axial) and rope_table
- logits_gemm over an fp8 128x128 joined head, gemm_nt_bias with its epilogue
- mul, sigmoid, scatter_rows, layernorm, grid_embed, attn_dense (the vision tower)
- dflash_conv and logit_rerank (plain and joined head) for the DFlash2 drafter

libref's gemm_m2 gains the parts (pb1/pb2) every other ParoQuant grid and libr4d's mx_parts
already define -- weight rows [0, e1), [e1, e2), [e2, N) read activation copies 0, 1, 2 side by
side -- since this model's fused projections are rotated per piece and the oracle declined them;
its shape hook widens a and a_scale by the part count. Both m2 arms now take the stored
codes-then-meta blob as the engine describes it, u8 [N, K], beside the shape hook's rank-1 form
(libref refused the engine's launch; only libr4d had been run on it).

rad-kbench on the fixture: every new row at rel_l2 0 (90 gdn/givens cases, 108 rope, 80
elementwise, 20 attn_dense, the drafter's and the heads'). On the B65 the model starts with every
op on the card and answers "The capital of France is" -> " Paris.\nThe capital of Germany is
Berlin.\nThe capital of Italy is" (greedy), at 0.1 tok/s: the correctness-first kernels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-24 00:30:40 +02:00
1 parent f98b4070cd
commit 4921641578
15 files changed
+2251 -78

No files matched your search

+31 -15
View File
@@ -384,27 +384,45 @@ static int gemm_zp(const ZpGrid& zg, const RadTensor* A, const RadTensor* as_,
* multiplier k_s the meta byte's two-bit field [2s, 2s+2) over subgroups s of eight columns
* (libr4d's r4d_m2xfp4.h folds the same law into its staging table); each product is one f32
* multiply, each row one f32 accumulation in k order -- the exact decode's own arithmetic, since
* the multipliers take the codes off the integer grid an mxfp4 group sub-sum would use. */
* the multipliers take the codes off the integer grid an mxfp4 group sub-sum would use.
*
* Parts (`pb1`, `pb2`), as the other ParoQuant grids read them (gemm_zp, libr4d's mx_parts): weight
* rows [0, e1), [e1, e2), [e2, N) meet copies 0, 1, 2 of the activation, side by side in `a`
* ([M, parts * K]) and in `a_scale` ([M, parts * K/128]) -- a fused projection whose pieces were
* rotated separately, each copy givens_dense_quant's for its own part. */
static const float kM2E2M1[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };
static int gemm_m2(const RadArgs* a) {
static int gemm_m2(const RadArgs* a, int64_t pb1, int64_t pb2) {
if (a->n_t < 5) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4];
const RadTensor *as_ = t_in(a, 1), *bs_ = t_in(a, 3);
if (!A->data || !B->data || !Y->data) return RAD_E_INVAL;
if (t_in(a, 5)) return RAD_E_UNSUPPORTED; /* a_sum belongs to the asym grids */
if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8 || B->rank != 1) return RAD_E_DTYPE;
if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8) return RAD_E_DTYPE;
if (!as_ || !bs_ || bs_->dtype != RAD_E8M0 || bs_->rank < 2) return RAD_E_DTYPE;
const int64_t K = p_int(a, "K", 0), N = p_int(a, "N", 0);
if (K <= 0 || K % 32 || N <= 0) return RAD_E_SHAPE;
const int64_t M = numel(Y) / N;
if (M <= 0) return RAD_E_SHAPE;
const int64_t kb = K / 32, akb = (K + 127) / 128;
if (numel(A) < M * K || numel(B) != N * (K / 2 + kb) || numel(bs_) < N * kb ||
numel(as_) < M * akb)
const bool parted = pb1 > 0 || pb2 > 0;
const int64_t e1 = pb1 > 0 ? std::min<int64_t>(pb1, N) : N;
const int64_t e2 = pb2 > 0 ? std::max<int64_t>(e1, std::min<int64_t>(pb2, N)) : N;
const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
if (parted && ((e1 % 16) || (e2 % 16))) return RAD_E_SHAPE;
if (parted && e1 == e2 && e2 < N) return RAD_E_SHAPE; /* an empty middle part has no copy */
const int64_t acols = parts * K, scols = parts * akb;
/* `b` is the stored codes-then-meta blob: a rank-1 u8 of its bytes (the shape hook's
* description), or the weight's logical [N, K] u8 view of the same bytes (the engine's). */
const bool blob = (B->rank == 1 && numel(B) == N * (K / 2 + kb)) ||
(B->rank == 2 && B->shape[0] == N && B->shape[1] == K);
if (!blob) return RAD_E_SHAPE;
if (numel(A) < M * acols || numel(bs_) < N * kb ||
numel(as_) < M * scols)
return RAD_E_SHAPE;
const int64_t ars = row_stride(A, K), acs = laststride(A);
const int64_t srs = row_stride(as_, akb), scs = laststride(as_);
if (A->rank >= 2 && A->shape[A->rank - 1] != acols) return RAD_E_SHAPE;
const int64_t ars = row_stride(A, acols), acs = laststride(A);
const int64_t srs = row_stride(as_, scols), scs = laststride(as_);
const int64_t brs = row_stride(bs_, kb);
const int64_t ys_r = row_stride(Y, N), ys_c = laststride(Y);
const uint8_t* meta0 = (const uint8_t*)B->data + N * (K / 2);
@@ -423,11 +441,12 @@ static int gemm_m2(const RadArgs* a) {
const float km = 1.0f + 0.25f * (float)((meta[k >> 5] >> (2 * ((k & 31) >> 3))) & 3);
w[(size_t)k] = cv * ev * km;
}
const int64_t part = n < e1 ? 0 : (n < e2 ? 1 : 2);
for (int64_t m = 0; m < M; ++m) {
const int64_t ab = m * ars;
const int64_t ab = m * ars + part * K * acs, sb = m * srs + part * akb * scs;
float acc = 0.0f;
for (int64_t k = 0; k < K; ++k)
acc += ldt(A, ab + k * acs) * ldt(as_, m * srs + (k >> 7) * scs) * w[(size_t)k];
acc += ldt(A, ab + k * acs) * ldt(as_, sb + (k >> 7) * scs) * w[(size_t)k];
stt(Y, m * ys_r + n * ys_c, acc);
}
}
@@ -450,13 +469,10 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
const ZpGrid* zg = zp_grid(p_str(a, "dtype", nullptr));
if (zg && bias_operand) return RAD_E_UNSUPPORTED;
/* M2XFP4: the quantiser's own scheme, decoded by gemm_m2 from its concatenated planes.
* Parts (pb1/pb2) are the rotated grids'; the m2 declare makes none, and a case carrying
* them is declined rather than answered unsplit. */
* Parts (pb1/pb2) are the rotated grids', a fused projection rotated per piece (gemm_m2). */
if (!bias_operand && p_str(a, "dtype", nullptr) &&
std::strcmp(p_str(a, "dtype", nullptr), "m2xfp4") == 0) {
if (p_int(a, "pb1", 0) > 0 || p_int(a, "pb2", 0) > 0) return RAD_E_UNSUPPORTED;
return gemm_m2(a);
}
std::strcmp(p_str(a, "dtype", nullptr), "m2xfp4") == 0)
return gemm_m2(a, p_int(a, "pb1", 0), p_int(a, "pb2", 0));
if (!bias_operand) {
if (t_in(a, 5) && (!zg || zg->act != ZP_I8_ROW)) return RAD_E_UNSUPPORTED;
if (t_in(a, 6) && B->dtype != RAD_FP4E2M1) return RAD_E_UNSUPPORTED;