本文档记录 facebook/dinov3-vitl16-pretrain-lvd1689m 在 Ascend NPU 环境的推理适配与验证结果。该模型为 Meta AI 发布的 Vision Transformer 基础模型(ViT-L/16 变体),基于 transformers 库直接加载推理,通过 runtime monkey-patch 方式将 torch.cuda 调用透明转发至 torch.npu,无需修改原始模型代码即可完成 NPU 适配。
相关获取地址:
参考文档:
推荐使用以下任一方式下载模型权重:
方式一:ModelScope 下载(推荐)
modelscope download --model facebook/dinov3-vitl16-pretrain-lvd1689m --local_dir /opt/atomgit/weight/dinov3-vitl16-pretrain-lvd1689m方式二:AtomGit 下载
python3 -m atomgit download hf_mirrors/facebook/dinov3-vitl16-pretrain-lvd1689m -d /opt/atomgit/weight/dinov3-vitl16-pretrain-lvd1689m在运行验证脚本前,请确保已安装以下依赖:
pip install torch==2.9.0+cpu transformers
pip install torch-npu==2.9.0.post1torch 与 torch-npu 版本需与当前 CANN 版本匹配transformers 用于加载模型与图像预处理pip install modelscope| 组件 | 版本 |
|---|---|
Python | 3.11.14 |
PyTorch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
CANN | 8.0.RC2 |
2 逻辑卡(Ascend910)/opt/atomgit/weight/dinov3-vitl16-pretrain-lvd1689m运行单图特征提取推理,验证模型在 NPU 上的基础前向传播能力。
推理使用的样本图片:

python3 inference.py验证输出:
[INFO] Applied NPU monkey-patch (torch.cuda -> torch.npu)
[INFO] Loading image from: /opt/atomgit/dinov3-vitl16-pretrain-lvd1689m/data/val2017/000000000139.jpg
[INFO] Loading model from: /opt/atomgit/weight/dinov3-vitl16-pretrain-lvd1689m
[INFO] Model moved to NPU device: Ascend910_9362
[INFO] Input shape: torch.Size([1, 3, 224, 224])
[INFO] Device: npu:0
[INFO] Warm-up inference...
[INFO] Running timed inference...
============================================================
Inference Results
============================================================
Device: NPU (Ascend910_9362)
Latency: 15.67 ms
Pooled output shape: torch.Size([1, 1024])
Last hidden state shape: torch.Size([1, 201, 1024])
Pooled output dtype: torch.float32
Pooled output device: npu:0
Pooled output first 5 values: [-0.07554872 -0.60289943 -0.48666883 -0.6598409 -0.29380554]
============================================================验证结论:
224x224 图像推理延迟约 15.67 ms[1, 1024],last_hidden_state [1, 201, 1024])使用 benchmark.py 对 NPU 推理进行延迟与吞吐量压测:
python3 benchmark.py| 指标 | 数值 |
|---|---|
iterations | 20 |
mean_ms | 15.10 ms |
stddev_ms | 0.03 ms |
min_ms | 15.04 ms |
max_ms | 15.15 ms |
p50_ms | 15.11 ms |
p90_ms | 15.15 ms |
p99_ms | 15.15 ms |
| batch_size | throughput_ips | avg_latency_ms |
|---|---|---|
1 | 65.31 | 15.31 |
2 | 130.52 | 15.32 |
4 | 198.48 | 20.15 |
8 | 258.96 | 30.89 |
验证结论:
~15 ms,约为 ViT-B/16 的 2 倍,符合参数量增长预期258.96 images/sec使用 accuracy.py 对 NPU 输出与 CPU 基线进行精度对比,评估指标为向量级相对误差与余弦相似度:
python3 accuracy.py验证输出:
============================================================
Accuracy Validation Results
============================================================
pooler_output:
Shape: (1, 1024)
Vector Relative Error: 0.008812 (PASS)
Cosine Similarity: 0.999961 (PASS)
MSE: 0.0000138399
Max Absolute Diff: 0.013645
Overall: PASS
last_hidden_state:
Shape: (1, 201, 1024)
Vector Relative Error: 0.007322 (PASS)
Cosine Similarity: 0.999973 (PASS)
MSE: 0.0000041440
Max Absolute Diff: 0.045191
Overall: PASS
============================================================
OVERALL: PASS (vector-level relative error < 1% and cosine similarity > 0.999)
============================================================| 指标 | 数值 | 阈值 | 结果 |
|---|---|---|---|
| Pooler Output 向量相对误差 | 0.88% | < 1% | PASS |
| Pooler Output 余弦相似度 | 0.999961 | > 0.999 | PASS |
| Last Hidden State 向量相对误差 | 0.73% | < 1% | PASS |
| Last Hidden State 余弦相似度 | 0.999973 | > 0.999 | PASS |
验证结论:
< 1%> 0.999,特征空间一致性高Monkey-patch 适配方式:当前采用运行时 torch.cuda -> torch.npu monkey-patch 实现适配,无需修改 transformers 源码。若后续 transformers 版本内部硬编码了 CUDA 特定调用,可能需要更新 patch 范围。
TORCH_COMPILE_DISABLE:脚本中已设置 TORCH_COMPILE_DISABLE=1,避免 torch.compile 在 NPU 上引发兼容问题。
输入尺寸约束:DINOv3 ViT-L/16 的 patch size 为 16,输入图像尺寸建议为 224x224 或 16 的整数倍,否则模型会自动 crop 到最接近的倍数。
ViT-L 与 ViT-B 差异:ViT-L 的 embedding dimension 为 1024(ViT-B 为 768),模型权重约 1.2 GB,推理延迟约为 ViT-B 的 2 倍,实际部署时需根据精度与性能需求权衡选择。
输出产物:inference.py、benchmark.py、accuracy.py 的运行结果均保存至 output/ 目录,包含 JSON 结构化数据与文本日志,便于后续自动化采集。