本仓库包含WeNet U2++ Conformer流式语音识别模型的NPU适配版本,该模型最初基于WenetSpeech数据集训练并导出为ONNX格式。
| 指标 | 结果 |
|---|---|
| NPU平台 | Ascend910(2x NPU) |
| CANN版本 | 8.5.1 |
| 适配方法 | onnx2torch → torch_npu |
| 编码器MAPE | 0.8653% ✅(< 1%) |
| CTC MAPE | 0.0213% ✅(< 1%) |
| 解码器MAPE | 0.0034% ✅(< 1%) |
| token准确率 | 100.00%(argmax匹配) |
| 余弦相似度 | >0.999999(所有模型) |
pip install torch torch_npu torchaudio onnx onnxruntime onnx2torch soundfile scipypip install modelscope
modelscope download --model manyeyes/wenet-u2pp-conformer-wenetspeech-onnx-online-20220506 --local_dir ./model# CPU inference (onnxruntime baseline)
python inference.py --backend cpu --wav test_wavs/0.wav
# NPU inference (torch_npu via onnx2torch)
python inference.py --backend npu --wav test_wavs/0.wav
# Evaluate NPU vs CPU accuracy
python evaluate_npu.py测试音频 0.wav(3.55秒):
Greedy text: 朱立楠在上书战的上
Beam+Attn: 朱立楠在上书战的上
RTF (CPU): 0.2462
RTF (NPU): 0.2833Audio (16kHz) → FBank [T, 80] → Streaming Conformer Encoder
↓
[T_enc, 512]
↙ ↘
CTC [T, 5538] Attention Decoder
↓ ↓
CTC Greedy Decoder Rescore
↓ ↓
Combined → Final Text| 组件 | 输入 | 输出 | 大小 |
|---|---|---|---|
| Encoder | chunk [1,T,80] + caches | [1,T_enc,512] + new caches | 343 MB |
| CTC | hidden [1,T_enc,512] | [1,T_enc,5538] | 11 MB |
| Decoder | hyps + encoder_out | [NBEST,L,5538] | 157 MB |
通过onnx2torch将ONNX模型转换为PyTorch FX GraphModules,
然后使用torch_npu在昇腾NPU上执行。该方法:
ONNX Model → onnx2torch.convert() → torch.fx.GraphModule → .to("npu") → torch_npu inference| 模型 | CPU (秒) | NPU (秒) | 加速比 |
|---|---|---|---|
| 编码器 | 0.4749 | 0.7772 | 0.61x |
| CTC | 0.0051 | 0.0026 | 1.97x |
| 解码器 | 0.3929 | 0.2247 | 1.75x |
| 总计 | 0.8729 | 1.0045 | 0.87x |
注:编码器NPU性能包含onnx2torch图优化开销。通过ATC OM转换可进一步优化。
CPU(onnxruntime)与NPU(torch_npu)输出的全面精度对比:
| 模型 | 最大绝对误差 | 平均绝对误差 | 平均绝对百分比误差 | 余弦相似度 | 令牌准确率 |
|---|---|---|---|---|---|
| 编码器 | 0.01127 | 0.00048 | 0.8653% | 0.99999945 | 100.00% |
| CTC | 0.04132 | 0.00461 | 0.0213% | 0.99999997 | 100.00% |
| 解码器 | 0.00240 | 0.00035 | 0.0034% | 1.00000000 | 100.00% |
| 文件 | 描述 |
|---|---|
inference.py | 主推理脚本(CPU + NPU后端) |
evaluate_npu.py | NPU与CPU精度评估脚本 |
evaluation_results.json | 完整精度+性能指标 |
encoder.onnx | 原始编码器ONNX模型 |
decoder.onnx | 原始解码器ONNX模型 |
ctc.onnx | 原始CTC ONNX模型 |
tokens.txt | 词汇表(5538个令牌) |
configuration.json | 模型元数据 |
test_wavs/ | 测试音频样本(4个WAV文件) |
@inproceedings{wenet2021,
title={WeNet: Production oriented Streaming and Non-streaming End-to-End Speech Recognition Toolkit},
author={Yao, Zhuoyuan and Wu, Di and Wang, Xiong and Zhang, Binbin and Yu, Fan and Yang, Chao and Peng, Zhendong and Chen, Xiaoyu and Xie, Lei and Lei, Xin},
booktitle={Proc. Interspeech},
year={2021}
}本模型基于 Apache License 2.0 许可协议发布。