/* 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 #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); }); }); } /* 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