faster-whisper-medium (Ascend NPU适配版)
基于 openai/whisper-medium 的华为昇腾 NPU 适配版本。支持 99 种语言的自动语音识别,已在 Ascend 910 NPU 上完成精度和性能验证。
模型信息
NPU适配结果
| 评测项 | 结果 | 说明 |
|---|
| 模型加载 | ✅ PASS | 加载时间 ~2.5s |
| 文本精度 | ✅ PASS | NPU与CPU输出完全一致 |
| Encoder精度 | ✅ PASS | 余弦相似度 0.9998,相对误差 1.66% (FP16) |
| 推理性能 | ✅ PASS | RTF 0.134 (7.5x 实时) |
| CPU加速比 | ✅ PASS | NPU vs CPU: 116.76x |
快速开始
环境要求
- Python 3.10+
- CANN 8.0+
- torch 2.6+
- torch_npu 2.6+
安装依赖
# 安装基础依赖
pip install torch torch_npu transformers
pip install librosa soundfile
# 下载模型(二选一)
# 方式1: 下载原始Whisper模型(推荐)
huggingface-cli download openai/whisper-medium --local-dir ./whisper_model
# 方式2: 从ModelScope下载CTranslate2版本(需要额外转换)
modelscope download --model pengzhendong/faster-whisper-medium
推理示例
import torch
import torch_npu
from transformers import WhisperForConditionalGeneration, WhisperProcessor
# 加载模型
model = WhisperForConditionalGeneration.from_pretrained(
"./whisper_model",
dtype=torch.float16,
).to("npu:0")
model.eval()
processor = WhisperProcessor.from_pretrained("./whisper_model")
# 加载音频
import librosa
audio, _ = librosa.load("audio.mp3", sr=16000, mono=True)
# 特征提取
inputs = processor.feature_extractor(
audio, sampling_rate=16000, return_tensors="pt"
).input_features.to("npu:0", dtype=torch.float16)
# 推理
with torch.no_grad():
predicted_ids = model.generate(inputs, max_length=448)
# 解码
text = processor.tokenizer.decode(predicted_ids[0], skip_special_tokens=True)
print(text)
命令行推理
# 自动语言检测(推荐)
python inference.py --audio sample.wav
# 指定语言
python inference.py --audio sample.wav --language en
# 翻译模式(语音→英文翻译)
python inference.py --audio sample.wav --task translate
# 保存结果
python inference.py --audio sample.wav --output result.txt
性能基准
Ascend 910 NPU (Atlas 800 A2)
| 指标 | 数值 |
|---|
| 测试音频 | 3.50s, 16kHz mono |
| 平均推理时间 | 0.47s |
| RTF (实时率) | 0.134 |
| 推理速度 | 7.5x 实时 |
| NPU vs CPU | 116.76x 加速 |
不同硬件对比
| 平台 | 推理时间 (3.5s音频) | 速度倍数 |
|---|
| Ascend 910 NPU (FP16) | 0.47s | 116.76x |
| CPU ARM AArch64 (FP32) | 54.85s | 1x |
精度验证
NPU (FP16) vs CPU (FP32)
| 指标 | 数值 |
|---|
| 文本匹配率 | 100% (完全一致) |
| Encoder余弦相似度 | 0.9998 |
| 平均绝对误差 | 0.0115 |
| 相对误差 | 1.66% |
相对误差 < 2% 符合 FP16 推理的预期精度范围。
目录结构
faster-whisper-medium-npu/
├── README.md # 本文档
├── inference.py # NPU推理脚本
├── benchmark.py # 精度/性能评测脚本
├── evaluation_report.md # 详细评测报告
└── whisper_model/ # 模型文件目录
├── config.json
├── generation_config.json
├── pytorch_model.bin
├── tokenizer.json
└── ...
文件说明
| 文件 | 功能 | 启动方式 |
|---|
inference.py | 推理脚本 | python inference.py --audio <音频文件> |
benchmark.py | 评测脚本 | python benchmark.py --audio <音频文件> |
evaluation_report.md | 评测报告 | 直接查看 |
whisper_model/ | 模型权重目录 | 存放模型文件 |
部署文档
模型权重获取
# 从 HuggingFace 下载(推荐)
pip install huggingface_hub
huggingface-cli download openai/whisper-medium --local-dir ./whisper_model
# 从 ModelScope 下载 CTranslate2 版本
pip install modelscope
modelscope download --model pengzhendong/faster-whisper-medium
环境验证
# 验证 NPU 可用性
python -c "import torch; import torch_npu; print(torch.npu.is_available())"
# 验证模型加载
python -c "
from transformers import WhisperForConditionalGeneration
model = WhisperForConditionalGeneration.from_pretrained('./whisper_model', dtype=torch.float16)
print(f'Model loaded: {sum(p.numel()) for p in model.parameters():,} params')
"
生产部署建议
- 使用自动语言检测模式(默认),避免指定语言导致的潜在生成退化问题
- 长音频分段处理:超过30秒的音频建议分段推理
- FP16精度:推荐使用默认FP16精度,平衡精度和性能
- 内存管理:单卡显存占用约3GB,支持并发推理
- 流式推理:可通过Whisper的chunking机制实现流式ASR
许可
MIT License - 同原始 openai/whisper-medium 模型
引用
@misc{faster-whisper-medium-npu,
title = {faster-whisper-medium Ascend NPU Adaptation},
author = {Model Agent},
year = {2026},
note = {Ascend NPU adapted version of openai/whisper-medium},
}