@fugood/llama.node 0.0.1-alpha.1

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 (204) hide show
  1. package/CMakeLists.txt +85 -0
  2. package/README.md +56 -0
  3. package/bin/darwin/arm64/llama-node.node +0 -0
  4. package/bin/darwin/x64/llama-node.node +0 -0
  5. package/bin/linux/arm64/llama-node.node +0 -0
  6. package/bin/linux/x64/llama-node.node +0 -0
  7. package/bin/win32/arm64/llama-node.node +0 -0
  8. package/bin/win32/arm64/node.lib +0 -0
  9. package/bin/win32/x64/llama-node.node +0 -0
  10. package/bin/win32/x64/node.lib +0 -0
  11. package/lib/binding.js +13 -0
  12. package/lib/binding.ts +57 -0
  13. package/lib/index.js +24 -0
  14. package/lib/index.ts +13 -0
  15. package/package.json +65 -0
  16. package/src/addons.cpp +506 -0
  17. package/src/llama.cpp/CMakeLists.txt +1320 -0
  18. package/src/llama.cpp/build.zig +172 -0
  19. package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
  20. package/src/llama.cpp/common/CMakeLists.txt +87 -0
  21. package/src/llama.cpp/common/base64.hpp +392 -0
  22. package/src/llama.cpp/common/common.cpp +2949 -0
  23. package/src/llama.cpp/common/common.h +324 -0
  24. package/src/llama.cpp/common/console.cpp +501 -0
  25. package/src/llama.cpp/common/console.h +19 -0
  26. package/src/llama.cpp/common/grammar-parser.cpp +440 -0
  27. package/src/llama.cpp/common/grammar-parser.h +29 -0
  28. package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
  29. package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
  30. package/src/llama.cpp/common/json.hpp +24766 -0
  31. package/src/llama.cpp/common/log.h +724 -0
  32. package/src/llama.cpp/common/ngram-cache.cpp +282 -0
  33. package/src/llama.cpp/common/ngram-cache.h +94 -0
  34. package/src/llama.cpp/common/sampling.cpp +353 -0
  35. package/src/llama.cpp/common/sampling.h +147 -0
  36. package/src/llama.cpp/common/stb_image.h +8396 -0
  37. package/src/llama.cpp/common/train.cpp +1513 -0
  38. package/src/llama.cpp/common/train.h +233 -0
  39. package/src/llama.cpp/examples/CMakeLists.txt +52 -0
  40. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
  41. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
  42. package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
  43. package/src/llama.cpp/examples/batched/batched.cpp +262 -0
  44. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
  45. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
  46. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
  47. package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
  48. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
  49. package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
  50. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
  51. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
  52. package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
  53. package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
  54. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
  55. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
  56. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
  57. package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
  58. package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
  59. package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
  60. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
  61. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
  62. package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
  63. package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
  64. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
  65. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
  66. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
  67. package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
  68. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
  69. package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
  70. package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
  71. package/src/llama.cpp/examples/infill/infill.cpp +767 -0
  72. package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
  73. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
  74. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
  75. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
  76. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
  77. package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
  78. package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
  79. package/src/llama.cpp/examples/llava/clip.h +85 -0
  80. package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
  81. package/src/llama.cpp/examples/llava/llava.cpp +426 -0
  82. package/src/llama.cpp/examples/llava/llava.h +50 -0
  83. package/src/llama.cpp/examples/llava/requirements.txt +3 -0
  84. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
  85. package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
  86. package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
  87. package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
  88. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
  89. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
  90. package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
  91. package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
  92. package/src/llama.cpp/examples/main/main.cpp +957 -0
  93. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
  94. package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
  95. package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
  96. package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
  97. package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
  98. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
  99. package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
  100. package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
  101. package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
  102. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
  103. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
  104. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
  105. package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
  106. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
  107. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
  108. package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
  109. package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
  110. package/src/llama.cpp/examples/server/httplib.h +9465 -0
  111. package/src/llama.cpp/examples/server/server.cpp +3826 -0
  112. package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
  113. package/src/llama.cpp/examples/server/utils.hpp +653 -0
  114. package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
  115. package/src/llama.cpp/examples/simple/simple.cpp +183 -0
  116. package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
  117. package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
  118. package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
  119. package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
  120. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
  121. package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
  122. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
  123. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
  124. package/src/llama.cpp/ggml-alloc.c +985 -0
  125. package/src/llama.cpp/ggml-alloc.h +76 -0
  126. package/src/llama.cpp/ggml-backend-impl.h +141 -0
  127. package/src/llama.cpp/ggml-backend.c +2099 -0
  128. package/src/llama.cpp/ggml-backend.h +233 -0
  129. package/src/llama.cpp/ggml-common.h +1853 -0
  130. package/src/llama.cpp/ggml-cuda.h +43 -0
  131. package/src/llama.cpp/ggml-impl.h +265 -0
  132. package/src/llama.cpp/ggml-kompute.cpp +2006 -0
  133. package/src/llama.cpp/ggml-kompute.h +46 -0
  134. package/src/llama.cpp/ggml-metal.h +66 -0
  135. package/src/llama.cpp/ggml-mpi.c +216 -0
  136. package/src/llama.cpp/ggml-mpi.h +39 -0
  137. package/src/llama.cpp/ggml-opencl.cpp +2301 -0
  138. package/src/llama.cpp/ggml-opencl.h +36 -0
  139. package/src/llama.cpp/ggml-quants.c +12678 -0
  140. package/src/llama.cpp/ggml-quants.h +133 -0
  141. package/src/llama.cpp/ggml-sycl.cpp +17882 -0
  142. package/src/llama.cpp/ggml-sycl.h +49 -0
  143. package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
  144. package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
  145. package/src/llama.cpp/ggml-vulkan.h +29 -0
  146. package/src/llama.cpp/ggml.c +21819 -0
  147. package/src/llama.cpp/ggml.h +2403 -0
  148. package/src/llama.cpp/llama.cpp +17468 -0
  149. package/src/llama.cpp/llama.h +1117 -0
  150. package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
  151. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
  152. package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
  153. package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
  154. package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
  155. package/src/llama.cpp/prompts/alpaca.txt +1 -0
  156. package/src/llama.cpp/prompts/assistant.txt +31 -0
  157. package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
  158. package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
  159. package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
  160. package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
  161. package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
  162. package/src/llama.cpp/prompts/chat.txt +28 -0
  163. package/src/llama.cpp/prompts/dan-modified.txt +1 -0
  164. package/src/llama.cpp/prompts/dan.txt +1 -0
  165. package/src/llama.cpp/prompts/mnemonics.txt +93 -0
  166. package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
  167. package/src/llama.cpp/prompts/reason-act.txt +18 -0
  168. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
  169. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
  170. package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
  171. package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
  172. package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
  173. package/src/llama.cpp/requirements.txt +12 -0
  174. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
  175. package/src/llama.cpp/scripts/xxd.cmake +16 -0
  176. package/src/llama.cpp/sgemm.cpp +999 -0
  177. package/src/llama.cpp/sgemm.h +12 -0
  178. package/src/llama.cpp/tests/CMakeLists.txt +78 -0
  179. package/src/llama.cpp/tests/get-model.cpp +21 -0
  180. package/src/llama.cpp/tests/get-model.h +2 -0
  181. package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
  182. package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
  183. package/src/llama.cpp/tests/test-c.c +7 -0
  184. package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
  185. package/src/llama.cpp/tests/test-double-float.cpp +57 -0
  186. package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
  187. package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
  188. package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
  189. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
  190. package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
  191. package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
  192. package/src/llama.cpp/tests/test-opt.cpp +181 -0
  193. package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
  194. package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
  195. package/src/llama.cpp/tests/test-rope.cpp +221 -0
  196. package/src/llama.cpp/tests/test-sampling.cpp +301 -0
  197. package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
  198. package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
  199. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
  200. package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
  201. package/src/llama.cpp/unicode-data.cpp +1651 -0
  202. package/src/llama.cpp/unicode-data.h +16 -0
  203. package/src/llama.cpp/unicode.cpp +277 -0
  204. package/src/llama.cpp/unicode.h +28 -0
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+ #include "ggml-backend.h"
5
+
6
+ #ifdef GGML_USE_HIPBLAS
7
+ #define GGML_CUDA_NAME "ROCm"
8
+ #define GGML_CUBLAS_NAME "hipBLAS"
9
+ #else
10
+ #define GGML_CUDA_NAME "CUDA"
11
+ #define GGML_CUBLAS_NAME "cuBLAS"
12
+ #endif
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ #define GGML_CUDA_MAX_DEVICES 16
19
+
20
+ // backend API
21
+ GGML_API GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device);
22
+
23
+ GGML_API GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend);
24
+
25
+ // device buffer
26
+ GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device);
27
+
28
+ // split tensor buffer that splits matrices by rows across multiple devices
29
+ GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split);
30
+
31
+ // pinned host buffer for use with the CPU backend for faster copies between CPU and GPU
32
+ GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type(void);
33
+
34
+ GGML_API GGML_CALL int ggml_backend_cuda_get_device_count(void);
35
+ GGML_API GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size);
36
+ GGML_API GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total);
37
+
38
+ GGML_API GGML_CALL bool ggml_backend_cuda_register_host_buffer(void * buffer, size_t size);
39
+ GGML_API GGML_CALL void ggml_backend_cuda_unregister_host_buffer(void * buffer);
40
+
41
+ #ifdef __cplusplus
42
+ }
43
+ #endif
@@ -0,0 +1,265 @@
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+
5
+ // GGML internal header
6
+
7
+ #include <assert.h>
8
+ #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
9
+ #include <stddef.h>
10
+ #include <stdbool.h>
11
+ #include <string.h> // memcpy
12
+ #include <math.h> // fabsf
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ // static_assert should be a #define, but if it's not,
19
+ // fall back to the _Static_assert C11 keyword.
20
+ // if C99 - static_assert is noop
21
+ // ref: https://stackoverflow.com/a/53923785/4039976
22
+ #ifndef __cplusplus
23
+ #ifndef static_assert
24
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
25
+ #define static_assert(cond, msg) _Static_assert(cond, msg)
26
+ #else
27
+ #define static_assert(cond, msg) struct global_scope_noop_trick
28
+ #endif
29
+ #endif
30
+ #endif
31
+
32
+ // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
33
+ #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
34
+ #ifndef __FMA__
35
+ #define __FMA__
36
+ #endif
37
+ #ifndef __F16C__
38
+ #define __F16C__
39
+ #endif
40
+ #ifndef __SSE3__
41
+ #define __SSE3__
42
+ #endif
43
+ #endif
44
+
45
+ // 16-bit float
46
+ // on Arm, we use __fp16
47
+ // on x86, we use uint16_t
48
+ #if defined(__ARM_NEON) && !defined(_MSC_VER)
49
+
50
+ // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
51
+ //
52
+ // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
53
+ //
54
+ #include <arm_neon.h>
55
+
56
+ typedef __fp16 ggml_fp16_internal_t;
57
+
58
+ #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
59
+ #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
60
+
61
+ #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
62
+
63
+ static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
64
+ ggml_fp16_internal_t tmp;
65
+ memcpy(&tmp, &h, sizeof(ggml_fp16_t));
66
+ return (float)tmp;
67
+ }
68
+
69
+ static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
70
+ ggml_fp16_t res;
71
+ ggml_fp16_internal_t tmp = f;
72
+ memcpy(&res, &tmp, sizeof(ggml_fp16_t));
73
+ return res;
74
+ }
75
+
76
+ #else
77
+
78
+ typedef uint16_t ggml_fp16_internal_t;
79
+
80
+ #ifdef __wasm_simd128__
81
+ #include <wasm_simd128.h>
82
+ #else
83
+ #ifdef __POWER9_VECTOR__
84
+ #include <altivec.h>
85
+ #undef bool
86
+ #define bool _Bool
87
+ #else
88
+ #if defined(_MSC_VER) || defined(__MINGW32__)
89
+ #include <intrin.h>
90
+ #else
91
+ #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
92
+ #if !defined(__riscv)
93
+ #include <immintrin.h>
94
+ #endif
95
+ #endif
96
+ #endif
97
+ #endif
98
+ #endif
99
+
100
+ #ifdef __riscv_v_intrinsic
101
+ #include <riscv_vector.h>
102
+ #endif
103
+
104
+ #ifdef __F16C__
105
+
106
+ #ifdef _MSC_VER
107
+ #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
108
+ #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
109
+ #else
110
+ #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
111
+ #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
112
+ #endif
113
+
114
+ #elif defined(__POWER9_VECTOR__)
115
+
116
+ #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
117
+ #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
118
+ /* the inline asm below is about 12% faster than the lookup method */
119
+ #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
120
+ #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
121
+
122
+ static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
123
+ register float f;
124
+ register double d;
125
+ __asm__(
126
+ "mtfprd %0,%2\n"
127
+ "xscvhpdp %0,%0\n"
128
+ "frsp %1,%0\n" :
129
+ /* temp */ "=d"(d),
130
+ /* out */ "=f"(f):
131
+ /* in */ "r"(h));
132
+ return f;
133
+ }
134
+
135
+ static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
136
+ register double d;
137
+ register ggml_fp16_t r;
138
+ __asm__( /* xscvdphp can work on double or single precision */
139
+ "xscvdphp %0,%2\n"
140
+ "mffprd %1,%0\n" :
141
+ /* temp */ "=d"(d),
142
+ /* out */ "=r"(r):
143
+ /* in */ "f"(f));
144
+ return r;
145
+ }
146
+
147
+ #else
148
+
149
+ // FP16 <-> FP32
150
+ // ref: https://github.com/Maratyszcza/FP16
151
+
152
+ static inline float fp32_from_bits(uint32_t w) {
153
+ union {
154
+ uint32_t as_bits;
155
+ float as_value;
156
+ } fp32;
157
+ fp32.as_bits = w;
158
+ return fp32.as_value;
159
+ }
160
+
161
+ static inline uint32_t fp32_to_bits(float f) {
162
+ union {
163
+ float as_value;
164
+ uint32_t as_bits;
165
+ } fp32;
166
+ fp32.as_value = f;
167
+ return fp32.as_bits;
168
+ }
169
+
170
+ static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
171
+ const uint32_t w = (uint32_t) h << 16;
172
+ const uint32_t sign = w & UINT32_C(0x80000000);
173
+ const uint32_t two_w = w + w;
174
+
175
+ const uint32_t exp_offset = UINT32_C(0xE0) << 23;
176
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
177
+ const float exp_scale = 0x1.0p-112f;
178
+ #else
179
+ const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
180
+ #endif
181
+ const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
182
+
183
+ const uint32_t magic_mask = UINT32_C(126) << 23;
184
+ const float magic_bias = 0.5f;
185
+ const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
186
+
187
+ const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
188
+ const uint32_t result = sign |
189
+ (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
190
+ return fp32_from_bits(result);
191
+ }
192
+
193
+ static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
194
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
195
+ const float scale_to_inf = 0x1.0p+112f;
196
+ const float scale_to_zero = 0x1.0p-110f;
197
+ #else
198
+ const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
199
+ const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
200
+ #endif
201
+ float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
202
+
203
+ const uint32_t w = fp32_to_bits(f);
204
+ const uint32_t shl1_w = w + w;
205
+ const uint32_t sign = w & UINT32_C(0x80000000);
206
+ uint32_t bias = shl1_w & UINT32_C(0xFF000000);
207
+ if (bias < UINT32_C(0x71000000)) {
208
+ bias = UINT32_C(0x71000000);
209
+ }
210
+
211
+ base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
212
+ const uint32_t bits = fp32_to_bits(base);
213
+ const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
214
+ const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
215
+ const uint32_t nonsign = exp_bits + mantissa_bits;
216
+ return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
217
+ }
218
+
219
+ #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
220
+ #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
221
+
222
+ #endif // __F16C__
223
+
224
+ #endif // __ARM_NEON
225
+
226
+ // precomputed f32 table for f16 (256 KB)
227
+ // defined in ggml.c, initialized in ggml_init()
228
+ extern float ggml_table_f32_f16[1 << 16];
229
+
230
+ // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
231
+ // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
232
+ // This is also true for POWER9.
233
+ #if !defined(GGML_FP16_TO_FP32)
234
+ inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
235
+ uint16_t s;
236
+ memcpy(&s, &f, sizeof(uint16_t));
237
+ return ggml_table_f32_f16[s];
238
+ }
239
+
240
+ #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
241
+ #endif
242
+
243
+ #if !defined(GGML_FP32_TO_FP16)
244
+ #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
245
+ #endif
246
+
247
+ #define GGML_HASHTABLE_FULL ((size_t)-1)
248
+ #define GGML_HASHTABLE_ALREADY_EXISTS ((size_t)-2)
249
+
250
+ struct ggml_hash_set ggml_hash_set_new(size_t size);
251
+
252
+ bool ggml_hash_contains (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
253
+
254
+ // returns GGML_HASHTABLE_FULL if table is full, otherwise the current index of the key or where it should be inserted
255
+ size_t ggml_hash_find (const struct ggml_hash_set hash_set, struct ggml_tensor * key);
256
+
257
+ // returns GGML_HASHTABLE_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
258
+ size_t ggml_hash_insert ( struct ggml_hash_set hash_set, struct ggml_tensor * key);
259
+
260
+ // return index, asserts if table is full
261
+ size_t ggml_hash_find_or_insert( struct ggml_hash_set hash_set, struct ggml_tensor * key);
262
+
263
+ #ifdef __cplusplus
264
+ }
265
+ #endif