本文档记录 google/vit-base-patch16-224-in21k 在昇腾 NPU 上的推理适配与验证结果。
ViT (Vision Transformer) 是 Google 提出的基于 Transformer 架构的视觉模型,vit-base-patch16-224-in21k 在 ImageNet-21k 数据集上预训练,可作为视觉特征提取 backbone 使用。本验证基于 PyTorch + torch_npu 方案,直接在昇腾 NPU 上跑通单图推理,并完成精度与性能评测。
相关获取地址:
| 组件 | 版本 |
|---|---|
transformers | 4.57.6 |
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
torchvision | 0.24.0 |
Pillow | 10.4.0 |
numpy | 1.26.4 |
Ascend910 × 2 逻辑卡/opt/atomgit/vit-base-patch16-224-in21kLinux 5.10.0 (aarch64)8.3.RC1# 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 指定可见 NPU 卡
export ASCEND_RT_VISIBLE_DEVICES=0
# 验证 torch_npu
python3 -c "import torch; import torch_npu; a = torch.randn(3,4).npu(); print(a + a)"git clone https://gitcode.com/hf_mirrors/google/vit-base-patch16-224-in21k.git
cd vit-base-patch16-224-in21k
# 若未安装 git-lfs,需先安装再拉取权重
# git lfs install && git lfs pull单设备推理(自动选择 NPU):
python3 inference.py --model_path . --num_runs 10CPU vs NPU 精度对比:
python3 inference.py --model_path . --compare --num_runs 10完整性能与精度评测:
python3 benchmark.py --model_path . --num_runs 20 --output benchmark_report.json模型路径: .
输入图片: /tmp/vit_test_image.png
PyTorch 版本: 2.9.0+cpu
torch_npu 版本: 2.9.0.post1+gitee7ba04
正在加载模型...
模型加载完成
============================================================
【CPU 基线推理】
============================================================
[CPU] Output keys: ['last_hidden_state', 'pooler_output']
[CPU] last_hidden_state shape: torch.Size([1, 197, 768]), dtype: torch.float32
[CPU] last_hidden_state mean: -0.018076, std: 0.199557
[CPU] pooler_output shape: torch.Size([1, 768]), dtype: torch.float32
[CPU] pooler_output mean: 0.012814, std: 0.417317
[CPU] 平均延迟: 639.725 ms (min=637.505, max=642.985)
============================================================
【NPU 推理】
============================================================
[NPU] Output keys: ['last_hidden_state', 'pooler_output']
[NPU] last_hidden_state shape: torch.Size([1, 197, 768]), dtype: torch.float32
[NPU] last_hidden_state mean: -0.018082, std: 0.199574
[NPU] pooler_output shape: torch.Size([1, 768]), dtype: torch.float32
[NPU] pooler_output mean: 0.012905, std: 0.417455
[NPU] 平均延迟: 6.220 ms (min=6.160, max=6.312)
============================================================
精度对比 (CPU vs NPU)
============================================================
last_hidden_state: max_diff=8.539950e-03, mean_diff=1.004748e-03, rel_err=6.323691e-03
pooler_output: max_diff=1.375449e-02, mean_diff=1.781710e-03, rel_err=5.030459e-03
------------------------------------------------------------
精度验证结果: PASS (relative_error < 1%)
============================================================
NPU 加速比 (vs CPU): 102.85x测试条件:单张 224×224 随机图像,连续 20 次推理,取第二次及以后数据。
| 指标 | CPU | NPU |
|---|---|---|
mean_ms | 647.168 ms | 6.041 ms |
min_ms | 644.242 ms | 5.695 ms |
max_ms | 649.640 ms | 6.316 ms |
p50_ms | 646.742 ms | 6.073 ms |
p99_ms | 649.594 ms | 6.299 ms |
std_ms | 1.286 ms | 0.165 ms |
throughput_img_per_sec | 1.55 | 165.54 |
| 加速比 | — | 107.13x |
使用 CPU 基线输出与 NPU 输出进行逐 tensor 对比,以 相对误差 < 1% 作为通过标准。
| Tensor | Shape | Max Diff | Mean Diff | Relative Error | 1% 通过 |
|---|---|---|---|---|---|
last_hidden_state | [1, 197, 768] | 8.54e-03 | 1.00e-03 | 0.63% | ✅ |
pooler_output | [1, 768] | 1.38e-02 | 1.78e-03 | 0.50% | ✅ |
精度验证结论:PASS(全部 tensor 相对误差 < 1%)
pytorch_model.bin 使用 Git LFS 托管,克隆后务必执行 git lfs pull 拉取实际权重(约 330 MB)。ViTModel(特征提取 backbone),无分类头。如需用于分类任务,需额外添加 nn.Linear 分类层并在下游数据集上微调。torch.npu.synchronize(),否则测得的延迟会偏小。| 文件 | 说明 |
|---|---|
inference.py | 单图推理脚本,支持 CPU/NPU 及精度对比 |
benchmark.py | 性能与精度评测脚本,输出 JSON 报告 |
benchmark_report.json | 本次验证的完整评测数据 |
inference_compare.log | CPU vs NPU 推理对比日志 |