ggml-impl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. #undef MIN
  11. #undef MAX
  12. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  13. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  14. /**
  15. * Converts brain16 to float32.
  16. *
  17. * The bfloat16 floating point format has the following structure:
  18. *
  19. * ┌sign
  20. * │
  21. * │ ┌exponent
  22. * │ │
  23. * │ │ ┌mantissa
  24. * │ │ │
  25. * │┌──┴───┐┌─┴───┐
  26. * 0b0000000000000000 brain16
  27. *
  28. * Since bf16 has the same number of exponent bits as a 32bit float,
  29. * encoding and decoding numbers becomes relatively straightforward.
  30. *
  31. * ┌sign
  32. * │
  33. * │ ┌exponent
  34. * │ │
  35. * │ │ ┌mantissa
  36. * │ │ │
  37. * │┌──┴───┐┌─┴───────────────────┐
  38. * 0b00000000000000000000000000000000 IEEE binary32
  39. *
  40. * For comparison, the standard fp16 format has fewer exponent bits.
  41. *
  42. * ┌sign
  43. * │
  44. * │ ┌exponent
  45. * │ │
  46. * │ │ ┌mantissa
  47. * │ │ │
  48. * │┌─┴─┐┌─┴──────┐
  49. * 0b0000000000000000 IEEE binary16
  50. *
  51. * @see IEEE 754-2008
  52. */
  53. static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {
  54. union {
  55. float f;
  56. uint32_t i;
  57. } u;
  58. u.i = (uint32_t)h.bits << 16;
  59. return u.f;
  60. }
  61. /**
  62. * Converts float32 to brain16.
  63. *
  64. * This function is binary identical to AMD Zen4 VCVTNEPS2BF16.
  65. * Subnormals shall be flushed to zero, and NANs will be quiet.
  66. * This code should vectorize nicely if using modern compilers.
  67. */
  68. static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
  69. ggml_bf16_t h;
  70. union {
  71. float f;
  72. uint32_t i;
  73. } u;
  74. u.f = s;
  75. if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
  76. h.bits = (u.i >> 16) | 64; /* force to quiet */
  77. return h;
  78. }
  79. if (!(u.i & 0x7f800000)) { /* subnormal */
  80. h.bits = (u.i & 0x80000000) >> 16; /* flush to zero */
  81. return h;
  82. }
  83. h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
  84. return h;
  85. }
  86. #define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)
  87. #define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)
  88. #ifdef __cplusplus
  89. extern "C" {
  90. #endif
  91. // static_assert should be a #define, but if it's not,
  92. // fall back to the _Static_assert C11 keyword.
  93. // if C99 - static_assert is noop
  94. // ref: https://stackoverflow.com/a/53923785/4039976
  95. #ifndef __cplusplus
  96. #ifndef static_assert
  97. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  98. #define static_assert(cond, msg) _Static_assert(cond, msg)
  99. #else
  100. #define static_assert(cond, msg) struct global_scope_noop_trick
  101. #endif
  102. #endif
  103. #endif
  104. // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
  105. #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
  106. #ifndef __FMA__
  107. #define __FMA__
  108. #endif
  109. #ifndef __F16C__
  110. #define __F16C__
  111. #endif
  112. #endif
  113. // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
  114. #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
  115. #ifndef __SSE3__
  116. #define __SSE3__
  117. #endif
  118. #ifndef __SSSE3__
  119. #define __SSSE3__
  120. #endif
  121. #endif
  122. // 16-bit float
  123. // on Arm, we use __fp16
  124. // on x86, we use uint16_t
  125. #if defined(__ARM_NEON)
  126. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  127. //
  128. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  129. //
  130. #include <arm_neon.h>
  131. #ifdef _MSC_VER
  132. typedef uint16_t ggml_fp16_internal_t;
  133. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  134. #else
  135. typedef __fp16 ggml_fp16_internal_t;
  136. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  137. #endif // _MSC_VER
  138. #if !defined(__aarch64__)
  139. // 32-bit ARM compatibility
  140. // vaddvq_s16
  141. // vpaddq_s16
  142. // vpaddq_s32
  143. // vaddvq_s32
  144. // vaddvq_f32
  145. // vmaxvq_f32
  146. // vcvtnq_s32_f32
  147. // vzip1_u8
  148. // vzip2_u8
  149. inline static int32_t vaddvq_s16(int16x8_t v) {
  150. return
  151. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  152. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  153. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  154. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  155. }
  156. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  157. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  158. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  159. return vcombine_s16(a0, b0);
  160. }
  161. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  162. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  163. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  164. return vcombine_s32(a0, b0);
  165. }
  166. inline static int32_t vaddvq_s32(int32x4_t v) {
  167. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  168. }
  169. inline static float vaddvq_f32(float32x4_t v) {
  170. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  171. }
  172. inline static float vmaxvq_f32(float32x4_t v) {
  173. return
  174. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  175. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  176. }
  177. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  178. int32x4_t res;
  179. res[0] = roundf(vgetq_lane_f32(v, 0));
  180. res[1] = roundf(vgetq_lane_f32(v, 1));
  181. res[2] = roundf(vgetq_lane_f32(v, 2));
  182. res[3] = roundf(vgetq_lane_f32(v, 3));
  183. return res;
  184. }
  185. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  186. uint8x8_t res;
  187. res[0] = a[0]; res[1] = b[0];
  188. res[2] = a[1]; res[3] = b[1];
  189. res[4] = a[2]; res[5] = b[2];
  190. res[6] = a[3]; res[7] = b[3];
  191. return res;
  192. }
  193. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  194. uint8x8_t res;
  195. res[0] = a[4]; res[1] = b[4];
  196. res[2] = a[5]; res[3] = b[5];
  197. res[4] = a[6]; res[5] = b[6];
  198. res[6] = a[7]; res[7] = b[7];
  199. return res;
  200. }
  201. // vld1q_s16_x2
  202. // vld1q_u8_x2
  203. // vld1q_u8_x4
  204. // vld1q_s8_x2
  205. // vld1q_s8_x4
  206. // TODO: double-check these work correctly
  207. typedef struct ggml_int16x8x2_t {
  208. int16x8_t val[2];
  209. } ggml_int16x8x2_t;
  210. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  211. ggml_int16x8x2_t res;
  212. res.val[0] = vld1q_s16(ptr + 0);
  213. res.val[1] = vld1q_s16(ptr + 8);
  214. return res;
  215. }
  216. typedef struct ggml_uint8x16x2_t {
  217. uint8x16_t val[2];
  218. } ggml_uint8x16x2_t;
  219. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  220. ggml_uint8x16x2_t res;
  221. res.val[0] = vld1q_u8(ptr + 0);
  222. res.val[1] = vld1q_u8(ptr + 16);
  223. return res;
  224. }
  225. typedef struct ggml_uint8x16x4_t {
  226. uint8x16_t val[4];
  227. } ggml_uint8x16x4_t;
  228. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  229. ggml_uint8x16x4_t res;
  230. res.val[0] = vld1q_u8(ptr + 0);
  231. res.val[1] = vld1q_u8(ptr + 16);
  232. res.val[2] = vld1q_u8(ptr + 32);
  233. res.val[3] = vld1q_u8(ptr + 48);
  234. return res;
  235. }
  236. typedef struct ggml_int8x16x2_t {
  237. int8x16_t val[2];
  238. } ggml_int8x16x2_t;
  239. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  240. ggml_int8x16x2_t res;
  241. res.val[0] = vld1q_s8(ptr + 0);
  242. res.val[1] = vld1q_s8(ptr + 16);
  243. return res;
  244. }
  245. typedef struct ggml_int8x16x4_t {
  246. int8x16_t val[4];
  247. } ggml_int8x16x4_t;
  248. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  249. ggml_int8x16x4_t res;
  250. res.val[0] = vld1q_s8(ptr + 0);
  251. res.val[1] = vld1q_s8(ptr + 16);
  252. res.val[2] = vld1q_s8(ptr + 32);
  253. res.val[3] = vld1q_s8(ptr + 48);
  254. return res;
  255. }
  256. // NOTE: not tested
  257. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  258. int8x16_t res;
  259. res[ 0] = a[b[ 0]];
  260. res[ 1] = a[b[ 1]];
  261. res[ 2] = a[b[ 2]];
  262. res[ 3] = a[b[ 3]];
  263. res[ 4] = a[b[ 4]];
  264. res[ 5] = a[b[ 5]];
  265. res[ 6] = a[b[ 6]];
  266. res[ 7] = a[b[ 7]];
  267. res[ 8] = a[b[ 8]];
  268. res[ 9] = a[b[ 9]];
  269. res[10] = a[b[10]];
  270. res[11] = a[b[11]];
  271. res[12] = a[b[12]];
  272. res[13] = a[b[13]];
  273. res[14] = a[b[14]];
  274. res[15] = a[b[15]];
  275. return res;
  276. }
  277. // NOTE: not tested
  278. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  279. uint8x16_t res;
  280. res[ 0] = a[b[ 0]];
  281. res[ 1] = a[b[ 1]];
  282. res[ 2] = a[b[ 2]];
  283. res[ 3] = a[b[ 3]];
  284. res[ 4] = a[b[ 4]];
  285. res[ 5] = a[b[ 5]];
  286. res[ 6] = a[b[ 6]];
  287. res[ 7] = a[b[ 7]];
  288. res[ 8] = a[b[ 8]];
  289. res[ 9] = a[b[ 9]];
  290. res[10] = a[b[10]];
  291. res[11] = a[b[11]];
  292. res[12] = a[b[12]];
  293. res[13] = a[b[13]];
  294. res[14] = a[b[14]];
  295. res[15] = a[b[15]];
  296. return res;
  297. }
  298. #else
  299. #define ggml_int16x8x2_t int16x8x2_t
  300. #define ggml_uint8x16x2_t uint8x16x2_t
  301. #define ggml_uint8x16x4_t uint8x16x4_t
  302. #define ggml_int8x16x2_t int8x16x2_t
  303. #define ggml_int8x16x4_t int8x16x4_t
  304. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  305. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  306. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  307. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  308. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  309. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  310. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  311. #endif // !defined(__aarch64__)
  312. #if !defined(__ARM_FEATURE_DOTPROD)
  313. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  314. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  315. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  316. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  317. }
  318. #else
  319. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  320. #endif // !defined(__ARM_FEATURE_DOTPROD)
  321. #endif // defined(__ARM_NEON)
  322. #if defined(__ARM_NEON) && !defined(_MSC_VER)
  323. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  324. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  325. #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  326. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  327. ggml_fp16_internal_t tmp;
  328. memcpy(&tmp, &h, sizeof(ggml_fp16_t));
  329. return (float)tmp;
  330. }
  331. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  332. ggml_fp16_t res;
  333. ggml_fp16_internal_t tmp = f;
  334. memcpy(&res, &tmp, sizeof(ggml_fp16_t));
  335. return res;
  336. }
  337. #else
  338. #ifdef __wasm_simd128__
  339. #include <wasm_simd128.h>
  340. #else
  341. #ifdef __POWER9_VECTOR__
  342. #include <altivec.h>
  343. #undef bool
  344. #define bool _Bool
  345. #else
  346. #if defined(_MSC_VER) || defined(__MINGW32__)
  347. #include <intrin.h>
  348. #else
  349. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
  350. #if !defined(__riscv)
  351. #include <immintrin.h>
  352. #endif
  353. #endif
  354. #endif
  355. #endif
  356. #endif
  357. #ifdef __riscv_v_intrinsic
  358. #include <riscv_vector.h>
  359. #endif
  360. #ifdef __F16C__
  361. #ifdef _MSC_VER
  362. #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
  363. #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
  364. #else
  365. #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
  366. #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
  367. #endif
  368. #elif defined(__POWER9_VECTOR__)
  369. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  370. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  371. /* the inline asm below is about 12% faster than the lookup method */
  372. #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
  373. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  374. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  375. register float f;
  376. register double d;
  377. __asm__(
  378. "mtfprd %0,%2\n"
  379. "xscvhpdp %0,%0\n"
  380. "frsp %1,%0\n" :
  381. /* temp */ "=d"(d),
  382. /* out */ "=f"(f):
  383. /* in */ "r"(h));
  384. return f;
  385. }
  386. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  387. register double d;
  388. register ggml_fp16_t r;
  389. __asm__( /* xscvdphp can work on double or single precision */
  390. "xscvdphp %0,%2\n"
  391. "mffprd %1,%0\n" :
  392. /* temp */ "=d"(d),
  393. /* out */ "=r"(r):
  394. /* in */ "f"(f));
  395. return r;
  396. }
  397. #else
  398. // FP16 <-> FP32
  399. // ref: https://github.com/Maratyszcza/FP16
  400. static inline float fp32_from_bits(uint32_t w) {
  401. union {
  402. uint32_t as_bits;
  403. float as_value;
  404. } fp32;
  405. fp32.as_bits = w;
  406. return fp32.as_value;
  407. }
  408. static inline uint32_t fp32_to_bits(float f) {
  409. union {
  410. float as_value;
  411. uint32_t as_bits;
  412. } fp32;
  413. fp32.as_value = f;
  414. return fp32.as_bits;
  415. }
  416. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  417. const uint32_t w = (uint32_t) h << 16;
  418. const uint32_t sign = w & UINT32_C(0x80000000);
  419. const uint32_t two_w = w + w;
  420. const uint32_t exp_offset = UINT32_C(0xE0) << 23;
  421. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  422. const float exp_scale = 0x1.0p-112f;
  423. #else
  424. const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
  425. #endif
  426. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  427. const uint32_t magic_mask = UINT32_C(126) << 23;
  428. const float magic_bias = 0.5f;
  429. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  430. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  431. const uint32_t result = sign |
  432. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  433. return fp32_from_bits(result);
  434. }
  435. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  436. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  437. const float scale_to_inf = 0x1.0p+112f;
  438. const float scale_to_zero = 0x1.0p-110f;
  439. #else
  440. const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
  441. const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
  442. #endif
  443. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  444. const uint32_t w = fp32_to_bits(f);
  445. const uint32_t shl1_w = w + w;
  446. const uint32_t sign = w & UINT32_C(0x80000000);
  447. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  448. if (bias < UINT32_C(0x71000000)) {
  449. bias = UINT32_C(0x71000000);
  450. }
  451. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  452. const uint32_t bits = fp32_to_bits(base);
  453. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  454. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  455. const uint32_t nonsign = exp_bits + mantissa_bits;
  456. return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
  457. }
  458. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  459. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  460. #endif // __F16C__
  461. #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
  462. // precomputed f32 table for f16 (256 KB)
  463. // defined in ggml.c, initialized in ggml_init()
  464. extern float ggml_table_f32_f16[1 << 16];
  465. // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
  466. // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
  467. // This is also true for POWER9.
  468. #if !defined(GGML_FP16_TO_FP32)
  469. inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
  470. uint16_t s;
  471. memcpy(&s, &f, sizeof(uint16_t));
  472. return ggml_table_f32_f16[s];
  473. }
  474. #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
  475. #endif
  476. #if !defined(GGML_FP32_TO_FP16)
  477. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  478. #endif
  479. #define GGML_HASHTABLE_FULL ((size_t)-1)
  480. #define GGML_HASHTABLE_ALREADY_EXISTS ((size_t)-2)
  481. struct ggml_hash_set ggml_hash_set_new(size_t size);
  482. bool ggml_hash_contains (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
  483. // returns GGML_HASHTABLE_FULL if table is full, otherwise the current index of the key or where it should be inserted
  484. size_t ggml_hash_find (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
  485. // returns GGML_HASHTABLE_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
  486. size_t ggml_hash_insert ( struct ggml_hash_set hash_set, struct ggml_tensor * key);
  487. // return index, asserts if table is full
  488. size_t ggml_hash_find_or_insert( struct ggml_hash_set hash_set, struct ggml_tensor * key);
  489. #ifdef __cplusplus
  490. }
  491. #endif