本项目将 ModelScope 上的 timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k 模型适配为可在单卡华为昇腾 NPU(Ascend910)上运行的提交工程。
模型采用 timm.create_model(pretrained=False) 创建结构,并通过 ModelScope snapshot_download 下载本地权重后进行加载,全程不使用 HuggingFace 直连下载,不 fallback。
| 项目 | 版本/型号 |
|---|---|
| NPU | Ascend910_9362 |
| npu-smi | 25.5.2 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 已安装 |
| timm | 1.0.27 |
| modelscope | 1.35.3 |
python inference.py模型通过 model_utils.py 统一加载:
modelscope.snapshot_download 下载权重timm.create_model(..., pretrained=False) 创建结构model.safetensors 加载权重推理在 npu:0 上执行,输入为 assets/test.jpg,输出 1000 类 logits,取 Top-5 展示。
推理输出示例:
============================================================
Inference on Ascend NPU
============================================================
Model: timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k
Model dir: /opt/atomgit/.cache/modelscope/hub/models/timm/vit_tiny_patch16_224___augreg_in21k_ft_in1k
Weights: /opt/atomgit/.cache/modelscope/hub/models/timm/vit_tiny_patch16_224___augreg_in21k_ft_in1k/model.safetensors
Missing keys: 0, Unexpected keys: 0
Model moved to npu:0
Input shape: torch.Size([1, 3, 224, 224])
Output shape: torch.Size([1, 1000])
Top-5 predictions:
class_447: 0.303056
class_538: 0.102479
class_787: 0.056499
class_727: 0.050405
class_581: 0.043690
Inference completed successfully.python eval_accuracy.py对单张测试图片进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.019603 |
| mean_abs_error | 0.005047 |
| relative_error | 0.2303% |
| cosine_similarity | 0.999997 |
| threshold | 1.0% |
| 结果 | PASS |
python benchmark.py| 指标 | 数值 |
|---|---|
| Avg latency | 5.223 ms |
| Min latency | 5.185 ms |
| Max latency | 5.269 ms |
| P50 | 5.217 ms |
| P90 | 5.264 ms |
| P95 | 5.267 ms |
| Images/sec | 191.45 |
测试条件:batch=1,输入 224x224,warmup=2,runs=10。
本工程仅提供 smoke consistency 验证。如需官方 ImageNet-1K Top-1/Top-5 精度,请使用标准评测数据集运行。
见 screenshots/self_verification.txt 和 screenshots/self_verification.png。
| 日志 | 说明 |
|---|---|
logs/env_check.log | NPU 环境检查 |
logs/paths.txt | ModelScope 下载路径记录 |
logs/inference.log | 推理结果 |
logs/accuracy.log | CPU/NPU 一致性对比 |
logs/benchmark.log | 性能基准测试 |
*.bin, *.safetensors, *.pth, *.pt, *.ckpt, *.onnx)已加入 .gitignore,不提交到仓库。torch_npu 已正确安装。#NPU