interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-sycl / add-id.cpp

90 lines · 2580 bytes Raw Blame History
1#include <sycl/sycl.hpp>
2#include "common.hpp"
3#include "add-id.hpp"
4
5static void add_id_kernel(
6 const float* src0,
7 const float* src1,
8 const int32_t* src2,
9 float* dst,
10 int64_t ne0,
11 int64_t ne1,
12 size_t nb01,
13 size_t nb02,
14 size_t nb11,
15 size_t nb21,
16 sycl::nd_item<3> item_ct1) {
17 const int64_t i1 = item_ct1.get_group(2);
18 const int64_t i2 = item_ct1.get_group(1);
19
20 const int i11 =
21 *(const int32_t*)((const char*)src2 + i1 * sizeof(int32_t) + i2 * nb21);
22
23 const size_t nb1 = ne0 * sizeof(float);
24 const size_t nb2 = ne1 * nb1;
25
26 float* dst_row = (float*)((char*)dst + i1 * nb1 + i2 * nb2);
27 const float* src0_row =
28 (const float*)((const char*)src0 + i1 * nb01 + i2 * nb02);
29
30 if (i11 == -1) { // skipped slot, add nothing
31 for (int64_t i0 = item_ct1.get_local_id(2); i0 < ne0;
32 i0 += item_ct1.get_local_range(2)) {
33 dst_row[i0] = src0_row[i0];
34 }
35 return;
36 }
37
38 const float* src1_row = (const float*)((const char*)src1 + i11 * nb11);
39
40 for (int64_t i0 = item_ct1.get_local_id(2); i0 < ne0;
41 i0 += item_ct1.get_local_range(2)) {
42 dst_row[i0] = src0_row[i0] + src1_row[i0];
43 }
44}
45
46void ggml_sycl_add_id(ggml_backend_sycl_context& ctx, ggml_tensor* dst) {
47 const ggml_tensor* src0 = dst->src[0];
48 const ggml_tensor* src1 = dst->src[1];
49 const ggml_tensor* src2 = dst->src[2];
50
51 GGML_TENSOR_TERNARY_OP_LOCALS
52
53 GGML_ASSERT(dst->type == GGML_TYPE_F32);
54 GGML_ASSERT(src0->type == GGML_TYPE_F32);
55 GGML_ASSERT(src1->type == GGML_TYPE_F32);
56 GGML_ASSERT(src2->type == GGML_TYPE_I32);
57
58 GGML_ASSERT(nb00 == sizeof(float));
59 GGML_ASSERT(nb10 == sizeof(float));
60 GGML_ASSERT(nb20 == sizeof(int32_t));
61
62 const float* src0_d = (const float*)src0->data;
63 const float* src1_d = (const float*)src1->data;
64 const int32_t* src2_d = (const int32_t*)src2->data;
65 float* dst_d = (float*)dst->data;
66
67 const unsigned int max_work_group_size = ggml_sycl_info().max_work_group_sizes[ctx.device];
68 GGML_ASSERT(max_work_group_size % (WARP_SIZE * WARP_SIZE) == 0);
69
70 int threads = std::min((unsigned int)ne00, max_work_group_size); // cols
71
72 ctx.stream()->parallel_for(
73 sycl::nd_range<3>(
74 sycl::range<3>(1, ne02, ne01) * sycl::range<3>(1, 1, threads),
75 sycl::range<3>(1, 1, threads)),
76 [=](sycl::nd_item<3> item_ct1) {
77 add_id_kernel(
78 src0_d,
79 src1_d,
80 src2_d,
81 dst_d,
82 ne0,
83 ne1,
84 nb01,
85 nb02,
86 nb11,
87 nb21,
88 item_ct1);
89 });
90}