g
gcw_AVRCax4T/manyspeech-cli
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

ManySpeech-DolphinASR 昇腾 NPU 适配

Model Hardware NPU CANN Framework Accuracy License

基于 华为昇腾 Ascend NPU 的 DolphinASR 语音识别模型适配,将 ModelScope manyeyes/manyspeech-cli 中的 DolphinASR-small-int8 模型成功部署至昇腾硬件平台,实现高精度离线语音识别推理。


模型简介

ManySpeech-CLI 是轻量化本地命令行语音识别工具,内置多种 ASR 模型。本项目将其中的 DolphinASR-small-int8 模型适配至华为昇腾 NPU,利用 ONNX Runtime 结合 torch_npu 实现混合推理加速。

模型架构:

  • Encoder: E-Branchformer (12 blocks, 768-dim, 12 heads)
  • Decoder: Transformer Decoder (12 layers, 768-dim, 3072 FFN)
  • Preprocessor: S2T (Speech-to-Text) with timestamps
  • Vocab Size: 40,002 tokens (Chinese + Japanese + English + multilingual)
  • Audio Input: 16kHz mono WAV
  • 量化方案: INT8 动态量化 (DynamicQuantizeLinear + MatMulInteger)

支持语言: 中文、日文、英文等多语言


环境要求

组件版本
昇腾 NPUAscend910_9362 (Atlas 800 A2)
CANN8.5.1
PyTorch2.9.0
torch_npu2.9.0.post1
ONNX Runtime1.26.0
Python3.11

快速开始

1. 下载模型

pip install modelscope
modelscope download --model manyeyes/manyspeech-cli

模型文件位于:

~/.cache/modelscope/hub/models/manyeyes/DolphinAsr-small-int8-onnx/
├── encoder.onnx          # E-Branchformer 编码器
├── decoder.int8.onnx     # INT8 Transformer 解码器
├── tokens.txt            # 词汇表 (40001 tokens)
├── bpe.model             # SentencePiece BPE 模型
├── conf.yaml             # 模型配置
├── configuration.json    # 框架配置
└── test_wavs/            # 测试音频
    ├── 0.wav             # 中文新闻 (17.4s)
    ├── 1.wav             # 日文朗读 (10.0s)
    └── 2.wav             # 中文散文 (31.0s)

2. 安装依赖

pip install onnx onnxruntime soundfile scipy

3. 运行推理

# CPU 推理 (基线)
python inference.py --audio test.wav --device cpu --beam-size 5

# NPU 推理 (混合加速:音频预处理 + Beam Search 在 NPU 执行)
python inference.py --audio test.wav --device npu --beam-size 5

# 多文件批量推理
python inference.py -f audio1.wav audio2.wav --device npu --format json -o results.json

参数说明:

参数简写说明默认值
--audio-f音频文件路径 (WAV)必填
--model-dir—模型目录DolphinAsr-small-int8-onnx
--device—推理设备 (cpu / npu)cpu
--beam-size—Beam Search 宽度5
--output-o结果输出路径 (JSON)None
--format—输出格式 (text/json/srt)text

精度评测

评测方法

对比 CPU (ONNX Runtime) 与 NPU (torch_npu 混合加速) 的推理输出,采用字符级完全匹配比对。

评测结果

测试文件时长CPU 输出NPU 输出相似度CPU 耗时NPU 耗时
0.wav (中文新闻)17.4s改革开放持续深化,最新外商投资准入负面清单...完全一致100%266.7s286.6s
1.wav (日文朗读)10.0sそういう願望を持っていました昔は宇宙というよりは...完全一致100%4.6s4.8s
2.wav (中文散文)31.0s每当新年的钟声敲响的时候,我总会闭起眼睛...完全一致100%25.5s28.5s

结论:

  • ✅ 精度:NPU 与 CPU 输出完全一致,文本相似度 100%,误差 < 0.01%
  • ✅ 所有测试文件均通过精度验证
  • ℹ️ NPU 耗时略高于 CPU,原因是当前 ONNX INT8 模型主要在 CPU 侧执行(模型包含 int8 动态量化 op,暂无法直接转换为 Ascend OM),NPU 主要加速音频预处理和 Beam Search 矩阵运算。后续可通过模型 FP32 化 + ATC 转换实现纯 NPU 推理以获得更优性能

运行评测

python evaluate.py --beam-size 5 --output output/evaluation_results.json

项目文件结构

manyspeech-npu/
├── inference.py                  # 推理脚本 (CPU + NPU 双后端)
├── evaluate.py                   # 精度/性能评测脚本
├── torch_decoder.py              # PyTorch 解码器 (NPU 权重加载)
├── onnx_to_torch.py              # ONNX → PyTorch 图转换器
├── README.md                     # 部署文档 (本文件)
├── requirements.txt              # Python 依赖
├── models/                       # 转换后的模型文件
│   ├── decoder_fp32.onnx         # FP32 解码器 (去量化)
│   └── decoder_weights.npz       # 解码器权重 (NumPy 格式)
└── output/                       # 输出目录
    └── evaluation_results.json   # 评测结果

技术实现

推理流程

音频输入 (WAV, 16kHz)
    │
    ▼
音频预处理 (torch_npu / NumPy)
    │ resample, normalize
    ▼
Encoder (ONNX Runtime)
    │ speech → enc_out [1, T, 768]
    ▼
Beam Search Decoder
    ├── ONNX Decoder (per-step logits)
    └── NPU Top-K + LogSoftmax (torch_npu 加速)
    │
    ▼
S2T 解码 + 文本后处理
    │
    ▼
识别文本输出

NPU 适配策略

由于 ONNX 模型使用了 INT8 动态量化算子 (DynamicQuantizeLinear, MatMulInteger),这些算子目前不被 ATC (Ascend Tensor Compiler) 和 onnx2torch 直接支持,因此采用混合推理策略:

  1. CPU 侧 (ONNX Runtime): 执行 INT8 量化的 Encoder 和 Decoder 推理
  2. NPU 侧 (torch_npu): 加速以下计算密集型操作:
    • LogSoftmax 运算
    • Beam Search 中的 Top-K 选择
    • 准备后续纯 NPU 推理的 FP32 模型转换

NPU 适配路线图:

  • CPU 基线推理
  • NPU 混合加速 (Beam Search + 预处理)
  • ONNX 权重提取与去量化
  • PyTorch 解码器重构
  • 完整 Encoder FP32 化 + ATC 转 OM
  • 纯 NPU 端到端推理

性能基准

指标CPU (ONNX Runtime)NPU (混合加速)
平均推理时间 (3 files)98.92s106.61s
Encoder 平均耗时1.28s7.85s
Decoder 平均耗时97.37s98.68s
实时率 RTF (17.4s 音频)15.3316.47
内存占用~2.5 GB~3.0 GB

注: 当前 NPU 模式下的性能瓶颈在于 ONNX INT8 模型的 CPU 侧推理。后续将模型完全 FP32 化并通过 ATC 转换为 OM 格式后,预计可获得 2-5x 的纯 NPU 推理加速。


许可与引用

  • 本项目基于 ManySpeech 开源项目
  • 模型来自 manyeyes/manyspeech-cli (ModelScope)
  • 昇腾适配代码采用 Apache 2.0 许可

标签

#NPU #Ascend910 #SpeechRecognition #DolphinASR #ONNX #torch_npu #CANN #ModelScope #ManySpeech #ASR


最后更新: 2026-05-18 | 昇腾 NPU 适配版本 v1.0