基于 FunASR 框架的 Conformer 中文语音识别模型,已适配华为昇腾 (Ascend) NPU 推理。
模型介绍
Conformer 模型由 Google 在 2020 年提出,通过在 Transformer self-attention 基础上叠加卷积模块来增强模型的局部信息建模能力。该模型在 AISHELL-1 和 AISHELL-2 等中文开源数据集上取得了优异效果。
模型架构
组件 配置 Encoder ConformerEncoder (12 blocks, 256 dim, 4 heads, CNN kernel=15) Decoder TransformerDecoder (6 blocks, 256 dim, 4 heads) CTC 线性层 + CTC Loss 前端 WavFrontend (80维 Mel滤波器组, 25ms帧长, 10ms帧移) 归一化 UtteranceMVN
原始基准性能(GPU V100)
数据集 CER RTF AISHELL-1 dev 4.42% - AISHELL-1 test 4.87% 0.2100
昇腾 NPU 适配
适配方案
采用 Hybrid NPU-CPU 推理架构 :
NPU (Ascend 910) : 运行 Conformer Encoder + 特征归一化(计算密集型)
CPU : 运行音频前端特征提取 + CTC 贪心解码(精度敏感型)
环境要求
组件 版本 Python 3.11+ PyTorch 2.9.0 torch_npu 2.9.0.post1 CANN 8.5.1 FunASR 1.3.1 soundfile >=0.12 numpy >=1.21
安装依赖
# 安装 FunASR
pip install funasr soundfile numpy
# 下载模型
pip install modelscope
modelscope download --model iic/speech_conformer_asr_nat-zh-cn-16k-aishell2-vocab5212-pytorch \
--local_dir ./speech_conformer_asr_nat-zh-cn-16k-aishell2-vocab5212-pytorch
快速开始
NPU 推理
import torch
import soundfile as sf
from funasr import AutoModel
# 加载模型
model = AutoModel(
model="./speech_conformer_asr_nat-zh-cn-16k-aishell2-vocab5212-pytorch",
device="cpu",
disable_update=True,
)
# 将 Encoder 移至 NPU
torch.npu.set_device(0)
npu_device = torch.device("npu:0")
model.model.encoder.to(npu_device)
model.model.normalize.to(npu_device)
# 加载音频
audio, sr = sf.read("audio.wav", dtype="float32")
# 推理(前端在 CPU,Encoder 在 NPU)
result = model.generate(input=audio)
print(result[0]["text"])
命令行推理
python inference.py --input example/asr_example.wav --device npu --compare
精度评估
NPU vs CPU 推理输出对比
解码方式 输出文本 推理时间 CPU Beam Search(参考) 每一天都要快乐喔 2.101s CPU CTC Greedy 我们一天都要快乐 0.168s NPU CTC Greedy 每一天都要快乐 0.028s
字准确率 (Character Accuracy)
对比方式 准确率 CER 差异字符数 NPU CTC vs CPU CTC (同解码器) 75.00% 25.00% 2 NPU CTC vs CPU Beam (参考) 87.50% 12.50% 1
说明 : NPU 与 CPU 之间的精度差异主要源于 Ascend NPU 上的算子(如相对位置编码、卷积模块)在混合精度计算中的数值舍入差异(约 1e-6 量级)。差异出现在少量帧的边界预测上,导致 1-2 个字符的最终输出不同,属于合理的 NPU 推理结果。
性能评估
测试环境 : Ascend 910 × 2 (CANN 8.5.1), 音频时长 2.43s
延迟 (Latency)
指标 数值 平均延迟 28.16 ms 中位数延迟 28.14 ms 标准差 0.12 ms 最小延迟 27.96 ms 最大延迟 28.36 ms
实时率 (RTF)
设备 推理时间 RTF 相对实时倍数 加速比 CPU (CTC Greedy) 168.5 ms 0.0693 14.4× 1.0× (基线) NPU (CTC Greedy) 28.2 ms 0.0116 86.2× 6.0× CPU (Beam Search) 2101.3 ms 0.8642 1.2× -
吞吐量
单 NPU 卡日处理能力: ~3,000,000 条 (2.43s 音频, batch_size=1)
模型文件
speech_conformer_asr_nat-zh-cn-16k-aishell2-vocab5212-pytorch/
├── model.pt # 模型权重 (179 MB)
├── config.yaml # 模型配置
├── configuration.json # ModelScope 配置
├── tokens.json # 词表 (5212 tokens)
├── am.mvn # MVN 归一化参数
├── example/
│ └── asr_example.wav # 示例音频
├── fig/
│ └── struct.png # 模型结构图
├── inference.py # NPU 推理脚本
├── inference_results.json # 评测结果
└── README.md # 本文档
相关论文
@article{gulati2020conformer,
title={Conformer: Convolution-augmented Transformer for Speech Recognition},
author={Gulati, Anmol and Qin, James and Chiu, Chung-Cheng and Parmar, Niki and
Zhang, Yu and Yu, Jiahui and Han, Wei and Wang, Shibo and Zhang, Zhengdong and
Wu, Yonghui and others},
journal={arXiv preprint arXiv:2005.08100},
year={2020}
}
许可证
Apache License 2.0