From b0ac7fd42c7df25e8e8f64bdcdeded44e1af53f9 Mon Sep 17 00:00:00 2001 From: aneinander Date: Sun, 8 Nov 2026 13:15:46 +0100 Subject: [PATCH] libxe2: device rows for gelu, silu, gelu_tanh, scale, mul_weight, clamp, fill, iota, act_mul, gelu_mul These weightless elementwise ops had no libxe2 row, and an op with no weights cannot be placed on the host, so a model whose graph declares one refused to start on Intel ("no device kernel for op 'gelu_mul' in any plugin"). libr4d serves them; the architectures that use them include Gemma 3 and 4, the embedding models (nomic-bert, embgemma, nemotron-embed), Qwen3-ASR, LFM2's SigLIP2, Nemotron-H and the diffusion and audio models. Each is libref's chain element by element (xe2_elem_act.cpp): erf, tanh and exp in double rounded once to float; silu (alone or in act_mul) to a bf16 result takes the float exp and libref's chain again near a rounding tie, as silu_mul and sigmoid do. gelu and gelu_tanh stay in double: 1 + erf(x) cancels for negative x, so a float erf's error is not bounded by the result's. rad-kbench: the nemotronh + moe + default fixtures, 339/339 (act_mul 60, mul_weight 10, iota 9, silu 20); a random-weight 2-layer Gemma3 converted to .rad, every resolved kernel agrees with libref (gelu_mul 10 cases). That Gemma3 now serves (1.5k tok/s decode on the B65); without these rows it does not start. Co-Authored-By: Claude Opus 5.5 --- libxe2/xe2_elem_act.cpp | 271 ++++++++++++++++++++++++++++++++++++++++ libxe2/xe2_ops.h | 11 ++ libxe2/xe2_rows.cpp | 40 ++++++ 3 files changed, 322 insertions(+) create mode 100644 libxe2/xe2_elem_act.cpp diff --git a/libxe2/xe2_elem_act.cpp b/libxe2/xe2_elem_act.cpp new file mode 100644 index 00000000..36b08a10 --- /dev/null +++ b/libxe2/xe2_elem_act.cpp @@ -0,0 +1,271 @@ +/* xe2_elem_act.cpp -- the activations and small elementwise ops that had no device row, so a graph + * using them ran them host-site (a join a call: the device drained, the host arena touched). + * Each is libref's (ref_elementwise.cpp, ref_audio.cpp) element by element: + * + * gelu, gelu_tanh: libref's act_gelu / act_gelu_tanh, erf and tanh in double rounded once to float, + * the rest of the chain in float. A float erf is not used even near the bf16 output: 1 + erf(x) + * cancels for negative x, so its error is not bounded by the result's. + * silu: x * (1 / (1 + exp(-x))) with the exp in double; for a bf16 result a float exp first and + * libref's chain again only near a rounding tie (near_bf16_tie, as silu_mul and sigmoid do). + * scale: y = x * s[0], s a one-element weight; any strides, logical order. + * mul_weight: y = x * w, w one element or one row broadcast down the rows. + * clamp: y = min(max(x, lo[0]), hi[0]). + * fill: y = value over an [M, n] extent at y's row pitch. + * iota: y[i] = start + (period ? i % period : i) * step, an integer store. + * act_mul: y = rd(act(a)) * b, act silu / gelu / gelu_tanh / sigmoid / quick_gelu / relu, the + * activation rounded to y's dtype before the product as the unfused pair stores it. + * gelu_mul: y = gelu_tanh(gate_up[i]) * gate_up[n + i], the gate the first half. + * + * One work-item an element; these are memory-bound and small next to the GEMMs around them. + */ +#include "xe2_common.h" +#include "xe2_ops.h" + +#include + +namespace xe2 { + +namespace { + +inline float d_exp(float x) { return (float)sycl::exp((double)x); } +inline float d_erf(float x) { return (float)sycl::erf((double)x); } +inline float d_tanh(float x) { return (float)sycl::tanh((double)x); } +inline float act_sigmoid(float x) { return 1.0f / (1.0f + d_exp(-x)); } +inline float act_silu(float x) { return x * act_sigmoid(x); } +inline float act_gelu(float x) { return 0.5f * x * (1.0f + d_erf(x * 0.70710678118654752f)); } +inline float act_gelu_tanh(float x) { + const float k0 = 0.7978845608028654f, k1 = 0.044715f; + return 0.5f * x * (1.0f + d_tanh(k0 * (x + k1 * x * x * x))); +} +/* silu for a result rounded to bf16: the float exp, libref's chain again near a tie */ +inline float silu_bf16(float x) { + const float r = x * sigmoid_fexp(x); + return near_bf16_tie(r) ? act_silu(x) : r; +} + +size_t groups_of(int64_t n, size_t wg) { return (size_t)((n + (int64_t)wg - 1) / (int64_t)wg); } + +/* unary over logical order (libref's offlin): 0 gelu, 1 silu, 2 gelu_tanh */ +int unary(const RadArgs* a, RadStream s, int which, const char* what) { + if (!rad_args_have(a, 2)) return refuse("%s: missing operand", what); + 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 bool fast = which == 1 && y.dtype == RAD_BF16 && !exact_only(); + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(nel, wg) * 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)); + const float r = which == 0 ? act_gelu(v) : which == 2 ? act_gelu_tanh(v) : fast ? silu_bf16(v) : act_silu(v); + rad_store_f32(y.base, y.dtype, y.off(i), r); + }); + }); +} + +} // namespace + +int gelu(const RadArgs* a, RadStream s) { return unary(a, s, 0, "gelu"); } +int silu(const RadArgs* a, RadStream s) { return unary(a, s, 1, "silu"); } +int gelu_tanh(const RadArgs* a, RadStream s) { return unary(a, s, 2, "gelu_tanh"); } + +int scale(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 3)) return refuse("scale: missing operand"); + const RadTensor *X = &a->t[0], *S = &a->t[1], *Y = &a->t[2]; + const int64_t nel = rad_tensor_numel(Y); + if (nel <= 0 || rad_tensor_numel(X) < nel || rad_tensor_numel(S) < 1) return RAD_E_SHAPE; + const Lin x = Lin::of(X), y = Lin::of(Y); + const char* sb = (const char*)S->data; + const uint32_t sdt = S->dtype; + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(nel, wg) * wg, wg}, [=](sycl::nd_item<1> it) { + const int64_t i = (int64_t)it.get_global_id(0); + if (i >= nel) return; + const float sv = rad_load_f32(sb, sdt, 0); + rad_store_f32(y.base, y.dtype, y.off(i), rad_load_f32(x.base, x.dtype, x.off(i)) * sv); + }); + }); +} + +int mul_weight(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 3)) return refuse("mul_weight: missing operand"); + const RadTensor *X = &a->t[0], *W = &a->t[1], *Y = &a->t[2]; + const int64_t n = rad_args_geti_or(a, "n", rad_tensor_numel(Y)); + if (n <= 0) return RAD_E_SHAPE; + Rows y, x; + if (int rc = rows_view(Y, n, &y, "mul_weight y"); rc < 0) return rc; + const int64_t rows = y.rows; + if (rows <= 0 || rad_tensor_numel(X) < rows * n) return RAD_E_SHAPE; + if (int rc = rows_view(X, n, &x, "mul_weight x"); rc < 0) return rc; + const bool scalar = rad_tensor_numel(W) == 1; + if (!scalar && rad_tensor_numel(W) != n) return RAD_E_SHAPE; + const int64_t ws = scalar ? 0 : (W->rank ? W->stride[W->rank - 1] : 1); + const char* wb = (const char*)W->data; + const uint32_t wdt = W->dtype; + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const int64_t total = rows * n; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * 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; + rad_store_f32(y.base, y.dtype, r * y.pitch + i, + rad_load_f32(x.base, x.dtype, r * x.pitch + i) * rad_load_f32(wb, wdt, i * ws)); + }); + }); +} + +int clamp(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 4)) return refuse("clamp: missing operand"); + const RadTensor *X = &a->t[0], *L = &a->t[1], *H = &a->t[2], *Y = &a->t[3]; + const int64_t n = rad_args_geti_or(a, "n", rad_tensor_numel(Y)); + if (n <= 0 || rad_tensor_numel(L) < 1 || rad_tensor_numel(H) < 1) return RAD_E_SHAPE; + Rows y, x; + if (int rc = rows_view(Y, n, &y, "clamp y"); rc < 0) return rc; + const int64_t rows = y.rows; + if (rows <= 0 || rad_tensor_numel(X) < rows * n) return RAD_E_SHAPE; + if (int rc = rows_view(X, n, &x, "clamp x"); rc < 0) return rc; + const char *lb = (const char*)L->data, *hb = (const char*)H->data; + const uint32_t ldt = L->dtype, hdt = H->dtype; + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const int64_t total = rows * n; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * 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 lo = rad_load_f32(lb, ldt, 0), hi = rad_load_f32(hb, hdt, 0); + float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i); + v = v < lo ? lo : v; + v = v > hi ? hi : v; + rad_store_f32(y.base, y.dtype, r * y.pitch + i, v); + }); + }); +} + +int fill(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 1)) return refuse("fill: missing operand"); + const RadTensor* Y = &a->t[0]; + const int64_t n = rad_args_geti_or(a, "n", rad_tensor_numel(Y)); + if (n <= 0) return RAD_E_SHAPE; + Rows y; + if (int rc = rows_view(Y, n, &y, "fill y"); rc < 0) return rc; + const int64_t rows = y.rows; + if (rows <= 0) return RAD_E_SHAPE; + const float v = (float)rad_args_getf_or(a, "value", 0.0); + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const int64_t total = rows * n; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * wg, wg}, [=](sycl::nd_item<1> it) { + const int64_t g = (int64_t)it.get_global_id(0); + if (g >= total) return; + rad_store_f32(y.base, y.dtype, (g / n) * y.pitch + g % n, v); + }); + }); +} + +int iota(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 1)) return refuse("iota: missing operand"); + const RadTensor* Y = &a->t[0]; + const int64_t start = rad_args_geti_or(a, "start", 0), step = rad_args_geti_or(a, "step", 1); + const int64_t period = rad_args_geti_or(a, "period", 0); + const int64_t nel = rad_tensor_numel(Y); + if (nel <= 0 || period < 0) return RAD_E_SHAPE; + const uint32_t dt = Y->dtype; + if (dt != RAD_I32 && dt != RAD_U32 && dt != RAD_I64 && dt != RAD_I16 && dt != RAD_I8 && dt != RAD_U8) + return refuse("iota: dtype %u is not an integer dtype this row stores", dt); + const Lin y = Lin::of(Y); + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(nel, wg) * wg, wg}, [=](sycl::nd_item<1> it) { + const int64_t i = (int64_t)it.get_global_id(0); + if (i >= nel) return; + st_index(y.dtype, y.base, y.off(i), start + (period ? i % period : i) * step); + }); + }); +} + +int act_mul(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 3)) return refuse("act_mul: missing operand"); + 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)); + if (n <= 0) return RAD_E_SHAPE; + static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu", "relu" }; + const char* as = rad_args_gets_or(a, "act", ""); + int act = -1; + for (int k = 0; k < 6; ++k) + if (!std::strcmp(as, kActs[k])) act = k; + if (act < 0) return RAD_E_INVAL; + Rows y, ra, rb; + if (int rc = rows_view(Y, n, &y, "act_mul y"); rc < 0) return rc; + const int64_t rows = y.rows; + if (rows <= 0 || rad_tensor_numel(A) < rows * n || rad_tensor_numel(B) < rows * n) return RAD_E_SHAPE; + if (int rc = rows_view(A, n, &ra, "act_mul a"); rc < 0) return rc; + if (int rc = rows_view(B, n, &rb, "act_mul b"); rc < 0) return rc; + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const bool fast = y.dtype == RAD_BF16 && !exact_only(); + const float k1702 = rad_bf16_to_f32(rad_f32_to_bf16(1.702f)); + const int64_t total = rows * n; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * wg, wg}, [=](sycl::nd_item<1> it) { + const int64_t gi = (int64_t)it.get_global_id(0); + if (gi >= total) return; + const int64_t r = gi / n, i = gi % n; + const float g = rad_load_f32(ra.base, ra.dtype, r * ra.pitch + i); + float v; + if (act == 0) v = fast ? silu_bf16(g) : act_silu(g); + else if (act == 1) v = act_gelu(g); + else if (act == 2) v = act_gelu_tanh(g); + else if (act == 3) v = act_sigmoid(g); + else if (act == 5) v = g > 0.0f ? g : 0.0f; + else v = act_sigmoid(round_dt(y.dtype, g * k1702)); + rad_store_f32(y.base, y.dtype, r * y.pitch + i, + round_dt(y.dtype, v) * rad_load_f32(rb.base, rb.dtype, r * rb.pitch + i)); + }); + }); +} + +int gelu_mul(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 2)) return refuse("gelu_mul: missing operand"); + const RadTensor *GU = &a->t[0], *Y = &a->t[1]; + const int64_t n = rad_args_geti_or(a, "n", rad_tensor_numel(Y)); + if (n <= 0) return RAD_E_SHAPE; + Rows y, gu; + if (int rc = rows_view(Y, n, &y, "gelu_mul y"); rc < 0) return rc; + const int64_t rows = y.rows; + if (rows <= 0 || rad_tensor_numel(GU) < rows * 2 * n) return RAD_E_SHAPE; + if (int rc = rows_view(GU, 2 * n, &gu, "gelu_mul gate_up"); rc < 0) return rc; + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const int64_t total = rows * n; + const size_t wg = 256; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{groups_of(total, wg) * 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 x = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + i); + const float up = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + n + i); + rad_store_f32(y.base, y.dtype, r * y.pitch + i, act_gelu_tanh(x) * up); + }); + }); +} + +} // namespace xe2 diff --git a/libxe2/xe2_ops.h b/libxe2/xe2_ops.h index cb091476..3e130719 100644 --- a/libxe2/xe2_ops.h +++ b/libxe2/xe2_ops.h @@ -11,6 +11,17 @@ namespace xe2 { int add(const RadArgs* a, RadStream s); int mul(const RadArgs* a, RadStream s); int sigmoid(const RadArgs* a, RadStream s); +/* xe2_elem_act.cpp */ +int gelu(const RadArgs* a, RadStream s); +int silu(const RadArgs* a, RadStream s); +int gelu_tanh(const RadArgs* a, RadStream s); +int scale(const RadArgs* a, RadStream s); +int mul_weight(const RadArgs* a, RadStream s); +int clamp(const RadArgs* a, RadStream s); +int fill(const RadArgs* a, RadStream s); +int iota(const RadArgs* a, RadStream s); +int act_mul(const RadArgs* a, RadStream s); +int gelu_mul(const RadArgs* a, RadStream s); int scatter_rows(const RadArgs* a, RadStream s); int layernorm(const RadArgs* a, RadStream s); int grid_embed(const RadArgs* a, RadStream s); diff --git a/libxe2/xe2_rows.cpp b/libxe2/xe2_rows.cpp index b1ca6255..9389276c 100644 --- a/libxe2/xe2_rows.cpp +++ b/libxe2/xe2_rows.cpp @@ -60,6 +60,46 @@ const RadKernelInfo kKernels[] = { .computes = "1 / (1 + exp(-x)), the exp in double rounded once as libref's", .shape = "any element count; any strides", .dtypes = "any float dtype in and out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sigmoid }, + { .name = "xe2_gelu", .op = "gelu", .family = "elem", + .computes = "0.5x(1 + erf(x / sqrt 2)), erf in double rounded once, as libref's", + .shape = "any element count; any strides", .dtypes = "any float dtype in and out", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gelu }, + { .name = "xe2_silu", .op = "silu", .family = "elem", + .computes = "x sigmoid(x), exp in double (a bf16 result: a float exp, libref's chain near a tie)", + .shape = "any element count; any strides", .dtypes = "any float dtype in and out", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::silu }, + { .name = "xe2_gelu_tanh", .op = "gelu_tanh", .family = "elem", + .computes = "the tanh gelu, tanh in double rounded once, as libref's", + .shape = "any element count; any strides", .dtypes = "any float dtype in and out", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gelu_tanh }, + { .name = "xe2_scale", .op = "scale", .family = "elem", + .computes = "y = x * s, s a one-element weight", + .shape = "any element count; any strides", .dtypes = "any float x, s and y", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::scale }, + { .name = "xe2_mul_weight", .op = "mul_weight", .family = "elem", + .computes = "y = x * w, w one element or one row broadcast down the rows; f32 product rounded once", + .shape = "rows of n, uniform row pitch", .dtypes = "any float x, w and y", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::mul_weight }, + { .name = "xe2_clamp", .op = "clamp", .family = "elem", + .computes = "y = min(max(x, lo), hi), lo and hi one-element weights", + .shape = "rows of n, uniform row pitch", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::clamp }, + { .name = "xe2_fill", .op = "fill", .family = "elem", + .computes = "y = value over [M, n], rounded once to y's dtype", + .shape = "rows of n, uniform row pitch", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::fill }, + { .name = "xe2_iota", .op = "iota", .family = "elem", + .computes = "y[i] = start + (period ? i % period : i) * step", + .shape = "any element count; any strides", .dtypes = "i32, u32, i64, i16, i8, u8", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::iota }, + { .name = "xe2_act_mul", .op = "act_mul", .family = "elem", + .computes = "y = rd(act(a)) * b; silu, gelu, gelu_tanh, sigmoid, quick_gelu, relu in libref's chain", + .shape = "rows of n, uniform row pitch (column slices of one fused row)", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::act_mul }, + { .name = "xe2_gelu_mul", .op = "gelu_mul", .family = "elem", + .computes = "y = gelu_tanh(gate_up[i]) * gate_up[n + i], the gate first, tanh in double", + .shape = "rows of 2n in, n out", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gelu_mul }, { .name = "xe2_scatter_rows", .op = "scatter_rows", .family = "gather", .computes = "row m of v to row idx[m] of x; a negative index skipped, one past x refused", .shape = "any n and row counts; rows may be padded, the last axis dense",