ggml-impl.h 21 KB

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