whisper.rn 0.4.0-rc.8 → 0.4.0

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 (201) hide show
  1. package/README.md +5 -1
  2. package/android/build.gradle +12 -3
  3. package/android/src/main/CMakeLists.txt +44 -13
  4. package/android/src/main/java/com/rnwhisper/AudioUtils.java +27 -12
  5. package/android/src/main/java/com/rnwhisper/RNWhisper.java +75 -34
  6. package/android/src/main/java/com/rnwhisper/WhisperContext.java +53 -38
  7. package/android/src/main/jni.cpp +38 -1
  8. package/android/src/main/jniLibs/arm64-v8a/librnwhisper.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnwhisper_v8fp16_va_2.so +0 -0
  10. package/android/src/main/jniLibs/armeabi-v7a/librnwhisper.so +0 -0
  11. package/android/src/main/jniLibs/armeabi-v7a/librnwhisper_vfpv4.so +0 -0
  12. package/android/src/main/jniLibs/x86_64/librnwhisper.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnwhisper_x86_64.so +0 -0
  14. package/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
  15. package/android/src/oldarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
  16. package/cpp/coreml/whisper-compat.h +10 -0
  17. package/cpp/coreml/whisper-compat.m +35 -0
  18. package/cpp/coreml/whisper-decoder-impl.h +27 -15
  19. package/cpp/coreml/whisper-decoder-impl.m +36 -10
  20. package/cpp/coreml/whisper-encoder-impl.h +21 -9
  21. package/cpp/coreml/whisper-encoder-impl.m +29 -3
  22. package/cpp/ggml-alloc.c +727 -517
  23. package/cpp/ggml-alloc.h +47 -65
  24. package/cpp/ggml-backend-impl.h +196 -57
  25. package/cpp/ggml-backend-reg.cpp +591 -0
  26. package/cpp/ggml-backend.cpp +2016 -0
  27. package/cpp/ggml-backend.h +234 -89
  28. package/cpp/ggml-common.h +1861 -0
  29. package/cpp/ggml-cpp.h +39 -0
  30. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  31. package/cpp/ggml-cpu/amx/amx.h +8 -0
  32. package/cpp/ggml-cpu/amx/common.h +91 -0
  33. package/cpp/ggml-cpu/amx/mmq.cpp +2511 -0
  34. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  35. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  36. package/cpp/ggml-cpu/arch/arm/quants.c +4113 -0
  37. package/cpp/ggml-cpu/arch/arm/repack.cpp +2162 -0
  38. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  39. package/cpp/ggml-cpu/arch/x86/quants.c +4310 -0
  40. package/cpp/ggml-cpu/arch/x86/repack.cpp +3284 -0
  41. package/cpp/ggml-cpu/arch-fallback.h +184 -0
  42. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  43. package/cpp/ggml-cpu/binary-ops.h +16 -0
  44. package/cpp/ggml-cpu/common.h +72 -0
  45. package/cpp/ggml-cpu/ggml-cpu-impl.h +511 -0
  46. package/cpp/ggml-cpu/ggml-cpu.c +3473 -0
  47. package/cpp/ggml-cpu/ggml-cpu.cpp +671 -0
  48. package/cpp/ggml-cpu/ops.cpp +9085 -0
  49. package/cpp/ggml-cpu/ops.h +111 -0
  50. package/cpp/ggml-cpu/quants.c +1157 -0
  51. package/cpp/ggml-cpu/quants.h +89 -0
  52. package/cpp/ggml-cpu/repack.cpp +1570 -0
  53. package/cpp/ggml-cpu/repack.h +98 -0
  54. package/cpp/ggml-cpu/simd-mappings.h +1006 -0
  55. package/cpp/ggml-cpu/traits.cpp +36 -0
  56. package/cpp/ggml-cpu/traits.h +38 -0
  57. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  58. package/cpp/ggml-cpu/unary-ops.h +28 -0
  59. package/cpp/ggml-cpu/vec.cpp +321 -0
  60. package/cpp/ggml-cpu/vec.h +973 -0
  61. package/cpp/ggml-cpu.h +143 -0
  62. package/cpp/ggml-impl.h +525 -168
  63. package/cpp/ggml-metal-impl.h +622 -0
  64. package/cpp/ggml-metal.h +16 -14
  65. package/cpp/ggml-metal.m +5289 -1859
  66. package/cpp/ggml-opt.cpp +1037 -0
  67. package/cpp/ggml-opt.h +237 -0
  68. package/cpp/ggml-quants.c +2916 -6877
  69. package/cpp/ggml-quants.h +87 -249
  70. package/cpp/ggml-threading.cpp +12 -0
  71. package/cpp/ggml-threading.h +14 -0
  72. package/cpp/ggml-whisper-sim.metallib +0 -0
  73. package/cpp/ggml-whisper.metallib +0 -0
  74. package/cpp/ggml.c +3293 -16770
  75. package/cpp/ggml.h +778 -835
  76. package/cpp/gguf.cpp +1347 -0
  77. package/cpp/gguf.h +202 -0
  78. package/cpp/rn-whisper.cpp +84 -0
  79. package/cpp/rn-whisper.h +2 -0
  80. package/cpp/whisper-arch.h +197 -0
  81. package/cpp/whisper.cpp +3240 -944
  82. package/cpp/whisper.h +144 -31
  83. package/ios/CMakeLists.txt +95 -0
  84. package/ios/RNWhisper.h +5 -0
  85. package/ios/RNWhisper.mm +124 -37
  86. package/ios/RNWhisperAudioUtils.h +1 -0
  87. package/ios/RNWhisperAudioUtils.m +24 -13
  88. package/ios/RNWhisperContext.h +8 -2
  89. package/ios/RNWhisperContext.mm +42 -8
  90. package/ios/rnwhisper.xcframework/Info.plist +74 -0
  91. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  92. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  93. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  94. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  95. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  96. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  97. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  98. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  99. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  100. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  101. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  102. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  103. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
  104. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
  105. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  106. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  107. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  108. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  109. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
  110. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Info.plist +0 -0
  111. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
  112. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/rnwhisper +0 -0
  113. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  114. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  115. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  116. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  117. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  118. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  119. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  120. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  121. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  122. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  123. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  124. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  125. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
  126. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
  127. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  128. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  129. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  130. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  131. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
  132. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
  133. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
  134. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
  135. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
  136. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  137. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  138. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  139. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  140. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  141. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  142. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  143. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  144. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  145. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  146. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  147. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  148. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
  149. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
  150. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  151. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  152. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  153. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  154. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
  155. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Info.plist +0 -0
  156. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
  157. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/rnwhisper +0 -0
  158. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  159. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  160. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  161. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  162. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  163. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  164. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  165. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  166. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  167. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  168. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  169. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  170. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
  171. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
  172. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  173. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  174. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  175. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  176. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
  177. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
  178. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
  179. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
  180. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
  181. package/jest/mock.js +14 -1
  182. package/lib/commonjs/NativeRNWhisper.js.map +1 -1
  183. package/lib/commonjs/index.js +48 -19
  184. package/lib/commonjs/index.js.map +1 -1
  185. package/lib/commonjs/version.json +1 -1
  186. package/lib/module/NativeRNWhisper.js.map +1 -1
  187. package/lib/module/index.js +48 -19
  188. package/lib/module/index.js.map +1 -1
  189. package/lib/module/version.json +1 -1
  190. package/lib/typescript/NativeRNWhisper.d.ts +6 -3
  191. package/lib/typescript/NativeRNWhisper.d.ts.map +1 -1
  192. package/lib/typescript/index.d.ts +25 -3
  193. package/lib/typescript/index.d.ts.map +1 -1
  194. package/package.json +15 -10
  195. package/src/NativeRNWhisper.ts +12 -3
  196. package/src/index.ts +63 -24
  197. package/src/version.json +1 -1
  198. package/whisper-rn.podspec +18 -18
  199. package/cpp/README.md +0 -4
  200. package/cpp/ggml-backend.c +0 -1718
  201. package/cpp/ggml-metal-whisper.metal +0 -5820
@@ -0,0 +1,603 @@
1
+ #pragma once
2
+
3
+ // GGML internal header
4
+
5
+ #include "ggml.h"
6
+ #include "gguf.h"
7
+
8
+ #include <assert.h>
9
+ #include <math.h>
10
+ #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
11
+ #include <stdbool.h>
12
+ #include <stdint.h>
13
+ #include <string.h>
14
+
15
+ #ifdef __ARM_FEATURE_SVE
16
+ #include <arm_sve.h>
17
+ #endif // __ARM_FEATURE_SVE
18
+
19
+ #if defined(__ARM_NEON) && !defined(__CUDACC__) && !defined(__MUSACC__)
20
+ // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
21
+ //
22
+ // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
23
+ //
24
+ #include <arm_neon.h>
25
+ #endif
26
+
27
+ #if defined(__F16C__)
28
+ #include <immintrin.h>
29
+ #endif
30
+
31
+ #ifdef __cplusplus
32
+ extern "C" {
33
+ #endif
34
+
35
+ void wsp_ggml_print_backtrace(void);
36
+
37
+ #ifndef MIN
38
+ # define MIN(a, b) ((a) < (b) ? (a) : (b))
39
+ #endif
40
+
41
+ #ifndef MAX
42
+ # define MAX(a, b) ((a) > (b) ? (a) : (b))
43
+ #endif
44
+
45
+ // required for mmap as gguf only guarantees 32-byte alignment
46
+ #define TENSOR_ALIGNMENT 32
47
+
48
+ // static_assert should be a #define, but if it's not,
49
+ // fall back to the _Static_assert C11 keyword.
50
+ // if C99 - static_assert is noop
51
+ // ref: https://stackoverflow.com/a/53923785/4039976
52
+ #ifndef __cplusplus
53
+ #ifndef static_assert
54
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
55
+ #define static_assert(cond, msg) _Static_assert(cond, msg)
56
+ #else
57
+ #define static_assert(cond, msg) struct global_scope_noop_trick
58
+ #endif
59
+ #endif
60
+ #endif
61
+
62
+ static inline int wsp_ggml_up32(int n) {
63
+ return (n + 31) & ~31;
64
+ }
65
+
66
+ //static inline int wsp_ggml_up64(int n) {
67
+ // return (n + 63) & ~63;
68
+ //}
69
+
70
+ static inline int wsp_ggml_up(int n, int m) {
71
+ // assert m is a power of 2
72
+ WSP_GGML_ASSERT((m & (m - 1)) == 0);
73
+ return (n + m - 1) & ~(m - 1);
74
+ }
75
+
76
+ //
77
+ // logging
78
+ //
79
+
80
+ WSP_GGML_ATTRIBUTE_FORMAT(2, 3)
81
+ WSP_GGML_API void wsp_ggml_log_internal (enum wsp_ggml_log_level level, const char * format, ...);
82
+ WSP_GGML_API void wsp_ggml_log_callback_default(enum wsp_ggml_log_level level, const char * text, void * user_data);
83
+
84
+ #define WSP_GGML_LOG(...) wsp_ggml_log_internal(WSP_GGML_LOG_LEVEL_NONE , __VA_ARGS__)
85
+ #define WSP_GGML_LOG_INFO(...) wsp_ggml_log_internal(WSP_GGML_LOG_LEVEL_INFO , __VA_ARGS__)
86
+ #define WSP_GGML_LOG_WARN(...) wsp_ggml_log_internal(WSP_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
87
+ #define WSP_GGML_LOG_ERROR(...) wsp_ggml_log_internal(WSP_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
88
+ #define WSP_GGML_LOG_DEBUG(...) wsp_ggml_log_internal(WSP_GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
89
+ #define WSP_GGML_LOG_CONT(...) wsp_ggml_log_internal(WSP_GGML_LOG_LEVEL_CONT , __VA_ARGS__)
90
+
91
+ #define WSP_GGML_DEBUG 0
92
+
93
+ #if (WSP_GGML_DEBUG >= 1)
94
+ #define WSP_GGML_PRINT_DEBUG(...) WSP_GGML_LOG_DEBUG(__VA_ARGS__)
95
+ #else
96
+ #define WSP_GGML_PRINT_DEBUG(...)
97
+ #endif
98
+
99
+ #if (WSP_GGML_DEBUG >= 5)
100
+ #define WSP_GGML_PRINT_DEBUG_5(...) WSP_GGML_LOG_DEBUG(__VA_ARGS__)
101
+ #else
102
+ #define WSP_GGML_PRINT_DEBUG_5(...)
103
+ #endif
104
+
105
+ #if (WSP_GGML_DEBUG >= 10)
106
+ #define WSP_GGML_PRINT_DEBUG_10(...) WSP_GGML_LOG_DEBUG(__VA_ARGS__)
107
+ #else
108
+ #define WSP_GGML_PRINT_DEBUG_10(...)
109
+ #endif
110
+
111
+ // tensor params
112
+
113
+ static void wsp_ggml_set_op_params(struct wsp_ggml_tensor * tensor, const void * params, size_t params_size) {
114
+ WSP_GGML_ASSERT(tensor != NULL); // silence -Warray-bounds warnings
115
+ assert(params_size <= WSP_GGML_MAX_OP_PARAMS);
116
+ memcpy(tensor->op_params, params, params_size);
117
+ }
118
+
119
+ static int32_t wsp_ggml_get_op_params_i32(const struct wsp_ggml_tensor * tensor, uint32_t i) {
120
+ assert(i < WSP_GGML_MAX_OP_PARAMS / sizeof(int32_t));
121
+ return ((const int32_t *)(tensor->op_params))[i];
122
+ }
123
+
124
+ static float wsp_ggml_get_op_params_f32(const struct wsp_ggml_tensor * tensor, uint32_t i) {
125
+ assert(i < WSP_GGML_MAX_OP_PARAMS / sizeof(float));
126
+ return ((const float *)(tensor->op_params))[i];
127
+ }
128
+
129
+ static void wsp_ggml_set_op_params_i32(struct wsp_ggml_tensor * tensor, uint32_t i, int32_t value) {
130
+ assert(i < WSP_GGML_MAX_OP_PARAMS / sizeof(int32_t));
131
+ ((int32_t *)(tensor->op_params))[i] = value;
132
+ }
133
+
134
+ static void wsp_ggml_set_op_params_f32(struct wsp_ggml_tensor * tensor, uint32_t i, float value) {
135
+ assert(i < WSP_GGML_MAX_OP_PARAMS / sizeof(float));
136
+ ((float *)(tensor->op_params))[i] = value;
137
+ }
138
+
139
+ struct wsp_ggml_map_custom1_op_params {
140
+ wsp_ggml_custom1_op_t fun;
141
+ int n_tasks;
142
+ void * userdata;
143
+ };
144
+
145
+ struct wsp_ggml_map_custom2_op_params {
146
+ wsp_ggml_custom2_op_t fun;
147
+ int n_tasks;
148
+ void * userdata;
149
+ };
150
+
151
+ struct wsp_ggml_map_custom3_op_params {
152
+ wsp_ggml_custom3_op_t fun;
153
+ int n_tasks;
154
+ void * userdata;
155
+ };
156
+
157
+ struct wsp_ggml_custom_op_params {
158
+ wsp_ggml_custom_op_t fun;
159
+ int n_tasks;
160
+ void * userdata;
161
+ };
162
+
163
+ // bitset
164
+
165
+ typedef uint32_t wsp_ggml_bitset_t;
166
+
167
+ static_assert(sizeof(wsp_ggml_bitset_t) == 4, "bitset_t constants must be updated");
168
+ #define BITSET_SHR 5 // log2(sizeof(wsp_ggml_bitset_t)*8)
169
+ #define BITSET_MASK (sizeof(wsp_ggml_bitset_t)*8 - 1)
170
+
171
+ static size_t wsp_ggml_bitset_size(size_t n) {
172
+ return (n + BITSET_MASK) >> BITSET_SHR;
173
+ }
174
+
175
+ static inline bool wsp_ggml_bitset_get(const wsp_ggml_bitset_t * bitset, size_t i) {
176
+ return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
177
+ }
178
+
179
+ static inline void wsp_ggml_bitset_set(wsp_ggml_bitset_t * bitset, size_t i) {
180
+ bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
181
+ }
182
+
183
+ static inline void wsp_ggml_bitset_clear(wsp_ggml_bitset_t * bitset, size_t i) {
184
+ bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
185
+ }
186
+
187
+ // hash set
188
+
189
+ #define WSP_GGML_HASHSET_FULL ((size_t)-1)
190
+ #define WSP_GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
191
+
192
+ struct wsp_ggml_hash_set {
193
+ size_t size;
194
+ wsp_ggml_bitset_t * used; // whether or not the keys are in use i.e. set
195
+ struct wsp_ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if wsp_ggml_bitset_get(used, i)
196
+ };
197
+
198
+ struct wsp_ggml_hash_set wsp_ggml_hash_set_new(size_t size);
199
+ void wsp_ggml_hash_set_free(struct wsp_ggml_hash_set * hash_set);
200
+
201
+ // returns the minimum size for a hash set that can hold min_sz elements
202
+ size_t wsp_ggml_hash_size(size_t min_sz);
203
+
204
+ // remove all elements from the hash set
205
+ void wsp_ggml_hash_set_reset(struct wsp_ggml_hash_set * hash_set);
206
+
207
+ // returns true if key is in the hash set
208
+ static bool wsp_ggml_hash_contains(const struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor * key);
209
+
210
+ // returns WSP_GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
211
+ static size_t wsp_ggml_hash_find(const struct wsp_ggml_hash_set * hash_set, const struct wsp_ggml_tensor * key);
212
+
213
+ // returns WSP_GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
214
+ static size_t wsp_ggml_hash_insert(struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor * key);
215
+
216
+ // return index, asserts if table is full
217
+ static size_t wsp_ggml_hash_find_or_insert(struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor * key);
218
+
219
+ // hash function for wsp_ggml_tensor
220
+ static inline size_t wsp_ggml_hash(const struct wsp_ggml_tensor * p) {
221
+ // the last 4 bits are always zero due to alignment
222
+ return (size_t)(uintptr_t)p >> 4;
223
+ }
224
+
225
+ static size_t wsp_ggml_hash_find(const struct wsp_ggml_hash_set * hash_set, const struct wsp_ggml_tensor * key) {
226
+ size_t h = wsp_ggml_hash(key) % hash_set->size;
227
+
228
+ // linear probing
229
+ size_t i = h;
230
+ while (wsp_ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
231
+ i = (i + 1) % hash_set->size;
232
+ if (i == h) {
233
+ // visited all hash table entries -> not found
234
+ return WSP_GGML_HASHSET_FULL;
235
+ }
236
+ }
237
+ return i;
238
+ }
239
+
240
+ static bool wsp_ggml_hash_contains(const struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor * key) {
241
+ size_t i = wsp_ggml_hash_find(hash_set, key);
242
+ return i != WSP_GGML_HASHSET_FULL && wsp_ggml_bitset_get(hash_set->used, i);
243
+ }
244
+
245
+ static size_t wsp_ggml_hash_insert(struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor * key) {
246
+ size_t h = wsp_ggml_hash(key) % hash_set->size;
247
+
248
+ // linear probing
249
+ size_t i = h;
250
+ do {
251
+ if (!wsp_ggml_bitset_get(hash_set->used, i)) {
252
+ wsp_ggml_bitset_set(hash_set->used, i);
253
+ hash_set->keys[i] = key;
254
+ return i;
255
+ }
256
+ if (hash_set->keys[i] == key) {
257
+ return WSP_GGML_HASHSET_ALREADY_EXISTS;
258
+ }
259
+ i = (i + 1) % hash_set->size;
260
+ } while (i != h);
261
+
262
+ // visited all hash table entries -> not found
263
+ WSP_GGML_ABORT("fatal error");
264
+ }
265
+
266
+ static size_t wsp_ggml_hash_find_or_insert(struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor * key) {
267
+ size_t h = wsp_ggml_hash(key) % hash_set->size;
268
+
269
+ // linear probing
270
+ size_t i = h;
271
+ do {
272
+ if (!wsp_ggml_bitset_get(hash_set->used, i)) {
273
+ wsp_ggml_bitset_set(hash_set->used, i);
274
+ hash_set->keys[i] = key;
275
+ return i;
276
+ }
277
+ if (hash_set->keys[i] == key) {
278
+ return i;
279
+ }
280
+ i = (i + 1) % hash_set->size;
281
+ } while (i != h);
282
+
283
+ // visited all hash table entries -> not found
284
+ WSP_GGML_ABORT("fatal error");
285
+ }
286
+
287
+ // computation graph
288
+
289
+ enum wsp_ggml_cgraph_eval_order {
290
+ WSP_GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
291
+ WSP_GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
292
+ WSP_GGML_CGRAPH_EVAL_ORDER_COUNT
293
+ };
294
+
295
+ struct wsp_ggml_cgraph {
296
+ int size; // maximum number of nodes/leafs/grads/grad_accs
297
+ int n_nodes; // number of nodes currently in use
298
+ int n_leafs; // number of leafs currently in use
299
+
300
+ struct wsp_ggml_tensor ** nodes; // tensors with data that can change if the graph is evaluated
301
+ struct wsp_ggml_tensor ** grads; // the outputs of these tensors are the gradients of the nodes
302
+ struct wsp_ggml_tensor ** grad_accs; // accumulators for node gradients
303
+ struct wsp_ggml_tensor ** leafs; // tensors with constant data
304
+
305
+ struct wsp_ggml_hash_set visited_hash_set;
306
+
307
+ enum wsp_ggml_cgraph_eval_order order;
308
+ };
309
+
310
+ // returns a slice of cgraph with nodes [i0, i1)
311
+ // the slice does not have leafs or gradients
312
+ // if you need the gradients, get them from the original graph
313
+ struct wsp_ggml_cgraph wsp_ggml_graph_view(struct wsp_ggml_cgraph * cgraph, int i0, int i1);
314
+
315
+ // Memory allocation
316
+
317
+ WSP_GGML_API void * wsp_ggml_aligned_malloc(size_t size);
318
+ WSP_GGML_API void wsp_ggml_aligned_free(void * ptr, size_t size);
319
+
320
+ // FP16 to FP32 conversion
321
+
322
+ // 16-bit float
323
+ // on Arm, we use __fp16
324
+ // on x86, we use uint16_t
325
+ //
326
+ // for old CUDA compilers (<= 11), we use uint16_t: ref https://github.com/ggml-org/llama.cpp/pull/10616
327
+ // for MUSA compilers , we use uint16_t: ref https://github.com/ggml-org/llama.cpp/pull/11843
328
+ //
329
+ #if defined(__ARM_NEON) && !(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ <= 11) && !defined(__MUSACC__)
330
+ #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
331
+ #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) wsp_ggml_compute_fp32_to_fp16(x)
332
+
333
+ #define WSP_GGML_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
334
+
335
+ static inline float wsp_ggml_compute_fp16_to_fp32(wsp_ggml_fp16_t h) {
336
+ __fp16 tmp;
337
+ memcpy(&tmp, &h, sizeof(wsp_ggml_fp16_t));
338
+ return (float)tmp;
339
+ }
340
+
341
+ static inline wsp_ggml_fp16_t wsp_ggml_compute_fp32_to_fp16(float f) {
342
+ wsp_ggml_fp16_t res;
343
+ __fp16 tmp = f;
344
+ memcpy(&res, &tmp, sizeof(wsp_ggml_fp16_t));
345
+ return res;
346
+ }
347
+
348
+ #elif defined(__F16C__)
349
+
350
+ #ifdef _MSC_VER
351
+ #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
352
+ #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
353
+ #else
354
+ #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
355
+ #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
356
+ #endif
357
+
358
+ #elif defined(__POWER9_VECTOR__)
359
+
360
+ #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
361
+ #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) wsp_ggml_compute_fp32_to_fp16(x)
362
+ /* the inline asm below is about 12% faster than the lookup method */
363
+ #define WSP_GGML_FP16_TO_FP32(x) WSP_GGML_COMPUTE_FP16_TO_FP32(x)
364
+ #define WSP_GGML_FP32_TO_FP16(x) WSP_GGML_COMPUTE_FP32_TO_FP16(x)
365
+
366
+ static inline float wsp_ggml_compute_fp16_to_fp32(wsp_ggml_fp16_t h) {
367
+ float f;
368
+ double d;
369
+ __asm__(
370
+ "mtfprd %0,%2\n"
371
+ "xscvhpdp %0,%0\n"
372
+ "frsp %1,%0\n" :
373
+ /* temp */ "=d"(d),
374
+ /* out */ "=f"(f):
375
+ /* in */ "r"(h));
376
+ return f;
377
+ }
378
+
379
+ static inline wsp_ggml_fp16_t wsp_ggml_compute_fp32_to_fp16(float f) {
380
+ double d;
381
+ wsp_ggml_fp16_t r;
382
+ __asm__( /* xscvdphp can work on double or single precision */
383
+ "xscvdphp %0,%2\n"
384
+ "mffprd %1,%0\n" :
385
+ /* temp */ "=d"(d),
386
+ /* out */ "=r"(r):
387
+ /* in */ "f"(f));
388
+ return r;
389
+ }
390
+
391
+ #elif defined(__riscv) && defined(__riscv_zfhmin)
392
+
393
+ static inline float wsp_ggml_compute_fp16_to_fp32(wsp_ggml_fp16_t h) {
394
+ float f;
395
+ __asm__(
396
+ "fmv.h.x %[f], %[h]\n\t"
397
+ "fcvt.s.h %[f], %[f]"
398
+ : [f] "=&f" (f)
399
+ : [h] "r" (h)
400
+ );
401
+ return f;
402
+ }
403
+
404
+ static inline wsp_ggml_fp16_t wsp_ggml_compute_fp32_to_fp16(float f) {
405
+ wsp_ggml_fp16_t res;
406
+ __asm__(
407
+ "fcvt.h.s %[f], %[f]\n\t"
408
+ "fmv.x.h %[h], %[f]"
409
+ : [h] "=&r" (res)
410
+ : [f] "f" (f)
411
+ );
412
+ return res;
413
+ }
414
+
415
+ #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
416
+ #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) wsp_ggml_compute_fp32_to_fp16(x)
417
+ #define WSP_GGML_FP16_TO_FP32(x) WSP_GGML_COMPUTE_FP16_TO_FP32(x)
418
+ #define WSP_GGML_FP32_TO_FP16(x) WSP_GGML_COMPUTE_FP32_TO_FP16(x)
419
+
420
+ #else
421
+
422
+ // FP16 <-> FP32
423
+ // ref: https://github.com/Maratyszcza/FP16
424
+
425
+ static inline float fp32_from_bits(uint32_t w) {
426
+ union {
427
+ uint32_t as_bits;
428
+ float as_value;
429
+ } fp32;
430
+ fp32.as_bits = w;
431
+ return fp32.as_value;
432
+ }
433
+
434
+ static inline uint32_t fp32_to_bits(float f) {
435
+ union {
436
+ float as_value;
437
+ uint32_t as_bits;
438
+ } fp32;
439
+ fp32.as_value = f;
440
+ return fp32.as_bits;
441
+ }
442
+
443
+ static inline float wsp_ggml_compute_fp16_to_fp32(wsp_ggml_fp16_t h) {
444
+ const uint32_t w = (uint32_t) h << 16;
445
+ const uint32_t sign = w & UINT32_C(0x80000000);
446
+ const uint32_t two_w = w + w;
447
+
448
+ const uint32_t exp_offset = UINT32_C(0xE0) << 23;
449
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
450
+ const float exp_scale = 0x1.0p-112f;
451
+ #else
452
+ const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
453
+ #endif
454
+ const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
455
+
456
+ const uint32_t magic_mask = UINT32_C(126) << 23;
457
+ const float magic_bias = 0.5f;
458
+ const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
459
+
460
+ const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
461
+ const uint32_t result = sign |
462
+ (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
463
+ return fp32_from_bits(result);
464
+ }
465
+
466
+ static inline wsp_ggml_fp16_t wsp_ggml_compute_fp32_to_fp16(float f) {
467
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
468
+ const float scale_to_inf = 0x1.0p+112f;
469
+ const float scale_to_zero = 0x1.0p-110f;
470
+ #else
471
+ const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
472
+ const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
473
+ #endif
474
+ float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
475
+
476
+ const uint32_t w = fp32_to_bits(f);
477
+ const uint32_t shl1_w = w + w;
478
+ const uint32_t sign = w & UINT32_C(0x80000000);
479
+ uint32_t bias = shl1_w & UINT32_C(0xFF000000);
480
+ if (bias < UINT32_C(0x71000000)) {
481
+ bias = UINT32_C(0x71000000);
482
+ }
483
+
484
+ base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
485
+ const uint32_t bits = fp32_to_bits(base);
486
+ const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
487
+ const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
488
+ const uint32_t nonsign = exp_bits + mantissa_bits;
489
+ return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
490
+ }
491
+
492
+ #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
493
+ #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) wsp_ggml_compute_fp32_to_fp16(x)
494
+
495
+ #endif // defined(__ARM_NEON) && !(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ <= 11) && !defined(__MUSACC__)
496
+
497
+ // precomputed f32 table for f16 (256 KB)
498
+ // defined in ggml.c, initialized in wsp_ggml_init()
499
+ WSP_GGML_API float wsp_ggml_table_f32_f16[1 << 16];
500
+
501
+ // On ARM NEON, it's quicker to directly convert x -> x instead of calling into wsp_ggml_lookup_fp16_to_fp32,
502
+ // so we define WSP_GGML_FP16_TO_FP32 and WSP_GGML_FP32_TO_FP16 elsewhere for NEON.
503
+ // This is also true for POWER9.
504
+ #if !defined(WSP_GGML_FP16_TO_FP32)
505
+ inline static float wsp_ggml_lookup_fp16_to_fp32(wsp_ggml_fp16_t f) {
506
+ uint16_t s;
507
+ memcpy(&s, &f, sizeof(uint16_t));
508
+ return wsp_ggml_table_f32_f16[s];
509
+ }
510
+
511
+ #define WSP_GGML_FP16_TO_FP32(x) wsp_ggml_lookup_fp16_to_fp32(x)
512
+ #endif
513
+
514
+ #if !defined(WSP_GGML_FP32_TO_FP16)
515
+ #define WSP_GGML_FP32_TO_FP16(x) WSP_GGML_COMPUTE_FP32_TO_FP16(x)
516
+ #endif
517
+
518
+ /**
519
+ * Converts brain16 to float32.
520
+ *
521
+ * The bfloat16 floating point format has the following structure:
522
+ *
523
+ * ┌sign
524
+ * │
525
+ * │ ┌exponent
526
+ * │ │
527
+ * │ │ ┌mantissa
528
+ * │ │ │
529
+ * │┌──┴───┐┌─┴───┐
530
+ * 0b0000000000000000 brain16
531
+ *
532
+ * Since bf16 has the same number of exponent bits as a 32bit float,
533
+ * encoding and decoding numbers becomes relatively straightforward.
534
+ *
535
+ * ┌sign
536
+ * │
537
+ * │ ┌exponent
538
+ * │ │
539
+ * │ │ ┌mantissa
540
+ * │ │ │
541
+ * │┌──┴───┐┌─┴───────────────────┐
542
+ * 0b00000000000000000000000000000000 IEEE binary32
543
+ *
544
+ * For comparison, the standard fp16 format has fewer exponent bits.
545
+ *
546
+ * ┌sign
547
+ * │
548
+ * │ ┌exponent
549
+ * │ │
550
+ * │ │ ┌mantissa
551
+ * │ │ │
552
+ * │┌─┴─┐┌─┴──────┐
553
+ * 0b0000000000000000 IEEE binary16
554
+ *
555
+ * @see IEEE 754-2008
556
+ */
557
+ static inline float wsp_ggml_compute_bf16_to_fp32(wsp_ggml_bf16_t h) {
558
+ union {
559
+ float f;
560
+ uint32_t i;
561
+ } u;
562
+ u.i = (uint32_t)h.bits << 16;
563
+ return u.f;
564
+ }
565
+
566
+ /**
567
+ * Converts float32 to brain16.
568
+ *
569
+ * This is binary identical with Google Brain float conversion.
570
+ * Floats shall round to nearest even, and NANs shall be quiet.
571
+ * Subnormals aren't flushed to zero, except perhaps when used.
572
+ * This code should vectorize nicely if using modern compilers.
573
+ */
574
+ static inline wsp_ggml_bf16_t wsp_ggml_compute_fp32_to_bf16(float s) {
575
+ wsp_ggml_bf16_t h;
576
+ union {
577
+ float f;
578
+ uint32_t i;
579
+ } u;
580
+ u.f = s;
581
+ if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
582
+ h.bits = (u.i >> 16) | 64; /* force to quiet */
583
+ return h;
584
+ }
585
+ h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
586
+ return h;
587
+ }
588
+
589
+ #define WSP_GGML_FP32_TO_BF16(x) wsp_ggml_compute_fp32_to_bf16(x)
590
+ #define WSP_GGML_BF16_TO_FP32(x) wsp_ggml_compute_bf16_to_fp32(x)
591
+
592
+ #ifdef __cplusplus
593
+ }
594
+ #endif
595
+
596
+ #ifdef __cplusplus
597
+ #include <vector>
598
+
599
+ // expose GGUF internals for test code
600
+ WSP_GGML_API size_t wsp_gguf_type_size(enum wsp_gguf_type type);
601
+ WSP_GGML_API struct wsp_gguf_context * wsp_gguf_init_from_file_impl(FILE * file, struct wsp_gguf_init_params params);
602
+ WSP_GGML_API void wsp_gguf_write_to_buf(const struct wsp_gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta);
603
+ #endif // __cplusplus