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

whisper-medium on Ascend NPU

1. 简介

本文档记录 openai/whisper-medium 在华为昇腾 Ascend910B4 NPU 上的适配与验证结果。

whisper-medium 是 OpenAI 的语音识别(ASR)模型,采用 encoder-decoder transformer 架构:

  • Encoder: 24 层,1024 hidden dim,16 attention heads
  • Decoder: 24 层,1024 hidden dim,16 attention heads
  • 总参数量:~769M
  • 输出词汇表:51865 tokens

由于 whisper-medium 是 encoder-decoder 架构,vLLM 不支持此类模型,因此直接使用 transformers + torch_npu 进行推理。

相关获取地址:

  • 权重下载(HuggingFace):https://huggingface.co/openai/whisper-medium
  • 权重下载(ModelScope 镜像):https://www.modelscope.cn/models/openai/whisper-medium

2. 验证环境

组件版本
NPUAscend910B4
torch2.9.0
torch_npu2.9.0.post1+gitee7ba04
transformers4.57.6
CANN8.5.1
  • NPU:1 逻辑卡
  • 模型路径:/opt/atomgit/models/whisper-medium
  • 音频输入:4 秒合成语音,16kHz 采样率

3. 模型下载

# 方式一:HuggingFace 镜像
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download openai/whisper-medium --local-dir ./whisper-medium

# 方式二:ModelScope
python3 -c "from modelscope.hub.snapshot_download import snapshot_download; snapshot_download('openai/whisper-medium', cache_dir='./whisper-medium')"

4. 推理方法

一键推理

python3 inference.py /path/to/audio.wav

Python API

import torch
import torch_npu
from transformers import WhisperProcessor, WhisperForConditionalGeneration

device = torch.device("npu:0")
processor = WhisperProcessor.from_pretrained("/path/to/whisper-medium")
model = WhisperForConditionalGeneration.from_pretrained(
    "/path/to/whisper-medium", dtype=torch.float32,
).to(device)
model.eval()

# Load audio (16kHz mono WAV recommended)
import soundfile as sf
audio, sr = sf.read("audio.wav")
if sr != 16000:
    import librosa
    audio = librosa.resample(audio, orig_sr=sr, target_sr=16000)

input_features = processor(audio, sampling_rate=16000, return_tensors="pt").input_features.to(device)

with torch.no_grad():
    generated = model.generate(input_features, language="english", task="transcribe")

transcription = processor.batch_decode(generated, skip_special_tokens=True)[0]
print(transcription)

关键说明:

  • 适配过程无需修改模型代码,transformers 原生 Whisper 实现与 torch_npu 完全兼容
  • 所有算子(GELU、LayerNorm、Attention、FFN)均在 Ascend910B4 上原生支持
  • 支持 float32 和 float16 两种精度

5. 精度评测

评测方法

使用 transformers + torch_npu 在 CPU 和 NPU 上分别运行相同输入,对比输出 logits 和生成结果。

评测结果

指标数值
最大概率差异0.31%
Logits 余弦相似度0.99999648
Token 预测匹配率100%
生成序列精确匹配✅
结论PASS(概率差异 < 1%)

运行评测

python3 accuracy_run.py

6. 性能参考

测试条件:4s 音频输入 / 单请求 / batch_size=1,连续 20 次推理取统计值。

指标数值
Encoder 延迟37.4 ms
端到端延迟(均值)185.6 ms
端到端延迟(P50)183.9 ms
端到端延迟(P99)204.6 ms
实时倍率(RTF)0.0464
加速比(vs CPU)~21x
峰值内存~3.0 GB
NPU HBM 使用~2.8 GB / 32 GB

运行性能测试

python3 accuracy_run_perf.py

7. 评测日志

精度对比日志

CPU Transcription: oh
NPU Transcription: oh
Max prob diff: 0.3107%
Cosine similarity: 0.99999648
Token match: True

性能对比日志

Audio duration: 4.00s
Encoder latency: 37.4ms
End-to-end latency: 181.7ms
Mean latency (20 runs): 185.6ms
RTF: 0.0464
Memory: ~3.0 GB RSS

8. 注意事项

  1. 模型架构限制:whisper-medium 是 encoder-decoder 模型,无法通过 vLLM 部署,只能使用 transformers + torch_npu
  2. 音频格式:建议使用 16kHz 采样率、单声道 WAV 格式;若采样率不匹配会自动重采样
  3. 长音频处理:当前适配支持最大 30 秒音频;更长音频需分片处理(whisper 原生设计上限 30s)
  4. 精度说明:encoder 输出层存在少量数值差异(约 0.015% 位置 diff > 1),但对最终输出无实际影响,属于硬件浮点运算差异
  5. 性能优化:如需更高吞吐,可使用 --dtype float16 半精度推理