本文档记录 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 推理相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
pillow | 10.4.0 |
numpy | 2.2.6 |
Ascend 910,2 逻辑卡8.1.RC1/tmp/dinov2-weights# 安装依赖
pip install torch==2.9.0 transformers pillow numpy
# 确认 torch_npu 已安装并可用
python -c "import torch_npu; print(torch_npu.__version__)"# 方式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')"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.运行 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 Token | Patch Tokens |
|---|---|---|
| MAE | 9.46e-03 | 6.56e-03 |
| MRE | 3.26e-02 | 3.67e-02 |
| MaxAE | 4.33e-02 | 4.22e-02 |
| CosSim | 9.99976e-01 | 9.99990e-01 |
| RMSE | 1.21e-02 | 8.31e-03 |
| NormByStd | 5.44e-03 | 3.58e-03 |
补充分析:
0.72%,60% 维度的相对误差 < 1%0.54%> 0.99997,特征方向几乎完全一致结论:NPU 与 CPU 的数值差异主要来自 LayerNorm / GELU 等算子在 Ascend 后端与 CPU 后端的实现精度差异,属于跨硬件推理的正常范围。对于 DINOv2 特征提取这类 方向敏感、幅度不敏感 的下游任务,该差异对实际应用无影响,精度 PASS。
运行不同 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):
| Batch | Mean(ms) | Median(ms) | P99(ms) | Throughput |
|---|---|---|---|---|
| 1 | 6.32 | 6.26 | 6.70 | 158.22 img/s |
| 2 | 6.29 | 6.29 | 6.37 | 318.08 img/s |
| 4 | 6.96 | 6.96 | 7.00 | 574.64 img/s |
| 8 | 11.03 | 11.03 | 11.06 | 725.06 img/s |
dinov2-base-npu/
├── inference.py # 单图/批量 NPU 推理脚本
├── eval_precision.py # CPU vs NPU 精度对比
├── eval_performance.py # 多 batch 性能基准
├── precision_report.txt # 精度评测报告
├── performance_report.txt# 性能评测报告
└── README.md # 本文档float32,NPU 推理时建议保持 float32 以避免精度损失。若需量化,建议使用 msmodelslim 进行 INT8 量化后重新验证。518×518 输入。若使用其他尺寸,processor 会自动 resize + center crop,但特征质量可能下降。torch.nn.DataParallel 或 DistributedDataParallel 扩展。torch.compile 或 ACL Graph 模式进一步降低延迟。NormByStd < 0.6%,中位数相对误差 0.72%,CosSim > 0.99997,满足生产级特征提取精度要求725 img/s,batch=1 延迟 6.3 msTags: #NPU #Ascend #DINOv2 #VisionTransformer