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:
claudeandClaude Opus 5.5 committed 2026-10-24 00:30:40 +02:00
1 parent 20ddb6aba1
commit 3158324bdb
15 files changed
+2251 -78

No files matched your search

+153 -36
View File
@@ -6,9 +6,10 @@
* angle pos * inv in f32 and its cosine and sine in double, rounded to f32, as libref's
* ref_cos/ref_sin are: positions reach 131072, where f32 range reduction is at its weakest.
*
* Modes: neox (i with i + rot/2) and gptj (2i with 2i+1), one position component per token. The
* multi-component modes (mrope, imrope, axial) are refused at launch with their name; their rows
* are not declared either (the `mode` constraint in xe2_rows.cpp).
* Modes: neox (i with i + rot/2) and gptj (2i with 2i+1); the multi-component modes pair like neox
* and choose each frequency's position component from `sections` (mrope contiguous, imrope
* interleaved as Qwen3-VL and Qwen3.5, axial contiguous on per-section ladders), positions [C, M]
* component-major -- libref's rope_freqs. rope_table writes cos/sin rows per position.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
@@ -16,6 +17,7 @@
#include "rad_yarn.h"
#include <cmath>
#include <cstdlib>
#include <cstring>
namespace xe2 {
@@ -24,10 +26,6 @@ namespace {
constexpr int kMaxHalf = 128; /* head_dim up to 256 */
struct Freqs {
float inv[kMaxHalf];
};
/* libref's row_stride: fold trailing axes until they span `cols`; the next axis out carries the
* row. */
int64_t row_stride(const RadTensor* t, int64_t cols) {
@@ -43,6 +41,75 @@ int64_t row_stride(const RadTensor* t, int64_t cols) {
} // namespace
namespace {
enum { kNeox = 0, kGptj = 1, kMrope = 2, kImrope = 3, kAxial = 4 };
struct Freqs {
float inv[kMaxHalf];
int comp[kMaxHalf];
};
/* "11 11 10" -> {11, 11, 10}; absent is none, -1 a non-positive entry (libref's rope_sections). */
int sections(const RadArgs* a, int64_t* sec) {
const char* v = rad_args_gets_or(a, "sections", nullptr);
int n = 0;
while (v && *v && n < 8) {
char* end = nullptr;
const long long x = std::strtoll(v, &end, 10);
if (end == v) break;
if (x <= 0) return -1;
sec[n++] = x;
v = end;
}
return n;
}
/* libref's rope_freqs: each frequency's position component and inverse. */
int freqs(int mode, int64_t rot, int64_t den, float theta, const int64_t* sec, int n_sec, Freqs* f) {
const int64_t half = rot / 2;
if (half > kMaxHalf) return refuse("rope: rotary_dim %lld over %d", (long long)rot, 2 * kMaxHalf);
int64_t start[8] = {}, total = 0;
for (int c = 0; c < n_sec; ++c) { start[c] = total; total += sec[c]; }
if ((mode == kMrope || mode == kAxial) && n_sec > 0 && total != half) return RAD_E_SHAPE;
for (int64_t i = 0; i < half; ++i) {
int c = 0;
float inv = 1.0f / (float)std::pow((double)theta, (double)((float)(2 * i) / (float)den));
if (n_sec > 0 && (mode == kMrope || mode == kAxial)) {
while (c + 1 < n_sec && i >= start[c + 1]) ++c;
if (mode == kAxial) {
const int64_t j = i - start[c];
inv = 1.0f / (float)std::pow((double)theta, (double)((float)(2 * j) / (float)(2 * sec[c])));
}
} else if (n_sec >= 3 && mode == kImrope) {
if (i % 3 == 1 && i < 3 * sec[1]) c = 1;
else if (i % 3 == 2 && i < 3 * sec[2]) c = 2;
}
f->comp[i] = c;
f->inv[i] = inv;
}
return RAD_OK;
}
int ladder(const RadArgs* a, int64_t rot, int64_t head_dim, int64_t* den) {
const char* lad = rad_args_gets_or(a, "ladder", nullptr);
if (!lad || !std::strcmp(lad, "rotary")) { *den = rot; return RAD_OK; }
if (!std::strcmp(lad, "head")) {
if (head_dim <= 0) return RAD_E_SHAPE;
*den = head_dim;
return RAD_OK;
}
return RAD_E_UNSUPPORTED;
}
/* A position as the rotation reads it, divided by the scale in f32 (libref's rope_pos). */
inline float pos_at(const char* pb, uint32_t pdt, int64_t off, float div) {
const bool flt = pdt == RAD_F32 || pdt == RAD_BF16 || pdt == RAD_F16;
return (flt ? rad_load_f32(pb, pdt, off) : (float)ld_index(pdt, pb, off)) / div;
}
} // namespace
int rope(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
const RadTensor *QKV = &a->t[0], *POS = &a->t[1];
@@ -51,40 +118,45 @@ int rope(const RadArgs* a, RadStream st) {
const int64_t n_head_kv = rad_args_geti_or(a, "n_head_kv", 0);
const float theta = (float)rad_args_getf_or(a, "theta", 10000.0);
const float rscale = (float)rad_args_getf_or(a, "scale", 1.0);
static const char* kModes[] = { "neox", "gptj", "mrope", "imrope", "axial" };
const char* mode_s = rad_args_gets_or(a, "mode", "");
int mode = -1;
if (!std::strcmp(mode_s, "neox")) mode = 0;
else if (!std::strcmp(mode_s, "gptj")) mode = 1;
else return refuse("rope: mode '%s' is not served (neox and gptj are)", mode_s);
for (int i = 0; i < 5; ++i)
if (!std::strcmp(mode_s, kModes[i])) mode = i;
if (mode < 0) return RAD_E_UNSUPPORTED;
int64_t rot = rad_args_geti_or(a, "rotary_dim", head_dim);
if (head_dim <= 0 || n_head <= 0 || rot <= 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE;
int64_t den = rot;
if (const char* lad = rad_args_gets_or(a, "ladder", nullptr)) {
if (!std::strcmp(lad, "head")) den = head_dim;
else if (std::strcmp(lad, "rotary")) return RAD_E_UNSUPPORTED;
int64_t den;
if (int rc = ladder(a, rot, head_dim, &den); rc < 0) return rc;
int64_t sec[8] = {};
const int n_sec = sections(a, sec);
if (n_sec < 0) return RAD_E_SHAPE;
/* Positions: [M] one component a token; [C, M] C components, component-major. */
const bool multi = POS->rank >= 2;
const int64_t n_comp = multi ? POS->shape[0] : 1;
const int64_t M = multi ? POS->shape[1] : rad_tensor_numel(POS);
if (multi) {
if (mode == kNeox || mode == kGptj || n_sec <= 0) return RAD_E_SHAPE;
const int64_t need = mode == kImrope ? 3 : n_sec;
if (n_comp < need || n_comp > 8) return RAD_E_SHAPE;
}
if (rad_args_gets_or(a, "sections", nullptr) || POS->rank >= 2)
return refuse("rope: multi-component positions are not served");
const int64_t M = rad_tensor_numel(POS);
const int64_t nfreq = rot / 2;
if (den == head_dim && rot < head_dim && mode == 0) rot = head_dim;
const int64_t half = rot / 2;
if (half > kMaxHalf) return refuse("rope: rotary_dim %lld over %d", (long long)rot, 2 * kMaxHalf);
/* The ladder, as libref builds it. */
if (den == head_dim && rot < head_dim && !multi && mode == kNeox) rot = head_dim;
Freqs f{};
for (int64_t i = 0; i < half; ++i)
f.inv[i] = 1.0f / (float)std::pow((double)theta, (double)((float)(2 * i) / (float)den));
if (int rc = freqs(mode, rot, den, theta, sec, multi ? n_sec : 0, &f); rc < 0) return rc;
const int64_t half = rot / 2;
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn)) return RAD_E_INVAL;
if (yarn.factor != 0.0f) {
if (rscale != 1.0f) return RAD_E_UNSUPPORTED;
if (mode == kAxial || rscale != 1.0f) return RAD_E_UNSUPPORTED;
for (int64_t i = 0; i < half; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i);
}
const RadLlama3 l3 = rad_llama3_args(a);
if (!rad_llama3_valid(l3) || (l3.factor != 0.0f && yarn.factor != 0.0f)) return RAD_E_INVAL;
if (l3.factor != 0.0f)
if (l3.factor != 0.0f) {
if (mode == kAxial || multi) return RAD_E_UNSUPPORTED;
for (int64_t i = 0; i < half; ++i) f.inv[i] = l3.frequency(f.inv[i]);
}
for (int64_t i = nfreq; i < half; ++i) f.inv[i] = 0.0f;
const float magnitude = yarn.magnitude;
@@ -92,14 +164,14 @@ int rope(const RadArgs* a, RadStream st) {
if (M <= 0 || rad_tensor_numel(QKV) < M * stride_row) return RAD_E_SHAPE;
const int64_t rs = row_stride(QKV, stride_row);
const int64_t ds = QKV->rank ? QKV->stride[QKV->rank - 1] : 1;
const int64_t pcs = multi ? POS->stride[0] : 0, pms = multi ? POS->stride[1] : 0;
const Lin pl = Lin::of(POS);
char* base = (char*)QKV->data;
const uint32_t dt = QKV->dtype;
const char* pbase = (const char*)POS->data;
const uint32_t pdt = POS->dtype;
const bool pflt = pdt == RAD_F32 || pdt == RAD_BF16 || pdt == RAD_F16;
const bool pcontig = rad_tensor_is_contiguous(POS);
if (!pcontig) return refuse("rope: positions are not contiguous");
const float div = rscale != 0.0f ? rscale : 1.0f;
const int pairs = mode == kGptj ? kGptj : kNeox; /* the multi modes pair like neox */
const int64_t heads = n_head + n_head_kv;
const int64_t total = M * heads * half;
@@ -112,17 +184,62 @@ int rope(const RadArgs* a, RadStream st) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t i = g % half, h = (g / half) % heads, m = g / (half * heads);
const float p = (pflt ? rad_load_f32(pbase, pdt, m) : (float)ld_index(pdt, pbase, m)) / div;
const int c = f.comp[i];
const float p = multi ? pos_at(pbase, pdt, c * pcs + m * pms, div)
: pos_at(pbase, pdt, pl.off(m), div);
const float ang = p * f.inv[i];
const float c = (float)sycl::cos((double)ang) * magnitude;
const float s = (float)sycl::sin((double)ang) * magnitude;
const int64_t i0 = mode == 1 ? 2 * i : i;
const int64_t i1 = mode == 1 ? 2 * i + 1 : i + half;
const float cs = (float)sycl::cos((double)ang) * magnitude;
const float sn = (float)sycl::sin((double)ang) * magnitude;
const int64_t i0 = pairs == kGptj ? 2 * i : i;
const int64_t i1 = pairs == kGptj ? 2 * i + 1 : i + half;
const int64_t hb = m * rs + h * head_dim * ds;
const float x0 = rad_load_f32(base, dt, hb + i0 * ds);
const float x1 = rad_load_f32(base, dt, hb + i1 * ds);
rad_store_f32(base, dt, hb + i0 * ds, x0 * c - x1 * s);
rad_store_f32(base, dt, hb + i1 * ds, x0 * s + x1 * c);
rad_store_f32(base, dt, hb + i0 * ds, x0 * cs - x1 * sn);
rad_store_f32(base, dt, hb + i1 * ds, x0 * sn + x1 * cs);
});
});
}
/* rope_table: cos and sin rows for each position, cs[pos * rot + i] and cs[pos * rot + half + i],
* as libref's ref_rope_table (a position outside the table is skipped). */
int rope_table(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
const RadTensor *POS = &a->t[0], *CS = &a->t[1];
const int64_t rot = rad_args_geti_or(a, "rot", 0);
const int64_t head_dim = rad_args_geti_or(a, "head_dim", 0);
const float theta = (float)rad_args_getf_or(a, "theta", 10000.0);
const float rscale = (float)rad_args_getf_or(a, "scale", 1.0);
if (rot < 2 || (rot & 1)) return RAD_E_SHAPE;
int64_t den;
if (int rc = ladder(a, rot, head_dim, &den); rc < 0) return rc;
const int64_t M = rad_tensor_numel(POS);
if (M <= 0 || rad_tensor_numel(CS) % rot) return RAD_E_SHAPE;
const int64_t rows = rad_tensor_numel(CS) / rot, half = rot / 2;
if (half > kMaxHalf) return refuse("rope_table: rot %lld over %d", (long long)rot, 2 * kMaxHalf);
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && rscale != 1.0f)) return RAD_E_INVAL;
Freqs f{};
for (int64_t i = 0; i < half; ++i)
f.inv[i] = yarn.frequency(1.0f / (float)std::pow((double)theta, (double)((float)(2 * i) / (float)den)), (int)i);
const float magnitude = yarn.magnitude;
const float div = rscale != 0.0f ? rscale : 1.0f;
const Lin pl = Lin::of(POS), cl = Lin::of(CS);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t total = M * half;
const size_t wg = 128;
const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg);
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t i = g % half, m = g / half;
const int64_t pp = ld_index(pl.dtype, pl.base, pl.off(m));
if (pp < 0 || pp >= rows) return;
const float ang = ((float)pp / div) * f.inv[i];
rad_store_f32(cl.base, cl.dtype, cl.off(pp * rot + i), (float)sycl::cos((double)ang) * magnitude);
rad_store_f32(cl.base, cl.dtype, cl.off(pp * rot + half + i), (float)sycl::sin((double)ang) * magnitude);
});
});
}