vec.h 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. // Vectorized functions for fundamental operations
  2. #pragma once
  3. #include "ggml-impl.h"
  4. #include "simd-mappings.h"
  5. #include "ggml.h"
  6. #include "ggml-cpu.h"
  7. #if defined(GGML_USE_ACCELERATE)
  8. #include <Accelerate/Accelerate.h>
  9. #endif
  10. // floating point type used to accumulate sums
  11. typedef double ggml_float;
  12. #define GGML_GELU_FP16
  13. #define GGML_GELU_QUICK_FP16
  14. #define GGML_SOFT_MAX_UNROLL 4
  15. #define GGML_VEC_DOT_UNROLL 2
  16. #define GGML_VEC_MAD_UNROLL 32
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. //
  21. // global data
  22. //
  23. // precomputed gelu table for f16 (128 KB)
  24. extern ggml_fp16_t ggml_table_gelu_f16[1 << 16];
  25. // precomputed quick gelu table for f16 (128 KB)
  26. extern ggml_fp16_t ggml_table_gelu_quick_f16[1 << 16];
  27. //
  28. // fundamental operations
  29. //
  30. 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);
  31. 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);
  32. 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);
  33. void ggml_vec_silu_f32(const int n, float * y, const float * x);
  34. 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 )
  35. ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max);
  36. ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max);
  37. 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; }
  38. 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; }
  39. 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; }
  40. 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]; }
  41. 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; }
  42. 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; }
  43. inline static void ggml_vec_add_f32 (const int n, float * z, const float * x, const float * y) {
  44. int i = 0;
  45. #if defined(__AVX2__)
  46. for (; i + 7 < n; i += 8) {
  47. __m256 vx = _mm256_loadu_ps(x + i);
  48. __m256 vy = _mm256_loadu_ps(y + i);
  49. __m256 vz = _mm256_add_ps(vx, vy);
  50. _mm256_storeu_ps(z + i, vz);
  51. }
  52. #endif
  53. for (; i < n; ++i) {
  54. z[i] = x[i] + y[i];
  55. }
  56. }
  57. inline static void ggml_vec_add_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
  58. for (int i = 0; i < n; ++i) {
  59. z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) + GGML_CPU_FP16_TO_FP32(y[i]));
  60. }
  61. }
  62. 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; }
  63. 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]; }
  64. 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; }
  65. 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]; }
  66. inline static void ggml_vec_sub_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
  67. for (int i = 0; i < n; ++i) {
  68. z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) - GGML_CPU_FP16_TO_FP32(y[i]));
  69. }
  70. }
  71. 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; }
  72. 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]; }
  73. 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]; }
  74. inline static void ggml_vec_neg_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  75. for (int i = 0; i < n; ++i) {
  76. y[i] = GGML_CPU_FP32_TO_FP16(-GGML_CPU_FP16_TO_FP32(x[i]));
  77. }
  78. }
  79. 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]; }
  80. inline static void ggml_vec_mul_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
  81. for (int i = 0; i < n; ++i) {
  82. z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) * GGML_CPU_FP16_TO_FP32(y[i]));
  83. }
  84. }
  85. 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]; }
  86. inline static void ggml_vec_div_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
  87. for (int i = 0; i < n; ++i) {
  88. z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) / GGML_CPU_FP16_TO_FP32(y[i]));
  89. }
  90. }
  91. // compute GGML_VEC_DOT_UNROLL dot products at once
  92. // xs - x row stride in bytes
  93. 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) {
  94. ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
  95. ggml_fp16_t * GGML_RESTRICT x[GGML_VEC_DOT_UNROLL];
  96. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  97. x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
  98. }
  99. #if defined(GGML_SIMD)
  100. #if defined(__ARM_FEATURE_SVE)
  101. const int sve_register_length = svcntb() * 8;
  102. const int ggml_f16_epr = sve_register_length / 16; // running when 16
  103. const int ggml_f16_step = 8 * ggml_f16_epr; // choose 8 SVE registers
  104. const int np = (n & ~(ggml_f16_step - 1));
  105. svfloat16_t sum_00 = svdup_n_f16(0.0f);
  106. svfloat16_t sum_01 = svdup_n_f16(0.0f);
  107. svfloat16_t sum_02 = svdup_n_f16(0.0f);
  108. svfloat16_t sum_03 = svdup_n_f16(0.0f);
  109. svfloat16_t sum_10 = svdup_n_f16(0.0f);
  110. svfloat16_t sum_11 = svdup_n_f16(0.0f);
  111. svfloat16_t sum_12 = svdup_n_f16(0.0f);
  112. svfloat16_t sum_13 = svdup_n_f16(0.0f);
  113. svfloat16_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
  114. svfloat16_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
  115. for (int i = 0; i < np; i += ggml_f16_step) {
  116. ay1 = GGML_F16x_VEC_LOAD(y + i + 0 * ggml_f16_epr, 0); // 8 elements
  117. ax1 = GGML_F16x_VEC_LOAD(x[0] + i + 0*ggml_f16_epr, 0); // 8 elements
  118. sum_00 = GGML_F16x_VEC_FMA(sum_00, ax1, ay1); // sum_00 = sum_00+ax1*ay1
  119. ax1 = GGML_F16x_VEC_LOAD(x[1] + i + 0*ggml_f16_epr, 0); // 8 elements
  120. sum_10 = GGML_F16x_VEC_FMA(sum_10, ax1, ay1);
  121. ay2 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 1); // next 8 elements
  122. ax2 = GGML_F16x_VEC_LOAD(x[0] + i + 1*ggml_f16_epr, 1); // next 8 elements
  123. sum_01 = GGML_F16x_VEC_FMA(sum_01, ax2, ay2);
  124. ax2 = GGML_F16x_VEC_LOAD(x[1] + i + 1*ggml_f16_epr, 1);
  125. sum_11 = GGML_F16x_VEC_FMA(sum_11, ax2, ay2);
  126. ay3 = GGML_F16x_VEC_LOAD(y + i + 2 * ggml_f16_epr, 2);
  127. ax3 = GGML_F16x_VEC_LOAD(x[0] + i + 2*ggml_f16_epr, 2);
  128. sum_02 = GGML_F16x_VEC_FMA(sum_02, ax3, ay3);
  129. ax3 = GGML_F16x_VEC_LOAD(x[1] + i + 2*ggml_f16_epr, 2);
  130. sum_12 = GGML_F16x_VEC_FMA(sum_12, ax3, ay3);
  131. ay4 = GGML_F16x_VEC_LOAD(y + i + 3 * ggml_f16_epr, 3);
  132. ax4 = GGML_F16x_VEC_LOAD(x[0] + i + 3*ggml_f16_epr, 3);
  133. sum_03 = GGML_F16x_VEC_FMA(sum_03, ax4, ay4);
  134. ax4 = GGML_F16x_VEC_LOAD(x[1] + i + 3*ggml_f16_epr, 3);
  135. sum_13 = GGML_F16x_VEC_FMA(sum_13, ax4, ay4);
  136. ay5 = GGML_F16x_VEC_LOAD(y + i + 4 * ggml_f16_epr, 4);
  137. ax5 = GGML_F16x_VEC_LOAD(x[0] + i + 4*ggml_f16_epr, 4);
  138. sum_00 = GGML_F16x_VEC_FMA(sum_00, ax5, ay5);
  139. ax5 = GGML_F16x_VEC_LOAD(x[1] + i + 4*ggml_f16_epr, 4);
  140. sum_10 = GGML_F16x_VEC_FMA(sum_10, ax5, ay5);
  141. ay6 = GGML_F16x_VEC_LOAD(y + i + 5 * ggml_f16_epr, 5);
  142. ax6 = GGML_F16x_VEC_LOAD(x[0] + i + 5*ggml_f16_epr, 5);
  143. sum_01 = GGML_F16x_VEC_FMA(sum_01, ax6, ay6);
  144. ax6 = GGML_F16x_VEC_LOAD(x[1] + i + 5*ggml_f16_epr, 5);
  145. sum_11 = GGML_F16x_VEC_FMA(sum_11, ax6, ay6);
  146. ay7 = GGML_F16x_VEC_LOAD(y + i + 6 * ggml_f16_epr, 6);
  147. ax7 = GGML_F16x_VEC_LOAD(x[0] + i + 6*ggml_f16_epr, 6);
  148. sum_02 = GGML_F16x_VEC_FMA(sum_02, ax7, ay7);
  149. ax7 = GGML_F16x_VEC_LOAD(x[1] + i + 6*ggml_f16_epr, 6);
  150. sum_12 = GGML_F16x_VEC_FMA(sum_12, ax7, ay7);
  151. ay8 = GGML_F16x_VEC_LOAD(y + i + 7 * ggml_f16_epr, 7);
  152. ax8 = GGML_F16x_VEC_LOAD(x[0] + i + 7*ggml_f16_epr, 7);
  153. sum_03 = GGML_F16x_VEC_FMA(sum_03, ax8, ay8);
  154. ax8 = GGML_F16x_VEC_LOAD(x[1] + i + 7*ggml_f16_epr, 7);
  155. sum_13 = GGML_F16x_VEC_FMA(sum_13, ax8, ay8);
  156. }
  157. const int np2 = (n & ~(ggml_f16_epr - 1));
  158. for (int k = np; k < np2; k += ggml_f16_epr) {
  159. svfloat16_t ry = GGML_F16x_VEC_LOAD(y + k, 0);
  160. svfloat16_t rx = GGML_F16x_VEC_LOAD(x[0] + k, 0);
  161. sum_00 = GGML_F16x_VEC_FMA(sum_00, rx, ry);
  162. rx = GGML_F16x_VEC_LOAD(x[1] + k, 0);
  163. sum_10 = GGML_F16x_VEC_FMA(sum_10, rx, ry);
  164. }
  165. if (np2 < n) {
  166. svbool_t pg = svwhilelt_b16(np2, n);
  167. svfloat16_t hx_0 = svld1_f16(pg, (const __fp16 *)(x[0] + np2));
  168. svfloat16_t hx_1 = svld1_f16(pg, (const __fp16 *)(x[1] + np2));
  169. svfloat16_t hy = svld1_f16(pg, (const __fp16 *)(y + np2));
  170. sum_00 = svmad_f16_x(pg, hx_0, hy, sum_00);
  171. sum_10 = svmad_f16_x(pg, hx_1, hy, sum_10);
  172. }
  173. GGML_F16x_VEC_REDUCE(sumf[0], sum_00, sum_01, sum_02, sum_03);
  174. GGML_F16x_VEC_REDUCE(sumf[1], sum_10, sum_11, sum_12, sum_13);
  175. #elif defined(__riscv_v_intrinsic)
  176. // todo: RVV impl
  177. for (int i = 0; i < n; ++i) {
  178. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  179. sumf[j] += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[j][i])*GGML_CPU_FP16_TO_FP32(y[i]));
  180. }
  181. }
  182. #else
  183. const int np = (n & ~(GGML_F16_STEP - 1));
  184. GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
  185. GGML_F16_VEC ax[GGML_F16_ARR];
  186. GGML_F16_VEC ay[GGML_F16_ARR];
  187. for (int i = 0; i < np; i += GGML_F16_STEP) {
  188. for (int j = 0; j < GGML_F16_ARR; j++) {
  189. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  190. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  191. ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
  192. sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
  193. }
  194. }
  195. }
  196. // reduce sum0..sum3 to sum0
  197. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  198. GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
  199. }
  200. // leftovers
  201. for (int i = np; i < n; ++i) {
  202. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  203. sumf[j] += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[j][i])*GGML_CPU_FP16_TO_FP32(y[i]));
  204. }
  205. }
  206. #endif
  207. #else
  208. for (int i = 0; i < n; ++i) {
  209. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  210. sumf[j] += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[j][i])*GGML_CPU_FP16_TO_FP32(y[i]));
  211. }
  212. }
  213. #endif
  214. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  215. s[i] = (float)sumf[i];
  216. }
  217. }
  218. inline static void ggml_vec_mad_f32(const int n, float * GGML_RESTRICT y, const float * GGML_RESTRICT x, const float v) {
  219. #if defined(GGML_SIMD)
  220. #if defined(__ARM_FEATURE_SVE)
  221. const int sve_register_length = ggml_cpu_get_sve_cnt() * 8;
  222. const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16
  223. const int ggml_f32_step = 8 * ggml_f32_epr; // choose 8 SVE registers
  224. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  225. const int np = (n & ~(ggml_f32_step - 1));
  226. svfloat32_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
  227. svfloat32_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
  228. for (int i = 0; i < np; i += ggml_f32_step) {
  229. ax1 = GGML_F32_VEC_LOAD(x + i);
  230. ay1 = GGML_F32_VEC_LOAD(y + i);
  231. ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx);
  232. GGML_F32_VEC_STORE(y + i, ay1);
  233. ax2 = GGML_F32_VEC_LOAD(x + i + 1*ggml_f32_epr);
  234. ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
  235. ay2 = GGML_F32_VEC_FMA(ay2, ax2, vx);
  236. GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2);
  237. ax3 = GGML_F32_VEC_LOAD(x + i + 2*ggml_f32_epr);
  238. ay3 = GGML_F32_VEC_LOAD(y + i + 2*ggml_f32_epr);
  239. ay3 = GGML_F32_VEC_FMA(ay3, ax3, vx);
  240. GGML_F32_VEC_STORE(y + i + 2*ggml_f32_epr, ay3);
  241. ax4 = GGML_F32_VEC_LOAD(x + i + 3*ggml_f32_epr);
  242. ay4 = GGML_F32_VEC_LOAD(y + i + 3*ggml_f32_epr);
  243. ay4 = GGML_F32_VEC_FMA(ay4, ax4, vx);
  244. GGML_F32_VEC_STORE(y + i + 3*ggml_f32_epr, ay4);
  245. ax5 = GGML_F32_VEC_LOAD(x + i + 4*ggml_f32_epr);
  246. ay5 = GGML_F32_VEC_LOAD(y + i + 4*ggml_f32_epr);
  247. ay5 = GGML_F32_VEC_FMA(ay5, ax5, vx);
  248. GGML_F32_VEC_STORE(y + i + 4*ggml_f32_epr, ay5);
  249. ax6 = GGML_F32_VEC_LOAD(x + i + 5*ggml_f32_epr);
  250. ay6 = GGML_F32_VEC_LOAD(y + i + 5*ggml_f32_epr);
  251. ay6 = GGML_F32_VEC_FMA(ay6, ax6, vx);
  252. GGML_F32_VEC_STORE(y + i + 5*ggml_f32_epr, ay6);
  253. ax7 = GGML_F32_VEC_LOAD(x + i + 6*ggml_f32_epr);
  254. ay7 = GGML_F32_VEC_LOAD(y + i + 6*ggml_f32_epr);
  255. ay7 = GGML_F32_VEC_FMA(ay7, ax7, vx);
  256. GGML_F32_VEC_STORE(y + i + 6*ggml_f32_epr, ay7);
  257. ax8 = GGML_F32_VEC_LOAD(x + i + 7*ggml_f32_epr);
  258. ay8 = GGML_F32_VEC_LOAD(y + i + 7*ggml_f32_epr);
  259. ay8 = GGML_F32_VEC_FMA(ay8, ax8, vx);
  260. GGML_F32_VEC_STORE(y + i + 7*ggml_f32_epr, ay8);
  261. }
  262. // leftovers
  263. // Since 8 unrolls are done in above loop, leftovers lie in range [0, ggml_f32_step] which is handled in below loop
  264. const int np2 = (n & ~(ggml_f32_epr - 1));
  265. for (int i = np; i < np2; i += ggml_f32_epr) {
  266. ax1 = GGML_F32_VEC_LOAD(x + i);
  267. ay1 = GGML_F32_VEC_LOAD(y + i);
  268. ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx);
  269. GGML_F32_VEC_STORE(y + i, ay1);
  270. }
  271. // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only
  272. if (np2 < n) {
  273. svbool_t pg =svwhilelt_b32(np2, n);
  274. ax1 = svld1_f32(pg, x + np2);
  275. ay1 = svld1_f32(pg, y + np2);
  276. ay1 = svmad_f32_m(pg, ax1, vx, ay1);
  277. svst1_f32(pg, y + np2, ay1);
  278. }
  279. #elif defined(__riscv_v_intrinsic)
  280. for (int i = 0, avl; i < n; i += avl) {
  281. avl = __riscv_vsetvl_e32m8(n - i);
  282. vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[i], avl);
  283. vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl);
  284. vfloat32m8_t ny = __riscv_vfmadd_vf_f32m8(ax, v, ay, avl);
  285. __riscv_vse32_v_f32m8(&y[i], ny, avl);
  286. }
  287. #else
  288. const int np = (n & ~(GGML_F32_STEP - 1));
  289. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  290. GGML_F32_VEC ax[GGML_F32_ARR];
  291. GGML_F32_VEC ay[GGML_F32_ARR];
  292. for (int i = 0; i < np; i += GGML_F32_STEP) {
  293. for (int j = 0; j < GGML_F32_ARR; j++) {
  294. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  295. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  296. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx);
  297. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  298. }
  299. }
  300. // leftovers
  301. for (int i = np; i < n; ++i) {
  302. y[i] += x[i]*v;
  303. }
  304. #endif
  305. #else
  306. // scalar
  307. for (int i = 0; i < n; ++i) {
  308. y[i] += x[i]*v;
  309. }
  310. #endif
  311. }
  312. 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) {
  313. #if defined(GGML_SIMD)
  314. #if defined(__ARM_FEATURE_SVE)
  315. const int sve_register_length = svcntb() * 8;
  316. const int ggml_f16_epr = sve_register_length / 16;
  317. const int ggml_f16_step = 8 * ggml_f16_epr;
  318. GGML_F16x_VEC vx = GGML_F16x_VEC_SET1(v);
  319. const int np= (n & ~(ggml_f16_step - 1));
  320. svfloat16_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
  321. svfloat16_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
  322. for (int i = 0; i < np; i += ggml_f16_step) {
  323. ax1 = GGML_F16x_VEC_LOAD(x + i + 0 * ggml_f16_epr, 0);
  324. ay1 = GGML_F16x_VEC_LOAD(y + i + 0 * ggml_f16_epr, 0);
  325. ay1 = GGML_F16x_VEC_FMA(ay1, ax1, vx);
  326. GGML_F16x_VEC_STORE(y + i + 0 * ggml_f16_epr, ay1, 0);
  327. ax2 = GGML_F16x_VEC_LOAD(x + i + 1 * ggml_f16_epr, 1);
  328. ay2 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 1);
  329. ay2 = GGML_F16x_VEC_FMA(ay2, ax2, vx);
  330. GGML_F16x_VEC_STORE(y + i + 1 * ggml_f16_epr, ay2, 1);
  331. ax3 = GGML_F16x_VEC_LOAD(x + i + 2 * ggml_f16_epr, 2);
  332. ay3 = GGML_F16x_VEC_LOAD(y + i + 2 * ggml_f16_epr, 2);
  333. ay3 = GGML_F16x_VEC_FMA(ay3, ax3, vx);
  334. GGML_F16x_VEC_STORE(y + i + 2 * ggml_f16_epr, ay3, 2);
  335. ax4 = GGML_F16x_VEC_LOAD(x + i + 3 * ggml_f16_epr, 3);
  336. ay4 = GGML_F16x_VEC_LOAD(y + i + 3 * ggml_f16_epr, 3);
  337. ay4 = GGML_F16x_VEC_FMA(ay4, ax4, vx);
  338. GGML_F16x_VEC_STORE(y + i + 3 * ggml_f16_epr, ay4, 3);
  339. ax5 = GGML_F16x_VEC_LOAD(x + i + 4 * ggml_f16_epr, 4);
  340. ay5 = GGML_F16x_VEC_LOAD(y + i + 4 * ggml_f16_epr, 4);
  341. ay5 = GGML_F16x_VEC_FMA(ay5, ax5, vx);
  342. GGML_F16x_VEC_STORE(y + i + 4 * ggml_f16_epr, ay5, 4);
  343. ax6 = GGML_F16x_VEC_LOAD(x + i + 5 * ggml_f16_epr, 5);
  344. ay6 = GGML_F16x_VEC_LOAD(y + i + 5 * ggml_f16_epr, 5);
  345. ay6 = GGML_F16x_VEC_FMA(ay6, ax6, vx);
  346. GGML_F16x_VEC_STORE(y + i + 5 * ggml_f16_epr, ay6, 5);
  347. ax7 = GGML_F16x_VEC_LOAD(x + i + 6 * ggml_f16_epr, 6);
  348. ay7 = GGML_F16x_VEC_LOAD(y + i + 6 * ggml_f16_epr, 6);
  349. ay7 = GGML_F16x_VEC_FMA(ay7, ax7, vx);
  350. GGML_F16x_VEC_STORE(y + i + 6 * ggml_f16_epr, ay7, 6);
  351. ax8 = GGML_F16x_VEC_LOAD(x + i + 7 * ggml_f16_epr, 7);
  352. ay8 = GGML_F16x_VEC_LOAD(y + i + 7 * ggml_f16_epr, 7);
  353. ay8 = GGML_F16x_VEC_FMA(ay8, ax8, vx);
  354. GGML_F16x_VEC_STORE(y + i + 7 * ggml_f16_epr, ay8, 7);
  355. }
  356. const int np2 = (n & ~(ggml_f16_epr - 1));
  357. for (int k = np; k < np2; k += ggml_f16_epr) {
  358. svfloat16_t rx = GGML_F16x_VEC_LOAD(x + k, 0);
  359. svfloat16_t ry = GGML_F16x_VEC_LOAD(y + k, 0);
  360. ry = GGML_F16x_VEC_FMA(ry, rx, vx);
  361. GGML_F16x_VEC_STORE(y + k, ry, 0);
  362. }
  363. if (np2 < n) {
  364. svbool_t pg = svwhilelt_b16(np2, n);
  365. svfloat16_t hx = svld1_f16(pg, (const __fp16 *)(x + np2));
  366. svfloat16_t hy = svld1_f16(pg, (const __fp16 *)(y + np2));
  367. hy = svmad_f16_x(pg, hx, vx, hy);
  368. svst1_f16(pg, (__fp16 *)(y + np2), hy);
  369. }
  370. #elif defined(__riscv_v_intrinsic)
  371. // todo: RVV impl
  372. // scalar
  373. for (int i = 0; i < n; ++i) {
  374. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v);
  375. }
  376. #else
  377. const int np = (n & ~(GGML_F16_STEP - 1));
  378. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  379. GGML_F16_VEC ax[GGML_F16_ARR];
  380. GGML_F16_VEC ay[GGML_F16_ARR];
  381. for (int i = 0; i < np; i += GGML_F16_STEP) {
  382. for (int j = 0; j < GGML_F16_ARR; j++) {
  383. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  384. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  385. ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
  386. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  387. }
  388. }
  389. // leftovers
  390. for (int i = np; i < n; ++i) {
  391. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v);
  392. }
  393. #endif
  394. #else
  395. // scalar
  396. for (int i = 0; i < n; ++i) {
  397. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v);
  398. }
  399. #endif
  400. }
  401. // xs and vs are byte strides of x and v
  402. 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) {
  403. const float * GGML_RESTRICT x[GGML_VEC_MAD_UNROLL];
  404. const float * GGML_RESTRICT v[GGML_VEC_MAD_UNROLL];
  405. for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) {
  406. x[i] = (const float *) ((const char *) xv + i*xs);
  407. v[i] = (const float *) ((const char *) vv + i*vs);
  408. }
  409. #if defined(GGML_SIMD)
  410. #if defined(__ARM_FEATURE_SVE)
  411. // scalar Route to scalar implementation //TODO: Write SVE code
  412. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  413. for (int i = 0; i < n; ++i) {
  414. y[i] += x[k][i]*v[k][0];
  415. }
  416. }
  417. #elif defined(__riscv_v_intrinsic)
  418. for (int i = 0, avl; i < n; i += avl) {
  419. avl = __riscv_vsetvl_e32m8(n - i);
  420. vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl);
  421. for (int k = 0; k < GGML_VEC_MAD_UNROLL; k++) {
  422. vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[k][i], avl);
  423. ay = __riscv_vfmadd_vf_f32m8(ax, v[k][0], ay, avl);
  424. }
  425. __riscv_vse32_v_f32m8(&y[i], ay, avl);
  426. }
  427. #else
  428. const int np = (n & ~(GGML_F32_STEP - 1));
  429. GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL];
  430. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  431. vx[k] = GGML_F32_VEC_SET1(v[k][0]);
  432. }
  433. GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR];
  434. GGML_F32_VEC ay[GGML_F32_ARR];
  435. for (int i = 0; i < np; i += GGML_F32_STEP) {
  436. for (int j = 0; j < GGML_F32_ARR; j++) {
  437. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  438. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  439. ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
  440. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
  441. }
  442. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  443. }
  444. }
  445. // leftovers
  446. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  447. for (int i = np; i < n; ++i) {
  448. y[i] += x[k][i]*v[k][0];
  449. }
  450. }
  451. #endif
  452. #else
  453. // scalar
  454. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  455. for (int i = 0; i < n; ++i) {
  456. y[i] += x[k][i]*v[k][0];
  457. }
  458. }
  459. #endif
  460. }
  461. inline static void ggml_vec_mad1_f32(const int n, float * y, const float * x, const float s, const float b) {
  462. #if defined(GGML_USE_ACCELERATE)
  463. vDSP_vsmsa(x, 1, &s, &b, y, 1, n);
  464. #elif defined(GGML_SIMD)
  465. #if defined(__ARM_FEATURE_SVE)
  466. // scalar ; TODO: Write SVE code
  467. for (int i = 0; i < n; ++i) {
  468. y[i] = x[i]*s + b;
  469. }
  470. #elif defined(__riscv_v_intrinsic)
  471. for (int i = 0, avl; i < n; i += avl) {
  472. avl = __riscv_vsetvl_e32m8(n - i);
  473. vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[i], avl);
  474. vfloat32m8_t vb = __riscv_vfmv_v_f_f32m8(b, avl);
  475. vfloat32m8_t ny = __riscv_vfmadd_vf_f32m8(ax, s, vb, avl);
  476. __riscv_vse32_v_f32m8(&y[i], ny, avl);
  477. }
  478. #else
  479. const int np = (n & ~(GGML_F32_STEP - 1));
  480. GGML_F32_VEC vs = GGML_F32_VEC_SET1(s);
  481. GGML_F32_VEC vb = GGML_F32_VEC_SET1(b);
  482. GGML_F32_VEC ay[GGML_F32_ARR];
  483. for (int i = 0; i < np; i += GGML_F32_STEP) {
  484. for (int j = 0; j < GGML_F32_ARR; j++) {
  485. ay[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  486. ay[j] = GGML_F32_VEC_FMA(vb, ay[j], vs);
  487. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  488. }
  489. }
  490. // leftovers
  491. for (int i = np; i < n; ++i) {
  492. y[i] = x[i]*s + b;
  493. }
  494. #endif
  495. #else
  496. // scalar
  497. for (int i = 0; i < n; ++i) {
  498. y[i] = x[i]*s + b;
  499. }
  500. #endif
  501. }
  502. //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; }
  503. inline static void ggml_vec_scale_f32(const int n, float * y, const float v) {
  504. #if defined(GGML_USE_ACCELERATE)
  505. vDSP_vsmul(y, 1, &v, y, 1, n);
  506. #elif defined(GGML_SIMD)
  507. #if defined(__ARM_FEATURE_SVE)
  508. const int sve_register_length = ggml_cpu_get_sve_cnt() * 8;
  509. const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16
  510. const int ggml_f32_step = 2 * ggml_f32_epr;
  511. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  512. const int np = (n & ~(ggml_f32_step - 1));
  513. svfloat32_t ay1;
  514. svfloat32_t ay2;
  515. for (int i = 0; i < np; i += ggml_f32_step) {
  516. ay1 = GGML_F32_VEC_LOAD(y + i);
  517. ay1 = GGML_F32_VEC_MUL(ay1, vx);
  518. GGML_F32_VEC_STORE(y + i, ay1);
  519. ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
  520. ay2 = GGML_F32_VEC_MUL(ay2, vx);
  521. GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2);
  522. }
  523. // leftovers
  524. // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only
  525. for (int i = np; i < n; i += ggml_f32_epr) {
  526. svbool_t pg = svwhilelt_b32(i, n);
  527. ay1 = svld1_f32(pg, y + i);
  528. ay1 = svmul_f32_m(pg, ay1, vx);
  529. svst1_f32(pg, y + i, ay1);
  530. }
  531. #elif defined(__riscv_v_intrinsic)
  532. for (int i = 0, avl; i < n; i += avl) {
  533. avl = __riscv_vsetvl_e32m8(n - i);
  534. vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl);
  535. vfloat32m8_t ny = __riscv_vfmul_vf_f32m8(ay, v, avl);
  536. __riscv_vse32_v_f32m8(&y[i], ny, avl);
  537. }
  538. #else
  539. const int np = (n & ~(GGML_F32_STEP - 1));
  540. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  541. GGML_F32_VEC ay[GGML_F32_ARR];
  542. for (int i = 0; i < np; i += GGML_F32_STEP) {
  543. for (int j = 0; j < GGML_F32_ARR; j++) {
  544. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  545. ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
  546. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  547. }
  548. }
  549. // leftovers
  550. for (int i = np; i < n; ++i) {
  551. y[i] *= v;
  552. }
  553. #endif
  554. #else
  555. // scalar
  556. for (int i = 0; i < n; ++i) {
  557. y[i] *= v;
  558. }
  559. #endif
  560. }
  561. inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) {
  562. #if defined(GGML_SIMD)
  563. #if defined(__ARM_FEATURE_SVE)
  564. const int sve_register_length = svcntb() * 8;
  565. const int ggml_f16_epr = sve_register_length / 16;
  566. const int ggml_f16_step = 2 * ggml_f16_epr;
  567. GGML_F16x_VEC vx = GGML_F16x_VEC_SET1(v);
  568. const int np = (n & ~(ggml_f16_step - 1));
  569. svfloat16_t ay1, ay2;
  570. for (int i = 0; i < np; i += ggml_f16_step) {
  571. ay1 = GGML_F16x_VEC_LOAD(y + i + 0*ggml_f16_epr, 0);
  572. ay1 = GGML_F16x_VEC_MUL(ay1, vx);
  573. GGML_F16x_VEC_STORE(y + i + 0*ggml_f16_epr, ay1, 0);
  574. ay2 = GGML_F16x_VEC_LOAD(y + i + 1*ggml_f16_epr, 1);
  575. ay2 = GGML_F16x_VEC_MUL(ay2, vx);
  576. GGML_F16x_VEC_STORE(y + i + 1*ggml_f16_epr, ay2, 1);
  577. }
  578. // leftovers
  579. // maximum number of leftover elements will be less that ggmlF_16x_epr. Apply predicated svmad on available elements only
  580. if (np < n) {
  581. svbool_t pg = svwhilelt_b16(np, n);
  582. svfloat16_t hy = svld1_f16(pg, (__fp16 *)(y + np));
  583. svfloat16_t out = svmul_f16_m(pg, hy, vx);
  584. svst1_f16(pg, (__fp16 *)(y + np), out);
  585. }
  586. #elif defined(__riscv_v_intrinsic)
  587. // todo: RVV impl
  588. // scalar
  589. for (int i = 0; i < n; ++i) {
  590. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v);
  591. }
  592. #else
  593. const int np = (n & ~(GGML_F16_STEP - 1));
  594. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  595. GGML_F16_VEC ay[GGML_F16_ARR];
  596. for (int i = 0; i < np; i += GGML_F16_STEP) {
  597. for (int j = 0; j < GGML_F16_ARR; j++) {
  598. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  599. ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
  600. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  601. }
  602. }
  603. // leftovers
  604. for (int i = np; i < n; ++i) {
  605. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v);
  606. }
  607. #endif
  608. #else
  609. // scalar
  610. for (int i = 0; i < n; ++i) {
  611. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v);
  612. }
  613. #endif
  614. }
  615. 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); }
  616. 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]; }
  617. inline static void ggml_vec_sqr_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  618. for (int i = 0; i < n; ++i) {
  619. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  620. y[i] = GGML_CPU_FP32_TO_FP16(v*v);
  621. }
  622. }
  623. 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]); }
  624. inline static void ggml_vec_sqrt_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  625. for (int i = 0; i < n; ++i) {
  626. y[i] = GGML_CPU_FP32_TO_FP16(sqrtf(GGML_CPU_FP16_TO_FP32(x[i])));
  627. }
  628. }
  629. 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]); }
  630. inline static void ggml_vec_log_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  631. for (int i = 0; i < n; ++i) {
  632. y[i] = GGML_CPU_FP32_TO_FP16(logf(GGML_CPU_FP16_TO_FP32(x[i])));
  633. }
  634. }
  635. 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]); }
  636. inline static void ggml_vec_sin_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  637. for (int i = 0; i < n; ++i) {
  638. y[i] = GGML_CPU_FP32_TO_FP16(sinf(GGML_CPU_FP16_TO_FP32(x[i])));
  639. }
  640. }
  641. 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]); }
  642. inline static void ggml_vec_cos_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  643. for (int i = 0; i < n; ++i) {
  644. y[i] = GGML_CPU_FP32_TO_FP16(cosf(GGML_CPU_FP16_TO_FP32(x[i])));
  645. }
  646. }
  647. 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]); }
  648. inline static void ggml_vec_abs_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  649. for (int i = 0; i < n; ++i) {
  650. y[i] = GGML_CPU_FP32_TO_FP16(fabsf(GGML_CPU_FP16_TO_FP32(x[i])));
  651. }
  652. }
  653. 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); }
  654. inline static void ggml_vec_sgn_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  655. for (int i = 0; i < n; ++i) {
  656. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  657. y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? 1.f : ((v < 0.f) ? -1.f : 0.f));
  658. }
  659. }
  660. 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; }
  661. inline static void ggml_vec_step_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  662. for (int i = 0; i < n; ++i) {
  663. y[i] = GGML_CPU_FP32_TO_FP16((GGML_CPU_FP16_TO_FP32(x[i]) > 0.f) ? 1.f : 0.f);
  664. }
  665. }
  666. 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]); }
  667. inline static void ggml_vec_tanh_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  668. for (int i = 0; i < n; ++i) {
  669. y[i] = GGML_CPU_FP32_TO_FP16(tanhf(GGML_CPU_FP16_TO_FP32(x[i])));
  670. }
  671. }
  672. 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]); }
  673. inline static void ggml_vec_elu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  674. for (int i = 0; i < n; ++i) {
  675. y[i] = GGML_CPU_FP32_TO_FP16(expm1f(GGML_CPU_FP16_TO_FP32(x[i])));
  676. }
  677. }
  678. 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; }
  679. inline static void ggml_vec_relu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  680. for (int i = 0; i < n; ++i) {
  681. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  682. y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v : 0.f);
  683. }
  684. }
  685. 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); }
  686. inline static void ggml_vec_leaky_relu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const float ns) {
  687. for (int i = 0; i < n; ++i) {
  688. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  689. y[i] = GGML_CPU_FP32_TO_FP16(((v > 0.f) ? v : 0.f) + ns * ((v < 0.0f) ? v : 0.f));
  690. }
  691. }
  692. 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])); }
  693. inline static void ggml_vec_sigmoid_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  694. for (int i = 0; i < n; ++i) {
  695. y[i] = GGML_CPU_FP32_TO_FP16(1.f / (1.f + expf(-GGML_CPU_FP16_TO_FP32(x[i]))));
  696. }
  697. }
  698. // TODO: optimize performance
  699. 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)); }
  700. inline static void ggml_vec_hardswish_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  701. for (int i = 0; i < n; ++i) {
  702. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  703. y[i] = GGML_CPU_FP32_TO_FP16(v * fminf(1.0f, fmaxf(0.0f, (v + 3.0f) / 6.0f)));
  704. }
  705. }
  706. 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)); }
  707. inline static void ggml_vec_hardsigmoid_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  708. for (int i = 0; i < n; ++i) {
  709. y[i] = GGML_CPU_FP32_TO_FP16(fminf(1.0f, fmaxf(0.0f, (GGML_CPU_FP16_TO_FP32(x[i]) + 3.0f) / 6.0f)));
  710. }
  711. }
  712. 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]); }
  713. inline static void ggml_vec_exp_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  714. for (int i = 0; i < n; ++i) {
  715. y[i] = GGML_CPU_FP32_TO_FP16(expf(GGML_CPU_FP16_TO_FP32(x[i])));
  716. }
  717. }
  718. static const float GELU_COEF_A = 0.044715f;
  719. static const float GELU_QUICK_COEF = -1.702f;
  720. static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  721. static const float SQRT_2_INV = 0.70710678118654752440084436210484f;
  722. inline static float ggml_gelu_f32(float x) {
  723. return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  724. }
  725. inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  726. const uint16_t * i16 = (const uint16_t *) x;
  727. for (int i = 0; i < n; ++i) {
  728. y[i] = ggml_table_gelu_f16[i16[i]];
  729. }
  730. }
  731. inline static void ggml_vec_gelu_erf_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  732. for (int i = 0; i < n; ++i) {
  733. float xi = GGML_CPU_FP16_TO_FP32(x[i]);
  734. float res = 0.5f*xi*(1.0f + erff(xi*SQRT_2_INV));
  735. y[i] = GGML_CPU_FP32_TO_FP16(res);
  736. }
  737. }
  738. #ifdef GGML_GELU_FP16
  739. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  740. uint16_t t;
  741. for (int i = 0; i < n; ++i) {
  742. if (x[i] <= -10.0f) {
  743. y[i] = 0.0f;
  744. } else if (x[i] >= 10.0f) {
  745. y[i] = x[i];
  746. } else {
  747. ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
  748. memcpy(&t, &fp16, sizeof(uint16_t));
  749. y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[t]);
  750. }
  751. }
  752. }
  753. #else
  754. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  755. for (int i = 0; i < n; ++i) {
  756. y[i] = ggml_gelu_f32(x[i]);
  757. }
  758. }
  759. #endif
  760. inline static void ggml_vec_gelu_erf_f32(const int n, float * y, const float * x) {
  761. for (int i = 0; i < n; ++i) {
  762. float xi = x[i];
  763. y[i] = 0.5f*xi*(1.0f + erff(xi*SQRT_2_INV));
  764. }
  765. }
  766. inline static float ggml_gelu_quick_f32(float x) {
  767. return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x)));
  768. }
  769. //inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  770. // const uint16_t * i16 = (const uint16_t *) x;
  771. // for (int i = 0; i < n; ++i) {
  772. // y[i] = ggml_table_gelu_quick_f16[i16[i]];
  773. // }
  774. //}
  775. #ifdef GGML_GELU_QUICK_FP16
  776. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  777. uint16_t t;
  778. for (int i = 0; i < n; ++i) {
  779. ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
  780. memcpy(&t, &fp16, sizeof(uint16_t));
  781. y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
  782. }
  783. }
  784. #else
  785. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  786. for (int i = 0; i < n; ++i) {
  787. y[i] = ggml_gelu_quick_f32(x[i]);
  788. }
  789. }
  790. #endif
  791. inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  792. for (int i = 0; i < n; ++i) {
  793. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  794. y[i] = GGML_CPU_FP32_TO_FP16(v*(1.0f/(1.0f+expf(GELU_QUICK_COEF*v))));
  795. }
  796. }
  797. // Sigmoid Linear Unit (SiLU) function
  798. inline static float ggml_silu_f32(float x) {
  799. return x/(1.0f + expf(-x));
  800. }
  801. inline static ggml_fp16_t ggml_silu_f16(ggml_fp16_t x) {
  802. float v = GGML_CPU_FP16_TO_FP32(x);
  803. return GGML_CPU_FP32_TO_FP16(v/(1.0f + expf(-v)));
  804. }
  805. #if __FINITE_MATH_ONLY__
  806. #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix"
  807. #error "ref: https://github.com/ggml-org/llama.cpp/pull/7154#issuecomment-2143844461"
  808. #endif
  809. /* Below function was borrowed from the GitHub repository:
  810. https://github.com/openvinotoolkit/openvino/blob/master/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp */
  811. #if defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
  812. inline static svfloat32_t exp_ps_sve(svbool_t pg, svfloat32_t src) {
  813. // Constants
  814. const svfloat32_t log2_e = svdup_n_f32(1.4426950409f);
  815. const svfloat32_t ln2 = svdup_n_f32(0.6931473921f);
  816. const svfloat32_t half_ln2_sq = svdup_n_f32(0.2413862043f);
  817. const svuint32_t not_mask17 = svdup_n_u32(~((1u << 17) - 1));
  818. const svfloat32_t one = svdup_n_f32(1.0f);
  819. const svfloat32_t inactive1 = svdup_n_f32(0.0f);
  820. const svint32_t inactive2 = svdup_n_s32(0);
  821. // Algorithm starts here
  822. svfloat32_t t0 = svmul_f32_m(pg, src, log2_e); // y = x * log2(e)
  823. svfloat32_t t1 = svrintm_f32_m(inactive1, pg, t0); // rount to int (float)
  824. svint32_t t2 = svcvt_s32_f32_m(inactive2, pg, t1); // n
  825. t1 = svsub_f32_m(pg, t0, t1); // a = y - floor(y)
  826. t1 = svadd_f32_m(pg, t1, one); // b = a + 1
  827. svuint32_t t3 = svlsr_n_u32_m(pg, svreinterpret_u32_f32(t1), 17); // v = b >> 17 (u32)
  828. svfloat32_t t4 = svexpa_f32(t3); // c = fexpa(v)
  829. t4 = svscale_f32_m(pg, t4, t2); // fexpa(v) * 2^(n)
  830. // and_(t2.d, t1.d, not_mask17.d)
  831. svfloat32_t t5 = svreinterpret_f32_u32(svand_u32_m(pg, svreinterpret_u32_f32(t1), not_mask17));
  832. t5 = svsub_f32_m(pg, t1, t5); // z
  833. t0 = svmla_f32_m(pg, ln2, t5, half_ln2_sq); // ln2 + half_ln2_sq * z
  834. t0 = svmla_f32_m(pg, one, t5, t0); // 1 + (ln2 * z) + (half_ln2_sq * z * z)
  835. t0 = svmul_f32_m(pg, t0, t4); // Final result
  836. return t0;
  837. }
  838. #endif
  839. #if defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
  840. inline static svfloat32_t ggml_v_expf(svbool_t pg, svfloat32_t x) {
  841. const svfloat32_t r = svdup_n_f32_x(pg, 0x1.8p23f);
  842. const svfloat32_t z = svmla_n_f32_x(pg, r, x, 0x1.715476p+0f);
  843. const svfloat32_t n = svsub_f32_x(pg, z, r);
  844. const svfloat32_t b = svmls_n_f32_x(pg, svmls_n_f32_x(pg, x, n, 0x1.62e4p-1f), n, 0x1.7f7d1cp-20f);
  845. const svuint32_t e = svlsl_n_u32_x(pg, svreinterpret_u32_f32(z), 23);
  846. const svfloat32_t k = svreinterpret_f32_u32(svadd_u32_x(pg, e, svreinterpret_u32_f32(svdup_n_f32_x(pg, 1))));
  847. const svbool_t c = svacgt_n_f32(pg, n, 126);
  848. const svfloat32_t u = svmul_f32_x(pg, b, b);
  849. const svfloat32_t j = svmla_f32_x(pg,
  850. svmul_n_f32_x(pg, b, 0x1.ffffecp-1f),
  851. 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),
  852. svmla_f32_x(pg, svdup_n_f32_x(pg, 0x1.573e2ep-5f), svdup_n_f32_x(pg, 0x1.0e4020p-7f), b), u), u);
  853. const svuint32_t d = svdup_n_u32_z(svcmple_n_f32(pg, n, 0.0), 0x82000000);
  854. const svfloat32_t s1 = svreinterpret_f32_u32(svadd_n_u32_x(pg, d, 0x7f000000));
  855. const svfloat32_t s2 = svreinterpret_f32_u32(svsub_u32_x(pg, e, d));
  856. return svsel_f32(svacgt_f32(pg, n, svdup_n_f32_x(pg, 192)), svmul_f32_x(pg, s1, s1),
  857. svsel_f32(c, svmul_f32_x(pg, svmla_f32_x(pg, s2, s2, j), s1), svmla_f32_x(pg, k, k, j)));
  858. }
  859. // computes silu x/(1+exp(-x)) in single precision vector
  860. inline static svfloat32_t ggml_v_silu(svbool_t pg, svfloat32_t x) {
  861. const svfloat32_t one = svdup_n_f32_x(pg, 1.0f);
  862. const svfloat32_t zero = svdup_n_f32_x(pg, 0.0f);
  863. const svfloat32_t neg_x = svsub_f32_x(pg, zero, x);
  864. const svfloat32_t exp_neg_x = ggml_v_expf(pg, neg_x);
  865. const svfloat32_t one_plus_exp_neg_x = svadd_f32_x(pg, one, exp_neg_x);
  866. return svdiv_f32_x(pg, x, one_plus_exp_neg_x);
  867. }
  868. #elif defined(__ARM_NEON) && defined(__aarch64__)
  869. // adapted from arm limited optimized routine
  870. // the maximum error is 1.45358 plus 0.5 ulps
  871. // numbers above 88.38 will flush to infinity
  872. // numbers beneath -103.97 will flush to zero
  873. inline static float32x4_t ggml_v_expf(float32x4_t x) {
  874. const float32x4_t r = vdupq_n_f32(0x1.8p23f);
  875. const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f));
  876. const float32x4_t n = vsubq_f32(z, r);
  877. const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n,
  878. vdupq_n_f32(0x1.7f7d1cp-20f));
  879. const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23);
  880. const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1))));
  881. const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126));
  882. const float32x4_t u = vmulq_f32(b, b);
  883. const float32x4_t j = vfmaq_f32(
  884. vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b),
  885. vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b),
  886. vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u);
  887. if (!vpaddd_u64(vreinterpretq_u64_u32(c)))
  888. return vfmaq_f32(k, j, k);
  889. const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000));
  890. const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000)));
  891. const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d));
  892. return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1),
  893. vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j)));
  894. }
  895. // computes silu x/(1+exp(-x)) in single precision vector
  896. inline static float32x4_t ggml_v_silu(float32x4_t x) {
  897. const float32x4_t one = vdupq_n_f32(1.0f);
  898. const float32x4_t zero = vdupq_n_f32(0.0f);
  899. const float32x4_t neg_x = vsubq_f32(zero, x);
  900. const float32x4_t exp_neg_x = ggml_v_expf(neg_x);
  901. const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x);
  902. return vdivq_f32(x, one_plus_exp_neg_x);
  903. }
  904. #elif defined(__AVX512F__) && defined(__AVX512DQ__)
  905. // adapted from arm limited optimized routine
  906. // the maximum error is 1.45358 plus 0.5 ulps
  907. // numbers above 88.38 will flush to infinity
  908. // numbers beneath -103.97 will flush to zero
  909. inline static __m512 ggml_v_expf(__m512 x) {
  910. const __m512 r = _mm512_set1_ps(0x1.8p23f);
  911. const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
  912. const __m512 n = _mm512_sub_ps(z, r);
  913. const __m512 b =
  914. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
  915. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
  916. const __mmask16 d =
  917. _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
  918. const __m512 u = _mm512_mul_ps(b, b);
  919. const __m512 j = _mm512_fmadd_ps(
  920. _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
  921. _mm512_set1_ps(0x1.573e2ep-5f)),
  922. u,
  923. _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
  924. _mm512_set1_ps(0x1.fffdb6p-2f))),
  925. u,
  926. _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
  927. const __m512 res = _mm512_scalef_ps(j, n);
  928. if (_mm512_kortestz(d, d))
  929. return res;
  930. const __m512 zero = _mm512_setzero_ps();
  931. const __m512 alt = _mm512_mask_blend_ps(
  932. _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
  933. return _mm512_mask_blend_ps(d, res, alt);
  934. }
  935. // computes silu x/(1+exp(-x)) in single precision vector
  936. inline static __m512 ggml_v_silu(__m512 x) {
  937. const __m512 one = _mm512_set1_ps(1);
  938. const __m512 zero = _mm512_setzero_ps();
  939. const __m512 neg_x = _mm512_sub_ps(zero, x);
  940. const __m512 exp_neg_x = ggml_v_expf(neg_x);
  941. const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x);
  942. return _mm512_div_ps(x, one_plus_exp_neg_x);
  943. }
  944. #elif defined(__AVX2__) && defined(__FMA__)
  945. // adapted from arm limited optimized routine
  946. // the maximum error is 1.45358 plus 0.5 ulps
  947. // numbers above 88.38 will flush to infinity
  948. // numbers beneath -103.97 will flush to zero
  949. inline static __m256 ggml_v_expf(__m256 x) {
  950. const __m256 r = _mm256_set1_ps(0x1.8p23f);
  951. const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
  952. const __m256 n = _mm256_sub_ps(z, r);
  953. const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f),
  954. _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
  955. const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
  956. const __m256 k = _mm256_castsi256_ps(
  957. _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1))));
  958. const __m256i c = _mm256_castps_si256(
  959. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  960. _mm256_set1_ps(126), _CMP_GT_OQ));
  961. const __m256 u = _mm256_mul_ps(b, b);
  962. const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
  963. _mm256_set1_ps(0x1.573e2ep-5f)), u,
  964. _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
  965. _mm256_set1_ps(0x1.fffdb6p-2f))),
  966. u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
  967. if (!_mm256_movemask_ps(_mm256_castsi256_ps(c)))
  968. return _mm256_fmadd_ps(j, k, k);
  969. const __m256i g = _mm256_and_si256(
  970. _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
  971. _mm256_set1_epi32(0x82000000u));
  972. const __m256 s1 =
  973. _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u)));
  974. const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
  975. const __m256i d = _mm256_castps_si256(
  976. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  977. _mm256_set1_ps(192), _CMP_GT_OQ));
  978. return _mm256_or_ps(
  979. _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
  980. _mm256_andnot_ps(
  981. _mm256_castsi256_ps(d),
  982. _mm256_or_ps(
  983. _mm256_and_ps(_mm256_castsi256_ps(c),
  984. _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
  985. _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
  986. }
  987. // computes silu x/(1+exp(-x)) in single precision vector
  988. inline static __m256 ggml_v_silu(__m256 x) {
  989. const __m256 one = _mm256_set1_ps(1);
  990. const __m256 zero = _mm256_setzero_ps();
  991. const __m256 neg_x = _mm256_sub_ps(zero, x);
  992. const __m256 exp_neg_x = ggml_v_expf(neg_x);
  993. const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x);
  994. return _mm256_div_ps(x, one_plus_exp_neg_x);
  995. }
  996. #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON
  997. #if defined(__FMA__)
  998. #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z)
  999. #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z)
  1000. #else
  1001. #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z)
  1002. #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y))
  1003. #endif
  1004. // adapted from arm limited optimized routine
  1005. // the maximum error is 1.45358 plus 0.5 ulps
  1006. // numbers above 88.38 will flush to infinity
  1007. // numbers beneath -103.97 will flush to zero
  1008. inline static __m128 ggml_v_expf(__m128 x) {
  1009. const __m128 r = _mm_set1_ps(0x1.8p23f);
  1010. const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r);
  1011. const __m128 n = _mm_sub_ps(z, r);
  1012. const __m128 b =
  1013. NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x));
  1014. const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23);
  1015. const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1))));
  1016. const __m128i c =
  1017. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126)));
  1018. const __m128 u = _mm_mul_ps(b, b);
  1019. const __m128 j =
  1020. MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u,
  1021. MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))),
  1022. u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b));
  1023. if (!_mm_movemask_epi8(c))
  1024. return MADD128(j, k, k);
  1025. const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())),
  1026. _mm_set1_epi32(0x82000000u));
  1027. const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u)));
  1028. const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g));
  1029. const __m128i d =
  1030. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192)));
  1031. return _mm_or_ps(
  1032. _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)),
  1033. _mm_andnot_ps(_mm_castsi128_ps(d),
  1034. _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)),
  1035. _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k)))));
  1036. }
  1037. // computes silu x/(1+exp(-x)) in single precision vector
  1038. inline static __m128 ggml_v_silu(__m128 x) {
  1039. const __m128 one = _mm_set1_ps(1);
  1040. const __m128 zero = _mm_setzero_ps();
  1041. const __m128 neg_x = _mm_sub_ps(zero, x);
  1042. const __m128 exp_neg_x = ggml_v_expf(neg_x);
  1043. const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x);
  1044. return _mm_div_ps(x, one_plus_exp_neg_x);
  1045. }
  1046. #elif defined(__riscv_v_intrinsic)
  1047. // adapted from arm limited optimized routine
  1048. // the maximum error is 1.45358 plus 0.5 ulps
  1049. // numbers above 88.38 will flush to infinity
  1050. // numbers beneath -103.97 will flush to zero
  1051. inline static vfloat32m2_t ggml_v_expf_m2(vfloat32m2_t x, int vl) {
  1052. const vfloat32m2_t r = __riscv_vfmv_v_f_f32m2(0x1.8p23f, vl);
  1053. #ifdef __riscv_xtheadvector
  1054. // workaround for compiler bug (gcc 14.3.0: Error: unrecognized opcode `th.vmv1r.v v2,v4')
  1055. vfloat32m2_t z = __riscv_vfadd_vf_f32m2(r, 0.0f, vl);
  1056. z = __riscv_vfmacc_vf_f32m2(z, 0x1.715476p+0f, x, vl);
  1057. #else
  1058. const vfloat32m2_t z = __riscv_vfmacc_vf_f32m2(r, 0x1.715476p+0f, x, vl);
  1059. #endif
  1060. const vfloat32m2_t n = __riscv_vfsub_vv_f32m2(z, r, vl);
  1061. const vfloat32m2_t b = __riscv_vfnmsac_vf_f32m2(__riscv_vfnmsac_vf_f32m2(x, 0x1.62e4p-1f, n, vl),
  1062. 0x1.7f7d1cp-20f, n, vl);
  1063. const vuint32m2_t e = __riscv_vsll_vx_u32m2(__riscv_vreinterpret_v_f32m2_u32m2(z), 23, vl);
  1064. const vfloat32m2_t k = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vadd_vx_u32m2(e, 0x3f800000, vl)); // 1.0f
  1065. const vbool16_t c = __riscv_vmfgt_vf_f32m2_b16(__riscv_vfabs_v_f32m2(n, vl), 126.0f, vl);
  1066. const vfloat32m2_t u = __riscv_vfmul_vv_f32m2(b, b, vl);
  1067. const vfloat32m2_t j = __riscv_vfmacc_vv_f32m2(
  1068. __riscv_vfmul_vf_f32m2(b, 0x1.ffffecp-1f, vl),
  1069. __riscv_vfmacc_vv_f32m2(
  1070. __riscv_vfmacc_vf_f32m2(__riscv_vfmv_v_f_f32m2(0x1.fffdb6p-2f, vl), 0x1.555e66p-3f, b, vl),
  1071. __riscv_vfmacc_vf_f32m2(__riscv_vfmv_v_f_f32m2(0x1.573e2ep-5f, vl), 0x1.0e4020p-7f, b, vl),
  1072. u, vl), u, vl);
  1073. if (!__riscv_vcpop_m_b16(c, vl))
  1074. return __riscv_vfmacc_vv_f32m2(k, j, k, vl);
  1075. const vbool16_t dm = __riscv_vmfle_vf_f32m2_b16(n, 0.0f, vl);
  1076. const vuint32m2_t d = __riscv_vmerge_vxm_u32m2(__riscv_vmv_v_x_u32m2(0, vl), 0x82000000, dm, vl);
  1077. const vfloat32m2_t s1 = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vadd_vx_u32m2(d, 0x7f000000, vl));
  1078. const vfloat32m2_t s2 = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vsub_vv_u32m2(e, d, vl));
  1079. const vfloat32m2_t r1 = __riscv_vmerge_vvm_f32m2(
  1080. __riscv_vfmacc_vv_f32m2(k, k, j, vl),
  1081. __riscv_vfmul_vv_f32m2(__riscv_vfmacc_vv_f32m2(s2, s2, j, vl), s1, vl),
  1082. c, vl);
  1083. return __riscv_vmerge_vvm_f32m2(
  1084. r1, __riscv_vfmul_vv_f32m2(s1, s1, vl),
  1085. __riscv_vmfgt_vf_f32m2_b16(__riscv_vfabs_v_f32m2(n, vl), 192.0f, vl),
  1086. vl);
  1087. }
  1088. // computes silu x/(1+exp(-x)) in single precision vector
  1089. inline static vfloat32m2_t ggml_v_silu_m2(vfloat32m2_t x, int vl) {
  1090. const vfloat32m2_t neg_x = __riscv_vfneg_v_f32m2(x, vl);
  1091. const vfloat32m2_t exp_neg_x = ggml_v_expf_m2(neg_x, vl);
  1092. const vfloat32m2_t one_plus_exp_neg_x = __riscv_vfadd_vf_f32m2(exp_neg_x, 1.0f, vl);
  1093. return __riscv_vfdiv_vv_f32m2(x, one_plus_exp_neg_x, vl);
  1094. }
  1095. #endif // __ARM_NEON / __AVX2__ / __SSE2__ / __riscv_v_intrinsic
  1096. inline static void ggml_vec_silu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1097. for (int i = 0; i < n; ++i) {
  1098. y[i] = ggml_silu_f16(x[i]);
  1099. }
  1100. }
  1101. inline static float ggml_silu_backward_f32(float x, float dy) {
  1102. const float s = 1.0f/(1.0f + expf(-x));
  1103. return dy*s*(1.0f + x*(1.0f - s));
  1104. }
  1105. inline static ggml_fp16_t ggml_silu_backward_f16(ggml_fp16_t x, ggml_fp16_t dy) {
  1106. const float v = GGML_CPU_FP16_TO_FP32(x);
  1107. const float s = 1.0f/(1.0f + expf(-v));
  1108. return GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(dy)*s*(1.0f + v*(1.0f - s)));
  1109. }
  1110. inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) {
  1111. for (int i = 0; i < n; ++i) {
  1112. dx[i] = ggml_silu_backward_f32(x[i], dy[i]);
  1113. }
  1114. }
  1115. 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) {
  1116. for (int i = 0; i < n; ++i) {
  1117. dx[i] = ggml_silu_backward_f16(x[i], dy[i]);
  1118. }
  1119. }
  1120. inline static void ggml_vec_reglu_f32 (const int n, float * y, const float * x, const float * g) {
  1121. for (int i = 0; i < n; ++i) {
  1122. y[i] = (x[i] > 0.f) ? x[i] * g[i] : 0.f;
  1123. }
  1124. }
  1125. inline static void ggml_vec_reglu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
  1126. for (int i = 0; i < n; ++i) {
  1127. float v = GGML_CPU_FP16_TO_FP32(x[i]);
  1128. y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v * GGML_CPU_FP16_TO_FP32(g[i]) : 0.f);
  1129. }
  1130. }
  1131. #ifdef GGML_GELU_FP16
  1132. inline static void ggml_vec_geglu_f32(const int n, float * y, const float * x, const float * g) {
  1133. uint16_t t;
  1134. for (int i = 0; i < n; ++i) {
  1135. if (x[i] <= -10.0f) {
  1136. y[i] = 0.0f;
  1137. } else if (x[i] >= 10.0f) {
  1138. y[i] = x[i] * g[i];
  1139. } else {
  1140. ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
  1141. memcpy(&t, &fp16, sizeof(uint16_t));
  1142. y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[t]) * g[i];
  1143. }
  1144. }
  1145. }
  1146. #else
  1147. inline static void ggml_vec_geglu_f32(const int n, float * y, const float * x, const float * g) {
  1148. for (int i = 0; i < n; ++i) {
  1149. y[i] = ggml_gelu_f32(x[i]) * g[i];
  1150. }
  1151. }
  1152. #endif
  1153. inline static void ggml_vec_geglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
  1154. const uint16_t * i16 = (const uint16_t *) x;
  1155. for (int i = 0; i < n; ++i) {
  1156. float v = GGML_CPU_FP16_TO_FP32(g[i]);
  1157. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[i16[i]]) * v);
  1158. }
  1159. }
  1160. void ggml_vec_swiglu_f32(const int n, float * y, const float * x, const float * g);
  1161. inline static void ggml_vec_swiglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
  1162. for (int i = 0; i < n; ++i) {
  1163. float xi = GGML_CPU_FP16_TO_FP32(x[i]);
  1164. float gi = GGML_CPU_FP16_TO_FP32(g[i]);
  1165. y[i] = GGML_CPU_FP32_TO_FP16((xi/(1.0f + expf(-xi))) * gi);
  1166. }
  1167. }
  1168. inline static void ggml_vec_geglu_erf_f32(const int n, float * y, const float * x, const float * g) {
  1169. for (int i = 0; i < n; ++i) {
  1170. float xi = x[i];
  1171. y[i] = 0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * g[i];
  1172. }
  1173. }
  1174. 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) {
  1175. for (int i = 0; i < n; ++i) {
  1176. float xi = GGML_CPU_FP16_TO_FP32(x[i]);
  1177. float gi = GGML_CPU_FP16_TO_FP32(g[i]);
  1178. y[i] = GGML_CPU_FP32_TO_FP16(0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * gi);
  1179. }
  1180. }
  1181. #ifdef GGML_GELU_QUICK_FP16
  1182. inline static void ggml_vec_geglu_quick_f32(const int n, float * y, const float * x, const float * g) {
  1183. uint16_t t;
  1184. for (int i = 0; i < n; ++i) {
  1185. ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
  1186. memcpy(&t, &fp16, sizeof(uint16_t));
  1187. y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]) * g[i];
  1188. }
  1189. }
  1190. #else
  1191. inline static void ggml_vec_geglu_quick_f32(const int n, float * y, const float * x, const float * g) {
  1192. for (int i = 0; i < n; ++i) {
  1193. y[i] = ggml_gelu_quick_f32(x[i]) * g[i];
  1194. }
  1195. }
  1196. #endif
  1197. 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) {
  1198. const uint16_t * i16 = (const uint16_t *) x;
  1199. for (int i = 0; i < n; ++i) {
  1200. float v = GGML_CPU_FP16_TO_FP32(g[i]);
  1201. y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[i16[i]]) * v);
  1202. }
  1203. }
  1204. inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
  1205. #ifndef GGML_USE_ACCELERATE
  1206. ggml_float sum = 0.0;
  1207. for (int i = 0; i < n; ++i) {
  1208. sum += (ggml_float)x[i];
  1209. }
  1210. *s = (float)sum;
  1211. #else
  1212. vDSP_sve(x, 1, s, n);
  1213. #endif
  1214. }
  1215. inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) {
  1216. ggml_float sum = 0.0;
  1217. for (int i = 0; i < n; ++i) {
  1218. sum += (ggml_float)x[i];
  1219. }
  1220. *s = sum;
  1221. }
  1222. inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) {
  1223. float sum = 0.0f;
  1224. for (int i = 0; i < n; ++i) {
  1225. sum += GGML_CPU_FP16_TO_FP32(x[i]);
  1226. }
  1227. *s = sum;
  1228. }
  1229. inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) {
  1230. float sum = 0.0f;
  1231. for (int i = 0; i < n; ++i) {
  1232. sum += GGML_BF16_TO_FP32(x[i]);
  1233. }
  1234. *s = sum;
  1235. }
  1236. inline static void ggml_vec_max_f32(const int n, float * s, const float * x) {
  1237. #ifndef GGML_USE_ACCELERATE
  1238. float max = -INFINITY;
  1239. for (int i = 0; i < n; ++i) {
  1240. max = MAX(max, x[i]);
  1241. }
  1242. *s = max;
  1243. #else
  1244. vDSP_maxv(x, 1, s, n);
  1245. #endif
  1246. }
  1247. inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) {
  1248. ggml_vec_norm_f32(n, s, x);
  1249. *s = 1.f/(*s);
  1250. }
  1251. inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
  1252. float max = -INFINITY;
  1253. int idx = 0;
  1254. for (int i = 0; i < n; ++i) {
  1255. max = MAX(max, x[i]);
  1256. if (max == x[i]) { idx = i; }
  1257. }
  1258. *s = idx;
  1259. }
  1260. #ifdef __cplusplus
  1261. }
  1262. #endif