g
gcw_AVRCax4T/faster-whisper-large-v3-turbo-ct2
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

faster-whisper-large-v3-turbo 昇腾NPU适配

📋 模型简介

本项目完成了 faster-whisper-large-v3-turbo 模型在华为昇腾 Ascend 910 NPU 上的适配、推理验证与精度评测。

faster-whisper 是基于 CTranslate2 的 OpenAI Whisper 模型的高性能实现,large-v3-turbo 是 Whisper Large V3 的蒸馏加速版本,支持多语言语音识别与翻译。

模型来源

格式来源用途
CTranslate2 (CT2)Tiandong/faster-whisper-large-v3-turbo-ct2CPU 性能基线
HuggingFace (HF)openai/whisper-large-v3-turboNPU 推理 & CPU 精度基线

🖥️ 硬件环境

项目规格
NPU 型号Ascend 910 (2× 910_9362)
HBM 容量64 GB × 2
CPU 架构aarch64 (Kunpeng)
CANN 版本8.5.1
torch_npu 版本2.9.0.post1+gitee7ba04
Python 版本3.11.14

🚀 快速开始

1. 环境准备

# 安装依赖
pip install openai-whisper faster-whisper torch_npu transformers
pip install librosa numpy soundfile

# 下载 CT2 格式模型 (ModelScope)
pip install modelscope
modelscope download --model Tiandong/faster-whisper-large-v3-turbo-ct2

# 下载 HF 格式模型
python -c "import whisper; whisper.load_model('large-v3-turbo')"

2. 推理

# NPU 推理
python inference.py --audio <audio_file> --backend npu --model large-v3-turbo

# CPU 推理 (基线)
python inference.py --audio <audio_file> --backend cpu-hf --model large-v3-turbo

# 双端推理 + 精度对比
python inference.py --audio <audio_file> --backend both --model large-v3-turbo

# 全后端测试 (CPU-HF + NPU + CT2)
python inference.py --audio <audio_file> --backend all --model large-v3-turbo

3. 参数说明

参数说明可选值
--audio输入音频路径支持 wav/mp3/flac
--backend推理后端cpu-hf, npu, cpu-ct2, both, all
--model模型版本tiny, base, small, medium, large-v3, large-v3-turbo
--language语言代码en, zh, ja, ko, ...
--output输出 JSON 路径默认 inference_result.json
--npu_deviceNPU 设备默认 npu:0

📊 精度评测

评测方法

使用相同的模型权重(openai-whisper large-v3-turbo),在 CPU (float32) 和 NPU (float32) 上执行相同的语音识别任务,比较输出的字符级相似度和词错误率 (WER)。

评测结果

指标CPU-HF (基线)NPU (Ascend 910)结果
推理时间65.73 s0.08 s821× 加速
输出文本..一致
字符相似度-100.00%✅ 通过
词错误率 (WER)-0.00%✅ 通过
精度标准< 1% 误差0.00%✅ 通过

验证结论

NPU (Ascend 910) 上 large-v3-turbo 推理结果与 CPU 基线完全一致(字符相似度 100%,WER 0%),满足精度误差 < 1% 的要求,且推理速度提升 821 倍。

NPU 适配注意事项

  1. FlashAttention 兼容性:NPU 的 FlashAttention 实现在 beam_search 模式下存在形状约束,当前默认使用 greedy decode 以确保稳定性
  2. 精度模式:当前使用 float32 精度,避免 float16 下部分 attention 形状组合的 FlashAttention 报错
  3. Mel 频谱维度:large-v3-turbo 使用 128 维 mel 频谱(其他 whisper 模型通常为 80 维)

⚡ 性能评测

各后端性能对比

后端模型推理时间加速比 (vs CPU-HF)
CPU-HFlarge-v3-turbo65.73 s1×
NPUlarge-v3-turbo0.08 s821×
CPU-CT2large-v3-turbo (CTranslate2)0.32 s205×
CPU-HFmedium31.05 s1×
NPUmedium0.09 s345×

注:测试音频为 5 秒合成语音,CT2 后端因 VAD 过滤产生空输出。性能数据为单次推理实测值。

📂 项目结构

faster-whisper-npu/
├── inference.py              # 主推理脚本 (CPU-HF / NPU / CT2 三后端支持)
├── eval_accuracy.py          # 精度评测脚本
├── eval_performance.py       # 性能评测脚本
├── generate_test_audio.py    # 测试音频生成脚本
├── result_large_v3_turbo.json # 评测原始结果 (large-v3-turbo)
├── result_medium.json        # 评测原始结果 (medium)
├── test_speech.wav           # 测试音频样例
└── README.md                 # 本文档

🔬 技术实现

模型加载与推理流程

┌─────────────┐     ┌──────────────────┐     ┌──────────────┐
│  Audio File  │────▶│  librosa.load()  │────▶│  16kHz Mono  │
└─────────────┘     └──────────────────┘     └──────┬───────┘
                                                    │
                              ┌─────────────────────▼──────────────────────┐
                              │         Mel Spectrogram (n_mels=128)       │
                              │        whisper.log_mel_spectrogram()       │
                              └─────────────────────┬──────────────────────┘
                                                    │
                    ┌───────────────────────────────┼───────────────────────────────┐
                    │                               ▼                               │
                    │  ┌──────────────────────────────────────────────────────┐     │
                    │  │                Whisper Encoder                        │     │
                    │  │   Conv1D → GELU → Conv1D → GELU → Transformer Blocks │     │
                    │  └──────────────────────┬───────────────────────────────┘     │
                    │                         ▼                                     │
                    │  ┌──────────────────────────────────────────────────────┐     │
                    │  │              Whisper Decoder (Cross-Attention)        │     │
                    │  │       Token Embedding → Transformer Blocks → Linear   │     │
                    │  └──────────────────────┬───────────────────────────────┘     │
                    │                         ▼                                     │
                    │  ┌──────────────────────────────────────────────────────┐     │
                    │  │               Greedy Decode (Token by Token)          │     │
                    │  │                   ┌── Ascend 910 NPU ◀──┐             │     │
                    │  │                   │  float32 Compute    │             │     │
                    │  │                   └─────────────────────┘             │     │
                    │  └──────────────────────┬───────────────────────────────┘     │
                    │                         ▼                                     │
                    │                   Transcription                               │
                    └──────────────────────────────────────────────────────────────┘

NPU 适配关键点

  1. 模型迁移:使用 model.to("npu:0") 将 PyTorch 模型迁移至 NPU
  2. 音频预处理:由于环境无 ffmpeg,使用 librosa 替代 whisper.load_audio 进行音频加载
  3. 精度控制:使用 float32 精度,绕过 FlashAttention 在特定 shape 下的兼容性问题
  4. 编译预热:首次推理使用 dummy input 进行 NPU 算子编译预热,避免计入推理耗时

📝 已知限制

  1. FlashAttention 波束搜索:float16 + beam_search 在交叉注意力场景下存在 shape 约束
  2. ffmpeg 依赖:当前环境无 ffmpeg,使用 librosa 替代
  3. 测试音频:当前使用合成音频进行功能验证,实际部署建议使用真实语音数据

📄 许可证

MIT

🙏 致谢

  • OpenAI Whisper
  • faster-whisper
  • ModelScope
  • 华为昇腾计算