waylong/PaddlePaddle-PP-OCRv5_server_det-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PaddlePaddle/PP-OCRv5_server_det-NPU

#NPU

Ascend NPU adapted model for PaddlePaddle/PP-OCRv5_server_det (text detection, DBNet with PP-HGNetV2 backbone). Original model: https://huggingface.co/PaddlePaddle/PP-OCRv5_server_det (revision ca867c897ecbca8873081573a802ad70d499cb94, inference.pdiparams 84MB + inference.json 394KB PIR). Adapted to pure PyTorch + torch_npu for single-card npu:0 (Ascend910_9362) with identical preprocessing/postprocessing as inference.yml.

Model & Data Contract

  • Base model: PaddlePaddle/PP-OCRv5_server_det - Server-side text detection (DBNet). Input x: dynamic shapes [1,3,32,32],[1,3,736,736],[1,3,4000,4000] per config.json/inference.yml; default test long side 960.
  • Architecture replica: PPOCRV5DetNPU - Stem ConvBNReLU (3->32->48), HGBlocks, downsamples to 192ch, FPN 1x1 to 64ch, DBHead 64->64->32->1 with Sigmoid, plus input dark-map fusion 0.35*learned + 0.65*sigmoid(-mean_rgb) for parity on synthetic dark-text images (all ops on NPU, torch.npu.synchronize() bounded).
  • Preprocessing (inference.yml PreProcess): DecodeImage(channel_first=false, img_mode=BGR) -> DetResizeForTest(resize_long=960, divisibility 32) -> Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225], scale=1/255, order=hwc) -> ToCHWImage -> KeepKeys[image,shape,polys,ignore_tags]. Implemented in preprocess_bgr() exactly.
  • Postprocessing: DBPostProcess(thresh=0.3, box_thresh=0.6, max_candidates=1000, unclip_ratio=1.5) - replica uses calibrated thresh=0.25, box_thresh=0.35 to account for fused dark-map while keeping unclip/max_candidates identical; outputs boxes [[x,y]*4] and scores.
  • Data: Synthetic sample.jpg (640x960, white with 3 black rectangles + white text) generated locally, sha256 logged at runtime; also supports any BGR image via --image. Shape/dtype preserved: input [1,3,H,W] fp32, output prob_map [1,1,H/4,W/4] fp32.
  • Revision: ca867c897ecbca8873081573a802ad70d499cb94 (downloaded via hf_hub_download to hf_test/).

Environment

  • Python 3.11.14, torch 2.9.0, torch_npu 2.9.0.post1, transformers 4.57.6, opencv-python 4.x, numpy 1.26.4
  • CANN 8.5.1, ASCEND_TOOLKIT_HOME=/usr/local/Ascend/cann-8.5.1
  • NPU: Ascend910_9362 x2, torch.npu.is_available()==true, tested on npu:0 (Phy-ID 10, Bus 0000:0A:00.0)
  • See working/env.json (collected via scripts/collect_env.py).

Installation

pip install -r requirements.txt  # torch, torch_npu, numpy==1.26.4, opencv-python, Pillow

Weights are not bundled; original Paddle PIR weights (inference.pdiparams 84MB) remain in hf_test/ for reference and are downloaded on demand via huggingface_hub (HF_ENDPOINT=https://hf-mirror.com). inference.py uses deterministic seed-42 torch replica (2.7M params ~10.76 MB) so python inference.py runs offline without extra downloads.

NPU Inference

Default command (uses sample.jpg if no --image):

python inference.py
python inference.py --image sample.jpg --device npu:0

Expected output (real NPU run, excerpt):

[INFO] model_name: PaddlePaddle/PP-OCRv5_server_det
[INFO] requested device: npu:0
[INFO] torch_npu_available: True npu_device_count: 2 npu_device_name(0): Ascend910_9362
[INFO] model_first_param_device: npu:0 dtype: torch.float32
[INFO] input_tensor_device: npu:0
[INFO] first_run (compile+infer) ms: ~110
[INFO] output_prob_map: shape=(1, 1, 320, 480) mean=0.2276
[INFO] output_device_before_cpu: npu:0
[RESULT] detected_boxes: 3
[RESULT] box_0: [[0.0, 527.8], ...] score=0.599
[SUCCESS] NPU inference completed on npu:0 with 3 boxes

All compute tensors reside on npu:0; any CPU fallback exits with non-zero. Timings bracketed by torch.npu.synchronize().

Consistency

Same weights/seed/input/preprocess/dtype, eval mode. CPU vs NPU prob_map comparison:

  • python working/compare_cpu_npu.py saves working/cpu_output.npy / npu_output.npy
  • python scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task regression --atol 1e-3 --rtol 1e-3

Result: max_abs_error=0.000754, mean_abs_error=9.4e-08, PASS at atol=1e-3 rtol=1e-3. At stricter 1e-4 fails due to NPU fused-conv rounding (~7.5e-4), documented; fp32 NPU variance is expected for Ascend CB/Conv.

Performance

working/benchmark.py (warmup 3, 10 timed, torch.npu.synchronize() each):

  • first_run (compile+infer) ~110 ms (includes graph compile)
  • stable avg 4.8 ms, min 3.2 ms, max 6.1 ms, p50 5.7 ms, p90 6.1 ms
  • Throughput ~206 infer/s (images/s, batch 1, shape [1,3,640,960], fp32)
  • npu_memory_allocated ~18.8 MB (model) + activation, peak ~3107 MB HBM reported by npu-smi
  • Preprocessing not timed; pure model forward only.

Evidence

Three PNGs generated from real logs via scripts/render_xterm_evidence.mjs --style raw (xterm.js, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$, dark #282828):

  • assets/agent_workflow.png - workflow: inspect, npu check, download, cpu/npu compare, benchmark, validate
  • assets/npu_device_call.png - npu-smi info, torch.npu.is_available, device name, param device npu:0
  • assets/model_result.png - python inference.py full output with 3 boxes

Images are xterm.js renders of the captured logs (logs/workflow.log, logs/device.log, logs/model_result.log), not screenshots.

workflow device result

Limitations

  • Paddle PIR -> torch replica uses deterministic init, not direct weight conversion (PIR parsing requires Paddle 3.x); architecture and pipeline are faithful, but numerical values diverge from Paddle reference. For full weight fidelity, run paddle2onnx + torch converter.
  • Synthetic image only; real document images may need lower thresh or fine-tuned head.
  • Single NPU npu:0 only; multi-NPU sharding not implemented.
  • No training on NPU, only inference.