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-metal / kernels / unary.metal

433 lines · 16362 bytes Raw Blame History
1#include "common.h"
2
3constant short FC_unary_op [[function_constant(FC_UNARY + 0)]];
4constant bool FC_unary_cnt[[function_constant(FC_UNARY + 1)]];
5
6template <typename T, typename U>
7inline constexpr T ggml_metal_unary_cast(U x) {
8 return (T) x;
9}
10
11#if defined(GGML_METAL_HAS_BF16)
12// MSL has no C-style cast from float vectors to bfloat vectors
13template <>
14inline bfloat4 ggml_metal_unary_cast<bfloat4, float4>(float4 x) {
15 return bfloat4(bfloat(x[0]), bfloat(x[1]), bfloat(x[2]), bfloat(x[3]));
16}
17
18template <>
19inline bfloat4 ggml_metal_unary_cast<bfloat4, float>(float x) {
20 return bfloat4(bfloat(x), bfloat(x), bfloat(x), bfloat(x));
21}
22#endif
23
24template <typename T0, typename T, typename TC>
25kernel void kernel_unary_impl(
26 constant ggml_metal_kargs_unary & args,
27 device const char * src0,
28 device char * dst,
29 uint3 tgpig[[threadgroup_position_in_grid]],
30 ushort3 tpitg[[thread_position_in_threadgroup]],
31 ushort3 ntg[[threads_per_threadgroup]]) {
32#define FC_OP FC_unary_op
33#define FC_CNT FC_unary_cnt
34
35 device const T0 * src0_ptr;
36 device T * dst_ptr;
37
38 int i0;
39
40 if (FC_CNT) {
41 i0 = tgpig.x*ntg.x + tpitg.x;
42
43 src0_ptr = (device const T0 *) (src0);
44 dst_ptr = (device T *) (dst);
45 } else {
46 const int i03 = tgpig.z;
47 const int i02 = tgpig.y;
48 const int k0 = tgpig.x/args.ne01;
49 const int i01 = tgpig.x - k0*args.ne01;
50
51 i0 = k0*ntg.x + tpitg.x;
52
53 src0_ptr = (device const T0 *) (src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01);
54 dst_ptr = (device T *) (dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 );
55 }
56
57 {
58 //threadgroup_barrier(mem_flags::mem_none);
59
60 if (!FC_CNT) {
61 if (i0 >= args.ne0) {
62 return;
63 }
64 } else {
65 if (i0 >= args.ne00*args.ne01*args.ne02*args.ne03) {
66 return;
67 }
68 }
69
70 const TC x = (TC) src0_ptr[i0];
71
72 if (FC_OP == OP_UNARY_NUM_SCALE) {
73 dst_ptr[i0] = ggml_metal_unary_cast<T>(args.scale * x + args.bias);
74 }
75
76 if (FC_OP == OP_UNARY_NUM_FILL) {
77 dst_ptr[i0] = ggml_metal_unary_cast<T>(args.val);
78 }
79
80 if (FC_OP == OP_UNARY_NUM_CLAMP) {
81 dst_ptr[i0] = ggml_metal_unary_cast<T>(clamp(x, args.min, args.max));
82 }
83
84 if (FC_OP == OP_UNARY_NUM_SQR) {
85 dst_ptr[i0] = ggml_metal_unary_cast<T>(x * x);
86 }
87
88 if (FC_OP == OP_UNARY_NUM_SQRT) {
89 dst_ptr[i0] = ggml_metal_unary_cast<T>(sqrt(x));
90 }
91
92 if (FC_OP == OP_UNARY_NUM_SIN) {
93 dst_ptr[i0] = ggml_metal_unary_cast<T>(sin(x));
94 }
95
96 if (FC_OP == OP_UNARY_NUM_COS) {
97 dst_ptr[i0] = ggml_metal_unary_cast<T>(cos(x));
98 }
99
100 if (FC_OP == OP_UNARY_NUM_LOG) {
101 dst_ptr[i0] = ggml_metal_unary_cast<T>(log(x));
102 }
103
104 if (FC_OP == OP_UNARY_NUM_LEAKY_RELU) {
105 dst_ptr[i0] = ggml_metal_unary_cast<T>(TC(x > 0)*x + TC(x <= 0)*(x * args.slope));
106 }
107
108 if (FC_OP == OP_UNARY_NUM_TANH) {
109 dst_ptr[i0] = ggml_metal_unary_cast<T>(precise::tanh(x));
110 }
111
112 if (FC_OP == OP_UNARY_NUM_RELU) {
113 dst_ptr[i0] = ggml_metal_unary_cast<T>(fmax(0, x));
114 }
115
116 if (FC_OP == OP_UNARY_NUM_SIGMOID) {
117 dst_ptr[i0] = ggml_metal_unary_cast<T>(1 / (1 + exp(-x)));
118 }
119
120 if (FC_OP == OP_UNARY_NUM_GELU) {
121 dst_ptr[i0] = ggml_metal_unary_cast<T>(0.5*x*(1 + precise::tanh(SQRT_2_OVER_PI*x*(1 + GELU_COEF_A*x*x))));
122 }
123
124 if (FC_OP == OP_UNARY_NUM_GELU_ERF) {
125 dst_ptr[i0] = ggml_metal_unary_cast<T>(0.5*x*(1 + erf_approx(SQRT_2_INV*x)));
126 }
127
128 if (FC_OP == OP_UNARY_NUM_GELU_QUICK) {
129 dst_ptr[i0] = ggml_metal_unary_cast<T>(x * (1/(1 + exp(GELU_QUICK_COEF*x))));
130 }
131
132 if (FC_OP == OP_UNARY_NUM_SILU) {
133 dst_ptr[i0] = ggml_metal_unary_cast<T>(x / (1 + exp(-x)));
134 }
135
136 if (FC_OP == OP_UNARY_NUM_ELU) {
137 dst_ptr[i0] = ggml_metal_unary_cast<T>(elu_approx(x));
138 }
139
140 if (FC_OP == OP_UNARY_NUM_NEG) {
141 dst_ptr[i0] = ggml_metal_unary_cast<T>(-x);
142 }
143
144 if (FC_OP == OP_UNARY_NUM_ABS) {
145 dst_ptr[i0] = ggml_metal_unary_cast<T>(fabs(x));
146 }
147
148 if (FC_OP == OP_UNARY_NUM_SGN) {
149 dst_ptr[i0] = T(x > 0) - T(x < 0);
150 }
151
152 if (FC_OP == OP_UNARY_NUM_STEP) {
153 dst_ptr[i0] = T(x > 0);
154 }
155
156 if (FC_OP == OP_UNARY_NUM_HARDSWISH) {
157 dst_ptr[i0] = ggml_metal_unary_cast<T>(x * fmax(0, fmin(1, x/6 + 0.5)));
158 }
159
160 if (FC_OP == OP_UNARY_NUM_HARDSIGMOID) {
161 dst_ptr[i0] = ggml_metal_unary_cast<T>(fmax(0, fmin(1, x/6 + 0.5)));
162 }
163
164 if (FC_OP == OP_UNARY_NUM_EXP) {
165 dst_ptr[i0] = ggml_metal_unary_cast<T>(exp(x));
166 }
167
168 if (FC_OP == OP_UNARY_NUM_SOFTPLUS) {
169 dst_ptr[i0] = ggml_metal_unary_cast<T>(select(log(1 + exp(x)), x, x > 20));
170 }
171
172 if (FC_OP == OP_UNARY_NUM_EXPM1) {
173 // TODO: precise implementation
174 dst_ptr[i0] = ggml_metal_unary_cast<T>(exp(x) - 1);
175 }
176
177 if (FC_OP == OP_UNARY_NUM_FLOOR) {
178 dst_ptr[i0] = ggml_metal_unary_cast<T>(floor(x));
179 }
180
181 if (FC_OP == OP_UNARY_NUM_CEIL) {
182 dst_ptr[i0] = ggml_metal_unary_cast<T>(ceil(x));
183 }
184
185 if (FC_OP == OP_UNARY_NUM_ROUND) {
186 dst_ptr[i0] = ggml_metal_unary_cast<T>(round(x));
187 }
188
189 if (FC_OP == OP_UNARY_NUM_TRUNC) {
190 dst_ptr[i0] = ggml_metal_unary_cast<T>(trunc(x));
191 }
192
193 if (FC_OP == OP_UNARY_NUM_XIELU) {
194 const TC xi = x;
195 const TC gate = TC(xi > TC(0.0f));
196 const TC clamped = fmin(xi, TC(args.val));
197 const TC y_pos = TC(args.scale) * xi * xi + TC(args.bias) * xi;
198 const TC y_neg = (exp(clamped) - TC(1.0f) - xi) * TC(args.slope) + TC(args.bias) * xi;
199 dst_ptr[i0] = ggml_metal_unary_cast<T>(gate * y_pos + (TC(1.0f) - gate) * y_neg);
200 }
201 }
202
203#undef FC_OP
204#undef FC_CNT
205}
206
207typedef decltype(kernel_unary_impl<float, float, float>) kernel_unary_t;
208
209template [[host_name("kernel_unary_f32_f32")]] kernel kernel_unary_t kernel_unary_impl<float, float, float>;
210template [[host_name("kernel_unary_f32_f32_4")]] kernel kernel_unary_t kernel_unary_impl<float4, float4, float4>;
211template [[host_name("kernel_unary_f16_f16")]] kernel kernel_unary_t kernel_unary_impl<half, half, float>;
212template [[host_name("kernel_unary_f16_f16_4")]] kernel kernel_unary_t kernel_unary_impl<half4, half4, float4>;
213#if defined(GGML_METAL_HAS_BF16)
214template [[host_name("kernel_unary_bf16_bf16")]] kernel kernel_unary_t kernel_unary_impl<bfloat, bfloat, float>;
215template [[host_name("kernel_unary_bf16_bf16_4")]] kernel kernel_unary_t kernel_unary_impl<bfloat4, bfloat4, float4>;
216#endif
217
218kernel void kernel_silu_back_f32(
219 constant ggml_metal_kargs_silu_back & args,
220 device const float * dy,
221 device const float * x,
222 device float * dx,
223 uint gid [[thread_position_in_grid]]) {
224 if (gid >= args.ne) {
225 return;
226 }
227
228 const float s = 1.0f / (1.0f + exp(-x[gid]));
229 dx[gid] = dy[gid] * s * (1.0f + x[gid] * (1.0f - s));
230}
231
232template<typename T>
233kernel void kernel_reglu(
234 constant ggml_metal_kargs_glu & args,
235 device const char * src0,
236 device const char * src1,
237 device char * dst,
238 uint2 tgpig[[threadgroup_position_in_grid]],
239 uint2 tpitg[[thread_position_in_threadgroup]],
240 uint2 ntg[[threads_per_threadgroup]],
241 uint2 ntgs[[threadgroups_per_grid]]) {
242 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
243 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
244 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
245
246 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
247 const float x0 = src0_row[i0];
248 const float x1 = src1_row[i0];
249
250 dst_row[i0] = (T)(x0*x1*(x0 > 0.0f));
251 }
252}
253
254typedef decltype(kernel_reglu<float>) kernel_reglu_t;
255
256template [[host_name("kernel_reglu_f32")]] kernel kernel_reglu_t kernel_reglu<float>;
257template [[host_name("kernel_reglu_f16")]] kernel kernel_reglu_t kernel_reglu<half>;
258
259template<typename T>
260kernel void kernel_geglu(
261 constant ggml_metal_kargs_glu & args,
262 device const char * src0,
263 device const char * src1,
264 device char * dst,
265 uint2 tgpig[[threadgroup_position_in_grid]],
266 uint2 tpitg[[thread_position_in_threadgroup]],
267 uint2 ntg[[threads_per_threadgroup]],
268 uint2 ntgs[[threadgroups_per_grid]]) {
269 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
270 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
271 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
272
273 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
274 const float x0 = src0_row[i0];
275 const float x1 = src1_row[i0];
276
277 const float gelu = 0.5f*x0*(1.0f + precise::tanh(SQRT_2_OVER_PI*x0*(1.0f + GELU_COEF_A*x0*x0)));
278
279 dst_row[i0] = (T)(gelu*x1);
280 }
281}
282
283typedef decltype(kernel_geglu<float>) kernel_geglu_t;
284
285template [[host_name("kernel_geglu_f32")]] kernel kernel_geglu_t kernel_geglu<float>;
286template [[host_name("kernel_geglu_f16")]] kernel kernel_geglu_t kernel_geglu<half>;
287
288template<typename T>
289kernel void kernel_swiglu(
290 constant ggml_metal_kargs_glu & args,
291 device const char * src0,
292 device const char * src1,
293 device char * dst,
294 uint2 tgpig[[threadgroup_position_in_grid]],
295 uint2 tpitg[[thread_position_in_threadgroup]],
296 uint2 ntg[[threads_per_threadgroup]],
297 uint2 ntgs[[threadgroups_per_grid]]) {
298 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
299 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
300 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
301
302 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
303 const float x0 = src0_row[i0];
304 const float x1 = src1_row[i0];
305
306 const float silu = x0 / (1.0f + exp(-x0));
307
308 dst_row[i0] = (T)(silu*x1);
309 }
310}
311
312typedef decltype(kernel_swiglu<float>) kernel_swiglu_t;
313
314template [[host_name("kernel_swiglu_f32")]] kernel kernel_swiglu_t kernel_swiglu<float>;
315template [[host_name("kernel_swiglu_f16")]] kernel kernel_swiglu_t kernel_swiglu<half>;
316
317template<typename T>
318kernel void kernel_swiglu_oai(
319 constant ggml_metal_kargs_glu & args,
320 device const char * src0,
321 device const char * src1,
322 device char * dst,
323 uint2 tgpig[[threadgroup_position_in_grid]],
324 uint2 tpitg[[thread_position_in_threadgroup]],
325 uint2 ntg[[threads_per_threadgroup]],
326 uint2 ntgs[[threadgroups_per_grid]]) {
327 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
328 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
329 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
330
331 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
332 float x0 = src0_row[i0];
333 float x1 = src1_row[i0];
334
335 x0 = min(x0, args.limit);
336 x1 = max(min(x1, args.limit), -args.limit);
337
338 float out_glu = x0 / (1.0f + exp(-x0 * args.alpha));
339 out_glu = out_glu * (1.0f + x1);
340
341 dst_row[i0] = (T)out_glu;
342 }
343}
344
345typedef decltype(kernel_swiglu_oai<float>) kernel_swiglu_oai_t;
346
347template [[host_name("kernel_swiglu_oai_f32")]] kernel kernel_swiglu_oai_t kernel_swiglu_oai<float>;
348template [[host_name("kernel_swiglu_oai_f16")]] kernel kernel_swiglu_oai_t kernel_swiglu_oai<half>;
349
350template<typename T>
351kernel void kernel_swiglu_clamp(
352 constant ggml_metal_kargs_glu & args,
353 device const char * src0,
354 device const char * src1,
355 device char * dst,
356 uint2 tgpig[[threadgroup_position_in_grid]],
357 uint2 tpitg[[thread_position_in_threadgroup]],
358 uint2 ntg[[threads_per_threadgroup]],
359 uint2 ntgs[[threadgroups_per_grid]]) {
360 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
361 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
362 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
363
364 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
365 const float gate = min((float) src0_row[i0], args.limit);
366 const float up = clamp((float) src1_row[i0], -args.limit, args.limit);
367
368 dst_row[i0] = (T)(gate / (1.0f + exp(-gate)) * up);
369 }
370}
371
372typedef decltype(kernel_swiglu_clamp<float>) kernel_swiglu_clamp_t;
373
374template [[host_name("kernel_swiglu_clamp_f32")]] kernel kernel_swiglu_clamp_t kernel_swiglu_clamp<float>;
375template [[host_name("kernel_swiglu_clamp_f16")]] kernel kernel_swiglu_clamp_t kernel_swiglu_clamp<half>;
376
377template<typename T>
378kernel void kernel_geglu_erf(
379 constant ggml_metal_kargs_glu & args,
380 device const char * src0,
381 device const char * src1,
382 device char * dst,
383 uint2 tgpig[[threadgroup_position_in_grid]],
384 uint2 tpitg[[thread_position_in_threadgroup]],
385 uint2 ntg[[threads_per_threadgroup]],
386 uint2 ntgs[[threadgroups_per_grid]]) {
387 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
388 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
389 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
390
391 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
392 const float x0 = src0_row[i0];
393 const float x1 = src1_row[i0];
394
395 const float gelu_erf = 0.5f*x0*(1.0f+erf_approx<float>(x0*SQRT_2_INV));
396
397 dst_row[i0] = (T)(gelu_erf*x1);
398 }
399}
400
401typedef decltype(kernel_geglu_erf<float>) kernel_geglu_erf_t;
402
403template [[host_name("kernel_geglu_erf_f32")]] kernel kernel_geglu_erf_t kernel_geglu_erf<float>;
404template [[host_name("kernel_geglu_erf_f16")]] kernel kernel_geglu_erf_t kernel_geglu_erf<half>;
405
406template<typename T>
407kernel void kernel_geglu_quick(
408 constant ggml_metal_kargs_glu & args,
409 device const char * src0,
410 device const char * src1,
411 device char * dst,
412 uint2 tgpig[[threadgroup_position_in_grid]],
413 uint2 tpitg[[thread_position_in_threadgroup]],
414 uint2 ntg[[threads_per_threadgroup]],
415 uint2 ntgs[[threadgroups_per_grid]]) {
416 device const T * src0_row = (device const T *) ((device const char *) src0 + tgpig.y*args.nb01) + args.i00;
417 device const T * src1_row = (device const T *) ((device const char *) src1 + tgpig.y*args.nb11) + args.i10;
418 device T * dst_row = (device T *) ((device char *) dst + tgpig.y*args.nb1);
419
420 for (int i0 = tgpig.x*ntg.x + tpitg.x; i0 < args.ne0; i0 += ntg.x*ntgs.x) {
421 const float x0 = src0_row[i0];
422 const float x1 = src1_row[i0];
423
424 const float gelu_quick = x0*(1.0f/(1.0f+exp(GELU_QUICK_COEF*x0)));
425
426 dst_row[i0] = (T)(gelu_quick*x1);
427 }
428}
429
430typedef decltype(kernel_geglu_quick<float>) kernel_geglu_quick_t;
431
432template [[host_name("kernel_geglu_quick_f32")]] kernel kernel_geglu_quick_t kernel_geglu_quick<float>;
433template [[host_name("kernel_geglu_quick_f16")]] kernel kernel_geglu_quick_t kernel_geglu_quick<half>;