ggml-impl.h 21 KB

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