-
Notifications
You must be signed in to change notification settings - Fork 56
/
bark.h
258 lines (204 loc) · 6.08 KB
/
bark.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <map>
#include <random>
#include <thread>
#include <vector>
#include "encodec.h"
#include "ggml-backend.h"
#include "ggml.h"
enum class bark_verbosity_level {
LOW = 0,
MEDIUM = 1,
HIGH = 2,
};
typedef int32_t bark_token;
typedef std::vector<int32_t> bark_sequence;
typedef std::vector<std::vector<int32_t>> bark_codes;
struct gpt_hparams {
int32_t n_in_vocab;
int32_t n_out_vocab;
int32_t n_layer;
int32_t n_head;
int32_t n_embd;
int32_t block_size;
int32_t n_lm_heads;
int32_t n_wtes;
int32_t ftype;
int32_t bias;
int32_t n_codes_given = 1;
};
struct bark_vocab {
using id = int32_t;
using token = std::string;
std::map<token, id> token_to_id;
std::map<id, token> id_to_token;
};
struct gpt_layer {
// normalization
struct ggml_tensor *ln_1_g;
struct ggml_tensor *ln_1_b;
struct ggml_tensor *ln_2_g;
struct ggml_tensor *ln_2_b;
// attention
struct ggml_tensor *c_attn_attn_w;
struct ggml_tensor *c_attn_attn_b;
struct ggml_tensor *c_attn_proj_w;
struct ggml_tensor *c_attn_proj_b;
// mlp
struct ggml_tensor *c_mlp_fc_w;
struct ggml_tensor *c_mlp_fc_b;
struct ggml_tensor *c_mlp_proj_w;
struct ggml_tensor *c_mlp_proj_b;
};
struct gpt_model {
gpt_hparams hparams;
// normalization
struct ggml_tensor *ln_f_g;
struct ggml_tensor *ln_f_b;
struct ggml_tensor *wpe; // position embedding
std::vector<struct ggml_tensor *> wtes; // token embedding
std::vector<struct ggml_tensor *> lm_heads; // language model head
std::vector<gpt_layer> layers;
// key + value memory
struct ggml_tensor *memory_k;
struct ggml_tensor *memory_v;
struct ggml_context *ctx;
ggml_backend_t backend = NULL;
ggml_backend_buffer_t buffer_w;
ggml_backend_buffer_t buffer_kv;
std::map<std::string, struct ggml_tensor *> tensors;
//
int64_t t_sample_us = 0;
int64_t t_predict_us = 0;
int64_t t_main_us = 0;
//
int64_t n_sample = 0;
//
int64_t memsize = 0;
};
struct bark_model {
// encoder
gpt_model coarse_model;
gpt_model fine_model;
gpt_model semantic_model;
// vocab
bark_vocab vocab;
};
struct bark_context_params {
// Verbosity level
bark_verbosity_level verbosity;
// Temperature for sampling (text and coarse encoders)
float temp;
// Temperature for sampling (fine encoder)
float fine_temp;
// Minimum probability for EOS token (text encoder)
float min_eos_p;
// Sliding window size for coarse encoder
int32_t sliding_window_size;
// Max history for coarse encoder
int32_t max_coarse_history;
// Sample rate
int32_t sample_rate;
// Target bandwidth
int32_t target_bandwidth;
// CLS token ID
int32_t cls_token_id;
// SEP token ID
int32_t sep_token_id;
// Maximum number of semantic tokens to generate
int32_t n_steps_text_encoder;
// Text PAD token ID
int32_t text_pad_token;
// Text encoding offset
int32_t text_encoding_offset;
// Semantic frequency rate
float semantic_rate_hz;
// Semantic PAD token ID
int32_t semantic_pad_token;
// Vocabulary size in semantic encoder
int32_t semantic_vocab_size;
// Semantic infernce token ID
int32_t semantic_infer_token;
// Coarse frequency rate
float coarse_rate_hz;
// Coarse infer token ID
int32_t coarse_infer_token;
// Coarse semantic pad token ID
int32_t coarse_semantic_pad_token;
// Number of codebooks in coarse encoder
int32_t n_coarse_codebooks;
// Number of codebooks in fine encoder
int32_t n_fine_codebooks;
// Dimension of the codes
int32_t codebook_size;
};
struct bark_context {
bark_model text_model;
struct encodec_context *encodec_ctx;
// buffer for model evaluation
ggml_backend_buffer_t buf_compute;
// custom allocator
struct ggml_allocr *allocr = NULL;
int n_gpu_layers = 0;
std::mt19937 rng;
bark_sequence tokens;
bark_sequence semantic_tokens;
bark_codes coarse_tokens;
bark_codes fine_tokens;
std::vector<float> audio_arr;
// hyperparameters
bark_context_params params;
// statistics
int64_t t_load_us = 0;
int64_t t_eval_us = 0;
// encodec parameters
std::string encodec_model_path;
};
/**
* @brief Returns the default parameters for a bark context.
*
* @return bark_context_params The default parameters for a bark context.
*/
struct bark_context_params bark_context_default_params(void);
/**
* Loads a BARK model from the specified file path with the given parameters.
*
* @param model_path The directory path of the bark model to load.
* @param verbosity The verbosity level when loading the model.
* @param seed The seed to use for random number generation.
* @return A pointer to the loaded bark model context.
*/
struct bark_context *bark_load_model(
const std::string &model_path,
bark_verbosity_level verbosity,
uint32_t seed);
/**
* Generates an audio file from the given text using the specified Bark context.
*
* @param bctx The Bark context to use for generating the audio.
* @param text The text to generate audio from.
* @param n_threads The number of threads to use for generating the audio.
* @return An integer indicating the success of the audio generation process.
*/
bool bark_generate_audio(
struct bark_context *bctx,
const std::string &text,
int n_threads);
/**
* Quantizes a bark model and saves the result to a file.
*
* @param fname_inp The name of the input file containing the BARK model.
* @param fname_out The name of the output file to save the quantized model to.
* @param ftype The type of the model's floating-point values.
* @return True if the model was successfully quantized and saved, false otherwise.
*/
bool bark_model_quantize(
const std::string &fname_inp,
const std::string &fname_out,
ggml_ftype ftype);
/**
* @brief Frees the memory allocated for a bark context.
*
* @param bctx The bark context to free.
*/
void bark_free(
struct bark_context *bctx);