ggml-cpu-impl.h 17 KB

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