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_elementwise.cpp
T
pascallinxweilerandClaude Opus 5.5 4921641578 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
2026-10-24 00:30:40 +02:00

225 lines
11 KiB
C++

/* xe2_elementwise.cpp -- elementwise ops, one work-item an element.
*
* add: y = a + b, b broadcast along rows when it holds one row (a bias add), and with `div`,
* y = rd(rd(a + b) / div), rd being y's dtype: libref's elementwise2 (ref_elementwise.cpp).
* mul: the same with a * b and no `div`.
* sigmoid: libref's act_sigmoid, 1 / (1 + exp(-x)) with the exp in double rounded once (ref_exp),
* walked in logical order through offlin.
* scatter_rows: row m of v to row idx[m] of x; a negative index is skipped, one past x's rows refused.
* layernorm: libref's, the mean and the squared deviations summed serially in index order by one
* work-item, then y = ((x - mean) * s) * w (+ b).
* grid_embed: x += the side x side table bilinearly resampled at each row's (r, c) of an (h, w) grid
* (the vision tower's position embedding), the four taps added in libref's order.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
namespace xe2 {
namespace {
int elementwise2(const RadArgs* a, RadStream s, bool mul) {
if (!rad_args_have(a, 3)) return refuse("%s: missing operand", mul ? "mul" : "add");
const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[2];
const int64_t n = rad_args_geti_or(a, "n", rad_tensor_numel(Y));
const float div = mul ? 1.0f : (float)rad_args_getf_or(a, "div", 1.0);
if (!(div != 0.0f) || !sycl::isfinite(div)) return RAD_E_INVAL;
if (n <= 0) return RAD_E_SHAPE;
Rows ya, aa, ba;
if (int rc = rows_view(Y, n, &ya, "add y"); rc < 0) return rc;
const int64_t rows = ya.rows;
if (rows <= 0 || rad_tensor_numel(A) < rows * n) return RAD_E_SHAPE;
const bool bcast = rad_tensor_numel(B) == n && rows > 1;
if (!bcast && rad_tensor_numel(B) < rows * n) return RAD_E_SHAPE;
if (int rc = rows_view(A, n, &aa, "add a"); rc < 0) return rc;
if (int rc = rows_view(B, n, &ba, "add b"); rc < 0) return rc;
if (bcast) ba.pitch = 0;
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
const int64_t total = rows * n;
const size_t wg = 256;
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 r = g / n, i = g % n;
const float u = rad_load_f32(aa.base, aa.dtype, r * aa.pitch + i);
const float v = rad_load_f32(ba.base, ba.dtype, r * ba.pitch + i);
float x = mul ? u * v : u + v;
if (div != 1.0f) x = round_dt(ya.dtype, x) / div;
rad_store_f32(ya.base, ya.dtype, r * ya.pitch + i, x);
});
});
}
} // namespace
int add(const RadArgs* a, RadStream s) { return elementwise2(a, s, false); }
int mul(const RadArgs* a, RadStream s) { return elementwise2(a, s, true); }
int sigmoid(const RadArgs* a, RadStream s) {
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *Y = &a->t[1];
const int64_t nel = rad_tensor_numel(Y);
if (nel <= 0 || rad_tensor_numel(X) < nel) return RAD_E_SHAPE;
const Lin x = Lin::of(X), y = Lin::of(Y);
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
const size_t wg = 256;
const size_t groups = (size_t)((nel + (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 i = (int64_t)it.get_global_id(0);
if (i >= nel) return;
const float v = rad_load_f32(x.base, x.dtype, x.off(i));
rad_store_f32(y.base, y.dtype, y.off(i), 1.0f / (1.0f + (float)sycl::exp((double)-v)));
});
});
}
int scatter_rows(const RadArgs* a, RadStream s) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *V = &a->t[0], *I = &a->t[1], *X = &a->t[2];
const int64_t n = rad_args_geti_or(a, "n", V->rank >= 1 ? V->shape[V->rank - 1] : 0);
if (n <= 0) return RAD_E_SHAPE;
Rows v, x;
if (int rc = rows_view(V, n, &v, "scatter_rows v"); rc < 0) return rc;
if (int rc = rows_view(X, n, &x, "scatter_rows x"); rc < 0) return rc;
const int64_t M = v.rows, dst_rows = x.rows;
if (M <= 0 || dst_rows <= 0 || rad_tensor_numel(I) < M) return RAD_E_SHAPE;
const Lin idx = Lin::of(I);
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
/* libref refuses an index past x's rows before writing anything; the indices live on the card,
* so a device flag records one and the call reports it after the copy is skipped. */
int* bad = sycl::malloc_shared<int>(1, *q);
if (!bad) return refuse("scatter_rows: no flag memory");
*bad = 0;
int rc = submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{256, 256}, [=](sycl::nd_item<1> it) {
for (int64_t m = (int64_t)it.get_local_id(0); m < M; m += 256)
if (ld_index(idx.dtype, idx.base, idx.off(m)) >= dst_rows) *bad = 1;
});
q->wait();
});
if (rc >= 0 && *bad) rc = RAD_E_INVAL;
sycl::free(bad, *q);
if (rc < 0) return rc;
const int64_t total = M * n;
const size_t wg = 256;
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 m = g / n, i = g % n;
const int64_t r = ld_index(idx.dtype, idx.base, idx.off(m));
if (r < 0) return;
rad_store_f32(x.base, x.dtype, r * x.pitch + i, rad_load_f32(v.base, v.dtype, m * v.pitch + i));
});
});
}
int layernorm(const RadArgs* a, RadStream s) {
if (!a || a->n_t < 4 || !a->t[0].data || !a->t[1].data || !a->t[3].data) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *W = &a->t[1], *Y = &a->t[3];
const RadTensor* B = rad_arg_in(a, 2);
const int64_t n = rad_args_geti_or(a, "n", W->rank ? W->shape[W->rank - 1] : 0);
const float eps = (float)rad_args_getf_or(a, "eps", 1e-5);
if (n <= 0) return RAD_E_SHAPE;
Rows x, y;
if (int rc = rows_view(X, n, &x, "layernorm x"); rc < 0) return rc;
if (int rc = rows_view(Y, n, &y, "layernorm y"); rc < 0) return rc;
const int64_t rows = x.rows;
if (rows <= 0 || rad_tensor_numel(Y) < rows * n || rad_tensor_numel(W) < n) return RAD_E_SHAPE;
if (B && rad_tensor_numel(B) < n) return RAD_E_SHAPE;
const Lin w = Lin::of(W);
const bool hb = B && B->data;
const Lin b = hb ? Lin::of(B) : Lin{};
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
constexpr size_t wg = 256;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t r = (int64_t)it.get_group(0);
const int64_t t = (int64_t)it.get_local_id(0);
float mean = 0.0f, sc = 0.0f;
if (t == 0) {
float sum = 0.0f;
for (int64_t i = 0; i < n; ++i) sum += rad_load_f32(x.base, x.dtype, r * x.pitch + i);
mean = sum / (float)n;
float ss = 0.0f;
for (int64_t i = 0; i < n; ++i) {
const float d = rad_load_f32(x.base, x.dtype, r * x.pitch + i) - mean;
ss += d * d;
}
sc = 1.0f / sycl::sqrt(ss / (float)n + eps);
}
mean = sycl::group_broadcast(it.get_group(), mean, 0);
sc = sycl::group_broadcast(it.get_group(), sc, 0);
for (int64_t i = t; i < n; i += (int64_t)wg) {
float v = (rad_load_f32(x.base, x.dtype, r * x.pitch + i) - mean) * sc *
rad_load_f32(w.base, w.dtype, w.off(i));
if (hb) v += rad_load_f32(b.base, b.dtype, b.off(i));
rad_store_f32(y.base, y.dtype, r * y.pitch + i, v);
}
});
});
}
int grid_embed(const RadArgs* a, RadStream s) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *TAB = &a->t[1], *CRD = &a->t[2];
const int64_t n = rad_args_geti_or(a, "n", TAB->rank ? TAB->shape[TAB->rank - 1] : 0);
const int64_t side = rad_args_geti_or(a, "side", 0);
if (n <= 0 || side <= 0 || rad_tensor_numel(TAB) < side * side * n) return RAD_E_SHAPE;
Rows x, tab;
if (int rc = rows_view(X, n, &x, "grid_embed x"); rc < 0) return rc;
if (int rc = rows_view(TAB, n, &tab, "grid_embed table"); rc < 0) return rc;
const int64_t rows = x.rows;
if (rows <= 0 || CRD->rank != 2 || CRD->shape[0] < 4 || CRD->shape[1] < rows) return RAD_E_SHAPE;
const int64_t cs = CRD->stride[0], ms = CRD->stride[1];
const char* cb = (const char*)CRD->data;
const uint32_t cdt = CRD->dtype;
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
const int64_t total = rows * n;
const size_t wg = 256;
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 r = g / n, i = g % n;
const int64_t gr = ld_index(cdt, cb, 0 * cs + r * ms), gc = ld_index(cdt, cb, 1 * cs + r * ms);
const int64_t gh = ld_index(cdt, cb, 2 * cs + r * ms), gw = ld_index(cdt, cb, 3 * cs + r * ms);
int64_t th[2], tw[2];
float wh[2], ww[2];
auto axis = [&](int64_t idx, int64_t size, int64_t* t, float* w) {
const float src = (float)idx * (float)(side - 1) / (float)(size - 1 > 1 ? size - 1 : 1);
const float fl = sycl::floor(src);
for (int k = 0; k < 2; ++k) {
const int64_t tap = (int64_t)fl + k;
t[k] = tap < 0 ? 0 : (tap > side - 1 ? side - 1 : tap);
const float d = sycl::fabs(src - fl - (float)k);
w[k] = 1.0f - d > 0.0f ? 1.0f - d : 0.0f;
}
};
axis(gr, gh, th, wh);
axis(gc, gw, tw, ww);
float acc = 0.0f;
for (int ki = 0; ki < 2; ++ki)
for (int kj = 0; kj < 2; ++kj)
acc += rad_load_f32(tab.base, tab.dtype, (th[ki] * side + tw[kj]) * tab.pitch + i) *
(wh[ki] * ww[kj]);
const float pe = round_dt(x.dtype, acc);
const int64_t xo = r * x.pitch + i;
rad_store_f32(x.base, x.dtype, xo, rad_load_f32(x.base, x.dtype, xo) + pe);
});
});
}
} // namespace xe2