@fugood/llama.node 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/CMakeLists.txt +2 -0
  2. package/bin/darwin/arm64/llama-node.node +0 -0
  3. package/bin/darwin/x64/llama-node.node +0 -0
  4. package/bin/linux/arm64/llama-node.node +0 -0
  5. package/bin/linux/x64/llama-node.node +0 -0
  6. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  7. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  8. package/bin/win32/arm64/llama-node.node +0 -0
  9. package/bin/win32/arm64/node.lib +0 -0
  10. package/bin/win32/x64/llama-node.node +0 -0
  11. package/bin/win32/x64/node.lib +0 -0
  12. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  13. package/bin/win32-vulkan/arm64/node.lib +0 -0
  14. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/x64/node.lib +0 -0
  16. package/package.json +1 -1
  17. package/src/DetokenizeWorker.cpp +1 -1
  18. package/src/EmbeddingWorker.cpp +2 -2
  19. package/src/LlamaCompletionWorker.cpp +8 -8
  20. package/src/LlamaCompletionWorker.h +2 -2
  21. package/src/LlamaContext.cpp +8 -9
  22. package/src/TokenizeWorker.cpp +1 -1
  23. package/src/common.hpp +4 -4
  24. package/src/llama.cpp/.github/workflows/build.yml +43 -9
  25. package/src/llama.cpp/.github/workflows/docker.yml +3 -0
  26. package/src/llama.cpp/CMakeLists.txt +7 -4
  27. package/src/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
  28. package/src/llama.cpp/common/CMakeLists.txt +0 -2
  29. package/src/llama.cpp/common/arg.cpp +642 -607
  30. package/src/llama.cpp/common/arg.h +22 -22
  31. package/src/llama.cpp/common/common.cpp +79 -281
  32. package/src/llama.cpp/common/common.h +130 -100
  33. package/src/llama.cpp/common/json-schema-to-grammar.cpp +1 -1
  34. package/src/llama.cpp/common/log.cpp +50 -50
  35. package/src/llama.cpp/common/log.h +18 -18
  36. package/src/llama.cpp/common/ngram-cache.cpp +36 -36
  37. package/src/llama.cpp/common/ngram-cache.h +19 -19
  38. package/src/llama.cpp/common/sampling.cpp +116 -108
  39. package/src/llama.cpp/common/sampling.h +20 -20
  40. package/src/llama.cpp/docs/build.md +37 -17
  41. package/src/llama.cpp/examples/CMakeLists.txt +1 -1
  42. package/src/llama.cpp/examples/batched/batched.cpp +14 -14
  43. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +10 -11
  44. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +1 -1
  45. package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +9 -9
  46. package/src/llama.cpp/examples/embedding/embedding.cpp +12 -12
  47. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +8 -8
  48. package/src/llama.cpp/examples/export-lora/export-lora.cpp +5 -5
  49. package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +7 -7
  50. package/src/llama.cpp/examples/gritlm/gritlm.cpp +18 -18
  51. package/src/llama.cpp/examples/imatrix/imatrix.cpp +20 -11
  52. package/src/llama.cpp/examples/infill/infill.cpp +40 -86
  53. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +42 -151
  54. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  55. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +11 -14
  56. package/src/llama.cpp/examples/llava/clip.cpp +1 -0
  57. package/src/llama.cpp/examples/llava/llava-cli.cpp +23 -23
  58. package/src/llama.cpp/examples/llava/llava.cpp +37 -3
  59. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +21 -21
  60. package/src/llama.cpp/examples/lookahead/lookahead.cpp +26 -26
  61. package/src/llama.cpp/examples/lookup/lookup-create.cpp +7 -7
  62. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
  63. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +14 -14
  64. package/src/llama.cpp/examples/lookup/lookup.cpp +29 -29
  65. package/src/llama.cpp/examples/main/main.cpp +64 -109
  66. package/src/llama.cpp/examples/parallel/parallel.cpp +18 -19
  67. package/src/llama.cpp/examples/passkey/passkey.cpp +14 -14
  68. package/src/llama.cpp/examples/perplexity/perplexity.cpp +99 -120
  69. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +10 -9
  70. package/src/llama.cpp/examples/retrieval/retrieval.cpp +13 -13
  71. package/src/llama.cpp/examples/rpc/rpc-server.cpp +3 -1
  72. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +34 -17
  73. package/src/llama.cpp/examples/server/CMakeLists.txt +4 -13
  74. package/src/llama.cpp/examples/server/server.cpp +553 -691
  75. package/src/llama.cpp/examples/server/utils.hpp +312 -25
  76. package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
  77. package/src/llama.cpp/examples/simple/simple.cpp +128 -96
  78. package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +5 -0
  79. package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +197 -0
  80. package/src/llama.cpp/examples/speculative/speculative.cpp +54 -51
  81. package/src/llama.cpp/examples/tokenize/tokenize.cpp +2 -2
  82. package/src/llama.cpp/ggml/CMakeLists.txt +15 -9
  83. package/src/llama.cpp/ggml/include/ggml-amx.h +25 -0
  84. package/src/llama.cpp/ggml/include/ggml-backend.h +46 -33
  85. package/src/llama.cpp/ggml/include/ggml-blas.h +5 -3
  86. package/src/llama.cpp/ggml/include/ggml-cann.h +9 -7
  87. package/src/llama.cpp/ggml/include/ggml-cpp.h +38 -0
  88. package/src/llama.cpp/ggml/include/ggml-cpu.h +177 -0
  89. package/src/llama.cpp/ggml/include/ggml-cuda.h +12 -12
  90. package/src/llama.cpp/ggml/include/ggml-kompute.h +7 -3
  91. package/src/llama.cpp/ggml/include/ggml-metal.h +11 -7
  92. package/src/llama.cpp/ggml/include/ggml-opt.h +216 -0
  93. package/src/llama.cpp/ggml/include/ggml-rpc.h +9 -5
  94. package/src/llama.cpp/ggml/include/ggml-sycl.h +18 -11
  95. package/src/llama.cpp/ggml/include/ggml-vulkan.h +10 -8
  96. package/src/llama.cpp/ggml/include/ggml.h +53 -393
  97. package/src/llama.cpp/ggml/src/CMakeLists.txt +66 -1149
  98. package/src/llama.cpp/ggml/src/ggml-aarch64.c +46 -3126
  99. package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -20
  100. package/src/llama.cpp/ggml/src/ggml-alloc.c +23 -27
  101. package/src/llama.cpp/ggml/src/ggml-amx/CMakeLists.txt +107 -0
  102. package/src/llama.cpp/ggml/src/ggml-amx/common.h +94 -0
  103. package/src/llama.cpp/ggml/src/ggml-amx/ggml-amx.cpp +446 -0
  104. package/src/llama.cpp/ggml/src/ggml-amx/mmq.cpp +2510 -0
  105. package/src/llama.cpp/ggml/src/ggml-amx/mmq.h +17 -0
  106. package/src/llama.cpp/ggml/src/ggml-backend-impl.h +6 -25
  107. package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +195 -0
  108. package/src/llama.cpp/ggml/src/ggml-backend.cpp +303 -864
  109. package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +91 -0
  110. package/src/llama.cpp/ggml/src/{ggml-blas.cpp → ggml-blas/ggml-blas.cpp} +213 -65
  111. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +46 -0
  112. package/src/llama.cpp/ggml/src/{ggml-cann.cpp → ggml-cann/ggml-cann.cpp} +255 -149
  113. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +261 -0
  114. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.c +3560 -0
  115. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +30 -0
  116. package/src/llama.cpp/ggml/src/{ggml-cpu-impl.h → ggml-cpu/ggml-cpu-impl.h} +0 -243
  117. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +10822 -0
  118. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
  119. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +13970 -0
  120. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +663 -0
  121. package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.cpp +667 -1
  122. package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +155 -0
  123. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +106 -0
  124. package/src/llama.cpp/ggml/src/ggml-impl.h +366 -16
  125. package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +162 -0
  126. package/src/llama.cpp/ggml/src/{ggml-kompute.cpp → ggml-kompute/ggml-kompute.cpp} +238 -72
  127. package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +108 -0
  128. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +249 -0
  129. package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +100 -0
  130. package/src/llama.cpp/ggml/src/ggml-opt.cpp +867 -0
  131. package/src/llama.cpp/ggml/src/ggml-quants.c +187 -10692
  132. package/src/llama.cpp/ggml/src/ggml-quants.h +78 -125
  133. package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +11 -0
  134. package/src/llama.cpp/ggml/src/{ggml-rpc.cpp → ggml-rpc/ggml-rpc.cpp} +475 -300
  135. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +81 -0
  136. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +3 -0
  137. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +40 -0
  138. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +258 -0
  139. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +1 -0
  140. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +2 -22
  141. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1011 -0
  142. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +76 -0
  143. package/src/llama.cpp/ggml/src/{ggml-sycl.cpp → ggml-sycl/ggml-sycl.cpp} +3584 -4142
  144. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +69 -67
  145. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +3 -3
  146. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +56 -0
  147. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +11 -0
  148. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +6 -0
  149. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +4 -4
  150. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +138 -0
  151. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.hpp +10 -0
  152. package/src/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
  153. package/src/llama.cpp/ggml/src/ggml-threading.h +12 -0
  154. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +78 -0
  155. package/src/llama.cpp/ggml/src/{ggml-vulkan.cpp → ggml-vulkan/ggml-vulkan.cpp} +555 -623
  156. package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/vulkan-shaders-gen.cpp +125 -206
  157. package/src/llama.cpp/ggml/src/ggml.c +4032 -19890
  158. package/src/llama.cpp/include/llama.h +67 -33
  159. package/src/llama.cpp/pocs/vdot/q8dot.cpp +4 -3
  160. package/src/llama.cpp/pocs/vdot/vdot.cpp +8 -7
  161. package/src/llama.cpp/src/CMakeLists.txt +2 -1
  162. package/src/llama.cpp/src/llama-sampling.cpp +745 -105
  163. package/src/llama.cpp/src/llama-sampling.h +21 -2
  164. package/src/llama.cpp/src/llama-vocab.cpp +49 -9
  165. package/src/llama.cpp/src/llama-vocab.h +35 -11
  166. package/src/llama.cpp/src/llama.cpp +2636 -2406
  167. package/src/llama.cpp/src/unicode-data.cpp +2 -2
  168. package/src/llama.cpp/tests/CMakeLists.txt +1 -2
  169. package/src/llama.cpp/tests/test-arg-parser.cpp +14 -14
  170. package/src/llama.cpp/tests/test-backend-ops.cpp +185 -60
  171. package/src/llama.cpp/tests/test-barrier.cpp +1 -0
  172. package/src/llama.cpp/tests/test-chat-template.cpp +9 -5
  173. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -4
  174. package/src/llama.cpp/tests/test-log.cpp +2 -2
  175. package/src/llama.cpp/tests/test-opt.cpp +853 -142
  176. package/src/llama.cpp/tests/test-quantize-fns.cpp +22 -19
  177. package/src/llama.cpp/tests/test-quantize-perf.cpp +16 -14
  178. package/src/llama.cpp/tests/test-rope.cpp +1 -0
  179. package/src/llama.cpp/tests/test-sampling.cpp +162 -137
  180. package/src/llama.cpp/tests/test-tokenizer-0.cpp +7 -7
  181. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +5 -5
  182. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +5 -5
  183. package/src/llama.cpp/common/train.cpp +0 -1515
  184. package/src/llama.cpp/common/train.h +0 -233
  185. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +0 -5
  186. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +0 -1639
  187. package/src/llama.cpp/tests/test-grad0.cpp +0 -1683
  188. /package/src/llama.cpp/ggml/{cmake → src/ggml-cpu/cmake}/FindSIMD.cmake +0 -0
  189. /package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.h +0 -0
  190. /package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/CMakeLists.txt +0 -0
@@ -0,0 +1,30 @@
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+
5
+ // GGML internal header
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ // Quantization
12
+ void quantize_mat_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t nrows, int64_t n_per_row, int64_t blck_size_interleave);
13
+
14
+ // GEMV
15
+ void ggml_gemv_q4_0_4x4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
16
+ void ggml_gemv_q4_0_4x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
17
+ void ggml_gemv_q4_0_8x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
18
+
19
+ // GEMM
20
+ void ggml_gemm_q4_0_4x4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
21
+ void ggml_gemm_q4_0_4x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
22
+ void ggml_gemm_q4_0_8x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
23
+
24
+ void ggml_aarch64_repack_tensor(struct ggml_tensor * cur, enum ggml_type repack_type, const void * data, size_t data_size);
25
+ enum ggml_type ggml_aarch64_get_optimal_repack_type(const struct ggml_tensor * cur);
26
+
27
+ #ifdef __cplusplus
28
+ }
29
+ #endif
30
+
@@ -27,80 +27,6 @@ extern "C" {
27
27
 
28
28
  #endif
29
29
 
30
- /**
31
- * Converts brain16 to float32.
32
- *
33
- * The bfloat16 floating point format has the following structure:
34
- *
35
- * ┌sign
36
- * │
37
- * │ ┌exponent
38
- * │ │
39
- * │ │ ┌mantissa
40
- * │ │ │
41
- * │┌──┴───┐┌─┴───┐
42
- * 0b0000000000000000 brain16
43
- *
44
- * Since bf16 has the same number of exponent bits as a 32bit float,
45
- * encoding and decoding numbers becomes relatively straightforward.
46
- *
47
- * ┌sign
48
- * │
49
- * │ ┌exponent
50
- * │ │
51
- * │ │ ┌mantissa
52
- * │ │ │
53
- * │┌──┴───┐┌─┴───────────────────┐
54
- * 0b00000000000000000000000000000000 IEEE binary32
55
- *
56
- * For comparison, the standard fp16 format has fewer exponent bits.
57
- *
58
- * ┌sign
59
- * │
60
- * │ ┌exponent
61
- * │ │
62
- * │ │ ┌mantissa
63
- * │ │ │
64
- * │┌─┴─┐┌─┴──────┐
65
- * 0b0000000000000000 IEEE binary16
66
- *
67
- * @see IEEE 754-2008
68
- */
69
- static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {
70
- union {
71
- float f;
72
- uint32_t i;
73
- } u;
74
- u.i = (uint32_t)h.bits << 16;
75
- return u.f;
76
- }
77
-
78
- /**
79
- * Converts float32 to brain16.
80
- *
81
- * This is binary identical with Google Brain float conversion.
82
- * Floats shall round to nearest even, and NANs shall be quiet.
83
- * Subnormals aren't flushed to zero, except perhaps when used.
84
- * This code should vectorize nicely if using modern compilers.
85
- */
86
- static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
87
- ggml_bf16_t h;
88
- union {
89
- float f;
90
- uint32_t i;
91
- } u;
92
- u.f = s;
93
- if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
94
- h.bits = (u.i >> 16) | 64; /* force to quiet */
95
- return h;
96
- }
97
- h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
98
- return h;
99
- }
100
-
101
- #define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)
102
- #define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)
103
-
104
30
  // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
105
31
  #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
106
32
  #ifndef __FMA__
@@ -388,28 +314,6 @@ inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b)
388
314
 
389
315
  #endif // defined(__ARM_NEON)
390
316
 
391
- #if defined(__ARM_NEON) && !defined(_MSC_VER)
392
-
393
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
394
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
395
-
396
- #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
397
-
398
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
399
- ggml_fp16_internal_t tmp;
400
- memcpy(&tmp, &h, sizeof(ggml_fp16_t));
401
- return (float)tmp;
402
- }
403
-
404
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
405
- ggml_fp16_t res;
406
- ggml_fp16_internal_t tmp = f;
407
- memcpy(&res, &tmp, sizeof(ggml_fp16_t));
408
- return res;
409
- }
410
-
411
- #else
412
-
413
317
  #ifdef __wasm_simd128__
414
318
  #include <wasm_simd128.h>
415
319
  #else
@@ -462,153 +366,6 @@ static __m256 __lasx_xvreplfr2vr_s(float val) {
462
366
  }
463
367
  #endif
464
368
 
465
- #ifdef __F16C__
466
-
467
- #ifdef _MSC_VER
468
- #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
469
- #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
470
- #else
471
- #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
472
- #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
473
- #endif
474
-
475
- #elif defined(__POWER9_VECTOR__)
476
-
477
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
478
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
479
- /* the inline asm below is about 12% faster than the lookup method */
480
- #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
481
- #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
482
-
483
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
484
- register float f;
485
- register double d;
486
- __asm__(
487
- "mtfprd %0,%2\n"
488
- "xscvhpdp %0,%0\n"
489
- "frsp %1,%0\n" :
490
- /* temp */ "=d"(d),
491
- /* out */ "=f"(f):
492
- /* in */ "r"(h));
493
- return f;
494
- }
495
-
496
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
497
- register double d;
498
- register ggml_fp16_t r;
499
- __asm__( /* xscvdphp can work on double or single precision */
500
- "xscvdphp %0,%2\n"
501
- "mffprd %1,%0\n" :
502
- /* temp */ "=d"(d),
503
- /* out */ "=r"(r):
504
- /* in */ "f"(f));
505
- return r;
506
- }
507
-
508
- #else
509
-
510
- // FP16 <-> FP32
511
- // ref: https://github.com/Maratyszcza/FP16
512
-
513
- static inline float fp32_from_bits(uint32_t w) {
514
- union {
515
- uint32_t as_bits;
516
- float as_value;
517
- } fp32;
518
- fp32.as_bits = w;
519
- return fp32.as_value;
520
- }
521
-
522
- static inline uint32_t fp32_to_bits(float f) {
523
- union {
524
- float as_value;
525
- uint32_t as_bits;
526
- } fp32;
527
- fp32.as_value = f;
528
- return fp32.as_bits;
529
- }
530
-
531
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
532
- const uint32_t w = (uint32_t) h << 16;
533
- const uint32_t sign = w & UINT32_C(0x80000000);
534
- const uint32_t two_w = w + w;
535
-
536
- const uint32_t exp_offset = UINT32_C(0xE0) << 23;
537
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
538
- const float exp_scale = 0x1.0p-112f;
539
- #else
540
- const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
541
- #endif
542
- const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
543
-
544
- const uint32_t magic_mask = UINT32_C(126) << 23;
545
- const float magic_bias = 0.5f;
546
- const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
547
-
548
- const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
549
- const uint32_t result = sign |
550
- (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
551
- return fp32_from_bits(result);
552
- }
553
-
554
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
555
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
556
- const float scale_to_inf = 0x1.0p+112f;
557
- const float scale_to_zero = 0x1.0p-110f;
558
- #else
559
- const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
560
- const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
561
- #endif
562
- float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
563
-
564
- const uint32_t w = fp32_to_bits(f);
565
- const uint32_t shl1_w = w + w;
566
- const uint32_t sign = w & UINT32_C(0x80000000);
567
- uint32_t bias = shl1_w & UINT32_C(0xFF000000);
568
- if (bias < UINT32_C(0x71000000)) {
569
- bias = UINT32_C(0x71000000);
570
- }
571
-
572
- base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
573
- const uint32_t bits = fp32_to_bits(base);
574
- const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
575
- const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
576
- const uint32_t nonsign = exp_bits + mantissa_bits;
577
- return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
578
- }
579
-
580
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
581
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
582
-
583
- #endif // __F16C__
584
-
585
- #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
586
-
587
- #ifdef __ARM_FEATURE_SVE
588
- #include <arm_sve.h>
589
- #endif // __ARM_FEATURE_SVE
590
-
591
- // precomputed f32 table for f16 (256 KB)
592
- // defined in ggml.c, initialized in ggml_init()
593
- extern float ggml_table_f32_f16[1 << 16];
594
-
595
- // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
596
- // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
597
- // This is also true for POWER9.
598
- #if !defined(GGML_FP16_TO_FP32)
599
- inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
600
- uint16_t s;
601
- memcpy(&s, &f, sizeof(uint16_t));
602
- return ggml_table_f32_f16[s];
603
- }
604
-
605
- #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
606
- #endif
607
-
608
- #if !defined(GGML_FP32_TO_FP16)
609
- #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
610
- #endif
611
-
612
369
  #ifdef __cplusplus
613
370
  }
614
371
  #endif