Model: MM-Grounding-DINO-Large-All (Swin-L + BERT 文本塔 + 跨模态融合) Target Hardware: Ascend 910B2 (CANN 8.5.0) Migration Scope: 推理 (Inference) + 训练 (DDP Fine-tune) Status: ✅ 迁移完成 Date: 2026-10-29
本项目将 OpenMMLab/MM-Grounding-DINO 的 Swin-L pretrain_all 变体(开放词汇目标检测模型)从 CPU/GPU 迁移到 华为昇腾 Ascend 910B2 NPU,完成端到端的训练 + 推理 + ONNX/OM 导出全流程验证。
模型特点:
grounding_dino_swin-l_pretrain_all-56d69e78.pth(1.5GB,含 13 个数据集预训练知识)| 评测项 | 基准 | NPU/OM 结果 | 偏差 | 结论 |
|---|---|---|---|---|
| COCO val2017 zero-shot mAP | 60.3 (论文) | 60.4 (NPU fp32) | +0.1 | ✅ 对齐 |
| RefCOCO/+/g 全量评测 | 官方 | 6 测试集全跑 | - | ✅ 通过 |
| PascalVOC 训练后 mAP | - | 75.30 (NPU DDP 混合精度, 15 epoch, epoch 12 best) | - | ✅ 收敛 |
| NPU vs GPU 训练对齐 (15 epoch) | - | mAP -0.53%, loss +0.93% | - | ✅ 对齐 |
| OM force_fp16 vs PyTorch fp32 (单档 800×1067) | 0.7463 (PyTorch) | 0.7460 (简化后 OM) | -0.0003 | ✅ 对齐 |
| 路径 | dtype | 纯推理 (ms) | 端到端 (ms) | 吞吐 (fps) | mAP |
|---|---|---|---|---|---|
| OM (force_fp16, 简化后,交付主路径) | fp16 | 632.4 | 662.4 | 1.51 | 0.7460 |
| OM (force_fp16, 未简化) | fp16 | 798.5 | 829.6 | 1.21 | 0.7459 |
简化后 OM 比未简化版推理快 20.8%(632.4ms vs 798.5ms),精度无损(mAP 0.7460 vs 0.7459,+0.0001)。交付主路径用简化后 OM。
| 项 | 配置 | 结果 |
|---|---|---|
| NPU 4 卡 DDP finetune | 15 epoch, 混合精度 | mAP 0.741→0.7530 (epoch 12 best) ✅ |
| 训练精度对齐 | NPU vs GPU 15 epoch | mAP -0.53%, loss +0.93% ✅ |
mm_grounding_dino_large_all/
├── README.md # 本文件
├── 迁移指导书.md / 验证报告.md / 案例集.md / 问题集.md / 案例_padding精度劣化定位.md / 案例_AOE调优进程导致OM精度暴跌.md / GPU训练指导.md # 交付文档
├── 给作者的修改清单.md # 复现问题与修改清单
│
└── code/
├── mmdet_npu_patch.diff # NPU 适配 patch(改 grounding_dino.py / text_transformers.py / pretrain_all.py)
├── mmdet_gpu_patch.diff # GPU 适配 patch
├── scripts/ # 全部脚本 + 配置(命令需加 code/scripts/ 前缀)
│ ├── requirements.txt # 依赖清单(pip install --dry-run 逐项校验)
│ ├── single_op_align.py # 单算子精度对比(NPU vs CPU, float32)
│ ├── run_train_npu.py # NPU 训练启动脚本(含 weights_only patch)
│ ├── gen_fewshot_train_500.py # fewshot 训练子集生成脚本(500 张, seed=42)
│ ├── loss_curve_compare_npu_vs_gpu.py # NPU vs GPU loss 曲线对比脚本
│ ├── export_onnx_full.py # ONNX 完整导出 wrapper(被 planA_export.py import)
│ ├── planA_export.py # 固定档静态 ONNX 导出(交付主路径)
│ ├── verify_onnx_inference.py # ONNX 推理验证(ORT vs PyTorch)
│ ├── eval_pytorch_npu_single_gear.py # PyTorch NPU 单档评测(fp32)
│ ├── eval_om_single_gear_static.py # OM 单档静态评测(force_fp16)
│ ├── onnx_postprocess.py # ONNX 后处理(bbox 解码 + sigmoid)
│ ├── test_npu.py # NPU 评测 wrapper(import torch_npu + 调 mmdet tools/test.py)
│ └── configs/mm_grounding_dino/pascalvoc/ # 训练配置(需复制到 code/mmdetection/configs/)
│ ├── grounding_dino_swin-l_npu_ddp_full.py # NPU DDP 全量微调(13690 张, 15 epoch)
│ └── grounding_dino_swin-l_gpu_baseline.py # GPU baseline 训练(约束4 对齐基准)
│
└── mmdetection/ # 官方 mmdetection v3.3.0(git clone,核心代码不修改)
└── configs/mm_grounding_dino/grounding_dino_swin-l_pretrain_all.py # 基座配置(官方自带)交付物(位于仓库根目录):
迁移指导书.md — 完整迁移步骤、关键技术、避坑指南验证报告.md — 精度/性能验证数据、profiling 分析案例集.md — 18 个迁移案例(含 dynamic shape 冲突、onnxsim 精度根因等)问题集.md — 25 个问题(每问题 8 节标准格式)案例_padding精度劣化定位.md — padding 精度劣化定位案例案例_AOE调优进程导致OM精度暴跌.md — AOE 调优进程导致 OM 精度暴跌案例GPU训练指导.md — GPU baseline 训练指导(约束4 对齐基准)给作者的修改清单.md — 复现问题与修改清单pip install --no-build-isolation,约束 <2.2.0)git clone -b v3.3.0,核心代码不修改;NPU 训练配置在 code/scripts/configs/,复制到 mmdetection 即可)完整依赖见 requirements.txt(已逐项 pip install --dry-run 校验)。
# 关键安装命令
pip install torch==2.7.1 torch-npu==2.7.1.post2
pip install --no-build-isolation "mmcv>=2.1.0,<2.2.0"
pip install mmengine mmdet transformers pycocotools# 1.1 ONNX 导出 (固定档, 训练后权重, 30195 节点)
cd /mm_grounding_dino_large_all
ASCEND_RT_VISIBLE_DEVICES="" HF_HUB_OFFLINE=1 \
PYTHONPATH=code/scripts:$PYTHONPATH \
python3 code/scripts/planA_export.py
# 输出: /tmp/finetuned_npu_onnx.onnx (1357MB, 30195 节点)
# 1.2 onnxsim 简化 (交付主路径必做, 节点 -86%, OM 推理快 20.8%)
python3 -m onnxsim /tmp/finetuned_npu_onnx.onnx /tmp/finetuned_npu_onnx_sim.onnx
# 输出: /tmp/finetuned_npu_onnx_sim.onnx (201MB) + /tmp/finetuned_npu_onnx_sim.onnx.data (1423MB)
# 1.3 ATC 转 OM (force_fp16, topo0)
source /usr/local/Ascend/cann-8.5.0/set_env.sh
atc --model=/tmp/finetuned_npu_onnx_sim.onnx --framework=5 --soc_version=Ascend910B2 \
--input_shape="img:1,3,800,1067;input_ids:1,51;attention_mask:1,51,51;position_ids:1,51;token_type_ids:1,51;text_token_mask:1,51" \
--precision_mode=force_fp16 \
--topo_sorting_mode=0 \
--output=/tmp/planA_oms/finetuned_om_force_fp16_topo0 \
--log=error
# 输出: /tmp/planA_oms/finetuned_om_force_fp16_topo0.om (866MB)
# 1.4 OM 评测 (mAP + 性能)
ASCEND_RT_VISIBLE_DEVICES=0 \
OM_PATH=/tmp/planA_oms/finetuned_om_force_fp16_topo0.om \
DEVICE=0 \
python3 code/scripts/eval_om_single_gear_static.py --n 1475cd /mm_grounding_dino_large_all
# NPU 4 卡 DDP 全量微调 (13690 张, 产出 ONNX/OM 导出用的权重)
ASCEND_RT_VISIBLE_DEVICES=0,1,2,3 \
torchrun --nproc_per_node=4 \
code/scripts/run_train_npu.py \
code/mmdetection/configs/mm_grounding_dino/pascalvoc/grounding_dino_swin-l_npu_ddp_full.py \
--launcher pytorch全模型 unsupported_op_count=0,无算子缺失。训练/推理前必须先扫描,验证管线跑通才能继续(约束1)。
head_inputs_dict['memory_text'](encoder 跨模态融合后),不是 text_dict['embedded'](原始 BERT)# 必须 import 触发链式注册,否则 MODELS.build(cfg.model) 失败
from export_onnx_full import precompute_text_inputs, GroundingDINOFullOnnxWrapperCANN 8.5.0 grid_sample_tiling.cpp:118 硬编码只支持 FLOAT32/FLOAT16,GroundingDINO 有 60 个 GridSample 算子无法绕过。OM 必须用 force_fp16。
Swin 的 AdaptivePadding 动态 padding 机制与 ATC 的 Case 动态 shape 编译架构性冲突:
dynamic_image_size 编译 32 分钟失败dynamic_image_size 编译 32 分钟成功| # | 问题 | 根因 | 方案/状态 |
|---|---|---|---|
| 1 | BOOL SelectV2 降级 AI_CPU | ONNX Where(BOOL mask) 在 ATC lower 为 AI_CPU 实现(20.5ms/个×6=123ms) | customize_dtypes 改 dtype 会破坏计算语义(实证输出全零),不可用 |
| 2 | EOFError (customize_dtypes 触发) | TBE 算子知识库 GlobalMgr 子进程崩溃,customize_dtypes 改 dtype 触发 KB search | 不影响 OM 生成(ATC_EXIT=0),但 customize_dtypes 方案已弃用 |
| 3 | 竖图(H>W)旋转方案不可用 | 旋转 90° 影响 mAP(实证 max_score 差 1.4%-52.2%) | OM 原生支持竖图(区间模式 OM H>800 验证 6/6 PASS) |
| 4 | mmcv 2.2.0 与 mmdet 3.3.0 不兼容 | API 变更 | 约束 mmcv<2.2.0 |
详见 问题集.md(25 个问题,每问题 8 节标准格式)和 案例集.md(18 个案例)。
| 数据集 | 用途 | 规模 |
|---|---|---|
| COCO val2017 | 训前推理对齐 (zero-shot) | 5000 张 |
| COCO train2017 | 训练 | 118K 张 |
| RefCOCO/+/g | 跨模态评测 | 57457 张 (6 测试集) |
| PascalVOC (ODinW) | 训练后 mAP + 性能对比 | 3422 valid (713 竖图 20.8%) |
约束3:全部使用真实官方数据集 + 真实预训练权重,禁合成数据/随机初始化。
| 约束 | 内容 | 状态 |
|---|---|---|
| 约束1 | 算子扫描 + 训推管线闭环(COCO + RefCOCO) | ✅ PASS |
| 约束2 | requirements.txt 逐项 dry-run 校验 | ✅ PASS |
| 约束3 | 真实数据集 + 真实权重 | ✅ PASS |
| 约束4 | 精度 vs 基准,正偏差 >1 返工 | ✅ PASS (COCO +0.1, NPU vs GPU mAP -0.53%/loss +0.93%) |
| 约束5 | 不跳过硬门禁 | ✅ PASS |
@article{liu2023groundingdino,
title={Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection},
author={Liu, Shilong and Zeng, Zhaoyang and Ren, Tianhe and Li, Feng and Zhang, Hao and Yang, Jie and Li, Chunyuan and Yang, Jianwei and Su, Hang and Zhu, Jun and Zhang, Lei},
journal={arXiv preprint arXiv:2303.05499},
year={2023}
}本项目模型代码遵循 Apache 2.0 License。
基座权重 grounding_dino_swin-l_pretrain_all 遵循 OpenMMLab 的使用条款。