本仓库包含了 timm 中的 test_vit2.r160_in1k 模型在华为昇腾 NPU 上的适配与推理实现。
test_vit2.r160_in1k (timm / HuggingFace Hub)本模型基于 timm 库加载预训练权重,模型权重通过 ModelScope 下载。在昇腾 NPU 上通过 torch_npu 实现硬件加速。
pip install -r requirements.txtpython inference.pypip install -r requirements.txtpython inference.pypython compare_cpu_npu.py| 指标 | 数值 |
|---|---|
| CPU 推理耗时 | 0.0087s |
| NPU 推理耗时 | 0.0041s |
| MAE (平均绝对误差) | 0.00671956 |
| Max Error (最大误差) | 0.0254631 |
| MSE (均方误差) | 7.041e-05 |
| 相对误差 | 0.00542541 |
| 误差百分比 | 0.5425 |
| 余弦相似度 | 0.99999153 |
| Top-5 索引一致性 | 是 |
结论: NPU 与 CPU 推理误差 < 1%
| 设备 | 推理耗时 (s) |
|---|---|
| CPU | 0.0087 |
| NPU | 0.0041 |
CPU inference time: 0.0087s
Top-5 indices: [530, 539, 549, 714, 646]
Top-5 values: [5.667263, 5.593978, 4.516138, 4.503229, 4.185716]
NPU inference time: 0.0041s
Top-5 indices: [530, 539, 549, 714, 646]
Top-5 values: [5.641813, 5.569588, 4.490881, 4.481538, 4.173948]
CPU/NPU Relative Error: 0.5425%Mean Absolute Error (MAE): 0.00671956
Max Absolute Error: 0.0254631
Cosine Similarity: 0.99999153
Relative Error: 0.00542541运行截图由 acend- 工具生成,展示推理成功画面。