ggml-impl.h 7.4 KB

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