ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / roll.cpp
| 1 | #include "roll.hpp" |
| 2 | #include "common.hpp" |
| 3 | |
| 4 | using namespace sycl; |
| 5 | |
| 6 | static inline int wrap_add(int i, int shift, int n) { |
| 7 | |
| 8 | int s = i + shift; |
| 9 | return (s >= n) ? (s - n) : s; |
| 10 | } |
| 11 | |
| 12 | static void kernel_roll_fused_i0_i1( |
| 13 | queue &q, |
| 14 | const float *src_d, |
| 15 | float *dst_d, |
| 16 | int ne0, int ne1, int ne2, int ne3, |
| 17 | int sh0, int sh1, int sh2, int sh3) |
| 18 | { |
| 19 | if (ne0 == 0 || ne1 == 0 || ne2 == 0 || ne3 == 0) return; |
| 20 | |
| 21 | |
| 22 | const int stride1 = ne0; |
| 23 | const int stride2 = ne0 * ne1; |
| 24 | const int stride3 = ne0 * ne1 * ne2; |
| 25 | |
| 26 | |
| 27 | const int shNe0 = (ne0 - sh0) % ne0; |
| 28 | const int shNe1 = (ne1 - sh1) % ne1; |
| 29 | const int shNe2 = (ne2 - sh2) % ne2; |
| 30 | const int shNe3 = (ne3 - sh3) % ne3; |
| 31 | |
| 32 | |
| 33 | const size_t g0 = (size_t) ne3; |
| 34 | const size_t g1 = (size_t) ne2; |
| 35 | const size_t g2 = (size_t) (ne1 * ne0); |
| 36 | |
| 37 | const range<3> global{ g0, g1, g2 }; |
| 38 | |
| 39 | q.submit([&](handler &h) { |
| 40 | h.parallel_for(global, [=](id<3> idx) { |
| 41 | const int i3 = (int) idx[0]; |
| 42 | const int i2 = (int) idx[1]; |
| 43 | |
| 44 | const int fused = (int) idx[2]; |
| 45 | const int i1 = fused / ne0; |
| 46 | const int i0 = fused - i1 * ne0; // fused % ne0 |
| 47 | |
| 48 | |
| 49 | const int idx_dst = i0 |
| 50 | + i1 * stride1 |
| 51 | + i2 * stride2 |
| 52 | + i3 * stride3; |
| 53 | |
| 54 | |
| 55 | const int s0 = wrap_add(i0, shNe0, ne0); |
| 56 | const int s1 = wrap_add(i1, shNe1, ne1); |
| 57 | const int s2 = wrap_add(i2, shNe2, ne2); |
| 58 | const int s3 = wrap_add(i3, shNe3, ne3); |
| 59 | |
| 60 | const int idx_src = s0 |
| 61 | + s1 * stride1 |
| 62 | + s2 * stride2 |
| 63 | + s3 * stride3; |
| 64 | |
| 65 | dst_d[idx_dst] = src_d[idx_src]; |
| 66 | }); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | void ggml_sycl_roll(ggml_backend_sycl_context & ctx, ggml_tensor *dst) { |
| 71 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 72 | |
| 73 | const ggml_tensor *src = dst->src[0]; |
| 74 | GGML_ASSERT(src && src->type == GGML_TYPE_F32); |
| 75 | |
| 76 | // the kernel indexes in 32-bit |
| 77 | GGML_ASSERT(dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3] <= INT32_MAX); |
| 78 | const int ne0 = (int) dst->ne[0]; |
| 79 | const int ne1 = (int) dst->ne[1]; |
| 80 | const int ne2 = (int) dst->ne[2]; |
| 81 | const int ne3 = (int) dst->ne[3]; |
| 82 | |
| 83 | const int32_t *params = (const int32_t *) dst->op_params; |
| 84 | int shift0 = params[0]; |
| 85 | int shift1 = params[1]; |
| 86 | int shift2 = params[2]; |
| 87 | int shift3 = params[3]; |
| 88 | |
| 89 | |
| 90 | if ((shift0 | shift1 | shift2 | shift3) == 0) { |
| 91 | const size_t nb = ggml_nbytes(src); |
| 92 | queue *q = ctx.stream(); |
| 93 | SYCL_CHECK(CHECK_TRY_ERROR(q->memcpy(dst->data, src->data, nb))); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | auto norm = [](int sh, int n) -> int { |
| 98 | if (n <= 0) return 0; |
| 99 | sh %= n; |
| 100 | if (sh < 0) sh += n; |
| 101 | return sh; |
| 102 | }; |
| 103 | shift0 = norm(shift0, ne0); |
| 104 | shift1 = norm(shift1, ne1); |
| 105 | shift2 = norm(shift2, ne2); |
| 106 | shift3 = norm(shift3, ne3); |
| 107 | |
| 108 | try { |
| 109 | queue *q = ctx.stream(); |
| 110 | |
| 111 | const float *src_d = (const float *) src->data; |
| 112 | float *dst_d = (float *) dst->data; |
| 113 | GGML_ASSERT(src_d && dst_d); |
| 114 | |
| 115 | kernel_roll_fused_i0_i1( |
| 116 | *q, src_d, dst_d, |
| 117 | ne0, ne1, ne2, ne3, |
| 118 | shift0, shift1, shift2, shift3 |
| 119 | ); |
| 120 | } catch (const std::exception &e) { |
| 121 | std::fprintf(stderr, "[SYCL-ROLL] ERROR: %s\n", e.what()); |
| 122 | throw; |
| 123 | } |
| 124 | } |