ggml-impl.h 17 KB

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