将 HuggingFace 图片分类模型 facebook/levit-384 适配为可在单卡昇腾 NPU(Ascend910B4)上运行的提交工程。
LevitForImageClassificationWithTeacheroutputs.logits 为学生模型分类 logits,outputs.teacher_logits 为教师模型输出(本适配未使用)| 项目 | 版本/信息 |
|---|---|
| NPU 型号 | Ascend910B4 |
| CANN 版本 | 8.5.1 |
| torch 版本 | 2.9.0+cpu |
| torch_npu 版本 | 2.9.0.post1+gitee7ba04 |
| transformers 版本 | 4.57.6 |
| Python 版本 | 3.11.14 |
pip install -r requirements.txt
# torch_npu 通常由昇腾容器提供,无需单独安装# 设置 HF 镜像(如需要)
export HF_ENDPOINT=https://hf-mirror.com
# 运行推理
python inference.pylogs/inference.loglogs/prediction.txttorch.npu.is_available(): True
NPU: Ascend910B4
模型类型: LevitForImageClassificationWithTeacher
说明: outputs.logits 为学生模型分类 logits(含 distillation head)
Top-1: coyote, prairie wolf, brush wolf, Canis latrans (0.9311)
Top-2: red wolf, maned wolf, Canis rufus, Canis niger (0.0526)
Top-3: timber wolf, grey wolf, gray wolf, Canis lupus (0.0068)
Top-4: grey fox, gray fox, Urocyon cinereoargenteus (0.0020)
Top-5: dhole, Cuon alpinus (0.0019)| 指标 | 值 |
|---|---|
| avg | 18.36 ms |
| min | 18.25 ms |
| max | 18.49 ms |
| p50 | 18.39 ms |
| p90 | 18.45 ms |
| p95 | 18.47 ms |
| images_per_sec | 54.47 |
CPU vs NPU 一致性验证(smoke accuracy,非官方 ImageNet 精度):
| 指标 | 值 |
|---|---|
| max_abs_diff | 1.637161e-03 |
| mean_abs_diff | 3.412865e-04 |
| prob_max_diff | 1.059175e-04 |
| Top-1 match | True |
| Top-5 match | True |
见 screenshots/self_verification.png
| 文件 | 说明 |
|---|---|
| logs/env_check.log | 环境检查日志 |
| logs/inference.log | 推理日志 |
| logs/prediction.txt | 预测结果 |
| logs/accuracy.log | 精度验证日志 |
| logs/benchmark.log | 性能测试日志 |
LevitForImageClassificationWithTeacher,包含蒸馏头。outputs.logits 为学生模型分类 logits,outputs.teacher_logits 为教师模型输出(本适配未使用)。所有日志和 README 中均已说明。LevitImageProcessor(slow processor),HuggingFace 建议未来版本可使用 fast processor#NPU