Archived
libxe2: rmsnorm_p, relu, logit_softcap, rel_sinusoid, pool_rows rows; rope to head_dim 512
Gemma 4 E2B did not start on Intel: five weightless ops had no device row (rmsnorm_p, the bare value norm, in 15 bands; relu; logit_softcap; rel_sinusoid; pool_rows), and xe2_rope was selected for its global layers (head_dim 512, proportional rope widened to the full head) and refused them at launch: the frequency table held head_dim <= 256. It now holds 512, its component index as int8 so the by-value table stays at 1.25 KB. rmsnorm_p is xe2_rmsnorm with the gain absent; the others are libref's chains (tanh, exp, sin, cos in double rounded once; pool_rows' f32 sum in row order, rounded, times mult, rounded). rad-kbench on the Gemma 4 E2B container: rmsnorm_p 20, relu 20, logit_softcap 4, rel_sinusoid 5, pool_rows 6 cases ok, rope 300/300; default fixture rope 162/162. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e1e21935cc
commit
ff4b7c9fb4
4 files changed
+147
-3
No files matched your search
@@ -21,6 +21,7 @@
|
||||
#include "xe2_common.h"
|
||||
#include "xe2_ops.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user