whisper.rn 0.4.0-rc.1 → 0.4.0-rc.11
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 +6 -6
- package/android/build.gradle +4 -0
- package/android/src/main/CMakeLists.txt +21 -1
- package/android/src/main/java/com/rnwhisper/AudioUtils.java +27 -92
- package/android/src/main/java/com/rnwhisper/RNWhisper.java +86 -40
- package/android/src/main/java/com/rnwhisper/WhisperContext.java +85 -131
- package/android/src/main/jni-utils.h +76 -0
- package/android/src/main/jni.cpp +226 -109
- package/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
- package/android/src/oldarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
- package/cpp/coreml/whisper-encoder-impl.h +1 -1
- package/cpp/coreml/whisper-encoder.h +4 -0
- package/cpp/coreml/whisper-encoder.mm +5 -3
- package/cpp/ggml-alloc.c +797 -400
- package/cpp/ggml-alloc.h +60 -10
- package/cpp/ggml-backend-impl.h +255 -0
- package/cpp/ggml-backend-reg.cpp +582 -0
- package/cpp/ggml-backend.cpp +2002 -0
- package/cpp/ggml-backend.h +354 -0
- package/cpp/ggml-common.h +1851 -0
- package/cpp/ggml-cpp.h +39 -0
- package/cpp/ggml-cpu-aarch64.cpp +4247 -0
- package/cpp/ggml-cpu-aarch64.h +8 -0
- package/cpp/ggml-cpu-impl.h +531 -0
- package/cpp/ggml-cpu-quants.c +12245 -0
- package/cpp/ggml-cpu-quants.h +63 -0
- package/cpp/ggml-cpu-traits.cpp +36 -0
- package/cpp/ggml-cpu-traits.h +38 -0
- package/cpp/ggml-cpu.c +14792 -0
- package/cpp/ggml-cpu.cpp +653 -0
- package/cpp/ggml-cpu.h +137 -0
- package/cpp/ggml-impl.h +567 -0
- package/cpp/ggml-metal-impl.h +288 -0
- package/cpp/ggml-metal.h +24 -43
- package/cpp/ggml-metal.m +4867 -1080
- package/cpp/ggml-opt.cpp +854 -0
- package/cpp/ggml-opt.h +216 -0
- package/cpp/ggml-quants.c +5238 -0
- package/cpp/ggml-quants.h +100 -0
- package/cpp/ggml-threading.cpp +12 -0
- package/cpp/ggml-threading.h +14 -0
- package/cpp/ggml-whisper.metallib +0 -0
- package/cpp/ggml.c +5106 -19431
- package/cpp/ggml.h +847 -669
- package/cpp/gguf.cpp +1329 -0
- package/cpp/gguf.h +202 -0
- package/cpp/rn-audioutils.cpp +68 -0
- package/cpp/rn-audioutils.h +14 -0
- package/cpp/rn-whisper-log.h +11 -0
- package/cpp/rn-whisper.cpp +221 -52
- package/cpp/rn-whisper.h +50 -15
- package/cpp/whisper.cpp +3174 -1533
- package/cpp/whisper.h +176 -44
- package/ios/RNWhisper.mm +139 -46
- package/ios/RNWhisperAudioUtils.h +1 -2
- package/ios/RNWhisperAudioUtils.m +18 -67
- package/ios/RNWhisperContext.h +11 -8
- package/ios/RNWhisperContext.mm +195 -150
- package/jest/mock.js +15 -2
- package/lib/commonjs/NativeRNWhisper.js.map +1 -1
- package/lib/commonjs/index.js +76 -28
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/version.json +1 -1
- package/lib/module/NativeRNWhisper.js.map +1 -1
- package/lib/module/index.js +76 -28
- package/lib/module/index.js.map +1 -1
- package/lib/module/version.json +1 -1
- package/lib/typescript/NativeRNWhisper.d.ts +13 -4
- package/lib/typescript/NativeRNWhisper.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +37 -5
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +9 -7
- package/src/NativeRNWhisper.ts +20 -4
- package/src/index.ts +98 -42
- package/src/version.json +1 -1
- package/whisper-rn.podspec +13 -20
- package/cpp/README.md +0 -4
- package/cpp/ggml-metal.metal +0 -2353
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "ggml.h"
|
|
4
|
+
#include "ggml-alloc.h"
|
|
5
|
+
|
|
6
|
+
#ifdef WSP_GGML_BACKEND_SHARED
|
|
7
|
+
# if defined(_WIN32) && !defined(__MINGW32__)
|
|
8
|
+
# ifdef WSP_GGML_BACKEND_BUILD
|
|
9
|
+
# define WSP_GGML_BACKEND_API __declspec(dllexport) extern
|
|
10
|
+
# else
|
|
11
|
+
# define WSP_GGML_BACKEND_API __declspec(dllimport) extern
|
|
12
|
+
# endif
|
|
13
|
+
# else
|
|
14
|
+
# define WSP_GGML_BACKEND_API __attribute__ ((visibility ("default"))) extern
|
|
15
|
+
# endif
|
|
16
|
+
#else
|
|
17
|
+
# define WSP_GGML_BACKEND_API extern
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
#ifdef __cplusplus
|
|
21
|
+
extern "C" {
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
typedef struct wsp_ggml_backend_buffer_type * wsp_ggml_backend_buffer_type_t;
|
|
25
|
+
typedef struct wsp_ggml_backend_buffer * wsp_ggml_backend_buffer_t;
|
|
26
|
+
typedef struct wsp_ggml_backend_event * wsp_ggml_backend_event_t;
|
|
27
|
+
typedef struct wsp_ggml_backend * wsp_ggml_backend_t;
|
|
28
|
+
typedef void * wsp_ggml_backend_graph_plan_t;
|
|
29
|
+
typedef struct wsp_ggml_backend_reg * wsp_ggml_backend_reg_t;
|
|
30
|
+
typedef struct wsp_ggml_backend_device * wsp_ggml_backend_dev_t;
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
//
|
|
34
|
+
// Backend buffer type
|
|
35
|
+
//
|
|
36
|
+
|
|
37
|
+
WSP_GGML_API const char * wsp_ggml_backend_buft_name (wsp_ggml_backend_buffer_type_t buft);
|
|
38
|
+
WSP_GGML_API wsp_ggml_backend_buffer_t wsp_ggml_backend_buft_alloc_buffer (wsp_ggml_backend_buffer_type_t buft, size_t size);
|
|
39
|
+
WSP_GGML_API size_t wsp_ggml_backend_buft_get_alignment (wsp_ggml_backend_buffer_type_t buft);
|
|
40
|
+
WSP_GGML_API size_t wsp_ggml_backend_buft_get_max_size (wsp_ggml_backend_buffer_type_t buft);
|
|
41
|
+
WSP_GGML_API size_t wsp_ggml_backend_buft_get_alloc_size(wsp_ggml_backend_buffer_type_t buft, struct wsp_ggml_tensor * tensor);
|
|
42
|
+
WSP_GGML_API bool wsp_ggml_backend_buft_is_host (wsp_ggml_backend_buffer_type_t buft);
|
|
43
|
+
WSP_GGML_API wsp_ggml_backend_dev_t wsp_ggml_backend_buft_get_device (wsp_ggml_backend_buffer_type_t buft);
|
|
44
|
+
|
|
45
|
+
//
|
|
46
|
+
// Backend buffer
|
|
47
|
+
//
|
|
48
|
+
|
|
49
|
+
enum wsp_ggml_backend_buffer_usage {
|
|
50
|
+
WSP_GGML_BACKEND_BUFFER_USAGE_ANY = 0,
|
|
51
|
+
WSP_GGML_BACKEND_BUFFER_USAGE_WEIGHTS = 1,
|
|
52
|
+
WSP_GGML_BACKEND_BUFFER_USAGE_COMPUTE = 2,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
WSP_GGML_API const char * wsp_ggml_backend_buffer_name (wsp_ggml_backend_buffer_t buffer);
|
|
56
|
+
WSP_GGML_API void wsp_ggml_backend_buffer_free (wsp_ggml_backend_buffer_t buffer);
|
|
57
|
+
WSP_GGML_API void * wsp_ggml_backend_buffer_get_base (wsp_ggml_backend_buffer_t buffer);
|
|
58
|
+
WSP_GGML_API size_t wsp_ggml_backend_buffer_get_size (wsp_ggml_backend_buffer_t buffer);
|
|
59
|
+
WSP_GGML_API void wsp_ggml_backend_buffer_init_tensor (wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor);
|
|
60
|
+
WSP_GGML_API size_t wsp_ggml_backend_buffer_get_alignment (wsp_ggml_backend_buffer_t buffer);
|
|
61
|
+
WSP_GGML_API size_t wsp_ggml_backend_buffer_get_max_size (wsp_ggml_backend_buffer_t buffer);
|
|
62
|
+
WSP_GGML_API size_t wsp_ggml_backend_buffer_get_alloc_size(wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor);
|
|
63
|
+
WSP_GGML_API void wsp_ggml_backend_buffer_clear (wsp_ggml_backend_buffer_t buffer, uint8_t value);
|
|
64
|
+
WSP_GGML_API bool wsp_ggml_backend_buffer_is_host (wsp_ggml_backend_buffer_t buffer);
|
|
65
|
+
WSP_GGML_API void wsp_ggml_backend_buffer_set_usage (wsp_ggml_backend_buffer_t buffer, enum wsp_ggml_backend_buffer_usage usage);
|
|
66
|
+
WSP_GGML_API enum wsp_ggml_backend_buffer_usage wsp_ggml_backend_buffer_get_usage (wsp_ggml_backend_buffer_t buffer);
|
|
67
|
+
WSP_GGML_API wsp_ggml_backend_buffer_type_t wsp_ggml_backend_buffer_get_type (wsp_ggml_backend_buffer_t buffer);
|
|
68
|
+
WSP_GGML_API void wsp_ggml_backend_buffer_reset (wsp_ggml_backend_buffer_t buffer);
|
|
69
|
+
|
|
70
|
+
// tensor copy between different backends
|
|
71
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_copy(struct wsp_ggml_tensor * src, struct wsp_ggml_tensor * dst);
|
|
72
|
+
|
|
73
|
+
//
|
|
74
|
+
// Backend (stream)
|
|
75
|
+
//
|
|
76
|
+
|
|
77
|
+
WSP_GGML_API wsp_ggml_guid_t wsp_ggml_backend_guid(wsp_ggml_backend_t backend);
|
|
78
|
+
WSP_GGML_API const char * wsp_ggml_backend_name(wsp_ggml_backend_t backend);
|
|
79
|
+
WSP_GGML_API void wsp_ggml_backend_free(wsp_ggml_backend_t backend);
|
|
80
|
+
|
|
81
|
+
WSP_GGML_API wsp_ggml_backend_buffer_type_t wsp_ggml_backend_get_default_buffer_type(wsp_ggml_backend_t backend);
|
|
82
|
+
WSP_GGML_API wsp_ggml_backend_buffer_t wsp_ggml_backend_alloc_buffer(wsp_ggml_backend_t backend, size_t size);
|
|
83
|
+
WSP_GGML_API size_t wsp_ggml_backend_get_alignment(wsp_ggml_backend_t backend);
|
|
84
|
+
WSP_GGML_API size_t wsp_ggml_backend_get_max_size(wsp_ggml_backend_t backend);
|
|
85
|
+
|
|
86
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_set_async(wsp_ggml_backend_t backend, struct wsp_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
|
|
87
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_get_async(wsp_ggml_backend_t backend, const struct wsp_ggml_tensor * tensor, void * data, size_t offset, size_t size);
|
|
88
|
+
|
|
89
|
+
// "offset" refers to the offset in tensor->data for setting/getting data
|
|
90
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_set( struct wsp_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
|
|
91
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_get(const struct wsp_ggml_tensor * tensor, void * data, size_t offset, size_t size);
|
|
92
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_memset( struct wsp_ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
|
|
93
|
+
|
|
94
|
+
WSP_GGML_API void wsp_ggml_backend_synchronize(wsp_ggml_backend_t backend);
|
|
95
|
+
|
|
96
|
+
WSP_GGML_API wsp_ggml_backend_graph_plan_t wsp_ggml_backend_graph_plan_create(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph);
|
|
97
|
+
WSP_GGML_API void wsp_ggml_backend_graph_plan_free (wsp_ggml_backend_t backend, wsp_ggml_backend_graph_plan_t plan);
|
|
98
|
+
|
|
99
|
+
WSP_GGML_API enum wsp_ggml_status wsp_ggml_backend_graph_plan_compute (wsp_ggml_backend_t backend, wsp_ggml_backend_graph_plan_t plan);
|
|
100
|
+
WSP_GGML_API enum wsp_ggml_status wsp_ggml_backend_graph_compute (wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph);
|
|
101
|
+
WSP_GGML_API enum wsp_ggml_status wsp_ggml_backend_graph_compute_async(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph);
|
|
102
|
+
|
|
103
|
+
// NOTE: will be removed, use device version instead
|
|
104
|
+
WSP_GGML_API bool wsp_ggml_backend_supports_op(wsp_ggml_backend_t backend, const struct wsp_ggml_tensor * op);
|
|
105
|
+
WSP_GGML_API bool wsp_ggml_backend_supports_buft(wsp_ggml_backend_t backend, wsp_ggml_backend_buffer_type_t buft);
|
|
106
|
+
WSP_GGML_API bool wsp_ggml_backend_offload_op(wsp_ggml_backend_t backend, const struct wsp_ggml_tensor * op);
|
|
107
|
+
|
|
108
|
+
// asynchronous copy
|
|
109
|
+
// the copy is performed after all the currently queued operations in backend_src
|
|
110
|
+
// backend_dst will wait for the copy to complete before performing other operations
|
|
111
|
+
// automatic fallback to sync copy if async is not supported
|
|
112
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_copy_async(wsp_ggml_backend_t backend_src, wsp_ggml_backend_t backend_dst, struct wsp_ggml_tensor * src, struct wsp_ggml_tensor * dst);
|
|
113
|
+
|
|
114
|
+
WSP_GGML_API wsp_ggml_backend_dev_t wsp_ggml_backend_get_device(wsp_ggml_backend_t backend);
|
|
115
|
+
|
|
116
|
+
//
|
|
117
|
+
// Events
|
|
118
|
+
//
|
|
119
|
+
|
|
120
|
+
WSP_GGML_API wsp_ggml_backend_event_t wsp_ggml_backend_event_new(wsp_ggml_backend_dev_t device);
|
|
121
|
+
WSP_GGML_API void wsp_ggml_backend_event_free(wsp_ggml_backend_event_t event);
|
|
122
|
+
WSP_GGML_API void wsp_ggml_backend_event_record(wsp_ggml_backend_event_t event, wsp_ggml_backend_t backend);
|
|
123
|
+
WSP_GGML_API void wsp_ggml_backend_event_synchronize(wsp_ggml_backend_event_t event);
|
|
124
|
+
WSP_GGML_API void wsp_ggml_backend_event_wait(wsp_ggml_backend_t backend, wsp_ggml_backend_event_t event);
|
|
125
|
+
|
|
126
|
+
//
|
|
127
|
+
// Backend device
|
|
128
|
+
//
|
|
129
|
+
|
|
130
|
+
enum wsp_ggml_backend_dev_type {
|
|
131
|
+
// CPU device using system memory
|
|
132
|
+
WSP_GGML_BACKEND_DEVICE_TYPE_CPU,
|
|
133
|
+
// GPU device using dedicated memory
|
|
134
|
+
WSP_GGML_BACKEND_DEVICE_TYPE_GPU,
|
|
135
|
+
// accelerator devices intended to be used together with the CPU backend (e.g. BLAS or AMX)
|
|
136
|
+
WSP_GGML_BACKEND_DEVICE_TYPE_ACCEL
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// functionality supported by the device
|
|
140
|
+
struct wsp_ggml_backend_dev_caps {
|
|
141
|
+
// asynchronous operations
|
|
142
|
+
bool async;
|
|
143
|
+
// pinned host buffer
|
|
144
|
+
bool host_buffer;
|
|
145
|
+
// creating buffers from host ptr
|
|
146
|
+
bool buffer_from_host_ptr;
|
|
147
|
+
// event synchronization
|
|
148
|
+
bool events;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// all the device properties
|
|
152
|
+
struct wsp_ggml_backend_dev_props {
|
|
153
|
+
const char * name;
|
|
154
|
+
const char * description;
|
|
155
|
+
size_t memory_free;
|
|
156
|
+
size_t memory_total;
|
|
157
|
+
enum wsp_ggml_backend_dev_type type;
|
|
158
|
+
struct wsp_ggml_backend_dev_caps caps;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
WSP_GGML_API const char * wsp_ggml_backend_dev_name(wsp_ggml_backend_dev_t device);
|
|
162
|
+
WSP_GGML_API const char * wsp_ggml_backend_dev_description(wsp_ggml_backend_dev_t device);
|
|
163
|
+
WSP_GGML_API void wsp_ggml_backend_dev_memory(wsp_ggml_backend_dev_t device, size_t * free, size_t * total);
|
|
164
|
+
WSP_GGML_API enum wsp_ggml_backend_dev_type wsp_ggml_backend_dev_type(wsp_ggml_backend_dev_t device);
|
|
165
|
+
WSP_GGML_API void wsp_ggml_backend_dev_get_props(wsp_ggml_backend_dev_t device, struct wsp_ggml_backend_dev_props * props);
|
|
166
|
+
WSP_GGML_API wsp_ggml_backend_reg_t wsp_ggml_backend_dev_backend_reg(wsp_ggml_backend_dev_t device);
|
|
167
|
+
WSP_GGML_API wsp_ggml_backend_t wsp_ggml_backend_dev_init(wsp_ggml_backend_dev_t device, const char * params);
|
|
168
|
+
WSP_GGML_API wsp_ggml_backend_buffer_type_t wsp_ggml_backend_dev_buffer_type(wsp_ggml_backend_dev_t device);
|
|
169
|
+
WSP_GGML_API wsp_ggml_backend_buffer_type_t wsp_ggml_backend_dev_host_buffer_type(wsp_ggml_backend_dev_t device);
|
|
170
|
+
WSP_GGML_API wsp_ggml_backend_buffer_t wsp_ggml_backend_dev_buffer_from_host_ptr(wsp_ggml_backend_dev_t device, void * ptr, size_t size, size_t max_tensor_size);
|
|
171
|
+
|
|
172
|
+
WSP_GGML_API bool wsp_ggml_backend_dev_supports_op(wsp_ggml_backend_dev_t device, const struct wsp_ggml_tensor * op);
|
|
173
|
+
WSP_GGML_API bool wsp_ggml_backend_dev_supports_buft(wsp_ggml_backend_dev_t device, wsp_ggml_backend_buffer_type_t buft);
|
|
174
|
+
WSP_GGML_API bool wsp_ggml_backend_dev_offload_op(wsp_ggml_backend_dev_t device, const struct wsp_ggml_tensor * op);
|
|
175
|
+
|
|
176
|
+
//
|
|
177
|
+
// Backend (reg)
|
|
178
|
+
//
|
|
179
|
+
|
|
180
|
+
WSP_GGML_API const char * wsp_ggml_backend_reg_name(wsp_ggml_backend_reg_t reg);
|
|
181
|
+
WSP_GGML_API size_t wsp_ggml_backend_reg_dev_count(wsp_ggml_backend_reg_t reg);
|
|
182
|
+
WSP_GGML_API wsp_ggml_backend_dev_t wsp_ggml_backend_reg_dev_get(wsp_ggml_backend_reg_t reg, size_t index);
|
|
183
|
+
WSP_GGML_API void * wsp_ggml_backend_reg_get_proc_address(wsp_ggml_backend_reg_t reg, const char * name);
|
|
184
|
+
|
|
185
|
+
// Common functions that may be obtained using wsp_ggml_backend_reg_get_proc_address
|
|
186
|
+
|
|
187
|
+
// Split buffer type for tensor parallelism
|
|
188
|
+
typedef wsp_ggml_backend_buffer_type_t (*wsp_ggml_backend_split_buffer_type_t)(int main_device, const float * tensor_split);
|
|
189
|
+
// Set the number of threads for the backend
|
|
190
|
+
typedef void (*wsp_ggml_backend_set_n_threads_t)(wsp_ggml_backend_t backend, int n_threads);
|
|
191
|
+
// Get additional buffer types provided by the device (returns a NULL-terminated array)
|
|
192
|
+
typedef wsp_ggml_backend_buffer_type_t * (*wsp_ggml_backend_dev_get_extra_bufts_t)(wsp_ggml_backend_dev_t device);
|
|
193
|
+
// Set the abort callback for the backend
|
|
194
|
+
typedef void (*wsp_ggml_backend_set_abort_callback_t)(wsp_ggml_backend_t backend, wsp_ggml_abort_callback abort_callback, void * abort_callback_data);
|
|
195
|
+
// Get a list of feature flags supported by the backend (returns a NULL-terminated array)
|
|
196
|
+
struct wsp_ggml_backend_feature {
|
|
197
|
+
const char * name;
|
|
198
|
+
const char * value;
|
|
199
|
+
};
|
|
200
|
+
typedef struct wsp_ggml_backend_feature * (*wsp_ggml_backend_get_features_t)(wsp_ggml_backend_reg_t reg);
|
|
201
|
+
|
|
202
|
+
//
|
|
203
|
+
// Backend registry
|
|
204
|
+
//
|
|
205
|
+
|
|
206
|
+
WSP_GGML_API void wsp_ggml_backend_device_register(wsp_ggml_backend_dev_t device);
|
|
207
|
+
|
|
208
|
+
// Backend (reg) enumeration
|
|
209
|
+
WSP_GGML_API size_t wsp_ggml_backend_reg_count(void);
|
|
210
|
+
WSP_GGML_API wsp_ggml_backend_reg_t wsp_ggml_backend_reg_get(size_t index);
|
|
211
|
+
WSP_GGML_API wsp_ggml_backend_reg_t wsp_ggml_backend_reg_by_name(const char * name);
|
|
212
|
+
|
|
213
|
+
// Device enumeration
|
|
214
|
+
WSP_GGML_API size_t wsp_ggml_backend_dev_count(void);
|
|
215
|
+
WSP_GGML_API wsp_ggml_backend_dev_t wsp_ggml_backend_dev_get(size_t index);
|
|
216
|
+
WSP_GGML_API wsp_ggml_backend_dev_t wsp_ggml_backend_dev_by_name(const char * name);
|
|
217
|
+
WSP_GGML_API wsp_ggml_backend_dev_t wsp_ggml_backend_dev_by_type(enum wsp_ggml_backend_dev_type type);
|
|
218
|
+
|
|
219
|
+
// Direct backend (stream) initialization
|
|
220
|
+
// = wsp_ggml_backend_dev_init(wsp_ggml_backend_dev_by_name(name), params)
|
|
221
|
+
WSP_GGML_API wsp_ggml_backend_t wsp_ggml_backend_init_by_name(const char * name, const char * params);
|
|
222
|
+
// = wsp_ggml_backend_dev_init(wsp_ggml_backend_dev_by_type(type), params)
|
|
223
|
+
WSP_GGML_API wsp_ggml_backend_t wsp_ggml_backend_init_by_type(enum wsp_ggml_backend_dev_type type, const char * params);
|
|
224
|
+
// = wsp_ggml_backend_dev_init(wsp_ggml_backend_dev_by_type(GPU) OR wsp_ggml_backend_dev_by_type(CPU), NULL)
|
|
225
|
+
WSP_GGML_API wsp_ggml_backend_t wsp_ggml_backend_init_best(void);
|
|
226
|
+
|
|
227
|
+
// Load a backend from a dynamic library and register it
|
|
228
|
+
WSP_GGML_API wsp_ggml_backend_reg_t wsp_ggml_backend_load(const char * path);
|
|
229
|
+
// Unload a backend if loaded dynamically and unregister it
|
|
230
|
+
WSP_GGML_API void wsp_ggml_backend_unload(wsp_ggml_backend_reg_t reg);
|
|
231
|
+
// Load all known backends from dynamic libraries
|
|
232
|
+
WSP_GGML_API void wsp_ggml_backend_load_all(void);
|
|
233
|
+
WSP_GGML_API void wsp_ggml_backend_load_all_from_path(const char * dir_path);
|
|
234
|
+
|
|
235
|
+
//
|
|
236
|
+
// Backend scheduler
|
|
237
|
+
//
|
|
238
|
+
|
|
239
|
+
// The backend scheduler allows for multiple backend devices to be used together
|
|
240
|
+
// Handles compute buffer allocation, assignment of tensors to backends, and copying of tensors between backends
|
|
241
|
+
// The backends are selected based on:
|
|
242
|
+
// - the backend that supports the operation
|
|
243
|
+
// - the location of the pre-allocated tensors (e.g. the weights)
|
|
244
|
+
/*
|
|
245
|
+
Example usage:
|
|
246
|
+
|
|
247
|
+
// operations that use tensors allocated in a buffer with USAGE_WEIGHTS will be assigned
|
|
248
|
+
// preferrably to run on the same backend as the buffer
|
|
249
|
+
wsp_ggml_backend_buffer_set_usage(buf_weights, WSP_GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
|
|
250
|
+
|
|
251
|
+
sched = wsp_ggml_backend_sched_new({backend_gpu, backend_gpu2, backend_cpu}, NULL, num_backends, WSP_GGML_DEFAULT_GRAPH_SIZE, false);
|
|
252
|
+
|
|
253
|
+
// initialize buffers from a max size graph (optional)
|
|
254
|
+
reserve_graph = build_graph(sched, max_batch_size);
|
|
255
|
+
|
|
256
|
+
// manually assign nodes to a backend (optional, should not be needed in most cases)
|
|
257
|
+
struct wsp_ggml_tensor * node = wsp_ggml_mul_mat(ctx, ...);
|
|
258
|
+
wsp_ggml_backend_sched_set_tensor_backend(sched, node, backend_gpu);
|
|
259
|
+
|
|
260
|
+
wsp_ggml_backend_sched_reserve(sched, reserve_graph);
|
|
261
|
+
|
|
262
|
+
// compute
|
|
263
|
+
graph = build_graph(sched); // the graph and its tensors are single-use in terms of allocation, multi-use in terms of computation
|
|
264
|
+
for (int i = 0; i < 10; ++i) {
|
|
265
|
+
wsp_ggml_backend_sched_graph_compute(sched, graph); // on the first iteration the graph is allocated automatically
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// if there are graph inputs:
|
|
269
|
+
graph = build_graph(sched); // get a new graph that is not allocated (the metadata for the old graph is freed once wsp_ggml_free is called)
|
|
270
|
+
wsp_ggml_backend_sched_reset(sched); // clear the allocation of the previous graph
|
|
271
|
+
wsp_ggml_backend_sched_alloc_graph(sched, graph); // explicitly allocate the new graph but do not execute it
|
|
272
|
+
wsp_ggml_backend_tensor_set(input_tensor, ...); // copy data to the newly allocated graph tensors
|
|
273
|
+
wsp_ggml_backend_sched_graph_compute(sched, graph); // execute the graph
|
|
274
|
+
|
|
275
|
+
// as an alternative to the above it is also possible to assign the inputs to a dedicated context and
|
|
276
|
+
// allocate them statically via wsp_ggml_backend_alloc_ctx_tensors
|
|
277
|
+
}
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
typedef struct wsp_ggml_backend_sched * wsp_ggml_backend_sched_t;
|
|
281
|
+
|
|
282
|
+
// Evaluation callback for each node in the graph (set with wsp_ggml_backend_sched_set_eval_callback)
|
|
283
|
+
// when ask == true, the scheduler wants to know if the user wants to observe this node
|
|
284
|
+
// this allows the scheduler to batch nodes together in order to evaluate them in a single call
|
|
285
|
+
//
|
|
286
|
+
// when ask == false, the scheduler is passing the node tensor to the user for observation
|
|
287
|
+
// if the user returns false, the scheduler will cancel the graph compute
|
|
288
|
+
//
|
|
289
|
+
typedef bool (*wsp_ggml_backend_sched_eval_callback)(struct wsp_ggml_tensor * t, bool ask, void * user_data);
|
|
290
|
+
|
|
291
|
+
// Initialize a backend scheduler, backends with low index are given priority over backends with high index
|
|
292
|
+
WSP_GGML_API wsp_ggml_backend_sched_t wsp_ggml_backend_sched_new(wsp_ggml_backend_t * backends, wsp_ggml_backend_buffer_type_t * bufts, int n_backends, size_t graph_size, bool parallel);
|
|
293
|
+
WSP_GGML_API void wsp_ggml_backend_sched_free(wsp_ggml_backend_sched_t sched);
|
|
294
|
+
|
|
295
|
+
// Initialize backend buffers from a measure graph
|
|
296
|
+
WSP_GGML_API bool wsp_ggml_backend_sched_reserve(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * measure_graph); // returns success
|
|
297
|
+
|
|
298
|
+
WSP_GGML_API int wsp_ggml_backend_sched_get_n_backends(wsp_ggml_backend_sched_t sched);
|
|
299
|
+
WSP_GGML_API wsp_ggml_backend_t wsp_ggml_backend_sched_get_backend(wsp_ggml_backend_sched_t sched, int i);
|
|
300
|
+
|
|
301
|
+
// Get the number of splits of the last graph
|
|
302
|
+
WSP_GGML_API int wsp_ggml_backend_sched_get_n_splits(wsp_ggml_backend_sched_t sched);
|
|
303
|
+
WSP_GGML_API int wsp_ggml_backend_sched_get_n_copies(wsp_ggml_backend_sched_t sched);
|
|
304
|
+
|
|
305
|
+
WSP_GGML_API size_t wsp_ggml_backend_sched_get_buffer_size(wsp_ggml_backend_sched_t sched, wsp_ggml_backend_t backend);
|
|
306
|
+
|
|
307
|
+
WSP_GGML_API void wsp_ggml_backend_sched_set_tensor_backend(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * node, wsp_ggml_backend_t backend);
|
|
308
|
+
WSP_GGML_API wsp_ggml_backend_t wsp_ggml_backend_sched_get_tensor_backend(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * node);
|
|
309
|
+
|
|
310
|
+
// Allocate and compute graph on the backend scheduler
|
|
311
|
+
WSP_GGML_API bool wsp_ggml_backend_sched_alloc_graph(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph); // returns success
|
|
312
|
+
WSP_GGML_API enum wsp_ggml_status wsp_ggml_backend_sched_graph_compute(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph);
|
|
313
|
+
WSP_GGML_API enum wsp_ggml_status wsp_ggml_backend_sched_graph_compute_async(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph);
|
|
314
|
+
WSP_GGML_API void wsp_ggml_backend_sched_synchronize(wsp_ggml_backend_sched_t sched);
|
|
315
|
+
|
|
316
|
+
// Reset all assignments and allocators - must be called before changing the node backends or allocating a new graph.
|
|
317
|
+
// This in effect deallocates all tensors that were previously allocated and leaves them with dangling pointers.
|
|
318
|
+
// The correct way to use this API is to discard the deallocated tensors and create new ones.
|
|
319
|
+
WSP_GGML_API void wsp_ggml_backend_sched_reset(wsp_ggml_backend_sched_t sched);
|
|
320
|
+
|
|
321
|
+
// Set a callback to be called for each resulting node during graph compute
|
|
322
|
+
WSP_GGML_API void wsp_ggml_backend_sched_set_eval_callback(wsp_ggml_backend_sched_t sched, wsp_ggml_backend_sched_eval_callback callback, void * user_data);
|
|
323
|
+
|
|
324
|
+
//
|
|
325
|
+
// Utils
|
|
326
|
+
//
|
|
327
|
+
|
|
328
|
+
struct wsp_ggml_backend_graph_copy {
|
|
329
|
+
wsp_ggml_backend_buffer_t buffer;
|
|
330
|
+
struct wsp_ggml_context * ctx_allocated;
|
|
331
|
+
struct wsp_ggml_context * ctx_unallocated;
|
|
332
|
+
struct wsp_ggml_cgraph * graph;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// Copy a graph to a different backend
|
|
336
|
+
WSP_GGML_API struct wsp_ggml_backend_graph_copy wsp_ggml_backend_graph_copy(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * graph);
|
|
337
|
+
WSP_GGML_API void wsp_ggml_backend_graph_copy_free(struct wsp_ggml_backend_graph_copy copy);
|
|
338
|
+
|
|
339
|
+
typedef bool (*wsp_ggml_backend_eval_callback)(int node_index, struct wsp_ggml_tensor * t1, struct wsp_ggml_tensor * t2, void * user_data);
|
|
340
|
+
|
|
341
|
+
// Compare the output of two backends
|
|
342
|
+
WSP_GGML_API bool wsp_ggml_backend_compare_graph_backend(wsp_ggml_backend_t backend1, wsp_ggml_backend_t backend2, struct wsp_ggml_cgraph * graph, wsp_ggml_backend_eval_callback callback, void * user_data);
|
|
343
|
+
|
|
344
|
+
// Tensor initialization
|
|
345
|
+
WSP_GGML_API void wsp_ggml_backend_tensor_alloc(wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor, void * addr);
|
|
346
|
+
WSP_GGML_API void wsp_ggml_backend_view_init(struct wsp_ggml_tensor * tensor);
|
|
347
|
+
|
|
348
|
+
// CPU buffer types are always available
|
|
349
|
+
WSP_GGML_API wsp_ggml_backend_buffer_t wsp_ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size);
|
|
350
|
+
WSP_GGML_API wsp_ggml_backend_buffer_type_t wsp_ggml_backend_cpu_buffer_type(void);
|
|
351
|
+
|
|
352
|
+
#ifdef __cplusplus
|
|
353
|
+
}
|
|
354
|
+
#endif
|