// 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 #include #include #include #include #include #include "tts.h" #ifdef _WIN32 #define NOMINMAX #include #include #endif template static std::vector 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 v(n); f.seekg(0); f.read(reinterpret_cast(v.data()), std::streamsize(n * sizeof(T))); return v; } static void compare(const char* name, const std::vector& ref, const std::vector& 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(dir + "/ids.bin"), stress = read_bin(dir + "/stress.bin"); const bool flow = std::ifstream(dir + "/x0.bin").good(); // scripts/tts_hi_flow_reference.py const auto noise = read_bin(dir + (flow ? "/x0.bin" : "/noise.bin")); TtsOptions opt; opt.style = style; opt.octave_fix = octave; opt.noise = noise.data(); std::vector 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(dir + "/dur_x0.bin"); pe_noise = read_bin(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 wave = model->synthesize(ids, stress, opt, &dbg); const double ms = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); const auto ref_dur = read_bin(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(dir + "/cond.bin"), dbg.flow_cond); compare("mel", read_bin(dir + "/mel.bin"), dbg.mel); compare("wave", read_bin(dir + "/wave.bin"), wave); } else { std::printf(" (later stages not comparable: different frame counts)\n"); } } else { compare("encoder", read_bin(dir + "/encoder.bin"), dbg.encoder); compare("duration_raw", read_bin(dir + "/duration_raw.bin"), dbg.duration_raw); } if (!flow && mismatch == 0) { compare("prosody_hidden", read_bin(dir + "/prosody_hidden.bin"), dbg.prosody_hidden); compare("prosody_raw", read_bin(dir + "/prosody_raw.bin"), dbg.prosody_raw); compare("prosody", read_bin(dir + "/prosody.bin"), dbg.prosody); compare("acoustic", read_bin(dir + "/acoustic.bin"), dbg.acoustic); compare("harmonic_head", read_bin(dir + "/harmonic_head.bin"), dbg.harmonic_head); compare("noise_head", read_bin(dir + "/noise_head.bin"), dbg.noise_head); compare("f0", read_bin(dir + "/f0.bin"), dbg.f0); compare("wave", read_bin(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; }