facebook/levit-128 是 Meta 提出的 LeViT(Vision Transformer)系列轻量级图像分类模型之一,参数量约 12M。本工程将其适配为可在单卡华为昇腾 910B NPU 上运行的推理提交工程。
注意: facebook/levit-128 的官方权重为 LevitForImageClassificationWithTeacher(蒸馏版本),模型输出包含 logits 和 distill_logits。本工程统一使用 outputs.logits 作为分类 logits,不使用 distill_logits。
| 项目 | 版本 |
|---|---|
| NPU 型号 | Ascend910B4 |
| CANN | 8.5.1 |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| Python | 3.11.14 |
pip install -r requirements.txt
# torch_npu 通常由昇腾容器预装,无需额外安装export ASCEND_RT_VISIBLE_DEVICES=0
export HF_ENDPOINT=https://hf-mirror.com
# 单图推理
python inference.py
# CPU vs NPU 精度对比
python eval_accuracy.py
# NPU 性能基准测试
python benchmark.pylogs/inference.log — 推理日志logs/prediction.txt — 预测结果logs/accuracy.log — 精度对比logs/benchmark.log — 性能测试torch.npu.is_available(): True
torch.npu.device_count(): 1
torch.npu.get_device_name(0): Ascend910B4| Rank | Label | Prob |
|---|---|---|
| 1 | nematode, nematode worm, roundworm (id=111) | 0.1281 |
| 2 | volcano (id=980) | 0.0553 |
| 3 | geyser (id=974) | 0.0458 |
| 4 | seashore, coast, seacoast, sea-coast (id=978) | 0.0228 |
| 5 | airship, dirigible (id=405) | 0.0176 |
注:测试图为随机下载的 picsum.photos 图片,非 ImageNet 类别图片,预测结果仅验证推理流程正确性。
| 指标 | 值 |
|---|---|
| avg | 19.23 ms |
| min | 18.12 ms |
| max | 23.07 ms |
| p50 | 18.30 ms |
| p90 | 23.07 ms |
| p95 | 23.07 ms |
| images/sec | 52.00 |
测试条件:Ascend910B4 单卡,batch_size=1,2 次预热 + 5 次正式测试。
| 指标 | 值 |
|---|---|
| max_abs_diff | 0.033165 |
| mean_abs_diff | 0.006578 |
| prob_max_diff | 0.000851 |
| Top-1 match | True |
| Top-5 match | False |
说明:此为 smoke accuracy / CPU-NPU 一致性验证,非官方 ImageNet 精度评测。Top-5 差异在 fp16/bf16 精度范围内属正常现象。
见 screenshots/self_verification.png。
logs/env_check.loglogs/inference.loglogs/accuracy.loglogs/benchmark.loglogs/prediction.txtLevitForImageClassificationWithTeacher(蒸馏版本),统一使用 outputs.logits 而非 distill_logitsLevitImageProcessor(AutoImageProcessor)做预处理,未手写伪预处理#NPU