d
dingdang666/vit_small_patch16_dinov3.lvd1689m-NPU
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

vit_small_patch16_dinov3.lvd1689m on Ascend NPU

1. 简介

本文档记录 vit_small_patch16_dinov3.lvd1689m 在昇腾 NPU(Ascend 910B3)环境的适配部署与精度验证结果。

DINOv3 ViT-Small(vit_small_patch16_dinov3)是一种基于 Vision Transformer 的视觉特征提取模型,参数量约 21.6M,输出 384 维图像特征向量,可通过 DINOv3 ViT-7B 教师模型蒸馏获得。该模型适用于图像分类、检索、分割等下游任务。本项目完成该模型在昇腾 NPU 上的推理适配,并验证 NPU 与 CPU 推理结果的精度误差 < 1%。

相关地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/timm/vit_small_patch16_dinov3.lvd1689m
  • 适配代码仓库:https://gitcode.com/dingdang666/vit_small_patch16_dinov3.lvd1689m-NPU

2. 验证环境

组件版本
Python3.11.x
PyTorch2.10.0+cpu
torch_npu2.10.0
torchvision0.25.0+cpu
timm1.0.27
safetensors0.7.0
CANN8.5.1
NPU 驱动25.5.2
NPU 硬件Ascend 910B3 (8卡)
操作系统Linux (aarch64)

3. 模型信息

项目值
模型架构ViT-Small/16 (DINOv3)
参数量~21.6M
特征维度384
Layers12
Attention Heads6
Patch Size16
输入尺寸3×256×256
输出特征形状(1, 384)
权重格式safetensors / pytorch_model.bin
原始框架PyTorch (timm)
预训练数据LVD-1689M
许可证DINOv3 License

4. Conda 环境安装

创建独立的 conda 环境并安装依赖(使用华为云 PyPI 镜像加速):

# 创建 conda 环境
conda create -n vit_small_patch16_dinov3 python=3.11 -y

# 激活环境
conda activate vit_small_patch16_dinov3

# 安装 PyTorch 及相关依赖(华为云镜像)
pip install torch==2.10.0 torchvision==0.25.0 --index-url https://repo.huaweicloud.com/repository/pypi/simple/

# 安装 torch_npu(根据环境选择对应版本)
pip install torch_npu==2.10.0 --index-url https://repo.huaweicloud.com/repository/pypi/simple/

# 安装 timm 及其他依赖
pip install timm safetensors pillow --index-url https://repo.huaweicloud.com/repository/pypi/simple/

如果 HuggingFace 网络不通,设置镜像:

export HF_ENDPOINT=https://hf-mirror.com/

权重文件可从 HuggingFace 下载:

export HF_ENDPOINT=https://hf-mirror.com/
huggingface-cli download timm/vit_small_patch16_dinov3.lvd1689m --local-dir /path/to/model_dir

5. 推理执行

单张图像推理

# NPU 推理(默认)
python3 inference.py \
    --model_path /path/to/vit_small_patch16_dinov3.lvd1689m \
    --image_path /path/to/image.jpg \
    --device npu:0

# CPU 推理
python3 inference.py \
    --model_path /path/to/vit_small_patch16_dinov3.lvd1689m \
    --image_path /path/to/image.jpg \
    --device cpu

精度与性能评测

python3 benchmark.py \
    --model_path /path/to/vit_small_patch16_dinov3.lvd1689m \
    --npu_device npu:0

评测结果日志将输出到 log.txt。

6. 参数说明

inference.py 参数

参数说明默认值
--model_path模型权重路径(包含 config.json, model.safetensors)必需
--image_path输入图像路径必需
--device运行设备,可选 npu:0 或 cpunpu:0
--weight_file权重文件名,可选 model.safetensors 或 pytorch_model.binmodel.safetensors
--no_warmup跳过 NPU 预热阶段False

benchmark.py 参数

参数说明默认值
--model_path模型权重路径必需
--image_path测试图像路径(默认自动生成)自动生成
--npu_deviceNPU 设备 IDnpu:0
--num_warmupNPU 预热轮数3

7. 精度评测结果

评测方法

使用同一张测试图像分别在 CPU(FP32)和 NPU(FP32)上运行推理,对比输出特征向量的差异。

评价指标说明

指标含义判定阈值
向量级相对误差`
余弦相似度NPU 与 CPU 输出向量间的方向一致性> 0.99
最大/平均绝对误差逐元素差值统计,反映数值偏差的绝对量级—

精度结果

输出张量向量级相对误差余弦相似度最大绝对误差平均绝对误差
features0.225044%0.99999755622.38e-036.31e-04

判定结论

指标实测值阈值状态
向量级相对误差0.23%< 1%PASS
余弦相似度0.999998> 0.99PASS

8. 性能数据

操作耗时
CPU 推理时间(FP32)0.77s
NPU 推理时间(FP32,3轮预热后)0.02s
加速比 (CPU / NPU)45.45x

9. 注意事项

  1. 首次加载:首次加载模型权重会从本地读取 safetensors/bin 文件,耗时约 1-2 秒。
  2. NPU 预热:NPU 首次推理包含编译优化,通常需要 1-2 轮预热才能达到稳定性能。脚本默认开启 3 轮预热。
  3. 精度:本模型使用 FP32 精度,NPU 与 CPU 输出误差极小(远小于 1%),不影响下游任务效果。
  4. 内存占用:NPU 推理约占用 1-2GB 显存。
  5. 权重文件:模型权重(.safetensors/.bin)不包含在适配仓库中,需单独下载。
  6. timm 模型:本模型基于 timm 库,需通过 timm.create_model() 创建架构后加载本地权重。DINOv3 的 QKV bias 在 timm 中默认为 False(与原始权重一致)。