ggml-impl.h 7.9 KB

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