ggml-impl.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #pragma once
  2. #include "ggml.h"
  3. // GGML internal header
  4. #include <assert.h>
  5. #include <stddef.h>
  6. #include <stdbool.h>
  7. #include <string.h> // memcpy
  8. #include <math.h> // fabsf
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. // static_assert should be a #define, but if it's not,
  13. // fall back to the _Static_assert C11 keyword.
  14. // if C99 - static_assert is noop
  15. // ref: https://stackoverflow.com/a/53923785/4039976
  16. #ifndef static_assert
  17. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  18. #define static_assert(cond, msg) _Static_assert(cond, msg)
  19. #else
  20. #define static_assert(cond, msg) struct global_scope_noop_trick
  21. #endif
  22. #endif
  23. // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
  24. #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
  25. #ifndef __FMA__
  26. #define __FMA__
  27. #endif
  28. #ifndef __F16C__
  29. #define __F16C__
  30. #endif
  31. #ifndef __SSE3__
  32. #define __SSE3__
  33. #endif
  34. #endif
  35. #undef MIN
  36. #undef MAX
  37. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  38. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  39. // 16-bit float
  40. // on Arm, we use __fp16
  41. // on x86, we use uint16_t
  42. #if defined(__ARM_NEON) && !defined(_MSC_VER)
  43. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  44. //
  45. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  46. //
  47. #include <arm_neon.h>
  48. #define GGML_COMPUTE_FP16_TO_FP32(x) ((float) (x))
  49. #define GGML_COMPUTE_FP32_TO_FP16(x) (x)
  50. #define GGML_FP16_TO_FP32(x) ((float) (x))
  51. #define GGML_FP32_TO_FP16(x) (x)
  52. #else
  53. #ifdef __wasm_simd128__
  54. #include <wasm_simd128.h>
  55. #else
  56. #ifdef __POWER9_VECTOR__
  57. #include <altivec.h>
  58. #undef bool
  59. #define bool _Bool
  60. #else
  61. #if defined(_MSC_VER) || defined(__MINGW32__)
  62. #include <intrin.h>
  63. #else
  64. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  65. #if !defined(__riscv)
  66. #include <immintrin.h>
  67. #endif
  68. #endif
  69. #endif
  70. #endif
  71. #endif
  72. #ifdef __riscv_v_intrinsic
  73. #include <riscv_vector.h>
  74. #endif
  75. #ifdef __F16C__
  76. #ifdef _MSC_VER
  77. #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
  78. #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
  79. #else
  80. #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
  81. #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
  82. #endif
  83. #elif defined(__POWER9_VECTOR__)
  84. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  85. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  86. /* the inline asm below is about 12% faster than the lookup method */
  87. #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
  88. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  89. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  90. register float f;
  91. register double d;
  92. __asm__(
  93. "mtfprd %0,%2\n"
  94. "xscvhpdp %0,%0\n"
  95. "frsp %1,%0\n" :
  96. /* temp */ "=d"(d),
  97. /* out */ "=f"(f):
  98. /* in */ "r"(h));
  99. return f;
  100. }
  101. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  102. register double d;
  103. register ggml_fp16_t r;
  104. __asm__( /* xscvdphp can work on double or single precision */
  105. "xscvdphp %0,%2\n"
  106. "mffprd %1,%0\n" :
  107. /* temp */ "=d"(d),
  108. /* out */ "=r"(r):
  109. /* in */ "f"(f));
  110. return r;
  111. }
  112. #else
  113. // FP16 <-> FP32
  114. // ref: https://github.com/Maratyszcza/FP16
  115. static inline float fp32_from_bits(uint32_t w) {
  116. union {
  117. uint32_t as_bits;
  118. float as_value;
  119. } fp32;
  120. fp32.as_bits = w;
  121. return fp32.as_value;
  122. }
  123. static inline uint32_t fp32_to_bits(float f) {
  124. union {
  125. float as_value;
  126. uint32_t as_bits;
  127. } fp32;
  128. fp32.as_value = f;
  129. return fp32.as_bits;
  130. }
  131. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  132. const uint32_t w = (uint32_t) h << 16;
  133. const uint32_t sign = w & UINT32_C(0x80000000);
  134. const uint32_t two_w = w + w;
  135. const uint32_t exp_offset = UINT32_C(0xE0) << 23;
  136. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  137. const float exp_scale = 0x1.0p-112f;
  138. #else
  139. const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
  140. #endif
  141. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  142. const uint32_t magic_mask = UINT32_C(126) << 23;
  143. const float magic_bias = 0.5f;
  144. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  145. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  146. const uint32_t result = sign |
  147. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  148. return fp32_from_bits(result);
  149. }
  150. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  151. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  152. const float scale_to_inf = 0x1.0p+112f;
  153. const float scale_to_zero = 0x1.0p-110f;
  154. #else
  155. const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
  156. const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
  157. #endif
  158. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  159. const uint32_t w = fp32_to_bits(f);
  160. const uint32_t shl1_w = w + w;
  161. const uint32_t sign = w & UINT32_C(0x80000000);
  162. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  163. if (bias < UINT32_C(0x71000000)) {
  164. bias = UINT32_C(0x71000000);
  165. }
  166. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  167. const uint32_t bits = fp32_to_bits(base);
  168. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  169. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  170. const uint32_t nonsign = exp_bits + mantissa_bits;
  171. return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
  172. }
  173. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  174. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  175. #endif // __F16C__
  176. #endif // __ARM_NEON
  177. // precomputed f32 table for f16 (256 KB)
  178. // defined in ggml.c, initialized in ggml_init()
  179. extern float ggml_table_f32_f16[1 << 16];
  180. // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
  181. // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
  182. // This is also true for POWER9.
  183. #if !defined(GGML_FP16_TO_FP32) || !defined(GGML_FP32_TO_FP16)
  184. inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
  185. uint16_t s;
  186. memcpy(&s, &f, sizeof(uint16_t));
  187. return ggml_table_f32_f16[s];
  188. }
  189. #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
  190. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  191. #endif
  192. #define GGML_HASHTABLE_FULL ((size_t)-1)
  193. #define GGML_HASHTABLE_ALREADY_EXISTS ((size_t)-2)
  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_HAHSHTABLE_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