本项目将 timm/deit_small_patch16_224.fb_in1k 图片分类模型适配到华为昇腾 NPU (Ascend910) 上进行推理验证与性能测试。
timm.create_model(pretrained=False) + 本地权重加载| 项目 | 版本/型号 |
|---|---|
| NPU | Ascend910_9362 |
| npu-smi | 25.5.2 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 已安装 |
| timm | 最新版 |
npu_available=True, device=Ascend910_9362pip install -r requirements.txt
python inference.py推理日志示例:
Loading model: timm/deit_small_patch16_224.fb_in1k
Model moved to npu:0
Input shape: torch.Size([1, 3, 224, 224])
Output shape: torch.Size([1, 1000])
Top-5 predictions:
1. class_405: 0.138455
2. class_701: 0.093867
3. class_895: 0.090754
4. class_404: 0.025870
5. class_21: 0.025128
Inference completed successfully on NPU.python eval_accuracy.py对单张测试图片进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.012967 |
| mean_abs_error | 0.002194 |
| relative_error | 0.2969% |
| cosine_similarity | 0.999997 |
| threshold | 1.0% |
| 结果 | PASS |
python benchmark.py| 指标 | 数值 |
|---|---|
| Avg Latency | 5.31 ms |
| Min Latency | 5.26 ms |
| Max Latency | 5.35 ms |
| P50 | 5.32 ms |
| P90 | 5.35 ms |
| P95 | 5.35 ms |
| Throughput | 188.27 images/sec |
测试条件:单卡 Ascend910,batch_size=1,预热 2 轮 + 正式 10 轮。
| 指标 | 数值 |
|---|---|
| Max logits diff | 0.01296711 |
| Mean logits diff | 0.00219398 |
| Max prob diff | 0.00048672 |
| Top-1 Match | True |
| Top-5 Match | True |
CPU 与 NPU 推理结果在概率分布和 Top-K 预测上完全一致,logits 差异处于正常浮点误差范围内。
见 screenshots/self_verification.png 和 screenshots/self_verification.txt。
| 文件 | 内容 |
|---|---|
logs/env_check.log | NPU 环境检查 |
logs/inference.log | 推理结果 |
logs/accuracy.log | CPU-NPU 精度对比 |
logs/benchmark.log | 性能基准测试 |
logs/paths.txt | 模型路径记录 |
snapshot_download 自动下载权重,缓存于 ~/.cache/modelscope/hub/。pretrained=True 触发 HuggingFace 直连下载,所有权重均通过 ModelScope 获取后本地加载。.bin / .safetensors / .pth 等已列入 .gitignore)。torch.npu.synchronize() 确保计时准确。#NPU