ggml-impl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. #pragma once
  2. // GGML internal header
  3. #include "ggml.h"
  4. #include "gguf.h"
  5. #include <assert.h>
  6. #include <math.h>
  7. #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #ifdef __ARM_FEATURE_SVE
  12. #include <arm_sve.h>
  13. #endif // __ARM_FEATURE_SVE
  14. #if defined(__ARM_NEON) && !defined(__CUDACC__)
  15. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  16. //
  17. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  18. //
  19. #include <arm_neon.h>
  20. #endif
  21. #if defined(__F16C__)
  22. #include <immintrin.h>
  23. #endif
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #ifndef MIN
  28. # define MIN(a, b) ((a) < (b) ? (a) : (b))
  29. #endif
  30. #ifndef MAX
  31. # define MAX(a, b) ((a) > (b) ? (a) : (b))
  32. #endif
  33. // required for mmap as gguf only guarantees 32-byte alignment
  34. #define TENSOR_ALIGNMENT 32
  35. // static_assert should be a #define, but if it's not,
  36. // fall back to the _Static_assert C11 keyword.
  37. // if C99 - static_assert is noop
  38. // ref: https://stackoverflow.com/a/53923785/4039976
  39. #ifndef __cplusplus
  40. #ifndef static_assert
  41. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  42. #define static_assert(cond, msg) _Static_assert(cond, msg)
  43. #else
  44. #define static_assert(cond, msg) struct global_scope_noop_trick
  45. #endif
  46. #endif
  47. #endif
  48. static inline int ggml_up32(int n) {
  49. return (n + 31) & ~31;
  50. }
  51. //static inline int ggml_up64(int n) {
  52. // return (n + 63) & ~63;
  53. //}
  54. static inline int ggml_up(int n, int m) {
  55. // assert m is a power of 2
  56. GGML_ASSERT((m & (m - 1)) == 0);
  57. return (n + m - 1) & ~(m - 1);
  58. }
  59. //
  60. // logging
  61. //
  62. GGML_ATTRIBUTE_FORMAT(2, 3)
  63. GGML_API void ggml_log_internal (enum ggml_log_level level, const char * format, ...);
  64. GGML_API void ggml_log_callback_default(enum ggml_log_level level, const char * text, void * user_data);
  65. #define GGML_LOG(...) ggml_log_internal(GGML_LOG_LEVEL_NONE , __VA_ARGS__)
  66. #define GGML_LOG_INFO(...) ggml_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)
  67. #define GGML_LOG_WARN(...) ggml_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)
  68. #define GGML_LOG_ERROR(...) ggml_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
  69. #define GGML_LOG_DEBUG(...) ggml_log_internal(GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
  70. #define GGML_LOG_CONT(...) ggml_log_internal(GGML_LOG_LEVEL_CONT , __VA_ARGS__)
  71. #define GGML_DEBUG 0
  72. #if (GGML_DEBUG >= 1)
  73. #define GGML_PRINT_DEBUG(...) GGML_LOG_DEBUG(__VA_ARGS__)
  74. #else
  75. #define GGML_PRINT_DEBUG(...)
  76. #endif
  77. #if (GGML_DEBUG >= 5)
  78. #define GGML_PRINT_DEBUG_5(...) GGML_LOG_DEBUG(__VA_ARGS__)
  79. #else
  80. #define GGML_PRINT_DEBUG_5(...)
  81. #endif
  82. #if (GGML_DEBUG >= 10)
  83. #define GGML_PRINT_DEBUG_10(...) GGML_LOG_DEBUG(__VA_ARGS__)
  84. #else
  85. #define GGML_PRINT_DEBUG_10(...)
  86. #endif
  87. // tensor params
  88. static void ggml_set_op_params(struct ggml_tensor * tensor, const void * params, size_t params_size) {
  89. GGML_ASSERT(tensor != NULL); // silence -Warray-bounds warnings
  90. assert(params_size <= GGML_MAX_OP_PARAMS);
  91. memcpy(tensor->op_params, params, params_size);
  92. }
  93. static int32_t ggml_get_op_params_i32(const struct ggml_tensor * tensor, uint32_t i) {
  94. assert(i < GGML_MAX_OP_PARAMS / sizeof(int32_t));
  95. return ((const int32_t *)(tensor->op_params))[i];
  96. }
  97. static float ggml_get_op_params_f32(const struct ggml_tensor * tensor, uint32_t i) {
  98. assert(i < GGML_MAX_OP_PARAMS / sizeof(float));
  99. return ((const float *)(tensor->op_params))[i];
  100. }
  101. static void ggml_set_op_params_i32(struct ggml_tensor * tensor, uint32_t i, int32_t value) {
  102. assert(i < GGML_MAX_OP_PARAMS / sizeof(int32_t));
  103. ((int32_t *)(tensor->op_params))[i] = value;
  104. }
  105. static void ggml_set_op_params_f32(struct ggml_tensor * tensor, uint32_t i, float value) {
  106. assert(i < GGML_MAX_OP_PARAMS / sizeof(float));
  107. ((float *)(tensor->op_params))[i] = value;
  108. }
  109. struct ggml_map_custom1_op_params {
  110. ggml_custom1_op_t fun;
  111. int n_tasks;
  112. void * userdata;
  113. };
  114. struct ggml_map_custom2_op_params {
  115. ggml_custom2_op_t fun;
  116. int n_tasks;
  117. void * userdata;
  118. };
  119. struct ggml_map_custom3_op_params {
  120. ggml_custom3_op_t fun;
  121. int n_tasks;
  122. void * userdata;
  123. };
  124. // bitset
  125. typedef uint32_t ggml_bitset_t;
  126. static_assert(sizeof(ggml_bitset_t) == 4, "bitset_t constants must be updated");
  127. #define BITSET_SHR 5 // log2(sizeof(ggml_bitset_t)*8)
  128. #define BITSET_MASK (sizeof(ggml_bitset_t)*8 - 1)
  129. static size_t ggml_bitset_size(size_t n) {
  130. return (n + BITSET_MASK) >> BITSET_SHR;
  131. }
  132. static inline bool ggml_bitset_get(const ggml_bitset_t * bitset, size_t i) {
  133. return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
  134. }
  135. static inline void ggml_bitset_set(ggml_bitset_t * bitset, size_t i) {
  136. bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
  137. }
  138. static inline void ggml_bitset_clear(ggml_bitset_t * bitset, size_t i) {
  139. bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
  140. }
  141. // hash set
  142. #define GGML_HASHSET_FULL ((size_t)-1)
  143. #define GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
  144. struct ggml_hash_set {
  145. size_t size;
  146. ggml_bitset_t * used; // whether or not the keys are in use i.e. set
  147. struct ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if ggml_bitset_get(used, i)
  148. };
  149. struct ggml_hash_set ggml_hash_set_new(size_t size);
  150. void ggml_hash_set_free(struct ggml_hash_set * hash_set);
  151. // returns the minimum size for a hash set that can hold min_sz elements
  152. size_t ggml_hash_size(size_t min_sz);
  153. // remove all elements from the hash set
  154. void ggml_hash_set_reset(struct ggml_hash_set * hash_set);
  155. // returns true if key is in the hash set
  156. static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  157. // returns GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
  158. static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, const struct ggml_tensor * key);
  159. // returns GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
  160. static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  161. // return index, asserts if table is full
  162. static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  163. // hash function for ggml_tensor
  164. static inline size_t ggml_hash(const struct ggml_tensor * p) {
  165. // the last 4 bits are always zero due to alignment
  166. return (size_t)(uintptr_t)p >> 4;
  167. }
  168. static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, const struct ggml_tensor * key) {
  169. size_t h = ggml_hash(key) % hash_set->size;
  170. // linear probing
  171. size_t i = h;
  172. while (ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
  173. i = (i + 1) % hash_set->size;
  174. if (i == h) {
  175. // visited all hash table entries -> not found
  176. return GGML_HASHSET_FULL;
  177. }
  178. }
  179. return i;
  180. }
  181. static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  182. size_t i = ggml_hash_find(hash_set, key);
  183. return i != GGML_HASHSET_FULL && ggml_bitset_get(hash_set->used, i);
  184. }
  185. static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  186. size_t h = ggml_hash(key) % hash_set->size;
  187. // linear probing
  188. size_t i = h;
  189. do {
  190. if (!ggml_bitset_get(hash_set->used, i)) {
  191. ggml_bitset_set(hash_set->used, i);
  192. hash_set->keys[i] = key;
  193. return i;
  194. }
  195. if (hash_set->keys[i] == key) {
  196. return GGML_HASHSET_ALREADY_EXISTS;
  197. }
  198. i = (i + 1) % hash_set->size;
  199. } while (i != h);
  200. // visited all hash table entries -> not found
  201. GGML_ABORT("fatal error");
  202. }
  203. static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  204. size_t h = ggml_hash(key) % hash_set->size;
  205. // linear probing
  206. size_t i = h;
  207. do {
  208. if (!ggml_bitset_get(hash_set->used, i)) {
  209. ggml_bitset_set(hash_set->used, i);
  210. hash_set->keys[i] = key;
  211. return i;
  212. }
  213. if (hash_set->keys[i] == key) {
  214. return i;
  215. }
  216. i = (i + 1) % hash_set->size;
  217. } while (i != h);
  218. // visited all hash table entries -> not found
  219. GGML_ABORT("fatal error");
  220. }
  221. // computation graph
  222. enum ggml_cgraph_eval_order {
  223. GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
  224. GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
  225. GGML_CGRAPH_EVAL_ORDER_COUNT
  226. };
  227. struct ggml_cgraph {
  228. int size; // maximum number of nodes/leafs/grads/grad_accs
  229. int n_nodes; // number of nodes currently in use
  230. int n_leafs; // number of leafs currently in use
  231. struct ggml_tensor ** nodes; // tensors with data that can change if the graph is evaluated
  232. struct ggml_tensor ** grads; // the outputs of these tensors are the gradients of the nodes
  233. struct ggml_tensor ** grad_accs; // accumulators for node gradients
  234. struct ggml_tensor ** leafs; // tensors with constant data
  235. struct ggml_hash_set visited_hash_set;
  236. enum ggml_cgraph_eval_order order;
  237. };
  238. // returns a slice of cgraph with nodes [i0, i1)
  239. // the slice does not have leafs or gradients
  240. // if you need the gradients, get them from the original graph
  241. struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);
  242. // Memory allocation
  243. GGML_API void * ggml_aligned_malloc(size_t size);
  244. GGML_API void ggml_aligned_free(void * ptr, size_t size);
  245. // FP16 to FP32 conversion
  246. #if defined(__ARM_NEON)
  247. #if defined(_MSC_VER) || (defined(__CUDACC__) && __CUDACC_VER_MAJOR__ <= 11)
  248. typedef uint16_t ggml_fp16_internal_t;
  249. #else
  250. typedef __fp16 ggml_fp16_internal_t;
  251. #endif
  252. #endif
  253. #if defined(__ARM_NEON) && !defined(_MSC_VER) && !(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ <= 11)
  254. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  255. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  256. #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  257. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  258. ggml_fp16_internal_t tmp;
  259. memcpy(&tmp, &h, sizeof(ggml_fp16_t));
  260. return (float)tmp;
  261. }
  262. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  263. ggml_fp16_t res;
  264. ggml_fp16_internal_t tmp = f;
  265. memcpy(&res, &tmp, sizeof(ggml_fp16_t));
  266. return res;
  267. }
  268. #elif defined(__F16C__)
  269. #ifdef _MSC_VER
  270. #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
  271. #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
  272. #else
  273. #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
  274. #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
  275. #endif
  276. #elif defined(__POWER9_VECTOR__)
  277. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  278. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  279. /* the inline asm below is about 12% faster than the lookup method */
  280. #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
  281. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  282. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  283. register float f;
  284. register double d;
  285. __asm__(
  286. "mtfprd %0,%2\n"
  287. "xscvhpdp %0,%0\n"
  288. "frsp %1,%0\n" :
  289. /* temp */ "=d"(d),
  290. /* out */ "=f"(f):
  291. /* in */ "r"(h));
  292. return f;
  293. }
  294. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  295. register double d;
  296. register ggml_fp16_t r;
  297. __asm__( /* xscvdphp can work on double or single precision */
  298. "xscvdphp %0,%2\n"
  299. "mffprd %1,%0\n" :
  300. /* temp */ "=d"(d),
  301. /* out */ "=r"(r):
  302. /* in */ "f"(f));
  303. return r;
  304. }
  305. #else
  306. // FP16 <-> FP32
  307. // ref: https://github.com/Maratyszcza/FP16
  308. static inline float fp32_from_bits(uint32_t w) {
  309. union {
  310. uint32_t as_bits;
  311. float as_value;
  312. } fp32;
  313. fp32.as_bits = w;
  314. return fp32.as_value;
  315. }
  316. static inline uint32_t fp32_to_bits(float f) {
  317. union {
  318. float as_value;
  319. uint32_t as_bits;
  320. } fp32;
  321. fp32.as_value = f;
  322. return fp32.as_bits;
  323. }
  324. static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
  325. const uint32_t w = (uint32_t) h << 16;
  326. const uint32_t sign = w & UINT32_C(0x80000000);
  327. const uint32_t two_w = w + w;
  328. const uint32_t exp_offset = UINT32_C(0xE0) << 23;
  329. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
  330. const float exp_scale = 0x1.0p-112f;
  331. #else
  332. const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
  333. #endif
  334. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  335. const uint32_t magic_mask = UINT32_C(126) << 23;
  336. const float magic_bias = 0.5f;
  337. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  338. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  339. const uint32_t result = sign |
  340. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  341. return fp32_from_bits(result);
  342. }
  343. static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
  344. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
  345. const float scale_to_inf = 0x1.0p+112f;
  346. const float scale_to_zero = 0x1.0p-110f;
  347. #else
  348. const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
  349. const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
  350. #endif
  351. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  352. const uint32_t w = fp32_to_bits(f);
  353. const uint32_t shl1_w = w + w;
  354. const uint32_t sign = w & UINT32_C(0x80000000);
  355. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  356. if (bias < UINT32_C(0x71000000)) {
  357. bias = UINT32_C(0x71000000);
  358. }
  359. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  360. const uint32_t bits = fp32_to_bits(base);
  361. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  362. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  363. const uint32_t nonsign = exp_bits + mantissa_bits;
  364. return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
  365. }
  366. #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
  367. #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
  368. #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
  369. // precomputed f32 table for f16 (256 KB)
  370. // defined in ggml.c, initialized in ggml_init()
  371. GGML_API float ggml_table_f32_f16[1 << 16];
  372. // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
  373. // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
  374. // This is also true for POWER9.
  375. #if !defined(GGML_FP16_TO_FP32)
  376. inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
  377. uint16_t s;
  378. memcpy(&s, &f, sizeof(uint16_t));
  379. return ggml_table_f32_f16[s];
  380. }
  381. #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
  382. #endif
  383. #if !defined(GGML_FP32_TO_FP16)
  384. #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
  385. #endif
  386. /**
  387. * Converts brain16 to float32.
  388. *
  389. * The bfloat16 floating point format has the following structure:
  390. *
  391. * ┌sign
  392. * │
  393. * │ ┌exponent
  394. * │ │
  395. * │ │ ┌mantissa
  396. * │ │ │
  397. * │┌──┴───┐┌─┴───┐
  398. * 0b0000000000000000 brain16
  399. *
  400. * Since bf16 has the same number of exponent bits as a 32bit float,
  401. * encoding and decoding numbers becomes relatively straightforward.
  402. *
  403. * ┌sign
  404. * │
  405. * │ ┌exponent
  406. * │ │
  407. * │ │ ┌mantissa
  408. * │ │ │
  409. * │┌──┴───┐┌─┴───────────────────┐
  410. * 0b00000000000000000000000000000000 IEEE binary32
  411. *
  412. * For comparison, the standard fp16 format has fewer exponent bits.
  413. *
  414. * ┌sign
  415. * │
  416. * │ ┌exponent
  417. * │ │
  418. * │ │ ┌mantissa
  419. * │ │ │
  420. * │┌─┴─┐┌─┴──────┐
  421. * 0b0000000000000000 IEEE binary16
  422. *
  423. * @see IEEE 754-2008
  424. */
  425. static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {
  426. union {
  427. float f;
  428. uint32_t i;
  429. } u;
  430. u.i = (uint32_t)h.bits << 16;
  431. return u.f;
  432. }
  433. /**
  434. * Converts float32 to brain16.
  435. *
  436. * This is binary identical with Google Brain float conversion.
  437. * Floats shall round to nearest even, and NANs shall be quiet.
  438. * Subnormals aren't flushed to zero, except perhaps when used.
  439. * This code should vectorize nicely if using modern compilers.
  440. */
  441. static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
  442. ggml_bf16_t h;
  443. union {
  444. float f;
  445. uint32_t i;
  446. } u;
  447. u.f = s;
  448. if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
  449. h.bits = (u.i >> 16) | 64; /* force to quiet */
  450. return h;
  451. }
  452. h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
  453. return h;
  454. }
  455. #define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)
  456. #define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)
  457. #ifdef __cplusplus
  458. }
  459. #endif
  460. #ifdef __cplusplus
  461. #include <vector>
  462. // expose GGUF internals for test code
  463. GGML_API size_t gguf_type_size(enum gguf_type type);
  464. GGML_API struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_params params);
  465. GGML_API void gguf_write_to_buf(const struct gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta);
  466. #endif // __cplusplus