ggml-impl.h 17 KB

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