ggml-impl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #pragma once
  2. #include "ggml.h"
  3. // GGML internal header
  4. #include <assert.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. // static_assert should be a #define, but if it's not,
  14. // fall back to the _Static_assert C11 keyword.
  15. // if C99 - static_assert is noop
  16. // ref: https://stackoverflow.com/a/53923785/4039976
  17. #ifndef __cplusplus
  18. #ifndef static_assert
  19. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  20. #define static_assert(cond, msg) _Static_assert(cond, msg)
  21. #else
  22. #define static_assert(cond, msg) struct global_scope_noop_trick
  23. #endif
  24. #endif
  25. #endif
  26. // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
  27. #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
  28. #ifndef __FMA__
  29. #define __FMA__
  30. #endif
  31. #ifndef __F16C__
  32. #define __F16C__
  33. #endif
  34. #ifndef __SSE3__
  35. #define __SSE3__
  36. #endif
  37. #endif
  38. // 16-bit float
  39. // on Arm, we use __fp16
  40. // on x86, we use uint16_t
  41. #if defined(__ARM_NEON)
  42. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  43. //
  44. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  45. //
  46. #include <arm_neon.h>
  47. #ifdef _MSC_VER
  48. typedef uint16_t ggml_fp16_internal_t;
  49. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  50. #else
  51. typedef __fp16 ggml_fp16_internal_t;
  52. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  53. #endif // _MSC_VER
  54. #if !defined(__aarch64__)
  55. // 32-bit ARM compatibility
  56. // vaddvq_s16
  57. // vpaddq_s16
  58. // vpaddq_s32
  59. // vaddvq_s32
  60. // vaddvq_f32
  61. // vmaxvq_f32
  62. // vcvtnq_s32_f32
  63. // vzip1_u8
  64. // vzip2_u8
  65. inline static int32_t vaddvq_s16(int16x8_t v) {
  66. return
  67. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  68. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  69. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  70. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  71. }
  72. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  73. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  74. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  75. return vcombine_s16(a0, b0);
  76. }
  77. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  78. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  79. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  80. return vcombine_s32(a0, b0);
  81. }
  82. inline static int32_t vaddvq_s32(int32x4_t v) {
  83. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  84. }
  85. inline static float vaddvq_f32(float32x4_t v) {
  86. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  87. }
  88. inline static float vmaxvq_f32(float32x4_t v) {
  89. return
  90. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  91. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  92. }
  93. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  94. int32x4_t res;
  95. res[0] = roundf(vgetq_lane_f32(v, 0));
  96. res[1] = roundf(vgetq_lane_f32(v, 1));
  97. res[2] = roundf(vgetq_lane_f32(v, 2));
  98. res[3] = roundf(vgetq_lane_f32(v, 3));
  99. return res;
  100. }
  101. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  102. uint8x8_t res;
  103. res[0] = a[0]; res[1] = b[0];
  104. res[2] = a[1]; res[3] = b[1];
  105. res[4] = a[2]; res[5] = b[2];
  106. res[6] = a[3]; res[7] = b[3];
  107. return res;
  108. }
  109. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  110. uint8x8_t res;
  111. res[0] = a[4]; res[1] = b[4];
  112. res[2] = a[5]; res[3] = b[5];
  113. res[4] = a[6]; res[5] = b[6];
  114. res[6] = a[7]; res[7] = b[7];
  115. return res;
  116. }
  117. // vld1q_s16_x2
  118. // vld1q_u8_x2
  119. // vld1q_u8_x4
  120. // vld1q_s8_x2
  121. // vld1q_s8_x4
  122. // TODO: double-check these work correctly
  123. typedef struct ggml_int16x8x2_t {
  124. int16x8_t val[2];
  125. } ggml_int16x8x2_t;
  126. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  127. ggml_int16x8x2_t res;
  128. res.val[0] = vld1q_s16(ptr + 0);
  129. res.val[1] = vld1q_s16(ptr + 8);
  130. return res;
  131. }
  132. typedef struct ggml_uint8x16x2_t {
  133. uint8x16_t val[2];
  134. } ggml_uint8x16x2_t;
  135. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  136. ggml_uint8x16x2_t res;
  137. res.val[0] = vld1q_u8(ptr + 0);
  138. res.val[1] = vld1q_u8(ptr + 16);
  139. return res;
  140. }
  141. typedef struct ggml_uint8x16x4_t {
  142. uint8x16_t val[4];
  143. } ggml_uint8x16x4_t;
  144. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  145. ggml_uint8x16x4_t res;
  146. res.val[0] = vld1q_u8(ptr + 0);
  147. res.val[1] = vld1q_u8(ptr + 16);
  148. res.val[2] = vld1q_u8(ptr + 32);
  149. res.val[3] = vld1q_u8(ptr + 48);
  150. return res;
  151. }
  152. typedef struct ggml_int8x16x2_t {
  153. int8x16_t val[2];
  154. } ggml_int8x16x2_t;
  155. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  156. ggml_int8x16x2_t res;
  157. res.val[0] = vld1q_s8(ptr + 0);
  158. res.val[1] = vld1q_s8(ptr + 16);
  159. return res;
  160. }
  161. typedef struct ggml_int8x16x4_t {
  162. int8x16_t val[4];
  163. } ggml_int8x16x4_t;
  164. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  165. ggml_int8x16x4_t res;
  166. res.val[0] = vld1q_s8(ptr + 0);
  167. res.val[1] = vld1q_s8(ptr + 16);
  168. res.val[2] = vld1q_s8(ptr + 32);
  169. res.val[3] = vld1q_s8(ptr + 48);
  170. return res;
  171. }
  172. // NOTE: not tested
  173. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  174. int8x16_t res;
  175. res[ 0] = a[b[ 0]];
  176. res[ 1] = a[b[ 1]];
  177. res[ 2] = a[b[ 2]];
  178. res[ 3] = a[b[ 3]];
  179. res[ 4] = a[b[ 4]];
  180. res[ 5] = a[b[ 5]];
  181. res[ 6] = a[b[ 6]];
  182. res[ 7] = a[b[ 7]];
  183. res[ 8] = a[b[ 8]];
  184. res[ 9] = a[b[ 9]];
  185. res[10] = a[b[10]];
  186. res[11] = a[b[11]];
  187. res[12] = a[b[12]];
  188. res[13] = a[b[13]];
  189. res[14] = a[b[14]];
  190. res[15] = a[b[15]];
  191. return res;
  192. }
  193. // NOTE: not tested
  194. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  195. uint8x16_t res;
  196. res[ 0] = a[b[ 0]];
  197. res[ 1] = a[b[ 1]];
  198. res[ 2] = a[b[ 2]];
  199. res[ 3] = a[b[ 3]];
  200. res[ 4] = a[b[ 4]];
  201. res[ 5] = a[b[ 5]];
  202. res[ 6] = a[b[ 6]];
  203. res[ 7] = a[b[ 7]];
  204. res[ 8] = a[b[ 8]];
  205. res[ 9] = a[b[ 9]];
  206. res[10] = a[b[10]];
  207. res[11] = a[b[11]];
  208. res[12] = a[b[12]];
  209. res[13] = a[b[13]];
  210. res[14] = a[b[14]];
  211. res[15] = a[b[15]];
  212. return res;
  213. }
  214. #else
  215. #define ggml_int16x8x2_t int16x8x2_t
  216. #define ggml_uint8x16x2_t uint8x16x2_t
  217. #define ggml_uint8x16x4_t uint8x16x4_t
  218. #define ggml_int8x16x2_t int8x16x2_t
  219. #define ggml_int8x16x4_t int8x16x4_t
  220. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  221. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  222. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  223. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  224. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  225. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  226. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  227. #endif // !defined(__aarch64__)
  228. #if !defined(__ARM_FEATURE_DOTPROD)
  229. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  230. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  231. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  232. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  233. }
  234. #else
  235. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  236. #endif // !defined(__ARM_FEATURE_DOTPROD)
  237. #endif // defined(__ARM_NEON)
  238. #if defined(__ARM_NEON) && !defined(__MSC_VER)
  239. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  240. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  241. #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  242. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  243. ggml_fp16_internal_t tmp;
  244. memcpy(&tmp, &h, sizeof(ggml_fp16_t));
  245. return (float)tmp;
  246. }
  247. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  248. ggml_fp16_t res;
  249. ggml_fp16_internal_t tmp = f;
  250. memcpy(&res, &tmp, sizeof(ggml_fp16_t));
  251. return res;
  252. }
  253. #else
  254. #ifdef __wasm_simd128__
  255. #include <wasm_simd128.h>
  256. #else
  257. #ifdef __POWER9_VECTOR__
  258. #include <altivec.h>
  259. #undef bool
  260. #define bool _Bool
  261. #else
  262. #if defined(_MSC_VER) || defined(__MINGW32__)
  263. #include <intrin.h>
  264. #else
  265. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
  266. #if !defined(__riscv)
  267. #include <immintrin.h>
  268. #endif
  269. #endif
  270. #endif
  271. #endif
  272. #endif
  273. #ifdef __riscv_v_intrinsic
  274. #include <riscv_vector.h>
  275. #endif
  276. #ifdef __F16C__
  277. #ifdef _MSC_VER
  278. #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
  279. #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
  280. #else
  281. #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
  282. #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
  283. #endif
  284. #elif defined(__POWER9_VECTOR__)
  285. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  286. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  287. /* the inline asm below is about 12% faster than the lookup method */
  288. #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
  289. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  290. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  291. register float f;
  292. register double d;
  293. __asm__(
  294. "mtfprd %0,%2\n"
  295. "xscvhpdp %0,%0\n"
  296. "frsp %1,%0\n" :
  297. /* temp */ "=d"(d),
  298. /* out */ "=f"(f):
  299. /* in */ "r"(h));
  300. return f;
  301. }
  302. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  303. register double d;
  304. register ggml_fp16_t r;
  305. __asm__( /* xscvdphp can work on double or single precision */
  306. "xscvdphp %0,%2\n"
  307. "mffprd %1,%0\n" :
  308. /* temp */ "=d"(d),
  309. /* out */ "=r"(r):
  310. /* in */ "f"(f));
  311. return r;
  312. }
  313. #else
  314. // FP16 <-> FP32
  315. // ref: https://github.com/Maratyszcza/FP16
  316. static inline float fp32_from_bits(uint32_t w) {
  317. union {
  318. uint32_t as_bits;
  319. float as_value;
  320. } fp32;
  321. fp32.as_bits = w;
  322. return fp32.as_value;
  323. }
  324. static inline uint32_t fp32_to_bits(float f) {
  325. union {
  326. float as_value;
  327. uint32_t as_bits;
  328. } fp32;
  329. fp32.as_value = f;
  330. return fp32.as_bits;
  331. }
  332. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  333. const uint32_t w = (uint32_t) h << 16;
  334. const uint32_t sign = w & UINT32_C(0x80000000);
  335. const uint32_t two_w = w + w;
  336. const uint32_t exp_offset = UINT32_C(0xE0) << 23;
  337. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  338. const float exp_scale = 0x1.0p-112f;
  339. #else
  340. const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
  341. #endif
  342. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  343. const uint32_t magic_mask = UINT32_C(126) << 23;
  344. const float magic_bias = 0.5f;
  345. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  346. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  347. const uint32_t result = sign |
  348. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  349. return fp32_from_bits(result);
  350. }
  351. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  352. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  353. const float scale_to_inf = 0x1.0p+112f;
  354. const float scale_to_zero = 0x1.0p-110f;
  355. #else
  356. const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
  357. const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
  358. #endif
  359. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  360. const uint32_t w = fp32_to_bits(f);
  361. const uint32_t shl1_w = w + w;
  362. const uint32_t sign = w & UINT32_C(0x80000000);
  363. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  364. if (bias < UINT32_C(0x71000000)) {
  365. bias = UINT32_C(0x71000000);
  366. }
  367. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  368. const uint32_t bits = fp32_to_bits(base);
  369. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  370. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  371. const uint32_t nonsign = exp_bits + mantissa_bits;
  372. return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
  373. }
  374. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  375. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  376. #endif // __F16C__
  377. #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
  378. // precomputed f32 table for f16 (256 KB)
  379. // defined in ggml.c, initialized in ggml_init()
  380. extern float ggml_table_f32_f16[1 << 16];
  381. // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
  382. // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
  383. // This is also true for POWER9.
  384. #if !defined(GGML_FP16_TO_FP32)
  385. inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
  386. uint16_t s;
  387. memcpy(&s, &f, sizeof(uint16_t));
  388. return ggml_table_f32_f16[s];
  389. }
  390. #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
  391. #endif
  392. #if !defined(GGML_FP32_TO_FP16)
  393. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  394. #endif
  395. #define GGML_HASHTABLE_FULL ((size_t)-1)
  396. #define GGML_HASHTABLE_ALREADY_EXISTS ((size_t)-2)
  397. struct ggml_hash_set ggml_hash_set_new(size_t size);
  398. bool ggml_hash_contains (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
  399. // returns GGML_HASHTABLE_FULL if table is full, otherwise the current index of the key or where it should be inserted
  400. size_t ggml_hash_find (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
  401. // returns GGML_HASHTABLE_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
  402. size_t ggml_hash_insert ( struct ggml_hash_set hash_set, struct ggml_tensor * key);
  403. // return index, asserts if table is full
  404. size_t ggml_hash_find_or_insert( struct ggml_hash_set hash_set, struct ggml_tensor * key);
  405. #ifdef __cplusplus
  406. }
  407. #endif