diff --git a/libxe2/xe2_elem_act.cpp b/libxe2/xe2_elem_act.cpp index 36b08a10..07ba5d93 100644 --- a/libxe2/xe2_elem_act.cpp +++ b/libxe2/xe2_elem_act.cpp @@ -21,6 +21,7 @@ #include "xe2_common.h" #include "xe2_ops.h" +#include #include namespace xe2 { @@ -268,4 +269,122 @@ int gelu_mul(const RadArgs* a, RadStream s) { }); } +/* rmsnorm with no gain (Gemma's bare value norm): libxe2's rmsnorm with its gain operand absent */ +int rmsnorm_p(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 2)) return refuse("rmsnorm_p: missing operand"); + RadTensor t[3] = { a->t[0], {}, a->t[1] }; + RadArgs sub = *a; + sub.t = t; + sub.n_t = 3; + return rmsnorm(&sub, s); +} + +int relu(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 2)) return refuse("relu: missing operand"); + const RadTensor *X = &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, x; + if (int rc = rows_view(Y, n, &y, "relu 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, "relu x"); 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 v = rad_load_f32(x.base, x.dtype, r * x.pitch + i); + rad_store_f32(y.base, y.dtype, r * y.pitch + i, v > 0.0f ? v : 0.0f); + }); + }); +} + +int logit_softcap(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 2)) return refuse("logit_softcap: missing operand"); + 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 float cap = (float)rad_args_getf_or(a, "cap", 30.0); + if (!(cap > 0.0f)) return RAD_E_INVAL; + 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; + 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)); + rad_store_f32(y.base, y.dtype, y.off(i), cap * d_tanh(v / cap)); + }); + }); +} + +/* row d: sin(d inv[i]) | cos(d inv[i]), inv[i] = exp(i * -inc), inc formed on the host as libref's */ +int rel_sinusoid(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 1)) return refuse("rel_sinusoid: missing operand"); + const RadTensor* Y = &a->t[0]; + const int64_t n = rad_args_geti_or(a, "n", 0); + if (n <= 0 || n % 2) return RAD_E_SHAPE; + Rows y; + if (int rc = rows_view(Y, n, &y, "rel_sinusoid y"); rc < 0) return rc; + const int64_t rows = y.rows; + if (rows <= 0) return RAD_E_SHAPE; + const int64_t h = n / 2; + const double mt = (double)(float)rad_args_getf_or(a, "max_timescale", 10000.0); + const float inc = (float)(std::log(mt / 1.0) / (double)(h - 1 > 1 ? h - 1 : 1)); + sycl::queue* q = queue_of(s); + if (!q) return RAD_E_DEVICE; + const int64_t total = rows * h; + 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 d = g / h, i = g % h; + const float inv = d_exp((float)i * -inc); + const float ang = (float)d * inv; + rad_store_f32(y.base, y.dtype, d * y.pitch + i, (float)sycl::sin((double)ang)); + rad_store_f32(y.base, y.dtype, d * y.pitch + h + i, (float)sycl::cos((double)ang)); + }); + }); +} + +/* row r: the mean of x's rows [r k, r k + k) summed in f32 in order, rounded, times mult, rounded */ +int pool_rows(const RadArgs* a, RadStream s) { + if (!rad_args_have(a, 2)) return refuse("pool_rows: missing operand"); + const RadTensor *X = &a->t[0], *Y = &a->t[1]; + const int64_t n = rad_args_geti_or(a, "n", 0), k = rad_args_geti_or(a, "k", 0); + const float mult = (float)rad_args_getf_or(a, "mult", 1.0); + if (n <= 0 || k <= 0) return RAD_E_SHAPE; + Rows x, y; + if (int rc = rows_view(X, n, &x, "pool_rows x"); rc < 0) return rc; + const int64_t rows_in = x.rows; + if (rows_in <= 0 || rows_in % k) return RAD_E_SHAPE; + const int64_t rows = rows_in / k; + if (rad_tensor_numel(Y) < rows * n) return RAD_E_SHAPE; + if (int rc = rows_view(Y, n, &y, "pool_rows y"); rc < 0) return rc; + const float inv = 1.0f / (float)k; + 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; + float acc = 0.0f; + for (int64_t j = 0; j < k; ++j) acc += rad_load_f32(x.base, x.dtype, (r * k + j) * x.pitch + i); + const float m = round_dt(y.dtype, acc * inv); + rad_store_f32(y.base, y.dtype, r * y.pitch + i, m * mult); + }); + }); +} + } // namespace xe2 diff --git a/libxe2/xe2_ops.h b/libxe2/xe2_ops.h index 3e130719..f0dfc0ed 100644 --- a/libxe2/xe2_ops.h +++ b/libxe2/xe2_ops.h @@ -22,6 +22,11 @@ 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 rmsnorm_p(const RadArgs* a, RadStream s); +int relu(const RadArgs* a, RadStream s); +int logit_softcap(const RadArgs* a, RadStream s); +int rel_sinusoid(const RadArgs* a, RadStream s); +int pool_rows(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_rope.cpp b/libxe2/xe2_rope.cpp index dec86362..ab86bd1e 100644 --- a/libxe2/xe2_rope.cpp +++ b/libxe2/xe2_rope.cpp @@ -24,7 +24,7 @@ namespace xe2 { namespace { -constexpr int kMaxHalf = 128; /* head_dim up to 256 */ +constexpr int kMaxHalf = 256; /* head_dim up to 512 (Gemma 4's global heads, proportional rope) */ /* libref's row_stride: fold trailing axes until they span `cols`; the next axis out carries the * row. */ @@ -47,7 +47,7 @@ enum { kNeox = 0, kGptj = 1, kMrope = 2, kImrope = 3, kAxial = 4 }; struct Freqs { float inv[kMaxHalf]; - int comp[kMaxHalf]; + int8_t comp[kMaxHalf]; /* a component index, < 8: int8 keeps the by-value table at 1.25 KB */ }; /* "11 11 10" -> {11, 11, 10}; absent is none, -1 a non-positive entry (libref's rope_sections). */ diff --git a/libxe2/xe2_rows.cpp b/libxe2/xe2_rows.cpp index 9389276c..ded4e5ff 100644 --- a/libxe2/xe2_rows.cpp +++ b/libxe2/xe2_rows.cpp @@ -100,6 +100,26 @@ const RadKernelInfo kKernels[] = { .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_rmsnorm_p", .op = "rmsnorm_p", .family = "elem", + .computes = "rmsnorm with no gain: xe2_rmsnorm with its gain operand absent", + .shape = "as xe2_rmsnorm", .dtypes = "as xe2_rmsnorm", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rmsnorm_p }, + { .name = "xe2_relu", .op = "relu", .family = "elem", + .computes = "y = max(x, 0)", + .shape = "rows of n, uniform row pitch", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::relu }, + { .name = "xe2_logit_softcap", .op = "logit_softcap", .family = "elem", + .computes = "y = cap tanh(x / cap), tanh in double rounded once", + .shape = "any element count; any strides", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::logit_softcap }, + { .name = "xe2_rel_sinusoid", .op = "rel_sinusoid", .family = "elem", + .computes = "row d: sin then cos of d inv[i], exp, sin and cos in double rounded once, as libref's", + .shape = "rows of n, n even", .dtypes = "any float y", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rel_sinusoid }, + { .name = "xe2_pool_rows", .op = "pool_rows", .family = "elem", + .computes = "the mean of k consecutive rows, f32 in order, rounded, times mult, rounded", + .shape = "rows of n, M a multiple of k", .dtypes = "any float dtype", + .domain = RAD_DOMAIN_DEVICE, .launch = xe2::pool_rows }, { .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", @@ -167,7 +187,7 @@ const RadKernelInfo kKernels[] = { { .name = "xe2_rope", .op = "rope", .family = "rope", .computes = "rotary embedding in place over q and k heads; frequency ladder (YaRN, llama3) " "on the host as libref builds it, cos/sin in double", - .shape = "neox, gptj, mrope, imrope, axial; [M] or [C, M] positions, rotary_dim <= 256", + .shape = "neox, gptj, mrope, imrope, axial; [M] or [C, M] positions, rotary_dim <= 512", .dtypes = "bf16 qkv (any float), integer or float positions", .domain = RAD_DOMAIN_DEVICE, .constraints = cRopeModes, .n_constraints = 1, .launch = xe2::rope },