Yanguan/webssl-dino7b-full8b-224
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

webssl-dino7b-full8b-224 在昇腾 NPU 上的部署

1. 简介

本文档记录 facebook/webssl-dino7b-full8b-224 在昇腾 NPU 上的适配与验证结果。

webssl-dino7b-full8b-224 是 Meta 发布的 DINOv2 大型视觉 Transformer 模型(约 70 亿参数,patch size 14),基于 WebSSL 数据集预训练,输入分辨率为 224×224。该模型适用于图像特征提取、视觉表示学习等下游任务。

  • 模型仓库(HuggingFace):https://huggingface.co/facebook/webssl-dino7b-full8b-224
  • 模型仓库(ModelScope):https://modelscope.cn/models/facebook/webssl-dino7b-full8b-224

2. 验证环境

组件版本
torch2.9.0+cpu
torch-npu2.9.0.post1+gitee7ba04
transformers>=4.30.0
Pillow>=9.0.0
numpy1.26.4
  • NPU:Ascend910
  • 模型路径:/opt/atomgit/weight/webssl-dino7b-full8b-224
  • 数据样本:sample_image.jpg(COCO val2017 真实样本,已包含在仓库中)

数据样本详细说明

本仓库用于推理验证的样本图片为 MS COCO 2017 验证集中的 000000000139.jpg。该图片在仓库中以 sample_image.jpg 命名提交,与 COCO val2017 原图 MD5 完全一致(a0204aa65acc51cd8ffc128e5e94a05c),尺寸 640 × 426,RGB,JPEG,约 158 KB。由于 data/ 目录被 .gitignore 排除(避免提交大型数据集),因此将单张样本图以副本形式提交到根目录,保证克隆后即可直接运行。

3. 环境依赖安装

pip install torch==2.9.0 transformers Pillow numpy

注:本环境已预装 torch-npu,若在新环境部署,请参考 CANN 官方安装指南 安装对应版本的 CANN 驱动与固件。

4. 模型权重下载

AtomGit 下载

python3 -m atomgit download hf_mirrors/facebook/webssl-dino7b-full8b-224 -d /opt/atomgit/weight/webssl-dino7b-full8b-224

下载完成后,目录结构应为:

/opt/atomgit/weight/webssl-dino7b-full8b-224/
├── config.json
├── model.safetensors.index.json
├── model-00001-of-00006.safetensors
├── model-00002-of-00006.safetensors
├── model-00003-of-00006.safetensors
├── model-00004-of-00006.safetensors
├── model-00005-of-00006.safetensors
└── model-00006-of-00006.safetensors

5. 推理验证(inference.py)

运行命令

python3 inference.py

运行说明

  • 脚本通过 model.to("npu") 将模型加载到 NPU 执行推理
  • 使用 monkey-patch 方式适配 NPU,不修改原始 transformers 库代码
  • 输入样本为 COCO val2017 真实图片(000000000139.jpg),非随机生成

运行结果

[INFO] Applied NPU monkey-patch (torch.cuda -> torch.npu)
[INFO] Loading image from: /opt/atomgit/webssl-dino7b-full8b-224/sample_image.jpg
[INFO] Loading model from: /opt/atomgit/weight/webssl-dino7b-full8b-224
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model.
[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:       85.35 ms
Pooled output shape:   torch.Size([1, 4096])
Last hidden state shape: torch.Size([1, 257, 4096])
Pooled output dtype:   torch.float32
Pooled output device:  npu:0
Pooled output first 5 values: [-0.37430924  0.15931404 -1.0104568  -0.57187915  0.12003752]
============================================================

[INFO] Results saved to: /opt/atomgit/webssl-dino7b-full8b-224/output/inference_result.json
[INFO] Log saved to: /opt/atomgit/webssl-dino7b-full8b-224/output/inference_log.txt

推理产物保存在 output/ 目录下:

  • inference_result.json:结构化推理结果(含输出张量形状、样本值、延迟等)
  • inference_log.txt:推理日志文本

6. 性能评测(benchmark.py)

运行命令

python3 benchmark.py

运行说明

  • 延迟测试:5 次预热 + 20 次正式测试,统计平均值 / 第 50 百分位数 / 第 90 百分位数 / 第 99 百分位数
  • 吞吐测试:分别在批大小为 1、2、4 的情况下测量每秒处理图像数量

运行结果

[INFO] Applied NPU monkey-patch (torch.cuda -> torch.npu)
[INFO] Loading image from: /opt/atomgit/webssl-dino7b-full8b-224/sample_image.jpg
[INFO] Loading model from: /opt/atomgit/weight/webssl-dino7b-full8b-224
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model.
[INFO] Model moved to NPU: Ascend910_9362
[INFO] Benchmarking latency (5 warmup + 20 timed)...

============================================================
Latency Benchmark Results
============================================================
Device:      NPU (Ascend910_9362)
Iterations:  20
Mean:        85.27 ms
StdDev:      0.03 ms
Min:         85.21 ms
Max:         85.33 ms
P50:         85.27 ms
P90:         85.32 ms
P99:         85.33 ms
============================================================
[INFO] Benchmarking throughput for batch sizes: [1, 2, 4]
  batch=1: 11.73 images/sec, avg_latency=85.26 ms
  batch=2: 14.23 images/sec, avg_latency=140.58 ms
  batch=4: 14.57 images/sec, avg_latency=274.54 ms

============================================================
Throughput Benchmark Results
============================================================
Batch= 1:   11.73 img/s | avg_latency=  85.26 ms
Batch= 2:   14.23 img/s | avg_latency= 140.58 ms
Batch= 4:   14.57 img/s | avg_latency= 274.54 ms
============================================================

[INFO] Results saved to /opt/atomgit/webssl-dino7b-full8b-224/output

性能产物保存在 output/ 目录下:

  • benchmark_result.json:结构化性能结果(延迟分布、吞吐数据)
  • benchmark_log.txt:性能日志文本

性能参考汇总

指标数值
平均延迟85.27 ms
50%分位延迟85.27 ms
90%分位延迟85.32 ms
99%分位延迟85.33 ms
吞吐量(批大小=1)11.73 img/s
吞吐量(批大小=2)14.23 img/s
吞吐量(批大小=4)14.57 img/s

7. 精度验证(accuracy.py)

运行命令

python3 accuracy.py

运行说明

  • 以 CPU 推理结果作为基线(baseline)
  • 对比 NPU 推理结果与 CPU 基线的向量相对误差和余弦相似度
  • 通过阈值:向量相对误差 < 1%,余弦相似度 > 0.999

运行结果

[INFO] Applied NPU monkey-patch (torch.cuda -> torch.npu)
[INFO] Loading image from: /opt/atomgit/webssl-dino7b-full8b-224/sample_image.jpg
[INFO] Loading model from: /opt/atomgit/weight/webssl-dino7b-full8b-224
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model.

[INFO] Running CPU baseline inference...
[INFO] CPU baseline done.

[INFO] Running NPU inference...
[INFO] NPU inference done.

============================================================
Accuracy Validation Results
============================================================

pooler_output:
  Shape:                  (1, 4096)
  Vector Relative Error:  0.000302 (PASS)
  Cosine Similarity:      1.000000 (PASS)
  MSE:                    0.0000000393
  Max Absolute Diff:      0.000776
  Overall:                PASS

last_hidden_state:
  Shape:                  (1, 257, 4096)
  Vector Relative Error:  0.002019 (PASS)
  Cosine Similarity:      0.999997 (PASS)
  MSE:                    0.0000020290
  Max Absolute Diff:      0.068947
  Overall:                PASS

============================================================
OVERALL: PASS (vector-level relative error < 1% and cosine similarity > 0.999)
============================================================

[INFO] Results saved to /opt/atomgit/webssl-dino7b-full8b-224/output

精度验证产物保存在 output/ 目录下:

  • accuracy_result.json:结构化精度对比结果
  • accuracy_log.txt:精度日志文本

精度指标汇总

输出项向量相对误差余弦相似度MSE结论
pooler_output0.0003021.0000000.0000000393PASS
last_hidden_state0.0020190.9999970.0000020290PASS

8. 注意事项

  1. monkey-patch 适配:本脚本通过运行时 monkey-patch(torch.cuda -> torch.npu)实现 NPU 兼容,未修改 transformers 原始库代码,升级 transformers 版本时通常无需重新适配。
  2. torch.compile 禁用:由于 CPU 环境不支持 torch.compile,脚本已通过环境变量 TORCH_COMPILE_DISABLE=1 显式禁用,避免潜在兼容性问题。
  3. 输入样本来源:推理与精度验证使用的图片均来自 COCO val2017 公开数据集,确保验证结果可复现且符合真实场景。
  4. NPU 日志目录警告:若运行时出现 [LOG_WARNING] can not create directory, directory: /home/atomgit/ascend/log,属于 Ascend 驱动日志目录未创建的提示,不影响推理结果,可忽略。
  5. 权重路径:脚本默认读取 /opt/atomgit/weight/webssl-dino7b-full8b-224,请根据实际下载路径修改 inference.py、benchmark.py、accuracy.py 中的 model_path 变量。
  6. 模型体积:本模型约 7B 参数,权重文件分为 6 个 safetensors 分片,总大小约 21 GB,加载至 NPU 需要较大显存。