ggml-cpu-impl.h 16 KB

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