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-cpu/vec.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-cpu / vec.h` 1709 lines ยท 72669 bytes Raw Blame History 1 // Vectorized functions for fundamental operations 2 3 #pragma once 4 5 #include "ggml-impl.h" 6 #include "simd-mappings.h" 7 #include "ggml.h" 8 #include "ggml-cpu.h" 9 10 #if defined(GGML_USE_ACCELERATE) 11 #include 12 #endif 13 14 // floating point type used to accumulate sums 15 typedef double ggml_float; 16 17 #if defined(__ARM_FEATURE_SVE) 18 inline static void ggml_sve_f16_fma_widened( 19 svfloat32_t * acc_lo, 20 svfloat32_t * acc_hi, 21 svfloat16_t x, 22 svfloat16_t y) { 23 #if defined(__ARM_FEATURE_SVE2) 24 *acc_lo = svmlalb_f32(*acc_lo, x, y); 25 *acc_hi = svmlalt_f32(*acc_hi, x, y); 26 #else 27 // Plain SVE fallback path if SVE2 instructions not available 28 svfloat16_t x_even = svtrn1_f16(x, x); 29 svfloat16_t x_odd = svtrn2_f16(x, x); 30 31 svfloat16_t y_even = svtrn1_f16(y, y); 32 svfloat16_t y_odd = svtrn2_f16(y, y); 33 34 svbool_t pg = svptrue_b32(); 35 36 *acc_lo = svmla_f32_x(pg, *acc_lo, svcvt_f32_f16_x(pg, x_even), svcvt_f32_f16_x(pg, y_even)); 37 *acc_hi = svmla_f32_x(pg, *acc_hi, svcvt_f32_f16_x(pg, x_odd), svcvt_f32_f16_x(pg, y_odd)); 38 #endif 39 } 40 41 inline static ggml_float ggml_sve_sum_f32x2(svfloat32_t sum_lo, svfloat32_t sum_hi) { 42 return (ggml_float) (svaddv_f32(svptrue_b32(), sum_lo) + svaddv_f32(svptrue_b32(), sum_hi)); 43 } 44 #endif 45 46 #define GGML_GELU_FP16 47 48 #define GGML_SOFT_MAX_UNROLL 4 49 #define GGML_VEC_DOT_UNROLL 2 50 #define GGML_VEC_MAD_UNROLL 32 51 52 #ifdef __cplusplus 53 extern "C" { 54 #endif 55 56 // 57 // global data 58 // 59 60 // precomputed gelu table for f16 (128 KB) 61 extern ggml_fp16_t ggml_table_gelu_f16[1 << 16]; 62 63 // precomputed quick gelu table for f16 (128 KB) 64 extern ggml_fp16_t ggml_table_gelu_quick_f16[1 << 16]; 65 66 // 67 // fundamental operations 68 // 69 70 void ggml_vec_dot_f32(int n, float * GGML_RESTRICT s, size_t bs, const float * GGML_RESTRICT x, size_t bx, const float * GGML_RESTRICT y, size_t by, int nrc); 71 void ggml_vec_dot_bf16(int n, float * GGML_RESTRICT s, size_t bs, ggml_bf16_t * GGML_RESTRICT x, size_t bx, ggml_bf16_t * GGML_RESTRICT y, size_t by, int nrc); 72 void ggml_vec_dot_f16(int n, float * GGML_RESTRICT s, size_t bs, ggml_fp16_t * GGML_RESTRICT x, size_t bx, ggml_fp16_t * GGML_RESTRICT y, size_t by, int nrc); 73 74 void ggml_vec_silu_f32(const int n, float * y, const float * x); 75 ggml_float ggml_vec_cvar_f32(const int n, float * y, const float * x, const float mean); //it will also center y ( y = y - mean ) 76 ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max); 77 ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max); 78 79 inline static void ggml_vec_set_i8(const int n, int8_t * x, const int8_t v) { for (int i = 0; i < n; ++i) x[i] = v; } 80 inline static void ggml_vec_set_i16(const int n, int16_t * x, const int16_t v) { for (int i = 0; i < n; ++i) x[i] = v; } 81 82 inline static void ggml_vec_set_i32(const int n, int32_t * x, const int32_t v) { for (int i = 0; i < n; ++i) x[i] = v; } 83 inline static void ggml_vec_cpy_i32(const int n, int32_t * y, const int32_t * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; } 84 85 inline static void ggml_vec_set_f16(const int n, ggml_fp16_t * x, const ggml_fp16_t v) { for (int i = 0; i < n; ++i) x[i] = v; } 86 inline static void ggml_vec_set_bf16(const int n, ggml_bf16_t * x, const ggml_bf16_t v) { for (int i = 0; i < n; ++i) x[i] = v; } 87 88 inline static void ggml_vec_add_f32 (const int n, float * z, const float * x, const float * y) { 89 int i = 0; 90 #if defined(__AVX2__) 91 for (; i + 7 < n; i += 8) { 92 __m256 vx = _mm256_loadu_ps(x + i); 93 __m256 vy = _mm256_loadu_ps(y + i); 94 __m256 vz = _mm256_add_ps(vx, vy); 95 _mm256_storeu_ps(z + i, vz); 96 } 97 #endif 98 for (; i < n; ++i) { 99 z[i] = x[i] + y[i]; 100 } 101 } 102 103 inline static void ggml_vec_add_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) { 104 for (int i = 0; i < n; ++i) { 105 z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) + GGML_CPU_FP16_TO_FP32(y[i])); 106 } 107 } 108 inline static void ggml_vec_add1_f32(const int n, float * z, const float * x, const float v) { for (int i = 0; i < n; ++i) z[i] = x[i] + v; } 109 inline static void ggml_vec_acc_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] += x[i]; } 110 inline static void ggml_vec_acc1_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] += v; } 111 inline static void ggml_vec_sub_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i] - y[i]; } 112 inline static void ggml_vec_sub_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) { 113 for (int i = 0; i < n; ++i) { 114 z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) - GGML_CPU_FP16_TO_FP32(y[i])); 115 } 116 } 117 inline static void ggml_vec_set_f32 (const int n, float * x, const float v) { for (int i = 0; i < n; ++i) x[i] = v; } 118 inline static void ggml_vec_cpy_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; } 119 inline static void ggml_vec_neg_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = -x[i]; } 120 inline static void ggml_vec_neg_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 121 for (int i = 0; i < n; ++i) { 122 y[i] = GGML_CPU_FP32_TO_FP16(-GGML_CPU_FP16_TO_FP32(x[i])); 123 } 124 } 125 126 inline static void ggml_vec_mul_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]*y[i]; } 127 inline static void ggml_vec_mul_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) { 128 for (int i = 0; i < n; ++i) { 129 z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) * GGML_CPU_FP16_TO_FP32(y[i])); 130 } 131 } 132 inline static void ggml_vec_div_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]/y[i]; } 133 inline static void ggml_vec_div_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) { 134 for (int i = 0; i < n; ++i) { 135 z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) / GGML_CPU_FP16_TO_FP32(y[i])); 136 } 137 } 138 139 // compute GGML_VEC_DOT_UNROLL dot products at once 140 // xs - x row stride in bytes 141 inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * GGML_RESTRICT s, void * GGML_RESTRICT xv, ggml_fp16_t * GGML_RESTRICT y) { 142 ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 }; 143 144 ggml_fp16_t * GGML_RESTRICT x[GGML_VEC_DOT_UNROLL]; 145 146 for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) { 147 x[i] = (ggml_fp16_t *) ((char *) xv + i*xs); 148 } 149 150 #if defined(GGML_SIMD) 151 #if defined(__ARM_FEATURE_SVE) 152 153 const int ggml_f16_epr = svcnth(); 154 const int ggml_f16_step = 2 * ggml_f16_epr; 155 int np = n - (n % ggml_f16_step); 156 int np2 = n - (n % ggml_f16_epr); 157 158 svfloat32_t sum_0_0_lo = svdup_n_f32(0.0f); 159 svfloat32_t sum_0_0_hi = svdup_n_f32(0.0f); 160 svfloat32_t sum_0_1_lo = svdup_n_f32(0.0f); 161 svfloat32_t sum_0_1_hi = svdup_n_f32(0.0f); 162 svfloat32_t sum_1_0_lo = svdup_n_f32(0.0f); 163 svfloat32_t sum_1_0_hi = svdup_n_f32(0.0f); 164 svfloat32_t sum_1_1_lo = svdup_n_f32(0.0f); 165 svfloat32_t sum_1_1_hi = svdup_n_f32(0.0f); 166 167 for (int i = 0; i < np; i += ggml_f16_step) { 168 const svfloat16_t ay0 = GGML_F16x_VEC_LOAD(y + i, 0); 169 const svfloat16_t ax00 = GGML_F16x_VEC_LOAD(x[0] + i, 0); 170 const svfloat16_t ax01 = GGML_F16x_VEC_LOAD(x[1] + i, 0); 171 172 ggml_sve_f16_fma_widened(&sum_0_0_lo, &sum_0_0_hi, ax00, ay0); 173 ggml_sve_f16_fma_widened(&sum_1_0_lo, &sum_1_0_hi, ax01, ay0); 174 175 const svfloat16_t ay1 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 0); 176 const svfloat16_t ax10 = GGML_F16x_VEC_LOAD(x[0] + i + 1 * ggml_f16_epr, 0); 177 const svfloat16_t ax11 = GGML_F16x_VEC_LOAD(x[1] + i + 1 * ggml_f16_epr, 0); 178 179 ggml_sve_f16_fma_widened(&sum_0_1_lo, &sum_0_1_hi, ax10, ay1); 180 ggml_sve_f16_fma_widened(&sum_1_1_lo, &sum_1_1_hi, ax11, ay1); 181 } 182 183 for (int i = np; i < np2; i += ggml_f16_epr) { 184 const svfloat16_t ry = GGML_F16x_VEC_LOAD(y + i, 0); 185 const svfloat16_t rx0 = GGML_F16x_VEC_LOAD(x[0] + i, 0); 186 const svfloat16_t rx1 = GGML_F16x_VEC_LOAD(x[1] + i, 0); 187 188 ggml_sve_f16_fma_widened(&sum_0_0_lo, &sum_0_0_hi, rx0, ry); 189 ggml_sve_f16_fma_widened(&sum_1_0_lo, &sum_1_0_hi, rx1, ry); 190 } 191 192 if (np2 < n) { 193 const svbool_t pg = svwhilelt_b16(np2, n); 194 const svfloat16_t ay = svld1_f16(pg, (const __fp16 *)(y + np2)); 195 const svfloat16_t ax0 = svld1_f16(pg, (const __fp16 *)(x[0] + np2)); 196 const svfloat16_t ax1 = svld1_f16(pg, (const __fp16 *)(x[1] + np2)); 197 198 ggml_sve_f16_fma_widened(&sum_0_0_lo, &sum_0_0_hi, ax0, ay); 199 ggml_sve_f16_fma_widened(&sum_1_0_lo, &sum_1_0_hi, ax1, ay); 200 } 201 202 svfloat32_t sum_0_lo = svadd_f32_x(DEFAULT_PG32, sum_0_0_lo, sum_0_1_lo); 203 svfloat32_t sum_0_hi = svadd_f32_x(DEFAULT_PG32, sum_0_0_hi, sum_0_1_hi); 204 svfloat32_t sum_1_lo = svadd_f32_x(DEFAULT_PG32, sum_1_0_lo, sum_1_1_lo); 205 svfloat32_t sum_1_hi = svadd_f32_x(DEFAULT_PG32, sum_1_0_hi, sum_1_1_hi); 206 sumf[0] = ggml_sve_sum_f32x2(sum_0_lo, sum_0_hi); 207 sumf[1] = ggml_sve_sum_f32x2(sum_1_lo, sum_1_hi); 208 np = n; 209 #elif defined(__riscv_v) 210 #if defined(__riscv_zvfh) 211 size_t vl = __riscv_vsetvlmax_e32m4(); 212 213 // initialize accumulators to all zeroes 214 vfloat32m4_t vsum0_0 = __riscv_vfmv_v_f_f32m4(0.0f, vl); 215 vfloat32m4_t vsum0_1 = __riscv_vfmv_v_f_f32m4(0.0f, vl); 216 vfloat32m4_t vsum1_0 = __riscv_vfmv_v_f_f32m4(0.0f, vl); 217 vfloat32m4_t vsum1_1 = __riscv_vfmv_v_f_f32m4(0.0f, vl); 218 219 // calculate step size 220 const size_t epr = __riscv_vsetvlmax_e16m2(); 221 const size_t step = epr * 2; 222 int np = (n & ~(step - 1)); 223 224 // unroll by 2 along the row dimension 225 for (int i = 0; i < np; i += step) { 226 vfloat16m2_t ay0 = __riscv_vle16_v_f16m2((const _Float16 *)(y + i), epr); 227 vfloat16m2_t ax0_0 = __riscv_vle16_v_f16m2((const _Float16 *)(x[0] + i), epr); 228 vfloat16m2_t ax1_0 = __riscv_vle16_v_f16m2((const _Float16 *)(x[1] + i), epr); 229 vsum0_0 = __riscv_vfwmacc_vv_f32m4(vsum0_0, ax0_0, ay0, epr); 230 vsum1_0 = __riscv_vfwmacc_vv_f32m4(vsum1_0, ax1_0, ay0, epr); 231 232 vfloat16m2_t ay1 = __riscv_vle16_v_f16m2((const _Float16 *)(y + i + epr), epr); 233 vfloat16m2_t ax0_1 = __riscv_vle16_v_f16m2((const _Float16 *)(x[0] + i + epr), epr); 234 vfloat16m2_t ax1_1 = __riscv_vle16_v_f16m2((const _Float16 *)(x[1] + i + epr), epr); 235 vsum0_1 = __riscv_vfwmacc_vv_f32m4(vsum0_1, ax0_1, ay1, epr); 236 vsum1_1 = __riscv_vfwmacc_vv_f32m4(vsum1_1, ax1_1, ay1, epr); 237 } 238 239 vfloat32m4_t vsum0 = __riscv_vfadd_vv_f32m4(vsum0_0, vsum0_1, vl); 240 vfloat32m4_t vsum1 = __riscv_vfadd_vv_f32m4(vsum1_0, vsum1_1, vl); 241 242 // leftovers 243 for (int i = np; i < n; i += vl) { 244 vl = __riscv_vsetvl_e16m2(n - i); 245 vfloat16m2_t ay = __riscv_vle16_v_f16m2((const _Float16 *)(y + i), vl); 246 vfloat16m2_t ax0 = __riscv_vle16_v_f16m2((const _Float16 *)(x[0] + i), vl); 247 vfloat16m2_t ax1 = __riscv_vle16_v_f16m2((const _Float16 *)(x[1] + i), vl); 248 249 vsum0 = __riscv_vfwmacc_vv_f32m4(vsum0, ax0, ay, vl); 250 vsum1 = __riscv_vfwmacc_vv_f32m4(vsum1, ax1, ay, vl); 251 } 252 253 // reduce 254 vl = __riscv_vsetvlmax_e32m2(); 255 vfloat32m2_t acc0_0 = __riscv_vfadd_vv_f32m2(__riscv_vget_v_f32m4_f32m2(vsum0, 0), 256 __riscv_vget_v_f32m4_f32m2(vsum0, 1), vl); 257 vl = __riscv_vsetvlmax_e32m1(); 258 vfloat32m1_t acc0_1 = __riscv_vfadd_vv_f32m1(__riscv_vget_v_f32m2_f32m1(acc0_0, 0), 259 __riscv_vget_v_f32m2_f32m1(acc0_0, 1), vl); 260 vfloat32m1_t redsum0 = __riscv_vfredusum_vs_f32m1_f32m1( 261 acc0_1, __riscv_vfmv_v_f_f32m1(0.0f, 1), vl); 262 263 vl = __riscv_vsetvlmax_e32m2(); 264 vfloat32m2_t acc1_0 = __riscv_vfadd_vv_f32m2(__riscv_vget_v_f32m4_f32m2(vsum1, 0), 265 __riscv_vget_v_f32m4_f32m2(vsum1, 1), vl); 266 vl = __riscv_vsetvlmax_e32m1(); 267 vfloat32m1_t acc1_1 = __riscv_vfadd_vv_f32m1(__riscv_vget_v_f32m2_f32m1(acc1_0, 0), 268 __riscv_vget_v_f32m2_f32m1(acc1_0, 1), vl); 269 vfloat32m1_t redsum1 = __riscv_vfredusum_vs_f32m1_f32m1( 270 acc1_1, __riscv_vfmv_v_f_f32m1(0.0f, 1), vl); 271 sumf[0] = __riscv_vfmv_f_s_f32m1_f32(redsum0); 272 sumf[1] = __riscv_vfmv_f_s_f32m1_f32(redsum1); 273 np = n; 274 #else 275 const int np = 0; 276 #endif 277 #else 278 const int np = (n & ~(GGML_F16_DOT_STEP - 1)); 279 280 GGML_F16_DOT_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_DOT_ARR] = { { GGML_F16_DOT_VEC_ZERO } }; 281 282 GGML_F16_DOT_VEC ax[GGML_F16_DOT_ARR]; 283 GGML_F16_DOT_VEC ay[GGML_F16_DOT_ARR]; 284 285 for (int i = 0; i < np; i += GGML_F16_DOT_STEP) { 286 for (int j = 0; j < GGML_F16_DOT_ARR; j++) { 287 ay[j] = GGML_F16_DOT_VEC_LOAD(y + i + j*GGML_F16_DOT_EPR, j); 288 289 for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) { 290 ax[j] = GGML_F16_DOT_VEC_LOAD(x[k] + i + j*GGML_F16_DOT_EPR, j); 291 292 sum[k][j] = GGML_F16_DOT_VEC_FMA(sum[k][j], ax[j], ay[j]); 293 } 294 } 295 } 296 297 // reduce sum0..sum3 to sum0 298 for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) { 299 GGML_F16_DOT_VEC_REDUCE(sumf[k], sum[k]); 300 } 301 #endif 302 #else 303 // scalar path 304 const int np = 0; 305 #endif 306 // scalar and leftovers 307 for (int i = np; i < n; ++i) { 308 for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) { 309 sumf[j] += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[j][i])*GGML_CPU_FP16_TO_FP32(y[i])); 310 } 311 } 312 313 for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) { 314 s[i] = (float)sumf[i]; 315 } 316 } 317 318 inline static void ggml_vec_mad_f32(const int n, float * GGML_RESTRICT y, const float * GGML_RESTRICT x, const float v) { 319 #if defined(GGML_SIMD) 320 #if defined(__ARM_FEATURE_SVE) 321 322 const int sve_register_length = ggml_cpu_get_sve_cnt() * 8; 323 const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16 324 const int ggml_f32_step = 8 * ggml_f32_epr; // choose 8 SVE registers 325 GGML_F32_VEC vx = GGML_F32_VEC_SET1(v); 326 327 const int np = (n & ~(ggml_f32_step - 1)); 328 svfloat32_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8; 329 svfloat32_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8; 330 for (int i = 0; i < np; i += ggml_f32_step) { 331 332 ax1 = GGML_F32_VEC_LOAD(x + i); 333 ay1 = GGML_F32_VEC_LOAD(y + i); 334 ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx); 335 336 GGML_F32_VEC_STORE(y + i, ay1); 337 338 ax2 = GGML_F32_VEC_LOAD(x + i + 1*ggml_f32_epr); 339 ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr); 340 ay2 = GGML_F32_VEC_FMA(ay2, ax2, vx); 341 342 GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2); 343 344 ax3 = GGML_F32_VEC_LOAD(x + i + 2*ggml_f32_epr); 345 ay3 = GGML_F32_VEC_LOAD(y + i + 2*ggml_f32_epr); 346 ay3 = GGML_F32_VEC_FMA(ay3, ax3, vx); 347 348 GGML_F32_VEC_STORE(y + i + 2*ggml_f32_epr, ay3); 349 350 ax4 = GGML_F32_VEC_LOAD(x + i + 3*ggml_f32_epr); 351 ay4 = GGML_F32_VEC_LOAD(y + i + 3*ggml_f32_epr); 352 ay4 = GGML_F32_VEC_FMA(ay4, ax4, vx); 353 354 GGML_F32_VEC_STORE(y + i + 3*ggml_f32_epr, ay4); 355 356 ax5 = GGML_F32_VEC_LOAD(x + i + 4*ggml_f32_epr); 357 ay5 = GGML_F32_VEC_LOAD(y + i + 4*ggml_f32_epr); 358 ay5 = GGML_F32_VEC_FMA(ay5, ax5, vx); 359 360 GGML_F32_VEC_STORE(y + i + 4*ggml_f32_epr, ay5); 361 362 ax6 = GGML_F32_VEC_LOAD(x + i + 5*ggml_f32_epr); 363 ay6 = GGML_F32_VEC_LOAD(y + i + 5*ggml_f32_epr); 364 ay6 = GGML_F32_VEC_FMA(ay6, ax6, vx); 365 366 GGML_F32_VEC_STORE(y + i + 5*ggml_f32_epr, ay6); 367 368 ax7 = GGML_F32_VEC_LOAD(x + i + 6*ggml_f32_epr); 369 ay7 = GGML_F32_VEC_LOAD(y + i + 6*ggml_f32_epr); 370 ay7 = GGML_F32_VEC_FMA(ay7, ax7, vx); 371 372 GGML_F32_VEC_STORE(y + i + 6*ggml_f32_epr, ay7); 373 374 ax8 = GGML_F32_VEC_LOAD(x + i + 7*ggml_f32_epr); 375 ay8 = GGML_F32_VEC_LOAD(y + i + 7*ggml_f32_epr); 376 ay8 = GGML_F32_VEC_FMA(ay8, ax8, vx); 377 378 GGML_F32_VEC_STORE(y + i + 7*ggml_f32_epr, ay8); 379 } 380 // leftovers 381 // Since 8 unrolls are done in above loop, leftovers lie in range [0, ggml_f32_step] which is handled in below loop 382 const int np2 = (n & ~(ggml_f32_epr - 1)); 383 for (int i = np; i < np2; i += ggml_f32_epr) { 384 ax1 = GGML_F32_VEC_LOAD(x + i); 385 ay1 = GGML_F32_VEC_LOAD(y + i); 386 ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx); 387 388 GGML_F32_VEC_STORE(y + i, ay1); 389 } 390 // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only 391 if (np2 < n) { 392 svbool_t pg =svwhilelt_b32(np2, n); 393 ax1 = svld1_f32(pg, x + np2); 394 ay1 = svld1_f32(pg, y + np2); 395 ay1 = svmad_f32_m(pg, ax1, vx, ay1); 396 397 svst1_f32(pg, y + np2, ay1); 398 } 399 #elif defined(__riscv_v) 400 for (int i = 0, avl; i < n; i += avl) { 401 avl = __riscv_vsetvl_e32m8(n - i); 402 vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[i], avl); 403 vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl); 404 vfloat32m8_t ny = __riscv_vfmadd_vf_f32m8(ax, v, ay, avl); 405 __riscv_vse32_v_f32m8(&y[i], ny, avl); 406 } 407 #else 408 const int np = (n & ~(GGML_F32_STEP - 1)); 409 410 GGML_F32_VEC vx = GGML_F32_VEC_SET1(v); 411 412 GGML_F32_VEC ax[GGML_F32_ARR]; 413 GGML_F32_VEC ay[GGML_F32_ARR]; 414 415 for (int i = 0; i < np; i += GGML_F32_STEP) { 416 for (int j = 0; j < GGML_F32_ARR; j++) { 417 ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR); 418 ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR); 419 ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx); 420 421 GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]); 422 } 423 } 424 425 // leftovers 426 for (int i = np; i < n; ++i) { 427 y[i] += x[i]*v; 428 } 429 #endif 430 #else 431 // scalar 432 for (int i = 0; i < n; ++i) { 433 y[i] += x[i]*v; 434 } 435 #endif 436 } 437 438 inline static void ggml_vec_mad_f16(const int n, ggml_fp16_t * GGML_RESTRICT y, const ggml_fp16_t * GGML_RESTRICT x, const float v) { 439 #if defined(GGML_SIMD) && defined(__ARM_FEATURE_SVE) 440 const int sve_register_length = svcntb() * 8; 441 const int ggml_f16_epr = sve_register_length / 16; 442 const int ggml_f16_step = 8 * ggml_f16_epr; 443 444 GGML_F16x_VEC vx = GGML_F16x_VEC_SET1(v); 445 446 int np = (n & ~(ggml_f16_step - 1)); 447 448 svfloat16_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8; 449 svfloat16_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8; 450 for (int i = 0; i < np; i += ggml_f16_step) { 451 ax1 = GGML_F16x_VEC_LOAD(x + i + 0 * ggml_f16_epr, 0); 452 ay1 = GGML_F16x_VEC_LOAD(y + i + 0 * ggml_f16_epr, 0); 453 ay1 = GGML_F16x_VEC_FMA(ay1, ax1, vx); 454 455 GGML_F16x_VEC_STORE(y + i + 0 * ggml_f16_epr, ay1, 0); 456 457 ax2 = GGML_F16x_VEC_LOAD(x + i + 1 * ggml_f16_epr, 1); 458 ay2 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 1); 459 ay2 = GGML_F16x_VEC_FMA(ay2, ax2, vx); 460 461 GGML_F16x_VEC_STORE(y + i + 1 * ggml_f16_epr, ay2, 1); 462 463 ax3 = GGML_F16x_VEC_LOAD(x + i + 2 * ggml_f16_epr, 2); 464 ay3 = GGML_F16x_VEC_LOAD(y + i + 2 * ggml_f16_epr, 2); 465 ay3 = GGML_F16x_VEC_FMA(ay3, ax3, vx); 466 467 GGML_F16x_VEC_STORE(y + i + 2 * ggml_f16_epr, ay3, 2); 468 469 ax4 = GGML_F16x_VEC_LOAD(x + i + 3 * ggml_f16_epr, 3); 470 ay4 = GGML_F16x_VEC_LOAD(y + i + 3 * ggml_f16_epr, 3); 471 ay4 = GGML_F16x_VEC_FMA(ay4, ax4, vx); 472 473 GGML_F16x_VEC_STORE(y + i + 3 * ggml_f16_epr, ay4, 3); 474 475 ax5 = GGML_F16x_VEC_LOAD(x + i + 4 * ggml_f16_epr, 4); 476 ay5 = GGML_F16x_VEC_LOAD(y + i + 4 * ggml_f16_epr, 4); 477 ay5 = GGML_F16x_VEC_FMA(ay5, ax5, vx); 478 479 GGML_F16x_VEC_STORE(y + i + 4 * ggml_f16_epr, ay5, 4); 480 481 ax6 = GGML_F16x_VEC_LOAD(x + i + 5 * ggml_f16_epr, 5); 482 ay6 = GGML_F16x_VEC_LOAD(y + i + 5 * ggml_f16_epr, 5); 483 ay6 = GGML_F16x_VEC_FMA(ay6, ax6, vx); 484 485 GGML_F16x_VEC_STORE(y + i + 5 * ggml_f16_epr, ay6, 5); 486 487 ax7 = GGML_F16x_VEC_LOAD(x + i + 6 * ggml_f16_epr, 6); 488 ay7 = GGML_F16x_VEC_LOAD(y + i + 6 * ggml_f16_epr, 6); 489 ay7 = GGML_F16x_VEC_FMA(ay7, ax7, vx); 490 491 GGML_F16x_VEC_STORE(y + i + 6 * ggml_f16_epr, ay7, 6); 492 493 ax8 = GGML_F16x_VEC_LOAD(x + i + 7 * ggml_f16_epr, 7); 494 ay8 = GGML_F16x_VEC_LOAD(y + i + 7 * ggml_f16_epr, 7); 495 ay8 = GGML_F16x_VEC_FMA(ay8, ax8, vx); 496 497 GGML_F16x_VEC_STORE(y + i + 7 * ggml_f16_epr, ay8, 7); 498 } 499 const int np2 = (n & ~(ggml_f16_epr - 1)); 500 for (int k = np; k < np2; k += ggml_f16_epr) { 501 svfloat16_t rx = GGML_F16x_VEC_LOAD(x + k, 0); 502 svfloat16_t ry = GGML_F16x_VEC_LOAD(y + k, 0); 503 ry = GGML_F16x_VEC_FMA(ry, rx, vx); 504 505 GGML_F16x_VEC_STORE(y + k, ry, 0); 506 } 507 508 if (np2 < n) { 509 svbool_t pg = svwhilelt_b16(np2, n); 510 svfloat16_t hx = svld1_f16(pg, (const __fp16 *)(x + np2)); 511 svfloat16_t hy = svld1_f16(pg, (const __fp16 *)(y + np2)); 512 hy = svmad_f16_x(pg, hx, vx, hy); 513 svst1_f16(pg, (__fp16 *)(y + np2), hy); 514 } 515 np = n; 516 #elif defined(__riscv_v) 517 #if defined (__riscv_zvfh) 518 const ggml_fp16_t s = GGML_CPU_FP32_TO_FP16(v); 519 const _Float16 scale = *(const _Float16*)(&s); 520 521 // calculate step size 522 const int epr = __riscv_vsetvlmax_e16m4(); 523 const int step = epr * 2; 524 int np = (n & ~(step - 1)); 525 526 // unroll by 2 527 for (int i = 0; i < np; i += step) { 528 vfloat16m4_t ax0 = __riscv_vle16_v_f16m4((const _Float16*)x + i, epr); 529 vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, epr); 530 ay0 = __riscv_vfmacc_vf_f16m4(ay0, scale, ax0, epr); 531 __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, epr); 532 __asm__ __volatile__ ("" ::: "memory"); 533 534 vfloat16m4_t ax1 = __riscv_vle16_v_f16m4((const _Float16*)x + i + epr, epr); 535 vfloat16m4_t ay1 = __riscv_vle16_v_f16m4((const _Float16*)y + i + epr, epr); 536 ay1 = __riscv_vfmacc_vf_f16m4(ay1, scale, ax1, epr); 537 __riscv_vse16_v_f16m4((_Float16*)y + i + epr, ay1, epr); 538 __asm__ __volatile__ ("" ::: "memory"); 539 } 540 541 // leftovers 542 int vl; 543 for (int i = np; i < n; i += vl) { 544 vl = __riscv_vsetvl_e16m4(n - i); 545 vfloat16m4_t ax0 = __riscv_vle16_v_f16m4((const _Float16*)x + i, vl); 546 vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, vl); 547 ay0 = __riscv_vfmacc_vf_f16m4(ay0, scale, ax0, vl); 548 __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, vl); 549 } 550 np = n; 551 #else 552 // fall to scalar path 553 const int np = 0; 554 #endif 555 #elif defined(GGML_SIMD) 556 const int np = (n & ~(GGML_F16_STEP - 1)); 557 558 GGML_F16_VEC vx = GGML_F16_VEC_SET1(v); 559 560 GGML_F16_VEC ax[GGML_F16_ARR]; 561 GGML_F16_VEC ay[GGML_F16_ARR]; 562 563 for (int i = 0; i < np; i += GGML_F16_STEP) { 564 for (int j = 0; j < GGML_F16_ARR; j++) { 565 ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j); 566 ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j); 567 ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx); 568 569 GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j); 570 } 571 } 572 #else 573 // scalar path 574 const int np = 0; 575 #endif 576 577 // scalar and leftovers 578 for (int i = np; i < n; ++i) { 579 y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v); 580 } 581 } 582 583 // xs and vs are byte strides of x and v 584 inline static void ggml_vec_mad_f32_unroll(const int n, const int xs, const int vs, float * GGML_RESTRICT y, const float * GGML_RESTRICT xv, const float * GGML_RESTRICT vv) { 585 586 const float * GGML_RESTRICT x[GGML_VEC_MAD_UNROLL]; 587 const float * GGML_RESTRICT v[GGML_VEC_MAD_UNROLL]; 588 589 for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) { 590 x[i] = (const float *) ((const char *) xv + i*xs); 591 v[i] = (const float *) ((const char *) vv + i*vs); 592 } 593 594 #if defined(GGML_SIMD) 595 #if defined(__ARM_FEATURE_SVE) 596 // scalar Route to scalar implementation //TODO: Write SVE code 597 for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { 598 for (int i = 0; i < n; ++i) { 599 y[i] += x[k][i]*v[k][0]; 600 } 601 } 602 #elif defined(__riscv_v) 603 for (int i = 0, avl; i < n; i += avl) { 604 avl = __riscv_vsetvl_e32m8(n - i); 605 vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl); 606 for (int k = 0; k < GGML_VEC_MAD_UNROLL; k++) { 607 vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[k][i], avl); 608 ay = __riscv_vfmadd_vf_f32m8(ax, v[k][0], ay, avl); 609 } 610 __riscv_vse32_v_f32m8(&y[i], ay, avl); 611 } 612 #else 613 const int np = (n & ~(GGML_F32_STEP - 1)); 614 615 GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL]; 616 617 for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { 618 vx[k] = GGML_F32_VEC_SET1(v[k][0]); 619 } 620 621 GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR]; 622 GGML_F32_VEC ay[GGML_F32_ARR]; 623 624 for (int i = 0; i < np; i += GGML_F32_STEP) { 625 for (int j = 0; j < GGML_F32_ARR; j++) { 626 ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR); 627 628 for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { 629 ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR); 630 ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]); 631 } 632 633 GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]); 634 } 635 } 636 637 // leftovers 638 for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { 639 for (int i = np; i < n; ++i) { 640 y[i] += x[k][i]*v[k][0]; 641 } 642 } 643 #endif 644 #else 645 // scalar 646 for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { 647 for (int i = 0; i < n; ++i) { 648 y[i] += x[k][i]*v[k][0]; 649 } 650 } 651 #endif 652 } 653 654 inline static void ggml_vec_mad1_f32(const int n, float * y, const float * x, const float s, const float b) { 655 #if defined(GGML_USE_ACCELERATE) 656 vDSP_vsmsa(x, 1, &s, &b, y, 1, n); 657 #elif defined(GGML_SIMD) 658 #if defined(__ARM_FEATURE_SVE) 659 // scalar ; TODO: Write SVE code 660 for (int i = 0; i < n; ++i) { 661 y[i] = x[i]*s + b; 662 } 663 #elif defined(__riscv_v) 664 for (int i = 0, avl; i < n; i += avl) { 665 avl = __riscv_vsetvl_e32m8(n - i); 666 vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[i], avl); 667 vfloat32m8_t vb = __riscv_vfmv_v_f_f32m8(b, avl); 668 vfloat32m8_t ny = __riscv_vfmadd_vf_f32m8(ax, s, vb, avl); 669 __riscv_vse32_v_f32m8(&y[i], ny, avl); 670 } 671 #else 672 const int np = (n & ~(GGML_F32_STEP - 1)); 673 674 GGML_F32_VEC vs = GGML_F32_VEC_SET1(s); 675 GGML_F32_VEC vb = GGML_F32_VEC_SET1(b); 676 677 GGML_F32_VEC ay[GGML_F32_ARR]; 678 679 for (int i = 0; i < np; i += GGML_F32_STEP) { 680 for (int j = 0; j < GGML_F32_ARR; j++) { 681 ay[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR); 682 ay[j] = GGML_F32_VEC_FMA(vb, ay[j], vs); 683 684 GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]); 685 } 686 } 687 688 // leftovers 689 for (int i = np; i < n; ++i) { 690 y[i] = x[i]*s + b; 691 } 692 #endif 693 #else 694 // scalar 695 for (int i = 0; i < n; ++i) { 696 y[i] = x[i]*s + b; 697 } 698 #endif 699 } 700 701 //inline static void ggml_vec_scale_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] *= v; } 702 inline static void ggml_vec_scale_f32(const int n, float * y, const float v) { 703 #if defined(GGML_USE_ACCELERATE) 704 vDSP_vsmul(y, 1, &v, y, 1, n); 705 #elif defined(GGML_SIMD) 706 #if defined(__ARM_FEATURE_SVE) 707 const int sve_register_length = ggml_cpu_get_sve_cnt() * 8; 708 const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16 709 const int ggml_f32_step = 2 * ggml_f32_epr; 710 711 GGML_F32_VEC vx = GGML_F32_VEC_SET1(v); 712 const int np = (n & ~(ggml_f32_step - 1)); 713 svfloat32_t ay1; 714 svfloat32_t ay2; 715 for (int i = 0; i < np; i += ggml_f32_step) { 716 ay1 = GGML_F32_VEC_LOAD(y + i); 717 ay1 = GGML_F32_VEC_MUL(ay1, vx); 718 GGML_F32_VEC_STORE(y + i, ay1); 719 720 ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr); 721 ay2 = GGML_F32_VEC_MUL(ay2, vx); 722 GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2); 723 } 724 // leftovers 725 // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only 726 for (int i = np; i < n; i += ggml_f32_epr) { 727 svbool_t pg = svwhilelt_b32(i, n); 728 ay1 = svld1_f32(pg, y + i); 729 ay1 = svmul_f32_m(pg, ay1, vx); 730 svst1_f32(pg, y + i, ay1); 731 } 732 #elif defined(__riscv_v) 733 for (int i = 0, avl; i < n; i += avl) { 734 avl = __riscv_vsetvl_e32m8(n - i); 735 vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl); 736 vfloat32m8_t ny = __riscv_vfmul_vf_f32m8(ay, v, avl); 737 __riscv_vse32_v_f32m8(&y[i], ny, avl); 738 } 739 #else 740 const int np = (n & ~(GGML_F32_STEP - 1)); 741 742 GGML_F32_VEC vx = GGML_F32_VEC_SET1(v); 743 744 GGML_F32_VEC ay[GGML_F32_ARR]; 745 746 for (int i = 0; i < np; i += GGML_F32_STEP) { 747 for (int j = 0; j < GGML_F32_ARR; j++) { 748 ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR); 749 ay[j] = GGML_F32_VEC_MUL(ay[j], vx); 750 751 GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]); 752 } 753 } 754 755 // leftovers 756 for (int i = np; i < n; ++i) { 757 y[i] *= v; 758 } 759 #endif 760 #else 761 // scalar 762 for (int i = 0; i < n; ++i) { 763 y[i] *= v; 764 } 765 #endif 766 } 767 768 inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) { 769 #if defined(GGML_SIMD) && defined(__ARM_FEATURE_SVE) 770 const int sve_register_length = svcntb() * 8; 771 const int ggml_f16_epr = sve_register_length / 16; 772 const int ggml_f16_step = 2 * ggml_f16_epr; 773 774 GGML_F16x_VEC vx = GGML_F16x_VEC_SET1(v); 775 int np = (n & ~(ggml_f16_step - 1)); 776 svfloat16_t ay1, ay2; 777 778 for (int i = 0; i < np; i += ggml_f16_step) { 779 ay1 = GGML_F16x_VEC_LOAD(y + i + 0*ggml_f16_epr, 0); 780 ay1 = GGML_F16x_VEC_MUL(ay1, vx); 781 GGML_F16x_VEC_STORE(y + i + 0*ggml_f16_epr, ay1, 0); 782 783 ay2 = GGML_F16x_VEC_LOAD(y + i + 1*ggml_f16_epr, 1); 784 ay2 = GGML_F16x_VEC_MUL(ay2, vx); 785 GGML_F16x_VEC_STORE(y + i + 1*ggml_f16_epr, ay2, 1); 786 } 787 // leftovers 788 // maximum number of leftover elements will be less that ggmlF_16x_epr. Apply predicated svmad on available elements only 789 if (np < n) { 790 svbool_t pg = svwhilelt_b16(np, n); 791 svfloat16_t hy = svld1_f16(pg, (__fp16 *)(y + np)); 792 svfloat16_t out = svmul_f16_m(pg, hy, vx); 793 svst1_f16(pg, (__fp16 *)(y + np), out); 794 } 795 np = n; 796 #elif defined(__riscv_v) 797 #if defined(__riscv_zvfh) 798 const ggml_fp16_t s = GGML_CPU_FP32_TO_FP16(v); 799 const _Float16 scale = *(const _Float16*)(&s); 800 801 // calculate step size 802 const int epr = __riscv_vsetvlmax_e16m4(); 803 const int step = epr * 2; 804 int np = (n & ~(step - 1)); 805 806 // unroll by 2 807 for (int i = 0; i < np; i += step) { 808 vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, epr); 809 ay0 = __riscv_vfmul_vf_f16m4(ay0, scale, epr); 810 __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, epr); 811 __asm__ __volatile__ ("" ::: "memory"); 812 813 vfloat16m4_t ay1 = __riscv_vle16_v_f16m4((const _Float16*)y + i + epr, epr); 814 ay1 = __riscv_vfmul_vf_f16m4(ay1, scale, epr); 815 __riscv_vse16_v_f16m4((_Float16*)y + i + epr, ay1, epr); 816 __asm__ __volatile__ ("" ::: "memory"); 817 } 818 819 // leftovers 820 int vl; 821 for (int i = np; i < n; i += vl) { 822 vl = __riscv_vsetvl_e16m4(n - i); 823 vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, vl); 824 ay0 = __riscv_vfmul_vf_f16m4(ay0, scale, vl); 825 __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, vl); 826 } 827 np = n; 828 #else 829 // fall to scalar path 830 const int np = 0; 831 #endif 832 #elif defined(GGML_SIMD) 833 const int np = (n & ~(GGML_F16_STEP - 1)); 834 835 GGML_F16_VEC vx = GGML_F16_VEC_SET1(v); 836 837 GGML_F16_VEC ay[GGML_F16_ARR]; 838 839 for (int i = 0; i < np; i += GGML_F16_STEP) { 840 for (int j = 0; j < GGML_F16_ARR; j++) { 841 ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j); 842 ay[j] = GGML_F16_VEC_MUL(ay[j], vx); 843 844 GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j); 845 } 846 } 847 #else 848 // scalar path 849 const int np = 0; 850 #endif 851 // scalar and leftovers 852 for (int i = np; i < n; ++i) { 853 y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v); 854 } 855 } 856 857 inline static void ggml_vec_norm_f32 (const int n, float * s, const float * x) { ggml_vec_dot_f32(n, s, 0, x, 0, x, 0, 1); *s = sqrtf(*s); } 858 inline static void ggml_vec_sqr_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]*x[i]; } 859 inline static void ggml_vec_sqr_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 860 for (int i = 0; i < n; ++i) { 861 float v = GGML_CPU_FP16_TO_FP32(x[i]); 862 y[i] = GGML_CPU_FP32_TO_FP16(v*v); 863 } 864 } 865 inline static void ggml_vec_sqrt_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sqrtf(x[i]); } 866 inline static void ggml_vec_sqrt_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 867 for (int i = 0; i < n; ++i) { 868 y[i] = GGML_CPU_FP32_TO_FP16(sqrtf(GGML_CPU_FP16_TO_FP32(x[i]))); 869 } 870 } 871 inline static void ggml_vec_log_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = logf(x[i]); } 872 inline static void ggml_vec_log_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 873 for (int i = 0; i < n; ++i) { 874 y[i] = GGML_CPU_FP32_TO_FP16(logf(GGML_CPU_FP16_TO_FP32(x[i]))); 875 } 876 } 877 inline static void ggml_vec_sin_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sinf(x[i]); } 878 inline static void ggml_vec_sin_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 879 for (int i = 0; i < n; ++i) { 880 y[i] = GGML_CPU_FP32_TO_FP16(sinf(GGML_CPU_FP16_TO_FP32(x[i]))); 881 } 882 } 883 inline static void ggml_vec_cos_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = cosf(x[i]); } 884 inline static void ggml_vec_cos_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 885 for (int i = 0; i < n; ++i) { 886 y[i] = GGML_CPU_FP32_TO_FP16(cosf(GGML_CPU_FP16_TO_FP32(x[i]))); 887 } 888 } 889 inline static void ggml_vec_abs_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fabsf(x[i]); } 890 inline static void ggml_vec_abs_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 891 for (int i = 0; i < n; ++i) { 892 y[i] = GGML_CPU_FP32_TO_FP16(fabsf(GGML_CPU_FP16_TO_FP32(x[i]))); 893 } 894 } 895 inline static void ggml_vec_sgn_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : ((x[i] < 0.f) ? -1.f : 0.f); } 896 inline static void ggml_vec_sgn_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 897 for (int i = 0; i < n; ++i) { 898 float v = GGML_CPU_FP16_TO_FP32(x[i]); 899 y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? 1.f : ((v < 0.f) ? -1.f : 0.f)); 900 } 901 } 902 inline static void ggml_vec_step_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : 0.f; } 903 inline static void ggml_vec_step_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 904 for (int i = 0; i < n; ++i) { 905 y[i] = GGML_CPU_FP32_TO_FP16((GGML_CPU_FP16_TO_FP32(x[i]) > 0.f) ? 1.f : 0.f); 906 } 907 } 908 inline static void ggml_vec_tanh_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = tanhf(x[i]); } 909 inline static void ggml_vec_tanh_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 910 for (int i = 0; i < n; ++i) { 911 y[i] = GGML_CPU_FP32_TO_FP16(tanhf(GGML_CPU_FP16_TO_FP32(x[i]))); 912 } 913 } 914 inline static void ggml_vec_elu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : expm1f(x[i]); } 915 inline static void ggml_vec_elu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 916 for (int i = 0; i < n; ++i) { 917 const float v = GGML_CPU_FP16_TO_FP32(x[i]); 918 y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v : expm1f(v)); 919 } 920 } 921 inline static void ggml_vec_relu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : 0.f; } 922 inline static void ggml_vec_relu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 923 for (int i = 0; i < n; ++i) { 924 float v = GGML_CPU_FP16_TO_FP32(x[i]); 925 y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v : 0.f); 926 } 927 } 928 inline static void ggml_vec_leaky_relu_f32 (const int n, float * y, const float * x, const float ns) { for (int i = 0; i < n; ++i) y[i] = ((x[i] > 0.f) ? x[i] : 0.f) + ns * ((x[i] < 0.0f) ? x[i] : 0.f); } 929 inline static void ggml_vec_leaky_relu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const float ns) { 930 for (int i = 0; i < n; ++i) { 931 float v = GGML_CPU_FP16_TO_FP32(x[i]); 932 y[i] = GGML_CPU_FP32_TO_FP16(((v > 0.f) ? v : 0.f) + ns * ((v < 0.0f) ? v : 0.f)); 933 } 934 } 935 inline static void ggml_vec_sigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = 1.f / (1.f + expf(-x[i])); } 936 inline static void ggml_vec_sigmoid_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 937 for (int i = 0; i < n; ++i) { 938 y[i] = GGML_CPU_FP32_TO_FP16(1.f / (1.f + expf(-GGML_CPU_FP16_TO_FP32(x[i])))); 939 } 940 } 941 // TODO: optimize performance 942 inline static void ggml_vec_hardswish_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); } 943 inline static void ggml_vec_hardswish_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 944 for (int i = 0; i < n; ++i) { 945 float v = GGML_CPU_FP16_TO_FP32(x[i]); 946 y[i] = GGML_CPU_FP32_TO_FP16(v * fminf(1.0f, fmaxf(0.0f, (v + 3.0f) / 6.0f))); 947 } 948 } 949 inline static void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); } 950 inline static void ggml_vec_hardsigmoid_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 951 for (int i = 0; i < n; ++i) { 952 y[i] = GGML_CPU_FP32_TO_FP16(fminf(1.0f, fmaxf(0.0f, (GGML_CPU_FP16_TO_FP32(x[i]) + 3.0f) / 6.0f))); 953 } 954 } 955 inline static void ggml_vec_exp_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = expf(x[i]); } 956 inline static void ggml_vec_exp_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 957 for (int i = 0; i < n; ++i) { 958 y[i] = GGML_CPU_FP32_TO_FP16(expf(GGML_CPU_FP16_TO_FP32(x[i]))); 959 } 960 } 961 962 static const float GELU_COEF_A = 0.`044715f`; 963 static const float GELU_QUICK_COEF = -1.702f; 964 static const float SQRT_2_OVER_PI = 0.`79788456080286535587989211986876f`; 965 static const float SQRT_2_INV = 0.`70710678118654752440084436210484f`; 966 967 inline static float ggml_gelu_f32(float x) { 968 return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x))); 969 } 970 971 inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 972 const uint16_t * i16 = (const uint16_t *) x; 973 for (int i = 0; i < n; ++i) { 974 y[i] = ggml_table_gelu_f16[i16[i]]; 975 } 976 } 977 978 inline static void ggml_vec_gelu_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x) { 979 for (int i = 0; i < n; ++i) { 980 y[i] = GGML_FP32_TO_BF16(ggml_gelu_f32(GGML_BF16_TO_FP32(x[i]))); 981 } 982 } 983 984 inline static void ggml_vec_gelu_erf_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 985 for (int i = 0; i < n; ++i) { 986 float xi = GGML_CPU_FP16_TO_FP32(x[i]); 987 float res = 0.5f*xi*(1.0f + erff(xi*SQRT_2_INV)); 988 y[i] = GGML_CPU_FP32_TO_FP16(res); 989 } 990 } 991 992 inline static void ggml_vec_gelu_erf_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x) { 993 for (int i = 0; i < n; ++i) { 994 float xi = GGML_BF16_TO_FP32(x[i]); 995 y[i] = GGML_FP32_TO_BF16(0.5f*xi*(1.0f + erff(xi*SQRT_2_INV))); 996 } 997 } 998 999 #ifdef GGML_GELU_FP16 1000 inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) { 1001 uint16_t t; 1002 for (int i = 0; i < n; ++i) { 1003 if (x[i] <= -10.0f) { 1004 y[i] = 0.0f; 1005 } else if (x[i] >= 10.0f) { 1006 y[i] = x[i]; 1007 } else { 1008 ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]); 1009 memcpy(&t, &fp16, sizeof(uint16_t)); 1010 y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[t]); 1011 } 1012 } 1013 } 1014 #else 1015 inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) { 1016 for (int i = 0; i < n; ++i) { 1017 y[i] = ggml_gelu_f32(x[i]); 1018 } 1019 } 1020 #endif 1021 1022 inline static void ggml_vec_gelu_erf_f32(const int n, float * y, const float * x) { 1023 for (int i = 0; i < n; ++i) { 1024 float xi = x[i]; 1025 y[i] = 0.5f*xi*(1.0f + erff(xi*SQRT_2_INV)); 1026 } 1027 } 1028 1029 inline static float ggml_gelu_quick_f32(float x) { 1030 return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x))); 1031 } 1032 1033 inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 1034 const uint16_t * i16 = (const uint16_t *) x; 1035 for (int i = 0; i < n; ++i) { 1036 y[i] = ggml_table_gelu_quick_f16[i16[i]]; 1037 } 1038 } 1039 1040 inline static void ggml_vec_gelu_quick_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x) { 1041 for (int i = 0; i < n; ++i) { 1042 y[i] = GGML_FP32_TO_BF16(ggml_gelu_quick_f32(GGML_BF16_TO_FP32(x[i]))); 1043 } 1044 } 1045 1046 #ifdef GGML_GELU_QUICK_FP16 1047 inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) { 1048 uint16_t t; 1049 for (int i = 0; i < n; ++i) { 1050 ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]); 1051 memcpy(&t, &fp16, sizeof(uint16_t)); 1052 y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]); 1053 } 1054 } 1055 #else 1056 void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x); 1057 #endif 1058 1059 // Sigmoid Linear Unit (SiLU) function 1060 inline static float ggml_silu_f32(float x) { 1061 return x/(1.0f + expf(-x)); 1062 } 1063 inline static ggml_fp16_t ggml_silu_f16(ggml_fp16_t x) { 1064 float v = GGML_CPU_FP16_TO_FP32(x); 1065 return GGML_CPU_FP32_TO_FP16(v/(1.0f + expf(-v))); 1066 } 1067 1068 #if __FINITE_MATH_ONLY__ 1069 #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix" 1070 #error "ref: https://github.com/ggml-org/llama.cpp/pull/7154#issuecomment-`2143844461`" 1071 #endif 1072 1073 /* Below function was borrowed from the GitHub repository: 1074 https://github.com/openvinotoolkit/openvino/blob/master/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp */ 1075 #if defined(__ARM_FEATURE_SVE) && defined(__aarch64__) 1076 inline static svfloat32_t exp_ps_sve(svbool_t pg, svfloat32_t src) { 1077 // Constants 1078 const svfloat32_t log2_e = svdup_n_f32(1.`4426950409f`); 1079 const svfloat32_t ln2 = svdup_n_f32(0.`6931473921f`); 1080 const svfloat32_t half_ln2_sq = svdup_n_f32(0.`2413862043f`); 1081 const svuint32_t not_mask17 = svdup_n_u32(~((1u << 17) - 1)); 1082 const svfloat32_t one = svdup_n_f32(1.0f); 1083 const svfloat32_t inactive1 = svdup_n_f32(0.0f); 1084 const svint32_t inactive2 = svdup_n_s32(0); 1085 1086 // Algorithm starts here 1087 svfloat32_t t0 = svmul_f32_m(pg, src, log2_e); // y = x * log2(e) 1088 svfloat32_t t1 = svrintm_f32_m(inactive1, pg, t0); // rount to int (float) 1089 svint32_t t2 = svcvt_s32_f32_m(inactive2, pg, t1); // n 1090 1091 t1 = svsub_f32_m(pg, t0, t1); // a = y - floor(y) 1092 t1 = svadd_f32_m(pg, t1, one); // b = a + 1 1093 1094 svuint32_t t3 = svlsr_n_u32_m(pg, svreinterpret_u32_f32(t1), 17); // v = b >> 17 (u32) 1095 svfloat32_t t4 = svexpa_f32(t3); // c = fexpa(v) 1096 t4 = svscale_f32_m(pg, t4, t2); // fexpa(v) * 2^(n) 1097 1098 // and_(t2.d, t1.d, not_mask17.d) 1099 svfloat32_t t5 = svreinterpret_f32_u32(svand_u32_m(pg, svreinterpret_u32_f32(t1), not_mask17)); 1100 t5 = svsub_f32_m(pg, t1, t5); // z 1101 t0 = svmla_f32_m(pg, ln2, t5, half_ln2_sq); // ln2 + half_ln2_sq * z 1102 t0 = svmla_f32_m(pg, one, t5, t0); // 1 + (ln2 * z) + (half_ln2_sq * z * z) 1103 t0 = svmul_f32_m(pg, t0, t4); // Final result 1104 1105 return t0; 1106 } 1107 #endif 1108 1109 #if defined(__ARM_FEATURE_SVE) && defined(__aarch64__) 1110 1111 inline static svfloat32_t ggml_v_expf(svbool_t pg, svfloat32_t x) { 1112 const svfloat32_t r = svdup_n_f32_x(pg, 0x1.8p23f); 1113 const svfloat32_t z = svmla_n_f32_x(pg, r, x, 0x1.715476p+0f); 1114 const svfloat32_t n = svsub_f32_x(pg, z, r); 1115 const svfloat32_t b = svmls_n_f32_x(pg, svmls_n_f32_x(pg, x, n, 0x1.62e4p-1f), n, 0x1.7f7d1cp-20f); 1116 const svuint32_t e = svlsl_n_u32_x(pg, svreinterpret_u32_f32(z), 23); 1117 const svfloat32_t k = svreinterpret_f32_u32(svadd_u32_x(pg, e, svreinterpret_u32_f32(svdup_n_f32_x(pg, 1)))); 1118 const svbool_t c = svacgt_n_f32(pg, n, 126); 1119 const svfloat32_t u = svmul_f32_x(pg, b, b); 1120 const svfloat32_t j = svmla_f32_x(pg, 1121 svmul_n_f32_x(pg, b, 0x1.ffffecp-1f), 1122 svmla_f32_x(pg, svmla_f32_x(pg, svdup_n_f32_x(pg, 0x1.fffdb6p-2f), svdup_n_f32_x(pg, 0x1.555e66p-3f), b), 1123 svmla_f32_x(pg, svdup_n_f32_x(pg, 0x1.573e2ep-5f), svdup_n_f32_x(pg, 0x1.0e4020p-7f), b), u), u); 1124 const svuint32_t d = svdup_n_u32_z(svcmple_n_f32(pg, n, 0.0), 0x82000000); 1125 const svfloat32_t s1 = svreinterpret_f32_u32(svadd_n_u32_x(pg, d, 0x7f000000)); 1126 const svfloat32_t s2 = svreinterpret_f32_u32(svsub_u32_x(pg, e, d)); 1127 return svsel_f32(svacgt_f32(pg, n, svdup_n_f32_x(pg, 192)), svmul_f32_x(pg, s1, s1), 1128 svsel_f32(c, svmul_f32_x(pg, svmla_f32_x(pg, s2, s2, j), s1), svmla_f32_x(pg, k, k, j))); 1129 } 1130 1131 // computes silu x/(1+exp(-x)) in single precision vector 1132 inline static svfloat32_t ggml_v_silu(svbool_t pg, svfloat32_t x) { 1133 const svfloat32_t one = svdup_n_f32_x(pg, 1.0f); 1134 const svfloat32_t zero = svdup_n_f32_x(pg, 0.0f); 1135 const svfloat32_t neg_x = svsub_f32_x(pg, zero, x); 1136 const svfloat32_t exp_neg_x = ggml_v_expf(pg, neg_x); 1137 const svfloat32_t one_plus_exp_neg_x = svadd_f32_x(pg, one, exp_neg_x); 1138 return svdiv_f32_x(pg, x, one_plus_exp_neg_x); 1139 } 1140 1141 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1142 inline static svfloat32_t ggml_v_gelu_quick(svbool_t pg, svfloat32_t x) { 1143 const svfloat32_t one = svdup_n_f32_x(pg, 1.0f); 1144 const svfloat32_t exp_cx = ggml_v_expf(pg, svmul_n_f32_x(pg, x, GELU_QUICK_COEF)); 1145 return svdiv_f32_x(pg, x, svadd_f32_x(pg, one, exp_cx)); 1146 } 1147 1148 #elif defined(__ARM_NEON) && defined(__aarch64__) 1149 1150 // adapted from arm limited optimized routine 1151 // the maximum error is 1.45358 plus 0.5 ulps 1152 // numbers above 88.38 will flush to infinity 1153 // numbers beneath -103.97 will flush to zero 1154 inline static float32x4_t ggml_v_expf(float32x4_t x) { 1155 const float32x4_t r = vdupq_n_f32(0x1.8p23f); 1156 const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f)); 1157 const float32x4_t n = vsubq_f32(z, r); 1158 const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n, 1159 vdupq_n_f32(0x1.7f7d1cp-20f)); 1160 const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23); 1161 const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1)))); 1162 const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126)); 1163 const float32x4_t u = vmulq_f32(b, b); 1164 const float32x4_t j = vfmaq_f32( 1165 vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b), 1166 vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b), 1167 vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u); 1168 if (!vpaddd_u64(vreinterpretq_u64_u32(c))) 1169 return vfmaq_f32(k, j, k); 1170 const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000)); 1171 const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000))); 1172 const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d)); 1173 return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1), 1174 vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j))); 1175 } 1176 1177 // computes silu x/(1+exp(-x)) in single precision vector 1178 inline static float32x4_t ggml_v_silu(float32x4_t x) { 1179 const float32x4_t one = vdupq_n_f32(1.0f); 1180 const float32x4_t zero = vdupq_n_f32(0.0f); 1181 const float32x4_t neg_x = vsubq_f32(zero, x); 1182 const float32x4_t exp_neg_x = ggml_v_expf(neg_x); 1183 const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x); 1184 return vdivq_f32(x, one_plus_exp_neg_x); 1185 } 1186 1187 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1188 inline static float32x4_t ggml_v_gelu_quick(float32x4_t x) { 1189 const float32x4_t one = vdupq_n_f32(1.0f); 1190 const float32x4_t exp_cx = ggml_v_expf(vmulq_n_f32(x, GELU_QUICK_COEF)); 1191 return vdivq_f32(x, vaddq_f32(one, exp_cx)); 1192 } 1193 1194 #elif defined(__AVX512F__) && defined(__AVX512DQ__) 1195 1196 // adapted from arm limited optimized routine 1197 // the maximum error is 1.45358 plus 0.5 ulps 1198 // numbers above 88.38 will flush to infinity 1199 // numbers beneath -103.97 will flush to zero 1200 inline static __m512 ggml_v_expf(__m512 x) { 1201 const __m512 r = _mm512_set1_ps(0x1.8p23f); 1202 const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r); 1203 const __m512 n = _mm512_sub_ps(z, r); 1204 const __m512 b = 1205 _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f), 1206 _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x)); 1207 const __mmask16 d = 1208 _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ); 1209 const __m512 u = _mm512_mul_ps(b, b); 1210 const __m512 j = _mm512_fmadd_ps( 1211 _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b, 1212 _mm512_set1_ps(0x1.573e2ep-5f)), 1213 u, 1214 _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b, 1215 _mm512_set1_ps(0x1.fffdb6p-2f))), 1216 u, 1217 _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F))); 1218 const __m512 res = _mm512_scalef_ps(j, n); 1219 if (_mm512_kortestz(d, d)) 1220 return res; 1221 const __m512 zero = _mm512_setzero_ps(); 1222 const __m512 alt = _mm512_mask_blend_ps( 1223 _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero); 1224 return _mm512_mask_blend_ps(d, res, alt); 1225 } 1226 1227 // computes silu x/(1+exp(-x)) in single precision vector 1228 inline static __m512 ggml_v_silu(__m512 x) { 1229 const __m512 one = _mm512_set1_ps(1); 1230 const __m512 zero = _mm512_setzero_ps(); 1231 const __m512 neg_x = _mm512_sub_ps(zero, x); 1232 const __m512 exp_neg_x = ggml_v_expf(neg_x); 1233 const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x); 1234 return _mm512_div_ps(x, one_plus_exp_neg_x); 1235 } 1236 1237 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1238 inline static __m512 ggml_v_gelu_quick(__m512 x) { 1239 const __m512 one = _mm512_set1_ps(1); 1240 const __m512 exp_cx = ggml_v_expf(_mm512_mul_ps(x, _mm512_set1_ps(GELU_QUICK_COEF))); 1241 return _mm512_div_ps(x, _mm512_add_ps(one, exp_cx)); 1242 } 1243 1244 #elif defined(__AVX2__) && defined(__FMA__) 1245 1246 // adapted from arm limited optimized routine 1247 // the maximum error is 1.45358 plus 0.5 ulps 1248 // numbers above 88.38 will flush to infinity 1249 // numbers beneath -103.97 will flush to zero 1250 inline static __m256 ggml_v_expf(__m256 x) { 1251 const __m256 r = _mm256_set1_ps(0x1.8p23f); 1252 const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r); 1253 const __m256 n = _mm256_sub_ps(z, r); 1254 const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f), 1255 _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x)); 1256 const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23); 1257 const __m256 k = _mm256_castsi256_ps( 1258 _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1)))); 1259 const __m256i c = _mm256_castps_si256( 1260 _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n), 1261 _mm256_set1_ps(126), _CMP_GT_OQ)); 1262 const __m256 u = _mm256_mul_ps(b, b); 1263 const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b, 1264 _mm256_set1_ps(0x1.573e2ep-5f)), u, 1265 _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b, 1266 _mm256_set1_ps(0x1.fffdb6p-2f))), 1267 u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b)); 1268 if (!_mm256_movemask_ps(_mm256_castsi256_ps(c))) 1269 return _mm256_fmadd_ps(j, k, k); 1270 const __m256i g = _mm256_and_si256( 1271 _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)), 1272 _mm256_set1_epi32(0x82000000u)); 1273 const __m256 s1 = 1274 _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u))); 1275 const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g)); 1276 const __m256i d = _mm256_castps_si256( 1277 _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n), 1278 _mm256_set1_ps(192), _CMP_GT_OQ)); 1279 return _mm256_or_ps( 1280 _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)), 1281 _mm256_andnot_ps( 1282 _mm256_castsi256_ps(d), 1283 _mm256_or_ps( 1284 _mm256_and_ps(_mm256_castsi256_ps(c), 1285 _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)), 1286 _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k))))); 1287 } 1288 1289 // computes silu x/(1+exp(-x)) in single precision vector 1290 inline static __m256 ggml_v_silu(__m256 x) { 1291 const __m256 one = _mm256_set1_ps(1); 1292 const __m256 zero = _mm256_setzero_ps(); 1293 const __m256 neg_x = _mm256_sub_ps(zero, x); 1294 const __m256 exp_neg_x = ggml_v_expf(neg_x); 1295 const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x); 1296 return _mm256_div_ps(x, one_plus_exp_neg_x); 1297 } 1298 1299 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1300 inline static __m256 ggml_v_gelu_quick(__m256 x) { 1301 const __m256 one = _mm256_set1_ps(1); 1302 const __m256 exp_cx = ggml_v_expf(_mm256_mul_ps(x, _mm256_set1_ps(GELU_QUICK_COEF))); 1303 return _mm256_div_ps(x, _mm256_add_ps(one, exp_cx)); 1304 } 1305 1306 #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON 1307 1308 #if defined(__FMA__) 1309 #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z) 1310 #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z) 1311 #else 1312 #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z) 1313 #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y)) 1314 #endif 1315 1316 // adapted from arm limited optimized routine 1317 // the maximum error is 1.45358 plus 0.5 ulps 1318 // numbers above 88.38 will flush to infinity 1319 // numbers beneath -103.97 will flush to zero 1320 inline static __m128 ggml_v_expf(__m128 x) { 1321 const __m128 r = _mm_set1_ps(0x1.8p23f); 1322 const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r); 1323 const __m128 n = _mm_sub_ps(z, r); 1324 const __m128 b = 1325 NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x)); 1326 const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23); 1327 const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1)))); 1328 const __m128i c = 1329 _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126))); 1330 const __m128 u = _mm_mul_ps(b, b); 1331 const __m128 j = 1332 MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u, 1333 MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))), 1334 u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b)); 1335 if (!_mm_movemask_epi8(c)) 1336 return MADD128(j, k, k); 1337 const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())), 1338 _mm_set1_epi32(0x82000000u)); 1339 const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u))); 1340 const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g)); 1341 const __m128i d = 1342 _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192))); 1343 return _mm_or_ps( 1344 _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)), 1345 _mm_andnot_ps(_mm_castsi128_ps(d), 1346 _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)), 1347 _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k))))); 1348 } 1349 1350 // computes silu x/(1+exp(-x)) in single precision vector 1351 inline static __m128 ggml_v_silu(__m128 x) { 1352 const __m128 one = _mm_set1_ps(1); 1353 const __m128 zero = _mm_setzero_ps(); 1354 const __m128 neg_x = _mm_sub_ps(zero, x); 1355 const __m128 exp_neg_x = ggml_v_expf(neg_x); 1356 const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x); 1357 return _mm_div_ps(x, one_plus_exp_neg_x); 1358 } 1359 1360 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1361 inline static __m128 ggml_v_gelu_quick(__m128 x) { 1362 const __m128 one = _mm_set1_ps(1); 1363 const __m128 exp_cx = ggml_v_expf(_mm_mul_ps(x, _mm_set1_ps(GELU_QUICK_COEF))); 1364 return _mm_div_ps(x, _mm_add_ps(one, exp_cx)); 1365 } 1366 1367 #elif defined(__VXE__) || defined(__VXE2__) 1368 1369 // adapted from arm limited optimized routine 1370 // the maximum error is 1.45358 plus 0.5 ulps 1371 // numbers above 88.38 will flush to infinity 1372 // numbers beneath -103.97 will flush to zero 1373 inline static float32x4_t ggml_v_expf(float32x4_t v_x) { 1374 const float32x4_t v_r = vec_splats(0x1.8p23f); 1375 const float32x4_t v_z = vec_madd(v_x, vec_splats(0x1.715476p+0f), v_r); 1376 const float32x4_t v_n = vec_sub(v_z, v_r); 1377 const float32x4_t v_b = vec_nmsub(v_n, vec_splats(0x1.7f7d1cp-20f), 1378 vec_nmsub(v_n, vec_splats(0x1.62e4p-1f), v_x)); 1379 const uint32x4_t v_e = vec_sl((uint32x4_t)v_z, 23); 1380 const float32x4_t v_k = (float32x4_t)vec_add(v_e, (uint32x4_t)vec_splats(1.0f)); 1381 const uint32x4_t v_c = (uint32x4_t)vec_cmpgt(vec_abs(v_n), vec_splats(126.0f)); 1382 const float32x4_t v_u = vec_mul(v_b, v_b); 1383 const float32x4_t v_j = vec_madd( 1384 vec_madd(vec_madd(vec_splats(0x1.0e4020p-7f), v_b, vec_splats(0x1.573e2ep-5f)), v_u, 1385 vec_madd(vec_splats(0x1.555e66p-3f), v_b, vec_splats(0x1.fffdb6p-2f))), 1386 v_u, vec_mul(vec_splats(0x1.ffffecp-1f), v_b)); 1387 if (vec_all_eq(v_c, vec_splats(UINT32_C(0)))) { 1388 return vec_madd(v_j, v_k, v_k); 1389 } 1390 const uint32x4_t v_d = vec_and((uint32x4_t)vec_cmple(v_n, vec_splats(0.0f)), vec_splats(UINT32_C(0x82000000))); 1391 const float32x4_t v_s1 = (float32x4_t)vec_add(v_d, vec_splats(UINT32_C(0x7f000000))); 1392 const float32x4_t v_s2 = (float32x4_t)vec_sub(v_e, v_d); 1393 return vec_sel(vec_sel(vec_madd(v_k, v_j, v_k), vec_mul(vec_madd(v_s2, v_j, v_s2), v_s1), v_c), 1394 vec_mul(v_s1, v_s1), (uint32x4_t)vec_cmpgt(vec_abs(v_n), vec_splats(192.0f))); 1395 } 1396 1397 // computes silu x/(1+exp(-x)) in single precision vector 1398 inline static float32x4_t ggml_v_silu(float32x4_t v_x) { 1399 const float32x4_t v_one = vec_splats(1.0f); 1400 const float32x4_t v_zero = vec_splats(0.0f); 1401 const float32x4_t v_neg_x = vec_sub(v_zero, v_x); 1402 const float32x4_t v_exp_neg_x = ggml_v_expf(v_neg_x); 1403 const float32x4_t v_one_plus_exp_neg_x = vec_add(v_one, v_exp_neg_x); 1404 return vec_div(v_x, v_one_plus_exp_neg_x); 1405 } 1406 1407 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1408 inline static float32x4_t ggml_v_gelu_quick(float32x4_t x) { 1409 const float32x4_t one = vec_splats(1.0f); 1410 const float32x4_t exp_cx = ggml_v_expf(vec_mul(x, vec_splats(GELU_QUICK_COEF))); 1411 return vec_div(x, vec_add(one, exp_cx)); 1412 } 1413 1414 #elif defined(__riscv_v) 1415 1416 // adapted from arm limited optimized routine 1417 // the maximum error is 1.45358 plus 0.5 ulps 1418 // numbers above 88.38 will flush to infinity 1419 // numbers beneath -103.97 will flush to zero 1420 inline static vfloat32m2_t ggml_v_expf_m2(vfloat32m2_t x, int vl) { 1421 const vfloat32m2_t r = __riscv_vfmv_v_f_f32m2(0x1.8p23f, vl); 1422 #ifdef __riscv_xtheadvector 1423 // workaround for compiler bug (gcc 14.3.0: Error: unrecognized opcode `th.vmv1r.v v2,v4') 1424 vfloat32m2_t z = __riscv_vfadd_vf_f32m2(r, 0.0f, vl); 1425 z = __riscv_vfmacc_vf_f32m2(z, 0x1.715476p+0f, x, vl); 1426 #else 1427 const vfloat32m2_t z = __riscv_vfmacc_vf_f32m2(r, 0x1.715476p+0f, x, vl); 1428 #endif 1429 const vfloat32m2_t n = __riscv_vfsub_vv_f32m2(z, r, vl); 1430 const vfloat32m2_t b = __riscv_vfnmsac_vf_f32m2(__riscv_vfnmsac_vf_f32m2(x, 0x1.62e4p-1f, n, vl), 1431 0x1.7f7d1cp-20f, n, vl); 1432 const vuint32m2_t e = __riscv_vsll_vx_u32m2(__riscv_vreinterpret_v_f32m2_u32m2(z), 23, vl); 1433 const vfloat32m2_t k = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vadd_vx_u32m2(e, 0x3f800000, vl)); // 1.0f 1434 const vbool16_t c = __riscv_vmfgt_vf_f32m2_b16(__riscv_vfabs_v_f32m2(n, vl), 126.0f, vl); 1435 const vfloat32m2_t u = __riscv_vfmul_vv_f32m2(b, b, vl); 1436 const vfloat32m2_t j = __riscv_vfmacc_vv_f32m2( 1437 __riscv_vfmul_vf_f32m2(b, 0x1.ffffecp-1f, vl), 1438 __riscv_vfmacc_vv_f32m2( 1439 __riscv_vfmacc_vf_f32m2(__riscv_vfmv_v_f_f32m2(0x1.fffdb6p-2f, vl), 0x1.555e66p-3f, b, vl), 1440 __riscv_vfmacc_vf_f32m2(__riscv_vfmv_v_f_f32m2(0x1.573e2ep-5f, vl), 0x1.0e4020p-7f, b, vl), 1441 u, vl), u, vl); 1442 if (!__riscv_vcpop_m_b16(c, vl)) 1443 return __riscv_vfmacc_vv_f32m2(k, j, k, vl); 1444 const vbool16_t dm = __riscv_vmfle_vf_f32m2_b16(n, 0.0f, vl); 1445 const vuint32m2_t d = __riscv_vmerge_vxm_u32m2(__riscv_vmv_v_x_u32m2(0, vl), 0x82000000, dm, vl); 1446 const vfloat32m2_t s1 = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vadd_vx_u32m2(d, 0x7f000000, vl)); 1447 const vfloat32m2_t s2 = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vsub_vv_u32m2(e, d, vl)); 1448 const vfloat32m2_t r1 = __riscv_vmerge_vvm_f32m2( 1449 __riscv_vfmacc_vv_f32m2(k, k, j, vl), 1450 __riscv_vfmul_vv_f32m2(__riscv_vfmacc_vv_f32m2(s2, s2, j, vl), s1, vl), 1451 c, vl); 1452 return __riscv_vmerge_vvm_f32m2( 1453 r1, __riscv_vfmul_vv_f32m2(s1, s1, vl), 1454 __riscv_vmfgt_vf_f32m2_b16(__riscv_vfabs_v_f32m2(n, vl), 192.0f, vl), 1455 vl); 1456 } 1457 1458 // computes silu x/(1+exp(-x)) in single precision vector 1459 inline static vfloat32m2_t ggml_v_silu_m2(vfloat32m2_t x, int vl) { 1460 const vfloat32m2_t neg_x = __riscv_vfneg_v_f32m2(x, vl); 1461 const vfloat32m2_t exp_neg_x = ggml_v_expf_m2(neg_x, vl); 1462 const vfloat32m2_t one_plus_exp_neg_x = __riscv_vfadd_vf_f32m2(exp_neg_x, 1.0f, vl); 1463 return __riscv_vfdiv_vv_f32m2(x, one_plus_exp_neg_x, vl); 1464 } 1465 1466 // gelu_quick(x) = x / (1 + exp(-1.702 x)); x stays the numerator so it cannot overflow 1467 inline static vfloat32m2_t ggml_v_gelu_quick_m2(vfloat32m2_t x, int vl) { 1468 const vfloat32m2_t exp_cx = ggml_v_expf_m2(__riscv_vfmul_vf_f32m2(x, GELU_QUICK_COEF, vl), vl); 1469 return __riscv_vfdiv_vv_f32m2(x, __riscv_vfadd_vf_f32m2(exp_cx, 1.0f, vl), vl); 1470 } 1471 1472 #endif // __ARM_NEON / __AVX2__ / __SSE2__ / __VXE__ / __riscv_v 1473 1474 inline static void ggml_vec_silu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { 1475 for (int i = 0; i < n; ++i) { 1476 y[i] = ggml_silu_f16(x[i]); 1477 } 1478 } 1479 1480 inline static void ggml_vec_silu_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x) { 1481 for (int i = 0; i < n; ++i) { 1482 y[i] = GGML_FP32_TO_BF16(ggml_silu_f32(GGML_BF16_TO_FP32(x[i]))); 1483 } 1484 } 1485 1486 inline static float ggml_silu_backward_f32(float x, float dy) { 1487 const float s = 1.0f/(1.0f + expf(-x)); 1488 return dy*s*(1.0f + x*(1.0f - s)); 1489 } 1490 1491 inline static ggml_fp16_t ggml_silu_backward_f16(ggml_fp16_t x, ggml_fp16_t dy) { 1492 const float v = GGML_CPU_FP16_TO_FP32(x); 1493 const float s = 1.0f/(1.0f + expf(-v)); 1494 return GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(dy)*s*(1.0f + v*(1.0f - s))); 1495 } 1496 1497 inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) { 1498 for (int i = 0; i < n; ++i) { 1499 dx[i] = ggml_silu_backward_f32(x[i], dy[i]); 1500 } 1501 } 1502 1503 inline static void ggml_vec_silu_backward_f16(const int n, ggml_fp16_t * dx, const ggml_fp16_t * x, const ggml_fp16_t * dy) { 1504 for (int i = 0; i < n; ++i) { 1505 dx[i] = ggml_silu_backward_f16(x[i], dy[i]); 1506 } 1507 } 1508 1509 inline static void ggml_vec_reglu_f32 (const int n, float * y, const float * x, const float * g) { 1510 for (int i = 0; i < n; ++i) { 1511 y[i] = (x[i] > 0.f) ? x[i] * g[i] : 0.f; 1512 } 1513 } 1514 1515 inline static void ggml_vec_reglu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) { 1516 for (int i = 0; i < n; ++i) { 1517 float v = GGML_CPU_FP16_TO_FP32(x[i]); 1518 y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v * GGML_CPU_FP16_TO_FP32(g[i]) : 0.f); 1519 } 1520 } 1521 1522 inline static void ggml_vec_reglu_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x, const ggml_bf16_t * g) { 1523 for (int i = 0; i < n; ++i) { 1524 float v = GGML_BF16_TO_FP32(x[i]); 1525 y[i] = GGML_FP32_TO_BF16((v > 0.f) ? v * GGML_BF16_TO_FP32(g[i]) : 0.f); 1526 } 1527 } 1528 1529 #ifdef GGML_GELU_FP16 1530 inline static void ggml_vec_geglu_f32(const int n, float * y, const float * x, const float * g) { 1531 uint16_t t; 1532 for (int i = 0; i < n; ++i) { 1533 if (x[i] <= -10.0f) { 1534 y[i] = 0.0f; 1535 } else if (x[i] >= 10.0f) { 1536 y[i] = x[i] * g[i]; 1537 } else { 1538 ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]); 1539 memcpy(&t, &fp16, sizeof(uint16_t)); 1540 y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[t]) * g[i]; 1541 } 1542 } 1543 } 1544 #else 1545 inline static void ggml_vec_geglu_f32(const int n, float * y, const float * x, const float * g) { 1546 for (int i = 0; i < n; ++i) { 1547 y[i] = ggml_gelu_f32(x[i]) * g[i]; 1548 } 1549 } 1550 #endif 1551 1552 inline static void ggml_vec_geglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) { 1553 const uint16_t * i16 = (const uint16_t *) x; 1554 for (int i = 0; i < n; ++i) { 1555 float v = GGML_CPU_FP16_TO_FP32(g[i]); 1556 y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[i16[i]]) * v); 1557 } 1558 } 1559 1560 inline static void ggml_vec_geglu_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x, const ggml_bf16_t * g) { 1561 for (int i = 0; i < n; ++i) { 1562 y[i] = GGML_FP32_TO_BF16(ggml_gelu_f32(GGML_BF16_TO_FP32(x[i])) * GGML_BF16_TO_FP32(g[i])); 1563 } 1564 } 1565 1566 void ggml_vec_swiglu_f32(const int n, float * y, const float * x, const float * g); 1567 1568 inline static void ggml_vec_swiglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) { 1569 for (int i = 0; i < n; ++i) { 1570 float xi = GGML_CPU_FP16_TO_FP32(x[i]); 1571 float gi = GGML_CPU_FP16_TO_FP32(g[i]); 1572 y[i] = GGML_CPU_FP32_TO_FP16((xi/(1.0f + expf(-xi))) * gi); 1573 } 1574 } 1575 1576 inline static void ggml_vec_swiglu_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x, const ggml_bf16_t * g) { 1577 for (int i = 0; i < n; ++i) { 1578 float xi = GGML_BF16_TO_FP32(x[i]); 1579 float gi = GGML_BF16_TO_FP32(g[i]); 1580 y[i] = GGML_FP32_TO_BF16((xi/(1.0f + expf(-xi))) * gi); 1581 } 1582 } 1583 1584 inline static void ggml_vec_geglu_erf_f32(const int n, float * y, const float * x, const float * g) { 1585 for (int i = 0; i < n; ++i) { 1586 float xi = x[i]; 1587 y[i] = 0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * g[i]; 1588 } 1589 } 1590 1591 inline static void ggml_vec_geglu_erf_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) { 1592 for (int i = 0; i < n; ++i) { 1593 float xi = GGML_CPU_FP16_TO_FP32(x[i]); 1594 float gi = GGML_CPU_FP16_TO_FP32(g[i]); 1595 y[i] = GGML_CPU_FP32_TO_FP16(0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * gi); 1596 } 1597 } 1598 1599 inline static void ggml_vec_geglu_erf_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x, const ggml_bf16_t * g) { 1600 for (int i = 0; i < n; ++i) { 1601 float xi = GGML_BF16_TO_FP32(x[i]); 1602 float gi = GGML_BF16_TO_FP32(g[i]); 1603 y[i] = GGML_FP32_TO_BF16(0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * gi); 1604 } 1605 } 1606 1607 #ifdef GGML_GELU_QUICK_FP16 1608 inline static void ggml_vec_geglu_quick_f32(const int n, float * y, const float * x, const float * g) { 1609 uint16_t t; 1610 for (int i = 0; i < n; ++i) { 1611 ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]); 1612 memcpy(&t, &fp16, sizeof(uint16_t)); 1613 y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]) * g[i]; 1614 } 1615 } 1616 #else 1617 void ggml_vec_geglu_quick_f32(const int n, float * y, const float * x, const float * g); 1618 #endif 1619 1620 inline static void ggml_vec_geglu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) { 1621 const uint16_t * i16 = (const uint16_t *) x; 1622 for (int i = 0; i < n; ++i) { 1623 float v = GGML_CPU_FP16_TO_FP32(g[i]); 1624 y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[i16[i]]) * v); 1625 } 1626 } 1627 1628 inline static void ggml_vec_geglu_quick_bf16(const int n, ggml_bf16_t * y, const ggml_bf16_t * x, const ggml_bf16_t * g) { 1629 for (int i = 0; i < n; ++i) { 1630 y[i] = GGML_FP32_TO_BF16(ggml_gelu_quick_f32(GGML_BF16_TO_FP32(x[i])) * GGML_BF16_TO_FP32(g[i])); 1631 } 1632 } 1633 1634 inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) { 1635 #ifndef GGML_USE_ACCELERATE 1636 ggml_float sum = 0.0; 1637 for (int i = 0; i < n; ++i) { 1638 sum += (ggml_float)x[i]; 1639 } 1640 *s = (float)sum; 1641 #else 1642 vDSP_sve(x, 1, s, n); 1643 #endif 1644 } 1645 1646 inline static void ggml_vec_cumsum_f32(const int n, float * y, const float * x) { 1647 for (int i = 0; i < n; ++i) { 1648 if (i == 0) { 1649 y[i] = x[i]; 1650 } else { 1651 y[i] = y[i - 1] + x[i]; 1652 } 1653 } 1654 } 1655 1656 inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) { 1657 ggml_float sum = 0.0; 1658 for (int i = 0; i < n; ++i) { 1659 sum += (ggml_float)x[i]; 1660 } 1661 *s = sum; 1662 } 1663 1664 inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) { 1665 float sum = 0.0f; 1666 for (int i = 0; i < n; ++i) { 1667 sum += GGML_CPU_FP16_TO_FP32(x[i]); 1668 } 1669 *s = sum; 1670 } 1671 1672 inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) { 1673 float sum = 0.0f; 1674 for (int i = 0; i < n; ++i) { 1675 sum += GGML_BF16_TO_FP32(x[i]); 1676 } 1677 *s = sum; 1678 } 1679 1680 inline static void ggml_vec_max_f32(const int n, float * s, const float * x) { 1681 #ifndef GGML_USE_ACCELERATE 1682 float max = -INFINITY; 1683 for (int i = 0; i < n; ++i) { 1684 max = MAX(max, x[i]); 1685 } 1686 *s = max; 1687 #else 1688 vDSP_maxv(x, 1, s, n); 1689 #endif 1690 } 1691 1692 inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) { 1693 ggml_vec_norm_f32(n, s, x); 1694 *s = 1.f/(*s); 1695 } 1696 1697 inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) { 1698 float max = -INFINITY; 1699 int idx = 0; 1700 for (int i = 0; i < n; ++i) { 1701 max = MAX(max, x[i]); 1702 if (max == x[i]) { idx = i; } 1703 } 1704 *s = idx; 1705 } 1706 1707 #ifdef __cplusplus 1708 } 1709 #endif