本项目将 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 ×2(每卡 HBM 62740 MB ≈ 64 GB) |
| npu-smi | 25.5.5 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| timm | 1.0.28 |
| modelscope | 1.36.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 展示。
推理输出示例(实际运行记录,详见第 7 节):
============================================================
Inference on Ascend NPU
============================================================
Model: timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k
Model dir: /data/.cache/models/timm/vit_tiny_patch16_224___augreg_in21k_ft_in1k
Weights: /data/.cache/models/timm/vit_tiny_patch16_224___augreg_in21k_ft_in1k/model.safetensors
Missing keys: 0, Unexpected keys: 0
Model moved to npu:0
NPU device: Ascend910_9362, total HBM: 62740 MB
NPU memory: allocated 21.9 MB, reserved 24.0 MB
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。
模型来源:timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k。
model_utils.py 通过 ModelScope snapshot_download 从本机可达源(ModelScope 镜像,权重已缓存于 /data/.cache)拉取权重,timm 仅用 timm.create_model(..., pretrained=False) 创建网络结构,全程不使用 HuggingFace 下载、不 fallback。model.to("npu:0") / inputs.to("npu:0") 挂载到昇腾 NPU,并在计时前后调用 torch.npu.synchronize() 同步等待算子执行完成,保证结果是真实在 NPU 上计算的。inference.py 对 assets/test.jpg 做预处理 → NPU 前向 → softmax → Top-5,并打印 NPU 设备名与显存占用(torch.npu.get_device_properties / memory_allocated / memory_reserved),用于记录真实 NPU 使用情况。eval_accuracy.py 用同一输入对比 CPU 与 NPU 输出(max/mean 绝对误差、相对误差、余弦相似度、Top-1/Top-5 类别一致性)。benchmark.py 统计 warmup=2 后 10 次推理的时延分布与吞吐。启动方式(运行前请确认 CANN/torch_npu 环境已配置):
python inference.py # 单次推理(NPU)
python eval_accuracy.py # CPU/NPU 一致性验证
python benchmark.py # NPU 性能基准模型权重默认从本地缓存(MODELSCOPE_CACHE,本机为 /data/.cache)解析,首次运行会自动下载到该目录。
本次在昇腾 910 环境(2 卡,每卡 HBM 64GB,aarch64)上真实运行验证。实测环境:torch 2.9.0+cpu、torch_npu 2.9.0.post1、timm 1.0.28、modelscope 1.36.3、npu-smi 25.5.5。
python inference.py)============================================================
Inference on Ascend NPU
============================================================
Model: timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k
Model dir: /data/.cache/models/timm/vit_tiny_patch16_224___augreg_in21k_ft_in1k
Weights: /data/.cache/models/timm/vit_tiny_patch16_224___augreg_in21k_ft_in1k/model.safetensors
Missing keys: 0, Unexpected keys: 0
Model moved to npu:0
NPU device: Ascend910_9362, total HBM: 62740 MB
NPU memory: allocated 21.9 MB, reserved 24.0 MB
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.npu-smi info 快照)+------------------------------------+----------------+----------------------+
| NPU Name | Health | Power(W) Temp | HBM-Usage(MB) |
+====================================+================+======================+
| 3 Ascend910 | OK | 180.3 45 | 3249 / 65536 |
| 0 6 | ... | 1 AICore(%) | (物理 ID 6) |
+------------------------------------+----------------+----------------------+
| 3 Ascend910 | OK | - 46 | 2870 / 65536 |
| 1 7 | ... | 0 AICore(%) | (物理 ID 7) |
+------------------------------------+----------------+----------------------+
| NPU Chip | Process id | Process name | Process memory(MB) |
| 3 0 | 31634 | python | 200 |说明:推理进程(python,PID 31634)在 NPU 0 上占用约 200 MB 设备内存;推理循环期间 AICore 有负载、整卡功耗升至约 180 W。完整快照见 logs/npu_usage.log。
python eval_accuracy.py)| 指标 | 数值 |
|---|---|
| max_abs_error | 0.019603 |
| mean_abs_error | 0.005047 |
| relative_error | 0.2303% |
| cosine_similarity | 0.999997 |
| threshold | 1.0% |
| 结果 | PASS |
class_447,一致[class_447, class_538, class_787, class_727, class_581],一致python benchmark.py)| 指标 | 数值 |
|---|---|
| Avg latency | 5.264 ms |
| Min latency | 5.197 ms |
| Max latency | 5.313 ms |
| P50 | 5.267 ms |
| P90 | 5.305 ms |
| P95 | 5.309 ms |
| Images/sec | 189.97 |
测试条件: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 | 性能基准测试 |
logs/npu_usage.log | 推理期间 NPU 使用快照(npu-smi) |
*.bin, *.safetensors, *.pth, *.pt, *.ckpt, *.onnx)已加入 .gitignore,不提交到仓库。MODELSCOPE_CACHE=/data/.cache)。torch_npu 已正确安装。#NPU