m0_74196153/whisper-tiny-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Whisper-tiny on Ascend NPU

1. 简介

本文档记录 openai/whisper-tiny 在昇腾 NPU 上的适配、部署与验证结果。Whisper-tiny 是 OpenAI 发布的轻量级多语言语音识别模型,约 39M 参数,支持 99 种语言的语音转文本任务。

vLLM-Ascend 官方支持列表中标注 Whisper 暂不支持(Issue #2262),但本验证证明通过 transformers 库直接加载模型权重,配合 torch_npu 的 transfer_to_npu 自动迁移,可在昇腾 NPU 上零改动完成推理。

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/openai/whisper-tiny
  • 权重下载地址(GitCode 镜像):https://gitcode.com/hf_mirrors/openai/whisper-tiny
  • 适配代码仓库:deploy-agent/whisper-tiny-npu/

参考文档:

  • https://huggingface.co/docs/transformers/model_doc/whisper
  • https://www.hiascend.com/document/detail/zh/Pytorch/730/ptmoddevg/trainingmigrguide/PT_LMTMOG_0070.html

2. 验证环境

组件版本
transformers4.57.6
torch2.9.0+cpu
torch-npu2.9.0.post1+gitee7ba04
CANN8.5.1
  • NPU:1 逻辑卡(Ascend910B4)
  • 模型加载方式:from_pretrained("openai/whisper-tiny")
  • 适配方式:transfer_to_npu 自动迁移

3. 推理脚本使用

3.1 环境准备

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.com

3.2 基础推理验证

cd /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]

3.3 精度验证

python3 scripts/whisper_tiny_npu_precision.py

该脚本对比 CPU 与 NPU 的前向传播输出(logits 和 probability 分布)以及 greedy decode token 一致性。

3.4 性能基准测试

python3 scripts/whisper_tiny_npu_benchmark.py

支持 FP32 / FP16 / BF16 三种精度及不同 batch_size 的 throughput 和 latency 测试。

4. Smoke 验证

运行基础推理脚本:

python3 scripts/whisper_tiny_npu_infer.py

验证结果:

  • 模型正常加载,无 NPU 算子报错
  • 输出非空识别文本
  • torch_npu 自动迁移生效,cuda API 透明映射到 npu

5. 性能参考

实测结果(Ascend910B4, CANN 8.5.1, torch 2.9.0):

精度BatchLatency(ms)Throughput内存(MB)
FP321105.379.49145.4
FP324108.6336.82148.2
FP161107.359.3274.0
FP164109.5036.5375.4
BF161103.579.6573.1
BF164108.7336.7974.5
  • 单条 5s 音频 FP32 推理约 105ms
  • tiny 模型约 39M 参数,FP32 峰值内存 < 150MB
  • FP16/BF16 内存较 FP32 降低约 50%

6. 精度评测

使用 whisper_tiny_npu_precision.py 对 CPU 与 NPU 做了前向传播精度对比。

指标数值说明
Logits cosine similarity0.99996583向量方向几乎一致
Logits max relative error199.6583%峰值处相对误差( logits 未归一化,小值相对误差被放大,仅作参考)
Logits mean abs error0.017933平均绝对误差
Logits RMSE0.018299均方根误差
Probability cosine similarity1.00000000softmax 后概率分布几乎完全重合
Probability max absolute error0.0560%概率最大偏差
Probability KL divergence0.00000311分布差异可忽略
Greedy token matchTrueToken 输出完全一致
Top-5 token match rate100.00%Top-5 Token 完全一致

结论:CPU 与 NPU 的数值输出高度一致。关键指标(概率分布和最终 token)误差均 < 1%,精度验证通过。

7. 注意事项

  1. 自动迁移限制:transfer_to_npu 会自动禁用 torch.jit.script,若模型依赖 TorchScript 需手动适配。
  2. 音频输入:脚本优先尝试从 datasets 加载真实测试音频,若未安装则降级为合成正弦波音频。
  3. 解码策略:Whisper 是多语言模型,默认进行语言检测后转录。如需固定翻译到英文,需显式设置 language='en'。
  4. 内存占用:tiny 模型极轻量,单卡可轻松部署,batch_size=4 时内存仍 < 150MB。
  5. 算子兼容性:Whisper 的编码器(CNN + Transformer)和解码器(Transformer)均为标准算子,NPU 原生支持,无需自定义算子替换。