-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.cpp
More file actions
352 lines (318 loc) · 13.9 KB
/
Copy pathpipeline.cpp
File metadata and controls
352 lines (318 loc) · 13.9 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "pipeline.h"
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <sstream>
#include <stdexcept>
#include "encode_helpers.h"
#include "gguf_util.h"
#include "mel.h"
#include "midi_io.h"
#include "resample.h"
#include "wav_io.h"
namespace muscriptor {
TranscriptionModel::TranscriptionModel(std::shared_ptr<LMModel> model, MT3Tokenizer tokenizer)
: model_(std::move(model)), tokenizer_(std::move(tokenizer)) {}
TranscriptionModel TranscriptionModel::load_gguf(const std::string& path) {
auto lm = std::make_shared<LMModel>(LMModel::load_gguf(path));
MT3Tokenizer tok(1001, 100);
TranscriptionModel tm(lm, tok);
// Try load mel weights from same GGUF
try {
GgufModel gguf(path);
auto load_f32 = [&](const std::string& name) {
auto* t = gguf.tensor(name);
size_t n = ggml_nelements(t);
std::vector<float> v(n);
if (t->type == GGML_TYPE_F32) {
std::memcpy(v.data(), t->data, n * sizeof(float));
} else if (t->type == GGML_TYPE_F16) {
const ggml_fp16_t* src = static_cast<const ggml_fp16_t*>(t->data);
for (size_t i = 0; i < n; ++i) v[i] = ggml_fp16_to_fp32(src[i]);
}
return v;
};
auto load_first = [&](std::initializer_list<const char*> names) {
for (const char* n : names) {
if (gguf.has_tensor(n)) return load_f32(n);
}
throw std::runtime_error(std::string("missing tensor: ") + *names.begin());
};
auto window = load_first({
"conditioners.self_wav.mel.window",
"condition_provider.conditioners.self_wav.mel_spec_transform.spectrogram.window"});
auto fb = load_first({
"conditioners.self_wav.mel.fb",
"condition_provider.conditioners.self_wav.mel_spec_transform.mel_scale.fb"});
auto proj_w = load_first({
"conditioners.self_wav.output_proj.weight",
"condition_provider.conditioners.self_wav.output_proj.weight"});
std::vector<float> proj_b;
if (gguf.has_tensor("conditioners.self_wav.output_proj.bias") ||
gguf.has_tensor("condition_provider.conditioners.self_wav.output_proj.bias")) {
proj_b = load_first({
"conditioners.self_wav.output_proj.bias",
"condition_provider.conditioners.self_wav.output_proj.bias"});
}
tm.set_mel_weights(std::move(window), std::move(fb), std::move(proj_w),
std::move(proj_b), 512, lm->dim());
// ClassConditioner tables (Python: tokenize adds +1, forward embed(+1) again).
auto load_embed = [&](std::initializer_list<const char*> names) {
return load_first(names); // row-major [n_classes, dim]
};
tm.instrument_group_embed_ = load_embed({
"conditioners.instrument_group.embed.weight",
"condition_provider.conditioners.instrument_group.embed.weight"});
tm.instrument_group_rows_ =
static_cast<int>(tm.instrument_group_embed_.size() / lm->dim());
auto dn = load_embed({
"conditioners.dataset_name.embed.weight",
"condition_provider.conditioners.dataset_name.embed.weight"});
const int dim = lm->dim();
if (static_cast<int>(dn.size()) < 2 * dim) {
throw std::runtime_error("dataset_name embed too small for null row");
}
tm.dataset_name_null_.T = 1;
tm.dataset_name_null_.dim = dim;
tm.dataset_name_null_.data.assign(dn.begin() + dim, dn.begin() + 2 * dim);
tm.has_class_conditioners_ = true;
} catch (const std::exception& e) {
std::fprintf(stderr, "[muscriptor] warning: conditioner weights not loaded: %s\n", e.what());
}
return tm;
}
void TranscriptionModel::set_mel_weights(std::vector<float> window, std::vector<float> fb,
std::vector<float> proj_w, std::vector<float> proj_b,
int n_mels, int dim) {
mel_window_ = std::move(window);
mel_fb_ = std::move(fb);
mel_proj_w_ = std::move(proj_w);
mel_proj_b_ = std::move(proj_b);
n_mels_ = n_mels;
(void)dim;
has_mel_weights_ = true;
}
int TranscriptionModel::resolve_batch_size(int batch_size, bool prelude_forcing) const {
if (prelude_forcing && batch_size > 1) {
throw std::runtime_error(
"prelude_forcing requires batch_size=1; pass prelude_forcing=false to batch");
}
if (batch_size > 0) return batch_size;
return 1; // default with forcing; without forcing still 1 on CPU
}
ConditionTensor TranscriptionModel::encode_mel_chunk(const float* wav, size_t n_samples) {
MelSpectrogramConfig cfg;
cfg.sample_rate = kSampleRate;
cfg.n_fft = 2048;
cfg.hop_length = 160;
cfg.n_mels = n_mels_;
cfg.power = 1.f;
cfg.center = true;
cfg.log_scale = true;
std::vector<float> window;
std::vector<float> fb;
if (has_mel_weights_) {
window = mel_window_;
fb = mel_fb_;
} else {
window.resize(cfg.n_fft);
for (int i = 0; i < cfg.n_fft; ++i) {
window[i] = 0.5f * (1.f - std::cos(2.f * static_cast<float>(M_PI) * i /
static_cast<float>(cfg.n_fft - 1)));
}
fb = melscale_fbanks(cfg.n_fft / 2 + 1, 0.f, cfg.sample_rate / 2.f, cfg.n_mels,
cfg.sample_rate);
}
int n_frames = 0;
auto mel = compute_mel_spectrogram(wav, n_samples, window.data(), fb.data(), cfg, &n_frames);
ConditionTensor cond;
cond.dim = model_->dim();
cond.T = n_frames;
if (has_mel_weights_ && !mel_proj_w_.empty()) {
const float* bias = mel_proj_b_.empty() ? nullptr : mel_proj_b_.data();
cond.data = mel_project(mel.data(), n_frames, n_mels_, mel_proj_w_.data(),
model_->dim(), bias);
} else {
// Zero projection for tiny tests without weights
cond.data.assign(static_cast<size_t>(n_frames) * model_->dim(), 0.f);
}
return cond;
}
ConditionTensor TranscriptionModel::embed_instrument_group(
const std::vector<std::string>& instruments) const {
const int dim = model_->dim();
ConditionTensor c;
c.dim = dim;
if (instrument_group_rows_ < 2 || instrument_group_embed_.empty()) {
c.T = 1;
c.data.assign(dim, 0.f);
return c;
}
auto row = [&](int r) -> const float* {
if (r < 0 || r >= instrument_group_rows_) {
throw std::runtime_error("instrument_group embed row out of range: " +
std::to_string(r));
}
return instrument_group_embed_.data() + static_cast<size_t>(r) * dim;
};
// Python ClassConditioner: tokenize(None)->0, forward embed(0+1)->row 1.
// tokenize("g") -> 1+g, forward embed(1+g+1)->row g+2.
if (instruments.empty()) {
c.T = 1;
c.data.assign(row(1), row(1) + dim);
return c;
}
std::string ids = instrument_group_from_names(instruments);
std::stringstream ss(ids);
std::vector<int> gids;
int g = 0;
while (ss >> g) gids.push_back(g);
c.T = static_cast<int>(gids.size());
c.data.resize(static_cast<size_t>(c.T) * dim);
for (int i = 0; i < c.T; ++i) {
const float* src = row(gids[static_cast<size_t>(i)] + 2);
std::memcpy(c.data.data() + static_cast<size_t>(i) * dim, src,
static_cast<size_t>(dim) * sizeof(float));
}
return c;
}
std::vector<ConditionTensor> TranscriptionModel::build_conditions(
const float* wav, size_t n_samples, const std::vector<std::string>& instruments) {
// Match Python ConditioningProvider order: instrument_group, dataset_name, self_wav.
std::vector<ConditionTensor> out;
if (has_class_conditioners_) {
out.push_back(embed_instrument_group(instruments));
out.push_back(dataset_name_null_);
}
out.push_back(encode_mel_chunk(wav, n_samples));
return out;
}
std::vector<DecodedEvent> TranscriptionModel::transcribe(const std::vector<float>& wav_16k,
const TranscribeOptions& opts) {
if (opts.prelude_forcing && opts.batch_size > 1) {
throw std::runtime_error("--batch-size > 1 requires --no-prelude-forcing");
}
if (opts.cfg_coef != 1.f) {
std::fprintf(stderr,
"[muscriptor] warning: --cfg-coef %.3g accepted for CLI parity; "
"released models are post-RL and use cfg_coef=1 "
"(CFG batching not applied)\n",
opts.cfg_coef);
}
int batch_size = resolve_batch_size(opts.batch_size, opts.prelude_forcing);
std::vector<int> forbidden;
if (!opts.instruments.empty()) {
forbidden = tokenizer_.forbidden_token_ids(opts.instruments);
}
int segment_samples = static_cast<int>(kSegmentDuration * kSampleRate);
int num_chunks = static_cast<int>(
std::ceil(static_cast<double>(wav_16k.size()) / segment_samples));
if (num_chunks == 0) num_chunks = 1;
std::fprintf(stderr, "[muscriptor] audio: %.1fs → %d chunk(s) of %.0fs\n",
static_cast<double>(wav_16k.size()) / kSampleRate, num_chunks,
static_cast<double>(kSegmentDuration));
std::vector<StreamItem> stream;
stream.push_back(ProgressEvent{0, num_chunks});
OpenNoteTracker tracker(tokenizer_.vocab(), tokenizer_.frame_rate());
for (int i = 0; i < num_chunks; i += batch_size) {
int batch_end = std::min(i + batch_size, num_chunks);
for (int ci = i; ci < batch_end; ++ci) {
size_t start = static_cast<size_t>(ci) * segment_samples;
std::vector<float> chunk(segment_samples, 0.f);
size_t avail = start < wav_16k.size() ? wav_16k.size() - start : 0;
if (avail > 0) {
std::memcpy(chunk.data(), wav_16k.data() + start,
std::min(avail, static_cast<size_t>(segment_samples)) * sizeof(float));
}
float seek_time = ci * kSegmentDuration;
std::optional<float> next_seek =
(ci + 1 < num_chunks) ? std::optional<float>((ci + 1) * kSegmentDuration)
: std::nullopt;
stream.push_back(ChunkBoundary{seek_time, next_seek});
tracker.feed(ChunkBoundary{seek_time, next_seek});
auto conditions =
build_conditions(chunk.data(), chunk.size(), opts.instruments);
GenerateConfig gcfg;
gcfg.max_gen_len = opts.max_gen_len;
gcfg.use_sampling = opts.use_sampling;
gcfg.temp = opts.temperature;
gcfg.cfg_coef = opts.cfg_coef;
gcfg.beam_size = opts.beam_size;
gcfg.early_stop_on_token = tokenizer_.eos_id();
gcfg.forbidden_tokens = forbidden;
if (opts.prelude_forcing && ci > 0) {
gcfg.prompt = tokenizer_.tie_section_token_ids(tracker.open_keys());
}
model_->reset_cache();
bool saw_eos = false;
auto tokens = model_->generate(conditions, gcfg, [&](int tok) {
if (tok == tokenizer_.eos_id()) {
saw_eos = true;
return;
}
if (!saw_eos) {
stream.push_back(tok);
tracker.feed(tok);
}
});
(void)tokens;
if (!saw_eos && !opts.no_eos_is_ok) {
throw std::runtime_error("chunk did not emit EOS");
}
if (!saw_eos) {
std::fprintf(stderr, "[muscriptor] warning: chunk %d missing EOS\n", ci);
}
}
stream.push_back(ProgressEvent{batch_end, num_chunks});
}
return decode_model_tokens(
stream, tokenizer_.vocab(),
[this](int program) { return tokenizer_.instrument_for_program(program); },
tokenizer_.frame_rate());
}
std::vector<uint8_t> TranscriptionModel::transcribe_to_midi(const std::vector<float>& wav_16k,
const TranscribeOptions& opts) {
auto events = transcribe(wav_16k, opts);
std::vector<Note> notes;
std::map<int, NoteStartEvent> open;
for (const auto& ev : events) {
if (std::holds_alternative<NoteStartEvent>(ev)) {
const auto& s = std::get<NoteStartEvent>(ev);
open[s.index] = s;
} else if (std::holds_alternative<NoteEndEvent>(ev)) {
const auto& e = std::get<NoteEndEvent>(ev);
const auto& s = e.start_event;
bool is_drum = (s.instrument == "drums");
int program = is_drum ? DRUM_PROGRAM : 0;
// Recover program from instrument name via group map
if (!is_drum) {
for (const auto& [name, gid] : MT3_FULL_PLUS_GROUP_NAMES) {
if (name == s.instrument) {
auto& gmap = tokenizer_.group_program_map();
auto it = gmap.find(gid);
if (it != gmap.end() && !it->second.empty()) {
program = it->second.front();
}
break;
}
}
}
notes.push_back({is_drum, program, s.start_time, e.end_time, s.pitch});
}
}
notes = validate_notes(notes);
notes = trim_overlapping_notes(notes);
return notes_to_midi_bytes(notes);
}
std::vector<DecodedEvent> TranscriptionModel::transcribe_file(const std::string& path,
const TranscribeOptions& opts) {
int sr = 0;
auto wav = read_wav_f32_mono(path, &sr);
if (wav.empty()) throw std::runtime_error("failed to read wav: " + path);
if (sr != kSampleRate) {
wav = resample_frac(wav, sr, kSampleRate);
}
return transcribe(wav, opts);
}
} // namespace muscriptor