AdelaiDet / BlendMask 实例分割模型在 Ascend 910B3 NPU 上的推理优化。
R_101_3x,ResNet-101 + FPN 骨干| 项目 | 详情 |
|---|---|
| CANN | 8.5.0 |
| NPU | Ascend 910B3(使用 NPU 1) |
| CPU | 鲲鹏 920,192 核 |
| torch / torch_npu | 2.8.0 / 2.8.0.post2 |
| Python | 3.9.25(conda: ascend_blendmask) |
| 配置文件 | configs/BlendMask/R_101_3x.yaml |
| 权重 | weights/R_101_3x.pth(211 MB,634 tensors) |
AdelaiDet_workspace/
├── inference.py # 最终推理脚本(P0~P4)
├── benchmark.py # 性能测试(P0~P4)
├── accuracy_check.py # 精度验证(CPU vs NPU)
├── README.md # 总览
├── deploy_guide.md # 环境部署指南(复现必读)
├── ascend_npu_optimization_skill.md # NPU 优化方法论 Skill
│
├── AdelaiDet/ # 模型仓库
│ ├── configs/BlendMask/R_101_3x.yaml
│ ├── weights/R_101_3x.pth # 模型权重(211 MB)
│ ├── output_vis.jpg # 可视化输出示例
│
├── baseline/ # 原始 baseline 分析
│ ├── AdelaiDet_survey.md # 仓库调研报告
│ ├── profiling_summary.md # 瓶颈分析(11 阶段 profiling)
│ ├── env_check_result.md # 环境检查
│ ├── weight_check_result.md # 权重验证
│ ├── inference_baseline.py # baseline 推理
│ ├── benchmark_baseline.py # baseline 性能测试
│ └── accuracy_check_baseline.py # baseline 精度测试
│
├── p0_resize/ # P0:NPU 侧 resize 优化
│ ├── benchmark_p0_resize.py # P0 性能测试
│ ├── accuracy_check_p0_resize.py # P0 精度验证
│ └── benchmark_compare_resize.md # P0 对比报告(−33.4%)
│
├── p1_decode/ # P1:decode 优化 + NMS 细拆
│ ├── benchmark_p1_decode.py # P1 性能测试
│ ├── accuracy_check_p1_decode.py # P1 精度验证
│ ├── benchmark_compare_decode.md # P1 对比报告
│ ├── benchmark_p1_nms.py # NMS 细拆 profiling
│ └── proposal_detail_profile.md # NMS 分析结论(1.57ms,不值得优化)
│
├── p2_runtime_tuning/ # P2:运行时调优扫参
├── runtime_tuning_compare.md # 扫参对比报告
├── runtime_best_config.sh # 最佳运行时配置
└── result_tq_*.json # TASK_QUEUE_ENABLE 扫参数据
├── p3_compile/ # P3:backbone torch.compile(npu)
│ ├── benchmark_compile_vs_eager.py
│ ├── accuracy_compile_vs_eager.py
│ └── compile_validation_notes.md
├── p4_pad64_recompile/ # P4:pad64 + recompile_limit=32
│ ├── p4_pad64.py # pad64 性能测试
│ ├── p4_accuracy.py # pad64 精度验证
│ └── p4_recompile32_result.json
│
├── P5_val2017test/ # P5:全量 val2017 性能 + COCO 精度评估
│ ├── benchmark_eval_full.py # P5 完整评估脚本
│ ├── coco_predictions.json # COCO 格式预测结果 (14.9 MB)
│ ├── p5_full_report.json # 性能 + 精度汇总报告
│ └── per_image_timing.jsonl # 逐图延迟明细
│
├── doc/ # 测试日志与结果
│ ├── benchmark_log.txt
│ ├── benchmark_result.json
│ ├── accuracy_log.txt
│ └── accuracy_result.json| 版本 | 延迟 | P50 | P90 | P99 | 吞吐 | 提升 |
|---|---|---|---|---|---|---|
| 官方 (1080Ti) | ~91ms | — | — | — | ~11 im/s | — |
| CPU 推理 | ~26s | — | — | — | 0.04 im/s | — |
| NPU 优化前 | 144.0ms | 143.0ms | 150.5ms | 193.8ms | 6.9 im/s | — |
| NPU 优化后 (P4) | 39.5ms | 39.3ms | 42.0ms | 47.5ms | 25.3 im/s | −72.6% |
| P5 全量 val2017 (NPU 2) | 40.4ms | 40.3ms | 43.7ms | 49.0ms | 14.8 im/s¹ | — |
¹ P5 的 14.8 im/s 含 mask RLE 编码和预测提取开销(纯 forward ~14.8-15.0)。 P4 在 NPU 1 上测 200 张重复图;P5 在 NPU 2 上测全量 5000 张唯一图并含完整后处理。
测试日志(pad64 + recompile_limit=32):
Avg=39.52ms P50=39.33ms P90=42.00ms P99=47.45ms Thr=25.31 im/s
| 对比项 | Box IoU | Score Diff | 说明 |
|---|---|---|---|
| CPU vs NPU 优化前 | 0.9999 | 0.00003 | 相同预处理,NPU 推理与 CPU 完全一致 |
| CPU vs NPU 优化后 | 0.9972 | 0.0014 | NPU resize (bilinear) 与 CPU resize (OpenCV) 插值方式不同 |
| NPU 优化前 vs 优化后 | 0.9974 | 0.0018 | 同上 |
使用 P0-P4 优化模型 + TASK_QUEUE_ENABLE=2,在 NPU 2 上对完整 5000 张 COCO val2017 进行端到端推理并计算标准 COCO AP 指标。
推理总耗时 12.4 分钟,产生 27,180 个预测框。
| 指标 | P5 (NPU 2) | 官方 BlendMask R_101_3x | 差异 |
|---|---|---|---|
| AP | 38.0 | 38.4 | −0.4 |
| AP50 | 51.6 | 58.0 | −6.4 |
| AP75 | 42.3 | 41.6 | +0.7 |
| APs | 19.2 | 21.5 | −2.3 |
| APm | 41.6 | 41.9 | −0.3 |
| APl | 53.3 | 49.3 | +4.0 |
| 指标 | P5 (NPU 2) | 官方 BlendMask R_101_3x | 差异 |
|---|---|---|---|
| AP | 34.2 | 34.2 | 0.0 ✅ |
| AP50 | 50.7 | 55.2 | −4.5 |
| AP75 | 37.8 | 36.2 | +1.6 |
| APs | 16.1 | 14.7 | +1.4 |
| APm | 37.5 | 37.2 | +0.3 |
| APl | 49.7 | 48.3 | +1.4 |
结论:Segm AP 与官方完全一致(34.2),BBox AP 差异 0.4 在 P0 NPU resize (bilinear vs OpenCV) 的预期误差范围内。NPU 推理精度可完全替代 CPU/GPU 推理。
延迟分布:77.3% 在 40-50ms,21.8% 在 30-40ms,0.3% 为 compile recompilation spike。
| 阶段 | 优化内容 | 延迟 | 相对提升 | 累计提升 |
|---|---|---|---|---|
| Baseline | 原始 NPU 推理 | 144.0ms | — | — |
| P0 Resize | CPU resize → NPU resize | 95.9ms | −33.4% | −33.4% |
| P1 Decode | 移除 .cpu() 同步点 | 79.6ms | −17.0% | −44.7% |
| P2 Runtime | TQ=2 + NUMA 绑核 + OMP=1 | 62.5ms | −21.5% | −56.6% |
| P3 Compile | backbone torch.compile(npu) | 51.5ms | −17.6% | −64.2% |
| P4 Pad64 | pad64 + recompile_limit=32 | 39.5ms | −23.3% | −72.6% |
ResizeShortestEdge.apply_image() 在 CPU 上耗时 34.8msF.interpolate(mode='bilinear') 在 NPU 上实现等价缩放select_over_all_levels() 中 post-NMS topk 的 .cpu() 同步点torch.compile(backbone, backend="npu", dynamic=False) 编译骨干网络recompile_limit=32 覆盖全部 shape,消除 compile fallback所有命令需在 AdelaiDet/ 目录下执行(config 和 weight 为相对路径)。
conda activate ascend_blendmask
source /usr/local/Ascend/ascend-toolkit/set_env.sh
cd <workspace>/AdelaiDet
# 推理(输出可视化)
taskset -c 144-167 python ../inference.py \
--input /path/to/input.jpg \
--output ../result.jpg
# 性能测试(200 张,NPU 1)
taskset -c 144-167 python ../benchmark.py
# 精度测试(CPU vs NPU,50 张)
python ../accuracy_check.py
# P5 全量评估 — 性能 + COCO 精度(5000 张,NPU 2)
taskset -c 96-119 python ../benchmark_eval_full.py
# P5 快速验证(20 张,调试用)
taskset -c 96-119 python ../benchmark_eval_full.py --max-images 20首次运行会触发 torch.compile 编译预热(~200s),后续推理无需等待。