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
package/cpp/ggml-opt.h
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// This file contains functionality for training models using GGML.
|
|
2
|
+
// It is not strictly needed vs. just vanilla GGML but it provides a more high-level interface for common needs such as datasets.
|
|
3
|
+
// At the bottom of this file especially there are relatively high-level functions that are suitable use or adaptation in user code.
|
|
4
|
+
//
|
|
5
|
+
// Module maintainer: Johannes Gäßler (@JohannesGaessler, johannesg@5d6.de)
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "ggml.h"
|
|
10
|
+
#include "ggml-backend.h"
|
|
11
|
+
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
|
|
14
|
+
#ifdef __cplusplus
|
|
15
|
+
extern "C" {
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
struct wsp_ggml_opt_dataset;
|
|
19
|
+
struct wsp_ggml_opt_context;
|
|
20
|
+
struct wsp_ggml_opt_result;
|
|
21
|
+
|
|
22
|
+
typedef struct wsp_ggml_opt_dataset * wsp_ggml_opt_dataset_t;
|
|
23
|
+
typedef struct wsp_ggml_opt_context * wsp_ggml_opt_context_t;
|
|
24
|
+
typedef struct wsp_ggml_opt_result * wsp_ggml_opt_result_t;
|
|
25
|
+
|
|
26
|
+
// ====== Loss ======
|
|
27
|
+
|
|
28
|
+
// built-in loss types, i.e. the built-in quantities minimized by the optimizer
|
|
29
|
+
// custom loss types can be defined via mean or sum which simply reduce the outputs for all datapoints to a single value
|
|
30
|
+
enum wsp_ggml_opt_loss_type {
|
|
31
|
+
WSP_GGML_OPT_LOSS_TYPE_MEAN,
|
|
32
|
+
WSP_GGML_OPT_LOSS_TYPE_SUM,
|
|
33
|
+
WSP_GGML_OPT_LOSS_TYPE_CROSS_ENTROPY,
|
|
34
|
+
WSP_GGML_OPT_LOSS_TYPE_MEAN_SQUARED_ERROR,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// ====== Dataset ======
|
|
38
|
+
|
|
39
|
+
WSP_GGML_API wsp_ggml_opt_dataset_t wsp_ggml_opt_dataset_init(
|
|
40
|
+
int64_t ne_datapoint, // number of elements per datapoint
|
|
41
|
+
int64_t ne_label, // number of elements per label
|
|
42
|
+
int64_t ndata, // total number of datapoints/labels
|
|
43
|
+
int64_t ndata_shard); // number of datapoints/labels per shard (unit at which the dataset is shuffled/copied)
|
|
44
|
+
WSP_GGML_API void wsp_ggml_opt_dataset_free(wsp_ggml_opt_dataset_t dataset);
|
|
45
|
+
|
|
46
|
+
// get underlying tensors that store the data
|
|
47
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_dataset_data (wsp_ggml_opt_dataset_t dataset); // shape = [ne_datapoint, ndata]
|
|
48
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_dataset_labels(wsp_ggml_opt_dataset_t dataset); // shape = [nd_label, ndata]
|
|
49
|
+
|
|
50
|
+
// shuffle idata first datapoints from dataset with RNG from opt_ctx, shuffle all datapoints if idata is negative
|
|
51
|
+
WSP_GGML_API void wsp_ggml_opt_dataset_shuffle(wsp_ggml_opt_context_t opt_ctx, wsp_ggml_opt_dataset_t dataset, int64_t idata);
|
|
52
|
+
|
|
53
|
+
// get batch at position ibatch from dataset and copy the data to data_batch and labels_batch
|
|
54
|
+
WSP_GGML_API void wsp_ggml_opt_dataset_get_batch(
|
|
55
|
+
wsp_ggml_opt_dataset_t dataset,
|
|
56
|
+
struct wsp_ggml_tensor * data_batch, // shape = [ne_datapoint, ndata_batch]
|
|
57
|
+
struct wsp_ggml_tensor * labels_batch, // shape = [ne_label, ndata_batch]
|
|
58
|
+
int64_t ibatch);
|
|
59
|
+
|
|
60
|
+
// ====== Model / Context ======
|
|
61
|
+
|
|
62
|
+
enum wsp_ggml_opt_build_type {
|
|
63
|
+
WSP_GGML_OPT_BUILD_TYPE_FORWARD,
|
|
64
|
+
WSP_GGML_OPT_BUILD_TYPE_GRAD,
|
|
65
|
+
WSP_GGML_OPT_BUILD_TYPE_OPT,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// parameters that control which optimizer is used and how said optimizer tries to find the minimal loss
|
|
69
|
+
struct wsp_ggml_opt_optimizer_params {
|
|
70
|
+
// AdamW optimizer parameters
|
|
71
|
+
struct {
|
|
72
|
+
float alpha; // learning rate
|
|
73
|
+
float beta1;
|
|
74
|
+
float beta2;
|
|
75
|
+
float eps; // epsilon for numerical stability
|
|
76
|
+
float wd; // weight decay for AdamW, use 0.0f to disable
|
|
77
|
+
} adamw;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// callback to calculate optimizer parameters prior to a backward pass
|
|
81
|
+
// userdata can be used to pass arbitrary data
|
|
82
|
+
typedef struct wsp_ggml_opt_optimizer_params (*wsp_ggml_opt_get_optimizer_params)(void * userdata);
|
|
83
|
+
|
|
84
|
+
// returns the default optimizer params (constant)
|
|
85
|
+
// userdata is not used
|
|
86
|
+
WSP_GGML_API struct wsp_ggml_opt_optimizer_params wsp_ggml_opt_get_default_optimizer_params(void * userdata);
|
|
87
|
+
|
|
88
|
+
// parameters for initializing a new optimization context
|
|
89
|
+
struct wsp_ggml_opt_params {
|
|
90
|
+
wsp_ggml_backend_sched_t backend_sched; // defines which backends are used to construct the compute graphs
|
|
91
|
+
|
|
92
|
+
struct wsp_ggml_context * ctx_compute; // created in user code, holds non-static tensors
|
|
93
|
+
|
|
94
|
+
// the forward graph is defined by inputs and outputs
|
|
95
|
+
// those tensors and all tensors inbetween are not intended to be reusable between multiple optimization contexts
|
|
96
|
+
struct wsp_ggml_tensor * inputs;
|
|
97
|
+
struct wsp_ggml_tensor * outputs;
|
|
98
|
+
|
|
99
|
+
enum wsp_ggml_opt_loss_type loss_type;
|
|
100
|
+
enum wsp_ggml_opt_build_type build_type;
|
|
101
|
+
|
|
102
|
+
int32_t opt_period; // after how many gradient accumulation steps an optimizer step should be done
|
|
103
|
+
|
|
104
|
+
wsp_ggml_opt_get_optimizer_params get_opt_pars; // callback for calculating optimizer parameters
|
|
105
|
+
void * get_opt_pars_ud; // userdata for calculating optimizer parameters
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// get parameters for an optimization context with defaults set where possible
|
|
109
|
+
// parameters for which no sensible defaults exist are supplied as arguments to this function
|
|
110
|
+
WSP_GGML_API wsp_ggml_opt_params wsp_ggml_opt_default_params(
|
|
111
|
+
wsp_ggml_backend_sched_t backend_sched,
|
|
112
|
+
struct wsp_ggml_context * ctx_compute,
|
|
113
|
+
struct wsp_ggml_tensor * inputs,
|
|
114
|
+
struct wsp_ggml_tensor * outputs,
|
|
115
|
+
enum wsp_ggml_opt_loss_type loss_type);
|
|
116
|
+
|
|
117
|
+
WSP_GGML_API wsp_ggml_opt_context_t wsp_ggml_opt_init(struct wsp_ggml_opt_params params);
|
|
118
|
+
WSP_GGML_API void wsp_ggml_opt_free(wsp_ggml_opt_context_t opt_ctx);
|
|
119
|
+
|
|
120
|
+
// set gradients to zero, initilize loss, and optionally reset the optimizer
|
|
121
|
+
WSP_GGML_API void wsp_ggml_opt_reset(wsp_ggml_opt_context_t opt_ctx, bool optimizer);
|
|
122
|
+
|
|
123
|
+
// get underlying tensors that store data
|
|
124
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_inputs( wsp_ggml_opt_context_t opt_ctx); // forward graph input tensor
|
|
125
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_outputs( wsp_ggml_opt_context_t opt_ctx); // forward graph output tensor
|
|
126
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_labels( wsp_ggml_opt_context_t opt_ctx); // labels to compare outputs against
|
|
127
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_loss( wsp_ggml_opt_context_t opt_ctx); // scalar tensor that contains the loss
|
|
128
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_pred( wsp_ggml_opt_context_t opt_ctx); // predictions made by outputs
|
|
129
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_ncorrect(wsp_ggml_opt_context_t opt_ctx); // number of matching predictions between outputs and labels
|
|
130
|
+
|
|
131
|
+
WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_grad_acc(wsp_ggml_opt_context_t opt_ctx, struct wsp_ggml_tensor * node);
|
|
132
|
+
|
|
133
|
+
// ====== Optimization Result ======
|
|
134
|
+
|
|
135
|
+
WSP_GGML_API wsp_ggml_opt_result_t wsp_ggml_opt_result_init();
|
|
136
|
+
WSP_GGML_API void wsp_ggml_opt_result_free(wsp_ggml_opt_result_t result);
|
|
137
|
+
WSP_GGML_API void wsp_ggml_opt_result_reset(wsp_ggml_opt_result_t result);
|
|
138
|
+
|
|
139
|
+
// get data from result, uncertainties are optional and can be ignored by passing NULL
|
|
140
|
+
WSP_GGML_API void wsp_ggml_opt_result_ndata( wsp_ggml_opt_result_t result, int64_t * ndata); // writes 1 value, number of datapoints
|
|
141
|
+
WSP_GGML_API void wsp_ggml_opt_result_loss( wsp_ggml_opt_result_t result, double * loss, double * unc); // writes 1 value
|
|
142
|
+
WSP_GGML_API void wsp_ggml_opt_result_pred( wsp_ggml_opt_result_t result, int32_t * pred); // writes ndata values
|
|
143
|
+
WSP_GGML_API void wsp_ggml_opt_result_accuracy(wsp_ggml_opt_result_t result, double * accuracy, double * unc); // writes 1 value
|
|
144
|
+
|
|
145
|
+
// ====== Computation ======
|
|
146
|
+
|
|
147
|
+
// do forward pass, increment result if not NULL
|
|
148
|
+
WSP_GGML_API void wsp_ggml_opt_forward(wsp_ggml_opt_context_t opt_ctx, wsp_ggml_opt_result_t result);
|
|
149
|
+
|
|
150
|
+
// do forward pass, increment result if not NULL, do backward pass
|
|
151
|
+
WSP_GGML_API void wsp_ggml_opt_forward_backward(wsp_ggml_opt_context_t opt_ctx, wsp_ggml_opt_result_t result);
|
|
152
|
+
|
|
153
|
+
// ############################################################################
|
|
154
|
+
// ## The high-level functions start here. They do not depend on any private ##
|
|
155
|
+
// ## functions or structs and can be copied to and adapted for user code. ##
|
|
156
|
+
// ############################################################################
|
|
157
|
+
|
|
158
|
+
// ====== Intended Usage ======
|
|
159
|
+
//
|
|
160
|
+
// 1. Select the appropriate loss for your problem.
|
|
161
|
+
// 2. Create a dataset and set the data for the "data" tensor. Also set the "labels" tensor if your loss needs them.
|
|
162
|
+
// Setting the shard size to 1 will be fine, it's the granularity with which data is shuffled/loaded (bigger values are faster).
|
|
163
|
+
// 3. Create a GGML graph for your model with no_alloc == true. Use two separate contexts for the tensors.
|
|
164
|
+
// The first context should contain the model parameters and inputs and be allocated statically in user code.
|
|
165
|
+
// The second context should contain all other tensors and will be (re)allocated automatically.
|
|
166
|
+
// Due to this automated allocation the data of the second context is not defined when accessed in user code.
|
|
167
|
+
// Note that the second dimension of the inputs/outputs are interpreted as the number of datapoints in those tensors.
|
|
168
|
+
// 4. Call wsp_ggml_opt_fit. If you need more control you can use wsp_ggml_opt_epoch instead.
|
|
169
|
+
|
|
170
|
+
// signature for a callback while evaluating opt_ctx on dataset, called after an evaluation
|
|
171
|
+
typedef void (*wsp_ggml_opt_epoch_callback)(
|
|
172
|
+
bool train, // true after training evaluation, false after validation evaluation
|
|
173
|
+
wsp_ggml_opt_context_t opt_ctx,
|
|
174
|
+
wsp_ggml_opt_dataset_t dataset,
|
|
175
|
+
wsp_ggml_opt_result_t result, // result associated with the dataset subsection
|
|
176
|
+
int64_t ibatch, // number of batches that have been evaluated so far
|
|
177
|
+
int64_t ibatch_max, // total number of batches in this dataset subsection
|
|
178
|
+
int64_t t_start_us); // time at which the evaluation on the dataset subsection was started
|
|
179
|
+
|
|
180
|
+
// do training on front of dataset, do evaluation only on back of dataset
|
|
181
|
+
WSP_GGML_API void wsp_ggml_opt_epoch(
|
|
182
|
+
wsp_ggml_opt_context_t opt_ctx,
|
|
183
|
+
wsp_ggml_opt_dataset_t dataset,
|
|
184
|
+
wsp_ggml_opt_result_t result_train, // result to increment during training, ignored if NULL
|
|
185
|
+
wsp_ggml_opt_result_t result_eval, // result to increment during evaluation, ignored if NULL
|
|
186
|
+
int64_t idata_split, // data index at which to split training and evaluation
|
|
187
|
+
wsp_ggml_opt_epoch_callback callback_train,
|
|
188
|
+
wsp_ggml_opt_epoch_callback callback_eval);
|
|
189
|
+
|
|
190
|
+
// callback that prints a progress bar on stderr
|
|
191
|
+
WSP_GGML_API void wsp_ggml_opt_epoch_callback_progress_bar(
|
|
192
|
+
bool train,
|
|
193
|
+
wsp_ggml_opt_context_t opt_ctx,
|
|
194
|
+
wsp_ggml_opt_dataset_t dataset,
|
|
195
|
+
wsp_ggml_opt_result_t result,
|
|
196
|
+
int64_t ibatch,
|
|
197
|
+
int64_t ibatch_max,
|
|
198
|
+
int64_t t_start_us);
|
|
199
|
+
|
|
200
|
+
// fit model defined by inputs and outputs to dataset
|
|
201
|
+
WSP_GGML_API void wsp_ggml_opt_fit(
|
|
202
|
+
wsp_ggml_backend_sched_t backend_sched, // backend scheduler for constructing the compute graphs
|
|
203
|
+
wsp_ggml_context * ctx_compute, // context with temporarily allocated tensors to calculate the outputs
|
|
204
|
+
wsp_ggml_tensor * inputs, // input tensor with shape [ne_datapoint, ndata_batch]
|
|
205
|
+
wsp_ggml_tensor * outputs, // output tensor, must have shape [ne_label, ndata_batch] if labels are used
|
|
206
|
+
wsp_ggml_opt_dataset_t dataset, // dataset with data and optionally also labels
|
|
207
|
+
enum wsp_ggml_opt_loss_type loss_type, // loss to minimize
|
|
208
|
+
wsp_ggml_opt_get_optimizer_params get_opt_pars, // callback to get optimizer params, userdata is pointer to epoch (of type int64_t)
|
|
209
|
+
int64_t nepoch, // how many times the dataset should be iterated over
|
|
210
|
+
int64_t nbatch_logical, // datapoints optimizer step, must be a multiple of ndata_batch in inputs/outputs
|
|
211
|
+
float val_split, // fraction of the dataset to use for validation, must be in [0.0f, 1.0f)
|
|
212
|
+
bool silent); // whether or not info prints to stderr should be suppressed
|
|
213
|
+
|
|
214
|
+
#ifdef __cplusplus
|
|
215
|
+
}
|
|
216
|
+
#endif
|