Upload sdk/src/kantts.cpp with huggingface_hub
Browse files- sdk/src/kantts.cpp +26 -65
sdk/src/kantts.cpp
CHANGED
|
@@ -635,64 +635,42 @@ std::vector<float> KanttsPipeline::SynthesizeSymbols(
|
|
| 635 |
const std::vector<std::string>& symbols) {
|
| 636 |
std::vector<float> audio;
|
| 637 |
for (const auto& sym : symbols) {
|
| 638 |
-
// 标点位置({#N...} 停顿标记),切分后的段 duration 对标点预测可能为 0,需强制停顿
|
| 639 |
-
std::vector<int> punct_idx;
|
| 640 |
-
{
|
| 641 |
-
std::stringstream ss(sym);
|
| 642 |
-
std::string t;
|
| 643 |
-
int idx = 0;
|
| 644 |
-
while (ss >> t) {
|
| 645 |
-
if (t.size() >= 2 && t[0] == '{' && t[1] == '#') punct_idx.push_back(idx);
|
| 646 |
-
++idx;
|
| 647 |
-
}
|
| 648 |
-
}
|
| 649 |
auto in = frontend_->Encode(sym);
|
| 650 |
auto t_stage = std::chrono::steady_clock::now();
|
| 651 |
std::fprintf(stderr, "[stage] encoded T=%d\n", in.T);
|
| 652 |
int T = in.T;
|
| 653 |
-
// 长句
|
| 654 |
-
//
|
| 655 |
-
|
|
|
|
|
|
|
| 656 |
if (use_cpu) std::fprintf(stderr, "[stage] 长句 T=%d -> CPU 管线\n", T);
|
| 657 |
-
constexpr int MT = 128,
|
| 658 |
-
const float* sy_w = w_.Get("sy_emb").data(); // (147,512)
|
| 659 |
-
const float* tone_w = w_.Get("tone_emb").data(); // (10,512)
|
| 660 |
-
const float* syll_w = w_.Get("syll_emb").data(); // (8,512)
|
| 661 |
-
const float* ws_w = w_.Get("ws_emb").data(); // (8,512)
|
| 662 |
-
const float* spk_w = w_.Get("spk_emb").data(); // (9,32)
|
| 663 |
-
const float* emo_w = w_.Get("emo_emb").data(); // (36,32)
|
| 664 |
-
const float* pos = w_.Get("pos_enc").data(); // (128,512)
|
| 665 |
-
std::vector<float> x_emb(MT * D, 0.0f), attn_mask(MT, 0.0f), mask_f(MT, 0.0f);
|
| 666 |
-
for (int t = 0; t < MT; ++t) {
|
| 667 |
-
bool valid = t < T;
|
| 668 |
-
mask_f[t] = valid ? 1.0f : 0.0f;
|
| 669 |
-
attn_mask[t] = valid ? 0.0f : -3e4f;
|
| 670 |
-
if (!valid) continue;
|
| 671 |
-
const int* l = &in.ling[t * 4];
|
| 672 |
-
for (int c = 0; c < D; ++c) {
|
| 673 |
-
float v = sy_w[l[0] * D + c] + tone_w[l[1] * D + c]
|
| 674 |
-
+ syll_w[l[2] * D + c] + ws_w[l[3] * D + c];
|
| 675 |
-
x_emb[t * D + c] = v * std::sqrt(128.0f) + pos[t * D + c];
|
| 676 |
-
}
|
| 677 |
-
}
|
| 678 |
std::vector<float> text_hid(MT * U), spk_hid(MT * U), emo_hid(MT * U);
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
enc_->Run();
|
| 690 |
-
|
| 691 |
-
enc_->GetOutput("
|
| 692 |
-
|
| 693 |
text_hid.resize(T * U);
|
| 694 |
spk_hid.resize(T * U);
|
| 695 |
emo_hid.resize(T * U);
|
|
|
|
| 696 |
std::fprintf(stderr, "[timing] enc %.0fms\n", std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now()-t_stage).count());
|
| 697 |
t_stage = std::chrono::steady_clock::now();
|
| 698 |
if (std::getenv("KANTTS_DUMP_ENC")) {
|
|
@@ -785,23 +763,6 @@ std::vector<float> KanttsPipeline::SynthesizeSymbols(
|
|
| 785 |
reps[t] = (int)(durations[t] + 0.5f);
|
| 786 |
sum += reps[t];
|
| 787 |
}
|
| 788 |
-
// 标点强制停顿:#1/#3 顿/逗号 ≈0.12s(10 帧),#4 句号 ≈0.2s(16 帧)
|
| 789 |
-
for (int pi : punct_idx) {
|
| 790 |
-
if (use_cpu) continue;
|
| 791 |
-
if (pi >= T) continue;
|
| 792 |
-
int min_reps = 10;
|
| 793 |
-
if (pi + 1 < T) {
|
| 794 |
-
// 看该标点符号内容:#4 句号停顿更长
|
| 795 |
-
std::stringstream ss(sym);
|
| 796 |
-
std::string t;
|
| 797 |
-
for (int k = 0; k <= pi; ++k) ss >> t;
|
| 798 |
-
if (t.size() >= 3 && t[1] == '#' && t[2] == '4') min_reps = 16;
|
| 799 |
-
}
|
| 800 |
-
if (reps[pi] < min_reps) {
|
| 801 |
-
sum += min_reps - reps[pi];
|
| 802 |
-
reps[pi] = min_reps;
|
| 803 |
-
}
|
| 804 |
-
}
|
| 805 |
std::fprintf(stderr, "[stage] speed=%.2f reps_sum=%d\n", speed, sum);
|
| 806 |
if (std::getenv("KANTTS_DUMP_ENC")) {
|
| 807 |
std::fprintf(stderr, "[dbg-npu] log_dur all:");
|
|
|
|
| 635 |
const std::vector<std::string>& symbols) {
|
| 636 |
std::vector<float> audio;
|
| 637 |
for (const auto& sym : symbols) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
auto in = frontend_->Encode(sym);
|
| 639 |
auto t_stage = std::chrono::steady_clock::now();
|
| 640 |
std::fprintf(stderr, "[stage] encoded T=%d\n", in.T);
|
| 641 |
int T = in.T;
|
| 642 |
+
// 默认:main.cpp 已把长句按标点切成 ≤22 子段,全走 NPU(除 PNCA 解码)。
|
| 643 |
+
// KANTTS_CPU_LONG=1 时整句不切分,回退 CPU 管线(对照用)。
|
| 644 |
+
// 诊断开关:KANTTS_CPU_ALL=1 时任意长度都走 CPU 韵律/时长/postnet(对照用)
|
| 645 |
+
const bool use_cpu = (T > 22 && std::getenv("KANTTS_CPU_LONG") != nullptr) ||
|
| 646 |
+
std::getenv("KANTTS_CPU_ALL") != nullptr;
|
| 647 |
if (use_cpu) std::fprintf(stderr, "[stage] 长句 T=%d -> CPU 管线\n", T);
|
| 648 |
+
constexpr int MT = 128, U = 32;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
std::vector<float> text_hid(MT * U), spk_hid(MT * U), emo_hid(MT * U);
|
| 650 |
+
enc_->SetInput("inputs_ling", in.ling.data(), in.ling.size() * 4);
|
| 651 |
+
enc_->SetInput("inputs_emo", in.emo.data(), in.emo.size() * 4);
|
| 652 |
+
enc_->SetInput("inputs_spk", in.spk.data(), in.spk.size() * 4);
|
| 653 |
+
enc_->SetInput("inputs_len", in.len.data(), in.len.size() * 4);
|
| 654 |
+
if (const char* le = std::getenv("KANTTS_LOAD_ENC")) {
|
| 655 |
+
// 诊断开关:从文件加载浮点 enc 输出(全 ONNX 对照用)
|
| 656 |
+
auto loadf = [&](const char* name, std::vector<float>& v) {
|
| 657 |
+
std::ifstream f(std::string(le) + "/" + name, std::ios::binary);
|
| 658 |
+
std::vector<char> b((std::istreambuf_iterator<char>(f)), {});
|
| 659 |
+
v.resize(b.size() / 4);
|
| 660 |
+
std::memcpy(v.data(), b.data(), b.size());
|
| 661 |
+
};
|
| 662 |
+
loadf("text.bin", text_hid);
|
| 663 |
+
loadf("spk.bin", spk_hid);
|
| 664 |
+
loadf("emo.bin", emo_hid);
|
| 665 |
+
} else {
|
| 666 |
enc_->Run();
|
| 667 |
+
enc_->GetOutput("text_hid", text_hid.data(), text_hid.size() * 4);
|
| 668 |
+
enc_->GetOutput("spk_hid", spk_hid.data(), spk_hid.size() * 4);
|
| 669 |
+
enc_->GetOutput("emo_hid", emo_hid.data(), emo_hid.size() * 4);
|
| 670 |
text_hid.resize(T * U);
|
| 671 |
spk_hid.resize(T * U);
|
| 672 |
emo_hid.resize(T * U);
|
| 673 |
+
}
|
| 674 |
std::fprintf(stderr, "[timing] enc %.0fms\n", std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now()-t_stage).count());
|
| 675 |
t_stage = std::chrono::steady_clock::now();
|
| 676 |
if (std::getenv("KANTTS_DUMP_ENC")) {
|
|
|
|
| 763 |
reps[t] = (int)(durations[t] + 0.5f);
|
| 764 |
sum += reps[t];
|
| 765 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
std::fprintf(stderr, "[stage] speed=%.2f reps_sum=%d\n", speed, sum);
|
| 767 |
if (std::getenv("KANTTS_DUMP_ENC")) {
|
| 768 |
std::fprintf(stderr, "[dbg-npu] log_dur all:");
|