- 人工智能
- 大模型
- 预训练
- 微调
- LoRA
- RLHF
- 强化学习
- 分布式训练
【免费下载链接】PaddleNLP
Easy-to-use and powerful LLM and SLM library with awesome model zoo.
导读
本文围绕 PaddleNLP 仓库中的 TIPC(Test Training and Inference for PaddlePaddle Chain)基础功能测试体系,系统讲解 Windows 端基于 Python 的训练、推理功能测试方法。读者将掌握test_train_inference_python.sh的四种运行模式(lite_train_lite_infer / lite_train_whole_infer / whole_infer / whole_train_whole_infer)、配置文件结构与参数含义、prepare.sh数据与模型准备逻辑,以及基于compare_results.py的预测精度校验方法,能够独立在 Windows(Git Bash 环境)或 Linux 上跑通"训练 → 动转静导出 → 推理"的完整验证链路。
1. 测试目标与结论汇总
Windows 端基础训练预测功能测试的主程序为test_train_inference_python.sh(位于 tests/test_tipc/test_train_inference_python.sh),用于验证基于 Python 的模型训练、推理等基本功能是否完整可用。在 PaddleNLP 中,该测试以bigru_crf(BiGRU + CRF 中文词法分析模型)为主要验证对象,其训练脚本为 tests/test_tipc/bigru_crf/train.py,推理脚本为 tests/test_tipc/bigru_crf/deploy/predict.py。
1.1 训练功能汇总
| 模型名称 | 单机单卡 | 单机多卡 | 多机多卡 | 模型压缩(单机多卡) |
|---|---|---|---|---|
| bigru_crf | 正常训练 | - | - | - |
从测试结论可以看出,bigru_crf 在 Windows 端 TIPC 测试中支持单机单卡正常训练,暂未覆盖单机多卡、多机多卡与模型压缩场景。
1.2 预测功能汇总
训练产出的模型为"正常模型",预测功能汇总如下:
| 模型类型 | device | batchsize | tensorrt | mkldnn | cpu多线程 |
|---|---|---|---|---|---|
| 正常模型 | GPU | 1/8 | fp32/fp16 | - | - |
| 正常模型 | CPU | 1/8 | - | fp32/fp16 | 支持 |
即正常模型在 GPU 端支持 batchsize 1/8 下的 fp32/fp16(可叠加 TensorRT 加速),在 CPU 端支持 batchsize 1/8 下的 fp32/fp16(可叠加 MKLDNN 加速与 CPU 多线程)。这些组合正是 TIPC 推理阶段要逐一覆盖的矩阵。
2. 测试流程概览
测试整体分为三个环节:
- 环境准备:配置运行环境(详见 tests/test_tipc/docs/install.md);
- 功能测试:先运行
prepare.sh准备数据和模型,再运行test_train_inference_python.sh执行训练与推理,最终在test_tipc/output目录下生成python_infer_*.log格式的日志文件; - 精度测试:使用
compare_results.py将预测日志中的结果与预存的基准结果(gt_file)比对,验证精度符合预期。
2.1 Windows 环境使用 Git Bash
由于 Windows 与 Linux 的路径管理方式不同,TIPC 建议在 Windows 上安装Git Bash终端。Git Bash 中执行指令的方式与 Linux 端一致(支持 bash 脚本、通配符与路径习惯),更方便 TIPC 测试。这一点对整条测试链路至关重要——prepare.sh与test_train_inference_python.sh都是 bash 脚本,且内部大量使用wget、tar、sed、ln -s等类 Unix 命令(见 tests/test_tipc/prepare.sh),原生 CMD / PowerShell 无法直接运行。
3. 安装依赖
3.1 安装 PaddlePaddle 与 PaddleNLP
- 安装 PaddlePaddle >= 2.0;
- 安装 PaddleNLP(在仓库根目录下执行):
pip3 install ../requirements.txt pip3 install ..3.2 安装 AutoLog(规范化日志输出工具)
AutoLog 用于在推理阶段输出规范化的性能与精度日志,predict.py依赖它完成日志落盘,compare_results.py也正是通过解析 AutoLog 格式的日志来提取预测结果的。安装步骤如下:
git clone <AutoLog 开源仓库地址> cd AutoLog pip3 install -r requirements.txt python3 setup.py bdist_wheel pip3 install ./dist/auto_log-1.0.0-py3-none-any.whl cd ../3.3 环境配置参考
TIPC 运行环境的完整搭建教程(包括 CUDA/CUDNN/TensorRT 组合选型、Docker 镜像安装与 Python 环境构建、PaddlePaddle 带 TRT 版本安装及常见 FAQ)参见 tests/test_tipc/docs/install.md。其中推荐的 CUDA/CUDNN/TensorRT 组合包括:
- CUDA 10.1 + CUDNN 7.6 + TensorRT 6
- CUDA 10.2 + CUDNN 8.1 + TensorRT 7
- CUDA 11.1 + CUDNN 8.1 + TensorRT 7
4. 功能测试:四种运行模式
test_train_inference_python.sh包含 4 种运行模式,每种模式的运行数据规模不同,分别用于验证流程走通、预测速度与训练/预测精度。Windows 端对应的配置文件为 tests/test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt。
模式1:lite_train_lite_infer(少量训练 + 少量预测)
使用少量数据训练,用于快速验证训练到预测的完整流程是否走通,不验证精度和速度:
bash test_tipc/prepare.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'lite_train_lite_infer' bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'lite_train_lite_infer'模式2:lite_train_whole_infer(少量训练 + 全量预测)
使用少量数据训练,全量数据预测,用于验证训练后的模型执行预测的流程与预测速度是否合理:
bash test_tipc/prepare.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'lite_train_whole_infer' bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'lite_train_whole_infer'模式3:whole_infer(不训练,全量预测)
不训练,直接使用预训练好的模型进行全量数据预测,走通开源模型评估与动转静流程,检查 inference model 的预测时间和精度。由于不涉及训练,prepare.sh在此模式下会直接下载静态图推理模型bigru_crf_infer_model.tgz并解压到./test_tipc/bigru_crf/infer_model(见 tests/test_tipc/prepare.sh 中whole_infer分支),跳过训练环节:
bash test_tipc/prepare.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'whole_infer' # 用法1: 默认使用 config 中 gpu_list 指定的卡 bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'whole_infer' # 用法2: 指定GPU卡预测,第三个传入参数为GPU卡号 bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'whole_infer' '1'模式4:whole_train_whole_infer(全量训练 + 全量预测)
CE 模式(持续集成回归):全量数据训练、全量数据预测,验证模型训练精度、预测精度与预测速度:
bash test_tipc/prepare.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'whole_train_whole_infer' bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 'whole_train_whole_infer'4.1 prepare.sh 的数据准备逻辑
从源码看,tests/test_tipc/prepare.sh 通过source test_tipc/common_func.sh引入参数解析函数,以"配置文件路径 + 模式名"为入参,按模式分支准备数据:
- 对 bigru_crf 模型,四种模式都会下载词法分析小数据集
lexical_analysis_dataset_tiny.tar.gz并解压到./data/目录(用于训练与推理);whole_infer模式还会额外下载预训练的推理模型; - 对 transformer 模型,
prepare.sh会通过sed动态调整max_out_len、random_seed等配置,并下载 WMT14 数据集与预训练模型,以控制不同模式下的运行时长与数据规模。
这种"按模式切换数据与模型"的设计,保证了轻量模式可在约 15 分钟内完成整条链路验证。
5. 配置文件深度解析
Windows 端 TIPC 使用键值对文本格式的配置文件(以|分隔多选参数、=区分模式专属参数)。以 tests/test_tipc/configs/bigru_crf/train_windows_gpu_normal_normal_infer_python_windows_cpu_gpu.txt 为例,核心参数含义如下:
5.1 训练参数段(train_params)
| 参数 | 取值 | 说明 |
|---|---|---|
model_name | bigru_crf | 测试的模型名,prepare.sh 据此分支准备数据 |
python | python | 执行 Python 的解释器命令 |
gpu_list | 0 | 使用的 GPU 卡号列表 |
--device | gpu | 训练设备,Windows 端默认 GPU |
Global.auto_cast | null | 混合精度开关,null 表示不使用 AMP |
--epochs | lite_train_lite_infer=1\|lite_train_whole_infer=1\|whole_train_whole_infer=100 | 各模式对应的训练轮数(轻量模式 1 轮快速走通,CE 模式 100 轮全量训练) |
--model_save_dir | ./test_tipc/bigru_crf/output/ | 训练日志与模型保存目录 |
--batch_size | lite_train_lite_infer=16\|lite_train_whole_infer=16\|whole_train_whole_infer=32 | 各模式对应的 batch size |
Global.pretrained_model | null | 预训练模型路径,null 表示从零训练 |
train_model_name | best_model.pdparams | 训练产出的最优模型文件名 |
train_infer_img_dir | ./data/lexical_analysis_dataset_tiny | 词法分析小数据集路径(prepare.sh 下载并解压) |
--data_dir | ./data/lexical_analysis_dataset_tiny | 数据目录参数 |
trainer | norm_train | 训练器类型:常规训练;pact_train/fpgm_train/distill_train为 null(未启用量化/剪枝/蒸馏压缩训练) |
norm_train | test_tipc/bigru_crf/train.py | 常规训练入口脚本 |
5.2 评估参数段(eval_params)
| 参数 | 取值 | 说明 |
|---|---|---|
eval | null | 评估开关,当前 Windows 测试未启用独立评估环节 |
5.3 推理参数段(infer_params)
| 参数 | 取值 | 说明 |
|---|---|---|
--output_path | ./test_tipc/bigru_crf/infer_model/ | 动转静导出模型的输出目录 |
--params_path | ./test_tipc/bigru_crf/output/best_model.pdparams | 待导出的训练权重路径 |
norm_export | test_tipc/bigru_crf/export_model.py | 正常模型动转静导出脚本(tests/test_tipc/bigru_crf/export_model.py) |
quant_export/fpgm_export/distill_export | null | 量化/剪枝/蒸馏导出均未启用 |
infer_model | ./test_tipc/bigru_crf/infer_model | inference model 目录 |
inference | ./test_tipc/bigru_crf/deploy/predict.py | 推理脚本(tests/test_tipc/bigru_crf/deploy/predict.py) |
--device | cpu\|gpu | 推理设备双选,覆盖 CPU 与 GPU 两种场景 |
--enable_mkldnn | True\|False | 是否开启 MKLDNN(CPU 加速开关)双选 |
--cpu_threads | 1\|6 | CPU 推理线程数双选,验证 cpu 多线程支持 |
--batch_size | 1\|8 | 推理 batchsize 双选 |
--use_tensorrt | False\|True | 是否开启 TensorRT(GPU 加速开关)双选 |
--precision | fp32\|fp16 | 推理精度双选 |
--benchmark | True | 开启 benchmark 模式,输出性能数据 |
正是这些cpu|gpu、True|False、1|8、fp32|fp16等以|分隔的多选参数,构成了"预测功能汇总表"中 GPU/CPU、TRT/MKLDNN、多线程等组合矩阵的自动化遍历来源——test_train_inference_python.sh会逐一遍历这些组合并生成对应的预测日志。
6. 运行结果与日志解读
运行相应指令后,在test_tipc/output文件夹下会自动保存运行日志。以lite_train_lite_infer模式为例(训练 + inference 全链路),test_tipc/bigru_crf/output/下会有如下文件:
test_tipc/bigru_crf/output/ |- results_python.log # 运行指令状态的日志 |- norm_train_gpus_0_autocast_null/ # GPU 0号卡上正常训练的训练日志和模型保存文件夹 ...... |- python_infer_cpu_usemkldnn_True_threads_1_batchsize_1.log # CPU上开启Mkldnn线程数设置为1,测试batch_size=1条件下的预测运行日志 |- python_infer_gpu_usetrt_True_precision_fp16_batchsize_1.log # GPU上开启TensorRT,测试batch_size=1的半精度预测日志 ......其中results_python.log汇总了每条指令的运行状态。从 tests/test_tipc/common_func.sh 中的status_check()函数实现可以看到判定逻辑:根据上一条命令的退出码,成功时输出
Run successfully with command ......失败时输出
Run failed with command ......借助results_python.log可以快速定位是哪一条训练/推理指令执行出错,是排查 TIPC 失败的第一步。
7. 精度测试:compare_results.py
7.1 测试原理
精度测试使用 tests/test_tipc/compare_results.py 比较模型预测结果是否符合预期,主要步骤包括:
- 提取日志中的预测结果:通过
parser_results_from_log_by_name()使用grep解析 AutoLog 输出的key\tvalue格式日志行,并用ast.literal_eval还原数值; - 从本地文件提取保存好的基准结果:
load_gt_from_txts()会按文件名中的fp32/fp16/int8关键字将基准结果归类到对应精度; - 比较两个结果:
testing_assert_allclose()对预测结果与基准结果执行np.testing.assert_equal全等比较,误差大于设置阈值时报错。
从源码可见,该脚本支持--precision参数(默认fp32)来选择对应的基准集合,并会逐个遍历所有匹配--log_file通配符的预测日志进行比对。
7.2 使用方式
运行命令(Windows 端同样需在 Git Bash 中执行):
python3.7 test_tipc/compare_results.py --gt_file=./test_tipc/bigru_crf/results/python_*.txt --log_file=./test_tipc/bigru_crf/output/python_bigru_crf_*.log参数介绍:
gt_file:指向事先保存好的预测基准结果路径,支持*.txt结尾,会自动索引*.txt格式的文件,文件默认保存在test_tipc/results/文件夹下。bigru_crf 的基准文件即 tests/test_tipc/results/python_bigru_crf_results_fp32.txt 与 tests/test_tipc/results/python_bigru_crf_results_fp16.txt;log_file:指向运行test_tipc/test_train_inference_python.sh脚本 infer 模式保存的预测日志(日志中打印了预测结果),同样支持python_infer_*.log格式传入。
7.3 运行结果判定
比对通过时输出Assert allclose passed!并提示两个文件结果一致;出现不一致时脚本会抛出异常并明确指出The results of ... are inconsistent!,从而将精度回归问题显式暴露出来。建议在 CI 中把 compare_results 的退出码纳入检查,任何预测结果漂移都会立即中断流水线。
8. 总结
PaddleNLP 的 Windows 端 TIPC 测试以test_train_inference_python.sh为核心,通过四种运行模式覆盖"轻量流程验证 → 训练后推理 → 纯推理评估 → 全量回归"四个递进层次:
- 环境层:依赖 Git Bash 模拟类 Unix 环境,配合 tests/test_tipc/docs/install.md 中的 CUDA/CUDNN/TensorRT 组合完成搭建;
- 数据层:tests/test_tipc/prepare.sh 按模式下载词法分析小数据集或预训练推理模型,保证不同模式下运行成本可控;
- 执行层:配置文件 中以
|分隔的多选参数自动生成 GPU/CPU、TensorRT/MKLDNN、fp32/fp16、线程数、batchsize 等推理组合矩阵,并以 common_func.sh 中的status_check()汇总每步执行状态到results_python.log; - 验收层:compare_results.py 通过 AutoLog 规范化日志完成"预测结果 vs 基准结果"的全等比对,形成精度回归闭环。
这套流程不仅适用于 bigru_crf,其配置驱动的设计同样可迁移到仓库内其他模型(如 transformer、ernie 系列),是验证 PaddleNLP 模型在 Windows 平台"训练、动转静、推理"全链路可用性的标准实践。
- 人工智能
- 大模型
- 预训练
- 微调
- LoRA
- RLHF
- 强化学习
- 分布式训练
【免费下载链接】PaddleNLP
Easy-to-use and powerful LLM and SLM library with awesome model zoo.
相关推荐
PaddleOCR TIPC Linux 端基础训练预测功能测试指南:test_train_inference_python.sh 全流程解析
PaddleOCR TIPC Linux 端基础训练预测功能测试指南:test_train_inference_python.sh 全流程解析 导读 本文围绕
人工智能计算机视觉OCR深度学习大模型RAGPaddleOCR Windows 端 TIPC 基础训练预测功能测试实战指南
PaddleOCR Windows 端 TIPC 基础训练预测功能测试实战指南 本文档基于 PaddleOCR 仓库 test_tipc 目录下的 win_te
人工智能计算机视觉OCR深度学习大模型RAGPaddleOCR Mac 端基础训练预测功能测试(TIPC)实战指南
PaddleOCR Mac 端基础训练预测功能测试(TIPC)实战指南 本文档围绕 PaddleOCR 仓库 test_tipc https://link.gi
人工智能计算机视觉OCR深度学习大模型RAG
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考