ggml-impl.h 18 KB

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