DINO ViT-S/16 到昇腾 Ascend 910B3 的完整迁移适配
DINO/
├── README.md ← 本文件
├── LICENSE
├── requirements.txt ← Python 依赖
│
├── scripts/ ← NPU 适配脚本(可执行)
│ ├── ascend_train.py ← 单卡训练
│ ├── ascend_train_ddp.py ← 8卡 DDP 训练(torchrun + HCCL)
│ ├── ascend_eval_linear.py ← 线性评估
│ ├── ascend_export_onnx.py ← ONNX 导出
│ ├── ascend_onnx_benchmark.py
│ └── ascend_om_benchmark.py
│
├── src/ ← DINO 原始源码(Facebook Research)
│ ├── vision_transformer.py ← ViT 核心实现
│ ├── main_dino.py ← 主训练入口
│ ├── utils.py ← 工具函数
│ ├── eval_*.py ← 各下游任务评测脚本
│ ├── hubconf.py
│ ├── video_generation.py
│ └── visualize_attention.py
│
├── templates_cv/ ← 训练模板(NPU TimingCollector 等)
│ └── template_utils.py
│
├── docs/ ← 技术文档与分析
│ ├── DINO-NPU-适配指导书.md ← ⭐ 完整迁移指南(必读)
│ ├── DINO-NPU-迁移验证报告.md ← 验证报告
│ ├── adaptation_report.md
│ ├── adaptation_checklist.md
│ ├── dino-skill改进建议.md
│ ├── env_matrix.md
│ ├── dataset_manifest.json
│ ├── model_analysis.json
│ └── requirement_analysis.json
│
├── doc/ ← 原始 DINO 文档(上游)
│ └── dino_paper.pdf
│
└── artifacts/ ← 训练/推理产物(可直接使用)
├── npu_timing.json ← 单卡 timing(7字段)
├── ddp_timing.json ← 8卡 DDP timing(7字段)
├── distributed_env.json ← 分布式环境信息
├── single_vs_multicard.json
├── npu_loss_b1.log ← 单卡训练 loss
├── ddp_2epoch_loss.log ← DDP 训练 loss
├── npu_phase4_val_report.json
├── accuracy_summary.json
├── perf_summary.json
├── benchmark_results.json
├── dino_vit_small_backbone.onnx (83MB)
├── dino_vit_small_full.onnx (168MB, 需手动上传或重生成)
├── dino_vit_small_backbone.om (45MB)
└── dino_vit_small_full.om (88MB)完整指南:docs/DINO-NPU-适配指导书.md
# 环境
pip install torch==2.7.1 torchvision==0.22.1 torch_npu==2.7.1.post1
# 下载数据集(Tiny ImageNet-200)
curl -L -o tiny-imagenet-200.zip https://cs231n.stanford.edu/tiny-imagenet-200.zip
unzip -q tiny-imagenet-200.zip && mv tiny-imagenet-200 dataset
# 调整数据集结构
mkdir -p dataset/imagenet_data && ln -s ../train dataset/imagenet_data/train
# 单卡训练
cd scripts
python3 ascend_train.py \
--epochs 2 --batch_size_per_gpu 64 \
--warmup_epochs 0 \
--data_path ./dataset/imagenet_data/train/ \
--output_dir ./output_pretrain
# 8卡 DDP 训练
torchrun --nproc_per_node=8 scripts/ascend_train_ddp.py \
--epochs 2 --batch-size 64 --device-type npu \
--data-dir ./dataset/imagenet_data \
--output-dir ./output_ddp --num-classes 200
# 线性评估
python3 ascend_eval_linear.py \
--load-pretrained ./checkpoint.pth \
--data_dir ./dataset/tiny-imagenet-200 --epochs 90| 配置 | 每步延迟 | 每epoch | 吞吐量 | 加速比 |
|---|---|---|---|---|
| 单卡 NPU | 762ms | 1191s | 83.9 s/s | — |
| 8卡 DDP NPU | 53.7ms | 16.7s | 9530 s/s | 14× |
| 适配项 | 原代码 | NPU 适配 |
|---|---|---|
| 设备 API | torch.cuda.* | torch.npu.* / transfer_to_npu |
| 分布式后端 | backend="nccl" | backend="hccl" |
| 混合精度 | torch.cuda.amp | torch.npu.amp |
| checkpoint 加载 | torch.load | torch.load(weights_only=False) |
原始 DINO(Facebook Research):github.com/facebookresearch/dino
本文档为 DINO 的 NPU(Ascend 910B3)适配版本。完整指南请参见 docs/DINO-NPU-适配指导书.md。
适配版本: 2026-04-24 | CANN 8.5.0 | PyTorch 2.7.1 | Ascend 910B3 × 8