ascend-yolov10m-plus:基于 Ascend NPU 的 YOLOv10-M 推理优化项目

可用于在 Ascend 910 硬件上实现高效目标检测,显著提升推理速度。项目通过 pipeline 优化、Rust 加速等技术,将单卡推理吞吐量从 45 img/s 提升至 439 img/s,实现 100x CPU 加速,同时保持较高检测精度。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
4 months ago

YOLOv10-M Ascend NPU 推理优化 (Plus)

上游仓库: ascend-yolov10m — 完成了 45→439 img/s 的 Pipeline 优化突破

本仓库 在上游基础上继续攻坚:DVPP+AIPP 全NPU流水线、Rust 全链路重构、硬件利用率 99%+、HiFloat8/4 低精度推理

上游成果 (ascend-yolov10m)

详见 上游仓库

指标 值
吞吐量 (峰值) 439.3 img/s (单次,无 D2H)
吞吐量 (3-run 中位数) 420.5 img/s
mAP50-95 50.10% (论文 51.1%)
mAP50 69.73% (超过论文 68.1%)
E2E 推理时间 11.55s / 5000 images
硬件利用率 95.9%
CPU 对比 1164s → 11.55s,100x 加速

本仓库增量工作

模块 状态 说明
DVPP + AIPP 全NPU流水线 ✅ 已合入 CPU预处理→全部NPU,H2D -88%,FP32精度 50.69% mAP
Rust NMS 后处理 ✅ 已合入 后处理 1.5ms→0.25ms,30.9x 加速
AIPP OM 模型 ✅ 已生成 NV12/RGB 双版本
全链路 Rust Pipeline 🔲 规划中 ACL FFI + 零拷贝 + 三级流水线
硬件利用率 99%+ 🔜 下一目标 当前 95.9%,华为自研加速模块
HiFloat8 / HiFloat4 🔜 探索 低精度浮点格式

关键分支

分支 吞吐 mAP 说明
pod/agent-xss2/perf-search 439 img/s 50.10% 突破分支 — pipeline 优化
pod/agent-xss2/rust-opt 420 img/s 50.10% Rust NMS 加速
pod/agent-xss/rust-opt 392 img/s 50.14% 最完整 Rust (9函数)

突破关键: commit 5862934 — pre-computed blobs + pinned memory + async pipeline


Rust 加速模块

三个版本保存在 rust_accel/ 下:

版本 目录 行数 函数数 依赖
v1 v1_xss2_perf_search/ 131 2 pyo3 0.21, numpy, rayon
v2 v2_xss2_rust_opt/ 299 4 pyo3 0.21, numpy, rayon
v3 (当前) src/lib.rs 392 9 pyo3 0.28, fast_image_resize, rayon, half
后处理方式 单张耗时 加速比
Python NMS 1.5ms 1x
Rust (release) 0.25ms 30.9x

DVPP + AIPP 全 NPU 流水线

分支: feat/dvpp-integration

将预处理从 CPU 全部迁移到 NPU,消除 CPU 瓶颈:

现状 (CPU 预处理):
  JPEG → cv2.imdecode(3ms) → cv2.resize(0.5ms) → copyMakeBorder → BGR2RGB → normalize → H2D(4.9MB) → OM
                                                        总计: ~4-6ms/img, H2D: 4.9MB

目标 (DVPP + AIPP):
  JPEG → H2D(原图) → JPEGD(NPU解码) → VPC(NPU resize+pad) → AIPP(YUV2RGB+归一化) → OM
                                                        总计: ~0ms CPU, H2D: <1MB

三种流水线对比

流水线 预处理 H2D 大小 CPU 开销
CPU + FP32 OM cv2 全在 CPU 4.9MB FP32 4-6ms/img
DVPP + host RGB + FP32 OM DVPP 解码+resize, CPU 转 RGB 4.9MB FP32 ~0.3ms/img
DVPP + AIPP FP32 OM 全 NPU 0.6MB NV12 0ms/img

AIPP OM 模型转换

# 生成 AIPP 模型 (NV12 输入, 内嵌 YUV2RGB + 归一化)
bash code/convert_fp32_aipp.sh

# 生成的模型:
#   yolov10m_fp32_aipp_nv12.om — 输入: NV12 uint8, AIPP 做 YUV→RGB + /255
#   yolov10m_fp32_aipp_rgb.om  — 输入: RGB uint8, AIPP 做 /255

运行 DVPP benchmark

python3 code/benchmark_dvpp_aipp.py   # 三种流水线对比 + COCO mAP 评测

目录结构

ascend-yolov10m-plus/
├── code/                    # Python 推理脚本
│   ├── archive/             #   历史版本链 (breakthrough_v2~v21, benchmark_v1~v8)
│   ├── benchmark_v9.py      # ★ 上游突破脚本 (439 img/s)
│   ├── release_benchmark.py # ★ 3-run release benchmark
│   ├── benchmark_dvpp_aipp.py  # ★ DVPP + AIPP 全NPU流水线
│   ├── benchmark_dvpp_fp32.py  # DVPP + FP32 benchmark
│   ├── convert_fp32_aipp.sh    # FP32 + AIPP OM 转换
│   ├── eval_accuracy.py        # mAP 评测
│   └── final_pipeline.py       # 最终 pipeline
├── rust_accel/              # Rust 加速模块
│   ├── src/lib.rs           # 当前版 (v3, 392行, 9函数)
│   ├── Cargo.toml / pyproject.toml
│   ├── v1_xss2_perf_search/ # 439 img/s (2函数)
│   ├── v2_xss2_rust_opt/    # 420 img/s (4函数)
│   └── v3_xss_rust_opt/     # 392 img/s (9函数)
├── models/                  # OM 模型
│   ├── yolov10m.onnx              # 原始 ONNX (64MB)
│   ├── yolov10m_fp32_aipp_nv12.om # ★ DVPP+AIPP: NV12输入
│   ├── yolov10m_fp32_aipp_rgb.om  # ★ DVPP+AIPP: RGB输入
│   ├── yolov10m_fp16_input_fp16_420img.om  # 上游冠军模型
│   └── om_variants/         # ATC 参数搜索变体
├── configs/                 # ATC 配置文件
├── docs/
│   └── DVPP_FP32_OPTIMIZATION.md
├── results/                 # benchmark 结果 JSON
├── logs/                    # 运行日志
└── BRANCH_REGISTRY.md       # 分支演化记录

OM 模型转换

SoC 版本 (踩坑)

ATC 报 "Conv2D operator not registered" 的根因是 SoC 版本错误。

  • ❌ --soc_version=Ascend910B — 通用代号,不匹配具体板卡
  • ✅ --soc_version=Ascend910_9362 (Board ID: 0x73) — 精确板卡版本

硬件变更分析: Ascend910_9362 (Board ID 0x73)

本仓库从 Ascend910B 通用环境迁移至 Ascend910_9362 (800I A2 服务器)。

硬件规格

参数 值
产品型号 IT22HMDA_4_S
NPU 芯片 2× Ascend910, 各 64GB HBM
Board ID 0x73 (SoC: Ascend910_9362)
主板 ID 0x14, PCB B, BOM 2
固件版本 25.5.2
CANN 8.5.1
CPU Kunpeng aarch64, 229GB RAM
PCIe Vendor 0x19E5, Device 0xD803

性能影响

维度 Ascend910_9362 (当前) 通用 Ascend910B 影响
算子兼容性 精确匹配板卡,全部算子可用 通用匹配,部分算子缺失 ✅ 解决 "Conv2D not registered"
融合策略 无专用策略文件,使用默认 ascend910b1~b4 有专用策略 ⚠️ AutoFuse 需手工配置
HiFloat8 910_95 系列支持 HF8 910_95 系列支持 HF8 ✅ 一致
Float4 910_95 系列支持 910_95 系列支持 ✅ 一致
AOE 调优 板卡级调优,效果更精确 通用调优 ✅ 性能更优
双芯能力 2 芯片可并行 取决于具体板卡 🔜 潜在 2x 吞吐

适配要点

  1. ATC 转换必须用 Ascend910_9362 — 不可用 Ascend910B 等通用名
  2. 算子调优更精细 — AOE OPAT 会根据 0x73 板卡的具体特性选择 kernel
  3. 融合策略需定制 — CANN 8.5.1 内置策略文件针对 ascend910b1~b4,对 9362 板卡建议用 --fusion_switch_file 手工配置
  4. 双芯片未利用 — 当前仅用 ASCEND_RT_VISIBLE_DEVICES=0,第二颗芯片 (phy-id 3) 闲置

FP32 ATC 转换 (推荐)

source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_KERNEL_META_LOCAL_DIR=/tmp/kernel_meta

atc --framework=5 \
    --model=yolov10m.onnx \
    --output=yolov10m_fp32 \
    --input_format=NCHW \
    --input_shape="images:1,3,640,640" \
    --output_type=FP32 \
    --soc_version=Ascend910_9362

模型对比

模型 吞吐 mAP50-95 说明
fp16_input_fp16_420img.om 420 img/s 50.14% 冠军方案 (pipeline)
fp16_output_fp16_50.69mAP.om 268.5 img/s 50.69% 精度最高
fp32.om 88.75 img/s 50.69% FP32 裸推理
fp16.om (基线) 78.29 img/s 0.0% ❌ 精度崩溃

下一步优化: 全链路 Rust Pipeline

详细方案见 docs/DVPP_FP32_OPTIMIZATION.md

优化目标

目标 当前 (FP16 pipeline) DVPP+AIPP (已完成) 目标 (全链路 Rust)
吞吐量 420.5 img/s 待测 400+ img/s
mAP50-95 50.10% 50.69% (+0.59pp) 50.69%
预处理 CPU cv2 (4-6ms/img) DVPP 硬件 (0ms CPU) DVPP + Rust
H2D 传输 4.9MB FP32 blob 0.6MB NV12 0.6MB NV12
运行时 Python 胶水 + GIL Python 胶水 + GIL Rust 全链路 (零拷贝)

1. DVPP + AIPP 流水线 ✅ 已完成

JPEG → H2D → JPEGD(NPU解码) → VPC(NPU resize+pad) → AIPP(YUV2RGB+归一化) → FP32 OM → 输出
  • 全程 NPU,CPU 零参与预处理
  • H2D 传输量 -88% (4.9MB → 0.6MB)
  • FP32 精度 50.69% mAP (当前最高)
  • AIPP OM 模型已生成: yolov10m_fp32_aipp_nv12.om
  • Benchmark 脚本: code/benchmark_dvpp_aipp.py

2. 全链路 Rust Pipeline

现状: Rust预处理 → Python bytes → ACL推理 → Python bytes → Rust后处理
      ↑ 2次拷贝 + GIL ↑              ↑ 2次拷贝 ↑

目标: [Rust 预处理] → pinned mem → H2D → execute_async → D2H → [Rust 后处理]
      ↑ 零拷贝,无 Python ↑
      三级流水线: 预处理 ‖ 推理 ‖ 后处理

C ABI 接口 (供 EFI 调用)

void* yolo_infer_init(const char* om_model_path);
int   yolo_infer_one(void* ctx, const uint8_t* jpeg, size_t len,
                     float orig_w, float orig_h,
                     float conf_thresh, float iou_thresh,
                     CocoDetection* out, int out_max);
void  yolo_infer_destroy(void* ctx);

实施阶段

阶段 内容 工作量
P0 ACL FFI 绑定 (acl_ffi.rs) 0.5 天
P1 InferenceContext (init / load / alloc / cleanup) 1 天
P2 零拷贝集成 (预处理→pinned→H2D,D2H→后处理) 0.5 天
P3 双缓冲 pipeline (2套 device buffer 交替) 1 天
P4 三级 pipeline (crossbeam: 预处理 ‖ 推理 ‖ 后处理) 1 天
P5 C ABI 导出 (yolo_infer_init / one / destroy) 0.5 天
P6 PyO3 兼容 (保留现有 Python 接口) 0.5 天

3. DVPP + Rust 集成

CPU 路径:  JPEG → image crate → fast_image_resize → CHW FP32
DVPP 路径: JPEG → H2D → JPEGD(NPU) → VPC(NPU) → NV12 → AIPP(OM内置)

冠军方案优化过程

45 img/s (基线: torch_npu eager)
  ↓ conf=0.01 + per-class NMS + centered letterbox → 精度 43%→50%
  ↓ ACL pinned memory + non-blocking H2D → 150 img/s
  ↓ double-buffered async pipeline → 400 img/s
  ↓ pre-computed blobs + direct memmove → 439 img/s (突破)
  ↓ Rust NMS postprocess (30.9x) → 420 img/s (release 稳定)

下一阶段目标

目标 说明
硬件利用率 99%+ 当前 95.9%,尚有 ~3pp 提升空间
华为自研加速模块 利用 Ascend NPU 原生加速能力进一步挖掘性能
HiFloat8 / HiFloat4 探索更低精度浮点格式,兼顾精度与吞吐

复现步骤

# 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
pip install torch torch_npu ultralytics opencv-python pycocotools
pip install maturin && cd rust_accel/ && maturin develop --release

# 运行
export ASCEND_RT_VISIBLE_DEVICES=0
export HCCL_WHITELIST_DISABLE=1
python3 code/benchmark_v9.py          # 439 img/s 突破
python3 code/release_benchmark.py     # 420.5 img/s release (3-run)

硬件环境

参数 值
NPU 2× Ascend910 (800I A2), 各 64GB HBM
SoC Ascend910_9362 (Board ID: 0x73)
CANN 8.5.1
固件 25.5.2
CPU Kunpeng aarch64, 229GB RAM
Python 3.11.14 + torch_npu
Rust 1.75+ (aarch64)
约束 当前单卡单芯 (card 1, chip 0),第二芯闲置

上游仓库: ascend-yolov10m — 45→439 img/s 的完整优化历程


分支索引

所有分支保留在 remote,详见 BRANCH_REGISTRY.md

git fetch origin && git branch -r | grep pod/
git checkout -b <local-name> origin/<branch-name>

Introduction

可用于在 Ascend 910 硬件上实现高效目标检测,显著提升推理速度。项目通过 pipeline 优化、Rust 加速等技术,将单卡推理吞吐量从 45 img/s 提升至 439 img/s,实现 100x CPU 加速,同时保持较高检测精度。【此简介由AI生成】

Customize your domain