Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-metal/kernels/unary.metal # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-metal / kernels / unary.metal` 433 lines ยท 16362 bytes Raw Blame History 1 #include "common.h" 2 3 constant short FC_unary_op [[function_constant(FC_UNARY + 0)]]; 4 constant bool FC_unary_cnt[[function_constant(FC_UNARY + 1)]]; 5 6 template 7 inline 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 13 template <> 14 inline bfloat4 ggml_metal_unary_cast(float4 x) { 15 return bfloat4(bfloat(x[0]), bfloat(x[1]), bfloat(x[2]), bfloat(x[3])); 16 } 17 18 template <> 19 inline bfloat4 ggml_metal_unary_cast(float x) { 20 return bfloat4(bfloat(x), bfloat(x), bfloat(x), bfloat(x)); 21 } 22 #endif 23 24 template 25 kernel 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(args.scale * x + args.bias); 74 } 75 76 if (FC_OP == OP_UNARY_NUM_FILL) { 77 dst_ptr[i0] = ggml_metal_unary_cast(args.val); 78 } 79 80 if (FC_OP == OP_UNARY_NUM_CLAMP) { 81 dst_ptr[i0] = ggml_metal_unary_cast(clamp(x, args.min, args.max)); 82 } 83 84 if (FC_OP == OP_UNARY_NUM_SQR) { 85 dst_ptr[i0] = ggml_metal_unary_cast(x * x); 86 } 87 88 if (FC_OP == OP_UNARY_NUM_SQRT) { 89 dst_ptr[i0] = ggml_metal_unary_cast(sqrt(x)); 90 } 91 92 if (FC_OP == OP_UNARY_NUM_SIN) { 93 dst_ptr[i0] = ggml_metal_unary_cast(sin(x)); 94 } 95 96 if (FC_OP == OP_UNARY_NUM_COS) { 97 dst_ptr[i0] = ggml_metal_unary_cast(cos(x)); 98 } 99 100 if (FC_OP == OP_UNARY_NUM_LOG) { 101 dst_ptr[i0] = ggml_metal_unary_cast(log(x)); 102 } 103 104 if (FC_OP == OP_UNARY_NUM_LEAKY_RELU) { 105 dst_ptr[i0] = ggml_metal_unary_cast(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(precise::tanh(x)); 110 } 111 112 if (FC_OP == OP_UNARY_NUM_RELU) { 113 dst_ptr[i0] = ggml_metal_unary_cast(fmax(0, x)); 114 } 115 116 if (FC_OP == OP_UNARY_NUM_SIGMOID) { 117 dst_ptr[i0] = ggml_metal_unary_cast(1 / (1 + exp(-x))); 118 } 119 120 if (FC_OP == OP_UNARY_NUM_GELU) { 121 dst_ptr[i0] = ggml_metal_unary_cast(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(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(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(x / (1 + exp(-x))); 134 } 135 136 if (FC_OP == OP_UNARY_NUM_ELU) { 137 dst_ptr[i0] = ggml_metal_unary_cast(elu_approx(x)); 138 } 139 140 if (FC_OP == OP_UNARY_NUM_NEG) { 141 dst_ptr[i0] = ggml_metal_unary_cast(-x); 142 } 143 144 if (FC_OP == OP_UNARY_NUM_ABS) { 145 dst_ptr[i0] = ggml_metal_unary_cast(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(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(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(exp(x)); 166 } 167 168 if (FC_OP == OP_UNARY_NUM_SOFTPLUS) { 169 dst_ptr[i0] = ggml_metal_unary_cast(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(exp(x) - 1); 175 } 176 177 if (FC_OP == OP_UNARY_NUM_FLOOR) { 178 dst_ptr[i0] = ggml_metal_unary_cast(floor(x)); 179 } 180 181 if (FC_OP == OP_UNARY_NUM_CEIL) { 182 dst_ptr[i0] = ggml_metal_unary_cast(ceil(x)); 183 } 184 185 if (FC_OP == OP_UNARY_NUM_ROUND) { 186 dst_ptr[i0] = ggml_metal_unary_cast(round(x)); 187 } 188 189 if (FC_OP == OP_UNARY_NUM_TRUNC) { 190 dst_ptr[i0] = ggml_metal_unary_cast(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(gate * y_pos + (TC(1.0f) - gate) * y_neg); 200 } 201 } 202 203 #undef FC_OP 204 #undef FC_CNT 205 } 206 207 typedef decltype(kernel_unary_impl) kernel_unary_t; 208 209 template [[host_name("kernel_unary_f32_f32")]] kernel kernel_unary_t kernel_unary_impl; 210 template [[host_name("kernel_unary_f32_f32_4")]] kernel kernel_unary_t kernel_unary_impl; 211 template [[host_name("kernel_unary_f16_f16")]] kernel kernel_unary_t kernel_unary_impl; 212 template [[host_name("kernel_unary_f16_f16_4")]] kernel kernel_unary_t kernel_unary_impl; 213 #if defined(GGML_METAL_HAS_BF16) 214 template [[host_name("kernel_unary_bf16_bf16")]] kernel kernel_unary_t kernel_unary_impl; 215 template [[host_name("kernel_unary_bf16_bf16_4")]] kernel kernel_unary_t kernel_unary_impl; 216 #endif 217 218 kernel 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 232 template 233 kernel 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 254 typedef decltype(kernel_reglu) kernel_reglu_t; 255 256 template [[host_name("kernel_reglu_f32")]] kernel kernel_reglu_t kernel_reglu; 257 template [[host_name("kernel_reglu_f16")]] kernel kernel_reglu_t kernel_reglu; 258 259 template 260 kernel 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 283 typedef decltype(kernel_geglu) kernel_geglu_t; 284 285 template [[host_name("kernel_geglu_f32")]] kernel kernel_geglu_t kernel_geglu; 286 template [[host_name("kernel_geglu_f16")]] kernel kernel_geglu_t kernel_geglu; 287 288 template 289 kernel 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 312 typedef decltype(kernel_swiglu) kernel_swiglu_t; 313 314 template [[host_name("kernel_swiglu_f32")]] kernel kernel_swiglu_t kernel_swiglu; 315 template [[host_name("kernel_swiglu_f16")]] kernel kernel_swiglu_t kernel_swiglu; 316 317 template 318 kernel 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 345 typedef decltype(kernel_swiglu_oai) kernel_swiglu_oai_t; 346 347 template [[host_name("kernel_swiglu_oai_f32")]] kernel kernel_swiglu_oai_t kernel_swiglu_oai; 348 template [[host_name("kernel_swiglu_oai_f16")]] kernel kernel_swiglu_oai_t kernel_swiglu_oai; 349 350 template 351 kernel 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 372 typedef decltype(kernel_swiglu_clamp) kernel_swiglu_clamp_t; 373 374 template [[host_name("kernel_swiglu_clamp_f32")]] kernel kernel_swiglu_clamp_t kernel_swiglu_clamp; 375 template [[host_name("kernel_swiglu_clamp_f16")]] kernel kernel_swiglu_clamp_t kernel_swiglu_clamp; 376 377 template 378 kernel 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(x0*SQRT_2_INV)); 396 397 dst_row[i0] = (T)(gelu_erf*x1); 398 } 399 } 400 401 typedef decltype(kernel_geglu_erf) kernel_geglu_erf_t; 402 403 template [[host_name("kernel_geglu_erf_f32")]] kernel kernel_geglu_erf_t kernel_geglu_erf; 404 template [[host_name("kernel_geglu_erf_f16")]] kernel kernel_geglu_erf_t kernel_geglu_erf; 405 406 template 407 kernel 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 430 typedef decltype(kernel_geglu_quick) kernel_geglu_quick_t; 431 432 template [[host_name("kernel_geglu_quick_f32")]] kernel kernel_geglu_quick_t kernel_geglu_quick; 433 template [[host_name("kernel_geglu_quick_f16")]] kernel kernel_geglu_quick_t kernel_geglu_quick;