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

+108
View File
@@ -263,4 +263,112 @@ int attn_paged(const RadArgs* a, RadStream st) {
});
}
/* ------------------------------------------------------------------ attn_dense */
/* The vision tower's unpaged attention, libref's ref_attn_dense: segments [cu[s] / cu_div,
* cu[s+1] / cu_div), optional causal mask, window (and window_right) and blocks. One work-item a
* (query row, head), which finds its segment on the card and runs libref's two passes in its
* order: the max over the visible keys' scaled dots, then w = exp(dot * scale - max) in double,
* sum += w and acc += w * v in key order, out = acc * (1 / sum). */
constexpr int kDenseHdMax = 256;
int attn_dense(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
const RadTensor *Q = &a->t[0], *K = &a->t[1], *V = &a->t[2], *CU = &a->t[3], *O = &a->t[4];
const int64_t head_dim = rad_args_geti_or(a, "head_dim", Q->rank >= 3 ? Q->shape[Q->rank - 1] : 0);
const int64_t gqa = rad_args_geti_or(a, "gqa", 1);
const int causal = (int)rad_args_geti_or(a, "causal", 0);
const int64_t window = rad_args_geti_or(a, "window", 0);
const int64_t wright = rad_args_geti_or(a, "window_right", 0);
const int64_t block = rad_args_geti_or(a, "block", 0);
const int64_t cdiv = rad_args_geti_or(a, "cu_div", 1);
if (head_dim <= 0 || gqa <= 0 || window < 0 || block < 0 || cdiv <= 0 || wright < 0 ||
(wright > 0 && (causal || window == 0)))
return RAD_E_SHAPE;
if (head_dim > kDenseHdMax) return refuse("attn_dense: head_dim %lld over %d", (long long)head_dim, kDenseHdMax);
const int64_t q_heads = Q->rank >= 3 ? Q->shape[Q->rank - 2] : (Q->rank == 2 ? Q->shape[1] / head_dim : 1);
const int64_t kv_heads = q_heads / gqa;
if (kv_heads <= 0 || q_heads % gqa != 0) return RAD_E_SHAPE;
const int64_t n_seq = rad_tensor_numel(CU) - 1;
if (n_seq <= 0) return RAD_E_SHAPE;
const int64_t total_q = rad_tensor_numel(Q) / (q_heads * head_dim);
if (total_q <= 0) return RAD_E_SHAPE;
if (rad_tensor_numel(O) < total_q * q_heads * head_dim) return RAD_E_SHAPE;
if (rad_tensor_numel(K) < total_q * kv_heads * head_dim) return RAD_E_SHAPE;
if (rad_tensor_numel(V) < total_q * kv_heads * head_dim) return RAD_E_SHAPE;
const int64_t q_ds = Q->rank >= 2 ? Q->stride[Q->rank - 1] : 1;
const int64_t q_hs = Q->rank >= 3 ? Q->stride[Q->rank - 2] : head_dim * q_ds;
const int64_t q_rs = row_stride(Q, q_heads * head_dim);
const int64_t k_ds = K->rank >= 2 ? K->stride[K->rank - 1] : 1;
const int64_t k_hs = K->rank >= 3 ? K->stride[K->rank - 2] : head_dim * k_ds;
const int64_t k_rs = row_stride(K, kv_heads * head_dim);
const int64_t v_ds = V->rank >= 2 ? V->stride[V->rank - 1] : 1;
const int64_t v_hs = V->rank >= 3 ? V->stride[V->rank - 2] : head_dim * v_ds;
const int64_t v_rs = row_stride(V, kv_heads * head_dim);
const int64_t o_ds = O->rank >= 2 ? O->stride[O->rank - 1] : 1;
const int64_t o_hs = O->rank >= 3 ? O->stride[O->rank - 2] : head_dim * o_ds;
const int64_t o_rs = row_stride(O, q_heads * head_dim);
const float scale = (float)rad_args_getf_or(a, "scale", (double)(1.0f / std::sqrt((float)head_dim)));
const char *qb0 = (const char*)Q->data, *kb0 = (const char*)K->data, *vb0 = (const char*)V->data;
char* ob0 = (char*)O->data;
const uint32_t qdt = Q->dtype, kdt = K->dtype, vdt = V->dtype, odt = O->dtype;
const Lin cu = Lin::of(CU);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t total = total_q * q_heads;
const size_t wg = 64;
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 / q_heads, h = g % q_heads, kh = h / gqa;
int64_t b0 = -1, b1 = -1;
for (int64_t s = 0; s < n_seq; ++s) {
const int64_t s0 = ld_index(cu.dtype, cu.base, cu.off(s)) / cdiv;
const int64_t s1 = ld_index(cu.dtype, cu.base, cu.off(s + 1)) / cdiv;
if (i >= s0 && i < s1) { b0 = s0; b1 = s1; break; }
}
if (b0 < 0) return; /* a row no segment covers is left as it is */
const int64_t qb = i * q_rs + h * q_hs, ob = i * o_rs + h * o_hs;
int64_t jbeg = b0, jend = causal ? (i + 1) : b1;
if (window > 0) {
jbeg = sycl::max(b0, i - window + 1);
if (!causal) jend = sycl::min(b1, i + (wright > 0 ? wright : window));
}
if (block > 0) {
const int64_t blk = b0 + (i - b0) / block * block;
jbeg = sycl::max(jbeg, blk);
jend = sycl::min(jend, blk + block);
}
float mx = -INFINITY;
for (int64_t j = jbeg; j < jend; ++j) {
const int64_t kb = j * k_rs + kh * k_hs;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d)
dot += rad_load_f32(qb0, qdt, qb + d * q_ds) * rad_load_f32(kb0, kdt, kb + d * k_ds);
dot *= scale;
if (dot > mx) mx = dot;
}
if (mx == -INFINITY) {
for (int64_t d = 0; d < head_dim; ++d) rad_store_f32(ob0, odt, ob + d * o_ds, 0.0f);
return;
}
float acc[kDenseHdMax];
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
float sum = 0.0f;
for (int64_t j = jbeg; j < jend; ++j) {
const int64_t kb = j * k_rs + kh * k_hs, vb = j * v_rs + kh * v_hs;
float dot = 0.0f;
for (int64_t d = 0; d < head_dim; ++d)
dot += rad_load_f32(qb0, qdt, qb + d * q_ds) * rad_load_f32(kb0, kdt, kb + d * k_ds);
const float w = (float)sycl::exp((double)(dot * scale - mx));
sum += w;
for (int64_t d = 0; d < head_dim; ++d) acc[d] += w * rad_load_f32(vb0, vdt, vb + d * v_ds);
}
const float inv = sum > 0.0f ? 1.0f / sum : 0.0f;
for (int64_t d = 0; d < head_dim; ++d) rad_store_f32(ob0, odt, ob + d * o_ds, acc[d] * inv);
});
});
}
} // namespace xe2