-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathcore.cpp
458 lines (395 loc) · 16.4 KB
/
core.cpp
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <onnxruntime_cxx_api.h>
#ifdef DIRECTML
#include <dml_provider_factory.h>
#endif
#include <array>
#include <exception>
#include <memory>
#include <optional>
#include <string>
#include <unordered_set>
#include "embedBin/embed.h"
#include "nlohmann/json.hpp"
#ifndef VOICEVOX_CORE_EXPORTS
#define VOICEVOX_CORE_EXPORTS
#endif // VOICEVOX_CORE_EXPORTS
#include "core.h"
#define NOT_INITIALIZED_ERR "Call initialize() first."
#define NOT_LOADED_ERR "Model is not loaded."
#define ONNX_ERR "ONNX raise exception: "
#define JSON_ERR "JSON parser raise exception: "
#define GPU_NOT_SUPPORTED_ERR "This library is CPU version. GPU is not supported."
#define UNKNOWN_STYLE "Unknown style ID: "
constexpr float PHONEME_LENGTH_MINIMAL = 0.01f;
constexpr std::array<int64_t, 0> scalar_shape{};
constexpr std::array<int64_t, 1> speaker_shape{1};
static std::string error_message;
static bool initialized = false;
static std::string supported_devices_str;
EMBED_DECL(METAS);
namespace EMBED_DECL_NAMESPACE {
EMBED_DECL(YUKARIN_S);
EMBED_DECL(YUKARIN_SA);
EMBED_DECL(DECODE);
/**
* 3種類のモデルを一纏めにしたもの
*/
struct VVMODEL {
embed::EMBED_RES (*YUKARIN_S)();
embed::EMBED_RES (*YUKARIN_SA)();
embed::EMBED_RES (*DECODE)();
};
const VVMODEL VVMODEL_LIST[] = {
{YUKARIN_S, YUKARIN_SA, DECODE},
};
} // namespace EMBED_DECL_NAMESPACE
using EMBED_DECL_NAMESPACE::VVMODEL_LIST;
// 複数モデルある場合のspeaker_idマッピング
// {元のspeaker_id: {モデル番号, 新しいspeaker_id}}
const auto speaker_id_map = std::map<int64_t, std::pair<int64_t, int64_t>>{};
struct SupportedDevices {
bool cpu = true;
bool cuda = false;
bool dml = false;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SupportedDevices, cpu, cuda, dml);
SupportedDevices get_supported_devices() {
SupportedDevices devices;
const auto providers = Ort::GetAvailableProviders();
for (const std::string &p : providers) {
if (p == "CUDAExecutionProvider") {
devices.cuda = true;
} else if (p == "DmlExecutionProvider") {
devices.dml = true;
}
}
return devices;
}
struct Status {
Status(int model_count, bool use_gpu, int cpu_num_threads)
: memory_info(Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)) {
yukarin_s_list = std::vector<std::optional<Ort::Session>>(model_count);
yukarin_sa_list = std::vector<std::optional<Ort::Session>>(model_count);
decode_list = std::vector<std::optional<Ort::Session>>(model_count);
// 軽いモデルの場合はCPUの方が速い
light_session_options.SetInterOpNumThreads(cpu_num_threads).SetIntraOpNumThreads(cpu_num_threads);
// 重いモデルはGPUを使ったほうが速い
heavy_session_options.SetInterOpNumThreads(cpu_num_threads).SetIntraOpNumThreads(cpu_num_threads);
if (use_gpu) {
#ifdef DIRECTML
heavy_session_options.DisableMemPattern().SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
Ort::ThrowOnError(OrtSessionOptionsAppendExecutionProvider_DML(heavy_session_options, 0));
#else
const OrtCUDAProviderOptions cuda_options;
heavy_session_options.AppendExecutionProvider_CUDA(cuda_options);
#endif
}
}
/**
* Loads the metas.json.
*
* schema:
* [{
* name: string,
* styles: [{name: string, id: int}],
* speaker_uuid: string,
* version: string
* }]
*/
bool load_metas() {
embed::Resource metas_file = METAS();
metas = nlohmann::json::parse(metas_file.data, metas_file.data + metas_file.size);
metas_str = metas.dump();
supported_styles.clear();
for (const auto &meta : metas) {
for (const auto &style : meta["styles"]) {
supported_styles.insert(style["id"].get<int64_t>());
}
}
return true;
}
/**
* モデルを読み込む
*/
bool load_model(int model_index) {
const auto VVMODEL = VVMODEL_LIST[model_index];
embed::Resource yukarin_s_model = VVMODEL.YUKARIN_S();
embed::Resource yukarin_sa_model = VVMODEL.YUKARIN_SA();
embed::Resource decode_model = VVMODEL.DECODE();
yukarin_s_list[model_index] =
std::move(Ort::Session(env, yukarin_s_model.data, yukarin_s_model.size, light_session_options));
yukarin_sa_list[model_index] =
std::move(Ort::Session(env, yukarin_sa_model.data, yukarin_sa_model.size, light_session_options));
decode_list[model_index] =
std::move(Ort::Session(env, decode_model.data, decode_model.size, heavy_session_options));
return true;
}
std::string root_dir_path;
Ort::SessionOptions light_session_options; // 軽いモデルはこちらを使う
Ort::SessionOptions heavy_session_options; // 重いモデルはこちらを使う
Ort::MemoryInfo memory_info;
Ort::Env env{ORT_LOGGING_LEVEL_ERROR};
std::vector<std::optional<Ort::Session>> yukarin_s_list, yukarin_sa_list, decode_list;
nlohmann::json metas;
std::string metas_str;
std::unordered_set<int64_t> supported_styles;
};
static std::unique_ptr<Status> status;
template <typename T, size_t Rank>
Ort::Value to_tensor(T *data, const std::array<int64_t, Rank> &shape) {
int64_t count = 1;
for (int64_t dim : shape) {
count *= dim;
}
return Ort::Value::CreateTensor<T>(status->memory_info, data, count, shape.data(), shape.size());
}
bool validate_speaker_id(int64_t speaker_id) {
if (status->supported_styles.find(speaker_id) == status->supported_styles.end()) {
error_message = UNKNOWN_STYLE + std::to_string(speaker_id);
return false;
}
return true;
}
/**
* 複数モデルあった場合のspeaker_idマッピング
*/
std::pair<int64_t, int64_t> get_model_index_and_speaker_id(int64_t speaker_id) {
const auto found = speaker_id_map.find(speaker_id);
if (found == speaker_id_map.end()) {
return {0, speaker_id};
}
return found->second;
}
bool initialize(bool use_gpu, int cpu_num_threads, bool load_all_models) {
initialized = false;
#ifdef DIRECTML
if (use_gpu && !get_supported_devices().dml) {
#else
if (use_gpu && !get_supported_devices().cuda) {
#endif /*DIRECTML*/
error_message = GPU_NOT_SUPPORTED_ERR;
return false;
}
try {
const int model_count = std::size(VVMODEL_LIST);
status = std::make_unique<Status>(model_count, use_gpu, cpu_num_threads);
if (!status->load_metas()) {
return false;
}
if (load_all_models) {
for (int model_index = 0; model_index < model_count; model_index++) {
if (!status->load_model(model_index)) {
return false;
}
}
if (use_gpu) {
// 一回走らせて十分なGPUメモリを確保させる
// TODO: 全MODELに対して行う
int length = 500;
int phoneme_size = 45;
std::vector<float> phoneme(length * phoneme_size), f0(length);
int64_t speaker_id = 0;
std::vector<float> output(length * 256);
decode_forward(length, phoneme_size, f0.data(), phoneme.data(), &speaker_id, output.data());
}
}
} catch (const Ort::Exception &e) {
error_message = ONNX_ERR;
error_message += e.what();
return false;
} catch (const nlohmann::json::exception &e) {
error_message = JSON_ERR;
error_message += e.what();
return false;
} catch (const std::exception &e) {
error_message = e.what();
return false;
}
initialized = true;
return true;
}
bool load_model(int64_t speaker_id) {
auto [model_index, _] = get_model_index_and_speaker_id(speaker_id);
return status->load_model(model_index);
}
bool is_model_loaded(int64_t speaker_id) {
auto [model_index, _] = get_model_index_and_speaker_id(speaker_id);
return (status->yukarin_s_list[model_index].has_value() && status->yukarin_sa_list[model_index].has_value() &&
status->decode_list[model_index].has_value());
}
void finalize() {
initialized = false;
status.reset();
}
const char *metas() { return status->metas_str.c_str(); }
const char *supported_devices() {
SupportedDevices devices = get_supported_devices();
nlohmann::json json = devices;
supported_devices_str = json.dump();
return supported_devices_str.c_str();
}
bool yukarin_s_forward(int64_t length, int64_t *phoneme_list, int64_t *speaker_id, float *output) {
if (!initialized) {
error_message = NOT_INITIALIZED_ERR;
return false;
}
if (!validate_speaker_id(*speaker_id)) {
return false;
}
auto [model_index, model_speaker_id] = get_model_index_and_speaker_id(*speaker_id);
auto &model = status->yukarin_s_list[model_index];
if (!model) {
error_message = NOT_LOADED_ERR;
return false;
}
try {
const char *inputs[] = {"phoneme_list", "speaker_id"};
const char *outputs[] = {"phoneme_length"};
const std::array<int64_t, 1> phoneme_shape{length};
std::array<Ort::Value, 2> input_tensors = {to_tensor(phoneme_list, phoneme_shape),
to_tensor(&model_speaker_id, speaker_shape)};
Ort::Value output_tensor = to_tensor(output, phoneme_shape);
model.value().Run(Ort::RunOptions{nullptr}, inputs, input_tensors.data(), input_tensors.size(), outputs,
&output_tensor, 1);
for (int64_t i = 0; i < length; i++) {
if (output[i] < PHONEME_LENGTH_MINIMAL) output[i] = PHONEME_LENGTH_MINIMAL;
}
} catch (const Ort::Exception &e) {
error_message = ONNX_ERR;
error_message += e.what();
return false;
}
return true;
}
bool yukarin_sa_forward(int64_t length, int64_t *vowel_phoneme_list, int64_t *consonant_phoneme_list,
int64_t *start_accent_list, int64_t *end_accent_list, int64_t *start_accent_phrase_list,
int64_t *end_accent_phrase_list, int64_t *speaker_id, float *output) {
if (!initialized) {
error_message = NOT_INITIALIZED_ERR;
return false;
}
if (!validate_speaker_id(*speaker_id)) {
return false;
}
auto [model_index, model_speaker_id] = get_model_index_and_speaker_id(*speaker_id);
auto &model = status->yukarin_sa_list[model_index];
if (!model) {
error_message = NOT_LOADED_ERR;
return false;
}
try {
const char *inputs[] = {
"length", "vowel_phoneme_list", "consonant_phoneme_list", "start_accent_list",
"end_accent_list", "start_accent_phrase_list", "end_accent_phrase_list", "speaker_id"};
const char *outputs[] = {"f0_list"};
const std::array<int64_t, 1> phoneme_shape{length};
std::array<Ort::Value, 8> input_tensors = {to_tensor(&length, scalar_shape),
to_tensor(vowel_phoneme_list, phoneme_shape),
to_tensor(consonant_phoneme_list, phoneme_shape),
to_tensor(start_accent_list, phoneme_shape),
to_tensor(end_accent_list, phoneme_shape),
to_tensor(start_accent_phrase_list, phoneme_shape),
to_tensor(end_accent_phrase_list, phoneme_shape),
to_tensor(&model_speaker_id, speaker_shape)};
Ort::Value output_tensor = to_tensor(output, phoneme_shape);
model.value().Run(Ort::RunOptions{nullptr}, inputs, input_tensors.data(), input_tensors.size(), outputs,
&output_tensor, 1);
} catch (const Ort::Exception &e) {
error_message = ONNX_ERR;
error_message += e.what();
return false;
}
return true;
}
std::vector<float> make_f0_with_padding(float *f0, int64_t length, int64_t length_with_padding,
int64_t padding_f0_size) {
std::vector<float> f0_with_padding;
f0_with_padding.reserve(length_with_padding);
f0_with_padding.insert(f0_with_padding.end(), padding_f0_size, 0.0);
f0_with_padding.insert(f0_with_padding.end(), f0, f0 + length);
f0_with_padding.insert(f0_with_padding.end(), padding_f0_size, 0.0);
return f0_with_padding;
}
void insert_padding_phonemes_to_phoneme_with_padding(std::vector<float> &phoneme_with_padding,
const std::vector<float> &padding_phoneme,
int64_t padding_phonemes_size) {
for (auto i = 0; i < padding_phonemes_size; i++) {
phoneme_with_padding.insert(phoneme_with_padding.end(), padding_phoneme.begin(), padding_phoneme.end());
}
}
std::vector<float> make_phoneme_with_padding(float *phoneme, int64_t phoneme_size, int64_t length,
int64_t length_with_padding, int64_t padding_phonemes_size) {
// 無音部分をphonemeに追加するための処理
// TODO: 改善したらここのcopy処理を取り除く
std::vector<float> padding_phoneme(phoneme_size, 0.0);
// 一番はじめのphonemeを有効化することで無音となる
padding_phoneme[0] = 1;
std::vector<float> phoneme_with_padding;
phoneme_with_padding.reserve(length_with_padding * phoneme_size);
insert_padding_phonemes_to_phoneme_with_padding(phoneme_with_padding, padding_phoneme, padding_phonemes_size);
const auto phoneme_dimension_size = length * phoneme_size;
phoneme_with_padding.insert(phoneme_with_padding.end(), phoneme, phoneme + phoneme_dimension_size);
insert_padding_phonemes_to_phoneme_with_padding(phoneme_with_padding, padding_phoneme, padding_phonemes_size);
return phoneme_with_padding;
}
void copy_output_with_padding_to_output(const std::vector<float> &output_with_padding, float *output,
int64_t padding_f0_size) {
const auto padding_sampling_size = padding_f0_size * 256;
const auto begin_output_copy = output_with_padding.begin() + padding_sampling_size;
const auto end_output_copy = output_with_padding.end() - padding_sampling_size;
std::copy(begin_output_copy, end_output_copy, output);
}
bool decode_forward(int64_t length, int64_t phoneme_size, float *f0, float *phoneme, int64_t *speaker_id,
float *output) {
if (!initialized) {
error_message = NOT_INITIALIZED_ERR;
return false;
}
if (!validate_speaker_id(*speaker_id)) {
return false;
}
auto [model_index, model_speaker_id] = get_model_index_and_speaker_id(*speaker_id);
auto &model = status->decode_list[model_index];
if (!model) {
error_message = NOT_LOADED_ERR;
return false;
}
try {
// 音が途切れてしまうのを避けるworkaround処理が入っている
// TODO: 改善したらここのpadding処理を取り除く
constexpr auto padding_size = 0.4;
constexpr auto default_sampling_rate = 24000;
const auto padding_f0_size =
static_cast<int64_t>(std::round(static_cast<double>(padding_size * default_sampling_rate) / 256));
const auto start_and_end_padding_f0_size = 2 * padding_f0_size;
const auto length_with_padding = length + start_and_end_padding_f0_size;
// TODO: 改善したらここの処理を取り除く
auto f0_with_padding = make_f0_with_padding(f0, length, length_with_padding, padding_f0_size);
// TODO: 改善したらここの処理を取り除く
const auto padding_phonemes_size = padding_f0_size;
auto phoneme_with_padding =
make_phoneme_with_padding(phoneme, phoneme_size, length, length_with_padding, padding_phonemes_size);
const std::array<int64_t, 2> f0_shape{length_with_padding, 1}, phoneme_shape{length_with_padding, phoneme_size};
std::array<Ort::Value, 3> input_tensor = {to_tensor(f0_with_padding.data(), f0_shape),
to_tensor(phoneme_with_padding.data(), phoneme_shape),
to_tensor(&model_speaker_id, speaker_shape)};
// TODO: 改善したらここのpadding処理を取り除く
const auto output_with_padding_size = length_with_padding * 256;
const std::array<int64_t, 1> wave_shape{output_with_padding_size};
// TODO: 改善したらここの処理を取り除く
std::vector<float> output_with_padding(output_with_padding_size, 0.0);
Ort::Value output_tensor = to_tensor(output_with_padding.data(), wave_shape);
const char *inputs[] = {"f0", "phoneme", "speaker_id"};
const char *outputs[] = {"wave"};
model.value().Run(Ort::RunOptions{nullptr}, inputs, input_tensor.data(), input_tensor.size(), outputs,
&output_tensor, 1);
// TODO: 改善したらここのcopy処理を取り除く
copy_output_with_padding_to_output(output_with_padding, output, padding_f0_size);
} catch (const Ort::Exception &e) {
error_message = ONNX_ERR;
error_message += e.what();
return false;
}
return true;
}
const char *last_error_message() { return error_message.c_str(); }