w
weasonlee/chatterbox
模型介绍
文件和版本
Pull Requests
讨论
分析

Chatterbox TTS — Ascend NPU Adaptation

模型简介

Chatterbox 是 ResembleAI 开源的 0.5B 参数级 TTS(文本转语音)模型,支持零样本语音克隆、情感夸张控制与 CFG 引导。模型由多组件构成:

  • T3(Token-To-Token transformer,532.4M 参数):文本 token → 语音 token 的自回归骨干
  • S3Gen:语音 token → 波形生成器(含 HiFi-GAN 声码器路径)
  • Voice Encoder:说话人嵌入编码器
  • EnTokenizer:英文 tokenizer(tokenizer.json),内置 conds.pt 预置参考音色条件

原始模型:ResembleAI/chatterbox(MIT) 官方代码:resemble-ai/chatterbox(pip install chatterbox-tts,本仓库验证 0.1.7 可用)

适配方案(确定性对比路径):完整 TTS 生成链路(T3 多步采样 + S3Gen 合成)含随机采样与水印,跨端复现不稳定。本仓库采用 T3 teacher-forcing 前向:固定英文文本经 tokenizer 编码(含 BOT/EOT),拼接固定种子(42)的合成语音 token 序列(128 个),一次前向得到 text/speech logits,双端对比 cos_sim 与 argmax 一致率——完整验证加载路径与 transformer 计算图,输入完全一致、结果可复现。

依赖注意:chatterbox-tts 0.1.7 需补装 perth / s3tokenizer / conformer;perth ≥1.0 将水印类改名(PerthImplicitWatermarker → Perth),inference.py 内置兼容别名(水印仅 generate() 路径使用,不影响本对比路径)。

环境要求

  • 昇腾 Ascend 910(本仓库在 910 ×2 上验证,单卡即可推理)
  • Python 3.11
  • torch 2.9.0 + torch-npu 2.9.0.post1
  • chatterbox-tts 0.1.7 + perth/s3tokenizer/conformer

部署步骤

# 1. 克隆仓库
git clone https://gitcode.com/weasonlee/chatterbox.git
cd chatterbox

# 2. 安装依赖
pip install -r requirements.txt

# 3. 下载权重到 models/(已 .gitignore 排除;全量约 12GB,
#    核心文件 ve/t3_cfg/s3gen.safetensors + tokenizer.json + conds.pt)
mkdir -p models
HF_ENDPOINT=https://hf-mirror.com HF_HUB_DOWNLOAD_TIMEOUT=120 python -c "
from huggingface_hub import snapshot_download
snapshot_download('ResembleAI/chatterbox', local_dir='models',
                  allow_patterns=['ve.safetensors','t3_cfg.safetensors',
                                  's3gen.safetensors','tokenizer.json','conds.pt'])"

# 4. 推理(NPU,T3 teacher-forcing 对比路径)
python inference.py --device npu
# 或 CPU
python inference.py --device cpu

推理示例

$ python inference.py --device npu
torch.npu.is_available() = True
torch.npu.device_count() = 2
[info] device = npu:0, torch = 2.9.0+cpu
[load] T3 transformer: 532.4M params (+ S3Gen/voice-encoder components)
[info] text = 'Ascend NPU brings this voice model to life.'
[info] text_tokens = (1, 31), speech_tokens = (1, 128) (synthetic, seed=42)
[time] run 0: 31 ms
[time] avg: 30 ms (T3 teacher-forcing pass)
[out ] speech_logits: shape=(1, 128, 8194) mean=-2.2974 std=3.2404
[out ] text_logits: shape=(1, 31, 704) mean=-7.3950 std=5.2702
[out ] speech argmax sample: [3677, 5020, 1724, 6486, 737, 3413, 6377, 1735]

===== CPU vs NPU comparison (T3 teacher-forcing logits) =====
  speech_logits  cos_sim=0.99999998 max_abs_err=3.922e-03 mean_abs_err=5.157e-04 argmax_agree=99.22%
  text_logits    cos_sim=0.99999998 max_abs_err=7.683e-03 mean_abs_err=1.303e-03 argmax_agree=100.00%

输入:固定英文文本("Ascend NPU brings this voice model to life.")+ 固定种子(42)合成语音 token(128 个,词表 6561)+ 内置 conds.pt 参考条件;CPU 与 NPU 输入完全一致。

CPU vs NPU 精度对比

输出cos_simmax_abs_errmean_abs_errargmax 一致率
speech_logits(128×8194)0.999999983.922e-035.157e-0499.22%
text_logits(31×704)0.999999987.683e-031.303e-03100.00%
单次前向耗时(含同步,3 次均值)NPU ~30 ms vs CPU ~3700 ms(~123× 加速)———

说明:speech_logits 的 argmax 一致率 99.22% 对应 128 个 token 中 1 个在 logits 差异 4e-3 量级下翻转(双端 logits 均为近并列值),cos_sim 仍达 0.99999998,计算图双端一致。fp32 正常差异水平,适配成功。

仓库结构

├── inference.py        # 推理脚本(T3 teacher-forcing + NPU/CPU 对比)
├── readme.md
├── requirements.txt
├── models/             # 权重目录(需自行下载,已 gitignore)
├── assets/             # 运行截图
│   ├── agent_workflow.png
│   ├── npu_device_call.png
│   └── model_result.png
└── results/            # 每次运行的 logits/耗时产物(gitignore)

引用

Resemble AI. "Chatterbox: Open-Source TTS with Emotion and Voice Cloning." 2025. https://github.com/resemble-ai/chatterbox