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/common.h # 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 / common.h` 126 lines ยท 3481 bytes Raw Blame History 1 #pragma once 2 3 #include "ggml-metal-impl.h" 4 5 #include 6 7 #ifdef GGML_METAL_HAS_TENSOR 8 #include 9 10 #include 11 #endif 12 13 using namespace metal; 14 15 #define MAX(x, y) ((x) > (y) ? (x) : (y)) 16 #define MIN(x, y) ((x) < (y) ? (x) : (y)) 17 #define SWAP(x, y) { auto tmp = (x); (x) = (y); (y) = tmp; } 18 19 #define PAD2(x, n) (((x) + (n) - 1) & ~((n) - 1)) 20 21 #define FOR_UNROLL(x) _Pragma("clang loop unroll(full)") for (x) 22 23 #define N_SIMDWIDTH 32 // assuming SIMD group size is 32 24 25 // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf 26 // 27 // cmd: 28 // .../usr/bin/metal -dM -E -c ggml/src/ggml-metal/kernels/.metal 29 // .../usr/bin/metal -dM -E -c -target air64-apple-ios14.0 ggml/src/ggml-metal/kernels/.metal 30 // 31 #if __METAL_VERSION__ < 310 && defined(GGML_METAL_HAS_BF16) 32 #undef GGML_METAL_HAS_BF16 33 #endif 34 35 #if defined(GGML_METAL_HAS_BF16) 36 typedef matrix bfloat4x4; 37 typedef matrix bfloat2x4; 38 #endif 39 40 constexpr constant static float kvalues_iq4nl_f[16] = { 41 -127.f, -104.f, -83.f, -65.f, -49.f, -35.f, -22.f, -10.f, 1.f, 13.f, 25.f, 38.f, 53.f, 69.f, 89.f, 113.f 42 }; 43 44 constexpr constant static float kvalues_mxfp4_f[16] = { 45 0, .5f, 1.f, 1.5f, 2.f, 3.f, 4.f, 6.f, -0, -.5f, -1.f, -1.5f, -2.f, -3.f, -4.f, -6.f 46 }; 47 48 static inline int best_index_int8(int n, constant float * val, float x) { 49 if (x <= val[0]) return 0; 50 if (x >= val[n-1]) return n-1; 51 int ml = 0, mu = n-1; 52 while (mu-ml > 1) { 53 int mav = (ml+mu)/2; 54 if (x < val[mav]) mu = mav; else ml = mav; 55 } 56 return x - val[mu-1] < val[mu] - x ? mu-1 : mu; 57 } 58 59 static inline float e8m0_to_fp32(uint8_t x) { 60 uint32_t bits; 61 62 if (x == 0) { 63 bits = 0x00400000; 64 } else { 65 bits = (uint32_t) x << 23; 66 } 67 68 return as_type(bits); 69 } 70 71 static inline float dot(float x, float y) { 72 return x*y; 73 } 74 75 static inline float sum(float x) { 76 return x; 77 } 78 79 static inline float sum(float4 x) { 80 return x[0] + x[1] + x[2] + x[3]; 81 } 82 83 enum ggml_sort_order { 84 GGML_SORT_ORDER_ASC, 85 GGML_SORT_ORDER_DESC, 86 }; 87 88 constant float GELU_COEF_A = 0.`044715f`; 89 constant float GELU_QUICK_COEF = -1.702f; 90 constant float SQRT_2_OVER_PI = 0.`79788456080286535587989211986876f`; 91 constant float SQRT_2_INV = 0.`70710678118654752440084436210484f`; 92 93 // based on Abramowitz and Stegun formula 7.1.26 or similar Hastings' approximation 94 // ref: https://www.johndcook.com/blog/python_erf/ 95 constant float p_erf = 0.`3275911f`; 96 constant float a1_erf = 0.`254829592f`; 97 constant float a2_erf = -0.`284496736f`; 98 constant float a3_erf = 1.`421413741f`; 99 constant float a4_erf = -1.`453152027f`; 100 constant float a5_erf = 1.`061405429f`; 101 102 template 103 inline T erf_approx(T x) { 104 T sign_x = sign(x); 105 x = fabs(x); 106 T t = 1.0f / (1.0f + p_erf * x); 107 T y = 1.0f - (((((a5_erf * t + a4_erf) * t) + a3_erf) * t + a2_erf) * t + a1_erf) * t * exp(-x * x); 108 return sign_x * y; 109 } 110 111 template T elu_approx(T x); 112 113 template<> inline float elu_approx(float x) { 114 return (x > 0.f) ? x : (exp(x) - 1); 115 } 116 117 template<> inline float4 elu_approx(float4 x) { 118 float4 res; 119 120 res[0] = (x[0] > 0.0f) ? x[0] : (exp(x[0]) - 1.0f); 121 res[1] = (x[1] > 0.0f) ? x[1] : (exp(x[1]) - 1.0f); 122 res[2] = (x[2] > 0.0f) ? x[2] : (exp(x[2]) - 1.0f); 123 res[3] = (x[3] > 0.0f) ? x[3] : (exp(x[3]) - 1.0f); 124 125 return res; 126 }