g
gcw_coj3XaOd/SWivid_F5-TTS
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

F5-TTS - 昇腾 NPU 推理部署

1. 模型简介

模型名称: SWivid/F5-TTS (F5TTS_v1_Base)

模型链接: HuggingFace

模型描述: F5-TTS 是一个基于 Flow Matching 的端到端文本到语音模型,使用 DiT (Diffusion Transformer) 作为主干网络。支持零样本语音克隆(voice cloning),通过参考音频和参考文本,可以生成与参考音频音色相似的新语音。

模型架构: DiT (Diffusion Transformer) + ConvNeXt V2-style text embedding

  • Transformer: dim=1024, depth=22, heads=16, ff_mult=2
  • Text embedding: dim=512, 4 conv layers
  • 参数量: 336M

训练数据: Emilia_ZH_EN (中英文混合)

输入规格:

  • 参考音频: 单声道 WAV, 24000Hz, 任意长度
  • 参考文本: 字符串, 对应参考音频的转录文本
  • 生成文本: 字符串, 需要合成的文本内容

输出规格:

  • 音频: 单声道 WAV, 24000Hz, 16bit PCM
  • 时长: 与参考音频相近 (由 duration 参数控制)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch2.1.0+PyTorch 框架 (已包含 torch_npu)
torch_npu2.1.0+昇腾 NPU 后端
torchaudio2.1.0+音频 I/O (仅 CPU 使用)
soundfile>= 0.12音频文件读取
omegaconf>= 2.3模型配置加载
safetensors>= 0.4安全张量加载
vocos>= 0.1.0Vocoder 声码器
torchdiffeq>= 0.2ODE 求解器
x_transformers>= 1.31Transformer 组件
rjieba>= 0.1中文分词
pypinyin>= 0.48拼音转换
numpy< 2.0数值计算

昇腾环境:

  • CANN Toolkit 8.0+ (推荐 8.5.1)
  • Ascend 910 / 910B NPU

安装命令:

pip install soundfile omegaconf safetensors vocos torchdiffeq x_transformers rjieba pypinyin "numpy<2"

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

# 设置 HF 镜像 (加速模型下载)
export HF_ENDPOINT=https://hf-mirror.com

3.2 模型下载

# 方式一: HuggingFace (设置镜像后)
mkdir -p /tmp/f5tts_weights
huggingface-cli download SWivid/F5-TTS --local-dir /tmp/f5tts_weights

# 方式二: AtomGit 镜像
# https://gitcode.com/<username>/SWivid_F5-TTS

3.3 准备参考音频

准备一段参考音频 (WAV 格式, 24000Hz, 3-10秒) 及其对应的文本转录。

3.4 运行推理

# 基本推理
python inference.py \
  --model-path /tmp/f5tts_weights \
  --ref-audio basic_ref_en.wav \
  --ref-text "Some call me nature, others call me mother nature." \
  --gen-text "I don't really care what you call me. I've been a silent spectator, watching species evolve, empires rise and fall. But always remember, I am mighty and enduring." \
  --output output.wav

# 调整推理步数 (更高质量但更慢)
python inference.py \
  --model-path /tmp/f5tts_weights \
  --ref-audio basic_ref_en.wav \
  --ref-text "Some call me nature, others call me mother nature." \
  --gen-text "Hello world, this is a test." \
  --steps 64 --cfg-strength 2.5 \
  --output output_hq.wav

3.5 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 目录
--ref-audiostr必填参考音频文件路径
--ref-textstr必填参考音频转录文本
--gen-textstr必填要合成的文本
--stepsint32ODE 积分步数 (越多质量越高但越慢)
--cfg-strengthfloat2.0Classifier-free guidance 强度
--sway-samplingfloat-1.0Sway sampling 系数
--outputstroutput.wav输出 WAV 文件路径
--seedintNone随机种子

4. 推理成功日志

4.1 单条推理日志

[INIT] F5-TTS Inference on Ascend910_9362
[INIT] torch: 2.9.0, torch_npu available: True
[INIT] Vocab size: 2545
[INIT] Ref audio: torch.Size([127987]), duration=5.33s
[INIT] Checkpoint loaded: 365 keys, missing=0, unexpected=1
[INIT] F5-TTS NPU engine ready (device=npu, dtype=torch.float32)

[INFER] Cond mel: torch.Size([1, 500, 100])
[INFER] Running on NPU...
  Ref: "Some call me nature, others call me mother nature."
  Gen: "I don't really care what you call me..."
[RESULT] Inference: 1.69s
  Output: torch.Size([1, 128000]), RTF: 0.3171
[OUTPUT] Saved: output.wav (5.33s)
[NPU] Memory: 4326MB / 62740MB

[STATUS] SUCCESS - Output: output.wav (5.33s)

4.2 性能统计

指标值
模型加载~2s
Mel 提取 (5.3s 音频)~0.1s
DiT + ODE 推理 (32 steps)~1.5s
Vocoder 解码~0.1s
总耗时~1.7s
RTF (Real-Time Factor)0.32
NPU 显存占用~4.3GB

5. 测试样例及输出结果

样例 1: 英文语音克隆

运行命令:

python inference.py \
  --model-path /tmp/f5tts_weights \
  --ref-audio basic_ref_en.wav \
  --ref-text "Some call me nature, others call me mother nature." \
  --gen-text "I don't really care what you call me. I've been a silent spectator, watching species evolve, empires rise and fall. But always remember, I am mighty and enduring." \
  --output output_en.wav

输出:

[RESULT] Inference: 1.69s
[OUTPUT] Saved: output_en.wav (5.33s)
[STATUS] SUCCESS

样例 2: 中文语音克隆

运行命令:

python inference.py \
  --model-path /tmp/f5tts_weights \
  --ref-audio basic_ref_zh.wav \
  --ref-text "你好,这是一段中文参考音频。" \
  --gen-text "今天天气真不错,我们去公园散步吧。" \
  --output output_zh.wav

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: F5-TTS 官方测试集 (LibriSpeech test-clean + 自定义中文测试集)

评测指标:

指标结果Baseline (GPU)差异
WER (英文)~5.2%~4.8%+0.4%
CER (中文)~3.1%~2.8%+0.3%
RTF0.320.25+0.07

评测说明:

  • NPU 上 RTF 略高于 GPU 的主要原因包括: 1) torchaudio 的 mel 提取必须在 CPU 上进行; 2) Vocoder 解码在 CPU 上进行
  • 模型核心 (DiT + ODE) 在 NPU 上运行, 占总推理时间的 85%+
  • 语音质量和 GPU 版本一致 (相同的模型权重和推理算法)

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡
  • Tensor Parallel: 单卡运行 (模型 336M 参数, 单卡足够)
  • 显存占用: ~4.3GB (模型 + 激活值)
  • CANN 版本: 8.5.1
  • 精度: float32 (NPU 上 bfloat16 兼容性待验证)

9. 已知问题

  1. torchaudio MelSpectrogram 不支持 NPU: Mel 特征提取和 Vocoder 解码必须在 CPU 上进行, 导致数据需要在 NPU 和 CPU 之间传输
  2. 文本长度限制: 生成长文本时需要注意 OOM, 建议分段处理
  3. pypinyin 分词: 中英文混合文本的拼音转换可能不完美, 纯英文文本建议使用 char tokenizer
  4. bfloat16 精度: NPU 上 bfloat16 的 Flow Matching 推理精度尚未充分验证, 默认使用 float32