PaddleOCR TIPC 全流程测试:Linux GPU/CPU KL 离线量化推理测试(test_ptq_inference_python)深度解析
PaddleOCR TIPC 全流程测试Linux GPU/CPU KL 离线量化推理测试test_ptq_inference_python深度解析【免费下载链接】PaddleOCRTurn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between images/PDFs and LLMs. Supports 100 languages.项目地址: https://gitcode.com/GitHub_Trending/pa/PaddleOCR本文以 PaddleOCR 仓库 TIPCTest Infrastructure for PaddlePaddle测试体系的 test_tipc/docs/test_ptq_inference_python.md 为骨架围绕test_ptq_inference_python.sh主程序系统讲解基于 Python 的 KL 离线量化PTQ推理全流程测试如何准备数据与模型、如何编写train_ptq_infer_python.txt参数配置文件、脚本内部如何完成量化导出 → CPU/GPU 多组合推理的自动化编排以及如何解读输出日志与结果。读完本文你将能够在 Linux GPU/CPU 环境下独立运行 PaddleOCR 检测、识别、表格、KIE 等模型的 KL 离线量化推理功能测试并理解其底层调用链与实现原理。1. 测试概述与结论汇总KL 离线量化推理测试的主程序为 test_tipc/test_ptq_inference_python.sh用于验证训练好的全精度模型 → PaddleSlim KL 离线量化导出 → int8 精度推理这条链路上基于 Python 的模型导出与推理功能是否正常。该测试属于 TIPC 测试框架中的whole_infer模式核心目标不是训练精度而是功能正确性量化命令能否跑通、导出的量化模型能否在 CPU/GPU 上完成 int8 推理。原文档给出的测试结论汇总如下训练相关算法名称模型名称单机单卡model_nameKL离线量化训练推理相关算法名称模型名称device_CPUdevice_GPUbatchsizemodel_name支持支持1这里的model_name是占位符实际运行时会替换为配置文件中的model_name字段值例如ch_PP-OCRv2_det_KL。从上表可以提炼出该测试的两个关键约束推理阶段同时覆盖 CPU 与 GPU 两种设备批量大小batchsize以 1 为基准进行测试由于测试对象是 KL 离线量化模型推理精度固定为int8脚本中对此有强制校验详见下文第 4 节。2. 测试流程总览整体流程分为两大阶段准备阶段运行 test_tipc/prepare.sh 下载推理模型与测试数据并解压到./inference目录执行阶段运行test_ptq_inference_python.sh完成量化导出 多组合推理最终在test_tipc/output/{model_name}/whole_infer目录下生成python_infer_*.log后缀的日志文件。原文档给出的标准命令为bash test_tipc/prepare.sh ./test_tipc/configs/ch_PP-OCRv2_det/train_ptq_infer_python.txt whole_infer # 用法: bash test_tipc/test_ptq_inference_python.sh ./test_tipc/configs/ch_PP-OCRv2_det/train_ptq_infer_python.txt whole_infer两条命令都接收两个参数第一个是参数配置文件路径第二个是运行模式。prepare.sh会读取同一份配置文件以确定需要准备哪些模型与数据其内部注释明确列出MODE的可选值为[lite_train_lite_infer lite_train_whole_infer whole_train_whole_infer, whole_infer, klquant_whole_infer]本测试使用的是whole_infer详见 test_tipc/prepare.sh。值得强调的是prepare.sh中为 KL 量化测试准备了专门的*_klquant_infer.tar压缩包例如ch_PP-OCRv2_det_klquant_infer.tar、ch_PP-OCRv3_det_klquant_infer.tar、PP-OCRv3_mobile_rec_klquant_infer.tar等解压后会连同测试图片集如ch_det_data_50.tar、rec_inference.tar一并就位确保后续量化导出与推理有可用的输入。3. 参数配置文件train_ptq_infer_python.txt 字段详解配置文件是驱动整个测试的数据源。test_ptq_inference_python.sh通过awk NR1, NR17截取配置文件的前 17 行实际解析到第 19 行附近再借助 test_tipc/common_func.sh 中的func_parser_key/func_parser_value解析出键值对。下面以检测模型为例逐字段拆解 test_tipc/configs/ch_PP-OCRv2_det/train_ptq_infer_python.txtkl_quant_params model_name:ch_PP-OCRv2_det_KL python:python3.7 Global.pretrained_model:null Global.save_inference_dir:null infer_model:./inference/ch_PP-OCRv2_det_infer/ infer_export:deploy/slim/quantization/quant_kl.py -c configs/det/ch_PP-OCRv2/ch_PP-OCRv2_det_cml.yml -o infer_quant:True inference:tools/infer/predict_det.py --use_gpu:False|True --enable_mkldnn:False --cpu_threads:6 --rec_batch_num:1 --use_tensorrt:False --precision:int8 --det_model_dir: --image_dir:./inference/ch_det_data_50/all-sum-510/ null:null --benchmark:True null:null null:null各字段含义与脚本中的对应关系如下配置行解析键脚本变量含义与说明model_name:ch_PP-OCRv2_det_KLmodel_name测试模型名决定输出目录test_tipc/output/{model_name}/whole_inferpython:python3.7python执行命令所用的 Python 解释器可按环境替换为python3等Global.pretrained_model:nullexport_weight导出阶段的预训练模型键名此处为 null 表示由infer_model直接提供 inference 模型Global.save_inference_dir:nullsave_infer_key量化模型保存目录键名脚本会将实际值拼为{infer_model}_klquantinfer_model:./inference/ch_PP-OCRv2_det_infer/infer_model_dir_list待量化的全精度 inference 模型目录支持\|分隔多个infer_export:deploy/slim/quantization/quant_kl.py -c ... -oinfer_export_listKL 离线量化导出命令不含键值部分null表示跳过导出、直接使用原模型infer_quant:Trueinfer_is_quant是否为量化模型True 时推理精度被强制限定为 int8inference:tools/infer/predict_det.pyinference_pyPython 推理脚本检测用predict_det.py识别用predict_rec.py表格/KIE 用对应 ppstructure 脚本--use_gpu:False\|Trueuse_gpu_list设备组合列表\|分隔CPU 与 GPU 都会覆盖--enable_mkldnn:Falseuse_mkldnn_listCPU 推理是否启用 MKLDNN--cpu_threads:6cpu_threads_listCPU 推理线程数--rec_batch_num:1batch_size_list推理 batchsize识别模型可设为1\|6覆盖多种取值--use_tensorrt:Falseuse_trt_listGPU 推理是否启用 TensorRT--precision:int8precision_list推理精度KL 量化测试固定为int8--det_model_dir:infer_model_key模型目录参数键名空值由脚本回填{infer_model}_klquant--image_dir:./inference/ch_det_data_50/all-sum-510/infer_img_dir推理输入图片目录null:nullsave_log_key预留参数位值为 null 时不追加--benchmark:Truebenchmark_key是否开启 benchmark 模式输出吞吐/耗时数据null:nullinfer_key1预留扩展参数位仓库中还提供了覆盖不同任务的同类配置可作为编写新配置的参考模板识别模型test_tipc/configs/ch_PP-OCRv2_rec/train_ptq_infer_python.txt其inference为tools/infer/predict_rec.py --rec_image_shape3,32,320batchsize 支持1|6两档KIE 模型test_tipc/configs/layoutxlm_ser/train_ptq_infer_python.txtinference为ppstructure/kie/predict_kie_token_ser.py --kie_algorithmLayoutXLM --ser_dict_path...表格结构识别test_tipc/configs/en_table_structure/train_ptq_infer_python.txtinference为ppstructure/table/predict_table.py同时携带检测/识别子模型路径与字符字典路径。4. 主程序内部实现test_ptq_inference_python.sh 执行逻辑test_ptq_inference_python.sh的流程可以拆成参数解析 → 导出 → 推理三层理解它有助于排查测试失败时的根因。4.1 参数解析层脚本通过IFS$\n将配置文件按行切分然后用func_parser_key/func_parser_value提取第 2 行到第 19 行的键值。common_func.sh中的解析函数实现非常朴素以冒号:为分隔符取第 1 个元素为 key、第 2 个元素为 value而func_set_params则负责在 key 或 value 为null/空时输出空串从而把未启用的参数位静默剔除见 test_tipc/common_func.sh。4.2 量化导出层在whole_infer模式下脚本先通过eval $env设置CUDA_VISIBLE_DEVICES第三个参数 GPUID 可指定测试用 GPU随后遍历infer_model_dir_list若infer_run_exports[Count] ! null则执行量化导出命令{python} {infer_export} Global.pretrained_model{infer_model} Global.save_inference_dir{infer_model}_klquant即对每个推理模型生成一个后缀为_klquant的量化模型目录导出日志写入{LOG_PATH}/whole_infer_export_{Count}.log若为null则直接使用原模型目录跳过导出。导出的实际动作由 deploy/slim/quantization/quant_kl.py 完成。该脚本核心是调用 PaddleSlim 的paddleslim.quant.quant_post_static读取inference.pdmodel/inference.pdiparams用训练数据采样生成器sample_generator见源码第 91-97 行统计激活值范围以batch_size1完成静态离线量化量化结果仍以inference.pdmodel/inference.pdiparams形式写入Global.save_inference_dir见 quant_kl.py。quant_kl.py顶部的quant_config字典源码第 67-88 行定义了默认量化策略值得展开说明weight_quantize_type: channel_wise_abs_max权重按通道粒度做 abs_max 量化保留通道间差异精度更好activation_quantize_type: moving_average_abs_max激活值用滑动平均统计动态范围weight_bits/activation_bits均为 8dtype为int8moving_rate: 0.9滑动平均衰减系数quantizable_layer_type: [Conv2D, Linear]只量化卷积与全连接层。此外quant_kl.py对 LayoutXLM KIE 模型做了特判is_layoutxlm_ser源码第 123-126 行这类模型的 dataloader 输出包含input_ids, bbox, attention_mask, token_type_ids, images五个张量因此使用独立的sample_generator_layoutxlm_ser源码第 100-109 行构造采样数据。4.3 推理层CPU/GPU 双分支组合遍历导出完成后调用func_inference按use_gpu_list分成两个互斥分支CPU 分支use_gpuFalse|cpu四重循环遍历use_mkldnn × cpu_threads × batch_size × precision日志文件命名为python_infer_cpu_usemkldnn_{flag}_threads_{n}_precision_{p}_batchsize_{b}.log。分支内有两个跳过规则use_mkldnnFalse且precisionfp16时跳过MKLDNN 关闭则无 fp16 加速路径量化模型_flag_quantTrue且精度不是int8时跳过——这是 KL 离线量化测试的核心约束保证只验证 int8 推理。GPU 分支use_gpuTrue|gpu三重循环遍历use_tensorrt × precision × batch_size日志命名为python_infer_gpu_usetrt_{flag}_precision_{p}_batchsize_{b}.log同样强制量化模型使用int8精度。每个组合都会拼装完整命令行并eval执行随后status_check依据退出码把结果写入汇总日志results_python.log。这种笛卡尔积式遍历设计使一份配置即可覆盖 CPU/MKLDNN/线程数、GPU/TensorRT/精度等主要推理路径组合。5. 运行结果解读各测试的运行情况会打印在test_tipc/output/{model_name}/whole_infer/results_python.log原文档写作paddle2onnx/results_paddle2onnx.log以实际路径为准中。运行成功时的输出形如Run successfully with command - ch_PP-OCRv2_det_KL - python3.7 deploy/slim/quantization/quant_kl.py -c configs/det/ch_PP-OCRv2/ch_PP-OCRv2_det_cml.yml -o Global.pretrained_model./inference/ch_PP-OCRv2_det_infer/ Global.save_inference_dir./inference/ch_PP-OCRv2_det_infer/_klquant ./test_tipc/output/ch_PP-OCRv2_det_KL/whole_infer/whole_infer_export_0.log 21 ! Run successfully with command - ch_PP-OCRv2_det_KL - python3.7 tools/infer/predict_det.py --use_gpuFalse --enable_mkldnnFalse --cpu_threads6 --det_model_dir./inference/ch_PP-OCRv2_det_infer/_klquant --rec_batch_num1 --image_dir./inference/ch_det_data_50/all-sum-510/ --precisionint8 ./test_tipc/output/ch_PP-OCRv2_det_KL/whole_infer/python_infer_cpu_usemkldnn_False_threads_6_precision_int8_batchsize_1.log 21 ! Run successfully with command - ch_PP-OCRv2_det_KL - python3.7 tools/infer/predict_det.py --use_gpuTrue --use_tensorrtFalse --precisionint8 --det_model_dir./inference/ch_PP-OCRv2_det_infer/_klquant --rec_batch_num1 --image_dir./inference/ch_det_data_50/all-sum-510/ ./test_tipc/output/ch_PP-OCRv2_det_KL/whole_infer/python_infer_gpu_usetrt_False_precision_int8_batchsize_1.log 21 !运行失败时输出Run failed with command - ch_PP-OCRv2_det_KL - python3.7 deploy/slim/quantization/quant_kl.py -c configs/det/ch_PP-OCRv2/ch_PP-OCRv2_det_cml.yml -o Global.pretrained_model./inference/ch_PP-OCRv2_det_infer/ Global.save_inference_dir./inference/ch_PP-OCRv2_det_infer/_klquant ./test_tipc/output/ch_PP-OCRv2_det_KL/whole_infer/whole_infer_export_0.log 21 ! ...失败的定位方法查看results_python.log中对应命令行的退出状态确定是导出阶段还是某个推理组合失败打开对应的whole_infer_export_{N}.log或python_infer_{device}_*.log查看完整报错堆栈常见失败原因包括inference.pdmodel/inference.pdiparams缺失或路径错误quant_kl.py第 143-149 行会显式抛出ValueError提示设置Global.inference_model或Global.pretrained_model、未安装/版本不匹配的 PaddleSlim、CUDA 环境变量未生效、图片目录不存在等。6. 更多教程与延伸本文档定位为 TIPC 功能测试说明更详细的量化使用教程请参考 deploy/slim/quantization/README.md其中覆盖了完整的量化五步流程安装 PaddleSlimpip3 install paddleslim2.3.2准备训练好的模型可选用 PaddleOCR 官方发布模型或按常规训练流程自行训练在线量化训练QATpython deploy/slim/quantization/quant.py -c configs/det/ch_PP-OCRv3/ch_PP-OCRv3_det_cml.yml -o Global.pretrained_model./ch_PP-OCRv3_det_distill_train/best_accuracy Global.save_model_dir./output/quant_model_distill/导出推理模型python deploy/slim/quantization/export_model.py -c ... -o Global.checkpointsoutput/quant_model/best_accuracy Global.save_inference_dir./output/quant_inference_model量化模型部署量化模型参数范围是 int8数值类型仍为 float32若要获得实际加速建议配合 TensorRT 并设置precisionINT8推理移动端部署可参考 deploy/lite/readme.md。从代码结构看TIPC 的whole_infer模式与klquant_whole_infer模式共用同一套 prepare 数据源区别在于whole_infer由测试脚本现场执行quant_kl.py完成量化导出这使得该测试天然具备端到端可复现的特点任何一次全流程回归都能从零验证全精度模型 → KL 离线量化 → int8 多端推理的完整链路。7. 结语test_ptq_inference_python.sh是 PaddleOCR TIPC 体系中验证 KL 离线量化功能的关键一环。通过理解配置文件字段、脚本的导出/推理编排逻辑以及quant_kl.py的底层实现开发者既可以一键跑通现有模型的量化推理回归测试也能快速迁移配置去覆盖自定义的检测、识别、表格或 KIE 模型为量化模型在端侧与服务器端的落地提供可靠的质量保障。【免费下载链接】PaddleOCRTurn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between images/PDFs and LLMs. Supports 100 languages.项目地址: https://gitcode.com/GitHub_Trending/pa/PaddleOCR创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考