1#pragma once
2#include "common.cuh"
3
4#define CUDA_NEG_BLOCK_SIZE 256
5#define CUDA_STEP_BLOCK_SIZE 256
6#define CUDA_GELU_BLOCK_SIZE 256
7#define CUDA_SILU_BLOCK_SIZE 256
8#define CUDA_SILU_BACK_BLOCK_SIZE 256
9#define CUDA_TANH_BLOCK_SIZE 256
10#define CUDA_RELU_BLOCK_SIZE 256
11#define CUDA_SIGMOID_BLOCK_SIZE 256
12#define CUDA_HARDSIGMOID_BLOCK_SIZE 256
13#define CUDA_EXP_BLOCK_SIZE 256
14#define CUDA_HARDSWISH_BLOCK_SIZE 256
15#define CUDA_SQR_BLOCK_SIZE 256
16#define CUDA_SQRT_BLOCK_SIZE 256
17#define CUDA_SIN_BLOCK_SIZE 256
18#define CUDA_COS_BLOCK_SIZE 256
19#define CUDA_GLU_BLOCK_SIZE 256
20#define CUDA_XIELU_BLOCK_SIZE 256
21
22void ggml_cuda_op_abs(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
23
24void ggml_cuda_op_sgn(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
25
26void ggml_cuda_op_neg(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
27
28void ggml_cuda_op_step(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
29
30void ggml_cuda_op_gelu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
31
32void ggml_cuda_op_silu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
33
34void ggml_cuda_op_silu_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
35
36void ggml_cuda_op_gelu_erf(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
37
38void ggml_cuda_op_gelu_quick(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
39
40void ggml_cuda_op_tanh(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
41
42void ggml_cuda_op_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
43
44void ggml_cuda_op_sigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
45
46void ggml_cuda_op_hardsigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
47
48void ggml_cuda_op_exp(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
49
50void ggml_cuda_op_hardswish(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
51
52void ggml_cuda_op_leaky_relu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
53
54void ggml_cuda_op_sqr(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
55
56void ggml_cuda_op_sqrt(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
57
58void ggml_cuda_op_sin(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
59
60void ggml_cuda_op_cos(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
61
62void ggml_cuda_op_log(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
63
64void ggml_cuda_op_elu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
65
66void ggml_cuda_op_floor(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
67
68void ggml_cuda_op_ceil(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
69
70void ggml_cuda_op_round(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
71
72void ggml_cuda_op_trunc(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
73
74void ggml_cuda_op_reglu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
75
76void ggml_cuda_op_geglu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
77
78void ggml_cuda_op_swiglu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
79
80void ggml_cuda_op_swiglu_oai(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
81
82void ggml_cuda_op_geglu_erf(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
83
84void ggml_cuda_op_geglu_quick(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
85
86void ggml_cuda_op_xielu(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
87
88__device__ __forceinline__ float ggml_cuda_op_silu_single(float x) {
89 return x / (1.0f + expf(a: -x));
90}
91
92__device__ __forceinline__ float ggml_cuda_op_gelu_single(float x) {
93 const float GELU_COEF_A = 0.044715f;
94 const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
95
96 return 0.5f * x * (1.0f + tanhf(a: SQRT_2_OVER_PI * x * (1.0f + GELU_COEF_A * x * x)));
97}
98
99__device__ __forceinline__ float ggml_cuda_op_swiglu_oai_single(float x, float g, float alpha = 1.702f, float limit = 7.0f) {
100 x = fminf(a: x, b: limit);
101 g = fmaxf(a: fminf(a: g, b: limit), b: -limit);
102
103 float out_glu = x / (1.0f + expf(a: -x * alpha));
104 out_glu = out_glu * (1.0f + g);
105 return out_glu;
106}
107