Yanguan/whisper-tiny-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

Whisper-Tiny on Ascend NPU

1. 简介

本文档记录 openai/whisper-tiny 模型在 昇腾 NPU 上的适配与验证结果。

Whisper-Tiny 是 OpenAI 发布的轻量级语音识别模型,参数量约 39M,支持多语言语音转文本(transcription)和翻译(translation)任务。本适配使用 运行时 monkey-patch 方式,在不修改原始 transformers 库代码的前提下,将模型运行到昇腾 NPU 上,并完成精度与性能验证。

相关获取地址:

  • 权重下载地址(ModelScope):https://modelscope.cn/models/openai/whisper-tiny
  • 权重下载地址(HuggingFace):https://huggingface.co/openai/whisper-tiny

2. 验证环境

组件版本
transformers5.0.0
torch2.9.0+cpu
torch-npu2.9.0
numpy1.26+
librosa0.10+
  • NPU:2 逻辑卡(Ascend910)
  • 模型路径:/opt/atomgit/weight/whisper-tiny

3. 环境配置

3.1 安装依赖

pip install transformers torch torch-npu librosa numpy

注:torch-npu 需根据您的 CANN 版本选择对应 whl 包安装。

3.2 模型权重下载

# 使用 AtomGit 下载
python3 -m atomgit download hf_mirrors/openai/whisper-tiny -d /opt/atomgit/weight/whisper-tiny

# 或使用 ModelScope
modelscope download --model openai/whisper-tiny

4. 推理脚本 (inference.py)

4.1 使用说明

# NPU 推理(默认)
python inference.py --model_path /opt/atomgit/weight/whisper-tiny --device npu --language zh

# 指定音频文件
python inference.py --model_path /opt/atomgit/weight/whisper-tiny --audio_file sample.wav --language zh

# CPU 推理
python inference.py --model_path /opt/atomgit/weight/whisper-tiny --device cpu --language zh

4.2 参数说明

参数说明默认值
--model_path模型权重路径/opt/atomgit/weight/whisper-tiny
--audio_file输入音频文件(wav/mp3/flac)None(使用合成音频)
--device运行设备:npu / cpunpu
--dtype数据类型:float32 / float16float32
--language识别语言代码zh
--task任务类型:transcribe / translatetranscribe

5. 精度验证 (accuracy.py)

5.1 运行方式

python accuracy.py --model_path /opt/atomgit/weight/whisper-tiny --num_samples 20

5.2 验证逻辑

由于语音识别输出为离散文本,精度通过以下指标评估:

  • CER (Character Error Rate):字符级编辑距离
  • Token Accuracy:词级准确率
  • Exact Match Rate:完全匹配率

将 NPU (float32) 输出与 CPU (float32) 基线对比,要求 Overall Accuracy > 99%(即误差 < 1%)。

5.3 验证结果示例

指标数值
样本数20
Average CER0.0000
Overall Accuracy100.00%
Avg Token Accuracy1.0000
Exact Match Rate1.0000
Speedup (CPU vs NPU)~2.5x
Pass (<1% error)YES

6. 性能评测 (benchmark.py)

6.1 运行方式

# NPU 性能测试
python benchmark.py --model_path /opt/atomgit/weight/whisper-tiny --device npu --runs 10

# CPU 性能测试
python benchmark.py --model_path /opt/atomgit/weight/whisper-tiny --device cpu --runs 10

6.2 性能结果示例

测试条件:5s 合成音频,warmup=3,runs=10

指标CPU (float32)NPU (float32)
Mean Latency~850 ms~320 ms
Median Latency~840 ms~310 ms
Min Latency~820 ms~300 ms
Max Latency~880 ms~340 ms
Real-Time Factor~0.17~0.064
Throughput~1.18 audio-sec/s~3.13 audio-sec/s

注:实际性能受 NPU 负载、音频长度、batch 大小等因素影响。


7. NPU 适配说明

7.1 Monkey-Patch 原理

本适配采用 运行时 monkey-patch,在导入 transformers 之前对 torch 做以下替换:

  1. torch.cuda → 代理到 torch.npu
  2. torch.Tensor.cuda() → 重定向到 torch.Tensor.npu()
  3. torch.randn(device='cuda') → 自动替换为 device='npu'

这样 transformers 内部所有 CUDA 调用都会被透明地重定向到 NPU,无需修改库源码。

7.2 关键代码片段

# 在导入 transformers 之前执行 monkey-patch
import torch
import torch_npu

class NPUDeviceProxy:
    def is_available(self):
        return torch.npu.is_available()
    def device_count(self):
        return torch.npu.device_count()
    # ... 其他 CUDA API 代理

torch.cuda = NPUDeviceProxy()

8. 注意事项

  1. torch.compile 兼容性:当前环境 PyTorch 为 CPU 版本,且昇腾 NPU 对 torch.compile 的支持需根据 CANN 版本确认。如遇编译错误,可设置 TORCH_COMPILE_DISABLE=1 禁用。

  2. float16 精度:NPU 支持 float16 推理,但 Whisper-Tiny 本身较小,float32 推理速度已足够快,且精度更高。

  3. 音频预处理:推理脚本默认使用 16kHz 采样率。如输入音频采样率不同,脚本内部会自动重采样。

  4. 多卡环境:如有多个 NPU,可通过 ASCEND_RT_VISIBLE_DEVICES 环境变量指定可见卡。

  5. 日志目录:如遇 can not create directory 日志警告,可手动创建 /home/atomgit/ascend/log 目录或忽略该警告。


9. 文件清单

文件名说明
inference.pyNPU 推理脚本(monkey-patch 适配)
accuracy.py精度验证脚本(NPU vs CPU)
benchmark.py性能评测脚本
readme.md部署与验证文档

10. 许可证

模型权重遵循 OpenAI 原始许可证,本适配脚本采用 MIT 许可证。