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 / unary.cu

780 lines · 28752 bytes Raw Blame History
1#include "unary.cuh"
2#include "convert.cuh"
3
4static __device__ __forceinline__ float op_abs(float x) {
5 return fabsf(x);
6}
7
8static __device__ __forceinline__ float op_sgn(float x) {
9 return (x > 0.f ? 1.f : ((x < 0.f ? -1.f : 0.f)));
10}
11
12static __device__ __forceinline__ float op_neg(float x) {
13 return -x;
14}
15
16static __device__ __forceinline__ float op_step(float x) {
17 return x > 0.0f;
18}
19
20static __device__ __forceinline__ float op_gelu(float x) {
21 return ggml_cuda_op_gelu_single(x);
22}
23
24static __device__ __forceinline__ float op_gelu_erf(float x) {
25 const float SQRT_2_INV = 0.70710678118654752440084436210484f;
26
27 return 0.5f*x*(1.0f + erff(x*SQRT_2_INV));
28}
29
30static __device__ __forceinline__ float op_gelu_quick(float x) {
31 const float GELU_QUICK_COEF = -1.702f;
32
33 return x * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x)));
34}
35
36static __device__ __forceinline__ float op_silu(float x) {
37 return ggml_cuda_op_silu_single(x);
38}
39
40static __device__ __forceinline__ float op_tanh(float x) {
41 return tanhf(x);
42}
43
44static __device__ __forceinline__ float op_relu(float x) {
45 return fmaxf(x, 0);
46}
47
48static __device__ __forceinline__ float op_sigmoid(float x) {
49 return 1.0f / (1.0f + expf(-x));
50}
51
52static __device__ __forceinline__ float op_hardsigmoid(float x) {
53 return fminf(1.0f, fmaxf(0.0f, (x + 3.0f) / 6.0f));
54}
55
56static __device__ __forceinline__ float op_hardswish(float x) {
57 return x * fminf(1.0f, fmaxf(0.0f, (x + 3.0f) / 6.0f));
58}
59
60static __device__ __forceinline__ float op_exp(float x) {
61 return expf(x);
62}
63
64static __device__ __forceinline__ float op_sqr(float x) {
65 return x * x;
66}
67
68static __device__ __forceinline__ float op_relu_sqr(float x) {
69 const float r = fmaxf(x, 0.0f);
70 return r * r;
71}
72
73static __device__ __forceinline__ float op_sqrt(float x) {
74 return sqrtf(x);
75}
76
77static __device__ __forceinline__ float op_sin(float x) {
78 return sinf(x);
79}
80
81static __device__ __forceinline__ float op_cos(float x) {
82 return cosf(x);
83}
84
85static __device__ __forceinline__ float op_log(float x) {
86 return logf(x);
87}
88
89static __device__ __forceinline__ float op_expm1(float x) {
90 return expm1f(x);
91}
92
93static __device__ __forceinline__ float op_softplus(float x) {
94 return (x > 20.0f) ? x : log1pf(expf(x));
95}
96
97static __device__ __forceinline__ float op_elu(float x) {
98 return (x > 0.f) ? x : expm1f(x);
99}
100
101static __device__ __forceinline__ float op_floor(float x) {
102 return floorf(x);
103}
104
105static __device__ __forceinline__ float op_ceil(float x) {
106 return ceilf(x);
107}
108
109static __device__ __forceinline__ float op_round(float x) {
110 return roundf(x);
111}
112
113static __device__ __forceinline__ float op_trunc(float x) {
114 return trunc(x);
115}
116
117template <float (*op)(float), typename T>
118static __global__ void unary_op_kernel(const T * x, T * dst, const int k) {
119 ggml_cuda_pdl_lc();
120 const int i = blockDim.x*blockIdx.x + threadIdx.x;
121
122 if (i >= k) {
123 return;
124 }
125
126 ggml_cuda_pdl_sync();
127 dst[i] = ggml_cuda_cast<T>(op(ggml_cuda_cast<float>(x[i])));
128}
129
130template <float (*op)(float), typename T>
131static void unary_cuda(const T * x, T * dst, const int k, cudaStream_t stream) {
132 const int num_blocks = (k + CUDA_NEG_BLOCK_SIZE - 1) / CUDA_NEG_BLOCK_SIZE;
133 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)num_blocks, CUDA_NEG_BLOCK_SIZE, 0, stream);
134 ggml_cuda_kernel_launch(unary_op_kernel<op, T>, launch_params, x, dst, k);
135}
136
137template <float (*op)(float), typename T>
138static __global__ void unary_op_kernel_strided(const T * x, T * dst, const int k,const int64_t ne00,const int64_t ne01,const int64_t ne02,const size_t nb00,const size_t nb01,const size_t nb02,const size_t nb03) {
139 ggml_cuda_pdl_lc();
140 const int i = blockDim.x*blockIdx.x + threadIdx.x;
141
142 if (i >= k) {
143 return;
144 }
145
146 int64_t rem = i;
147 const int64_t i0 = rem % ne00; rem /= ne00;
148 const int64_t i1 = rem % ne01; rem /= ne01;
149 const int64_t i2 = rem % ne02;
150 const int64_t i3 = rem / ne02;
151 const size_t src_byte_offset = i0 * nb00 + i1 * nb01 + i2 * nb02 + i3 * nb03;
152 const T * src_ptr = (const T *)((const char *)x + src_byte_offset);
153
154 ggml_cuda_pdl_sync();
155 dst[i] = ggml_cuda_cast<T>(op(ggml_cuda_cast<float>(*src_ptr)));
156}
157
158template <float (*op)(float), typename T>
159static void unary_cuda_strided(const T * x, T * dst, const int k,const int64_t ne00,const int64_t ne01,const int64_t ne02,const size_t nb00,const size_t nb01,const size_t nb02,const size_t nb03, cudaStream_t stream) {
160 const int num_blocks = (k + CUDA_NEG_BLOCK_SIZE - 1) / CUDA_NEG_BLOCK_SIZE;
161 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)num_blocks, CUDA_NEG_BLOCK_SIZE, 0, stream);
162 ggml_cuda_kernel_launch(unary_op_kernel_strided<op, T>, launch_params, x, dst, k, ne00,ne01,ne02,nb00,nb01,nb02,nb03);
163}
164
165template <float (*op)(float)>
166void ggml_cuda_op_unary(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
167 const ggml_tensor * src0 = dst->src[0];
168 const void * src0_d = src0->data;
169 void * dst_d = dst->data;
170
171 cudaStream_t stream = ctx.stream();
172
173 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
174 GGML_ASSERT(src0->type == dst->type);
175
176 if (ggml_is_contiguous(src0)) {
177 if (src0->type == GGML_TYPE_F16) {
178 unary_cuda<op>((const half *)src0_d, (half *)dst_d, ggml_nelements(src0), stream);
179 } else if (src0->type == GGML_TYPE_BF16) {
180 unary_cuda<op>((const nv_bfloat16 *)src0_d, (nv_bfloat16 *)dst_d, ggml_nelements(src0), stream);
181 } else {
182 unary_cuda<op>((const float *)src0_d, (float *)dst_d, ggml_nelements(src0), stream);
183 }
184 } else {
185 if (src0->type == GGML_TYPE_F16) {
186 unary_cuda_strided<op>((const half *)src0_d, (half *)dst_d, ggml_nelements(src0),
187 src0->ne[0], src0->ne[1], src0->ne[2],
188 src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], stream);
189 } else if (src0->type == GGML_TYPE_BF16) {
190 unary_cuda_strided<op>((const nv_bfloat16 *)src0_d, (nv_bfloat16 *)dst_d, ggml_nelements(src0),
191 src0->ne[0], src0->ne[1], src0->ne[2],
192 src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], stream);
193 } else {
194 unary_cuda_strided<op>((const float *)src0_d, (float *)dst_d, ggml_nelements(src0),
195 src0->ne[0], src0->ne[1], src0->ne[2],
196 src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], stream);
197 }
198 }
199}
200
201void ggml_cuda_op_abs(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
202 ggml_cuda_op_unary<op_abs>(ctx, dst);
203}
204
205void ggml_cuda_op_sgn(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
206 ggml_cuda_op_unary<op_sgn>(ctx, dst);
207}
208
209void ggml_cuda_op_neg(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
210 ggml_cuda_op_unary<op_neg>(ctx, dst);
211}
212
213void ggml_cuda_op_step(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
214 ggml_cuda_op_unary<op_step>(ctx, dst);
215}
216
217void ggml_cuda_op_gelu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
218 ggml_cuda_op_unary<op_gelu>(ctx, dst);
219}
220
221void ggml_cuda_op_gelu_erf(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
222 ggml_cuda_op_unary<op_gelu_erf>(ctx, dst);
223}
224
225void ggml_cuda_op_gelu_quick(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
226 ggml_cuda_op_unary<op_gelu_quick>(ctx, dst);
227}
228
229void ggml_cuda_op_silu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
230 ggml_cuda_op_unary<op_silu>(ctx, dst);
231}
232
233void ggml_cuda_op_tanh(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
234 ggml_cuda_op_unary<op_tanh>(ctx, dst);
235}
236
237void ggml_cuda_op_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
238 ggml_cuda_op_unary<op_relu>(ctx, dst);
239}
240
241void ggml_cuda_op_sigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
242 ggml_cuda_op_unary<op_sigmoid>(ctx, dst);
243}
244
245void ggml_cuda_op_hardsigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
246 ggml_cuda_op_unary<op_hardsigmoid>(ctx, dst);
247}
248
249void ggml_cuda_op_hardswish(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
250 ggml_cuda_op_unary<op_hardswish>(ctx, dst);
251}
252
253void ggml_cuda_op_exp(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
254 ggml_cuda_op_unary<op_exp>(ctx, dst);
255}
256
257void ggml_cuda_op_sqr(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
258 ggml_cuda_op_unary<op_sqr>(ctx, dst);
259}
260
261void ggml_cuda_op_sqrt(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
262 ggml_cuda_op_unary<op_sqrt>(ctx, dst);
263}
264
265void ggml_cuda_op_sin(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
266 ggml_cuda_op_unary<op_sin>(ctx, dst);
267}
268
269void ggml_cuda_op_cos(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
270 ggml_cuda_op_unary<op_cos>(ctx, dst);
271}
272
273void ggml_cuda_op_log(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
274 ggml_cuda_op_unary<op_log>(ctx, dst);
275}
276
277void ggml_cuda_op_elu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
278 ggml_cuda_op_unary<op_elu>(ctx, dst);
279}
280
281void ggml_cuda_op_floor(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
282 ggml_cuda_op_unary<op_floor>(ctx, dst);
283}
284
285void ggml_cuda_op_ceil(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
286 ggml_cuda_op_unary<op_ceil>(ctx, dst);
287}
288
289void ggml_cuda_op_round(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
290 ggml_cuda_op_unary<op_round>(ctx, dst);
291}
292
293void ggml_cuda_op_trunc(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
294 ggml_cuda_op_unary<op_trunc>(ctx, dst);
295}
296
297void ggml_cuda_op_expm1(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
298 ggml_cuda_op_unary<op_expm1>(ctx, dst);
299}
300
301void ggml_cuda_op_softplus(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
302 ggml_cuda_op_unary<op_softplus>(ctx, dst);
303}
304/* gated ops */
305
306template <float (*op)(float), typename T>
307static __global__ void unary_gated_op_kernel(const T * x, const T * g, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1) {
308 ggml_cuda_pdl_lc();
309 const int64_t i = int64_t(blockDim.x)*blockIdx.x + threadIdx.x;
310
311 if (i >= k) {
312 return;
313 }
314
315 // perform base op and multiply with gate (either offset in same tensor or a separate one)
316 const int64_t j0 = (i / n) * o0 + (i % n);
317 const int64_t j1 = o0 == o1 ? j0 : (i / n) * o1 + (i % n);
318
319 ggml_cuda_pdl_sync();
320 dst[i] = ggml_cuda_cast<T>(op(ggml_cuda_cast<float>(x[j0])) * ggml_cuda_cast<float>(g[j1]));
321}
322
323template <float (*op)(float), typename T>
324static void unary_gated_cuda(const T * x, const T * g, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1, cudaStream_t stream) {
325 const int64_t num_blocks = (k + CUDA_GLU_BLOCK_SIZE - 1) / CUDA_GLU_BLOCK_SIZE;
326 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)num_blocks, CUDA_GLU_BLOCK_SIZE, 0, stream);
327 ggml_cuda_kernel_launch(unary_gated_op_kernel<op, T>, launch_params, x, g, dst, k, n, o0, o1);
328}
329
330template <float (*op)(float)>
331void ggml_cuda_op_unary_gated(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
332 const ggml_tensor * src0 = dst->src[0];
333 const ggml_tensor * src1 = dst->src[1];
334 void * src0_d = src0->data;
335 void * src1_d = src1 ? src1->data : src0->data;
336 const int64_t src0_o = src0->nb[1];
337 const int64_t src1_o = src1 ? src1->nb[1] : src0->nb[1];
338 void * dst_d = dst->data;
339 const int64_t nc = src1 ? src0->ne[0] : src0->ne[0] / 2;
340 cudaStream_t stream = ctx.stream();
341
342 GGML_ASSERT(ggml_is_contiguous_1(src0));
343 GGML_ASSERT(src0->nb[0] == ggml_element_size(src0));
344 GGML_ASSERT(ggml_is_contiguous(dst));
345
346 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
347 GGML_ASSERT(src0->type == dst->type);
348 GGML_ASSERT(dst->ne[0] == nc);
349 GGML_ASSERT(ggml_nrows(dst) == ggml_nrows(src0));
350
351 if (src1) {
352 GGML_ASSERT(ggml_is_contiguous_1(src1));
353 GGML_ASSERT(src1->nb[0] == ggml_element_size(src1));
354 GGML_ASSERT(src1->ne[0] == nc);
355 GGML_ASSERT(src0->type == src1->type);
356 }
357
358 const int32_t swapped = ((const int32_t *) dst->op_params)[1];
359
360 if (src0->type == GGML_TYPE_F16) {
361 half * src0_p = (half *) src0_d;
362 half * src1_p = (half *) src1_d;
363
364 if (!src1) {
365 src0_p += swapped ? nc : 0;
366 src1_p += swapped ? 0 : nc;
367 }
368
369 unary_gated_cuda<op>(src0_p, src1_p, (half *)dst_d, ggml_nelements(dst), nc, src0_o / sizeof(half), src1_o / sizeof(half), stream);
370 } else if (src0->type == GGML_TYPE_BF16) {
371 nv_bfloat16 * src0_p = (nv_bfloat16 *) src0_d;
372 nv_bfloat16 * src1_p = (nv_bfloat16 *) src1_d;
373
374 if (!src1) {
375 src0_p += swapped ? nc : 0;
376 src1_p += swapped ? 0 : nc;
377 }
378
379 unary_gated_cuda<op>(src0_p, src1_p, (nv_bfloat16 *)dst_d, ggml_nelements(dst), nc, src0_o / sizeof(nv_bfloat16), src1_o / sizeof(nv_bfloat16), stream);
380 } else {
381 float * src0_p = (float *) src0_d;
382 float * src1_p = (float *) src1_d;
383
384 if (!src1) {
385 src0_p += swapped ? nc : 0;
386 src1_p += swapped ? 0 : nc;
387 }
388
389 unary_gated_cuda<op>(src0_p, src1_p, (float *)dst_d, ggml_nelements(dst), nc, src0_o / sizeof(float), src1_o / sizeof(float), stream);
390 }
391}
392
393void ggml_cuda_op_reglu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
394 ggml_cuda_op_unary_gated<op_relu>(ctx, dst);
395}
396
397void ggml_cuda_op_geglu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
398 ggml_cuda_op_unary_gated<op_gelu>(ctx, dst);
399}
400
401void ggml_cuda_op_swiglu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
402 ggml_cuda_op_unary_gated<op_silu>(ctx, dst);
403}
404
405void ggml_cuda_op_geglu_erf(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
406 ggml_cuda_op_unary_gated<op_gelu_erf>(ctx, dst);
407}
408
409void ggml_cuda_op_geglu_quick(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
410 ggml_cuda_op_unary_gated<op_gelu_quick>(ctx, dst);
411}
412
413// swiglu_oai
414
415template <typename T>
416static __global__ void swiglu_oai_kernel(const T * x, const T * g, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1, float alpha, float limit) {
417 const int64_t i = int64_t(blockDim.x)*blockIdx.x + threadIdx.x;
418
419 if (i >= k) {
420 return;
421 }
422
423 // perform base op and multiply with gate (either offset in same tensor or a separate one)
424 const int64_t j0 = (i / n) * o0 + (i % n);
425 const int64_t j1 = o0 == o1 ? j0 : (i / n) * o1 + (i % n);
426
427 float xi = x[j0];
428 float gi = g[j1];
429
430 dst[i] = ggml_cuda_op_swiglu_oai_single(xi, gi, alpha, limit);
431}
432
433template <typename T>
434static void swiglu_oai_cuda(const T * x, const T * g, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1, const float alpha, const float limit, cudaStream_t stream) {
435 const int64_t num_blocks = (k + CUDA_GLU_BLOCK_SIZE - 1) / CUDA_GLU_BLOCK_SIZE;
436 swiglu_oai_kernel<<<num_blocks, CUDA_GLU_BLOCK_SIZE, 0, stream>>>(x, g, dst, k, n, o0, o1, alpha, limit);
437}
438
439void ggml_cuda_op_swiglu_oai(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
440 const ggml_tensor * src0 = dst->src[0];
441 const ggml_tensor * src1 = dst->src[1];
442 void * src0_d = src0->data;
443 void * src1_d = src1 ? src1->data : src0->data;
444 const int64_t src0_o = src0->nb[1];
445 const int64_t src1_o = src1 ? src1->nb[1] : src0->nb[1];
446 void * dst_d = dst->data;
447 const int64_t nc = src1 ? src0->ne[0] : src0->ne[0] / 2;
448 cudaStream_t stream = ctx.stream();
449
450 GGML_ASSERT(ggml_is_contiguous_1(src0));
451 GGML_ASSERT(src0->nb[0] == ggml_element_size(src0));
452 GGML_ASSERT(ggml_is_contiguous(dst));
453
454 GGML_ASSERT(src0->type == GGML_TYPE_F32);
455 GGML_ASSERT( dst->type == GGML_TYPE_F32);
456 GGML_ASSERT(src0->type == dst->type);
457 GGML_ASSERT(dst->ne[0] == nc);
458 GGML_ASSERT(ggml_nrows(dst) == ggml_nrows(src0));
459
460 if (src1) {
461 GGML_ASSERT(ggml_is_contiguous_1(src1));
462 GGML_ASSERT(src1->nb[0] == ggml_element_size(src1));
463 GGML_ASSERT(src1->ne[0] == nc);
464 GGML_ASSERT(src0->type == src1->type);
465 }
466
467 //const int32_t swapped = ((const int32_t *) dst->op_params)[1];
468 const int32_t swapped = ggml_get_op_params_i32(dst, 1);
469 const float alpha = ggml_get_op_params_f32(dst, 2);
470 const float limit = ggml_get_op_params_f32(dst, 3);
471
472 float * src0_p = (float *) src0_d;
473 float * src1_p = (float *) src1_d;
474
475 if (!src1) {
476 src0_p += swapped ? nc : 0;
477 src1_p += swapped ? 0 : nc;
478 }
479
480 swiglu_oai_cuda(src0_p, src1_p, (float *)dst_d, ggml_nelements(dst), nc, src0_o / sizeof(float), src1_o / sizeof(float), alpha, limit, stream);
481}
482
483// swiglu_clamp
484
485template <typename T>
486static __global__ void swiglu_clamp_kernel(const T * gate, const T * up, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1, float limit) {
487 const int64_t i = int64_t(blockDim.x)*blockIdx.x + threadIdx.x;
488
489 if (i >= k) {
490 return;
491 }
492
493 const int64_t j0 = (i / n) * o0 + (i % n);
494 const int64_t j1 = o0 == o1 ? j0 : (i / n) * o1 + (i % n);
495
496 dst[i] = (T) ggml_cuda_op_swiglu_clamp_single((float) gate[j0], (float) up[j1], limit);
497}
498
499template <typename T>
500static void swiglu_clamp_cuda(const T * gate, const T * up, T * dst, const int64_t k, const int64_t n, const int64_t o0, const int64_t o1, const float limit, cudaStream_t stream) {
501 const int64_t num_blocks = (k + CUDA_GLU_BLOCK_SIZE - 1) / CUDA_GLU_BLOCK_SIZE;
502 swiglu_clamp_kernel<<<num_blocks, CUDA_GLU_BLOCK_SIZE, 0, stream>>>(gate, up, dst, k, n, o0, o1, limit);
503}
504
505void ggml_cuda_op_swiglu_clamp(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
506 const ggml_tensor * src0 = dst->src[0];
507 const ggml_tensor * src1 = dst->src[1];
508 void * src0_d = src0->data;
509 void * src1_d = src1 ? src1->data : src0->data;
510 const int64_t src0_o = src0->nb[1];
511 const int64_t src1_o = src1 ? src1->nb[1] : src0->nb[1];
512 void * dst_d = dst->data;
513 const int64_t nc = src1 ? src0->ne[0] : src0->ne[0] / 2;
514 cudaStream_t stream = ctx.stream();
515
516 GGML_ASSERT(ggml_is_contiguous_1(src0));
517 GGML_ASSERT(src0->nb[0] == ggml_element_size(src0));
518 GGML_ASSERT(ggml_is_contiguous(dst));
519
520 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
521 GGML_ASSERT(src0->type == dst->type);
522 GGML_ASSERT(dst->ne[0] == nc);
523 GGML_ASSERT(ggml_nrows(dst) == ggml_nrows(src0));
524
525 if (src1) {
526 GGML_ASSERT(ggml_is_contiguous_1(src1));
527 GGML_ASSERT(src1->nb[0] == ggml_element_size(src1));
528 GGML_ASSERT(src1->ne[0] == nc);
529 GGML_ASSERT(src0->type == src1->type);
530 }
531
532 const int32_t swapped = ggml_get_op_params_i32(dst, 1);
533 const float limit = ggml_get_op_params_f32(dst, 3);
534
535 if (src0->type == GGML_TYPE_F16) {
536 half * src0_p = (half *) src0_d;
537 half * src1_p = (half *) src1_d;
538
539 if (!src1) {
540 src0_p += swapped ? nc : 0;
541 src1_p += swapped ? 0 : nc;
542 }
543
544 swiglu_clamp_cuda(src0_p, src1_p, (half *) dst_d, ggml_nelements(dst), nc, src0_o / sizeof(half), src1_o / sizeof(half), limit, stream);
545 } else {
546 float * src0_p = (float *) src0_d;
547 float * src1_p = (float *) src1_d;
548
549 if (!src1) {
550 src0_p += swapped ? nc : 0;
551 src1_p += swapped ? 0 : nc;
552 }
553
554 swiglu_clamp_cuda(src0_p, src1_p, (float *) dst_d, ggml_nelements(dst), nc, src0_o / sizeof(float), src1_o / sizeof(float), limit, stream);
555 }
556}
557
558/* CUDA kernel + launcher for xIELU */
559
560template <typename T>
561static __global__ void xielu_kernel(const T * x, T * dst, const int k, float alpha_n, float alpha_p, float beta, float eps) {
562 const int i = blockDim.x*blockIdx.x + threadIdx.x;
563
564 if (i >= k) {
565 return;
566 }
567
568 const float xi = ggml_cuda_cast<float>(x[i]);
569
570 const float gate_pos = (xi > 0.0f);
571 const float y_pos = alpha_p * xi * xi + beta * xi;
572 const float min_v_eps = fminf(xi, eps);
573 const float y_neg = (expm1f(min_v_eps) - xi) * alpha_n + beta * xi;
574 const float out = gate_pos * y_pos + (1.0f - gate_pos) * y_neg;
575
576 dst[i] = ggml_cuda_cast<T>(out);
577}
578
579template <typename T>
580static void xielu_cuda(const T * x, T * dst, const int k, float alpha_n, float alpha_p, float beta, float eps, cudaStream_t stream) {
581 const int num_blocks = (k + CUDA_XIELU_BLOCK_SIZE) / CUDA_XIELU_BLOCK_SIZE;
582 xielu_kernel<<<num_blocks, CUDA_XIELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, alpha_n, alpha_p, beta, eps);
583}
584
585void ggml_cuda_op_xielu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
586 const ggml_tensor * src0 = dst->src[0];
587 const void * src0_d = src0->data;
588 void * dst_d = dst->data;
589 cudaStream_t stream = ctx.stream();
590
591 GGML_ASSERT(ggml_is_contiguous(src0));
592
593 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
594 GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_BF16);
595 GGML_ASSERT(src0->type == dst->type);
596
597 const float alpha_n = ggml_get_op_params_f32(dst, 1);
598 const float alpha_p = ggml_get_op_params_f32(dst, 2);
599 const float beta = ggml_get_op_params_f32(dst, 3);
600 const float eps = ggml_get_op_params_f32(dst, 4);
601
602 if (src0->type == GGML_TYPE_F16) {
603 xielu_cuda((const half *)src0_d, (half *)dst_d, ggml_nelements(src0), alpha_n, alpha_p, beta, eps, stream);
604 } else if (src0->type == GGML_TYPE_BF16) {
605 xielu_cuda((const nv_bfloat16 *)src0_d, (nv_bfloat16 *)dst_d, ggml_nelements(src0), alpha_n, alpha_p, beta, eps, stream);
606 } else {
607 xielu_cuda((const float *)src0_d, (float *)dst_d, ggml_nelements(src0), alpha_n, alpha_p, beta, eps, stream);
608 }
609}
610
611
612
613/* silu_back */
614
615static __device__ __forceinline__ float op_silu_back(float grad, float x) {
616 const float s = 1.0f / (1.0f + expf(-x));
617 return grad * s * (1.0f + x * (1.0f - s));
618}
619
620template <class T>
621static __global__ void silu_back_kernel(const T * grad, const T * xf, T * dst, const int k) {
622 const int i = blockDim.x*blockIdx.x + threadIdx.x;
623
624 if (i >= k) {
625 return;
626 }
627
628 dst[i] = (T)op_silu_back((float)grad[i], (float)xf[i]);
629}
630
631template <class T>
632static void silu_back_cuda(const T * grad, const T * x, T * dst, const int k, cudaStream_t stream) {
633 const int num_blocks = (k + CUDA_SILU_BACK_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
634 silu_back_kernel<<<num_blocks, CUDA_SILU_BACK_BLOCK_SIZE, 0, stream>>>(grad, x, dst, k);
635}
636
637void ggml_cuda_op_silu_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
638 const ggml_tensor * src0 = dst->src[0]; // input from forward pass
639 const ggml_tensor * src1 = dst->src[1]; // grads of forward pass output
640
641 const float * src0_d = (const float *) src0->data;
642 const float * src1_d = (const float *) src1->data;
643 float * dst_d = (float *) dst->data;
644
645 cudaStream_t stream = ctx.stream();
646
647 GGML_ASSERT(ggml_is_contiguous(src0));
648
649 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
650 GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
651 GGML_ASSERT(src0->type == dst->type);
652
653 if (src0->type == GGML_TYPE_F16) {
654 silu_back_cuda((const half *)src0_d, (const half *)src1_d, (half *)dst_d, ggml_nelements(src0), stream);
655 } else {
656 silu_back_cuda((const float*)src0_d, (const float*)src1_d, (float *)dst_d, ggml_nelements(src0), stream);
657 }
658}
659
660/* leaky relu */
661
662static __device__ __forceinline__ float op_leaky_relu(float x, const float negative_slope) {
663 return fmaxf(x, 0) + fminf(x, 0.0f) * negative_slope;
664}
665
666template <class T>
667static __global__ void leaky_relu_kernel(const T * x, T * dst, const int k, const float negative_slope) {
668 const int i = blockDim.x*blockIdx.x + threadIdx.x;
669
670 if (i >= k) {
671 return;
672 }
673
674 dst[i] = (T)op_leaky_relu((float)x[i], negative_slope);
675}
676
677template <class T>
678static void leaky_relu_cuda(const T * x, T * dst, const int k, const float negative_slope, cudaStream_t stream) {
679 const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
680 leaky_relu_kernel<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
681}
682
683void ggml_cuda_op_leaky_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
684 const ggml_tensor * src0 = dst->src[0];
685 const void * src0_d = src0->data;
686 void * dst_d = dst->data;
687 cudaStream_t stream = ctx.stream();
688
689 GGML_ASSERT(ggml_is_contiguous(src0));
690
691 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
692 GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
693 GGML_ASSERT(src0->type == dst->type);
694
695 float negative_slope;
696 memcpy(&negative_slope, dst->op_params, sizeof(float));
697
698 if (src0->type == GGML_TYPE_F16) {
699 leaky_relu_cuda((const half *)src0_d, (half *)dst_d, ggml_nelements(src0), negative_slope, stream);
700 } else {
701 leaky_relu_cuda((const float *)src0_d, (float *)dst_d, ggml_nelements(src0), negative_slope, stream);
702 }
703}
704
705/* fused unary + mul */
706
707template <float (*op)(float)>
708static void ggml_cuda_op_unary_mul_impl(ggml_backend_cuda_context & ctx, ggml_tensor * unary_node, ggml_tensor * mul_node) {
709 // unary_node: UNARY op applied to unary_node->src[0]
710 // mul_node: MUL(a, b) where one of a/b is unary_node
711 // Output goes to mul_node->data
712
713 const ggml_tensor * unary_src = unary_node->src[0]; // input to the unary op
714 const ggml_tensor * other_src = (mul_node->src[0] == unary_node) ? mul_node->src[1] : mul_node->src[0];
715
716 GGML_ASSERT(ggml_is_contiguous_1(unary_src));
717 GGML_ASSERT(unary_src->nb[0] == ggml_element_size(unary_src));
718 GGML_ASSERT(ggml_is_contiguous_1(other_src));
719 GGML_ASSERT(other_src->nb[0] == ggml_element_size(other_src));
720 GGML_ASSERT(ggml_are_same_shape(unary_src, other_src));
721
722 GGML_ASSERT(unary_src->type == GGML_TYPE_F32 || unary_src->type == GGML_TYPE_F16 || unary_src->type == GGML_TYPE_BF16);
723 GGML_ASSERT(unary_src->type == other_src->type);
724 GGML_ASSERT(unary_src->type == mul_node->type);
725
726 cudaStream_t stream = ctx.stream();
727
728 const int64_t k = ggml_nelements(mul_node);
729 const int64_t nc = unary_src->ne[0];
730 const int64_t unary_stride = unary_src->nb[1];
731 const int64_t other_stride = other_src->nb[1];
732
733 if (unary_src->type == GGML_TYPE_F16) {
734 unary_gated_cuda<op>((const half *) unary_src->data, (const half *) other_src->data,
735 (half *) mul_node->data, k, nc,
736 unary_stride / sizeof(half), other_stride / sizeof(half), stream);
737 } else if (unary_src->type == GGML_TYPE_BF16) {
738 unary_gated_cuda<op>((const nv_bfloat16 *) unary_src->data, (const nv_bfloat16 *) other_src->data,
739 (nv_bfloat16 *) mul_node->data, k, nc,
740 unary_stride / sizeof(nv_bfloat16), other_stride / sizeof(nv_bfloat16), stream);
741 } else {
742 unary_gated_cuda<op>((const float *) unary_src->data, (const float *) other_src->data,
743 (float *) mul_node->data, k, nc,
744 unary_stride / sizeof(float), other_stride / sizeof(float), stream);
745 }
746}
747
748void ggml_cuda_op_unary_mul(ggml_backend_cuda_context & ctx, ggml_tensor * unary_node, ggml_tensor * mul_node) {
749 switch (ggml_get_unary_op(unary_node)) {
750 case GGML_UNARY_OP_SILU:
751 ggml_cuda_op_unary_mul_impl<op_silu>(ctx, unary_node, mul_node);
752 break;
753 case GGML_UNARY_OP_SIGMOID:
754 ggml_cuda_op_unary_mul_impl<op_sigmoid>(ctx, unary_node, mul_node);
755 break;
756 case GGML_UNARY_OP_SOFTPLUS:
757 ggml_cuda_op_unary_mul_impl<op_softplus>(ctx, unary_node, mul_node);
758 break;
759 default:
760 GGML_ABORT("Unsupported unary op for fused unary+mul");
761 }
762}
763
764/* fused relu + sqr */
765
766void ggml_cuda_op_relu_sqr(ggml_backend_cuda_context & ctx, ggml_tensor * relu_node, ggml_tensor * sqr_node) {
767 const ggml_tensor * src = relu_node->src[0];
768 cudaStream_t stream = ctx.stream();
769
770 GGML_ASSERT(ggml_is_contiguous(src));
771 GGML_ASSERT(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
772 GGML_ASSERT(src->type == sqr_node->type);
773
774 const int k = ggml_nelements(src);
775 if (src->type == GGML_TYPE_F16) {
776 unary_cuda<op_relu_sqr>((const half *)src->data, (half *)sqr_node->data, k, stream);
777 } else {
778 unary_cuda<op_relu_sqr>((const float *)src->data, (float *)sqr_node->data, k, stream);
779 }
780}