ggml-cpu-impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #pragma once
  2. // GGML CPU internal header
  3. #include "ggml.h"
  4. #include "ggml-impl.h"
  5. #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
  6. //#include <stddef.h>
  7. #include <stdbool.h>
  8. #include <string.h> // memcpy
  9. #include <math.h> // fabsf
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. struct ggml_compute_params {
  14. // ith = thread index, nth = number of threads
  15. int ith, nth;
  16. // work buffer for all threads
  17. size_t wsize;
  18. void * wdata;
  19. struct ggml_threadpool * threadpool;
  20. };
  21. #if defined(_MSC_VER)
  22. #define m512bh(p) p
  23. #define m512i(p) p
  24. #else
  25. #define m512bh(p) (__m512bh)(p)
  26. #define m512i(p) (__m512i)(p)
  27. #endif
  28. // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
  29. #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
  30. #ifndef __FMA__
  31. #define __FMA__
  32. #endif
  33. #ifndef __F16C__
  34. #define __F16C__
  35. #endif
  36. #endif
  37. // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
  38. #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
  39. #ifndef __SSE3__
  40. #define __SSE3__
  41. #endif
  42. #ifndef __SSSE3__
  43. #define __SSSE3__
  44. #endif
  45. #endif
  46. #if defined(__s390x__) && defined(__VEC__)
  47. #ifndef __VXE__
  48. #define __VXE__
  49. #endif // __VXE__
  50. #ifndef __VXE2__
  51. #define __VXE2__
  52. #endif // __VXE2__
  53. #endif // __s390x__ && __VEC__
  54. #if defined(__s390x__) && defined(GGML_NNPA)
  55. #ifndef __NNPA__
  56. #define __NNPA__
  57. #endif // __NNPA__
  58. #endif // __s390x__ && GGML_NNPA
  59. #if defined(__ARM_FEATURE_SVE)
  60. #include <sys/prctl.h>
  61. #endif
  62. #if defined(__ARM_NEON)
  63. // ref: https://github.com/ggml-org/llama.cpp/pull/5404
  64. #ifdef _MSC_VER
  65. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  66. #else
  67. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  68. #endif // _MSC_VER
  69. #if !defined(__aarch64__)
  70. // 32-bit ARM compatibility
  71. // vaddlvq_s16
  72. // vpaddq_s16
  73. // vpaddq_s32
  74. // vaddvq_s32
  75. // vaddvq_f32
  76. // vmaxvq_f32
  77. // vcvtnq_s32_f32
  78. // vzip1_u8
  79. // vzip2_u8
  80. inline static int32_t vaddlvq_s16(int16x8_t v) {
  81. int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
  82. return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
  83. }
  84. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  85. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  86. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  87. return vcombine_s16(a0, b0);
  88. }
  89. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  90. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  91. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  92. return vcombine_s32(a0, b0);
  93. }
  94. inline static int32_t vaddvq_s32(int32x4_t v) {
  95. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  96. }
  97. inline static float vaddvq_f32(float32x4_t v) {
  98. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  99. }
  100. inline static float vmaxvq_f32(float32x4_t v) {
  101. return
  102. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  103. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  104. }
  105. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  106. int32x4_t res;
  107. res[0] = roundf(vgetq_lane_f32(v, 0));
  108. res[1] = roundf(vgetq_lane_f32(v, 1));
  109. res[2] = roundf(vgetq_lane_f32(v, 2));
  110. res[3] = roundf(vgetq_lane_f32(v, 3));
  111. return res;
  112. }
  113. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  114. uint8x8_t res;
  115. res[0] = a[0]; res[1] = b[0];
  116. res[2] = a[1]; res[3] = b[1];
  117. res[4] = a[2]; res[5] = b[2];
  118. res[6] = a[3]; res[7] = b[3];
  119. return res;
  120. }
  121. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  122. uint8x8_t res;
  123. res[0] = a[4]; res[1] = b[4];
  124. res[2] = a[5]; res[3] = b[5];
  125. res[4] = a[6]; res[5] = b[6];
  126. res[6] = a[7]; res[7] = b[7];
  127. return res;
  128. }
  129. // vld1q_s16_x2
  130. // vld1q_u8_x2
  131. // vld1q_u8_x4
  132. // vld1q_s8_x2
  133. // vld1q_s8_x4
  134. // TODO: double-check these work correctly
  135. typedef struct ggml_int16x8x2_t {
  136. int16x8_t val[2];
  137. } ggml_int16x8x2_t;
  138. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  139. ggml_int16x8x2_t res;
  140. res.val[0] = vld1q_s16(ptr + 0);
  141. res.val[1] = vld1q_s16(ptr + 8);
  142. return res;
  143. }
  144. typedef struct ggml_uint8x16x2_t {
  145. uint8x16_t val[2];
  146. } ggml_uint8x16x2_t;
  147. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  148. ggml_uint8x16x2_t res;
  149. res.val[0] = vld1q_u8(ptr + 0);
  150. res.val[1] = vld1q_u8(ptr + 16);
  151. return res;
  152. }
  153. typedef struct ggml_uint8x16x4_t {
  154. uint8x16_t val[4];
  155. } ggml_uint8x16x4_t;
  156. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  157. ggml_uint8x16x4_t res;
  158. res.val[0] = vld1q_u8(ptr + 0);
  159. res.val[1] = vld1q_u8(ptr + 16);
  160. res.val[2] = vld1q_u8(ptr + 32);
  161. res.val[3] = vld1q_u8(ptr + 48);
  162. return res;
  163. }
  164. typedef struct ggml_int8x16x2_t {
  165. int8x16_t val[2];
  166. } ggml_int8x16x2_t;
  167. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  168. ggml_int8x16x2_t res;
  169. res.val[0] = vld1q_s8(ptr + 0);
  170. res.val[1] = vld1q_s8(ptr + 16);
  171. return res;
  172. }
  173. typedef struct ggml_int8x16x4_t {
  174. int8x16_t val[4];
  175. } ggml_int8x16x4_t;
  176. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  177. ggml_int8x16x4_t res;
  178. res.val[0] = vld1q_s8(ptr + 0);
  179. res.val[1] = vld1q_s8(ptr + 16);
  180. res.val[2] = vld1q_s8(ptr + 32);
  181. res.val[3] = vld1q_s8(ptr + 48);
  182. return res;
  183. }
  184. // NOTE: not tested
  185. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  186. int8x16_t res;
  187. res[ 0] = a[b[ 0]];
  188. res[ 1] = a[b[ 1]];
  189. res[ 2] = a[b[ 2]];
  190. res[ 3] = a[b[ 3]];
  191. res[ 4] = a[b[ 4]];
  192. res[ 5] = a[b[ 5]];
  193. res[ 6] = a[b[ 6]];
  194. res[ 7] = a[b[ 7]];
  195. res[ 8] = a[b[ 8]];
  196. res[ 9] = a[b[ 9]];
  197. res[10] = a[b[10]];
  198. res[11] = a[b[11]];
  199. res[12] = a[b[12]];
  200. res[13] = a[b[13]];
  201. res[14] = a[b[14]];
  202. res[15] = a[b[15]];
  203. return res;
  204. }
  205. // NOTE: not tested
  206. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  207. uint8x16_t res;
  208. res[ 0] = a[b[ 0]];
  209. res[ 1] = a[b[ 1]];
  210. res[ 2] = a[b[ 2]];
  211. res[ 3] = a[b[ 3]];
  212. res[ 4] = a[b[ 4]];
  213. res[ 5] = a[b[ 5]];
  214. res[ 6] = a[b[ 6]];
  215. res[ 7] = a[b[ 7]];
  216. res[ 8] = a[b[ 8]];
  217. res[ 9] = a[b[ 9]];
  218. res[10] = a[b[10]];
  219. res[11] = a[b[11]];
  220. res[12] = a[b[12]];
  221. res[13] = a[b[13]];
  222. res[14] = a[b[14]];
  223. res[15] = a[b[15]];
  224. return res;
  225. }
  226. #else
  227. #define ggml_int16x8x2_t int16x8x2_t
  228. #define ggml_uint8x16x2_t uint8x16x2_t
  229. #define ggml_uint8x16x4_t uint8x16x4_t
  230. #define ggml_int8x16x2_t int8x16x2_t
  231. #define ggml_int8x16x4_t int8x16x4_t
  232. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  233. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  234. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  235. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  236. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  237. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  238. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  239. #endif // !defined(__aarch64__)
  240. #if !defined(__ARM_FEATURE_DOTPROD)
  241. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  242. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  243. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  244. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  245. }
  246. #else
  247. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  248. #endif // !defined(__ARM_FEATURE_DOTPROD)
  249. #endif // defined(__ARM_NEON)
  250. #ifdef __wasm_simd128__
  251. #include <wasm_simd128.h>
  252. #endif
  253. #ifdef __POWER9_VECTOR__
  254. #include <altivec.h>
  255. #endif
  256. #if defined(_MSC_VER) || defined(__MINGW32__)
  257. #include <intrin.h>
  258. #elif defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
  259. #include <immintrin.h>
  260. #endif
  261. #ifdef __riscv_v_intrinsic
  262. #include <riscv_vector.h>
  263. #endif
  264. #if defined(__loongarch64)
  265. #if defined(__loongarch_asx)
  266. #include <lasxintrin.h>
  267. #endif
  268. #if defined(__loongarch_sx)
  269. #include <lsxintrin.h>
  270. #endif
  271. #endif
  272. #if defined(__VXE__) || defined(__VXE2__)
  273. #include <vecintrin.h>
  274. #define vec_neg(a) (-(a)) // Vector Negate
  275. #define vec_add(a, b) ((a) + (b)) // Vector Add
  276. #define vec_sub(a, b) ((a) - (b)) // Vector Subtract
  277. #define vec_mul(a, b) ((a) * (b)) // Vector Multiply
  278. #define vec_div(a, b) ((a) / (b)) // Vector Divide
  279. #define vec_sl(a, b) ((a) << (b)) // Vector Shift Left
  280. #define vec_sra(a, b) ((a) >> (b)) // Vector Shift Right
  281. #define vec_sr(a, b) ((a) >> (b)) // Vector Shift Right Algebraic
  282. #define vec_slo(a, b) vec_slb(a, (b) << 64) // Vector Shift Left by Octet
  283. #define vec_sro(a, b) vec_srb(a, (b) << 64) // Vector Shift Right by Octet
  284. #ifndef vec_and
  285. #define vec_and(a, b) ((a) & (b)) // Vector AND
  286. #endif
  287. #ifndef vec_or
  288. #define vec_or(a, b) ((a) | (b)) // Vector OR
  289. #endif
  290. #ifndef vec_xor
  291. #define vec_xor(a, b) ((a) ^ (b)) // Vector XOR
  292. #endif
  293. typedef signed char char8x16_t __attribute__((vector_size(16)));
  294. typedef unsigned char uchar8x16_t __attribute__((vector_size(16)));
  295. typedef int8_t int8x16_t __attribute__((vector_size(16)));
  296. typedef int16_t int16x8_t __attribute__((vector_size(16)));
  297. typedef int32_t int32x4_t __attribute__((vector_size(16)));
  298. typedef uint8_t uint8x16_t __attribute__((vector_size(16)));
  299. typedef uint16_t uint16x8_t __attribute__((vector_size(16)));
  300. typedef uint32_t uint32x4_t __attribute__((vector_size(16)));
  301. typedef float float32x4_t __attribute__((vector_size(16)));
  302. typedef double double64x2_t __attribute__((vector_size(16)));
  303. typedef signed long long long64x2_t __attribute__((vector_size(16)));
  304. typedef unsigned long long ulong64x2_t __attribute__((vector_size(16)));
  305. typedef struct ggml_uint8x16x2_t {
  306. uint8x16_t val[2];
  307. } ggml_uint8x16x2_t;
  308. inline static ggml_uint8x16x2_t ggml_vec_xl_u8x2(const uint8_t * ptr) {
  309. ggml_uint8x16x2_t res;
  310. res.val[0] = vec_xl( 0, ptr);
  311. res.val[1] = vec_xl(16, ptr);
  312. return res;
  313. }
  314. typedef struct ggml_uint8x16x4_t {
  315. uint8x16_t val[4];
  316. } ggml_uint8x16x4_t;
  317. inline static ggml_uint8x16x4_t ggml_vec_xl_u8x4(const uint8_t * ptr) {
  318. ggml_uint8x16x4_t res;
  319. res.val[0] = vec_xl( 0, ptr);
  320. res.val[1] = vec_xl(16, ptr);
  321. res.val[2] = vec_xl(32, ptr);
  322. res.val[3] = vec_xl(48, ptr);
  323. return res;
  324. }
  325. typedef struct ggml_int8x16x4_t {
  326. int8x16_t val[4];
  327. } ggml_int8x16x4_t;
  328. inline static ggml_int8x16x4_t ggml_vec_xl_s8x4(const int8_t * ptr) {
  329. ggml_int8x16x4_t res;
  330. res.val[0] = vec_xl( 0, ptr);
  331. res.val[1] = vec_xl(16, ptr);
  332. res.val[2] = vec_xl(32, ptr);
  333. res.val[3] = vec_xl(48, ptr);
  334. return res;
  335. }
  336. typedef struct ggml_int16x8x2_t {
  337. int16x8_t val[2];
  338. } ggml_int16x8x2_t;
  339. inline static ggml_int16x8x2_t ggml_vec_xl_s16x2(const int16_t * ptr) {
  340. ggml_int16x8x2_t res;
  341. res.val[0] = vec_xl( 0, ptr);
  342. res.val[1] = vec_xl(16, ptr);
  343. return res;
  344. }
  345. /*
  346. ! WARNING: Very slow. Use vec_perm if possible. Refer to iq4_xs
  347. ! or iq4_nl for example implementation.
  348. */
  349. inline static int8x16_t ggml_vec_tbl(int8x16_t a, uint8x16_t b) {
  350. int8x16_t res;
  351. res[ 0] = a[b[ 0]];
  352. res[ 1] = a[b[ 1]];
  353. res[ 2] = a[b[ 2]];
  354. res[ 3] = a[b[ 3]];
  355. res[ 4] = a[b[ 4]];
  356. res[ 5] = a[b[ 5]];
  357. res[ 6] = a[b[ 6]];
  358. res[ 7] = a[b[ 7]];
  359. res[ 8] = a[b[ 8]];
  360. res[ 9] = a[b[ 9]];
  361. res[10] = a[b[10]];
  362. res[11] = a[b[11]];
  363. res[12] = a[b[12]];
  364. res[13] = a[b[13]];
  365. res[14] = a[b[14]];
  366. res[15] = a[b[15]];
  367. return res;
  368. }
  369. inline static int16x8_t vec_padd_s16(int16x8_t a, int16x8_t b) {
  370. const uchar8x16_t v_maske = { 0, 1, 4, 5, 8, 9, 12, 13,
  371. 16, 17, 20, 21, 24, 25, 28, 29 };
  372. const int16x8_t v_abo = vec_pack((int32x4_t)a, (int32x4_t)b);
  373. const int16x8_t v_abe = vec_perm(a, b, v_maske);
  374. return v_abo + v_abe;
  375. }
  376. inline static int32x4_t ggml_vec_dot(int32x4_t acc, int8x16_t a, int8x16_t b) {
  377. const int16x8_t p = vec_mule(a, b) + vec_mulo(a, b);
  378. return acc + (vec_unpackh(p) + vec_unpackl(p));
  379. }
  380. #endif
  381. #if defined(__loongarch_asx)
  382. /* float type data load instructions */
  383. static __m128 __lsx_vreplfr2vr_s(const float val) {
  384. v4f32 res = {val, val, val, val};
  385. return (__m128)res;
  386. }
  387. static __m256 __lasx_xvreplfr2vr_s(const float val) {
  388. v8f32 res = {val, val, val, val, val, val, val, val};
  389. return (__m256)res;
  390. }
  391. #endif
  392. // TODO: move to ggml-threading
  393. void ggml_barrier(struct ggml_threadpool * tp);
  394. void ggml_threadpool_chunk_set(struct ggml_threadpool * tp, int value);
  395. int ggml_threadpool_chunk_add(struct ggml_threadpool * tp, int value);
  396. #ifdef __cplusplus
  397. }
  398. #endif