本模型是iic/speech_sanm_kws_phone-xiaoyun-commands-offline的NPU适配版本,这是一款基于FunASR项目的SANM(带记忆的自注意力网络)架构开发的移动端级中文关键词识别模型。
该模型支持38个中文命令关键词,涵盖多种场景(唤醒词、音乐、导航、智能家居和通用命令),并已在华为昇腾NPU上验证通过,准确率与CPU参考值相差在1%以内。
| 项目 | 数值 |
|---|---|
| 模型架构 | SANM(6层编码器 + CTC) |
| 参数数量 | 3.28M |
| 输入 | 16kHz单声道WAV |
| 特征 | 40维Fbank + LFR(m=7,n=6) |
| 输出 | 2602-token CTC对数概率 |
| 解码方式 | CTC贪婪解码 |
| 支持关键词 | 38个(唤醒词 + 多场景命令) |
| 组件 | 版本 |
|---|---|
| CANN | 8.5.1 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| FunASR | 1.3.1 |
| Python | 3.11 |
| NPU设备 | Ascend910_9362(Atlas 800 A2) |
该模型是标准的PyTorch模型(FunASR SanmKWS)。NPU适配通过torch_npu将计算迁移至昇腾设备:
AutoModel在CPU上加载SANM KWS模型model.to("npu:0")将所有参数和缓冲区迁移至昇腾NPUtorch.npu.synchronize()进行计时无需自定义算子或修改计算图——通过torch_npu的PyTorch算子映射,模型可直接在昇腾NPU上运行。
通过以下指标将 CPU 参考推理与 NPU 推理进行比较:
1 - cosine_similarity 的百分比(阈值 <0.01%)| 指标 | 数值 | 状态 |
|---|---|---|
| 解码文本 CPU | 你好我小云小云 | - |
| 解码文本 NPU | 你好我小云小云 | - |
| 解码文本匹配 | True | PASS |
| 关键词匹配 | True (小云小云) | PASS |
| CTC 余弦相似度 | 0.99998474 | PASS |
| CTC 角度误差 | 0.0015% | PASS (<0.01%) |
| 编码器余弦相似度 | 0.99952179 | - |
| 编码器角度误差 | 0.0478% | - |
基准测试配置:5 次预热运行 + 100 次计时运行,测量编码器+CTC 推理。
| 指标 | CPU | NPU | 加速比 |
|---|---|---|---|
| 平均延迟 | 17.58 ms | 6.42 ms | 2.74x |
| 标准差 | 0.17 ms | 0.05 ms | - |
| 最小延迟 | 17.31 ms | 6.34 ms | 2.73x |
| 最大延迟 | 18.21 ms | 6.54 ms | 2.78x |
# Install dependencies
pip install torch torch_npu funasr modelscope soundfile scipy
# The model will be auto-downloaded from ModelScope on first use# Basic NPU inference
python inference.py --audio test_audio.wav --keywords "小云小云" --device npu
# With benchmark
python inference.py --audio test_audio.wav --keywords "小云小云,你好小云" --device npu --benchmark
# CPU mode
python inference.py --audio test_audio.wav --keywords "小云小云" --device cpu# Compare NPU vs CPU accuracy + benchmark
python evaluate.py --audio test_audio.wav --keywords "小云小云" --benchmarkfrom inference import KWSInference
# Initialize on NPU
engine = KWSInference(keywords="小云小云,播放音乐", device="npu")
# Run inference
result = engine.infer("audio.wav")
print(result["decoded_text"]) # "你好我小云小云"
print(result["keyword_matches"]) # {"小云小云": True, "播放音乐": False}| 文件 | 描述 |
|---|---|
inference.py | 基于 Python API 的 NPU 推理脚本 |
evaluate.py | 精度评估(CPU 与 NPU 对比)及性能基准测试 |
outputs/evaluation_result.json | 精度对比结果 |
outputs/benchmark_result.json | 性能基准测试结果 |
logs/ | 运行日志目录 |
test_audio.wav | 示例音频(“小云小云”唤醒词,时长 4.53 秒) |
@inproceedings{Gao2020SANMME,
title={SAN-M: Memory Equipped Self-Attention for End-to-End Speech Recognition},
author={Zhifu Gao and Shiliang Zhang and Ming Lei and Ian Mcloughlin},
booktitle={Interspeech},
year={2020},
}