File size: 5,246 Bytes
ab1b068 | 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 | // Compare the C++ engine with the PyTorch reference dumps (scripts/tts_hi_reference.py), stage by stage.
// tts_check model.gguf tts_cpp/ref/case0 [threads]
#include <chrono>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
#include "tts.h"
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#include <psapi.h>
#endif
template <typename T>
static std::vector<T> read_bin(const std::string& path) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) { std::fprintf(stderr, "missing %s\n", path.c_str()); std::exit(1); }
const size_t n = size_t(f.tellg()) / sizeof(T);
std::vector<T> v(n);
f.seekg(0);
f.read(reinterpret_cast<char*>(v.data()), std::streamsize(n * sizeof(T)));
return v;
}
static void compare(const char* name, const std::vector<float>& ref, const std::vector<float>& got) {
if (ref.size() != got.size()) {
std::printf(" %-15s size differs: ref %zu vs engine %zu\n", name, ref.size(), got.size());
return;
}
double max_abs = 0, err = 0, pow_ = 0;
for (size_t i = 0; i < ref.size(); ++i) {
const double d = double(got[i]) - ref[i];
max_abs = std::max(max_abs, std::fabs(d));
err += d * d; pow_ += double(ref[i]) * ref[i];
}
const double snr = 10 * std::log10(pow_ / std::max(err, 1e-30));
std::printf(" %-15s max|diff| %.3g SNR %.1f dB\n", name, max_abs, snr);
}
int main(int argc, char** argv) {
if (argc < 3) { std::fprintf(stderr, "usage: tts_check model.gguf ref_case_dir [threads]\n"); return 2; }
const std::string dir = argv[2];
auto model = TtsModel::load(argv[1], argc > 3 ? std::atoi(argv[3]) : 4);
int style = 0; unsigned seed = 0; float octave = 1.6f; int steps = 0; float temperature = -1.0f, guidance = 0.0f;
{ std::ifstream f(dir + "/inputs.txt"); f >> style >> seed >> octave; f >> steps >> temperature; f >> guidance; }
const auto ids = read_bin<int32_t>(dir + "/ids.bin"), stress = read_bin<int32_t>(dir + "/stress.bin");
const bool flow = std::ifstream(dir + "/x0.bin").good(); // scripts/tts_hi_flow_reference.py
const auto noise = read_bin<float>(dir + (flow ? "/x0.bin" : "/noise.bin"));
TtsOptions opt; opt.style = style; opt.octave_fix = octave; opt.noise = noise.data();
std::vector<float> dur_noise, pe_noise;
if (flow) {
opt.flow_steps = steps; opt.temperature = temperature; opt.guidance = guidance;
if (std::ifstream(dir + "/dur_x0.bin").good()) { // prosody-flow model
dur_noise = read_bin<float>(dir + "/dur_x0.bin"); pe_noise = read_bin<float>(dir + "/pe_x0.bin");
opt.duration_noise = dur_noise.data(); opt.pitch_energy_noise = pe_noise.data();
}
}
TtsDebug dbg;
auto t0 = std::chrono::steady_clock::now();
const std::vector<float> wave = model->synthesize(ids, stress, opt, &dbg);
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
const auto ref_dur = read_bin<int32_t>(dir + "/durations.bin");
int mismatch = 0;
for (size_t i = 0; i < ref_dur.size() && i < dbg.durations.size(); ++i) mismatch += ref_dur[i] != dbg.durations[i];
std::printf("%s: %zu tokens, style %s, %d frames (%.2f s audio) in %.1f ms\n", dir.c_str(), ids.size(),
model->styles()[style].c_str(), dbg.frames, double(wave.size()) / model->sample_rate(), ms);
std::printf(" durations %d of %zu tokens differ\n", mismatch, ref_dur.size());
if (flow) {
if (mismatch == 0) {
compare("flow_cond", read_bin<float>(dir + "/cond.bin"), dbg.flow_cond);
compare("mel", read_bin<float>(dir + "/mel.bin"), dbg.mel);
compare("wave", read_bin<float>(dir + "/wave.bin"), wave);
} else {
std::printf(" (later stages not comparable: different frame counts)\n");
}
} else {
compare("encoder", read_bin<float>(dir + "/encoder.bin"), dbg.encoder);
compare("duration_raw", read_bin<float>(dir + "/duration_raw.bin"), dbg.duration_raw);
}
if (!flow && mismatch == 0) {
compare("prosody_hidden", read_bin<float>(dir + "/prosody_hidden.bin"), dbg.prosody_hidden);
compare("prosody_raw", read_bin<float>(dir + "/prosody_raw.bin"), dbg.prosody_raw);
compare("prosody", read_bin<float>(dir + "/prosody.bin"), dbg.prosody);
compare("acoustic", read_bin<float>(dir + "/acoustic.bin"), dbg.acoustic);
compare("harmonic_head", read_bin<float>(dir + "/harmonic_head.bin"), dbg.harmonic_head);
compare("noise_head", read_bin<float>(dir + "/noise_head.bin"), dbg.noise_head);
compare("f0", read_bin<float>(dir + "/f0.bin"), dbg.f0);
compare("wave", read_bin<float>(dir + "/wave.bin"), wave);
} else if (!flow) {
std::printf(" (later stages not comparable: different frame counts)\n");
}
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS pmc{};
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
std::printf(" RAM: peak working set %.1f MB (weights %.1f MB)\n", pmc.PeakWorkingSetSize / 1048576.0,
model->weight_bytes() / 1048576.0);
#endif
return 0;
}
|