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-cuda / binbcast.cu

586 lines · 26192 bytes Raw Blame History
1#include "binbcast.cuh"
2#include "convert.cuh"
3#include <cstdint>
4#include <utility>
5
6template<typename T, size_t>
7using type_for_index = T;
8
9static __device__ __forceinline__ float op_repeat(const float a, const float b) {
10 return b;
11 GGML_UNUSED(a);
12}
13
14static __device__ __forceinline__ float op_add(const float a, const float b) {
15 return a + b;
16}
17
18static __device__ __forceinline__ float op_sub(const float a, const float b) {
19 return a - b;
20}
21
22static __device__ __forceinline__ float op_mul(const float a, const float b) {
23 return a * b;
24}
25
26static __device__ __forceinline__ float op_div(const float a, const float b) {
27 return a / b;
28}
29
30template <float (*bin_op)(const float, const float),
31 typename src0_t,
32 typename src1_t,
33 typename dst_t,
34 typename... src1_ptrs>
35static __global__ void k_bin_bcast(const src0_t * src0,
36 const src1_t * src1,
37 dst_t * dst,
38 const uint32_t ne0,
39 const uint32_t ne1,
40 const uint32_t ne2,
41 const uint3 ne3,
42 const uint3 ne10,
43 const uint3 ne11,
44 const uint3 ne12,
45 const uint3 ne13,
46 /*const uint32_t s0,*/
47 const uint32_t s1,
48 const uint32_t s2,
49 const uint32_t s3,
50 const uint32_t s00,
51 const uint32_t s01,
52 const uint32_t s02,
53 const uint32_t s03,
54 const uint32_t s10,
55 const uint32_t s11,
56 const uint32_t s12,
57 const uint32_t s13,
58 src1_ptrs... src1s) {
59 ggml_cuda_pdl_lc();
60 const uint32_t i0s = blockDim.x * blockIdx.x + threadIdx.x;
61 const uint32_t i1 = (blockDim.y * blockIdx.y + threadIdx.y);
62 const uint32_t i2 = fastdiv((blockDim.z * blockIdx.z + threadIdx.z), ne3);
63 const uint32_t i3 = (blockDim.z * blockIdx.z + threadIdx.z) - (i2 * ne3.z);
64
65 if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3.z) {
66 return;
67 }
68
69 const uint32_t i11 = fastmodulo(i1, ne11);
70 const uint32_t i12 = fastmodulo(i2, ne12);
71 const uint32_t i13 = fastmodulo(i3, ne13);
72
73 const size_t i_src0 = size_t( i3)*s03 + size_t( i2)*s02 + size_t( i1)*s01;
74 const size_t i_src1 = size_t(i13)*s13 + size_t(i12)*s12 + size_t(i11)*s11;
75 const size_t i_dst = size_t( i3)*s3 + size_t( i2)*s2 + size_t( i1)*s1;
76
77 const src0_t * src0_row = src0 ? (src0 + i_src0) : nullptr;
78 dst_t * dst_row = dst + i_dst;
79
80 const uint32_t s0 = blockDim.x * gridDim.x;
81
82 ggml_cuda_pdl_sync();
83 for (uint32_t i0 = i0s; i0 < ne0; i0 += s0) {
84 const uint32_t i10 = fastmodulo(i0, ne10);
85
86 float result = src0_row ? ggml_cuda_cast<float>(src0_row[size_t(i0)*s00]) : 0.0f;
87 if constexpr (sizeof...(src1_ptrs) > 0) {
88 result = (..., (result = bin_op(result, ggml_cuda_cast<float>(src1s[i_src1 + size_t(i10)*s10]))));
89 } else {
90 result = bin_op(result, ggml_cuda_cast<float>(src1[i_src1 + size_t(i10)*s10]));
91 }
92
93 dst_row[i0] = ggml_cuda_cast<dst_t>(result);
94
95 // protect i0 from overflow
96 if (ne0 - i0 <= s0) {
97 break;
98 }
99 }
100}
101
102template <float (*bin_op)(const float, const float),
103 typename src0_t,
104 typename src1_t,
105 typename dst_t,
106 typename... src1_ptrs>
107static __global__ void k_bin_bcast_unravel(const src0_t * src0,
108 const src1_t * src1,
109 dst_t * dst,
110 const uint3 ne0,
111 const uint3 ne1,
112 const uint3 ne2,
113 const uint32_t ne3,
114 const uint3 prod_012,
115 const uint3 prod_01,
116 const uint3 ne10,
117 const uint3 ne11,
118 const uint3 ne12,
119 const uint3 ne13,
120 /*const int s0,*/
121 const uint32_t s1,
122 const uint32_t s2,
123 const uint32_t s3,
124 const uint32_t s00,
125 const uint32_t s01,
126 const uint32_t s02,
127 const uint32_t s03,
128 const uint32_t s10,
129 const uint32_t s11,
130 const uint32_t s12,
131 const uint32_t s13,
132 src1_ptrs... src1s) {
133 const uint32_t i = blockDim.x*blockIdx.x + threadIdx.x;
134
135 const uint32_t i3 = fastdiv(i, prod_012);
136 const uint32_t i2 = fastdiv(i - i3 * prod_012.z, prod_01);
137 const uint32_t i1 = fastdiv(i - i3 * prod_012.z - i2 * prod_01.z, ne0);
138 const uint32_t i0 = i - i3 * prod_012.z - i2 * prod_01.z - i1 * ne0.z;
139
140 if (i0 >= ne0.z || i1 >= ne1.z || i2 >= ne2.z || i3 >= ne3) {
141 return;
142 }
143
144 const uint32_t i11 = fastmodulo(i1, ne11);
145 const uint32_t i12 = fastmodulo(i2, ne12);
146 const uint32_t i13 = fastmodulo(i3, ne13);
147
148 const size_t i_src0 = size_t( i3)*s03 + size_t( i2)*s02 + size_t( i1)*s01;
149 const size_t i_src1 = size_t(i13)*s13 + size_t(i12)*s12 + size_t(i11)*s11;
150 const size_t i_dst = size_t( i3)*s3 + size_t( i2)*s2 + size_t( i1)*s1;
151
152 const src0_t * src0_row = src0 ? (src0 + i_src0) : nullptr;
153 dst_t * dst_row = dst + i_dst;
154
155 const uint32_t i10 = fastmodulo(i0, ne10);
156
157 ggml_cuda_pdl_sync();
158 float result = src0_row ? ggml_cuda_cast<float>(src0_row[size_t(i0)*s00]) : 0.0f;
159 if constexpr (sizeof...(src1_ptrs) > 0) {
160 result = (..., (result = bin_op(result, ggml_cuda_cast<float>(src1s[i_src1 + size_t(i10)*s10]))));
161 } else {
162 result = bin_op(result, ggml_cuda_cast<float>(src1[i_src1 + size_t(i10)*s10]));
163 }
164
165 dst_row[i0] = ggml_cuda_cast<dst_t>(result);
166}
167
168template <float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t, size_t... I>
169static void launch_bin_bcast_pack(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
170 const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
171 cudaStream_t stream, std::index_sequence<I...>) {
172 GGML_TENSOR_BINARY_OP_LOCALS
173
174 int nr0 = ne10 / ne0;
175 int nr1 = ne11 / ne1;
176 int nr2 = ne12 / ne2;
177 int nr3 = ne13 / ne3;
178
179 int nr[4] = { nr0, nr1, nr2, nr3 };
180
181 int64_t cne[] = { ne0, ne1, ne2, ne3 };
182 int64_t cne0[] = { ne00, ne01, ne02, ne03 };
183 int64_t cne1[] = { ne10, ne11, ne12, ne13 };
184
185 size_t cnb[] = { nb0, nb1, nb2, nb3 };
186 size_t cnb0[] = { nb00, nb01, nb02, nb03 };
187 size_t cnb1[] = { nb10, nb11, nb12, nb13 };
188
189 auto collapse = [](int64_t cne[]) {
190 cne[0] *= cne[1];
191 cne[1] = cne[2];
192 cne[2] = cne[3];
193 cne[3] = 1;
194 };
195
196 auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
197 cnb[1] *= cne[1];
198 cnb[2] *= cne[2];
199 cnb[3] *= cne[3];
200 };
201
202 if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
203 for (int i = 0; i < 4; i++) {
204 if (nr[i] != 1) {
205 break;
206 }
207 if (i > 0) {
208 collapse_nb(cnb, cne);
209 collapse_nb(cnb0, cne0);
210 collapse_nb(cnb1, cne1);
211 collapse(cne);
212 collapse(cne0);
213 collapse(cne1);
214 }
215 }
216 }
217
218 {
219 int64_t ne0 = cne[0];
220 int64_t ne1 = cne[1];
221 int64_t ne2 = cne[2];
222 int64_t ne3 = cne[3];
223
224 //int64_t ne00 = cne0[0]; GGML_UNUSED(ne00);
225 //int64_t ne01 = cne0[1]; GGML_UNUSED(ne01);
226 //int64_t ne02 = cne0[2]; GGML_UNUSED(ne02);
227 //int64_t ne03 = cne0[3]; GGML_UNUSED(ne03);
228
229 size_t nb0 = cnb[0];
230 size_t nb1 = cnb[1];
231 size_t nb2 = cnb[2];
232 size_t nb3 = cnb[3];
233
234 size_t nb00 = cnb0[0];
235 size_t nb01 = cnb0[1];
236 size_t nb02 = cnb0[2];
237 size_t nb03 = cnb0[3];
238
239 size_t nb10 = cnb1[0];
240 size_t nb11 = cnb1[1];
241 size_t nb12 = cnb1[2];
242 size_t nb13 = cnb1[3];
243
244 //size_t s0 = nb0 / sizeof(dst_t);
245 size_t s1 = nb1 / sizeof(dst_t);
246 size_t s2 = nb2 / sizeof(dst_t);
247 size_t s3 = nb3 / sizeof(dst_t);
248
249 size_t s10 = nb10 / sizeof(src1_t);
250 size_t s11 = nb11 / sizeof(src1_t);
251 size_t s12 = nb12 / sizeof(src1_t);
252 size_t s13 = nb13 / sizeof(src1_t);
253
254 size_t s00 = nb00 / sizeof(src0_t);
255 size_t s01 = nb01 / sizeof(src0_t);
256 size_t s02 = nb02 / sizeof(src0_t);
257 size_t s03 = nb03 / sizeof(src0_t);
258
259 GGML_ASSERT(ne0 <= std::numeric_limits<uint32_t>::max());
260 GGML_ASSERT(ne1 <= std::numeric_limits<uint32_t>::max());
261 GGML_ASSERT(ne2 <= std::numeric_limits<uint32_t>::max());
262 GGML_ASSERT(ne3 <= std::numeric_limits<uint32_t>::max());
263
264 //GGML_ASSERT(s0 <= std::numeric_limits<uint32_t>::max());
265 GGML_ASSERT(s1 <= std::numeric_limits<uint32_t>::max());
266 GGML_ASSERT(s2 <= std::numeric_limits<uint32_t>::max());
267 GGML_ASSERT(s3 <= std::numeric_limits<uint32_t>::max());
268
269 GGML_ASSERT(s00 <= std::numeric_limits<uint32_t>::max());
270 GGML_ASSERT(s01 <= std::numeric_limits<uint32_t>::max());
271 GGML_ASSERT(s02 <= std::numeric_limits<uint32_t>::max());
272 GGML_ASSERT(s03 <= std::numeric_limits<uint32_t>::max());
273
274 GGML_ASSERT(s10 <= std::numeric_limits<uint32_t>::max());
275 GGML_ASSERT(s11 <= std::numeric_limits<uint32_t>::max());
276 GGML_ASSERT(s12 <= std::numeric_limits<uint32_t>::max());
277 GGML_ASSERT(s13 <= std::numeric_limits<uint32_t>::max());
278
279 GGML_ASSERT(cne1[0] <= std::numeric_limits<uint32_t>::max());
280 GGML_ASSERT(cne1[1] <= std::numeric_limits<uint32_t>::max());
281 GGML_ASSERT(cne1[2] <= std::numeric_limits<uint32_t>::max());
282 GGML_ASSERT(cne1[3] <= std::numeric_limits<uint32_t>::max());
283
284 GGML_ASSERT(nb0 % sizeof(dst_t) == 0);
285 GGML_ASSERT(nb1 % sizeof(dst_t) == 0);
286 GGML_ASSERT(nb2 % sizeof(dst_t) == 0);
287 GGML_ASSERT(nb3 % sizeof(dst_t) == 0);
288
289 GGML_ASSERT(nb00 % sizeof(src0_t) == 0);
290 GGML_ASSERT(nb01 % sizeof(src0_t) == 0);
291 GGML_ASSERT(nb02 % sizeof(src0_t) == 0);
292 GGML_ASSERT(nb03 % sizeof(src0_t) == 0);
293
294 GGML_ASSERT(nb10 % sizeof(src1_t) == 0);
295 GGML_ASSERT(nb11 % sizeof(src1_t) == 0);
296 GGML_ASSERT(nb12 % sizeof(src1_t) == 0);
297 GGML_ASSERT(nb13 % sizeof(src1_t) == 0);
298
299 GGML_ASSERT(ne2 * ne3 <= std::numeric_limits<unsigned int>::max());
300
301 const int block_size = 128;
302
303 int64_t hne0 = std::max(ne0 / 2LL, 1LL);
304
305 dim3 block_dims;
306 block_dims.x = std::min<unsigned int>(hne0, block_size);
307 block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
308 block_dims.z = std::min(std::min<unsigned int>(ne2 * ne3, block_size / block_dims.x / block_dims.y), 64U);
309
310 dim3 block_nums((hne0 + block_dims.x - 1) / block_dims.x, (ne1 + block_dims.y - 1) / block_dims.y,
311 (ne2 * ne3 + block_dims.z - 1) / block_dims.z);
312
313 const uint3 ne10 = init_fastdiv_values((uint32_t) cne1[0]);
314 const uint3 ne11 = init_fastdiv_values((uint32_t) cne1[1]);
315 const uint3 ne12 = init_fastdiv_values((uint32_t) cne1[2]);
316 const uint3 ne13 = init_fastdiv_values((uint32_t) cne1[3]);
317
318 if (block_nums.z > 65535 || block_nums.y > 65535) {
319 int64_t block_num = (ne0 * ne1 * ne2 * ne3 + block_size - 1) / block_size;
320
321 GGML_ASSERT(block_num <= std::numeric_limits<uint32_t>::max());
322 GGML_ASSERT(block_num * block_size <= std::numeric_limits<uint32_t>::max());
323 GGML_ASSERT(ne0 * ne1 <= std::numeric_limits<uint32_t>::max());
324 GGML_ASSERT(ne0 * ne1 * ne2 <= std::numeric_limits<uint32_t>::max());
325
326 const uint3 prod_012 = init_fastdiv_values((uint32_t) (ne0 * ne1 * ne2));
327 const uint3 prod_01 = init_fastdiv_values((uint32_t) (ne0 * ne1));
328 const uint3 ne0_fastdiv = init_fastdiv_values((uint32_t) ne0);
329 const uint3 ne1_fastdiv = init_fastdiv_values((uint32_t) ne1);
330 const uint3 ne2_fastdiv = init_fastdiv_values((uint32_t) ne2);
331
332 {
333 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)block_num, block_size, 0, stream);
334 ggml_cuda_kernel_launch(k_bin_bcast_unravel<bin_op, src0_t, src1_t, dst_t, type_for_index<const src1_t *, I>...>, launch_params,
335 src0_dd, src1_dd, dst_dd, ne0_fastdiv, ne1_fastdiv, ne2_fastdiv, ne3, prod_012, prod_01, ne10, ne11,
336 ne12, ne13,
337 /*s0,*/ s1, s2, s3,
338 s00, s01, s02, s03,
339 s10, s11, s12, s13, (const src1_t *) dst->src[I + 1]->data...);
340 }
341 } else {
342 GGML_ASSERT(int64_t(block_nums.x) * block_dims.x <= std::numeric_limits<uint32_t>::max());
343 GGML_ASSERT(int64_t(block_nums.y) * block_dims.y <= std::numeric_limits<uint32_t>::max());
344 GGML_ASSERT(int64_t(block_nums.z) * block_dims.z <= std::numeric_limits<uint32_t>::max());
345
346 const uint3 ne3_fastdiv = init_fastdiv_values((uint32_t) ne3);
347 {
348 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
349 ggml_cuda_kernel_launch(k_bin_bcast<bin_op, src0_t, src1_t, dst_t, type_for_index<const src1_t *, I>...>, launch_params,
350 src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3_fastdiv, ne10, ne11, ne12, ne13,
351 /*s0,*/ s1, s2, s3,
352 s00, s01, s02, s03,
353 s10, s11, s12, s13, (const src1_t *) dst->src[I + 1]->data...);
354 }
355 }
356 }
357}
358
359template <typename T>
360static __global__ void k_repeat_back(
361 const T * __restrict__ src, T * __restrict__ dst, const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03,
362 const size_t s00, const size_t s01, const size_t s02, const size_t s03,
363 const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3) {
364
365 const int64_t tid0 = int64_t(blockIdx.x)*blockDim.x + threadIdx.x;
366 const int64_t tid1 = int64_t(blockIdx.y)*blockDim.y + threadIdx.y;
367 const int64_t tid23 = int64_t(blockIdx.z)*blockDim.z + threadIdx.z;
368 const int64_t tid2 = tid23 % ne2;
369 const int64_t tid3 = tid23 / ne2;
370
371 if (tid0 >= ne0) {
372 return;
373 }
374
375 T sum = 0;
376 ggml_cuda_pdl_sync();
377 for (int64_t i3 = tid3; i3 < ne03; i3 += ne3) {
378 for (int64_t i2 = tid2; i2 < ne02; i2 += ne2) {
379 for (int64_t i1 = tid1; i1 < ne01; i1 += ne1) {
380 for (int64_t i0 = tid0; i0 < ne00; i0 += ne0) {
381 sum += src[i3*s03 + i2*s02 + i1*s01 + i0*s00];
382 }
383 }
384 }
385 }
386 dst[tid3*ne2*ne1*ne0 + tid2*ne1*ne0 + tid1*ne0 + tid0] = sum;
387}
388
389template <float (*bin_op)(const float, const float), int n_fuse = 1>
390struct bin_bcast_cuda {
391 template<typename src0_t, typename src1_t, typename dst_t>
392 void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
393 const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
394 cudaStream_t stream) {
395 launch_bin_bcast_pack<bin_op, src0_t, src1_t, dst_t>(
396 src0, src1, dst, src0_dd, src1_dd, dst_dd, stream, std::make_index_sequence<n_fuse>{});
397 }
398};
399
400template <typename T>
401static void repeat_back_cuda(
402 const T * src, T * dst, const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03,
403 const size_t s00, const size_t s01, const size_t s02, const size_t s03,
404 const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
405
406 const dim3 block_dims(WARP_SIZE, 1, 1);
407 const dim3 block_nums((ne0 + WARP_SIZE - 1) / WARP_SIZE, ne1, ne2*ne3);
408 k_repeat_back<T><<<block_nums, block_dims, 0, stream>>>
409 (src, dst, ne00, ne01, ne02, ne03, s00, s01, s02, s03, ne0, ne1, ne2, ne3);
410}
411
412template<class op>
413static void ggml_cuda_op_bin_bcast(
414 const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
415 const void * src0_dd, const void * src1_dd, void * dst_dd, cudaStream_t stream) {
416
417 GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_BF16);
418
419 if (src0->type == GGML_TYPE_F32 && src1->type != GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) {
420 op()(src0, src1, dst, (const float *)src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
421 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) {
422 op()(src0, src1, dst, (const nv_bfloat16 *) src0_dd, (const nv_bfloat16 *) src1_dd, (nv_bfloat16 *) dst_dd, stream);
423 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_BF16) {
424 op()(src0, src1, dst, (const nv_bfloat16 *) src0_dd, (const float *) src1_dd, (nv_bfloat16 *) dst_dd, stream);
425 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
426 op()(src0, src1, dst, (const half *) src0_dd, (const half *)src1_dd, (half *) dst_dd, stream);
427 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
428 op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (half *) dst_dd, stream);
429 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
430 op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
431 } else {
432 fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
433 ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
434 GGML_ABORT("fatal error");
435 }
436}
437
438void ggml_cuda_op_repeat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
439 ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat, 0>>(dst, dst->src[0], dst, nullptr, dst->src[0]->data, dst->data, ctx.stream());
440}
441
442void ggml_cuda_op_add(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
443 ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
444}
445
446void ggml_cuda_op_sub(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
447 ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_sub>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
448}
449
450void ggml_cuda_op_mul(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
451 ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
452}
453
454void ggml_cuda_op_div(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
455 ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
456}
457
458template <float (*op)(const float, const float), int n_fuse>
459static void ggml_cuda_op_fused_binbcast_impl(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
460 cudaStream_t stream = ctx.stream();
461
462 const ggml_tensor * src0 = dst->src[0];
463 const ggml_tensor * src1 = dst->src[1];
464
465 if (src0->type == GGML_TYPE_F32 && src1->type != GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) {
466 launch_bin_bcast_pack<op, float, float, float>(src0, src1, dst,
467 (const float *) src0->data, (const float *) src1->data, (float *) dst->data,
468 stream, std::make_index_sequence<n_fuse>{});
469 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) {
470 launch_bin_bcast_pack<op, nv_bfloat16, nv_bfloat16, nv_bfloat16>(src0, src1, dst,
471 (const nv_bfloat16 *) src0->data, (const nv_bfloat16 *) src1->data, (nv_bfloat16 *) dst->data,
472 stream, std::make_index_sequence<n_fuse>{});
473 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_BF16) {
474 launch_bin_bcast_pack<op, nv_bfloat16, float, nv_bfloat16>(src0, src1, dst,
475 (const nv_bfloat16 *) src0->data, (const float *) src1->data, (nv_bfloat16 *) dst->data,
476 stream, std::make_index_sequence<n_fuse>{});
477 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
478 launch_bin_bcast_pack<op, half, half, half>(src0, src1, dst,
479 (const half *) src0->data, (const half *) src1->data, (half *) dst->data,
480 stream, std::make_index_sequence<n_fuse>{});
481 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
482 launch_bin_bcast_pack<op, half, float, half>(src0, src1, dst,
483 (const half *) src0->data, (const float *) src1->data, (half *) dst->data,
484 stream, std::make_index_sequence<n_fuse>{});
485 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
486 launch_bin_bcast_pack<op, half, float, float>(src0, src1, dst,
487 (const half *) src0->data, (const float *) src1->data, (float *) dst->data,
488 stream, std::make_index_sequence<n_fuse>{});
489 } else {
490 fprintf(stderr,
491 "%s: unsupported types for fusion: dst: %s, src0: %s, src1: %s\n",
492 __func__, ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
493 GGML_ABORT("fatal error");
494 }
495}
496
497
498void ggml_cuda_op_fused_add(ggml_backend_cuda_context & ctx, ggml_tensor * dst, int n_fuse) {
499 GGML_ASSERT(2 <= n_fuse && n_fuse <= 8);
500
501 switch (n_fuse) {
502 case 2:
503 ggml_cuda_op_fused_binbcast_impl<op_add, 2>(ctx, dst);
504 break;
505 case 3:
506 ggml_cuda_op_fused_binbcast_impl<op_add, 3>(ctx, dst);
507 break;
508 case 4:
509 ggml_cuda_op_fused_binbcast_impl<op_add, 4>(ctx, dst);
510 break;
511 case 5:
512 ggml_cuda_op_fused_binbcast_impl<op_add, 5>(ctx, dst);
513 break;
514 case 6:
515 ggml_cuda_op_fused_binbcast_impl<op_add, 6>(ctx, dst);
516 break;
517 case 7:
518 ggml_cuda_op_fused_binbcast_impl<op_add, 7>(ctx, dst);
519 break;
520 case 8:
521 ggml_cuda_op_fused_binbcast_impl<op_add, 8>(ctx, dst);
522 break;
523 default:
524 GGML_ASSERT(false && "Unsupported n_fuse value");
525 }
526}
527
528void ggml_cuda_op_fused_mul(ggml_backend_cuda_context & ctx, ggml_tensor * dst, int n_fuse) {
529 GGML_ASSERT(2 <= n_fuse && n_fuse <= 8);
530
531 switch (n_fuse) {
532 case 2:
533 ggml_cuda_op_fused_binbcast_impl<op_mul, 2>(ctx, dst);
534 break;
535 case 3:
536 ggml_cuda_op_fused_binbcast_impl<op_mul, 3>(ctx, dst);
537 break;
538 case 4:
539 ggml_cuda_op_fused_binbcast_impl<op_mul, 4>(ctx, dst);
540 break;
541 case 5:
542 ggml_cuda_op_fused_binbcast_impl<op_mul, 5>(ctx, dst);
543 break;
544 case 6:
545 ggml_cuda_op_fused_binbcast_impl<op_mul, 6>(ctx, dst);
546 break;
547 case 7:
548 ggml_cuda_op_fused_binbcast_impl<op_mul, 7>(ctx, dst);
549 break;
550 case 8:
551 ggml_cuda_op_fused_binbcast_impl<op_mul, 8>(ctx, dst);
552 break;
553 default:
554 GGML_ASSERT(false && "Unsupported n_fuse value");
555 }
556}
557
558void ggml_cuda_op_repeat_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
559 const ggml_tensor * src0 = dst->src[0];
560
561 GGML_ASSERT(src0->type == dst->type);
562 GGML_ASSERT(ggml_is_contiguous(dst));
563 GGML_ASSERT(ggml_can_repeat(dst, src0));
564
565 cudaStream_t stream = ctx.stream();
566
567 GGML_TENSOR_UNARY_OP_LOCALS;
568
569 GGML_ASSERT(ne2*ne3 <= (1 << 15));
570
571 const size_t ts = ggml_type_size(src0->type);
572 const size_t s00 = nb00 / ts;
573 const size_t s01 = nb01 / ts;
574 const size_t s02 = nb02 / ts;
575 const size_t s03 = nb03 / ts;
576
577 switch (dst->type) {
578 case GGML_TYPE_F32: {
579 const float * src0_d = (const float *) src0->data;
580 float * dst_d = (float *) dst->data;
581 repeat_back_cuda(src0_d, dst_d, ne00, ne01, ne02, ne03, s00, s01, s02, s03, ne0, ne1, ne2, ne3, stream);
582 } break;
583 default:
584 GGML_ASSERT(false);
585 }
586}