panhg/fish-speech-1.5
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

Fish Speech V1.5 - 昇腾NPU适配

Fish Speech V1.5 是一款领先的多语言文本转语音(TTS)模型,其训练基于超过100万小时的音频数据。本仓库包含经适配并验证可在华为昇腾NPU上进行推理的模型。

模型概述

属性值
模型Fish Speech V1.5
架构DualAR Transformer(GPT风格 + 快速码本解码器)
参数规模637.9M
精度BF16
基础模型fishaudio/fish-speech-1.5
支持语言zh, en, ja, de, fr, es, ko, ar, nl, ru, it, pl, pt
声码器Firefly-GAN VQ-FSQ-8x1024-21Hz
许可证CC-BY-NC-SA-4.0

模型架构

Fish Speech V1.5模型采用双自回归(DualAR) 架构:

  • 慢速Transformer:24层,维度=1024,16个注意力头(GQA包含2个局部头),中间层维度=4096
  • 快速Transformer:4层,维度=1024,16个注意力头(包含2个局部头),中间层维度=4096
  • 码本:8个码本 × 每个码本1024个码
  • 词汇表:102,048个 tokens
  • 最大序列长度:8192
  • 位置编码:旋转位置嵌入(RoPE)

推理流程:文本 → 分词器 → DualAR Transformer → 矢量量化码(VQ Codes)→ Firefly-GAN声码器 → 音频

硬件要求

组件规格
NPU昇腾910B2 / 910B / 910A
CANN8.5.1或更高版本
驱动昇腾HDK 24.1+
torch_npu2.9.0+
PyTorch2.9.0+
内存≥ 8 GB NPU内存
存储≥ 3 GB可用磁盘空间

已验证环境

NPU:                  Ascend 910B2 (9362) x2
CANN:                 8.5.1
PyTorch:              2.9.0
torch_npu:            2.9.0.post1
Python:               3.11
OS:                   Linux (aarch64)

快速入门

1. 环境搭建

# Install dependencies
pip install torch torch_npu
pip install modelscope loguru tiktoken soundfile numpy
pip install dac  # For vocoder support

# Set environment variables
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True

2. 下载模型

# Download the model
pip install modelscope
modelscope download --model fishaudio/fish-speech-1.5 --local_dir ./fish-speech-1.5

# Clone the fish-speech repo for tokenizer support
git clone --depth 1 https://github.com/fishaudio/fish-speech.git /tmp/fish-speech

3. 运行推理

# Basic TTS inference
python inference.py \
    --model-path ./fish-speech-1.5 \
    --text "Hello world, this is a test of Fish Speech on Ascend NPU." \
    --output output.wav

# Chinese text
python inference.py \
    --model-path ./fish-speech-1.5 \
    --text "你好,世界!这是Fish Speech在昇腾NPU上的测试。" \
    --output output_cn.wav \
    --temperature 0.7 \
    --top-p 0.9

# With custom sampling parameters
python inference.py \
    --model-path ./fish-speech-1.5 \
    --text "Your text here." \
    --output output.wav \
    --temperature 0.8 \
    --top-p 0.8 \
    --top-k 40 \
    --max-new-tokens 256

4. 精度评估

python evaluation/eval_precision.py \
    --model-path ./fish-speech-1.5 \
    --output-dir ./evaluation

5. 性能基准测试

python evaluation/eval_performance.py \
    --model-path ./fish-speech-1.5 \
    --num-decode-tokens 100 \
    --decode-runs 5 \
    --output-dir ./evaluation

推理参数

参数类型默认值描述
--model-pathstr./fish-speech-1.5模型目录路径
--textstr必填TTS输入文本
--outputstroutput.wav输出音频文件路径
--max-new-tokensint512最大生成token数量
--temperaturefloat0.7采样温度(0-2)
--top-pfloat0.7Top-p(核)采样
--top-kint30Top-k采样
--device-idint0NPU设备ID
--skip-vocoder标志False跳过音频生成
--codes-outputstrNone将VQ码保存为.npy文件

NPU优化

以下优化适用于昇腾NPU推理:

内存管理

  • PYTORCH_NPU_ALLOC_CONF=expandable_segments:True:启用动态内存段扩展,减少内存碎片
  • KV缓存预分配并采用适当对齐方式(8的倍数)

注意力优化

  • 当FlashAttention不可用时,使用NPU的备用数学注意力机制
  • 采用2个本地头的GQA(分组查询注意力),实现高效KV缓存

执行

  • 使用Torch推理模式(@torch.inference_mode())进行算子融合
  • 采用BF16精度以降低内存带宽
  • 使用torch.npu.synchronize()确保计时准确

精度验证

测试项目结果阈值
权重加载(BF16与FP32对比)< 1% 相对误差1%
CPU与NPU编码重叠度~95%+-
输出编码范围[0, 1023]有效码本范围

精度状态:✅ 通过(误差 < 1%)

性能指标

性能在昇腾910B2上以BF16精度测量:

指标数值
模型加载时间~8秒
预填充延迟(提示=50个token)~200毫秒
解码吞吐量~30-50 token/秒
NPU内存(峰值)~4-6 GB
首token延迟~300毫秒

注:性能因提示长度、生成序列长度和采样参数而异。

文件结构

fish-speech-1.5/
├── config.json                    # Model configuration
├── model.pth                      # DualAR Transformer checkpoint
├── firefly-gan-vq-fsq-8x1024-21hz-generator.pth  # Vocoder checkpoint
├── tokenizer.tiktoken             # Tokenizer
├── special_tokens.json            # Special tokens
├── inference.py                   # NPU inference script
├── README.md                      # This document
└── evaluation/
    ├── eval_precision.py          # Precision evaluation script
    ├── eval_performance.py        # Performance benchmark script
    ├── precision_results.json     # Precision test results
    └── performance_results.json   # Performance benchmark results

局限性

  1. 声码器依赖:Firefly-GAN 声码器需要 dac 包。若该包不可用,脚本将保存原始 VQ 码。
  2. 批量推理:当前实现支持 batch_size=1。多批量推理需额外进行内存优化。
  3. 长文本:超过模型上下文窗口(8192 个 tokens)的文本将被截断。
  4. 无流式处理:当前推理为非流式(先生成所有 tokens,再进行解码)。

引用

@misc{fish-speech-v1.4,
      title={Fish-Speech: Leveraging Large Language Models for Advanced Multilingual Text-to-Speech Synthesis},
      author={Shijia Liao and Yuxuan Wang and Tianyu Li and Yifan Cheng and Ruoyi Zhang and Rongzhi Zhou and Yijin Xing},
      year={2024},
      eprint={2411.01156},
      archivePrefix={arXiv},
      primaryClass={cs.SD},
      url={https://arxiv.org/abs/2411.01156},
}

许可协议

本模型采用 CC-BY-NC-SA-4.0 许可协议。详情请参见原始 Fish Speech 仓库。