本文档记录 openai/whisper-tiny 在昇腾 NPU 上的适配、部署与验证结果。Whisper-tiny 是 OpenAI 发布的轻量级多语言语音识别模型,约 39M 参数,支持 99 种语言的语音转文本任务。
vLLM-Ascend 官方支持列表中标注 Whisper 暂不支持(Issue #2262),但本验证证明通过 transformers 库直接加载模型权重,配合 torch_npu 的 transfer_to_npu 自动迁移,可在昇腾 NPU 上零改动完成推理。
相关获取地址:
deploy-agent/whisper-tiny-npu/参考文档:
| 组件 | 版本 |
|---|---|
transformers | 4.57.6 |
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
CANN | 8.5.1 |
1 逻辑卡(Ascend910B4)from_pretrained("openai/whisper-tiny")transfer_to_npu 自动迁移source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
export HF_ENDPOINT=https://hf-mirror.comcd /path/to/whisper-tiny-npu
python3 scripts/whisper_tiny_npu_infer.py脚本核心逻辑:
import torch
import torch_npu
from torch_npu.contrib import transfer_to_npu
from transformers import WhisperForConditionalGeneration, WhisperProcessor
processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
model.eval()
model = model.to("npu")
inputs = processor(audio_array, sampling_rate=16000, return_tensors="pt")
input_features = inputs.input_features.to("npu")
with torch.no_grad():
generated_ids = model.generate(input_features)
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]python3 scripts/whisper_tiny_npu_precision.py该脚本对比 CPU 与 NPU 的前向传播输出(logits 和 probability 分布)以及 greedy decode token 一致性。
python3 scripts/whisper_tiny_npu_benchmark.py支持 FP32 / FP16 / BF16 三种精度及不同 batch_size 的 throughput 和 latency 测试。
运行基础推理脚本:
python3 scripts/whisper_tiny_npu_infer.py验证结果:
torch_npu 自动迁移生效,cuda API 透明映射到 npu实测结果(Ascend910B4, CANN 8.5.1, torch 2.9.0):
| 精度 | Batch | Latency(ms) | Throughput | 内存(MB) |
|---|---|---|---|---|
| FP32 | 1 | 105.37 | 9.49 | 145.4 |
| FP32 | 4 | 108.63 | 36.82 | 148.2 |
| FP16 | 1 | 107.35 | 9.32 | 74.0 |
| FP16 | 4 | 109.50 | 36.53 | 75.4 |
| BF16 | 1 | 103.57 | 9.65 | 73.1 |
| BF16 | 4 | 108.73 | 36.79 | 74.5 |
使用 whisper_tiny_npu_precision.py 对 CPU 与 NPU 做了前向传播精度对比。
| 指标 | 数值 | 说明 |
|---|---|---|
| Logits cosine similarity | 0.99996583 | 向量方向几乎一致 |
| Logits max relative error | 199.6583% | 峰值处相对误差( logits 未归一化,小值相对误差被放大,仅作参考) |
| Logits mean abs error | 0.017933 | 平均绝对误差 |
| Logits RMSE | 0.018299 | 均方根误差 |
| Probability cosine similarity | 1.00000000 | softmax 后概率分布几乎完全重合 |
| Probability max absolute error | 0.0560% | 概率最大偏差 |
| Probability KL divergence | 0.00000311 | 分布差异可忽略 |
| Greedy token match | True | Token 输出完全一致 |
| Top-5 token match rate | 100.00% | Top-5 Token 完全一致 |
结论:CPU 与 NPU 的数值输出高度一致。关键指标(概率分布和最终 token)误差均 < 1%,精度验证通过。
transfer_to_npu 会自动禁用 torch.jit.script,若模型依赖 TorchScript 需手动适配。datasets 加载真实测试音频,若未安装则降级为合成正弦波音频。language='en'。