本项目基于 ModelScope 上的 UniASR 闽南语模型,完成了在华为昇腾 Ascend 910 NPU 上的推理适配、精度验证和性能评测。
UniASR(Universal ASR)是阿里巴巴达摩院推出的流式/离线一体化语音识别模型,支持闽南语方言和普通话的识别。该模型采用 2-pass 刷新架构:
模型结构包含共享的 SAN-M 动态编码器、CIF 预测器、SCAMA 解码器和降采样层,支持 fast(流式)、normal(2-pass)和 offline(离线)三种解码模式。
| 项目 | 版本 |
|---|---|
| 硬件 | Ascend 910 NPU × 2 |
| CANN | 8.5.1 |
| torch | 2.9.0 |
| torch_npu | 2.9.0 |
| FunASR | 1.3.1 |
| Python | 3.11 |
该模型为 PyTorch 原生的 FunASR UniASR 模型,通过 torch_npu 进行 Ascend NPU 适配:
torch_npu 将模型和输入张量从 CPU 移至 NPU 设备(npu:0)输入音频 (16kHz wav)
↓
WavFrontend (Mel Filterbank + LFR)
↓
Encoder1 (SAN-M, 35 blocks, chunk=20/60)
↓
CIF Predictor → 声学 Embedding
↓
Stride Conv (降采样 ×2)
↓
Encoder2 (SAN-M, 20 blocks, chunk=45/70)
↓
CIF Predictor2 → Token 数量预测
↓
SCAMA Decoder2 (Beam Search, size=5)
↓
输出文本使用相同的输入音频,分别在 CPU 和 NPU 上执行推理,比较输出文本的一致性。验证指标:文本完全匹配率 = 100%(误差 < 1%)。
| 测试音频 | 时长 | CPU 输出 | NPU 输出 | 文本匹配 |
|---|---|---|---|---|
| asr_example.wav | 5.55s | 欢迎大家来体验达摩院推出的语音识别模型 | 欢迎大家来体验达摩院推出的语音识别模型 | ✅ |
精度结论:NPU 推理输出与 CPU 基线完全一致,误差 < 1%,通过精度验证。
测试音频:example/asr_example.wav(时长 5.547s)
| 指标 | CPU | NPU (Ascend 910) | 加速比 |
|---|---|---|---|
| 平均推理时间 | 2.754s | 1.144s | 2.41x |
| RTF (Real Time Factor) | 0.497 | 0.206 | 2.41x |
| 推理标准差 | 0.070s | 0.157s | - |
精度验证:NPU 与 CPU 输出文本完全一致,误差 < 1%,PASS
# CPU 基准测试
python inference.py --input example/asr_example.wav --device cpu --benchmark_runs 10
# NPU 推理测试
python inference.py --input example/asr_example.wav --device npu --benchmark_runs 10
# 精度对比测试
python accuracy_benchmark.py --input example/asr_example.wav --runs 10# 安装依赖
pip install modelscope funasr torch_npu soundfile numpy
# 下载模型
modelscope download --model chenyongxian299/speech_UniASR_asr_2pass-minnan-16k-common-vocab3825 --local_dir .import soundfile as sf
from funasr import AutoModel
# 加载模型
model = AutoModel(model=".", device="npu:0", disable_update=True)
# 加载音频
audio, sr = sf.read("example/asr_example.wav")
# 推理
result = model.generate(input=audio)
print(result[0]["text"])
# 输出: 欢迎大家来体验达摩院推出的语音识别模型# CPU 推理
python inference.py --input example/asr_example.wav --device cpu
# NPU 推理
python inference.py --input example/asr_example.wav --device npu
# 切换解码模式
python inference.py --input example/asr_example.wav --device npu --decoding_mode fast
python inference.py --input example/asr_example.wav --device npu --decoding_mode offline| 文件 | 说明 |
|---|---|
inference.py | 推理脚本,支持 CPU/NPU,支持单文件和批量推理 |
accuracy_benchmark.py | 精度验证脚本,CPU vs NPU 对比测试 |
model.pb | 模型权重文件(PyTorch 格式) |
config.yaml | 模型结构配置 |
tokens.json | 词表(3825 tokens) |
seg_dict | 分词字典 |
am.mvn | 声学模型均值方差归一化参数 |
example/asr_example.wav | 示例音频 |
Apache License 2.0
@inproceedings{gao2020universal,
title={Universal ASR: Unifying Streaming and Non-Streaming ASR Using a Single Encoder-Decoder Model},
author={Gao, Zhifu and Zhang, Shiliang and Lei, Ming and McLoughlin, Ian},
booktitle={arXiv preprint arXiv:2010.14099},
year={2020}
}