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
package/cpp/ggml-impl.h CHANGED
@@ -1,219 +1,507 @@
1
1
  #pragma once
2
2
 
3
- #include "ggml.h"
4
-
5
3
  // GGML internal header
6
4
 
5
+ #include "ggml.h"
6
+ #include "gguf.h"
7
+
7
8
  #include <assert.h>
9
+ #include <math.h>
8
10
  #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
11
  #include <stdbool.h>
11
- #include <string.h> // memcpy
12
- #include <math.h> // fabsf
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
13
30
 
14
31
  #ifdef __cplusplus
15
32
  extern "C" {
16
33
  #endif
17
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
+
18
48
  // static_assert should be a #define, but if it's not,
19
49
  // fall back to the _Static_assert C11 keyword.
20
50
  // if C99 - static_assert is noop
21
51
  // ref: https://stackoverflow.com/a/53923785/4039976
22
- #ifndef static_assert
23
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
24
- #define static_assert(cond, msg) _Static_assert(cond, msg)
25
- #else
26
- #define static_assert(cond, msg) struct global_scope_noop_trick
27
- #endif
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
28
60
  #endif
29
61
 
30
- // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
31
- #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
32
- #ifndef __FMA__
33
- #define __FMA__
34
- #endif
35
- #ifndef __F16C__
36
- #define __F16C__
37
- #endif
38
- #ifndef __SSE3__
39
- #define __SSE3__
40
- #endif
41
- #endif
62
+ static inline int wsp_ggml_up32(int n) {
63
+ return (n + 31) & ~31;
64
+ }
42
65
 
43
- // 16-bit float
44
- // on Arm, we use __fp16
45
- // on x86, we use uint16_t
46
- #if defined(__ARM_NEON) && !defined(_MSC_VER)
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
+ }
47
75
 
48
- // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
49
76
  //
50
- // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
77
+ // logging
51
78
  //
52
- #include <arm_neon.h>
53
79
 
54
- #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) ((float) (x))
55
- #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) (x)
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);
56
83
 
57
- #define WSP_GGML_FP16_TO_FP32(x) ((float) (x))
58
- #define WSP_GGML_FP32_TO_FP16(x) (x)
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__)
59
90
 
60
- #else
91
+ #define WSP_GGML_DEBUG 0
61
92
 
62
- #ifdef __wasm_simd128__
63
- #include <wasm_simd128.h>
93
+ #if (WSP_GGML_DEBUG >= 1)
94
+ #define WSP_GGML_PRINT_DEBUG(...) WSP_GGML_LOG_DEBUG(__VA_ARGS__)
64
95
  #else
65
- #ifdef __POWER9_VECTOR__
66
- #include <altivec.h>
67
- #undef bool
68
- #define bool _Bool
69
- #else
70
- #if defined(_MSC_VER) || defined(__MINGW32__)
71
- #include <intrin.h>
72
- #else
73
- #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
74
- #if !defined(__riscv)
75
- #include <immintrin.h>
76
- #endif
77
- #endif
78
- #endif
79
- #endif
96
+ #define WSP_GGML_PRINT_DEBUG(...)
80
97
  #endif
81
98
 
82
- #ifdef __riscv_v_intrinsic
83
- #include <riscv_vector.h>
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(...)
84
103
  #endif
85
104
 
86
- #ifdef __F16C__
87
-
88
- #ifdef _MSC_VER
89
- #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
90
- #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
105
+ #if (WSP_GGML_DEBUG >= 10)
106
+ #define WSP_GGML_PRINT_DEBUG_10(...) WSP_GGML_LOG_DEBUG(__VA_ARGS__)
91
107
  #else
92
- #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
93
- #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
108
+ #define WSP_GGML_PRINT_DEBUG_10(...)
94
109
  #endif
95
110
 
96
- #elif defined(__POWER9_VECTOR__)
111
+ // tensor params
97
112
 
98
- #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
99
- #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) wsp_ggml_compute_fp32_to_fp16(x)
100
- /* the inline asm below is about 12% faster than the lookup method */
101
- #define WSP_GGML_FP16_TO_FP32(x) WSP_GGML_COMPUTE_FP16_TO_FP32(x)
102
- #define WSP_GGML_FP32_TO_FP16(x) WSP_GGML_COMPUTE_FP32_TO_FP16(x)
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
+ }
103
118
 
104
- static inline float wsp_ggml_compute_fp16_to_fp32(wsp_ggml_fp16_t h) {
105
- register float f;
106
- register double d;
107
- __asm__(
108
- "mtfprd %0,%2\n"
109
- "xscvhpdp %0,%0\n"
110
- "frsp %1,%0\n" :
111
- /* temp */ "=d"(d),
112
- /* out */ "=f"(f):
113
- /* in */ "r"(h));
114
- return f;
115
- }
116
-
117
- static inline wsp_ggml_fp16_t wsp_ggml_compute_fp32_to_fp16(float f) {
118
- register double d;
119
- register wsp_ggml_fp16_t r;
120
- __asm__( /* xscvdphp can work on double or single precision */
121
- "xscvdphp %0,%2\n"
122
- "mffprd %1,%0\n" :
123
- /* temp */ "=d"(d),
124
- /* out */ "=r"(r):
125
- /* in */ "f"(f));
126
- return r;
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];
127
122
  }
128
123
 
129
- #else
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
+ }
130
128
 
131
- // FP16 <-> FP32
132
- // ref: https://github.com/Maratyszcza/FP16
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
133
 
134
- static inline float fp32_from_bits(uint32_t w) {
135
- union {
136
- uint32_t as_bits;
137
- float as_value;
138
- } fp32;
139
- fp32.as_bits = w;
140
- return fp32.as_value;
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;
141
137
  }
142
138
 
143
- static inline uint32_t fp32_to_bits(float f) {
144
- union {
145
- float as_value;
146
- uint32_t as_bits;
147
- } fp32;
148
- fp32.as_value = f;
149
- return fp32.as_bits;
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;
150
173
  }
151
174
 
152
- static inline float wsp_ggml_compute_fp16_to_fp32(wsp_ggml_fp16_t h) {
153
- const uint32_t w = (uint32_t) h << 16;
154
- const uint32_t sign = w & UINT32_C(0x80000000);
155
- const uint32_t two_w = w + w;
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
+ }
156
178
 
157
- const uint32_t exp_offset = UINT32_C(0xE0) << 23;
158
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
159
- const float exp_scale = 0x1.0p-112f;
160
- #else
161
- const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
162
- #endif
163
- const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
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);
164
203
 
165
- const uint32_t magic_mask = UINT32_C(126) << 23;
166
- const float magic_bias = 0.5f;
167
- const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
204
+ // remove all elements from the hash set
205
+ void wsp_ggml_hash_set_reset(struct wsp_ggml_hash_set * hash_set);
168
206
 
169
- const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
170
- const uint32_t result = sign |
171
- (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
172
- return fp32_from_bits(result);
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;
173
223
  }
174
224
 
175
- static inline wsp_ggml_fp16_t wsp_ggml_compute_fp32_to_fp16(float f) {
176
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
177
- const float scale_to_inf = 0x1.0p+112f;
178
- const float scale_to_zero = 0x1.0p-110f;
179
- #else
180
- const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
181
- const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
182
- #endif
183
- float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
184
-
185
- const uint32_t w = fp32_to_bits(f);
186
- const uint32_t shl1_w = w + w;
187
- const uint32_t sign = w & UINT32_C(0x80000000);
188
- uint32_t bias = shl1_w & UINT32_C(0xFF000000);
189
- if (bias < UINT32_C(0x71000000)) {
190
- bias = UINT32_C(0x71000000);
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
+ }
191
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
+ }
192
244
 
193
- base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
194
- const uint32_t bits = fp32_to_bits(base);
195
- const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
196
- const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
197
- const uint32_t nonsign = exp_bits + mantissa_bits;
198
- return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
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");
199
264
  }
200
265
 
201
- #define WSP_GGML_COMPUTE_FP16_TO_FP32(x) wsp_ggml_compute_fp16_to_fp32(x)
202
- #define WSP_GGML_COMPUTE_FP32_TO_FP16(x) wsp_ggml_compute_fp32_to_fp16(x)
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
203
316
 
204
- #endif // __F16C__
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);
205
319
 
206
- #endif // __ARM_NEON
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__)
207
496
 
208
497
  // precomputed f32 table for f16 (256 KB)
209
498
  // defined in ggml.c, initialized in wsp_ggml_init()
210
- extern float wsp_ggml_table_f32_f16[1 << 16];
499
+ WSP_GGML_API float wsp_ggml_table_f32_f16[1 << 16];
211
500
 
212
501
  // On ARM NEON, it's quicker to directly convert x -> x instead of calling into wsp_ggml_lookup_fp16_to_fp32,
213
502
  // so we define WSP_GGML_FP16_TO_FP32 and WSP_GGML_FP32_TO_FP16 elsewhere for NEON.
214
503
  // This is also true for POWER9.
215
- #if !defined(WSP_GGML_FP16_TO_FP32) || !defined(WSP_GGML_FP32_TO_FP16)
216
-
504
+ #if !defined(WSP_GGML_FP16_TO_FP32)
217
505
  inline static float wsp_ggml_lookup_fp16_to_fp32(wsp_ggml_fp16_t f) {
218
506
  uint16_t s;
219
507
  memcpy(&s, &f, sizeof(uint16_t));
@@ -221,26 +509,95 @@ inline static float wsp_ggml_lookup_fp16_to_fp32(wsp_ggml_fp16_t f) {
221
509
  }
222
510
 
223
511
  #define WSP_GGML_FP16_TO_FP32(x) wsp_ggml_lookup_fp16_to_fp32(x)
224
- #define WSP_GGML_FP32_TO_FP16(x) WSP_GGML_COMPUTE_FP32_TO_FP16(x)
225
-
226
512
  #endif
227
513
 
228
- #define WSP_GGML_HASHTABLE_FULL ((size_t)-1)
229
- #define WSP_GGML_HASHTABLE_ALREADY_EXISTS ((size_t)-2)
230
-
231
- struct wsp_ggml_hash_set wsp_ggml_hash_set_new(size_t size);
232
-
233
- bool wsp_ggml_hash_contains (const struct wsp_ggml_hash_set hash_set, struct wsp_ggml_tensor * key);
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
234
517
 
235
- // returns WSP_GGML_HASHTABLE_FULL if table is full, otherwise the current index of the key or where it should be inserted
236
- size_t wsp_ggml_hash_find (const struct wsp_ggml_hash_set hash_set, struct wsp_ggml_tensor * key);
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
+ }
237
565
 
238
- // returns WSP_GGML_HASHTABLE_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
239
- size_t wsp_ggml_hash_insert ( struct wsp_ggml_hash_set hash_set, struct wsp_ggml_tensor * key);
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
+ }
240
588
 
241
- // return index, asserts if table is full
242
- size_t wsp_ggml_hash_find_or_insert( struct wsp_ggml_hash_set hash_set, struct wsp_ggml_tensor * key);
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)
243
591
 
244
592
  #ifdef __cplusplus
245
593
  }
246
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