/* 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(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