ggml-impl.h 7.2 KB

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