ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-vulkan / vulkan-shaders / unary.comp
| 1 | #version 450 |
| 2 | |
| 3 | #include "types.glsl" |
| 4 | #if defined(UNARY_MUL_FUSION) |
| 5 | #include "generic_binary_head.glsl" |
| 6 | #else |
| 7 | #include "generic_unary_head.glsl" |
| 8 | #endif |
| 9 | |
| 10 | #if defined(UNARY_MUL_FUSION) |
| 11 | // OP on src1 |
| 12 | layout(constant_id = 1) const bool op_on_b = false; |
| 13 | #endif |
| 14 | |
| 15 | #if defined(UNARY_MUL_FUSION) |
| 16 | layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in; |
| 17 | const uint num_threads = 256; |
| 18 | #else |
| 19 | layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in; |
| 20 | #endif |
| 21 | |
| 22 | float op_abs(float x) { |
| 23 | return abs(x); |
| 24 | } |
| 25 | |
| 26 | float op_sgn(float x) { |
| 27 | return sign(x); |
| 28 | } |
| 29 | |
| 30 | float op_neg(float x) { |
| 31 | return -x; |
| 32 | } |
| 33 | |
| 34 | float op_sqr(float x) { |
| 35 | return x * x; |
| 36 | } |
| 37 | |
| 38 | float op_sqrt(float x) { |
| 39 | return sqrt(x); |
| 40 | } |
| 41 | |
| 42 | float op_sin(float x) { |
| 43 | return sin(x); |
| 44 | } |
| 45 | |
| 46 | float op_cos(float x) { |
| 47 | return cos(x); |
| 48 | } |
| 49 | |
| 50 | float op_clamp(float x) { |
| 51 | return clamp(x, p.param1, p.param2); |
| 52 | } |
| 53 | |
| 54 | float op_leaky_relu(float x) { |
| 55 | return max(x, 0.0f) + min(x, 0.0f) * p.param1; |
| 56 | } |
| 57 | |
| 58 | float op_step(float x) { |
| 59 | return x > 0.0f ? 1.0f : 0.0f; |
| 60 | } |
| 61 | |
| 62 | float op_tanh(float x) { |
| 63 | return 1.0f - 2.0f / (exp(2.0f*x) + 1.0f); |
| 64 | } |
| 65 | |
| 66 | float op_elu(float x) { |
| 67 | return x < 0.0f ? exp(x) - 1.0f : x; |
| 68 | } |
| 69 | |
| 70 | float op_relu(float x) { |
| 71 | return max(x, 0.0f); |
| 72 | } |
| 73 | |
| 74 | float op_sigmoid(float x) { |
| 75 | return 1.0f / (1.0f + exp(-x)); |
| 76 | } |
| 77 | |
| 78 | float op_gelu(float x) { |
| 79 | const float GELU_COEF_A = 0.044715f; |
| 80 | const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f; |
| 81 | const float val = SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x); |
| 82 | return 0.5f*x*(2.0f - 2.0f / (exp(2.0f * val) + 1.0f)); |
| 83 | } |
| 84 | |
| 85 | float op_gelu_quick(float x) { |
| 86 | const float GELU_QUICK_COEF = -1.702f; |
| 87 | return x * (1.0f / (1.0f + exp(GELU_QUICK_COEF * x))); |
| 88 | } |
| 89 | |
| 90 | float op_silu(float x) { |
| 91 | return x / (1.0f + exp(-x)); |
| 92 | } |
| 93 | |
| 94 | float op_hardswish(float x) { |
| 95 | return x * min(1.0f, max(0.0f, (x + 3.0f) / 6.0f)); |
| 96 | } |
| 97 | |
| 98 | float op_hardsigmoid(float x) { |
| 99 | return min(1.0f, max(0.0f, (x + 3.0f) / 6.0f)); |
| 100 | } |
| 101 | |
| 102 | float op_exp(float x) { |
| 103 | return exp(x); |
| 104 | } |
| 105 | |
| 106 | float op_expm1(float x) { |
| 107 | // exp(x) - 1 loses many ulps to cancellation near zero. Use a degree-6 |
| 108 | // Taylor expansion for |x| <= 1/4: the omitted x^7/5040 term is < 1.3e-8, |
| 109 | // about 0.5 ulp at expm1(0.25), and a host-side f32 model stays within |
| 110 | // 2 ulps over the interval. The first native exp(x)-1 values outside the |
| 111 | // cutoff are about 1 ulp for +0.25 and 2 ulps for -0.25. |
| 112 | if (abs(x) <= 0.25f) { |
| 113 | return x * (1.0f + x * (0.5f + x * ((1.0f/6.0f) + x * ((1.0f/24.0f) + x * ((1.0f/120.0f) + x * (1.0f/720.0f)))))); |
| 114 | } |
| 115 | return exp(x) - 1.0f; |
| 116 | } |
| 117 | |
| 118 | float op_softplus(float x) { |
| 119 | return (x > 20.0f) ? x : log(1.0f + exp(x)); |
| 120 | } |
| 121 | |
| 122 | float op_gelu_erf(float a) { |
| 123 | // based on Abramowitz and Stegun formula 7.1.26 or similar Hastings' approximation |
| 124 | const float p_erf = 0.3275911f; |
| 125 | const float a1_erf = 0.254829592f; |
| 126 | const float a2_erf = -0.284496736f; |
| 127 | const float a3_erf = 1.421413741f; |
| 128 | const float a4_erf = -1.453152027f; |
| 129 | const float a5_erf = 1.061405429f; |
| 130 | |
| 131 | const float SQRT_2_INV = 0.70710678118654752440084436210484f; |
| 132 | const float a_div_sqr2 = a * SQRT_2_INV; |
| 133 | const float sign_x = sign(a_div_sqr2); |
| 134 | const float x = abs(a_div_sqr2); |
| 135 | const float t = 1.0f / (1.0f + p_erf * x); |
| 136 | const float y = 1.0f - (((((a5_erf * t + a4_erf) * t) + a3_erf) * t + a2_erf) * t + a1_erf) * t * exp(-x * x); |
| 137 | return 0.5f * a * (1.0f + sign_x * y); |
| 138 | } |
| 139 | |
| 140 | #if !defined(UNARY_MUL_FUSION) |
| 141 | float op_xielu(float x) { |
| 142 | const float alpha_n = p.param1; |
| 143 | const float alpha_p = p.param2; |
| 144 | const float beta = p.param3; |
| 145 | const float eps = p.param4; |
| 146 | |
| 147 | if (x > 0.0f) { |
| 148 | return alpha_p * x * x + beta * x; |
| 149 | } |
| 150 | |
| 151 | const float min_x_eps = min(x, eps); |
| 152 | return (op_expm1(min_x_eps) - x) * alpha_n + beta * x; |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | float op_floor(float x) { |
| 157 | return floor(x); |
| 158 | } |
| 159 | |
| 160 | float op_ceil(float x) { |
| 161 | return ceil(x); |
| 162 | } |
| 163 | |
| 164 | float op_round(float x) { |
| 165 | // Round halfway cases away from zero as roundf does. |
| 166 | return x >= 0.0f ? floor(x + 0.5f) : ceil(x - 0.5f); |
| 167 | } |
| 168 | |
| 169 | float op_trunc(float x) { |
| 170 | return trunc(x); |
| 171 | } |
| 172 | |
| 173 | void main() { |
| 174 | uint idx = get_idx(); |
| 175 | |
| 176 | #if defined(UNARY_MUL_FUSION) |
| 177 | // keep total threads at 512 |
| 178 | [[unroll]] for (uint iter = 0; iter < 2; ++iter) { |
| 179 | if (idx >= p.ne) { |
| 180 | continue; |
| 181 | } |
| 182 | uint i00, i01, i02, i03; |
| 183 | get_indices(idx, i00, i01, i02, i03); |
| 184 | |
| 185 | if (op_on_b) { |
| 186 | data_d[get_doffset() + dst_idx(i00, i01, i02, i03)] = |
| 187 | D_TYPE(FLOAT_TYPE(OP(float(data_b[get_boffset() + src1_idx(i00, i01, i02, i03)]))) * FLOAT_TYPE(data_a[get_aoffset() + src0_idx(i00, i01, i02, i03)])); |
| 188 | } else { |
| 189 | data_d[get_doffset() + dst_idx(i00, i01, i02, i03)] = |
| 190 | D_TYPE(FLOAT_TYPE(OP(float(data_a[get_aoffset() + src0_idx(i00, i01, i02, i03)]))) * FLOAT_TYPE(data_b[get_boffset() + src1_idx(i00, i01, i02, i03)])); |
| 191 | } |
| 192 | |
| 193 | idx += num_threads; |
| 194 | } |
| 195 | #else |
| 196 | if (idx >= p.ne) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | const uint a_idx = get_aoffset() + src0_idx(idx); |
| 201 | const uint d_idx = get_doffset() + dst_idx(idx); |
| 202 | |
| 203 | data_d[d_idx] = D_TYPE(OP(float(data_a[a_idx]))); |
| 204 | #endif |
| 205 | } |