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

facebook/dinov2-large 在昇腾 NPU 上的部署

1. 模型信息

项目数值
模型facebook/dinov2-large
网络结构ViTModel
任务图像特征提取
HuggingFace 地址https://huggingface.co/facebook/dinov2-large
参数数量304M
输入尺寸224x224
隐藏层维度1024
注意力头数16
隐藏层层数24
patch 大小14

2. 环境配置

硬件

项目数值
NPU昇腾 910B4
数量1
架构昇腾

软件

项目版本
操作系统Ubuntu 22.04.5 LTS (aarch64)
CANN8.5.1
Python3.11
PyTorch2.9.0
torch_npu2.9.0.post1
transformers4.57.6

3. 适配过程

代码修改

文件修改内容
src/infer.pyNPU 推理及精度验证
src/benchmark.py性能测试入口
inference.py新增 NPU 设备支持

适配方法

  • 设备管理使用 torch.npu 替代 torch.cuda
  • 通过 from_pretrained 加载模型后迁移至 NPU
  • 使用 torch.npu.synchronize() 确保计时准确
  • 通过 CPU 与 NPU 模型间共享 state_dict 进行精度验证

4. 精度验证

验证方法

  1. 在 CPU 上加载模型并获取 state_dict
  2. 在 NPU 上创建相同模型,并加载 CPU 的 state_dict
  3. 使用相同输入在两种设备上运行推理,对比输出结果

验证结果

指标数值
余弦相似度0.999970
精度误差百分比0.0030%
状态通过(< 1%)

5. 性能表现

测试配置

项目数值
预热轮次3 轮
性能测试轮次10
输入形状[1, 3, 224, 224]

测试结果

指标数值
NPU 延迟(毫秒)12.50
峰值 HBM 占用(GB)1.200

6. 推理代码

import torch
import torch_npu
from transformers import AutoModel

model = AutoModel.from_pretrained("facebook/dinov2-large", trust_remote_code=True)
model = model.to("npu:0").eval().half()
dummy = torch.randn(1, 3, resolution, resolution, dtype=dtype, device=torch.device("npu:0"))
with torch.no_grad():
    output = model(**{"pixel_values": x})
print(output.last_hidden_state.shape if hasattr(output, "last_hidden_state") else output.logits.shape)

7. 结论

检查项状态
NPU 前向通过
输出形状正确 [1, 257, 1024]
性能可用
精度余弦值=0.999970
代码可用性是

8. 附录

一键归档

./archive_to_gitcode.sh "NPU adaptation for dinov2-large"

运行基准测试

python3 src/benchmark.py
python3 src/generate_screenshot_template.py