whisper.rn 0.4.0-rc.9 → 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.
- package/README.md +5 -1
- package/android/build.gradle +12 -3
- package/android/src/main/CMakeLists.txt +43 -13
- package/android/src/main/java/com/rnwhisper/WhisperContext.java +33 -35
- package/android/src/main/jni.cpp +9 -0
- package/android/src/main/jniLibs/arm64-v8a/librnwhisper.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnwhisper_v8fp16_va_2.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/librnwhisper.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/librnwhisper_vfpv4.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnwhisper.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnwhisper_x86_64.so +0 -0
- package/cpp/coreml/whisper-compat.h +10 -0
- package/cpp/coreml/whisper-compat.m +35 -0
- package/cpp/coreml/whisper-decoder-impl.h +27 -15
- package/cpp/coreml/whisper-decoder-impl.m +36 -10
- package/cpp/coreml/whisper-encoder-impl.h +21 -9
- package/cpp/coreml/whisper-encoder-impl.m +29 -3
- package/cpp/ggml-alloc.c +39 -37
- package/cpp/ggml-alloc.h +1 -1
- package/cpp/ggml-backend-impl.h +55 -27
- package/cpp/ggml-backend-reg.cpp +591 -0
- package/cpp/ggml-backend.cpp +336 -955
- package/cpp/ggml-backend.h +70 -42
- package/cpp/ggml-common.h +57 -49
- package/cpp/ggml-cpp.h +39 -0
- package/cpp/ggml-cpu/amx/amx.cpp +221 -0
- package/cpp/ggml-cpu/amx/amx.h +8 -0
- package/cpp/ggml-cpu/amx/common.h +91 -0
- package/cpp/ggml-cpu/amx/mmq.cpp +2511 -0
- package/cpp/ggml-cpu/amx/mmq.h +10 -0
- package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- package/cpp/ggml-cpu/arch/arm/quants.c +4113 -0
- package/cpp/ggml-cpu/arch/arm/repack.cpp +2162 -0
- package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
- package/cpp/ggml-cpu/arch/x86/quants.c +4310 -0
- package/cpp/ggml-cpu/arch/x86/repack.cpp +3284 -0
- package/cpp/ggml-cpu/arch-fallback.h +184 -0
- package/cpp/ggml-cpu/binary-ops.cpp +158 -0
- package/cpp/ggml-cpu/binary-ops.h +16 -0
- package/cpp/ggml-cpu/common.h +72 -0
- package/cpp/ggml-cpu/ggml-cpu-impl.h +511 -0
- package/cpp/ggml-cpu/ggml-cpu.c +3473 -0
- package/cpp/ggml-cpu/ggml-cpu.cpp +671 -0
- package/cpp/ggml-cpu/ops.cpp +9085 -0
- package/cpp/ggml-cpu/ops.h +111 -0
- package/cpp/ggml-cpu/quants.c +1157 -0
- package/cpp/ggml-cpu/quants.h +89 -0
- package/cpp/ggml-cpu/repack.cpp +1570 -0
- package/cpp/ggml-cpu/repack.h +98 -0
- package/cpp/ggml-cpu/simd-mappings.h +1006 -0
- package/cpp/ggml-cpu/traits.cpp +36 -0
- package/cpp/ggml-cpu/traits.h +38 -0
- package/cpp/ggml-cpu/unary-ops.cpp +186 -0
- package/cpp/ggml-cpu/unary-ops.h +28 -0
- package/cpp/ggml-cpu/vec.cpp +321 -0
- package/cpp/ggml-cpu/vec.h +973 -0
- package/cpp/ggml-cpu.h +143 -0
- package/cpp/ggml-impl.h +417 -23
- package/cpp/ggml-metal-impl.h +622 -0
- package/cpp/ggml-metal.h +9 -9
- package/cpp/ggml-metal.m +3451 -1344
- package/cpp/ggml-opt.cpp +1037 -0
- package/cpp/ggml-opt.h +237 -0
- package/cpp/ggml-quants.c +296 -10818
- package/cpp/ggml-quants.h +78 -125
- package/cpp/ggml-threading.cpp +12 -0
- package/cpp/ggml-threading.h +14 -0
- package/cpp/ggml-whisper-sim.metallib +0 -0
- package/cpp/ggml-whisper.metallib +0 -0
- package/cpp/ggml.c +4633 -21450
- package/cpp/ggml.h +320 -661
- package/cpp/gguf.cpp +1347 -0
- package/cpp/gguf.h +202 -0
- package/cpp/rn-whisper.cpp +4 -11
- package/cpp/whisper-arch.h +197 -0
- package/cpp/whisper.cpp +2022 -495
- package/cpp/whisper.h +75 -18
- package/ios/CMakeLists.txt +95 -0
- package/ios/RNWhisper.h +5 -0
- package/ios/RNWhisperAudioUtils.m +4 -0
- package/ios/RNWhisperContext.h +5 -0
- package/ios/RNWhisperContext.mm +4 -2
- package/ios/rnwhisper.xcframework/Info.plist +74 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/rnwhisper +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/rnwhisper +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
- package/jest/mock.js +5 -0
- package/lib/commonjs/version.json +1 -1
- package/lib/module/version.json +1 -1
- package/package.json +10 -6
- package/src/version.json +1 -1
- package/whisper-rn.podspec +11 -18
- package/cpp/README.md +0 -4
- package/cpp/ggml-aarch64.c +0 -3209
- package/cpp/ggml-aarch64.h +0 -39
- package/cpp/ggml-cpu-impl.h +0 -614
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
#include "ggml-backend.h"
|
|
2
|
+
#include "ggml-backend-impl.h"
|
|
3
|
+
#include "ggml-cpu.h"
|
|
4
|
+
#include "repack.h"
|
|
5
|
+
#include "traits.h"
|
|
6
|
+
#include "ggml-impl.h"
|
|
7
|
+
#include "amx/amx.h"
|
|
8
|
+
|
|
9
|
+
#include <cctype>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <vector>
|
|
12
|
+
|
|
13
|
+
#ifdef WSP_GGML_USE_CPU_HBM
|
|
14
|
+
# include "hbm.h"
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
#ifdef WSP_GGML_USE_CPU_KLEIDIAI
|
|
18
|
+
# include "kleidiai/kleidiai.h"
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
#if defined(_WIN32)
|
|
22
|
+
# define WIN32_LEAN_AND_MEAN
|
|
23
|
+
# ifndef NOMINMAX
|
|
24
|
+
# define NOMINMAX
|
|
25
|
+
# endif
|
|
26
|
+
# include <windows.h>
|
|
27
|
+
#else
|
|
28
|
+
# include <unistd.h>
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
#if defined(__APPLE__)
|
|
32
|
+
# include <sys/sysctl.h>
|
|
33
|
+
# include <sys/types.h>
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
// ggml-backend interface
|
|
37
|
+
|
|
38
|
+
std::vector<wsp_ggml_backend_buffer_type_t>& wsp_ggml_backend_cpu_get_extra_buffers_type() {
|
|
39
|
+
static std::vector<wsp_ggml_backend_buffer_type_t> bufts = []() {
|
|
40
|
+
std::vector<wsp_ggml_backend_buffer_type_t> bufts;
|
|
41
|
+
|
|
42
|
+
#if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
|
|
43
|
+
if (wsp_ggml_backend_amx_buffer_type()) {
|
|
44
|
+
bufts.push_back(wsp_ggml_backend_amx_buffer_type());
|
|
45
|
+
}
|
|
46
|
+
#endif
|
|
47
|
+
|
|
48
|
+
#ifdef WSP_GGML_USE_CPU_KLEIDIAI
|
|
49
|
+
if (wsp_ggml_backend_cpu_kleidiai_buffer_type()) {
|
|
50
|
+
bufts.push_back(wsp_ggml_backend_cpu_kleidiai_buffer_type());
|
|
51
|
+
}
|
|
52
|
+
#endif
|
|
53
|
+
|
|
54
|
+
#ifdef WSP_GGML_USE_CPU_REPACK
|
|
55
|
+
if (wsp_ggml_backend_cpu_repack_buffer_type()) {
|
|
56
|
+
bufts.push_back(wsp_ggml_backend_cpu_repack_buffer_type());
|
|
57
|
+
}
|
|
58
|
+
#endif
|
|
59
|
+
|
|
60
|
+
bufts.push_back(NULL);
|
|
61
|
+
|
|
62
|
+
return bufts;
|
|
63
|
+
}();
|
|
64
|
+
|
|
65
|
+
return bufts;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static wsp_ggml_backend_buffer_type_t * wsp_ggml_backend_cpu_device_get_extra_buffers_type(wsp_ggml_backend_dev_t device) {
|
|
69
|
+
return wsp_ggml_backend_cpu_get_extra_buffers_type().data();
|
|
70
|
+
|
|
71
|
+
WSP_GGML_UNUSED(device);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static bool wsp_ggml_backend_cpu_is_extra_buffer_type(wsp_ggml_backend_buffer_type_t buft) {
|
|
75
|
+
for (auto * extra : wsp_ggml_backend_cpu_get_extra_buffers_type()) {
|
|
76
|
+
if (extra && extra == buft) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// CPU backend - backend (stream)
|
|
84
|
+
|
|
85
|
+
struct wsp_ggml_backend_cpu_context {
|
|
86
|
+
int n_threads;
|
|
87
|
+
wsp_ggml_threadpool_t threadpool;
|
|
88
|
+
|
|
89
|
+
uint8_t * work_data;
|
|
90
|
+
size_t work_size;
|
|
91
|
+
|
|
92
|
+
wsp_ggml_abort_callback abort_callback;
|
|
93
|
+
void * abort_callback_data;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
static const char * wsp_ggml_backend_cpu_get_name(wsp_ggml_backend_t backend) {
|
|
97
|
+
return "CPU";
|
|
98
|
+
|
|
99
|
+
WSP_GGML_UNUSED(backend);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static void wsp_ggml_backend_cpu_free(wsp_ggml_backend_t backend) {
|
|
103
|
+
struct wsp_ggml_backend_cpu_context * cpu_ctx = (struct wsp_ggml_backend_cpu_context *)backend->context;
|
|
104
|
+
delete[] cpu_ctx->work_data;
|
|
105
|
+
delete cpu_ctx;
|
|
106
|
+
delete backend;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
struct wsp_ggml_backend_plan_cpu {
|
|
110
|
+
struct wsp_ggml_cplan cplan;
|
|
111
|
+
struct wsp_ggml_cgraph cgraph;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
static wsp_ggml_backend_graph_plan_t wsp_ggml_backend_cpu_graph_plan_create(wsp_ggml_backend_t backend, const struct wsp_ggml_cgraph * cgraph) {
|
|
115
|
+
struct wsp_ggml_backend_cpu_context * cpu_ctx = (struct wsp_ggml_backend_cpu_context *)backend->context;
|
|
116
|
+
|
|
117
|
+
struct wsp_ggml_backend_plan_cpu * cpu_plan = new wsp_ggml_backend_plan_cpu;
|
|
118
|
+
|
|
119
|
+
cpu_plan->cplan = wsp_ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
|
|
120
|
+
cpu_plan->cgraph = *cgraph; // FIXME: deep copy
|
|
121
|
+
|
|
122
|
+
if (cpu_plan->cplan.work_size > 0) {
|
|
123
|
+
cpu_plan->cplan.work_data = new uint8_t[cpu_plan->cplan.work_size];
|
|
124
|
+
if (cpu_plan->cplan.work_data == NULL) {
|
|
125
|
+
delete cpu_plan;
|
|
126
|
+
return NULL;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
|
|
131
|
+
cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data;
|
|
132
|
+
|
|
133
|
+
return cpu_plan;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static void wsp_ggml_backend_cpu_graph_plan_free(wsp_ggml_backend_t backend, wsp_ggml_backend_graph_plan_t plan) {
|
|
137
|
+
struct wsp_ggml_backend_plan_cpu * cpu_plan = (struct wsp_ggml_backend_plan_cpu *)plan;
|
|
138
|
+
|
|
139
|
+
delete[] cpu_plan->cplan.work_data;
|
|
140
|
+
delete cpu_plan;
|
|
141
|
+
|
|
142
|
+
WSP_GGML_UNUSED(backend);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static enum wsp_ggml_status wsp_ggml_backend_cpu_graph_plan_compute(wsp_ggml_backend_t backend, wsp_ggml_backend_graph_plan_t plan) {
|
|
146
|
+
struct wsp_ggml_backend_plan_cpu * cpu_plan = (struct wsp_ggml_backend_plan_cpu *)plan;
|
|
147
|
+
|
|
148
|
+
return wsp_ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
|
|
149
|
+
|
|
150
|
+
WSP_GGML_UNUSED(backend);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static enum wsp_ggml_status wsp_ggml_backend_cpu_graph_compute(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph) {
|
|
154
|
+
struct wsp_ggml_backend_cpu_context * cpu_ctx = (struct wsp_ggml_backend_cpu_context *)backend->context;
|
|
155
|
+
|
|
156
|
+
struct wsp_ggml_cplan cplan = wsp_ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
|
|
157
|
+
|
|
158
|
+
if (cpu_ctx->work_size < cplan.work_size) {
|
|
159
|
+
delete[] cpu_ctx->work_data;
|
|
160
|
+
cpu_ctx->work_data = new uint8_t[cplan.work_size];
|
|
161
|
+
if (cpu_ctx->work_data == NULL) {
|
|
162
|
+
cpu_ctx->work_size = 0;
|
|
163
|
+
return WSP_GGML_STATUS_ALLOC_FAILED;
|
|
164
|
+
}
|
|
165
|
+
cpu_ctx->work_size = cplan.work_size;
|
|
166
|
+
}
|
|
167
|
+
cplan.work_data = (uint8_t *)cpu_ctx->work_data;
|
|
168
|
+
|
|
169
|
+
cplan.abort_callback = cpu_ctx->abort_callback;
|
|
170
|
+
cplan.abort_callback_data = cpu_ctx->abort_callback_data;
|
|
171
|
+
|
|
172
|
+
return wsp_ggml_graph_compute(cgraph, &cplan);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static const struct wsp_ggml_backend_i wsp_ggml_backend_cpu_i = {
|
|
176
|
+
/* .get_name = */ wsp_ggml_backend_cpu_get_name,
|
|
177
|
+
/* .free = */ wsp_ggml_backend_cpu_free,
|
|
178
|
+
/* .set_tensor_async = */ NULL,
|
|
179
|
+
/* .get_tensor_async = */ NULL,
|
|
180
|
+
/* .cpy_tensor_async = */ NULL,
|
|
181
|
+
/* .synchronize = */ NULL,
|
|
182
|
+
/* .graph_plan_create = */ wsp_ggml_backend_cpu_graph_plan_create,
|
|
183
|
+
/* .graph_plan_free = */ wsp_ggml_backend_cpu_graph_plan_free,
|
|
184
|
+
/* .graph_plan_update = */ NULL,
|
|
185
|
+
/* .graph_plan_compute = */ wsp_ggml_backend_cpu_graph_plan_compute,
|
|
186
|
+
/* .graph_compute = */ wsp_ggml_backend_cpu_graph_compute,
|
|
187
|
+
/* .event_record = */ NULL,
|
|
188
|
+
/* .event_wait = */ NULL,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
static wsp_ggml_guid_t wsp_ggml_backend_cpu_guid(void) {
|
|
192
|
+
static wsp_ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
|
|
193
|
+
return &guid;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
wsp_ggml_backend_t wsp_ggml_backend_cpu_init(void) {
|
|
197
|
+
// initialize CPU backend now to avoid slowing the first graph computation
|
|
198
|
+
wsp_ggml_cpu_init();
|
|
199
|
+
|
|
200
|
+
struct wsp_ggml_backend_cpu_context * ctx = new wsp_ggml_backend_cpu_context;
|
|
201
|
+
if (ctx == NULL) {
|
|
202
|
+
return NULL;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
ctx->n_threads = WSP_GGML_DEFAULT_N_THREADS;
|
|
206
|
+
ctx->threadpool = NULL;
|
|
207
|
+
ctx->work_data = NULL;
|
|
208
|
+
ctx->work_size = 0;
|
|
209
|
+
ctx->abort_callback = NULL;
|
|
210
|
+
ctx->abort_callback_data = NULL;
|
|
211
|
+
|
|
212
|
+
wsp_ggml_backend_t cpu_backend = new wsp_ggml_backend {
|
|
213
|
+
/* .guid = */ wsp_ggml_backend_cpu_guid(),
|
|
214
|
+
/* .interface = */ wsp_ggml_backend_cpu_i,
|
|
215
|
+
/* .device = */ wsp_ggml_backend_reg_dev_get(wsp_ggml_backend_cpu_reg(), 0),
|
|
216
|
+
/* .context = */ ctx,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
if (cpu_backend == NULL) {
|
|
220
|
+
delete ctx;
|
|
221
|
+
return NULL;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return cpu_backend;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
bool wsp_ggml_backend_is_cpu(wsp_ggml_backend_t backend) {
|
|
228
|
+
return backend != NULL && wsp_ggml_guid_matches(backend->guid, wsp_ggml_backend_cpu_guid());
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
void wsp_ggml_backend_cpu_set_n_threads(wsp_ggml_backend_t backend_cpu, int n_threads) {
|
|
232
|
+
WSP_GGML_ASSERT(wsp_ggml_backend_is_cpu(backend_cpu));
|
|
233
|
+
|
|
234
|
+
struct wsp_ggml_backend_cpu_context * ctx = (struct wsp_ggml_backend_cpu_context *)backend_cpu->context;
|
|
235
|
+
ctx->n_threads = n_threads;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
void wsp_ggml_backend_cpu_set_threadpool(wsp_ggml_backend_t backend_cpu, wsp_ggml_threadpool_t threadpool) {
|
|
239
|
+
WSP_GGML_ASSERT(wsp_ggml_backend_is_cpu(backend_cpu));
|
|
240
|
+
|
|
241
|
+
struct wsp_ggml_backend_cpu_context * ctx = (struct wsp_ggml_backend_cpu_context *)backend_cpu->context;
|
|
242
|
+
|
|
243
|
+
if (ctx->threadpool && ctx->threadpool != threadpool) {
|
|
244
|
+
// already had a different threadpool, pause/suspend it before switching
|
|
245
|
+
wsp_ggml_threadpool_pause(ctx->threadpool);
|
|
246
|
+
}
|
|
247
|
+
ctx->threadpool = threadpool;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
void wsp_ggml_backend_cpu_set_abort_callback(wsp_ggml_backend_t backend_cpu, wsp_ggml_abort_callback abort_callback, void * abort_callback_data) {
|
|
251
|
+
WSP_GGML_ASSERT(wsp_ggml_backend_is_cpu(backend_cpu));
|
|
252
|
+
|
|
253
|
+
struct wsp_ggml_backend_cpu_context * ctx = (struct wsp_ggml_backend_cpu_context *)backend_cpu->context;
|
|
254
|
+
ctx->abort_callback = abort_callback;
|
|
255
|
+
ctx->abort_callback_data = abort_callback_data;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// CPU backend - device
|
|
259
|
+
|
|
260
|
+
struct wsp_ggml_backend_cpu_device_context {
|
|
261
|
+
std::string description = "CPU";
|
|
262
|
+
|
|
263
|
+
wsp_ggml_backend_cpu_device_context() {
|
|
264
|
+
#ifdef __APPLE__
|
|
265
|
+
size_t len = 0;
|
|
266
|
+
if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
|
|
267
|
+
description.resize(len);
|
|
268
|
+
sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT
|
|
269
|
+
}
|
|
270
|
+
#elif defined(__linux__)
|
|
271
|
+
FILE * f = fopen("/proc/cpuinfo", "r");
|
|
272
|
+
if (f) {
|
|
273
|
+
char buf[1024];
|
|
274
|
+
while (fgets(buf, sizeof(buf), f)) {
|
|
275
|
+
if (strncmp(buf, "model name", 10) == 0) {
|
|
276
|
+
char * p = strchr(buf, ':');
|
|
277
|
+
if (p) {
|
|
278
|
+
p++;
|
|
279
|
+
while (std::isspace(*p)) {
|
|
280
|
+
p++;
|
|
281
|
+
}
|
|
282
|
+
while (std::isspace(p[strlen(p) - 1])) {
|
|
283
|
+
p[strlen(p) - 1] = '\0';
|
|
284
|
+
}
|
|
285
|
+
description = p;
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
fclose(f);
|
|
291
|
+
}
|
|
292
|
+
#elif defined(_WIN32)
|
|
293
|
+
HKEY hKey;
|
|
294
|
+
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
|
|
295
|
+
TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
|
|
296
|
+
0,
|
|
297
|
+
KEY_READ,
|
|
298
|
+
&hKey) == ERROR_SUCCESS) {
|
|
299
|
+
DWORD cpu_brand_size = 0;
|
|
300
|
+
if (RegQueryValueExA(hKey,
|
|
301
|
+
"ProcessorNameString",
|
|
302
|
+
NULL,
|
|
303
|
+
NULL,
|
|
304
|
+
NULL,
|
|
305
|
+
&cpu_brand_size) == ERROR_SUCCESS) {
|
|
306
|
+
description.resize(cpu_brand_size);
|
|
307
|
+
if (RegQueryValueExA(hKey,
|
|
308
|
+
"ProcessorNameString",
|
|
309
|
+
NULL,
|
|
310
|
+
NULL,
|
|
311
|
+
(LPBYTE)&description[0], // NOLINT
|
|
312
|
+
&cpu_brand_size) == ERROR_SUCCESS) {
|
|
313
|
+
if (description.find('\0') != std::string::npos) {
|
|
314
|
+
description.resize(description.find('\0'));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
RegCloseKey(hKey);
|
|
319
|
+
}
|
|
320
|
+
#endif
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
static const char * wsp_ggml_backend_cpu_device_get_name(wsp_ggml_backend_dev_t dev) {
|
|
325
|
+
return "CPU";
|
|
326
|
+
|
|
327
|
+
WSP_GGML_UNUSED(dev);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
static const char * wsp_ggml_backend_cpu_device_get_description(wsp_ggml_backend_dev_t dev) {
|
|
331
|
+
struct wsp_ggml_backend_cpu_device_context * ctx = (struct wsp_ggml_backend_cpu_device_context *)dev->context;
|
|
332
|
+
|
|
333
|
+
return ctx->description.c_str();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static void wsp_ggml_backend_cpu_device_get_memory(wsp_ggml_backend_dev_t dev, size_t * free, size_t * total) {
|
|
337
|
+
#ifdef _WIN32
|
|
338
|
+
MEMORYSTATUSEX status;
|
|
339
|
+
status.dwLength = sizeof(status);
|
|
340
|
+
GlobalMemoryStatusEx(&status);
|
|
341
|
+
*total = status.ullTotalPhys;
|
|
342
|
+
*free = status.ullAvailPhys;
|
|
343
|
+
#else
|
|
344
|
+
long pages = sysconf(_SC_PHYS_PAGES);
|
|
345
|
+
long page_size = sysconf(_SC_PAGE_SIZE);
|
|
346
|
+
*total = pages * page_size;
|
|
347
|
+
*free = *total;
|
|
348
|
+
#endif
|
|
349
|
+
|
|
350
|
+
WSP_GGML_UNUSED(dev);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
static enum wsp_ggml_backend_dev_type wsp_ggml_backend_cpu_device_get_type(wsp_ggml_backend_dev_t dev) {
|
|
354
|
+
return WSP_GGML_BACKEND_DEVICE_TYPE_CPU;
|
|
355
|
+
|
|
356
|
+
WSP_GGML_UNUSED(dev);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
static void wsp_ggml_backend_cpu_device_get_props(wsp_ggml_backend_dev_t dev, struct wsp_ggml_backend_dev_props * props) {
|
|
360
|
+
props->name = wsp_ggml_backend_cpu_device_get_name(dev);
|
|
361
|
+
props->description = wsp_ggml_backend_cpu_device_get_description(dev);
|
|
362
|
+
props->type = wsp_ggml_backend_cpu_device_get_type(dev);
|
|
363
|
+
wsp_ggml_backend_cpu_device_get_memory(dev, &props->memory_free, &props->memory_total);
|
|
364
|
+
props->caps = {
|
|
365
|
+
/* .async = */ false,
|
|
366
|
+
/* .host_buffer = */ false,
|
|
367
|
+
/* .buffer_from_host_ptr = */ true,
|
|
368
|
+
/* .events = */ false,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
static wsp_ggml_backend_t wsp_ggml_backend_cpu_device_init_backend(wsp_ggml_backend_dev_t dev, const char * params) {
|
|
373
|
+
return wsp_ggml_backend_cpu_init();
|
|
374
|
+
|
|
375
|
+
WSP_GGML_UNUSED(dev);
|
|
376
|
+
WSP_GGML_UNUSED(params);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
static wsp_ggml_backend_buffer_type_t wsp_ggml_backend_cpu_device_get_buffer_type(wsp_ggml_backend_dev_t dev) {
|
|
380
|
+
return wsp_ggml_backend_cpu_buffer_type();
|
|
381
|
+
|
|
382
|
+
WSP_GGML_UNUSED(dev);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
static wsp_ggml_backend_buffer_t wsp_ggml_backend_cpu_device_buffer_from_host_ptr(wsp_ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
|
|
386
|
+
return wsp_ggml_backend_cpu_buffer_from_ptr(ptr, size);
|
|
387
|
+
|
|
388
|
+
WSP_GGML_UNUSED(dev);
|
|
389
|
+
WSP_GGML_UNUSED(max_tensor_size);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
static bool wsp_ggml_backend_cpu_device_supports_op(wsp_ggml_backend_dev_t dev, const struct wsp_ggml_tensor * op) {
|
|
393
|
+
const struct wsp_ggml_tensor * src0 = op->src[0];
|
|
394
|
+
const struct wsp_ggml_tensor * src1 = op->src[1];
|
|
395
|
+
|
|
396
|
+
if (op->op == WSP_GGML_OP_NONE || op->op == WSP_GGML_OP_RESHAPE || op->op == WSP_GGML_OP_VIEW || op->op == WSP_GGML_OP_PERMUTE || op->op == WSP_GGML_OP_TRANSPOSE) {
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// extra_buffer_op?
|
|
401
|
+
for (auto extra : wsp_ggml_backend_cpu_get_extra_buffers_type()) {
|
|
402
|
+
if (extra) {
|
|
403
|
+
auto buf_extra = (ggml::cpu::extra_buffer_type*) extra->context;
|
|
404
|
+
if (buf_extra && buf_extra->supports_op(dev, op)) {
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// the other case need host buffer.
|
|
411
|
+
for (int i = 0; i < WSP_GGML_MAX_SRC; i++) {
|
|
412
|
+
if (op->src[i] && op->src[i]->buffer && !wsp_ggml_backend_buft_is_host(op->src[i]->buffer->buft)) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
switch (op->op) {
|
|
418
|
+
case WSP_GGML_OP_CPY:
|
|
419
|
+
return
|
|
420
|
+
op->type != WSP_GGML_TYPE_IQ3_XXS &&
|
|
421
|
+
op->type != WSP_GGML_TYPE_IQ3_S &&
|
|
422
|
+
op->type != WSP_GGML_TYPE_IQ2_XXS &&
|
|
423
|
+
op->type != WSP_GGML_TYPE_IQ2_XS &&
|
|
424
|
+
op->type != WSP_GGML_TYPE_IQ2_S &&
|
|
425
|
+
op->type != WSP_GGML_TYPE_IQ1_S &&
|
|
426
|
+
op->type != WSP_GGML_TYPE_IQ1_M; // missing type_traits.from_float
|
|
427
|
+
case WSP_GGML_OP_MUL_MAT:
|
|
428
|
+
return src1->type == WSP_GGML_TYPE_F32 || src1->type == wsp_ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
|
|
429
|
+
case WSP_GGML_OP_SOFT_MAX_BACK: {
|
|
430
|
+
if (op->src[0]->type != WSP_GGML_TYPE_F32 || op->src[1]->type != WSP_GGML_TYPE_F32) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
float max_bias = 0.0f;
|
|
434
|
+
|
|
435
|
+
memcpy(&max_bias, (const float *) op->op_params + 1, sizeof(float));
|
|
436
|
+
|
|
437
|
+
return max_bias == 0.0f;
|
|
438
|
+
}
|
|
439
|
+
case WSP_GGML_OP_IM2COL_BACK:
|
|
440
|
+
return src0->type == WSP_GGML_TYPE_F32 && src1->type == WSP_GGML_TYPE_F32;
|
|
441
|
+
case WSP_GGML_OP_GET_ROWS_BACK:
|
|
442
|
+
return src0->type == WSP_GGML_TYPE_F32 || src0->type == WSP_GGML_TYPE_F16;
|
|
443
|
+
case WSP_GGML_OP_OUT_PROD:
|
|
444
|
+
return (src0->type == WSP_GGML_TYPE_F32 || (wsp_ggml_is_quantized(src0->type) && src0->ne[2] == src1->ne[2] && src0->ne[3] == src1->ne[3])) &&
|
|
445
|
+
src1->type == WSP_GGML_TYPE_F32 && op->type == WSP_GGML_TYPE_F32;
|
|
446
|
+
default:
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
static bool wsp_ggml_backend_cpu_device_supports_buft(wsp_ggml_backend_dev_t dev, wsp_ggml_backend_buffer_type_t buft) {
|
|
452
|
+
return wsp_ggml_backend_buft_is_host(buft) || wsp_ggml_backend_cpu_is_extra_buffer_type(buft);
|
|
453
|
+
WSP_GGML_UNUSED(dev);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
static const struct wsp_ggml_backend_device_i wsp_ggml_backend_cpu_device_i = {
|
|
457
|
+
/* .get_name = */ wsp_ggml_backend_cpu_device_get_name,
|
|
458
|
+
/* .get_description = */ wsp_ggml_backend_cpu_device_get_description,
|
|
459
|
+
/* .get_memory = */ wsp_ggml_backend_cpu_device_get_memory,
|
|
460
|
+
/* .get_type = */ wsp_ggml_backend_cpu_device_get_type,
|
|
461
|
+
/* .get_props = */ wsp_ggml_backend_cpu_device_get_props,
|
|
462
|
+
/* .init_backend = */ wsp_ggml_backend_cpu_device_init_backend,
|
|
463
|
+
/* .get_buffer_type = */ wsp_ggml_backend_cpu_device_get_buffer_type,
|
|
464
|
+
/* .get_host_buffer_type = */ NULL,
|
|
465
|
+
/* .buffer_from_host_ptr = */ wsp_ggml_backend_cpu_device_buffer_from_host_ptr,
|
|
466
|
+
/* .supports_op = */ wsp_ggml_backend_cpu_device_supports_op,
|
|
467
|
+
/* .supports_buft = */ wsp_ggml_backend_cpu_device_supports_buft,
|
|
468
|
+
/* .offload_op = */ NULL,
|
|
469
|
+
/* .event_new = */ NULL,
|
|
470
|
+
/* .event_free = */ NULL,
|
|
471
|
+
/* .event_synchronize = */ NULL,
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// CPU backend - backend (reg)
|
|
475
|
+
|
|
476
|
+
static const char * wsp_ggml_backend_cpu_reg_get_name(wsp_ggml_backend_reg_t reg) {
|
|
477
|
+
return "CPU";
|
|
478
|
+
|
|
479
|
+
WSP_GGML_UNUSED(reg);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
static size_t wsp_ggml_backend_cpu_reg_get_device_count(wsp_ggml_backend_reg_t reg) {
|
|
483
|
+
return 1;
|
|
484
|
+
|
|
485
|
+
WSP_GGML_UNUSED(reg);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
static wsp_ggml_backend_dev_t wsp_ggml_backend_cpu_reg_get_device(wsp_ggml_backend_reg_t reg, size_t index) {
|
|
489
|
+
WSP_GGML_ASSERT(index == 0);
|
|
490
|
+
|
|
491
|
+
static wsp_ggml_backend_cpu_device_context ctx;
|
|
492
|
+
static wsp_ggml_backend_device wsp_ggml_backend_cpu_device = {
|
|
493
|
+
/* .iface = */ wsp_ggml_backend_cpu_device_i,
|
|
494
|
+
/* .reg = */ reg,
|
|
495
|
+
/* .context = */ &ctx,
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
return &wsp_ggml_backend_cpu_device;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// This is intended to replace the the wsp_ggml_cpu_has_* functions when loading the CPU backend dynamically,
|
|
502
|
+
// and additionally to allow other backends to expose their own list of features that applications can query using the same API
|
|
503
|
+
static wsp_ggml_backend_feature * wsp_ggml_backend_cpu_get_features(wsp_ggml_backend_reg_t reg) {
|
|
504
|
+
static std::vector<wsp_ggml_backend_feature> features = []() {
|
|
505
|
+
wsp_ggml_cpu_init();
|
|
506
|
+
|
|
507
|
+
std::vector<wsp_ggml_backend_feature> features;
|
|
508
|
+
if (wsp_ggml_cpu_has_sse3()) {
|
|
509
|
+
features.push_back({ "SSE3", "1" });
|
|
510
|
+
}
|
|
511
|
+
if (wsp_ggml_cpu_has_ssse3()) {
|
|
512
|
+
features.push_back({ "SSSE3", "1" });
|
|
513
|
+
}
|
|
514
|
+
if (wsp_ggml_cpu_has_avx()) {
|
|
515
|
+
features.push_back({ "AVX", "1" });
|
|
516
|
+
}
|
|
517
|
+
if (wsp_ggml_cpu_has_avx_vnni()) {
|
|
518
|
+
features.push_back({ "AVX_VNNI", "1" });
|
|
519
|
+
}
|
|
520
|
+
if (wsp_ggml_cpu_has_avx2()) {
|
|
521
|
+
features.push_back({ "AVX2", "1" });
|
|
522
|
+
}
|
|
523
|
+
if (wsp_ggml_cpu_has_f16c()) {
|
|
524
|
+
features.push_back({ "F16C", "1" });
|
|
525
|
+
}
|
|
526
|
+
if (wsp_ggml_cpu_has_fma()) {
|
|
527
|
+
features.push_back({ "FMA", "1" });
|
|
528
|
+
}
|
|
529
|
+
if (wsp_ggml_cpu_has_bmi2()) {
|
|
530
|
+
features.push_back({ "BMI2", "1" });
|
|
531
|
+
}
|
|
532
|
+
if (wsp_ggml_cpu_has_avx512()) {
|
|
533
|
+
features.push_back({ "AVX512", "1" });
|
|
534
|
+
}
|
|
535
|
+
if (wsp_ggml_cpu_has_avx512_vbmi()) {
|
|
536
|
+
features.push_back({ "AVX512_VBMI", "1" });
|
|
537
|
+
}
|
|
538
|
+
if (wsp_ggml_cpu_has_avx512_vnni()) {
|
|
539
|
+
features.push_back({ "AVX512_VNNI", "1" });
|
|
540
|
+
}
|
|
541
|
+
if (wsp_ggml_cpu_has_avx512_bf16()) {
|
|
542
|
+
features.push_back({ "AVX512_BF16", "1" });
|
|
543
|
+
}
|
|
544
|
+
if (wsp_ggml_cpu_has_amx_int8()) {
|
|
545
|
+
features.push_back({ "AMX_INT8", "1" });
|
|
546
|
+
}
|
|
547
|
+
if (wsp_ggml_cpu_has_neon()) {
|
|
548
|
+
features.push_back({ "NEON", "1" });
|
|
549
|
+
}
|
|
550
|
+
if (wsp_ggml_cpu_has_arm_fma()) {
|
|
551
|
+
features.push_back({ "ARM_FMA", "1" });
|
|
552
|
+
}
|
|
553
|
+
if (wsp_ggml_cpu_has_fp16_va()) {
|
|
554
|
+
features.push_back({ "FP16_VA", "1" });
|
|
555
|
+
}
|
|
556
|
+
if (wsp_ggml_cpu_has_matmul_int8()) {
|
|
557
|
+
features.push_back({ "MATMUL_INT8", "1" });
|
|
558
|
+
}
|
|
559
|
+
if (wsp_ggml_cpu_has_sve()) {
|
|
560
|
+
features.push_back({ "SVE", "1" });
|
|
561
|
+
}
|
|
562
|
+
if (wsp_ggml_cpu_has_dotprod()) {
|
|
563
|
+
features.push_back({ "DOTPROD", "1" });
|
|
564
|
+
}
|
|
565
|
+
if (wsp_ggml_cpu_get_sve_cnt() > 0) {
|
|
566
|
+
static std::string sve_cnt = std::to_string(wsp_ggml_cpu_get_sve_cnt());
|
|
567
|
+
features.push_back({ "SVE_CNT", sve_cnt.c_str() });
|
|
568
|
+
}
|
|
569
|
+
if (wsp_ggml_cpu_has_sme()) {
|
|
570
|
+
features.push_back({ "SME", "1" });
|
|
571
|
+
}
|
|
572
|
+
if (wsp_ggml_cpu_has_riscv_v()) {
|
|
573
|
+
features.push_back({ "RISCV_V", "1" });
|
|
574
|
+
}
|
|
575
|
+
if (wsp_ggml_cpu_has_vsx()) {
|
|
576
|
+
features.push_back({ "VSX", "1" });
|
|
577
|
+
}
|
|
578
|
+
if (wsp_ggml_cpu_has_vxe()) {
|
|
579
|
+
features.push_back({ "VXE", "1" });
|
|
580
|
+
}
|
|
581
|
+
if (wsp_ggml_cpu_has_wasm_simd()) {
|
|
582
|
+
features.push_back({ "WASM_SIMD", "1" });
|
|
583
|
+
}
|
|
584
|
+
if (wsp_ggml_cpu_has_llamafile()) {
|
|
585
|
+
features.push_back({ "LLAMAFILE", "1" });
|
|
586
|
+
}
|
|
587
|
+
#ifdef WSP_GGML_USE_ACCELERATE
|
|
588
|
+
features.push_back({ "ACCELERATE", "1" });
|
|
589
|
+
#endif
|
|
590
|
+
#ifdef WSP_GGML_USE_CPU_HBM
|
|
591
|
+
features.push_back({ "CPU_HBM", "1" });
|
|
592
|
+
#endif
|
|
593
|
+
#ifdef WSP_GGML_USE_OPENMP
|
|
594
|
+
features.push_back({ "OPENMP", "1" });
|
|
595
|
+
#endif
|
|
596
|
+
#ifdef WSP_GGML_USE_CPU_KLEIDIAI
|
|
597
|
+
features.push_back({ "KLEIDIAI", "1" });
|
|
598
|
+
#endif
|
|
599
|
+
#ifdef WSP_GGML_USE_CPU_REPACK
|
|
600
|
+
features.push_back({ "REPACK", "1" });
|
|
601
|
+
#endif
|
|
602
|
+
|
|
603
|
+
features.push_back({ nullptr, nullptr });
|
|
604
|
+
|
|
605
|
+
return features;
|
|
606
|
+
}();
|
|
607
|
+
|
|
608
|
+
return features.data();
|
|
609
|
+
|
|
610
|
+
WSP_GGML_UNUSED(reg);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
static void * wsp_ggml_backend_cpu_get_proc_address(wsp_ggml_backend_reg_t reg, const char * name) {
|
|
614
|
+
if (strcmp(name, "wsp_ggml_backend_set_n_threads") == 0) {
|
|
615
|
+
wsp_ggml_backend_set_n_threads_t fct = wsp_ggml_backend_cpu_set_n_threads;
|
|
616
|
+
return (void *)fct;
|
|
617
|
+
}
|
|
618
|
+
if (strcmp(name, "wsp_ggml_backend_dev_get_extra_bufts") == 0) {
|
|
619
|
+
wsp_ggml_backend_dev_get_extra_bufts_t fct = wsp_ggml_backend_cpu_device_get_extra_buffers_type;
|
|
620
|
+
return (void *)fct;
|
|
621
|
+
}
|
|
622
|
+
if (strcmp(name, "wsp_ggml_backend_get_features") == 0) {
|
|
623
|
+
return (void *)wsp_ggml_backend_cpu_get_features;
|
|
624
|
+
}
|
|
625
|
+
if (strcmp(name, "wsp_ggml_backend_set_abort_callback") == 0) {
|
|
626
|
+
return (void *)wsp_ggml_backend_cpu_set_abort_callback;
|
|
627
|
+
}
|
|
628
|
+
if (strcmp(name, "wsp_ggml_backend_cpu_numa_init") == 0) {
|
|
629
|
+
return (void *)wsp_ggml_numa_init;
|
|
630
|
+
}
|
|
631
|
+
if (strcmp(name, "wsp_ggml_backend_cpu_is_numa") == 0) {
|
|
632
|
+
return (void *)wsp_ggml_is_numa;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// threadpool - TODO: move to ggml-base
|
|
636
|
+
if (strcmp(name, "wsp_ggml_threadpool_new") == 0) {
|
|
637
|
+
return (void *)wsp_ggml_threadpool_new;
|
|
638
|
+
}
|
|
639
|
+
if (strcmp(name, "wsp_ggml_threadpool_free") == 0) {
|
|
640
|
+
return (void *)wsp_ggml_threadpool_free;
|
|
641
|
+
}
|
|
642
|
+
if (strcmp(name, "wsp_ggml_backend_cpu_set_threadpool") == 0) {
|
|
643
|
+
return (void *)wsp_ggml_backend_cpu_set_threadpool;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
return NULL;
|
|
647
|
+
|
|
648
|
+
WSP_GGML_UNUSED(reg);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
static const struct wsp_ggml_backend_reg_i wsp_ggml_backend_cpu_reg_i = {
|
|
652
|
+
/* .get_name = */ wsp_ggml_backend_cpu_reg_get_name,
|
|
653
|
+
/* .get_device_count = */ wsp_ggml_backend_cpu_reg_get_device_count,
|
|
654
|
+
/* .get_device = */ wsp_ggml_backend_cpu_reg_get_device,
|
|
655
|
+
/* .get_proc_address = */ wsp_ggml_backend_cpu_get_proc_address,
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
wsp_ggml_backend_reg_t wsp_ggml_backend_cpu_reg(void) {
|
|
659
|
+
// init CPU feature detection
|
|
660
|
+
wsp_ggml_cpu_init();
|
|
661
|
+
|
|
662
|
+
static struct wsp_ggml_backend_reg wsp_ggml_backend_cpu_reg = {
|
|
663
|
+
/* .api_version = */ WSP_GGML_BACKEND_API_VERSION,
|
|
664
|
+
/* .iface = */ wsp_ggml_backend_cpu_reg_i,
|
|
665
|
+
/* .context = */ NULL,
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
return &wsp_ggml_backend_cpu_reg;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
WSP_GGML_BACKEND_DL_IMPL(wsp_ggml_backend_cpu_reg)
|