g
gyccc/timm-vit_tiny_patch16_224.augreg_in21k_ft_in1k-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k on Ascend NPU

1. 简介

本项目将 ModelScope 上的 timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k 模型适配为可在单卡华为昇腾 NPU(Ascend910)上运行的提交工程。

模型采用 timm.create_model(pretrained=False) 创建结构,并通过 ModelScope snapshot_download 下载本地权重后进行加载,全程不使用 HuggingFace 直连下载,不 fallback。

2. 验证环境

项目版本/型号
NPUAscend910_9362 ×2(每卡 HBM 62740 MB ≈ 64 GB)
npu-smi25.5.5
PyTorch2.9.0+cpu
torch_npu2.9.0.post1
timm1.0.28
modelscope1.36.3

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.

4. 精度验证

python eval_accuracy.py

对单张测试图片进行 CPU 与 NPU 一致性验证:

指标数值
max_abs_error0.019603
mean_abs_error0.005047
relative_error0.2303%
cosine_similarity0.999997
threshold1.0%
结果PASS
  • CPU Top-1 与 NPU Top-1 类别一致
  • CPU Top-5 与 NPU Top-5 类别一致

5. 性能参考

python benchmark.py
指标数值
Avg latency5.223 ms
Min latency5.185 ms
Max latency5.269 ms
P505.217 ms
P905.264 ms
P955.267 ms
Images/sec191.45

测试条件:batch=1,输入 224x224,warmup=2,runs=10。

6. NPU 适配步骤

模型来源:timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k。

  1. 权重获取(非 HuggingFace 直连):model_utils.py 通过 ModelScope snapshot_download 从本机可达源(ModelScope 镜像,权重已缓存于 /data/.cache)拉取权重,timm 仅用 timm.create_model(..., pretrained=False) 创建网络结构,全程不使用 HuggingFace 下载、不 fallback。
  2. NPU 设备挂载:脚本统一将模型与输入执行 model.to("npu:0") / inputs.to("npu:0") 挂载到昇腾 NPU,并在计时前后调用 torch.npu.synchronize() 同步等待算子执行完成,保证结果是真实在 NPU 上计算的。
  3. 推理入口:inference.py 对 assets/test.jpg 做预处理 → NPU 前向 → softmax → Top-5,并打印 NPU 设备名与显存占用(torch.npu.get_device_properties / memory_allocated / memory_reserved),用于记录真实 NPU 使用情况。
  4. 精度验证:eval_accuracy.py 用同一输入对比 CPU 与 NPU 输出(max/mean 绝对误差、相对误差、余弦相似度、Top-1/Top-5 类别一致性)。
  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)解析,首次运行会自动下载到该目录。

7. NPU 实测结果(2026-08-04)

本次在昇腾 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。

7.1 推理输出(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.

7.2 NPU 使用情况(推理期间 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。

7.3 CPU/NPU 一致性(python eval_accuracy.py)

指标数值
max_abs_error0.019603
mean_abs_error0.005047
relative_error0.2303%
cosine_similarity0.999997
threshold1.0%
结果PASS
  • CPU Top-1 / NPU Top-1:class_447,一致
  • CPU Top-5 / NPU Top-5:[class_447, class_538, class_787, class_727, class_581],一致

7.4 性能基准(python benchmark.py)

指标数值
Avg latency5.264 ms
Min latency5.197 ms
Max latency5.313 ms
P505.267 ms
P905.305 ms
P955.309 ms
Images/sec189.97

测试条件:batch=1,输入 224x224,warmup=2,runs=10。

8. 精度评测

本工程仅提供 smoke consistency 验证。如需官方 ImageNet-1K Top-1/Top-5 精度,请使用标准评测数据集运行。

9. 自验证截图

见 screenshots/self_verification.txt 和 screenshots/self_verification.png。

10. 日志文件

日志说明
logs/env_check.logNPU 环境检查
logs/paths.txtModelScope 下载路径记录
logs/inference.log推理结果
logs/accuracy.logCPU/NPU 一致性对比
logs/benchmark.log性能基准测试
logs/npu_usage.log推理期间 NPU 使用快照(npu-smi)

11. 注意事项

  1. 权重文件(*.bin, *.safetensors, *.pth, *.pt, *.ckpt, *.onnx)已加入 .gitignore,不提交到仓库。
  2. 首次运行时会通过 ModelScope 自动下载模型权重到本地缓存目录(本机 MODELSCOPE_CACHE=/data/.cache)。
  3. 运行前请确保 NPU 驱动和 torch_npu 已正确安装。

12. 标签

#NPU