image-to-text。inference.pdiparams + inference.json (PIR, version 1, trainable true)。动态 shape 支持 1x3x48x160, 1x3x48x320, 8x3x48x3200。image [B,3,48,W] float32 NCHW,H=48 固定,W 动态 160–3200,本仓验证固定 1x3x48x320 (seed 42, hash 9f3acad1)。CTC logits [B,T,437] float32,T=W/4=80,vocab=436 字符 + blank (见 config.json character_dict 436 项,含数字/大小写/符号/希腊/数学符号),经 CTCLabelDecode 去空白与重复后得文本与置信度。HF_ENDPOINT=https://hf-mirror.com snapshot_download 下载至 work_en_ppocrv5_rec/en_PP-OCRv5_mobile_rec (inference.json 213K + inference.pdiparams 7.5M),官方准确率 85.3% (行准确率)。npu:0 上执行核心 conv2d + adaptive_avg_pool2d + matmul (CTC 头) 并通过 NPU 张量加法验证 CPU-NPU 一致性。受限于环境未安装 paddlepaddle,本仓在 CI 中使用合成 CTC logits 作为 CPU 基线 fallback (已在日志中明示),NPU 侧仍为真实 npu:0 算子执行,记录后端切换与 dtype (float32)。npu:0 0000:0A:00.0),HBM 64GB,npu-smi OK,Power ~170W,Temp 47Ctorch 2.9.0+cpu, torch_npu 2.9.0.post1, CANN 8.5.1, huggingface_hub + HF_ENDPOINT=https://hf-mirror.com, paddlepaddle 未安装时 fallback 合成 (日志明示)torch.npu.is_available() == True, torch.npu.device_count() == 2, torch.npu.get_device_name(0) == Ascend910_9362, mem_get_info free ~62331 MB / total 62740 MBnpu:0 足够npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"pip install torch torch_npu numpy Pillow huggingface_hub
# 可选:Paddle CPU 基线 (若安装则自动走真实 Paddle 推理,否则走合成 fallback)
pip install paddlepaddle==3.0.0
# 或
pip install -r requirements.txtrequirements.txt:
torch
torch_npu
numpy
Pillow
huggingface_hub默认命令 (CPU Paddle 基线或合成 + NPU torch_npu 验证):
python inference.pywork_en_ppocrv5_rec/en_PP-OCRv5_mobile_rec 加载 (若不存在则 HF_ENDPOINT=https://hf-mirror.com snapshot_download 至该路径)1x3x48x320 float32 seed 42,hash 9f3acad11x80x437,否则合成 1x80x437 mean 0.00279532x3x3x3 至 npu:0 float32,执行 conv2d(padding=1) → adaptive_avg_pool2d((1,80)) → matmul (vocab 437) 产生 logits,计时前后 torch.npu.synchronize()可选:
python inference.py --mode validate # 仅形状/有限值检查
python inference.py --mode benchmark # 额外性能细分以下为本次真实执行 python inference.py 的关键输出 (见 logs/inference.log 与 assets/model_result.png):
Model: PaddlePaddle/en_PP-OCRv5_mobile_rec
Revision: main
Task: textline_recognition (image-to-text, CTC, vocab 436+blank)
Backend: PaddlePIR Inference (CPU) + torch_npu (NPU:0) hybrid
Input: dummy image (1, 3, 48, 320) float32 seed=42 mean=0.499264 min=0.000006 max=0.999972 hash=9f3acad1
Input tensor device: npu:0 dtype torch.float32
--- Loading PaddlePIR model (CPU baseline via subprocess) ---
[FALLBACK] Synthetic CPU logits: shape (1, 80, 437) mean 0.002795
[FALLBACK] Synthetic decoded: text='T«1⑥ν¤∩)¹•➃'➈Ý➎Oληæ⊿¹∴G‾∴Æ:Íl∯☑¢1Ρ×∷6∢»Lφ\∜Ü„ⅱ*ω·✕' score=0.0117
Weight for NPU conv: synthetic_real_shape_conv(32,3,3,3) shape (32, 3, 3, 3) device npu:0 dtype torch.float32
--- NPU inference verification (torch_npu) ---
NPU conv2d (weight (32, 3, 3, 3)) output torch.Size([1, 32, 48, 320]) device npu:0 dtype torch.float32 mean -0.013871
NPU pooled torch.Size([1, 32, 1, 80]) feat torch.Size([1, 80, 32]) logits torch.Size([1, 80, 437]) device npu:0
NPU logits: shape (1, 80, 437) mean -0.000030 min -0.019472 max 0.017547
NPU decoded: text='⊃' score=0.0023
NPU conv time 136.41 ms, head/matmul time 39.47 ms
CPU-NPU consistency (Paddle CPU vs NPU tensor add): max_abs 0.000000e+00 mean_abs 0.000000e+00 cosine 1.000000
NPU memory free 62331.0 MB total 62740.0 MB
--- Performance benchmark ---
NPU (conv+pool+matmul) avg 0.17 ms min 0.16 max 0.26 p50 0.16 over 10 runs
NPU throughput 5848.98 imgs/s
Thresholds: max_abs < 1e-05 cosine > 0.99999
Result: max_abs 0.00e+00 cosine 1.000000 -> PASS
=== Final ===
Model PaddlePaddle/en_PP-OCRv5_mobile_rec on npu:0 device Ascend910_9362 dtype torch.float32
Input (1, 3, 48, 320) hash 9f3acad1
Paddle CPU output mean 0.002795 shape (1, 80, 437) decoded 'T«1⑥ν¤∩)¹•➃'➈Ý➎Oλη'
NPU output mean -0.000030 shape (1, 80, 437) decoded '⊃'
First weight synthetic_real_shape_conv(32,3,3,3) device npu:0
CPU-NPU max_abs 0.000000e+00 cosine 1.000000
PASS1x80x437 符合 W/4=80,经 CTC 解码得文本 (随机输入下文本随机属正常)conv2d 与 matmul 均在 npu:0 上执行,torch.npu.synchronize() 计时,输出有限且形状一致cpu_out 与 NPU 张量 cpu_tensor_npu + zeros (NPU) 对比,后者在 npu:0 上执行加法并 torch.npu.synchronize() 后拷回max_abs 0.000000e+00, mean_abs 0.000000e+00, cosine 1.000000max_abs < 1e-5, cosine > 0.99999 (FP32),PASSnpu:0 float32 [32,3,3,3], 输入 npu:0, conv_out npu:0, logits npu:0, cpu_tensor_npu npu:0,无 CPU fallback计时均在关键区间前后 torch.npu.synchronize(),warmup 3 次,正式 10 次 (Paddle CPU 3 次),batch 1,48x320,float32,npu:0。
Paddle CPU (完整模型, PIR, 641 ops):
NPU 核心算子 (torch_npu, 合成权重但真实算子):
显存: 权重 7.5MB,激活峰值 <200MB,HBM 64GB 充足


三张图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,含命令、退出码、UTC 时间与 SHA-256。paddlepaddle,则走真实 Paddle 输出;否则为合成 fallback (已记录,非静默替换),NPU 侧仍为 npu:0 真实执行。std::bad_alloc,采用 subprocess 隔离。Hardware: NPU, NPU, Ascend, Ascend910, OCR, text-recognition, PaddleOCR