本文档记录 alefiury/wav2vec2-large-xlsr-53-gender-recognition-librispeech 在华为昇腾 Ascend 910B NPU 上的适配、部署与验证结果。
alefiury/wav2vec2-large-xlsr-53-gender-recognition-librispeech 是基于 Wav2Vec2-XLSR-53 架构在 English语音数据上进行微调得到的 音频分类 模型。 该模型基于 Wav2Vec2-XLSR-53 架构在 LibriSpeech 上进行微调,用于性别识别(Gender Recognition),能够从语音中区分男性和女性说话人。
模型参数量约 315M,采用 Wav2Vec2 自监督学习框架,在 16kHz 单声道原始音频上进行特征提取。
torch_npu 将 PyTorch 模型迁移至 Ascend NPUtransfer_to_npu 自动完成 CUDA 到 NPU 的 API 映射| 组件 | 版本 |
|---|---|
CANN | 8.5.1 |
torch | 2.5.1 |
torch-npu | 2.5.1.dev20260320 |
transformers | 4.57.6 |
numpy | 2.2.2 |
| NPU 型号 | Ascend 910B4(1 卡,32GB HBM) |
| 操作系统 | Linux 5.10.0 aarch64 |
# 安装依赖(使用清华 PyPI 镜像)
pip install torch transformers numpy -i https://pypi.tuna.tsinghua.edu.cn/simple/
# 确保 CANN 和 torch_npu 已正确安装
# 参考: https://www.hiascend.com/document/# 使用 huggingface-cli 下载
huggingface-cli download alefiury/wav2vec2-large-xlsr-53-gender-recognition-librispeech \
--local-dir ./model --local-dir-use-symlinks False
# 或使用 GitCode 镜像
git lfs clone https://gitcode.com/hf_mirrors/alefiury/wav2vec2-large-xlsr-53-gender-recognition-librispeech.git ./model# 单次推理(生成 3 秒虚拟音频)
python inference.py --model_path ./model --duration 3.0
# 运行性能基准测试
python inference.py --model_path ./model --benchmark参数说明:
| 参数 | 说明 | 默认值 |
|---|---|---|
--model_path | 模型权重路径 | ./model |
--duration | 生成的虚拟音频时长(秒) | 3.0 |
--benchmark | 启用性能测试模式 | False |
Wav2Vec2 是确定性模型(deterministic),给定相同输入,CPU 和 NPU 应产生几乎完全一致的输出。
运行命令:
python accuracy_run.py ./model accuracy_report.json验证结果:
| 音频时长 | 余弦相似度 | MAE | 相对误差 | 最大绝对误差 | 皮尔逊相关系数 | 状态 |
|---|---|---|---|---|---|---|
| 0.5s | 1.000000 | 5.42e-05 | 0.37% | 5.94e-04 | 0.9999999441 | PASS |
| 1.0s | 1.000000 | 5.30e-05 | 0.26% | 3.97e-04 | 0.9999999483 | PASS |
| 2.0s | 1.000000 | 5.04e-05 | 0.30% | 3.56e-04 | 0.9999999533 | PASS |
| 3.0s | 1.000001 | 5.28e-05 | 0.50% | 8.04e-04 | 0.9999999487 | PASS |
| 5.0s | 1.000001 | 5.23e-05 | 0.31% | 3.66e-04 | 0.9999999501 | PASS |
总体状态:PASS(阈值:余弦相似度 > 0.999,相对误差 < 1%)
精度分析:
运行命令:
python accuracy_run_perf.py ./model 10 perf_report.jsonNPU 性能结果(10 次迭代,warmup 3 次):
| 音频时长 | 平均延迟 | P50 延迟 | P90 延迟 | RTF | 吞吐率 |
|---|---|---|---|---|---|
| 0.5s | 26.4ms | 26.3ms | 27.3ms | 0.0528 | 18.93x |
| 1.0s | 27.1ms | 27.1ms | 27.7ms | 0.0271 | 36.85x |
| 2.0s | 28.8ms | 28.6ms | 31.9ms | 0.0144 | 69.38x |
| 3.0s | 28.0ms | 27.9ms | 28.4ms | 0.0093 | 107.32x |
| 5.0s | 28.1ms | 28.0ms | 29.1ms | 0.0056 | 177.73x |
RTF (Real-Time Factor) 表示处理 1 秒音频所需的计算时间。 吞吐率 = 1 / RTF,表示每秒可处理的音频时长倍数。
性能分析:
.
├── model/ # 模型权重(需自行下载)
│ ├── config.json
│ ├── pytorch_model.bin
│ └── preprocessor_config.json
├── inference.py # NPU 推理脚本
├── accuracy_run.py # CPU vs NPU 精度验证脚本(串行执行)
├── accuracy_run_perf.py # NPU 性能基准测试脚本
├── accuracy_report.json # 精度验证报告
├── perf_report.json # 性能测试报告
└── README.md # 本文档accuracy_run.py 先运行 CPU 推理,释放内存后再运行 NPU 推理,防止单卡显存不足。transfer_to_npu 会自动替换 torch.cuda.* 为 torch.npu.*,首次 import 会有警告,属于正常现象。@article{conneau2020unsupervised,
title={Unsupervised Cross-lingual Representation Learning for Speech Recognition},
author={Conneau, Alexis and Baevski, Alexei and Collobert, Ronan and Mohamed, Abdelrahman and Auli, Michael},
journal={arXiv preprint arXiv:2006.13979},
year={2020}
}适配方:Ascend-SACT
标签:#NPU #Ascend #Wav2Vec2 #XLSR #English
基于现有评测数据,CPU 与 NPU 的 余弦相似度 精度误差为 0.0%,小于 1% 的精度要求。
本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。
基于现有评测数据,CPU 与 NPU 的 余弦相似度 精度误差为 0.0%,小于 1% 的精度要求。