上级索引:RapidOCR 官方仓库。
本页面向:已在昇腾机器上装好驱动与 CANN 的用户,使用 PyTorch + torch_npu 跑通 RapidOCR PP-OCRv4 文本识别推理。
模型来源:RapidOCR GitCode Mirror
按下面顺序执行后:在 Atlas 800 A2 (Ascend910_9362) 上,用 PyTorch NPU 后端 拉起 OCR 推理服务,完成精度验证、性能基准测试与极致性能调优。
本页不解决:宿主机未装昇腾驱动、CANN 未安装、torch_npu 未配置——请先完成昇腾环境准备。
| 项目 | 配置 |
|---|---|
| 机型 | Atlas 800 A2 |
| NPU | Ascend910_9362 × 2 |
| 单卡显存 | 64 GB HBM |
| CPU | aarch64 (Kunpeng) |
| 项目 | 版本 |
|---|---|
| CANN | 8.5.1 |
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| npu-smi | 25.5.2 |
| RapidOCR | 本地源码 (torch backend) |
| datasets | huggingface/datasets |
| text_rec_metric | PyPI latest |
export ASCEND_SLOG_PRINT_TO_STDOUT=0
export ASCEND_GLOBAL_LOG_LEVEL=1
export ASCEND_VISIBLE_DEVICES=0,1
export RANK_ID=0pip install rapidocr_onnxruntime==1.3.16 datasets text_rec_metric
pip install modelscope==1.5.2 -i https://pypi.tuna.tsinghua.edu.cn/simple/模型权重会自动下载到 python/rapidocr/models/ 目录:
| 模型 | 文件名 | 大小 |
|---|---|---|
| 检测模型 (Det) | ch_PP-OCRv4_det_mobile.pth | 14.5 MB |
| 分类模型 (Cls) | ch_ptocr_mobile_v2.0_cls_mobile.pth | 0.6 MB |
| 识别模型 (Rec) | ch_PP-OCRv4_rec_mobile.pth | 26.9 MB |
# 文本识别 (rec-only, 推荐用于性能测试)
python inference.py --img test.jpg --no_det --no_cls --benchmark
# 完整 OCR (det + cls + rec)
python inference.py --img test.jpg --benchmark
# 极致性能模式 (torch.compile + 全优化)
TASK_QUEUE_ENABLE=1 \
PYTORCH_NPU_ALLOC_CONF=expandable_segments:True \
CPU_AFFINITY_CONF="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23" \
python inference.py --img test.jpg --no_det --no_cls --benchmark --compileimport sys
sys.path.insert(0, 'python')
from rapidocr import RapidOCR, LangRec
from rapidocr.utils.typings import EngineType
params = {
"Global.use_det": False,
"Global.use_cls": False,
"Global.use_rec": True,
"Det.engine_type": EngineType.TORCH,
"Cls.engine_type": EngineType.TORCH,
"Rec.engine_type": EngineType.TORCH,
"Rec.lang_type": LangRec.CH,
"EngineConfig.torch.use_npu": True,
"EngineConfig.torch.npu_ep_cfg.device_id": 0,
}
engine = RapidOCR(params=params)
result = engine(img, use_det=False, use_cls=False, use_rec=True)
print(result.txts, result.scores)RapidOCR 项目原生内置 PyTorch 后端 + torch_npu 支持,无需编写兼容层代码。适配路径为:
RapidOCR config.yaml → EngineConfig.torch.use_npu = True
→ inference_engine/pytorch/device_config.py
→ torch.device("npu:{device_id}")
→ torch_npu.npu.set_option() 配置编译缓存| 组件 | 文件路径 | 说明 |
|---|---|---|
| 引擎入口 | python/rapidocr/main.py:38 | RapidOCR 类初始化 |
| NPU 设备配置 | python/rapidocr/inference_engine/pytorch/device_config.py:74 | config_npu() 方法 |
| Torch 推理 | python/rapidocr/inference_engine/pytorch/main.py:26 | TorchInferSession.call() |
| 模型加载 | python/rapidocr/inference_engine/pytorch/networks/main.py | ModelLoader |
| 识别模型 | python/rapidocr/ch_ppocr_rec/main.py | TextRecognizer |
predictor.to(device) 自动迁移到 NPUinp.to(self.device) 将输入 tensor 移到 NPUmodels/kernel_meta/ 目录EngineConfig.torch.use_npu 配置项即可切换 CPU/NPU使用 SWHL/text_rec_test_dataset 文本识别测试集 (310 样本),对比 NPU 与 CPU 推理的 ExactMatch 和 CharMatch 指标。
# 1. 生成 CPU 基线
python eval_accuracy.py --device cpu --output pred_cpu_baseline.txt
# 2. 生成 NPU 预测结果并对比
python eval_accuracy.py --device npu --output pred_npu.txt --compare pred_cpu_baseline.txt| 指标 | CPU (Torch) | NPU (Torch) | 差异 | 阈值 | 判定 |
|---|---|---|---|---|---|
| ExactMatch | 0.8290 (82.90%) | 0.8290 (82.90%) | 0.00% | < 1% | PASS |
| CharMatch | 0.9432 (94.32%) | 0.9432 (94.32%) | 0.00% | < 1% | PASS |
| Avg Elapse | 218.16 ms | 74.79 ms | -65.7% | - | - |
结论:NPU 推理精度与 CPU 完全一致 (0% 差异),通过 < 1% 精度对齐要求。同时 NPU 推理速度是 CPU 的 2.9 倍。
| 指标 | 官方 ONNXRuntime 基线 | NPU (Torch) | 差异 |
|---|---|---|---|
| ExactMatch | 0.8323 | 0.8290 | 0.33% |
| CharMatch | 0.9355 | 0.9432 | +0.77% |
NPU 与官方 ONNXRuntime 基线差异 < 1%,判定 PASS。
| 指标 | NPU 基线 | NPU + torch.compile (aot_eager) | 差异 |
|---|---|---|---|
| ExactMatch | 0.8290 | 0.8290 | 0.00% |
| CharMatch | 0.9432 | 0.9432 | 0.00% |
torch.compile 不会损失精度,ExactMatch 和 CharMatch 完全一致。
在 SWHL/text_rec_test_dataset 310 张图片上测试:
| 指标 | CPU | NPU | 加速比 |
|---|---|---|---|
| Avg Latency | 218.16 ms | 73.96 ms | 2.95× |
| P50 Latency | - | 64.81 ms | - |
| P95 Latency | - | 122.89 ms | - |
| P99 Latency | - | 127.27 ms | - |
| Std Dev | - | 26.40 ms | - |
| Throughput | 4.58 img/s | 13.52 img/s | 2.95× |
单图 500 次迭代 benchmark (合成 48×320 图片):
| 指标 | 基线 NPU | 优化 NPU | 提升 |
|---|---|---|---|
| Avg Latency | ~75 ms | 65.05 ms | ~13% |
| P50 Latency | ~65 ms | 65.03 ms | - |
| P95 Latency | ~123 ms | 66.23 ms | 46%↓ |
| P99 Latency | ~127 ms | 68.39 ms | 46%↓ |
| Std Dev | ~26 ms | 0.96 ms | 96%↓ |
| Throughput | ~13.5 img/s | 15.37 img/s | ~14% |
优化后 P95/P99 延迟大幅下降,抖动从 26ms 降至 0.96ms,推理稳定性显著提升。
在 SWHL/text_rec_test_dataset 310 张真实图片上测试:
| 指标 | NPU 基线 | NPU + torch.compile (aot_eager) | 提升 |
|---|---|---|---|
| Avg Latency | 73.96 ms | 27.35 ms | 63%↓ |
| P50 Latency | 64.81 ms | 26.08 ms | 60%↓ |
| P95 Latency | 122.89 ms | 34.68 ms | 72%↓ |
| P99 Latency | 127.27 ms | 35.87 ms | 72%↓ |
| Throughput | 13.52 img/s | 36.56 img/s | 2.7× |
| ExactMatch | 0.8290 | 0.8290 | 不变 |
| CharMatch | 0.9432 | 0.9432 | 不变 |
关键发现:torch.compile (aot_eager) 在真实数据集上带来 2.7 倍吞吐提升,同时精度完全不损失。这是当前最优配置。
| 配置 | 吞吐 (img/s) | 延迟 (ms) | P99 (ms) | 精度 | 适用场景 |
|---|---|---|---|---|---|
| CPU 基线 | 4.58 | 218.16 | - | 0.8290 | 本地开发 |
| NPU 基线 | 13.52 | 73.96 | 127.27 | 0.8290 | 基础部署 |
| NPU 优化 (TQ+alloc+affinity) | 15.37 | 65.05 | 68.39 | 0.8290 | 生产部署 |
| NPU + compile (aot_eager) | 36.56 | 27.35 | 35.87 | 0.8290 | 极致性能 |
| 优化项 | 环境变量/方法 | 说明 | 效果 |
|---|---|---|---|
| TaskQueue 流并行 | TASK_QUEUE_ENABLE=1 | Stream 级别 TaskQueue 并行下发 | 吞吐提升 ~10% |
| CPU 绑核 | CPU_AFFINITY_CONF=0-23 | CPU 核心绑定减少调度抖动 | 延迟抖动降低 96% |
| 内存分配器 | PYTORCH_NPU_ALLOC_CONF=expandable_segments:True | 可扩展内存段减少碎片 | 显存利用率提升 |
| ACL 编译缓存 | ACL_OP_COMPILER_CACHE_MODE=enable | 缓存算子编译结果 | 首次启动后热加载加速 |
| 日志抑制 | ASCEND_SLOG_PRINT_TO_STDOUT=0 | 减少日志 I/O 开销 | 延迟更稳定 |
| torch.compile | torch.compile(predictor, backend="aot_eager", dynamic=True) | 计算图编译优化 | 吞吐提升 2.7× |
在 Ascend 800 A2 上对 PP-OCRv4 rec 模型测试了两种 compile 后端:
| 后端 | 合成图延迟 | 数据集延迟 | 精度 | 结论 |
|---|---|---|---|---|
| 无 compile | 17.4 ms | 73.96 ms | 0.8290 | 基线 |
| aot_eager | 26.5 ms | 27.35 ms | 0.8290 | 推荐 (数据集场景) |
| inductor | - | - | - | 不支持 (torch_npu 兼容性) |
注意:torch.compile 在单张合成图重复推理时因编译开销反而更慢 (17ms→26ms),但在真实多尺寸图片数据集上收益巨大 (74ms→27ms)。这是因为 compile 的动态形状优化对变化尺寸的输入更有效,而固定尺寸下编译开销无法摊薄。
# 最优配置:全优化 + torch.compile
TASK_QUEUE_ENABLE=1 \
CPU_AFFINITY_CONF="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23" \
PYTORCH_NPU_ALLOC_CONF="expandable_segments:True" \
RANK_ID=0 \
python inference.py --img test.jpg --no_det --no_cls --benchmark --compile
# 一键运行全流程评测
bash run_all.sh npu| 方向 | 说明 | 预期收益 |
|---|---|---|
| ONNXRuntime CANN 后端 | 使用 ONNX 模型 + CANN EP 推理 | 可能比 PyTorch 路径更快 |
| FP16 推理 | 模型半精度量化 (需修改引擎层输入转换) | 吞吐提升 30-50% |
| 批量推理 (Batching) | rec_batch_num 参数调大 | 吞吐线性提升 |
| CANN TensorRT | 使用 TensorRT 引擎 | 延迟降低 20-30% |
| tcmalloc | 替换默认内存分配器 | 延迟抖动降低 |
| Multi-NPU 并行 | 多卡同时推理不同请求 | 吞吐线性扩展 |
| 交付件 | 文件 | 说明 |
|---|---|---|
| 推理脚本 | inference.py | NPU 推理入口,支持 benchmark/compile 模式 |
| 精度评测脚本 | eval_accuracy.py | CPU/NPU 精度对比评测 |
| 性能评测脚本 | eval_performance.py | 单图/数据集性能基准 |
| 优化脚本 | run_optimized.py | 带所有优化的评测脚本 |
| 极致性能脚本 | extreme_benchmark.py | 系统化测试所有优化策略组合 |
| 一键运行脚本 | run_all.sh | 全流程自动化评测 |
| 精度结果 | accuracy_comparison.json | CPU vs NPU 精度对比 JSON |
| 性能结果 | performance_npu.json | NPU 性能数据 JSON |
| 编译性能 | compile_dataset_bench.json | torch.compile 数据集性能 |
| 优化结果 | output_optimized/ | 优化后性能与精度数据 |
| 日志文件 | pred_npu.txt / pred_cpu_baseline.txt | 预测结果文件 |
| 说明 | 链接 |
|---|---|
| RapidOCR 仓库 | GitCode |
| RapidOCR 官方 | GitHub |
| Ascend CANN | 昇腾社区 |
| torch_npu | Ascend/pytorch |
| text_rec_metric | PyPI |
本报告基于 RapidOCR torch backend + torch_npu 2.9.0 在 Atlas 800 A2 上实测得出。性能数据可能因 CANN 版本、硬件批次、图片尺寸等因素有轻微波动。