ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / element_wise.cpp
| 1 | #include "common.hpp" |
| 2 | #include "ggml-sycl/presets.hpp" |
| 3 | #include "ggml.h" |
| 4 | #include "element_wise.hpp" |
| 5 | |
| 6 | #define SYCL_GLOBAL_ID_LOOP(K, ITEM) \ |
| 7 | for (auto i = ITEM.get_global_id(0); i < (size_t)K; i += ITEM.get_global_range(0)) |
| 8 | |
| 9 | #define SYCL_LOCAL_ID_CALC(ITEM, IDX) \ |
| 10 | (ITEM.get_local_range(IDX) * ITEM.get_group(IDX) + ITEM.get_local_id(IDX)) |
| 11 | |
| 12 | static void acc_f32(const char * x, const char * y, float * dst, const int64_t ne, |
| 13 | const int64_t ne0, const int64_t ne1, const int64_t ne2, |
| 14 | const int64_t nb00, const int64_t nb01, const int64_t nb02, const int64_t nb03, |
| 15 | const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t ne13, |
| 16 | const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, |
| 17 | const int64_t s11, const int64_t s12, const int64_t s13, const int64_t offset) { |
| 18 | auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 19 | const int64_t i = SYCL_LOCAL_ID_CALC(item_ct1, 2); |
| 20 | |
| 21 | if (i >= ne) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | int64_t src1_idx = i - offset; |
| 26 | |
| 27 | int64_t tmp = src1_idx; |
| 28 | const int64_t i13 = tmp / s13; |
| 29 | tmp -= i13 * s13; |
| 30 | const int64_t i12 = tmp / s12; |
| 31 | tmp -= i12 * s12; |
| 32 | const int64_t i11 = tmp / s11; |
| 33 | tmp -= i11 * s11; |
| 34 | const int64_t i10 = tmp; |
| 35 | |
| 36 | int64_t tmp_dst = i; |
| 37 | const int64_t i3 = tmp_dst / (ne2*ne1*ne0); |
| 38 | tmp_dst -= i3 * (ne2*ne1*ne0); |
| 39 | const int64_t i2 = tmp_dst / (ne1*ne0); |
| 40 | tmp_dst -= i2 * (ne1*ne0); |
| 41 | const int64_t i1 = tmp_dst / ne0; |
| 42 | tmp_dst -= i1 * ne0; |
| 43 | const int64_t i0 = tmp_dst; |
| 44 | |
| 45 | float val = *(const float *) (x + i0*nb00 + i1*nb01 + i2*nb02 + i3*nb03); |
| 46 | if (src1_idx >= 0 && i10 < ne10 && i11 < ne11 && i12 < ne12 && i13 < ne13) { |
| 47 | val += *(const float *) (y + i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13); |
| 48 | } |
| 49 | dst[i] = val; |
| 50 | } |
| 51 | |
| 52 | /* Unary OP funcs */ |
| 53 | template<typename T> |
| 54 | static __dpct_inline__ T op_sgn(T x) { |
| 55 | return x > static_cast<T>(0.f) ? static_cast<T>(1.f) : ((x < static_cast<T>(0.f) ? static_cast<T>(-1.f) : static_cast<T>(0.f))); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | template<typename T> |
| 60 | static __dpct_inline__ T op_abs(T x) { |
| 61 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 62 | return sycl::ext::oneapi::experimental::fabs(x); // or experimental namespace if needed |
| 63 | } else { |
| 64 | return sycl::fabs(x); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | template<typename T> |
| 69 | static __dpct_inline__ T op_expm1(T x) { |
| 70 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 71 | return static_cast<sycl::ext::oneapi::bfloat16>( |
| 72 | sycl::expm1(static_cast<float>(x)) |
| 73 | ); |
| 74 | } else { |
| 75 | return sycl::expm1(x); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | template<typename T> |
| 80 | static __dpct_inline__ T op_elu(T x) { |
| 81 | return (x > static_cast<T>(0.f)) ? x : op_expm1(x); |
| 82 | } |
| 83 | |
| 84 | template<typename T> |
| 85 | static __dpct_inline__ T op_erf(T x) { |
| 86 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 87 | return static_cast<sycl::ext::oneapi::bfloat16>( |
| 88 | sycl::erf(static_cast<float>(x)) |
| 89 | ); |
| 90 | } else { |
| 91 | return sycl::erf(x); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | template<typename T> |
| 96 | static __dpct_inline__ T op_gelu_erf(T x) { |
| 97 | const T SQRT_2_INV = static_cast<T>(0.70710678118654752440084436210484f); |
| 98 | return static_cast<T>(0.5f) * x * (static_cast<T>(1.0f) + op_erf(x * SQRT_2_INV)); |
| 99 | } |
| 100 | |
| 101 | template<typename T> |
| 102 | static __dpct_inline__ T op_gelu_quick(T x) { |
| 103 | const T GELU_QUICK_COEF_LOCAL = static_cast<T>(-1.702f); |
| 104 | return x * (static_cast<T>(1.0f) / (static_cast<T>(1.0f) + op_exp(GELU_QUICK_COEF_LOCAL * x))); |
| 105 | } |
| 106 | |
| 107 | template<typename T> |
| 108 | static __dpct_inline__ T op_relu(T x) { |
| 109 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 110 | return sycl::ext::oneapi::experimental::fmax(x, static_cast<T>(0)); |
| 111 | } else { |
| 112 | return sycl::fmax(x, static_cast<T>(0)); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | template<typename T> |
| 117 | static __dpct_inline__ T op_sigmoid(T x) { |
| 118 | return static_cast<T>(1.0f) / (static_cast<T>(1.0f) + op_exp(-x)); |
| 119 | } |
| 120 | |
| 121 | template<typename T> |
| 122 | static __dpct_inline__ T op_sqrt(T x) { |
| 123 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 124 | return sycl::ext::oneapi::experimental::sqrt(x); |
| 125 | } else { |
| 126 | return sycl::sqrt(x); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | template<typename T> |
| 131 | static __dpct_inline__ T op_sin(T x) { |
| 132 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 133 | return sycl::ext::oneapi::experimental::sin(x); |
| 134 | } else { |
| 135 | return sycl::sin(x); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | template<typename T> |
| 140 | static __dpct_inline__ T op_cos(T x) { |
| 141 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 142 | return sycl::ext::oneapi::experimental::cos(x); |
| 143 | } else { |
| 144 | return sycl::cos(x); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | template<typename T> |
| 149 | static __dpct_inline__ T op_hardsigmoid(T x) { |
| 150 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 151 | return sycl::ext::oneapi::experimental::fmin( |
| 152 | static_cast<T>(1.0f), sycl::ext::oneapi::experimental::fmax( |
| 153 | static_cast<T>(0.0f), (x + static_cast<T>(3.0f)) / static_cast<T>(6.0f))); |
| 154 | } else { |
| 155 | return sycl::fmin(static_cast<T>(1.0f), |
| 156 | sycl::fmax(static_cast<T>(0.0f), (x + static_cast<T>(3.0f)) / static_cast<T>(6.0f))); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | template<typename T> |
| 161 | static __dpct_inline__ T op_hardswish(T x) { |
| 162 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 163 | return x * sycl::ext::oneapi::experimental::fmin(static_cast<T>(1.0f), sycl::ext::oneapi::experimental::fmax(static_cast<T>(0.0f), (x + static_cast<T>(3.0f)) / static_cast<T>(6.0f))); |
| 164 | } else { |
| 165 | return x * sycl::fmin(static_cast<T>(1.0f), sycl::fmax(static_cast<T>(0.0f), (x + static_cast<T>(3.0f)) / static_cast<T>(6.0f))); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | template<typename T> |
| 170 | static __dpct_inline__ T op_log(T x) { |
| 171 | if (x <= static_cast<T>(0)) { |
| 172 | return neg_infinity<T>(); |
| 173 | } |
| 174 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 175 | return sycl::ext::oneapi::experimental::log(x); |
| 176 | } else { |
| 177 | return sycl::log(x); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | template<typename T> |
| 182 | static __dpct_inline__ T op_softplus(T x) { |
| 183 | const float xf = (float) x; |
| 184 | const float ax = op_abs(xf); |
| 185 | const float m = sycl::fmax(xf, 0.0f); |
| 186 | const float y = m + sycl::log1p(sycl::exp(-ax)); |
| 187 | return (T) y; |
| 188 | } |
| 189 | |
| 190 | template<typename T> |
| 191 | static __dpct_inline__ T op_neg(T x) { |
| 192 | return -x; |
| 193 | } |
| 194 | |
| 195 | template<typename T> |
| 196 | static __dpct_inline__ T op_step(T x) { |
| 197 | return (x > static_cast<T>(0.0f)) ? static_cast<T>(1.0f) : static_cast<T>(0.0f); |
| 198 | } |
| 199 | |
| 200 | template<typename T> |
| 201 | static __dpct_inline__ T op_leaky_relu(T x, float negative_slope) { |
| 202 | T neg_slope_T = static_cast<T>(negative_slope); |
| 203 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 204 | return sycl::ext::oneapi::experimental::fmax(x, static_cast<T>(0)) + |
| 205 | sycl::ext::oneapi::experimental::fmin(x, static_cast<T>(0.0f)) * neg_slope_T; |
| 206 | |
| 207 | } else { |
| 208 | return sycl::fmax(x, static_cast<T>(0)) + |
| 209 | sycl::fmin(x, static_cast<T>(0.0f)) * neg_slope_T; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | template<typename T> |
| 214 | static __dpct_inline__ T op_xielu(T x, float alpha_n, float alpha_p, float beta, float eps) { |
| 215 | const float xi = static_cast<float>(x); |
| 216 | const float gate_pos = (xi > 0.0f); |
| 217 | const float y_pos = alpha_p * xi * xi + beta * xi; |
| 218 | const float min_v_eps = sycl::fmin(xi, eps); |
| 219 | const float y_neg = (sycl::expm1(min_v_eps) - xi) * alpha_n + beta * xi; |
| 220 | const float out = gate_pos * y_pos + (1.0f - gate_pos) * y_neg; |
| 221 | return static_cast<T>(out); |
| 222 | } |
| 223 | |
| 224 | template<typename T> |
| 225 | static __dpct_inline__ T op_sqr(T x) { |
| 226 | return x * x; |
| 227 | } |
| 228 | |
| 229 | template<typename T> |
| 230 | static __dpct_inline__ T op_clamp(T x, float min_val, float max_val) { |
| 231 | return x < static_cast<T>(min_val) ? static_cast<T>(min_val) : (x > static_cast<T>(max_val) ? static_cast<T>(max_val) : x); |
| 232 | } |
| 233 | |
| 234 | template<typename T> |
| 235 | static __dpct_inline__ T op_floor(T x) { |
| 236 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 237 | return sycl::ext::oneapi::experimental::floor(x); |
| 238 | } else { |
| 239 | return sycl::floor(x); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | template<typename T> |
| 244 | static __dpct_inline__ T op_ceil(T x) { |
| 245 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 246 | return sycl::ext::oneapi::experimental::ceil(x); |
| 247 | } else { |
| 248 | return sycl::ceil(x); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | template<typename T> |
| 253 | static __dpct_inline__ T op_round(T x) { |
| 254 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 255 | return static_cast<sycl::ext::oneapi::bfloat16>( |
| 256 | sycl::round(static_cast<float>(x)) |
| 257 | ); |
| 258 | } else { |
| 259 | return sycl::round(x); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | template<typename T> |
| 264 | static __dpct_inline__ T op_trunc(T x) { |
| 265 | if constexpr (std::is_same_v<T, sycl::ext::oneapi::bfloat16>) { |
| 266 | return sycl::ext::oneapi::experimental::trunc(x); |
| 267 | } else { |
| 268 | return sycl::trunc(x); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | template<typename T, typename F> |
| 273 | static void unary_op_flat_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> & item_ct1, F func) { |
| 274 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 275 | dst[i] = func(x[i]); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | template<typename T, typename F> |
| 280 | static void unary_op_generic_kernel( |
| 281 | const T * x, |
| 282 | T * dst, |
| 283 | const int k, |
| 284 | const sycl::uint3 ne0_fd, const sycl::uint3 ne1_fd, const sycl::uint3 ne2_fd, |
| 285 | const size_t nb0, const size_t nb1, const size_t nb2, const size_t nb3, |
| 286 | const size_t nbd0, const size_t nbd1, const size_t nbd2, const size_t nbd3, |
| 287 | const sycl::nd_item<1> & item_ct1, |
| 288 | F func) { |
| 289 | |
| 290 | // 32-bit index math: k is int, so every logical index fits u32. 64-bit integer div/mod is |
| 291 | // emulated on Xe and dominates this kernel otherwise, and even the 32-bit divide is worth |
| 292 | // avoiding -- the divisors are launch-invariant, so the magic numbers are precomputed |
| 293 | // host-side and each division becomes a multiply-high plus a shift. |
| 294 | // Byte offsets are widened back to size_t only for the final address math. |
| 295 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 296 | sycl::uint2 dm = fast_div_modulo((uint32_t) i, ne0_fd); |
| 297 | const uint32_t i0 = dm.y(); |
| 298 | dm = fast_div_modulo(dm.x(), ne1_fd); |
| 299 | const uint32_t i1 = dm.y(); |
| 300 | dm = fast_div_modulo(dm.x(), ne2_fd); |
| 301 | const uint32_t i2 = dm.y(); |
| 302 | const uint32_t i3 = dm.x(); |
| 303 | |
| 304 | const char * src_base = (const char *) x; |
| 305 | char * dst_base = (char *) dst; |
| 306 | |
| 307 | const T * srcp = (const T *)(src_base + (size_t) i0*nb0 + (size_t) i1*nb1 + (size_t) i2*nb2 + (size_t) i3*nb3 ); |
| 308 | T * dstp = (T *)(dst_base + (size_t) i0*nbd0 + (size_t) i1*nbd1 + (size_t) i2*nbd2 + (size_t) i3*nbd3); |
| 309 | |
| 310 | *dstp = func(*srcp); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | template<typename T> |
| 315 | static void unary_op_sqrt_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1) { |
| 316 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 317 | dst[i] = op_sqrt(x[i]); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | template<typename T> |
| 322 | static void unary_op_sin_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1) { |
| 323 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 324 | dst[i] = op_sin(x[i]); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | template<typename T> |
| 329 | static void unary_op_cos_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1) { |
| 330 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 331 | dst[i] = op_cos(x[i]); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | template<typename T> |
| 336 | static void unary_op_log_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1) { |
| 337 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 338 | dst[i] = op_log(x[i]); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | template<typename T> |
| 344 | static void unary_op_leaky_relu_kernel(const T * x, T * dst, const int k, float negative_slope, const sycl::nd_item<1> &item_ct1) { |
| 345 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 346 | dst[i] = op_leaky_relu(x[i], negative_slope); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | template<typename T> |
| 351 | static void unary_op_xielu_kernel(const T * x, T * dst, const int k, float alpha_n, float alpha_p, float beta, float eps, const sycl::nd_item<1> &item_ct1) { |
| 352 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 353 | dst[i] = op_xielu(x[i], alpha_n, alpha_p, beta, eps); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | template<typename T> |
| 358 | static void unary_op_sqr_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1) { |
| 359 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 360 | dst[i] = op_sqr(x[i]); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | template<typename T> |
| 365 | static void unary_op_clamp_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1, float min_val, float max_val) { |
| 366 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 367 | dst[i] = op_clamp(x[i], min_val, max_val); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | template<typename T> |
| 372 | static void unary_op_ceil_kernel(const T * x, T * dst, const int k, const sycl::nd_item<1> &item_ct1) { |
| 373 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 374 | dst[i] = op_ceil(x[i]); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | template<typename T> |
| 379 | static void clamp(const T * x, T * dst, const float min, const float max, const int k, |
| 380 | const sycl::nd_item<1> &item_ct1) { |
| 381 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 382 | dst[i] = x[i] < static_cast<T>(min) ? static_cast<T>(min) : (x[i] > static_cast<T>(max) ? static_cast<T>(max) : x[i]); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | template<typename T, typename F> |
| 387 | static void unary_gated_op_flat_kernel(const T * x, const T * g, T * dst, const uint64_t k, const sycl::nd_item<1> & item_ct1, F func) { |
| 388 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 389 | dst[i] = func(x[i]) * g[i]; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | template<typename T, typename F> |
| 394 | static void unary_gated_op_generic_kernel( |
| 395 | const T * x, |
| 396 | const T * g, |
| 397 | T * dst, |
| 398 | const uint64_t k, |
| 399 | const sycl::uint3 n_fd, |
| 400 | const uint64_t o0, |
| 401 | const uint64_t o1, |
| 402 | const sycl::nd_item<1> & item_ct1, |
| 403 | F func) { |
| 404 | |
| 405 | // rows of n columns at strides o0 and o1: two halves of one fused tensor, or two tensors |
| 406 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 407 | const sycl::uint2 rc = fast_div_modulo((uint32_t) i, n_fd); |
| 408 | const int64_t j0 = rc.x() * o0 + rc.y(); |
| 409 | const int64_t j1 = o0 == o1 ? j0 : rc.x() * o1 + rc.y(); |
| 410 | dst[i] = func(x[j0]) * g[j1]; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Fused UNARY + MUL. Unlike the gated ops above, `x` and `g` are separate tensors of the |
| 415 | // same shape; `o0`/`o1` are their row strides in elements, so a half-view needs no repack. |
| 416 | // `dst` is contiguous and indexed flat. Math is done in f32, as the CPU and CUDA references do. |
| 417 | template<typename T, typename F> |
| 418 | static void unary_mul_flat_kernel(const T * x, const T * g, T * dst, const int64_t k, const sycl::nd_item<1> &item_ct1, F op) { |
| 419 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 420 | dst[i] = (T) (op((float) x[i]) * (float) g[i]); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | template<typename T, typename F> |
| 425 | static void unary_mul_strided_kernel(const T * x, const T * g, T * dst, const int64_t k, const sycl::uint3 n_fd, const int64_t o0, const int64_t o1, const sycl::nd_item<1> &item_ct1, F op) { |
| 426 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 427 | const sycl::uint2 rc = fast_div_modulo((uint32_t) i, n_fd); |
| 428 | const int64_t j0 = rc.x() * o0 + rc.y(); |
| 429 | const int64_t j1 = o0 == o1 ? j0 : rc.x() * o1 + rc.y(); |
| 430 | dst[i] = (T) (op((float) x[j0]) * (float) g[j1]); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | template<typename T, typename F> |
| 435 | static void unary_mul_sycl(const T * x, const T * g, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1, queue_ptr main_stream, F op) { |
| 436 | const size_t num_blocks = ceil_div((size_t) k, (size_t) SYCL_GLU_BLOCK_SIZE); |
| 437 | const sycl::nd_range<1> range(num_blocks * sycl::range<1>(SYCL_GLU_BLOCK_SIZE), sycl::range<1>(SYCL_GLU_BLOCK_SIZE)); |
| 438 | |
| 439 | // o0 == o1 == n makes (i/n)*o0 + (i%n) == i, so the strided kernel degenerates to the flat one |
| 440 | if (o0 == n && o1 == n) { |
| 441 | main_stream->parallel_for(range, [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 442 | unary_mul_flat_kernel(x, g, dst, k, item_ct1, op); |
| 443 | }); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | // 32-bit fastdiv, exact only below 2^31; ggml_sycl_can_fuse() already declined past that |
| 448 | GGML_ASSERT(k < ((int64_t) 1 << 31)); |
| 449 | const sycl::uint3 n_fd = init_fastdiv_values((uint32_t) n); |
| 450 | main_stream->parallel_for(range, [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 451 | unary_mul_strided_kernel(x, g, dst, k, n_fd, o0, o1, item_ct1, op); |
| 452 | }); |
| 453 | } |
| 454 | |
| 455 | // ADD(bias) + UNARY + MUL(scale) with both broadcast over dim 0, the delta-net alpha gate: |
| 456 | // dst[i] = op(a[i] + bias[i % ne0]) * scale[i % ne0]. k == ne0 makes that the flat index. |
| 457 | template<typename F> |
| 458 | static void add_unary_mul_flat_kernel(const float * a, const float * bias, const float * scale, float * dst, |
| 459 | const int64_t k, const sycl::nd_item<1> &item_ct1, F op) { |
| 460 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 461 | dst[i] = op(a[i] + bias[i]) * scale[i]; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | template<typename F> |
| 466 | static void add_unary_mul_bcast_kernel(const float * a, const float * bias, const float * scale, float * dst, |
| 467 | const int64_t k, const sycl::uint3 ne0_fd, const sycl::nd_item<1> &item_ct1, F op) { |
| 468 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 469 | const uint32_t h = fastmodulo((uint32_t) i, ne0_fd); |
| 470 | dst[i] = op(a[i] + bias[h]) * scale[h]; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | template<typename F> |
| 475 | static void add_unary_mul_sycl(const float * a, const float * bias, const float * scale, float * dst, |
| 476 | const int64_t k, const int64_t ne0, queue_ptr main_stream, F op) { |
| 477 | const size_t num_blocks = ceil_div((size_t) k, (size_t) SYCL_GLU_BLOCK_SIZE); |
| 478 | const sycl::nd_range<1> range(num_blocks * sycl::range<1>(SYCL_GLU_BLOCK_SIZE), sycl::range<1>(SYCL_GLU_BLOCK_SIZE)); |
| 479 | |
| 480 | if (k == ne0) { |
| 481 | main_stream->parallel_for(range, [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 482 | add_unary_mul_flat_kernel(a, bias, scale, dst, k, item_ct1, op); |
| 483 | }); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | // 32-bit fastdiv, exact only below 2^31; ggml_sycl_can_fuse() already declined past that |
| 488 | GGML_ASSERT(k < ((int64_t) 1 << 31)); |
| 489 | const sycl::uint3 ne0_fd = init_fastdiv_values((uint32_t) ne0); |
| 490 | main_stream->parallel_for(range, [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 491 | add_unary_mul_bcast_kernel(a, bias, scale, dst, k, ne0_fd, item_ct1, op); |
| 492 | }); |
| 493 | } |
| 494 | |
| 495 | namespace ggml_sycl_detail { |
| 496 | static void acc_f32_sycl(const char *x, const char *y, float *dst, |
| 497 | const int64_t n_elements, |
| 498 | const int64_t ne0, const int64_t ne1, const int64_t ne2, |
| 499 | const int64_t nb00, const int64_t nb01, const int64_t nb02, const int64_t nb03, |
| 500 | const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t ne13, |
| 501 | const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, |
| 502 | const int64_t s1, const int64_t s2, const int64_t s3, |
| 503 | const int64_t offset, queue_ptr stream) { |
| 504 | const int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE; |
| 505 | stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE), |
| 506 | sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)), |
| 507 | [=](sycl::nd_item<3> /*item_ct1*/) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 508 | acc_f32(x, y, dst, n_elements, |
| 509 | ne0, ne1, ne2, |
| 510 | nb00, nb01, nb02, nb03, |
| 511 | ne10, ne11, ne12, ne13, |
| 512 | nb10, nb11, nb12, nb13, |
| 513 | s1, s2, s3, offset); |
| 514 | }); |
| 515 | } |
| 516 | |
| 517 | template<typename T> |
| 518 | static void arange_kernel(T * dst, const int k, T start, T step, |
| 519 | const sycl::nd_item<1> &item_ct1) { |
| 520 | SYCL_GLOBAL_ID_LOOP(k, item_ct1) { |
| 521 | dst[i] = start + static_cast<T>(i) * step; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | template<typename KernelInvoker, typename... Args> |
| 526 | static inline void dispatch_ggml_sycl_op_unary(ggml_backend_sycl_context & ctx, ggml_tensor * dst, KernelInvoker kernel_invoker, Args&&... args) { |
| 527 | GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32 || dst->src[0]->type == GGML_TYPE_F16 || dst->src[0]->type == GGML_TYPE_BF16); |
| 528 | GGML_ASSERT(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_BF16); |
| 529 | GGML_ASSERT(dst->src[0]->type == dst->type); |
| 530 | |
| 531 | dpct::queue_ptr main_stream = ctx.stream(); |
| 532 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 533 | switch (dst->type) { |
| 534 | case GGML_TYPE_F16: |
| 535 | { |
| 536 | auto data_pts = cast_data<sycl::half>(dst); |
| 537 | kernel_invoker(data_pts.src, data_pts.dst, (int)ggml_nelements(dst->src[0]), main_stream, std::forward<Args>(args)...); |
| 538 | break; |
| 539 | } |
| 540 | #ifdef GGML_SYCL_HAS_BF16 |
| 541 | case GGML_TYPE_BF16: |
| 542 | { |
| 543 | auto data_pts = cast_data<sycl::ext::oneapi::bfloat16>(dst); |
| 544 | kernel_invoker(data_pts.src, data_pts.dst, (int)ggml_nelements(dst->src[0]), main_stream, std::forward<Args>(args)...); |
| 545 | break; |
| 546 | } |
| 547 | #endif |
| 548 | case GGML_TYPE_F32: |
| 549 | { |
| 550 | auto data_pts = cast_data<float>(dst); |
| 551 | kernel_invoker(data_pts.src, data_pts.dst, (int)ggml_nelements(dst->src[0]), main_stream, std::forward<Args>(args)...); |
| 552 | break; |
| 553 | } |
| 554 | default: |
| 555 | GGML_ABORT("GGML tensor type not supported!\n"); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | template<typename KernelInvoker, typename... Args> |
| 560 | static inline void dispatch_ggml_sycl_op_fused_glu(ggml_backend_sycl_context & ctx, ggml_tensor * dst, KernelInvoker kernel_invoker, Args&&... args) { |
| 561 | GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32 || dst->src[0]->type == GGML_TYPE_F16); |
| 562 | GGML_ASSERT(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16); |
| 563 | GGML_ASSERT(dst->src[0]->type == dst->type); |
| 564 | |
| 565 | dpct::queue_ptr main_stream = ctx.stream(); |
| 566 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 567 | const ggml_tensor * src0 = dst->src[0]; |
| 568 | const ggml_tensor * src1 = dst->src[1]; |
| 569 | const int64_t nc = src1 ? src0->ne[0] : src0->ne[0] / 2;; |
| 570 | GGML_ASSERT(dst->ne[0] == nc); |
| 571 | GGML_ASSERT(ggml_is_contiguous_1(dst->src[0])); |
| 572 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 573 | // The fused GLU kernels index with 32-bit fastdiv, which is exact only for indices below |
| 574 | // 2^31. A dst that large is ~8 GB at f32, and the grid sizing already narrows to 32 bits, |
| 575 | // so assert the bound rather than carry a second code path for it. |
| 576 | GGML_ASSERT(ggml_nelements(dst) < ((int64_t) 1 << 31)); |
| 577 | const int32_t swapped = ((const int32_t *) dst->op_params)[1]; |
| 578 | void * src0_d = src0->data; |
| 579 | void * src1_d = src1 ? src1->data : src0->data; |
| 580 | const int64_t src0_o = src0->nb[1]; |
| 581 | const int64_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 582 | void * dst_d = dst->data; |
| 583 | if (src1) { |
| 584 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 585 | GGML_ASSERT(src1->nb[0] == ggml_element_size(src1)); |
| 586 | GGML_ASSERT(src1->ne[0] == nc); |
| 587 | GGML_ASSERT(src0->type == src1->type); |
| 588 | } |
| 589 | switch (dst->type) { |
| 590 | case GGML_TYPE_F16: |
| 591 | { |
| 592 | sycl::half * src0_p = (sycl::half *) src0_d; |
| 593 | sycl::half * src1_p = (sycl::half *) src1_d; |
| 594 | |
| 595 | if (!src1) { |
| 596 | src0_p += swapped ? nc : 0; |
| 597 | src1_p += swapped ? 0 : nc; |
| 598 | } |
| 599 | kernel_invoker(src0_p, |
| 600 | src1_p, |
| 601 | (sycl::half *) dst_d, |
| 602 | ggml_nelements(dst), |
| 603 | nc, |
| 604 | src0_o / sizeof(sycl::half), |
| 605 | src1_o / sizeof(sycl::half), |
| 606 | main_stream, |
| 607 | std::forward<Args>(args)...); |
| 608 | break; |
| 609 | } |
| 610 | case GGML_TYPE_F32: |
| 611 | { |
| 612 | float * src0_p = (float *) src0_d; |
| 613 | float * src1_p = (float *) src1_d; |
| 614 | |
| 615 | if (!src1) { |
| 616 | src0_p += swapped ? nc : 0; |
| 617 | src1_p += swapped ? 0 : nc; |
| 618 | } |
| 619 | |
| 620 | kernel_invoker(src0_p, |
| 621 | src1_p, |
| 622 | (float *) dst_d, |
| 623 | ggml_nelements(dst), |
| 624 | nc, |
| 625 | src0_o / sizeof(float), |
| 626 | src1_o / sizeof(float), |
| 627 | main_stream, |
| 628 | std::forward<Args>(args)...); |
| 629 | break; |
| 630 | } |
| 631 | default: |
| 632 | GGML_ABORT("GGML tensor type not supported!\n"); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | template<typename F> |
| 637 | static inline void ggml_sycl_op_unary( |
| 638 | ggml_backend_sycl_context & ctx, ggml_tensor * dst, F func) { |
| 639 | |
| 640 | ggml_tensor * src0 = dst->src[0]; |
| 641 | |
| 642 | const int64_t ne0 = dst->ne[0]; |
| 643 | const int64_t ne1 = dst->ne[1]; |
| 644 | const int64_t ne2 = dst->ne[2]; |
| 645 | |
| 646 | const size_t nb0 = src0->nb[0]; |
| 647 | const size_t nb1 = src0->nb[1]; |
| 648 | const size_t nb2 = src0->nb[2]; |
| 649 | const size_t nb3 = src0->nb[3]; |
| 650 | |
| 651 | const size_t nbd0 = dst->nb[0]; |
| 652 | const size_t nbd1 = dst->nb[1]; |
| 653 | const size_t nbd2 = dst->nb[2]; |
| 654 | const size_t nbd3 = dst->nb[3]; |
| 655 | |
| 656 | // Hot unary ops (FFN/GDN silu, sigmoid, ...) run on contiguous tensors; |
| 657 | // skip the strided index math entirely for them. |
| 658 | const bool contiguous = ggml_is_contiguous(src0) && ggml_is_contiguous(dst); |
| 659 | |
| 660 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 661 | [=](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream) { |
| 662 | |
| 663 | const int num_blocks = ceil_div(k_elements, 256); |
| 664 | |
| 665 | if (contiguous) { |
| 666 | stream->parallel_for( |
| 667 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(256), |
| 668 | sycl::range<1>(256)), |
| 669 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 670 | unary_op_flat_kernel(src, dst_ptr, k_elements, item_ct1, func); |
| 671 | }); |
| 672 | } else { |
| 673 | // Launch-invariant divisors: compute the magic numbers once on the host so the |
| 674 | // kernel never issues an integer divide. Only the strided path needs them. |
| 675 | const sycl::uint3 ne0_fd = init_fastdiv_values((uint32_t) ne0); |
| 676 | const sycl::uint3 ne1_fd = init_fastdiv_values((uint32_t) ne1); |
| 677 | const sycl::uint3 ne2_fd = init_fastdiv_values((uint32_t) ne2); |
| 678 | stream->parallel_for( |
| 679 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(256), |
| 680 | sycl::range<1>(256)), |
| 681 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 682 | unary_op_generic_kernel( |
| 683 | src, dst_ptr, k_elements, |
| 684 | ne0_fd, ne1_fd, ne2_fd, |
| 685 | nb0, nb1, nb2, nb3, |
| 686 | nbd0, nbd1, nbd2, nbd3, |
| 687 | item_ct1, |
| 688 | func |
| 689 | ); |
| 690 | }); |
| 691 | } |
| 692 | }); |
| 693 | } |
| 694 | |
| 695 | template<typename F> |
| 696 | static inline void ggml_sycl_op_unary_gated( |
| 697 | ggml_backend_sycl_context & ctx, ggml_tensor * dst, F func) { |
| 698 | |
| 699 | dispatch_ggml_sycl_op_fused_glu(ctx, dst, |
| 700 | [func](const auto * x_ptr, const auto * g_ptr, auto * dst_ptr, uint64_t k, uint64_t n, uint64_t o0, uint64_t o1, queue_ptr main_stream) { |
| 701 | |
| 702 | const uint32_t num_blocks = (uint32_t) ceil_div(k, SYCL_GLU_BLOCK_SIZE); |
| 703 | const sycl::nd_range<1> launch_range(num_blocks * sycl::range<1>(SYCL_GLU_BLOCK_SIZE), |
| 704 | sycl::range<1>(SYCL_GLU_BLOCK_SIZE)); |
| 705 | |
| 706 | // o0 == n and o1 == n make the index math the identity, so index flat |
| 707 | // note: not ggml_is_contiguous - a fused [gate|up] src0 is contiguous with o0 == 2n |
| 708 | if (o0 == n && o1 == n) { |
| 709 | main_stream->parallel_for(launch_range, |
| 710 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 711 | unary_gated_op_flat_kernel(x_ptr, g_ptr, dst_ptr, k, item_ct1, func); |
| 712 | }); |
| 713 | } else { |
| 714 | // launch-invariant divisor, and only this path needs it |
| 715 | const sycl::uint3 n_fd = init_fastdiv_values((uint32_t) n); |
| 716 | main_stream->parallel_for(launch_range, |
| 717 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 718 | unary_gated_op_generic_kernel(x_ptr, g_ptr, dst_ptr, k, n_fd, o0, o1, item_ct1, func); |
| 719 | }); |
| 720 | } |
| 721 | }); |
| 722 | } |
| 723 | |
| 724 | |
| 725 | static inline void ggml_sycl_op_arange(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 726 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 727 | float start, stop, step; |
| 728 | memcpy(&start, dst->op_params, sizeof(float)); |
| 729 | memcpy(&stop, (float *) dst->op_params + 1, sizeof(float)); |
| 730 | memcpy(&step, (float *) dst->op_params + 2, sizeof(float)); |
| 731 | dpct::queue_ptr stream = ctx.stream(); |
| 732 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 733 | float * dst_ptr = (float *)dst->data; |
| 734 | const int k = (int)ggml_nelements(dst); |
| 735 | const int num_blocks = ceil_div(k, SYCL_ARANGE_BLOCK_SIZE); |
| 736 | stream->parallel_for( |
| 737 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_ARANGE_BLOCK_SIZE), |
| 738 | sycl::range<1>(SYCL_ARANGE_BLOCK_SIZE)), |
| 739 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 740 | arange_kernel(dst_ptr, k, start, step, item_ct1); |
| 741 | }); |
| 742 | } |
| 743 | |
| 744 | } // namespace ggml_sycl_detail |
| 745 | |
| 746 | |
| 747 | |
| 748 | static inline void ggml_sycl_op_sgn(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 749 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 750 | return op_sgn(x); |
| 751 | }); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | static inline void ggml_sycl_op_abs(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 756 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 757 | return op_abs(x); |
| 758 | }); |
| 759 | } |
| 760 | |
| 761 | static inline void ggml_sycl_op_elu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 762 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 763 | return op_elu(x); |
| 764 | }); |
| 765 | } |
| 766 | static inline void ggml_sycl_op_silu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 767 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 768 | return op_silu(x); |
| 769 | }); |
| 770 | } |
| 771 | |
| 772 | static inline void ggml_sycl_op_gelu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 773 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 774 | return op_gelu(x); |
| 775 | }); |
| 776 | } |
| 777 | |
| 778 | static inline void ggml_sycl_op_gelu_quick(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 779 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 780 | return op_gelu_quick(x); |
| 781 | }); |
| 782 | } |
| 783 | |
| 784 | static inline void ggml_sycl_op_gelu_erf(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 785 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 786 | return op_gelu_erf(x); |
| 787 | }); |
| 788 | } |
| 789 | |
| 790 | static inline void ggml_sycl_op_tanh(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 791 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 792 | return op_tanh(x); |
| 793 | }); |
| 794 | } |
| 795 | |
| 796 | static inline void ggml_sycl_op_relu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 797 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 798 | return op_relu(x); |
| 799 | }); |
| 800 | } |
| 801 | |
| 802 | static inline void ggml_sycl_op_hardsigmoid(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 803 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 804 | return op_hardsigmoid(x); |
| 805 | }); |
| 806 | } |
| 807 | |
| 808 | static inline void ggml_sycl_op_hardswish(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 809 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 810 | return op_hardswish(x); |
| 811 | }); |
| 812 | } |
| 813 | |
| 814 | static inline void ggml_sycl_op_exp(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 815 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 816 | return op_exp(x); |
| 817 | }); |
| 818 | } |
| 819 | |
| 820 | static inline void ggml_sycl_op_expm1(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 821 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 822 | return op_expm1(x); |
| 823 | }); |
| 824 | } |
| 825 | |
| 826 | static inline void ggml_sycl_op_log(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 827 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 828 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream) { |
| 829 | const int num_blocks = ceil_div(k_elements, SYCL_EXP_BLOCK_SIZE); // Using EXP block size |
| 830 | stream->parallel_for( |
| 831 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_EXP_BLOCK_SIZE), |
| 832 | sycl::range<1>(SYCL_EXP_BLOCK_SIZE)), |
| 833 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 834 | unary_op_log_kernel(src, dst_ptr, k_elements, item_ct1); |
| 835 | }); |
| 836 | }); |
| 837 | } |
| 838 | |
| 839 | static inline void ggml_sycl_op_softplus(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 840 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 841 | return op_softplus(x); |
| 842 | }); |
| 843 | } |
| 844 | |
| 845 | static inline void ggml_sycl_op_neg(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 846 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 847 | return op_neg(x); |
| 848 | }); |
| 849 | } |
| 850 | |
| 851 | |
| 852 | static inline void ggml_sycl_op_step(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 853 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 854 | return op_step(x); |
| 855 | }); |
| 856 | } |
| 857 | |
| 858 | static inline void ggml_sycl_op_sigmoid(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 859 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 860 | return op_sigmoid(x); |
| 861 | }); |
| 862 | } |
| 863 | |
| 864 | static inline void ggml_sycl_op_sqrt(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 865 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 866 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream) { |
| 867 | const int num_blocks = ceil_div(k_elements, SYCL_SQRT_BLOCK_SIZE); |
| 868 | stream->parallel_for( |
| 869 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_SQRT_BLOCK_SIZE), |
| 870 | sycl::range<1>(SYCL_SQRT_BLOCK_SIZE)), |
| 871 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 872 | unary_op_sqrt_kernel(src, dst_ptr, k_elements, item_ct1); |
| 873 | }); |
| 874 | }); |
| 875 | } |
| 876 | |
| 877 | static inline void ggml_sycl_op_sin(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 878 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 879 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream) { |
| 880 | const int num_blocks = ceil_div(k_elements, SYCL_SIN_BLOCK_SIZE); |
| 881 | stream->parallel_for( |
| 882 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_SIN_BLOCK_SIZE), |
| 883 | sycl::range<1>(SYCL_SIN_BLOCK_SIZE)), |
| 884 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 885 | unary_op_sin_kernel(src, dst_ptr, k_elements, item_ct1); |
| 886 | }); |
| 887 | }); |
| 888 | } |
| 889 | |
| 890 | static inline void ggml_sycl_op_cos(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 891 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 892 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream) { |
| 893 | const int num_blocks = ceil_div(k_elements, SYCL_SIN_BLOCK_SIZE); // Using SIN block size |
| 894 | stream->parallel_for( |
| 895 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_SIN_BLOCK_SIZE), |
| 896 | sycl::range<1>(SYCL_SIN_BLOCK_SIZE)), |
| 897 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 898 | unary_op_cos_kernel(src, dst_ptr, k_elements, item_ct1); |
| 899 | }); |
| 900 | }); |
| 901 | } |
| 902 | |
| 903 | static inline void ggml_sycl_op_leaky_relu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 904 | float negative_slope; |
| 905 | memcpy(&negative_slope, dst->op_params, sizeof(float)); |
| 906 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 907 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream, float slope) { |
| 908 | const int num_blocks = ceil_div(k_elements, SYCL_RELU_BLOCK_SIZE); |
| 909 | stream->parallel_for( |
| 910 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_RELU_BLOCK_SIZE), |
| 911 | sycl::range<1>(SYCL_RELU_BLOCK_SIZE)), |
| 912 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 913 | unary_op_leaky_relu_kernel(src, dst_ptr, k_elements, slope, item_ct1); |
| 914 | }); |
| 915 | }, negative_slope); |
| 916 | } |
| 917 | |
| 918 | static inline void ggml_sycl_op_sqr(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 919 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 920 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream) { |
| 921 | const int num_blocks = ceil_div(k_elements, SYCL_SQR_BLOCK_SIZE); |
| 922 | stream->parallel_for( |
| 923 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_SQR_BLOCK_SIZE), |
| 924 | sycl::range<1>(SYCL_SQR_BLOCK_SIZE)), |
| 925 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 926 | unary_op_sqr_kernel(src, dst_ptr, k_elements, item_ct1); |
| 927 | }); |
| 928 | }); |
| 929 | } |
| 930 | |
| 931 | static inline void ggml_sycl_op_clamp(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 932 | float min_val; |
| 933 | float max_val; |
| 934 | memcpy(&min_val, dst->op_params, sizeof(float)); |
| 935 | memcpy(&max_val, (float *) dst->op_params + 1, sizeof(float)); |
| 936 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 937 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream, float min_arg, float max_arg) { |
| 938 | const int num_blocks = ceil_div(k_elements, SYCL_CLAMP_BLOCK_SIZE); |
| 939 | stream->parallel_for( |
| 940 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_CLAMP_BLOCK_SIZE), |
| 941 | sycl::range<1>(SYCL_CLAMP_BLOCK_SIZE)), |
| 942 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 943 | clamp(src, dst_ptr, min_arg, max_arg, k_elements, item_ct1); |
| 944 | }); |
| 945 | }, min_val, max_val); |
| 946 | } |
| 947 | |
| 948 | static inline void ggml_sycl_op_xielu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 949 | const float alpha_n = ggml_get_op_params_f32(dst, 1); |
| 950 | const float alpha_p = ggml_get_op_params_f32(dst, 2); |
| 951 | const float beta = ggml_get_op_params_f32(dst, 3); |
| 952 | const float eps = ggml_get_op_params_f32(dst, 4); |
| 953 | ggml_sycl_detail::dispatch_ggml_sycl_op_unary(ctx, dst, |
| 954 | [](const auto* src, auto* dst_ptr, int k_elements, queue_ptr stream, float alpha_n_arg, float alpha_p_arg, float beta_arg, float eps_arg) { |
| 955 | const int num_blocks = ceil_div(k_elements, SYCL_RELU_BLOCK_SIZE); |
| 956 | stream->parallel_for( |
| 957 | sycl::nd_range<1>(sycl::range<1>(num_blocks) * sycl::range<1>(SYCL_RELU_BLOCK_SIZE), |
| 958 | sycl::range<1>(SYCL_RELU_BLOCK_SIZE)), |
| 959 | [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 960 | unary_op_xielu_kernel(src, dst_ptr, k_elements, alpha_n_arg, alpha_p_arg, beta_arg, eps_arg, item_ct1); |
| 961 | }); |
| 962 | }, alpha_n, alpha_p, beta, eps); |
| 963 | } |
| 964 | |
| 965 | static inline void ggml_sycl_op_floor(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 966 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 967 | return op_floor(x); |
| 968 | }); |
| 969 | } |
| 970 | |
| 971 | static inline void ggml_sycl_op_ceil(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 972 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 973 | return op_ceil(x); |
| 974 | }); |
| 975 | } |
| 976 | |
| 977 | static inline void ggml_sycl_op_round(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 978 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 979 | return op_round(x); |
| 980 | }); |
| 981 | } |
| 982 | |
| 983 | static inline void ggml_sycl_op_trunc(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 984 | ggml_sycl_detail::ggml_sycl_op_unary(ctx, dst, [](auto x) { |
| 985 | return op_trunc(x); |
| 986 | }); |
| 987 | } |
| 988 | |
| 989 | static inline void ggml_sycl_op_acc(ggml_backend_sycl_context & ctx, ggml_tensor *dst) { |
| 990 | const ggml_tensor * src0 = dst->src[0]; |
| 991 | const ggml_tensor * src1 = dst->src[1]; |
| 992 | |
| 993 | const char * src0_d = (const char *) src0->data; |
| 994 | const char * src1_d = (const char *) src1->data; |
| 995 | float * dst_d = (float *) dst->data; |
| 996 | |
| 997 | dpct::queue_ptr stream = ctx.stream(); |
| 998 | |
| 999 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 1000 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 1001 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 1002 | |
| 1003 | GGML_ASSERT(dst->nb[0] == ggml_element_size(dst)); |
| 1004 | GGML_ASSERT(ggml_is_contiguously_allocated(dst)); |
| 1005 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 1006 | |
| 1007 | const int64_t s1 = (int64_t) ((const int32_t *) dst->op_params)[0] / (int64_t) sizeof(float); |
| 1008 | const int64_t s2 = (int64_t) ((const int32_t *) dst->op_params)[1] / (int64_t) sizeof(float); |
| 1009 | const int64_t s3 = (int64_t) ((const int32_t *) dst->op_params)[2] / (int64_t) sizeof(float); |
| 1010 | const int64_t offset = (int64_t) ((const int32_t *) dst->op_params)[3] / (int64_t) sizeof(float); |
| 1011 | |
| 1012 | ggml_sycl_detail::acc_f32_sycl(src0_d, src1_d, dst_d, ggml_nelements(dst), |
| 1013 | dst->ne[0], dst->ne[1], dst->ne[2], |
| 1014 | src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], |
| 1015 | src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3], |
| 1016 | src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3], |
| 1017 | s1, s2, s3, offset, stream); |
| 1018 | } |
| 1019 | |
| 1020 | static inline void ggml_sycl_op_geglu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1021 | ggml_sycl_detail::ggml_sycl_op_unary_gated(ctx, dst, [](auto x) { |
| 1022 | return op_gelu(x); |
| 1023 | }); |
| 1024 | } |
| 1025 | |
| 1026 | static inline void ggml_sycl_op_reglu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1027 | ggml_sycl_detail::ggml_sycl_op_unary_gated(ctx, dst, [](auto x) { |
| 1028 | return op_relu(x); |
| 1029 | }); |
| 1030 | } |
| 1031 | |
| 1032 | static inline void ggml_sycl_op_swiglu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1033 | ggml_sycl_detail::ggml_sycl_op_unary_gated(ctx, dst, [](auto x) { |
| 1034 | return op_silu(x); |
| 1035 | }); |
| 1036 | } |
| 1037 | |
| 1038 | // Hands `launch` the functor for the unary op of a fused unary chain. Anything else |
| 1039 | // ggml_sycl_can_fuse() has already declined, so the default is a dispatcher bug. |
| 1040 | template<typename F> |
| 1041 | static void dispatch_fused_unary_op(ggml_unary_op uop, F && launch) { |
| 1042 | switch (uop) { |
| 1043 | case GGML_UNARY_OP_SILU: launch([](float v) { return op_silu(v); }); break; |
| 1044 | case GGML_UNARY_OP_SIGMOID: launch([](float v) { return op_sigmoid(v); }); break; |
| 1045 | case GGML_UNARY_OP_SOFTPLUS: launch([](float v) { return op_softplus(v); }); break; |
| 1046 | default: |
| 1047 | GGML_ABORT("fused unary chain: unsupported unary op %s", ggml_unary_op_name(uop)); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // dst = op(unary_node->src[0]) * other, written straight to the MUL output, saving the |
| 1052 | // standalone unary launch. Preconditions come from ggml_sycl_can_fuse(); re-asserted here. |
| 1053 | void ggml_sycl_op_unary_mul_fused(ggml_backend_sycl_context & ctx, ggml_tensor * unary_node, ggml_tensor * mul_node) { |
| 1054 | scope_op_debug_print scope_dbg_print(__func__, mul_node, /*num_src=*/2); |
| 1055 | |
| 1056 | const ggml_tensor * x = unary_node->src[0]; |
| 1057 | const ggml_tensor * g = (mul_node->src[0] == unary_node) ? mul_node->src[1] : mul_node->src[0]; |
| 1058 | |
| 1059 | // g is picked by elimination; ggml_can_fuse()'s single-use rule rules out MUL(unary, unary) |
| 1060 | GGML_ASSERT(g != unary_node); |
| 1061 | GGML_ASSERT(x->type == g->type && x->type == mul_node->type); |
| 1062 | GGML_ASSERT(ggml_are_same_shape(x, g) && ggml_are_same_shape(x, mul_node)); |
| 1063 | GGML_ASSERT(ggml_is_contiguous_1(x) && ggml_is_contiguous_1(g)); |
| 1064 | // dst is indexed flat |
| 1065 | GGML_ASSERT(ggml_is_contiguous(mul_node)); |
| 1066 | |
| 1067 | queue_ptr main_stream = ctx.stream(); |
| 1068 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 1069 | |
| 1070 | const int64_t k = ggml_nelements(mul_node); |
| 1071 | const int64_t n = mul_node->ne[0]; |
| 1072 | |
| 1073 | const auto dispatch_type = [&](auto op) { |
| 1074 | switch (mul_node->type) { |
| 1075 | case GGML_TYPE_F32: |
| 1076 | unary_mul_sycl((const float *) x->data, (const float *) g->data, (float *) mul_node->data, |
| 1077 | k, n, x->nb[1] / sizeof(float), g->nb[1] / sizeof(float), main_stream, op); |
| 1078 | break; |
| 1079 | case GGML_TYPE_F16: |
| 1080 | unary_mul_sycl((const sycl::half *) x->data, (const sycl::half *) g->data, (sycl::half *) mul_node->data, |
| 1081 | k, n, x->nb[1] / sizeof(sycl::half), g->nb[1] / sizeof(sycl::half), main_stream, op); |
| 1082 | break; |
| 1083 | default: |
| 1084 | GGML_ABORT("fused unary+mul: unsupported type %s", ggml_type_name(mul_node->type)); |
| 1085 | } |
| 1086 | }; |
| 1087 | |
| 1088 | dispatch_fused_unary_op(ggml_get_unary_op(unary_node), dispatch_type); |
| 1089 | } |
| 1090 | |
| 1091 | // dst = op(a + bias) * scale for an ADD + UNARY + MUL chain whose bias and scale broadcast |
| 1092 | // over dim 0. Preconditions come from ggml_sycl_can_fuse(); re-asserted here. |
| 1093 | void ggml_sycl_op_add_unary_mul_fused(ggml_backend_sycl_context & ctx, ggml_tensor * add_node, |
| 1094 | ggml_tensor * unary_node, ggml_tensor * mul_node) { |
| 1095 | // the dst-arity convention the other fusions follow; a and bias live on add_node |
| 1096 | scope_op_debug_print scope_dbg_print(__func__, mul_node, /*num_src=*/2); |
| 1097 | |
| 1098 | const ggml_tensor * a = add_node->src[0]; |
| 1099 | const ggml_tensor * bias = add_node->src[1]; |
| 1100 | const ggml_tensor * scale = (mul_node->src[0] == unary_node) ? mul_node->src[1] : mul_node->src[0]; |
| 1101 | |
| 1102 | // scale is picked by elimination; ggml_can_fuse()'s single-use rule rules out MUL(unary, unary) |
| 1103 | GGML_ASSERT(scale != unary_node); |
| 1104 | GGML_ASSERT(a->type == GGML_TYPE_F32 && bias->type == GGML_TYPE_F32); |
| 1105 | GGML_ASSERT(scale->type == GGML_TYPE_F32 && mul_node->type == GGML_TYPE_F32); |
| 1106 | GGML_ASSERT(ggml_are_same_shape(a, mul_node)); |
| 1107 | // a and dst are indexed flat |
| 1108 | GGML_ASSERT(ggml_is_contiguous(a) && ggml_is_contiguous(mul_node)); |
| 1109 | // bias and scale are one contiguous ne0-length row each, broadcast over the outer dims |
| 1110 | GGML_ASSERT(bias->ne[0] == a->ne[0] && scale->ne[0] == a->ne[0]); |
| 1111 | GGML_ASSERT(ggml_nrows(bias) == 1 && ggml_nrows(scale) == 1); |
| 1112 | GGML_ASSERT(ggml_is_contiguous(bias) && ggml_is_contiguous(scale)); |
| 1113 | |
| 1114 | queue_ptr main_stream = ctx.stream(); |
| 1115 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 1116 | |
| 1117 | const auto dispatch_op = [&](auto op) { |
| 1118 | add_unary_mul_sycl((const float *) a->data, (const float *) bias->data, (const float *) scale->data, |
| 1119 | (float *) mul_node->data, ggml_nelements(mul_node), mul_node->ne[0], main_stream, op); |
| 1120 | }; |
| 1121 | |
| 1122 | dispatch_fused_unary_op(ggml_get_unary_op(unary_node), dispatch_op); |
| 1123 | } |
| 1124 | |
| 1125 | __dpct_inline__ float ggml_sycl_op_swiglu_oai_single(float x, float g, float alpha = 1.702f, float limit = 7.0f) { |
| 1126 | x = sycl::fmin(x, limit); |
| 1127 | g = sycl::fmax(sycl::fmin(g, limit), -limit); |
| 1128 | |
| 1129 | float out_glu = x / (1.0f + sycl::native::exp(-x * alpha)); |
| 1130 | out_glu = out_glu * (1.0f + g); |
| 1131 | return out_glu; |
| 1132 | } |
| 1133 | |
| 1134 | template <typename T> |
| 1135 | static void swiglu_oai_kernel(const T * x, const T * g, T * dst, const int64_t k, |
| 1136 | const int64_t n, const int64_t o0, const int64_t o1, |
| 1137 | float alpha, float limit, sycl::nd_item<3> item_ct1) { |
| 1138 | const int64_t i = int64_t(item_ct1.get_local_range(2)) * item_ct1.get_group(2) + item_ct1.get_local_id(2); |
| 1139 | |
| 1140 | if (i >= k) { |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | const int64_t j0 = (i / n) * o0 + (i % n); |
| 1145 | const int64_t j1 = o0 == o1 ? j0 : (i / n) * o1 + (i % n); |
| 1146 | |
| 1147 | float xi = x[j0]; |
| 1148 | float gi = g[j1]; |
| 1149 | |
| 1150 | dst[i] = ggml_sycl_op_swiglu_oai_single(xi, gi, alpha, limit); |
| 1151 | } |
| 1152 | |
| 1153 | template <typename T> |
| 1154 | static void swiglu_oai_sycl(const T * x, |
| 1155 | const T * g, |
| 1156 | T * dst, |
| 1157 | const int64_t k, |
| 1158 | const int64_t n, |
| 1159 | const int64_t o0, |
| 1160 | const int64_t o1, |
| 1161 | const float alpha, |
| 1162 | const float limit, |
| 1163 | dpct::queue_ptr stream) { |
| 1164 | const int64_t num_blocks = (k + SYCL_GLU_BLOCK_SIZE - 1) / SYCL_GLU_BLOCK_SIZE; |
| 1165 | stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_GLU_BLOCK_SIZE), |
| 1166 | sycl::range<3>(1, 1, SYCL_GLU_BLOCK_SIZE)), |
| 1167 | [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 1168 | swiglu_oai_kernel(x, g, dst, k, n, o0, o1, alpha, limit, item_ct1); |
| 1169 | }); |
| 1170 | } |
| 1171 | |
| 1172 | static void ggml_sycl_op_swiglu_oai(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1173 | const ggml_tensor * src0 = dst->src[0]; |
| 1174 | const ggml_tensor * src1 = dst->src[1]; |
| 1175 | void * src0_d = src0->data; |
| 1176 | void * src1_d = src1 ? src1->data : src0->data; |
| 1177 | const int64_t src0_o = src0->nb[1]; |
| 1178 | const int64_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 1179 | void * dst_d = dst->data; |
| 1180 | const int64_t nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 1181 | dpct::queue_ptr stream = ctx.stream(); |
| 1182 | |
| 1183 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 1184 | GGML_ASSERT(src0->nb[0] == ggml_element_size(src0)); |
| 1185 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 1186 | |
| 1187 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 1188 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 1189 | GGML_ASSERT(src0->type == dst->type); |
| 1190 | GGML_ASSERT(dst->ne[0] == nc); |
| 1191 | GGML_ASSERT(ggml_nrows(dst) == ggml_nrows(src0)); |
| 1192 | |
| 1193 | if (src1) { |
| 1194 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 1195 | GGML_ASSERT(src1->nb[0] == ggml_element_size(src1)); |
| 1196 | GGML_ASSERT(src1->ne[0] == nc); |
| 1197 | GGML_ASSERT(src0->type == src1->type); |
| 1198 | } |
| 1199 | |
| 1200 | //const int32_t swapped = ((const int32_t *) dst->op_params)[1]; |
| 1201 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 1202 | const float alpha = ggml_get_op_params_f32(dst, 2); |
| 1203 | const float limit = ggml_get_op_params_f32(dst, 3); |
| 1204 | |
| 1205 | float * src0_p = (float *) src0_d; |
| 1206 | float * src1_p = (float *) src1_d; |
| 1207 | |
| 1208 | if (!src1) { |
| 1209 | src0_p += swapped ? nc : 0; |
| 1210 | src1_p += swapped ? 0 : nc; |
| 1211 | } |
| 1212 | |
| 1213 | swiglu_oai_sycl(src0_p, src1_p, (float *)dst_d, ggml_nelements(dst), nc, src0_o / sizeof(float), src1_o / sizeof(float), alpha, limit, stream); |
| 1214 | } |
| 1215 | |
| 1216 | template <typename T> |
| 1217 | static void swiglu_clamp_kernel(const T * gate, |
| 1218 | const T * up, |
| 1219 | T * dst, |
| 1220 | const int64_t k, |
| 1221 | const int64_t n, |
| 1222 | const int64_t o0, |
| 1223 | const int64_t o1, |
| 1224 | float limit, |
| 1225 | sycl::nd_item<3> item_ct1) { |
| 1226 | const int64_t i = int64_t(item_ct1.get_local_range(2)) * item_ct1.get_group(2) + item_ct1.get_local_id(2); |
| 1227 | |
| 1228 | if (i >= k) { |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | const int64_t j0 = (i / n) * o0 + (i % n); |
| 1233 | const int64_t j1 = o0 == o1 ? j0 : (i / n) * o1 + (i % n); |
| 1234 | |
| 1235 | const float gate_value = sycl::fmin((float) gate[j0], limit); |
| 1236 | const float up_value = sycl::fmax(sycl::fmin((float) up[j1], limit), -limit); |
| 1237 | dst[i] = (T) (gate_value / (1.0f + sycl::native::exp(-gate_value)) * up_value); |
| 1238 | } |
| 1239 | |
| 1240 | template <typename T> |
| 1241 | static void swiglu_clamp_sycl(const T * gate, |
| 1242 | const T * up, |
| 1243 | T * dst, |
| 1244 | const int64_t k, |
| 1245 | const int64_t n, |
| 1246 | const int64_t o0, |
| 1247 | const int64_t o1, |
| 1248 | float limit, |
| 1249 | dpct::queue_ptr stream) { |
| 1250 | const int64_t num_blocks = (k + SYCL_GLU_BLOCK_SIZE - 1) / SYCL_GLU_BLOCK_SIZE; |
| 1251 | stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_GLU_BLOCK_SIZE), |
| 1252 | sycl::range<3>(1, 1, SYCL_GLU_BLOCK_SIZE)), |
| 1253 | [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 1254 | swiglu_clamp_kernel(gate, up, dst, k, n, o0, o1, limit, item_ct1); |
| 1255 | }); |
| 1256 | } |
| 1257 | |
| 1258 | static void ggml_sycl_op_swiglu_clamp(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1259 | const ggml_tensor * src0 = dst->src[0]; |
| 1260 | const ggml_tensor * src1 = dst->src[1]; |
| 1261 | void * src0_d = src0->data; |
| 1262 | void * src1_d = src1 ? src1->data : src0->data; |
| 1263 | const int64_t src0_o = src0->nb[1]; |
| 1264 | const int64_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 1265 | void * dst_d = dst->data; |
| 1266 | const int64_t nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 1267 | dpct::queue_ptr stream = ctx.stream(); |
| 1268 | |
| 1269 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 1270 | GGML_ASSERT(src0->nb[0] == ggml_element_size(src0)); |
| 1271 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 1272 | GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); |
| 1273 | GGML_ASSERT(src0->type == dst->type); |
| 1274 | GGML_ASSERT(dst->ne[0] == nc); |
| 1275 | GGML_ASSERT(ggml_nrows(dst) == ggml_nrows(src0)); |
| 1276 | |
| 1277 | if (src1) { |
| 1278 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 1279 | GGML_ASSERT(src1->nb[0] == ggml_element_size(src1)); |
| 1280 | GGML_ASSERT(src1->ne[0] == nc); |
| 1281 | GGML_ASSERT(src0->type == src1->type); |
| 1282 | } |
| 1283 | |
| 1284 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 1285 | const float limit = ggml_get_op_params_f32(dst, 3); |
| 1286 | |
| 1287 | if (src0->type == GGML_TYPE_F16) { |
| 1288 | sycl::half * src0_p = (sycl::half *) src0_d; |
| 1289 | sycl::half * src1_p = (sycl::half *) src1_d; |
| 1290 | |
| 1291 | if (!src1) { |
| 1292 | src0_p += swapped ? nc : 0; |
| 1293 | src1_p += swapped ? 0 : nc; |
| 1294 | } |
| 1295 | |
| 1296 | swiglu_clamp_sycl(src0_p, src1_p, (sycl::half *) dst_d, ggml_nelements(dst), nc, src0_o / sizeof(sycl::half), |
| 1297 | src1_o / sizeof(sycl::half), limit, stream); |
| 1298 | } else { |
| 1299 | float * src0_p = (float *) src0_d; |
| 1300 | float * src1_p = (float *) src1_d; |
| 1301 | |
| 1302 | if (!src1) { |
| 1303 | src0_p += swapped ? nc : 0; |
| 1304 | src1_p += swapped ? 0 : nc; |
| 1305 | } |
| 1306 | |
| 1307 | swiglu_clamp_sycl(src0_p, src1_p, (float *) dst_d, ggml_nelements(dst), nc, src0_o / sizeof(float), |
| 1308 | src1_o / sizeof(float), limit, stream); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | static inline void ggml_sycl_op_geglu_erf(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1313 | ggml_sycl_detail::ggml_sycl_op_unary_gated(ctx, dst, [](auto x) { |
| 1314 | return op_gelu_erf(x); |
| 1315 | }); |
| 1316 | } |
| 1317 | |
| 1318 | static inline void ggml_sycl_op_geglu_quick(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1319 | ggml_sycl_detail::ggml_sycl_op_unary_gated(ctx, dst, [](auto x) { |
| 1320 | return op_gelu_quick(x); |
| 1321 | }); |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | void ggml_sycl_sqrt(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1326 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1327 | ggml_sycl_op_sqrt(ctx, dst); |
| 1328 | } |
| 1329 | |
| 1330 | void ggml_sycl_sin(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1331 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1332 | ggml_sycl_op_sin(ctx, dst); |
| 1333 | } |
| 1334 | |
| 1335 | void ggml_sycl_cos(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1336 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1337 | ggml_sycl_op_cos(ctx, dst); |
| 1338 | } |
| 1339 | |
| 1340 | void ggml_sycl_acc(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1341 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); |
| 1342 | ggml_sycl_op_acc(ctx, dst); |
| 1343 | } |
| 1344 | |
| 1345 | void ggml_sycl_gelu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1346 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1347 | ggml_sycl_op_gelu(ctx, dst); |
| 1348 | } |
| 1349 | |
| 1350 | void ggml_sycl_silu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1351 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1352 | ggml_sycl_op_silu(ctx, dst); |
| 1353 | } |
| 1354 | |
| 1355 | void ggml_sycl_gelu_quick(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1356 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1357 | ggml_sycl_op_gelu_quick(ctx, dst); |
| 1358 | } |
| 1359 | |
| 1360 | void ggml_sycl_gelu_erf(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1361 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1362 | ggml_sycl_op_gelu_erf(ctx, dst); |
| 1363 | } |
| 1364 | |
| 1365 | void ggml_sycl_tanh(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1366 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1367 | ggml_sycl_op_tanh(ctx, dst); |
| 1368 | } |
| 1369 | |
| 1370 | void ggml_sycl_relu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1371 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1372 | ggml_sycl_op_relu(ctx, dst); |
| 1373 | } |
| 1374 | |
| 1375 | void ggml_sycl_sigmoid(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1376 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1377 | ggml_sycl_op_sigmoid(ctx, dst); |
| 1378 | } |
| 1379 | |
| 1380 | void ggml_sycl_hardsigmoid(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1381 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1382 | ggml_sycl_op_hardsigmoid(ctx, dst); |
| 1383 | } |
| 1384 | |
| 1385 | void ggml_sycl_hardswish(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1386 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1387 | ggml_sycl_op_hardswish(ctx, dst); |
| 1388 | } |
| 1389 | |
| 1390 | void ggml_sycl_exp(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1391 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1392 | ggml_sycl_op_exp(ctx, dst); |
| 1393 | } |
| 1394 | |
| 1395 | void ggml_sycl_expm1(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1396 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1397 | ggml_sycl_op_expm1(ctx, dst); |
| 1398 | } |
| 1399 | |
| 1400 | void ggml_sycl_log(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1401 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1402 | ggml_sycl_op_log(ctx, dst); |
| 1403 | } |
| 1404 | |
| 1405 | void ggml_sycl_softplus(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1406 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1407 | ggml_sycl_op_softplus(ctx, dst); |
| 1408 | } |
| 1409 | |
| 1410 | void ggml_sycl_neg(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1411 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1412 | ggml_sycl_op_neg(ctx, dst); |
| 1413 | } |
| 1414 | |
| 1415 | void ggml_sycl_step(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1416 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1417 | ggml_sycl_op_step(ctx, dst); |
| 1418 | } |
| 1419 | |
| 1420 | void ggml_sycl_leaky_relu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1421 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1422 | ggml_sycl_op_leaky_relu(ctx, dst); |
| 1423 | } |
| 1424 | |
| 1425 | void ggml_sycl_sqr(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1426 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1427 | ggml_sycl_op_sqr(ctx, dst); |
| 1428 | } |
| 1429 | |
| 1430 | void ggml_sycl_clamp(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1431 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1432 | ggml_sycl_op_clamp(ctx, dst); |
| 1433 | } |
| 1434 | |
| 1435 | void ggml_sycl_xielu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1436 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1437 | ggml_sycl_op_xielu(ctx, dst); |
| 1438 | } |
| 1439 | |
| 1440 | void ggml_sycl_sgn(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1441 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1442 | ggml_sycl_op_sgn(ctx, dst); |
| 1443 | } |
| 1444 | |
| 1445 | void ggml_sycl_abs(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1446 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1447 | ggml_sycl_op_abs(ctx, dst); |
| 1448 | } |
| 1449 | |
| 1450 | void ggml_sycl_elu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1451 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1452 | ggml_sycl_op_elu(ctx, dst); |
| 1453 | } |
| 1454 | |
| 1455 | void ggml_sycl_geglu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1456 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1457 | ggml_sycl_op_geglu(ctx, dst); |
| 1458 | } |
| 1459 | |
| 1460 | void ggml_sycl_reglu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1461 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1462 | ggml_sycl_op_reglu(ctx, dst); |
| 1463 | } |
| 1464 | |
| 1465 | void ggml_sycl_swiglu(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1466 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1467 | ggml_sycl_op_swiglu(ctx, dst); |
| 1468 | } |
| 1469 | |
| 1470 | void ggml_sycl_swiglu_oai(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1471 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1472 | ggml_sycl_op_swiglu_oai(ctx, dst); |
| 1473 | } |
| 1474 | |
| 1475 | void ggml_sycl_swiglu_clamp(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1476 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1477 | ggml_sycl_op_swiglu_clamp(ctx, dst); |
| 1478 | } |
| 1479 | |
| 1480 | void ggml_sycl_geglu_erf(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1481 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1482 | ggml_sycl_op_geglu_erf(ctx, dst); |
| 1483 | } |
| 1484 | |
| 1485 | void ggml_sycl_geglu_quick(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1486 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1487 | ggml_sycl_op_geglu_quick(ctx, dst); |
| 1488 | } |
| 1489 | |
| 1490 | void ggml_sycl_arange(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1491 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/0); |
| 1492 | ggml_sycl_detail::ggml_sycl_op_arange(ctx, dst); |
| 1493 | } |
| 1494 | |
| 1495 | void ggml_sycl_floor(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1496 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1497 | ggml_sycl_op_floor(ctx, dst); |
| 1498 | } |
| 1499 | |
| 1500 | void ggml_sycl_ceil(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1501 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1502 | ggml_sycl_op_ceil(ctx, dst); |
| 1503 | } |
| 1504 | |
| 1505 | void ggml_sycl_round(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1506 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1507 | ggml_sycl_op_round(ctx, dst); |
| 1508 | } |
| 1509 | |
| 1510 | void ggml_sycl_trunc(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 1511 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 1512 | ggml_sycl_op_trunc(ctx, dst); |
| 1513 | } |