timm/repvit_m1_5.dist_450e_in1ktimm/repvit_m1_5.dist_450e_in1k 模型在昇腾 NPU 环境下的图像分类推理验证,并与 CPU 推理结果进行误差对比。本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 timm/repvit_m1_5.dist_450e_in1k 图像分类模型在 Ascend NPU 环境下的适配验证。项目基于 PyTorch、timm 和 torch-npu,实现模型加载、测试图片生成、CPU 推理、NPU 推理、Top-5 分类输出、CPU/NPU 输出一致性对比以及验证材料整理。
repvit_m1_5.dist_450e_in1k 是 timm 模型库中的 ImageNet-1k 图像分类模型。模型输入一张 RGB 图像后输出 1000 维 ImageNet 分类 logits,并进一步得到 Top-5 分类结果。
本次适配内容包括:
repvit_m1_5.dist_450e_in1k;test.jpg;.
├── README.md
├── adaptation_report.md
├── download_model.sh
├── make_test_image.py
├── inference.py
├── compare_cpu_npu.py
├── make_report.py
├── requirements.txt
├── test.jpg
├── hf_model/
│ ├── config.json
│ └── model.safetensors
├── cpu_result.json
├── cpu_result.txt
├── cpu_infer.log
├── npu_result.json
├── npu_result.txt
├── npu_infer.log
├── compare_result.txt
├── compare_metrics.json
├── compare.log
├── fusion_result.json
├── npu_env.txt
└── screenshots/
├── npu_env.png
├── npu_result.png
└── compare_result.png其中:
README.md:项目说明文档;adaptation_report.md:Ascend NPU 适配报告;download_model.sh:模型下载脚本;make_test_image.py:测试图片生成脚本;inference.py:CPU/NPU 推理脚本;compare_cpu_npu.py:CPU/NPU 输出误差对比脚本;make_report.py:报告或结果整理脚本;requirements.txt:Python 依赖文件;test.jpg:测试图片;hf_model/config.json:模型配置文件;hf_model/model.safetensors:模型权重文件;cpu_result.json:CPU 推理结构化结果;cpu_result.txt:CPU 推理日志;cpu_infer.log:CPU 推理补充日志;npu_result.json:NPU 推理结构化结果;npu_result.txt:NPU 推理日志;npu_infer.log:NPU 推理补充日志;compare_result.txt:CPU/NPU 对比文本结果;compare_metrics.json:CPU/NPU 对比结构化指标;compare.log:CPU/NPU 对比运行日志;fusion_result.json:汇总结果文件;npu_env.txt:NPU 环境信息;screenshots/:验证截图材料。在 Ascend NPU Notebook 中执行以下命令检查运行环境:
npu-smi info
python --version
python - <<'PY'
import torch
import timm
print("torch:", torch.__version__)
print("timm:", timm.__version__)
try:
import torch_npu
print("torch_npu import success")
print("npu available:", torch.npu.is_available())
except Exception as e:
print("torch_npu import failed:", repr(e))
PY环境信息保存为:
npu_env.txt环境检查截图保存为:
screenshots/npu_env.png该截图用于证明当前运行环境存在 Ascend NPU,并记录 NPU 型号、运行状态和 Python 版本信息。
本项目使用 Hugging Face / timm 模型:
timm/repvit_m1_5.dist_450e_in1k本地模型文件位于:
hf_model/config.json
hf_model/model.safetensors推理日志中显示:
checkpoint: hf_model/model.safetensors说明本次验证使用本地 Hugging Face 权重文件完成模型加载与推理。
本项目使用测试图片:
test.jpg推理流程包括:
输入张量形状为:
[1, 3, 224, 224]运行:
python inference.py --device cpu --image test.jpg --output cpu_result.json 2>&1 | tee cpu_result.txtCPU 推理输出文件:
cpu_result.json
cpu_result.txt
cpu_infer.logCPU 推理日志摘要如下:
model: timm/repvit_m1_5.dist_450e_in1k
device: cpu
torch: 2.9.0+cpu
timm: 1.0.27
checkpoint: hf_model/model.safetensors
input_shape: [1, 3, 224, 224]
output_shape: [1, 1000]
avg_latency_ms: 467.8799CPU Top-5 输出如下:
rank 1: class_index=111, prob=0.14002095, logit=7.00545454
rank 2: class_index=644, prob=0.13577750, logit=6.97467995
rank 3: class_index=409, prob=0.04388503, logit=5.84523582
rank 4: class_index=818, prob=0.02927318, logit=5.44033432
rank 5: class_index=326, prob=0.02859800, logit=5.41699934运行:
python inference.py --device npu --image test.jpg --output npu_result.json 2>&1 | tee npu_result.txtNPU 推理输出文件:
npu_result.json
npu_result.txt
npu_infer.logNPU 推理日志摘要如下:
model: timm/repvit_m1_5.dist_450e_in1k
device: npu
torch: 2.9.0+cpu
timm: 1.0.27
checkpoint: hf_model/model.safetensors
input_shape: [1, 3, 224, 224]
output_shape: [1, 1000]
avg_latency_ms: 34.2422NPU Top-5 输出如下:
rank 1: class_index=111, prob=0.13956892, logit=7.01538944
rank 2: class_index=644, prob=0.13759053, logit=7.00111294
rank 3: class_index=409, prob=0.04359126, logit=5.85168743
rank 4: class_index=818, prob=0.02909385, logit=5.44735765
rank 5: class_index=326, prob=0.02884225, logit=5.43867207NPU 推理结果截图保存为:
screenshots/npu_result.png运行:
python compare_cpu_npu.py 2>&1 | tee compare_result.txt或:
python compare_cpu_npu.py 2>&1 | tee compare.log对比脚本会读取 CPU 与 NPU 的 logits 和 probability 输出,并计算:
对比结果保存为:
compare_result.txt
compare_metrics.json
compare.log
fusion_result.json本次 timm/repvit_m1_5.dist_450e_in1k 适配验证的 CPU/NPU 误差结果如下:
| 指标 | 结果 |
|---|---|
| 类别数 | 1000 |
| Top-K | 5 |
| CPU Top-5 | [111, 644, 409, 818, 326] |
| NPU Top-5 | [111, 644, 409, 818, 326] |
| CPU/NPU Top-1 是否一致 | True |
| CPU/NPU Top-5 顺序是否一致 | True |
| CPU/NPU Top-5 集合是否一致 | True |
| logits 最大绝对误差 | 0.0747933388 |
| logits 平均绝对误差 | 0.0074124314 |
| logits 余弦相似度 | 0.9999870135 |
| probability 最大绝对误差 | 0.0018130243 |
| probability 平均绝对误差 | 0.0000107833 |
| probability 余弦相似度 | 0.9999324711 |
| CPU 平均推理耗时 | 467.8799 ms |
| NPU 平均推理耗时 | 34.2422 ms |
| 是否通过建议验证 | True |
对应的 compare_result.txt 内容如下:
CPU/NPU comparison for Ascend NPU adaptation
model: timm/repvit_m1_5.dist_450e_in1k
cpu_result: cpu_result.json
npu_result: npu_result.json
num_classes: 1000
top_k: 5
Top-k consistency:
CPU top5: [111, 644, 409, 818, 326]
NPU top5: [111, 644, 409, 818, 326]
top1_same: True
top5_order_same: True
top5_set_same: True
Numerical metrics:
logit_max_abs: 0.0747933388
logit_mean_abs: 0.0074124314
logit_cosine_similarity: 0.9999870135
probability_max_abs: 0.0018130243
probability_mean_abs: 0.0000107833
probability_cosine_similarity: 0.9999324711
Latency:
CPU avg latency ms: 467.8799
NPU avg latency ms: 34.2422
suggested_pass: True根据上述结果,CPU 与 NPU 的 Top-1 结果一致,Top-5 顺序完全一致,Top-5 集合一致。logits 余弦相似度达到 0.9999870135,probability 最大绝对误差为 0.0018130243,probability 余弦相似度为 0.9999324711。同时,NPU 平均推理耗时为 34.2422 ms,CPU 平均推理耗时为 467.8799 ms,验证结果为 suggested_pass: True。因此,本次 timm/repvit_m1_5.dist_450e_in1k 昇腾 NPU 推理验证通过。

该截图展示 Ascend NPU Notebook 环境、npu-smi info 输出和 Python 版本信息。

该截图展示 NPU 推理日志,包括模型名称、运行设备、输入张量形状、输出张量形状、平均推理耗时和 Top-5 分类结果。

该截图展示 CPU/NPU 输出误差对比结果,包括 Top-K 一致性、logits 误差、probability 误差、余弦相似度、推理耗时和建议验证结论。
本项目提交材料包括:
README.mdadaptation_report.mddownload_model.shmake_test_image.pyinference.pycompare_cpu_npu.pymake_report.pyrequirements.txttest.jpghf_model/config.jsonhf_model/model.safetensorscpu_result.jsoncpu_result.txtcpu_infer.lognpu_result.jsonnpu_result.txtnpu_infer.logcompare_result.txtcompare_metrics.jsoncompare.logfusion_result.jsonnpu_env.txtscreenshots/npu_env.pngscreenshots/npu_result.pngscreenshots/compare_result.png适配报告可查看:
adaptation_report.mdCPU 推理日志可查看:
cpu_result.txt
cpu_infer.logNPU 推理日志可查看:
npu_result.txt
npu_infer.logCPU/NPU 误差对比结果可查看:
compare_result.txt
compare_metrics.json
compare.log本项目的适配工作包括:
repvit_m1_5.dist_450e_in1k 模型;hf_model/model.safetensors;本项目完成了 timm/repvit_m1_5.dist_450e_in1k 模型在 Ascend NPU 环境下的图像分类推理适配验证。
验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和 Top-5 分类输出。CPU 与 NPU 的 Top-1 结果一致,Top-5 顺序完全一致,logits 余弦相似度达到 0.9999870135,probability 最大绝对误差为 0.0018130243,probability 余弦相似度为 0.9999324711,并通过建议验证。同时,本次测试中 NPU 平均推理耗时低于 CPU。本项目可作为赛道一模型适配提交材料。