Cr---/blendmask
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

BlendMask Ascend NPU Validation

This repository contains the complete validation, optimization, and benchmarking toolchain for running a BlendMask-Lite instance-segmentation model on Huawei Ascend NPUs (Atlas 800 A2/A3, CANN 8.5.1).

Overview

PhaseStatusDescription
Precision VerificationPASSCPU/NPU tensor-level comparison with msaccucmp
Performance BenchmarkPASSBatch/input tuning + msprof profiling
Preprocessing OptimizationPASSFused NPU tensor ops vs PIL baseline
ATC OM ConversionPASSONNX exported; ATC conversion succeeded with BN+FP16 fusion

Hardware & Software Environment

  • NPU: Ascend 910B4
  • CANN: 8.5.1
  • PyTorch: 2.x + torch_npu
  • OS: EulerOS / openEuler (aarch64)

Repository Structure

blendmask-ascend-validation/
├── blendmask_model.py           # Lightweight BlendMask-like model (ResNet50-like + FPN + FCOS + Blender)
├── blendmask_validator.py       # Precision validation: CPU vs NPU with DumpCollector + msaccucmp
├── dump_collector.py            # PyTorch hook-based tensor dumper (replaces ptdbg_ascend)
├── perf_benchmark.py            # Performance benchmark: batch tuning, latency, throughput, msprof
├── atc_optimize.sh              # ONNX export + ATC conversion script with fusion & AIPP configs
├── generate_final_report.py     # Aggregates precision + performance + ATC into FINAL_REPORT.md/json
├── reports/
│   ├── precision_report.json        # Detailed precision metrics
│   ├── performance_report.json      # Benchmark results across configs
│   ├── sota_performance_report.json # SOTA benchmark results
│   ├── SOTA_SUMMARY.txt             # Full-text SOTA validation summary
│   ├── FINAL_REPORT.md              # Human-readable consolidated report
│   ├── FINAL_REPORT.json            # Machine-readable consolidated report
│   ├── atc_conversion.log           # ATC conversion log
│   ├── dumps/
│   │   ├── cpu/                     # CPU baseline dump tensors (.npy)
│   │   └── npu/                     # NPU inference dump tensors (.npy)
│   └── msprof_bs8_640x640/          # msprof profiling output
├── models/
│   ├── blendmask_lite.onnx          # Exported ONNX model
│   ├── blendmask_bn_bs1.onnx        # BN static batch ONNX
│   ├── blendmask_bn_dynamic.onnx    # BN dynamic batch ONNX
│   ├── blendmask_bn_bs1.om          # ATC-converted OM model
│   ├── fusion_switch.cfg            # ATC graph fusion configuration
│   └── insert_op.cfg                # ATC AIPP preprocess configuration
├── validation_report.json           # verify-agent scoring schema report
└── README.md                        # This file

Note: The following files referenced in the original specification were not present in the source directory and are therefore not included: blendmask_model_v2.py, blendmask_validator_v2.py, blendmask_validator_v3.py, generate_final_report_v3.py.

Quick Start

1. Precision Validation

python3 blendmask_validator.py --output-dir ./reports --input-size 640 640

Validates numerical consistency between CPU baseline and NPU inference using:

  • Custom DumpCollector (PyTorch forward hooks)
  • msaccucmp.py file_compare for tensor-level comparison
  • Detection-level metrics: box error, score error, class accuracy, top-5 overlap

2. Performance Benchmark

python3 perf_benchmark.py --output-dir ./reports

Benchmarks multiple (batch_size, input_h, input_w) configurations and runs msprof on the best config.

3. ATC Model Conversion

bash atc_optimize.sh

Exports ONNX from PyTorch and runs ATC with:

  • Graph fusion (ConvBNFusionPass, UBFusion, etc.)
  • buffer_optimize=l2_optimize
  • AIPP static preprocessing

4. Generate Final Report

python3 generate_final_report.py

Aggregates precision_report.json + performance_report.json into FINAL_REPORT.md and FINAL_REPORT.json.

Key Results

Precision

MetricValueThresholdStatus
Tensor Mean Relative Error1.7264%< 10%✅ PASS
Tensor Max Layer Error7.2268%< 10%✅ PASS
Box Absolute Error0.081620< 0.10✅ PASS
Score Relative Error0.0237%< 1%✅ PASS
Class Accuracy85.00%> 70%✅ PASS
Top-5 Class Overlap100%—✅ PASS

Performance (SOTA: BN+FP16, BS=8, 640×640)

ConfigThroughput (samples/s)Latency (ms)Speedup
Baseline GN FP32241.274.1451.00×
GN + FP16318.733.1371.32×
BN + FP32 (CANN fusion)252.953.9531.05×
BN + FP16 (old SOTA)375.592.6621.56×
BN + FP16 + Streams (SOTA)490.552.0392.03×

Batch Size Sweep (BN+FP16, Stream-Parallel FPN Head)

Batch SizeSequential (samples/s)Streams Parallel (samples/s)SpeedupBest Config
1121.87101.500.83×SEQ
2215.47234.451.09×PAR
4315.92396.911.26×PAR
8375.36490.551.31×PAR ← default
16420.79503.591.20×PAR

ONNX / ATC

ItemStatus
ONNX Static Export✅ OK (blendmask_bn_bs1.onnx)
ONNX Dynamic Export✅ OK (blendmask_bn_dynamic.onnx)
ATC OM Conversion✅ SUCCESS (blendmask_bn_bs1.om)

SOTA Optimization Progression

The performance journey from baseline to SOTA:

RoundChangeThroughput (samples/s)Gain
BaselineGN FP32241.27—
R1GN → BN (CANN Conv+BN fusion)252.95+4.8%
R2FP32 → FP16 (amp)318.73+32.1%
R3GN+FP16 → BN+FP16375.59+55.7%
R4EnvOpt (tcmalloc, stream queue)375.48+55.6% (marginal)
R5FPN Head Stream Parallelism490.55+103%

Key breakthrough: Stream-parallel FPN Head The 5 FPN levels (P3-P7) are processed on independent NPU Streams, allowing the AI Cores to compute them in parallel instead of sequentially. This yields +30.7% throughput at BS=8, scaling to +19.7% at BS=16.

Key model changes enabling SOTA:

  1. No inplace ops — ReLU inplace=False for NPU compatibility
  2. GN/BN switchable — GN for precision validation, BN for CANN fusion perf
  3. FP16 amp support — mixed precision inference with torch.npu.amp.autocast
  4. bias=False with BN — enables CANN Conv+BN fusion pass
  5. Safe interpolate — mode-aware align_corners for ONNX export stability
  6. NPU Stream parallelism — forward_with_streams() for concurrent FPN head processing

Engineering Notes

  • GroupNorm vs BatchNorm: GroupNorm is used during precision validation to improve CPU/NPU numerical consistency (reduced mean relative error from ~540% to ~5%). BatchNorm is used for ONNX export / ATC conversion.
  • ptdbg_ascend alternative: Since ptdbg_ascend was unavailable in this environment, dump_collector.py provides a lightweight hook-based replacement fully compatible with msaccucmp.
  • ATC Conversion: The static-batch BN variant (blendmask_bn_bs1.onnx) converts successfully via ATC with graph fusion (ConvBNFusionPass, UBFusion) and buffer_optimize=l2_optimize. The dynamic-batch ONNX variant requires AOE tuning for unsupported interpolation patterns.

Next Steps for Production

  1. Load official BlendMask pretrained weights into BlendMaskLite.
  2. Re-run validator; expect tensor error to drop to <1% with pretrained activations.
  3. Use AOE (Auto Tuning Engine) to compile unsupported dynamic interpolation ops before ATC.
  4. Deploy OM with aclmdlExecute pipeline for production inference.

License

Apache-2.0