resososi/dinov2-large
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

facebook/dinov2-large on Ascend NPU

1. Model Info

ItemValue
Modelfacebook/dinov2-large
ArchitectureViTModel
Taskimage-feature-extraction
HuggingFacehttps://huggingface.co/facebook/dinov2-large
Parameters304M
Input Size224x224
Hidden Size1024
Attention Heads16
Hidden Layers24
Patch Size14

2. Environment

Hardware

ItemValue
NPUAscend 910B4
Count1
ArchitectureAscend

Software

ItemVersion
OSUbuntu 22.04.5 LTS (aarch64)
CANN8.5.1
Python3.11
PyTorch2.9.0
torch_npu2.9.0.post1
transformers4.57.6

3. Adaptation

Code Changes

FileChange
src/infer.pyNPU inference and precision verification
src/benchmark.pyBenchmark entry point
inference.pyAdded NPU device support

Method

  • Use torch.npu instead of torch.cuda for device management
  • Load model via from_pretrained then migrate to NPU
  • Use torch.npu.synchronize() for accurate timing
  • Precision verification via shared state_dict between CPU and NPU models

4. Precision Verification

Method

  1. Load model on CPU, get state_dict
  2. Create same model on NPU, load CPU state_dict
  3. Run inference on both with same input, compare outputs

Results

MetricValue
Cosine Similarity0.999970
Precision Error %0.0030%
StatusPASS (< 1%)

5. Performance

Config

ItemValue
Warmup3 rounds
Benchmark runs10
Input shape[1, 3, 224, 224]

Results

MetricValue
NPU Latency (ms)12.50
Peak HBM (GB)1.200

6. Inference Code

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. Conclusion

CheckStatus
NPU ForwardPASS
Output ShapeCorrect [1, 257, 1024]
PerformanceAvailable
PrecisionCosine=0.999970
Code UsableYes

8. Appendix

One-click Archive

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

Run Benchmark

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