ggml-impl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) && !defined(_MSC_VER)
  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. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  48. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  49. #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  50. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  51. __fp16 tmp;
  52. memcpy(&tmp, &h, sizeof(ggml_fp16_t));
  53. return (float)tmp;
  54. }
  55. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  56. ggml_fp16_t res;
  57. __fp16 tmp = f;
  58. memcpy(&res, &tmp, sizeof(ggml_fp16_t));
  59. return res;
  60. }
  61. #else
  62. #ifdef __wasm_simd128__
  63. #include <wasm_simd128.h>
  64. #else
  65. #ifdef __POWER9_VECTOR__
  66. #include <altivec.h>
  67. #undef bool
  68. #define bool _Bool
  69. #else
  70. #if defined(_MSC_VER) || defined(__MINGW32__)
  71. #include <intrin.h>
  72. #else
  73. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  74. #if !defined(__riscv)
  75. #include <immintrin.h>
  76. #endif
  77. #endif
  78. #endif
  79. #endif
  80. #endif
  81. #ifdef __riscv_v_intrinsic
  82. #include <riscv_vector.h>
  83. #endif
  84. #ifdef __F16C__
  85. #ifdef _MSC_VER
  86. #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
  87. #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
  88. #else
  89. #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
  90. #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
  91. #endif
  92. #elif defined(__POWER9_VECTOR__)
  93. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  94. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  95. /* the inline asm below is about 12% faster than the lookup method */
  96. #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
  97. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  98. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  99. register float f;
  100. register double d;
  101. __asm__(
  102. "mtfprd %0,%2\n"
  103. "xscvhpdp %0,%0\n"
  104. "frsp %1,%0\n" :
  105. /* temp */ "=d"(d),
  106. /* out */ "=f"(f):
  107. /* in */ "r"(h));
  108. return f;
  109. }
  110. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  111. register double d;
  112. register ggml_fp16_t r;
  113. __asm__( /* xscvdphp can work on double or single precision */
  114. "xscvdphp %0,%2\n"
  115. "mffprd %1,%0\n" :
  116. /* temp */ "=d"(d),
  117. /* out */ "=r"(r):
  118. /* in */ "f"(f));
  119. return r;
  120. }
  121. #else
  122. // FP16 <-> FP32
  123. // ref: https://github.com/Maratyszcza/FP16
  124. static inline float fp32_from_bits(uint32_t w) {
  125. union {
  126. uint32_t as_bits;
  127. float as_value;
  128. } fp32;
  129. fp32.as_bits = w;
  130. return fp32.as_value;
  131. }
  132. static inline uint32_t fp32_to_bits(float f) {
  133. union {
  134. float as_value;
  135. uint32_t as_bits;
  136. } fp32;
  137. fp32.as_value = f;
  138. return fp32.as_bits;
  139. }
  140. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  141. const uint32_t w = (uint32_t) h << 16;
  142. const uint32_t sign = w & UINT32_C(0x80000000);
  143. const uint32_t two_w = w + w;
  144. const uint32_t exp_offset = UINT32_C(0xE0) << 23;
  145. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  146. const float exp_scale = 0x1.0p-112f;
  147. #else
  148. const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
  149. #endif
  150. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  151. const uint32_t magic_mask = UINT32_C(126) << 23;
  152. const float magic_bias = 0.5f;
  153. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  154. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  155. const uint32_t result = sign |
  156. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  157. return fp32_from_bits(result);
  158. }
  159. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  160. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  161. const float scale_to_inf = 0x1.0p+112f;
  162. const float scale_to_zero = 0x1.0p-110f;
  163. #else
  164. const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
  165. const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
  166. #endif
  167. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  168. const uint32_t w = fp32_to_bits(f);
  169. const uint32_t shl1_w = w + w;
  170. const uint32_t sign = w & UINT32_C(0x80000000);
  171. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  172. if (bias < UINT32_C(0x71000000)) {
  173. bias = UINT32_C(0x71000000);
  174. }
  175. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  176. const uint32_t bits = fp32_to_bits(base);
  177. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  178. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  179. const uint32_t nonsign = exp_bits + mantissa_bits;
  180. return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
  181. }
  182. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  183. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  184. #endif // __F16C__
  185. #endif // __ARM_NEON
  186. // precomputed f32 table for f16 (256 KB)
  187. // defined in ggml.c, initialized in ggml_init()
  188. extern float ggml_table_f32_f16[1 << 16];
  189. // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
  190. // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
  191. // This is also true for POWER9.
  192. #if !defined(GGML_FP16_TO_FP32)
  193. inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
  194. uint16_t s;
  195. memcpy(&s, &f, sizeof(uint16_t));
  196. return ggml_table_f32_f16[s];
  197. }
  198. #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
  199. #endif
  200. #if !defined(GGML_FP32_TO_FP16)
  201. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  202. #endif
  203. #define GGML_HASHTABLE_FULL ((size_t)-1)
  204. #define GGML_HASHTABLE_ALREADY_EXISTS ((size_t)-2)
  205. struct ggml_hash_set ggml_hash_set_new(size_t size);
  206. bool ggml_hash_contains (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
  207. // returns GGML_HASHTABLE_FULL if table is full, otherwise the current index of the key or where it should be inserted
  208. size_t ggml_hash_find (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
  209. // returns GGML_HASHTABLE_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
  210. size_t ggml_hash_insert ( struct ggml_hash_set hash_set, struct ggml_tensor * key);
  211. // return index, asserts if table is full
  212. size_t ggml_hash_find_or_insert( struct ggml_hash_set hash_set, struct ggml_tensor * key);
  213. #ifdef __cplusplus
  214. }
  215. #endif