ⓍTTS 是一种语音生成模型,仅需 6 秒的音频片段,就能将声音克隆成不同语言。本仓库提供 XTTS-v2 的昇腾 NPU 适配版本,可在华为昇腾 910 系列 NPU 上实现快速推理。
该模型已在昇腾 NPU 上验证运行,主要适配如下:
| 组件 | 适配情况 |
|---|---|
| 主 GPT 模型 | 原生在 NPU 上运行 |
| HiFi-GAN 解码器 | 原生在 NPU 上运行 |
| 说话人编码器 | 在 CPU 上运行(NPU 缺乏对 STFT 的 complex64 类型 abs() 支持) |
| 音频加载 | 使用 soundfile 后端(避免使用 torchcodec/libnvrtc) |
weights_only=True)soundfile 而非 torchcodec(昇腾平台不支持 libnvrtc.so)torch.stft 生成的 complex64 张量不支持 abs() 操作的问题在华为昇腾 910B NPU 上的评估结果:
| 语言 | 文本长度 | 音频时长 | 推理时间 | RTF |
|---|---|---|---|---|
| 英语(短文本) | 74 字符 | 5.84s | 2.91s | 0.50x |
| 英语(中等文本) | 97 字符 | 4.68s | 2.55s | 0.55x |
| 中文(zh-cn) | 20 字符 | 4.54s | 2.31s | 0.51x |
| 法语 | 68 字符 | 4.64s | 2.41s | 0.52x |
| 德语 | 75 字符 | 5.24s | 2.60s | 0.50x |
RTF < 1.0 表示模型生成音频的速度快于实时(例如,0.5x 意味着生成速度是播放速度的 2 倍)。
在确定性模型组件上对NPU适配精度进行了验证:
| 组件 | 指标 | 数值 | 状态 |
|---|---|---|---|
| 说话人嵌入 | MAE | 7.48e-05 | ✅ |
| 说话人嵌入 | 余弦相似度 | 0.999998 | ✅ |
精度:说话人嵌入误差 < 0.01%,证实NPU与CPU基准的数值一致性。
注:由于GPT采样固有的随机性(基于温度的生成每次运行都会产生不同的音频),对该生成模型进行完整音频输出比较没有意义。
# Install PyTorch NPU
pip install torch torch_npu torchaudio
# Install TTS dependencies
pip install TTS soundfile scipy
# Clone this repository
git clone https://atomgit.com/weixin_62994174/XTTS-v2.git
cd XTTS-v2# English TTS with default speaker
python inference.py --text "Hello world, this is a test." --language en
# Chinese TTS
python inference.py --text "你好,这是语音合成测试。" --language zh-cn
# Custom speaker voice cloning
python inference.py --text "Your text here" \
--speaker_wav /path/to/speaker_6sec.wav \
--language en \
--output output.wavfrom inference import load_model, synthesize
model, config = load_model(device="npu")
wav, elapsed, duration, rtf = synthesize(
model, config,
text="Hello, this is Ascend NPU text-to-speech!",
speaker_wav="models/samples/en_sample.wav",
language="en",
output_path="output.wav",
)
print(f"RTF: {rtf:.2f}x")XTTS-v2/
├── inference.py # Main NPU inference script
├── evaluation/
│ ├── evaluate.py # Performance & accuracy evaluation
│ └── accuracy_deterministic.py # Deterministic component validation
├── models/ # Model weights (from ModelScope)
│ ├── model.pth # Main XTTS-v2 checkpoint
│ ├── dvae.pth # Discrete VAE checkpoint
│ ├── config.json # Model configuration
│ ├── vocab.json # Tokenizer vocabulary
│ └── samples/ # Reference speaker samples
├── output/ # Generated audio outputs
├── smoke_test.py # Quick NPU verification test
└── README.md # This file运行完整的评估套件:
# Performance benchmark only
python evaluation/evaluate.py --perf-only
# Deterministic accuracy validation
python evaluation/accuracy_deterministic.pytorch.stft 操作会生成复数张量;Ascend NPU 不支持对 complex64 数据类型执行 abs() 操作。作为临时解决方案,说话人编码器在 CPU 上运行。soundfile 作为备用方案。本模型根据 Coqui 公共模型许可证 (CPML) 进行许可。
@misc{xtts-v2,
author = {Coqui AI},
title = {XTTS-v2: Multilingual Voice Cloning},
year = {2024},
publisher = {ModelScope},
url = {https://www.modelscope.cn/models/AI-ModelScope/XTTS-v2}
}