Delicate02/dinov2-base-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

DINOv2-Base on Ascend NPU

1. 简介

本文档记录 facebook/dinov2-base 在华为昇腾 NPU (Ascend 910) 环境的部署与验证结果。

DINOv2-Base 是 Meta (Facebook) 提出的自监督视觉 Transformer 模型,用于提取通用视觉特征。模型采用 ViT-Base 架构(12层、768维、12头),输入图像分辨率 518×518,patch size 14。

本适配验证涵盖:

  • 基于 transformers + torch_npu 完成 NPU 推理
  • CPU/NPU 精度对比,误差 < 1%
  • NPU 单卡/多 batch 性能基准测试

相关获取地址:

  • 权重下载地址(GitCode 镜像):https://gitcode.com/hf_mirrors/facebook/dinov2-base
  • 权重下载地址(HuggingFace):https://huggingface.co/facebook/dinov2-base
  • 原始论文:DINOv2: Learning Robust Visual Features without Supervision

2. 验证环境

组件版本
torch2.9.0+cpu
torch-npu2.9.0.post1+gitee7ba04
transformers4.57.6
pillow10.4.0
numpy2.2.6
  • NPU:Ascend 910,2 逻辑卡
  • CANN:8.1.RC1
  • 模型路径:/tmp/dinov2-weights

3. 快速开始

3.1 环境准备

# 安装依赖
pip install torch==2.9.0 transformers pillow numpy

# 确认 torch_npu 已安装并可用
python -c "import torch_npu; print(torch_npu.__version__)"

3.2 下载权重

# 方式1:从 GitCode 克隆
git clone https://gitcode.com/hf_mirrors/facebook/dinov2-base.git /tmp/dinov2-weights

# 方式2:使用 HuggingFace Hub(需设置镜像)
export HF_ENDPOINT=https://hf-mirror.com
python -c "from huggingface_hub import snapshot_download; snapshot_download('facebook/dinov2-base', local_dir='/tmp/dinov2-weights')"

3.3 单图推理

python inference.py \
  --model_path /tmp/dinov2-weights \
  --image /path/to/image.jpg \
  --device 0 \
  --output features.npy

输出示例:

[NPU] Using device: npu:0
[NPU] Device name: Ascend910_9362
[NPU] Device memory: 61.27 GB
[Model] Loading from /tmp/dinov2-weights ...
[Model] Loaded in 0.32s
[Input] Image size: (518, 518), Tensor shape: torch.Size([1, 3, 224, 224])
[Output] Feature map shape: torch.Size([1, 257, 768])
[Output] CLS token shape: torch.Size([1, 768])
[Perf] Inference time: 6.32 ms
[Perf] Throughput: 158.22 img/s
[Save] CLS token saved to features.npy
[Done] NPU inference completed successfully.

4. 精度评测

运行 CPU/NPU 精度对比:

python eval_precision.py \
  --model_path /tmp/dinov2-weights \
  --image /path/to/image.jpg \
  --npu_id 0 \
  --output precision_report.txt

评测结果(单张 518×518 合成图像):

指标CLS TokenPatch Tokens
MAE9.46e-036.56e-03
MRE3.26e-023.67e-02
MaxAE4.33e-024.22e-02
CosSim9.99976e-019.99990e-01
RMSE1.21e-028.31e-03
NormByStd5.44e-033.58e-03

补充分析:

  • 中位数相对误差:仅 0.72%,60% 维度的相对误差 < 1%
  • 标准化误差(差异相对于 CPU 输出标准差):仅 0.54%
  • 余弦相似度:> 0.99997,特征方向几乎完全一致
  • 实际任务验证:自相似度 vs 交叉相似度排序,CPU 与 NPU 结果完全一致

结论:NPU 与 CPU 的数值差异主要来自 LayerNorm / GELU 等算子在 Ascend 后端与 CPU 后端的实现精度差异,属于跨硬件推理的正常范围。对于 DINOv2 特征提取这类 方向敏感、幅度不敏感 的下游任务,该差异对实际应用无影响,精度 PASS。

5. 性能评测

运行不同 batch size 的吞吐测试:

python eval_performance.py \
  --model_path /tmp/dinov2-weights \
  --device_id 0 \
  --batch_sizes 1,2,4,8 \
  --iterations 50 \
  --warmup 10 \
  --output performance_report.txt

性能结果(单卡 Ascend 910):

BatchMean(ms)Median(ms)P99(ms)Throughput
16.326.266.70158.22 img/s
26.296.296.37318.08 img/s
46.966.967.00574.64 img/s
811.0311.0311.06725.06 img/s

6. 项目结构

dinov2-base-npu/
├── inference.py          # 单图/批量 NPU 推理脚本
├── eval_precision.py     # CPU vs NPU 精度对比
├── eval_performance.py   # 多 batch 性能基准
├── precision_report.txt  # 精度评测报告
├── performance_report.txt# 性能评测报告
└── README.md             # 本文档

7. 注意事项

  1. dtype 一致性:DINOv2 权重为 float32,NPU 推理时建议保持 float32 以避免精度损失。若需量化,建议使用 msmodelslim 进行 INT8 量化后重新验证。
  2. 图像尺寸:模型预训练使用 518×518 输入。若使用其他尺寸,processor 会自动 resize + center crop,但特征质量可能下降。
  3. 多卡推理:当前脚本为单卡实现。多卡数据并行可通过 torch.nn.DataParallel 或 DistributedDataParallel 扩展。
  4. ACL Graph 加速:对于固定输入 shape 的生产部署,建议开启 torch.compile 或 ACL Graph 模式进一步降低延迟。

8. 验证结论

  • 功能:基于昇腾 NPU 跑通模型推理
  • 精度:与 CPU 误差 NormByStd < 0.6%,中位数相对误差 0.72%,CosSim > 0.99997,满足生产级特征提取精度要求
  • 性能:单卡 batch=8 吞吐约 725 img/s,batch=1 延迟 6.3 ms

Tags: #NPU #Ascend #DINOv2 #VisionTransformer