使用ERNIE3.0百亿 & 15亿模型训练和预测:序列标注
更新时间:2022-12-17
目录结构
序列标注任务位于wenxin_appzoo/wenxin_appzoo/tasks/sequence_labeling
├── data ## 示例数据文件夹,包括各任务所需的训练集、测试集、验证集和预测集
│ ├── dev_data
│ │ └── dev.txt
│ ├── msra_ner
│ │ ├── dev
│ │ │ └── dev.tsv
│ │ ├── label_map.json
│ │ ├── predict
│ │ │ └── predict.tsv
│ │ ├── test
│ │ │ └── test.tsv
│ │ └── train
│ │ └── train.tsv
│ ├── predict_data
│ │ └── infer.txt
│ ├── test_data
│ │ └── test.txt
│ └── train_data
│ └── train.txt
├── data_set_reader
│ ├── ernie_layout_seqlabel_dataset_reader.py
│ ├── ernie_seqlabel_base_dataset_reader.py
│ └── ernie_seqlabel_dataset_reader.py
├── dict
│ ├── vocab_label_map.txt
│ └── vocab.txt
├── examples ## 各典型网络的json配置文件,infer后缀的为对应的预测配置文件
│ ├── seqlab_ernie_3.0_msra_ner_ch_infer.json
│ ├── seqlab_ernie_3.0_msra_ner_ch.json
│ ├── seqlab_ernie_3.0_msra_ner_save_infer_from_ckpt.json
│ ├── seqlab_ernie_fc_ch_infer.json
│ ├── seqlab_ernie_fc_ch_infer_with_active_learning.json
│ ├── seqlab_ernie_fc_ch.json
│ ├── seqlab_ernie_fc_ch_train_dal_infer.json
│ ├── seqlab_ernie_fc_ch_with_active_learning.json
│ ├── seqlab_ernie_layout_ch_infer.json
│ └── seqlab_ernie_layout_ch.json
├── inference ## 模型预测代码
│ ├── custom_ernie_layout_inference.py ## 序列标注任务ERNIE-layout的模型预测代码
│ ├── custom_inference.py ## 序列标注任务通用的模型预测代码
│ ├── custom_seqlab_inference_ernie3.py ## 序列标注任务ERNIE-3.0的模型预测代码
│ └── __init__.py
├── model ## 序列标注任务相关的网络文件
│ ├── ernie3_fc_sequence_label.py
│ ├── ernie_fc_sequence_label.py
│ ├── ernie_layout_sequence_label.py
│ └── __init__.py
├── reader
│ ├── categorical_field_reader.py
│ └── multi_label_field_reader.py
├── run_infer_ernie3.py
├── run_infer.py
├── run_infer_with_active_learning.py
├── run_trainer.py
└── trainer
├── custom_dynamic_trainer.py
├── custom_trainer_ernie3.py
├── custom_trainer.py
└── __init__.py
训练配置
dataset_reader配置
- dataset_reader部分对预置reader进行配置,对于ERNIE 3.0任务,配置文件为 seqlab_ernie_3.0_msra_ner_ch.json
{
"is_ernie3": true, # 表明为ernie3.0任务
"dataset_reader": {
"train_reader": {
"name": "train_reader",
"type": "ErnieSequenceLabelReader", ## 采用了SequenceLabelReader
"fields": [ ## 域(field)是文心的高阶封装, 但在ERNIE 3.0中本字段不起作用,默认设置即可。
{
"name": "text_a",
"data_type": "string",
"reader": null,
"tokenizer": null,
"need_convert": true,
"vocab_path": "",
"max_seq_len": 256,
"truncation_type": 0,
"padding_id": 0
},
{
"name": "label",
"data_type": "string",
"reader": null,
"tokenizer": null,
"need_convert": false,
"vocab_path": "",
"max_seq_len": 256,
"truncation_type": 0,
"padding_id": 0,
"embedding": null
}
],
"config": {
"data_path": "./data/msra_ner/train",
"shuffle": true,
"batch_size": 2,
"epoch": 10,
"sampling_rate": 1.0,
"need_data_distribute": true,
"extra_params":{
"vocab_path":"../../models_hub/ernie_3.0_ch_dir/vocab.txt", ## ERNIE 3.0 词表
"label_map_config":"./data/msra_ner/label_map.json", ## 配置标签的标注方式
"max_seq_len":256,
"do_lower_case":true,
"in_tokens":false,
"tokenizer": "FullTokenizer",
"is_glyce": false ## label标注形式为BIO格式默认为False, 采用BMESO格式设置为True
}
}
},
......
},
......
}
###model配置
- 用于配置模型训练时的预置网络,包括预置网络的类别及其优化器的参数等
- ERNIE 3.0模型的model配置:ERNIE 3.0任务的model部分和ERNIE其他模型基本一致,区别在于增加了trainnig_server 和 sharding 相关的配置。
- 以./examples/seqlab_ernie_3.0_msra_ner_ch.json中的model部分为例说明。
"model": {
"type": "Ernie3FcSeqLabel",
"num_labels": 7, ## label个数,随任务变化
"is_glyce": false,
"is_cluener": false, ## 针对cluener数据集,会采用不同的eval方式(打平baseline需要), 其他数据集设置为False
"label_map_config": "./data/msra_ner/label_map.json",
"max_seq_len": 256,
"embedding": {
"emb_dim": 4096,
"use_amp": true,
"mem_len": 0,
"weight_sharing": false,
"training_server": true, ## 是否对ERNIE 3.0 server部分进行训练
"config_path": "../../models_hub/ernie_3.0_ch_dir/ernie_config.json"
},
"optimization":{ ## 优化器设置,建议使用文心ERNIE推荐的默认设置。
"learning_rate": 1e-5,
"use_lr_decay": true,
"use_default_decay": false,
"lr_scheduler": "linear_warmup_decay",
"use_release_paddle": false,
"epsilon": 1e-6,
"warmup_steps": 0,
"warmup_proportion": 0.1,
"weight_decay": 0.01,
"use_dynamic_loss_scaling": false,
"init_loss_scaling": 524288,
"incr_every_n_steps": 1000,
"decr_every_n_nan_or_inf": 2,
"incr_ratio": 2.0,
"decr_ratio": 0.8,
"use_layer_decay": false,
"layer_decay_ratio": 0.95,
"n_layers": 60,
"sharing_layers": 48
}
trainer
- 用于配置模型训练的启动器,包括保存模型时的间隔步数、进行测试集或验证集评估的间隔步数等。
- 以下为/examples/seqlab_ernie_3.0_msra_ner_ch.json 中抽取出来的trainer部分配置,并通过注释说明。
"trainer": {
"type" : "CustomTrainerErnie3", ## 表示使用的trainer对应的类名,ERNIE 3.0目前支持静态图(CustomTrainer)。
"PADDLE_PLACE_TYPE": "gpu", ## 表示运行时的设备类别,取值为cpu和gpu。
"PADDLE_IS_FLEET": 1, ## 表示是否使用fleetrun模式进行训练,gpu多卡情况下必须设置为1,并使用fleetrun命令进行训练。
"is_recompute": true, ##是否开启重计算,默认开启。通过使用内存与磁盘存储训练前向阶段的激活值,减少了显存的占用,可以加载具有更多参数的模型。
"ramdom_seed": 1,
"use_amp": true, ## 是否开启混合精度训练,默认开启。
"use_sharding": true, ## 是否开启sharding,默认开启。可以将模型参数自动分配到多个显卡上,实现超大模型训练的关键分布式训练技术。
"save_inference_model": false, ## 是否开启训练时保存inference_model,默认关闭。注意:暂不支持开启sharding训练时保存inference_model,请使用checkpoints到inference_model的转化工具。
"use_fast_executor": true,
"train_log_step": 10,
"is_do_train": 1,
"is_eval_dev": 1, ## 是否开启dev集评估,默认开启
"is_eval_test": 1, ## 是否开启test集评估,默认开启
"eval_step": 500,
"save_model_step": 1000000000000, ## 保存模型时的间隔步数,建议设置为eval_step的整数倍。
"load_parameters": "", ## 加载包含各op参数值的训练好的模型,用于热启动。此处填写checkpoint路径。不填则表示不使用热启动
"load_checkpoint": "", ## 加载包含学习率等所有参数的训练模型,用于热启动。此处填写checkpoint路径。不填则表示不使用热启动。
"pre_train_model": [ ## 加载ERNIE 3.0 预训练模型
{
"name": "ernie_3.0_ch",
"params_path": "../../models_hub/ernie_3.0_ch_dir/params"
}
],
"output_path": "./output/cls_ernie_3.0_msra_ch" ## 保存模型的输出路径,若为空则默认。为"./output"
}
开始训练
- 模型均存放于wenxin_appzoo/wenxin_appzoo/models_hub文件夹下,进入文件夹执行下载脚本即可获得模型参数等。
# ernie_3.0模型下载
# 进入models_hub目录
cd ../../models_hub/
# 运行下载脚本
sh download_ernie_3.0_ch.sh
- 进入指定任务的目录,序列标注任务目录为wenxin_appzoo/wenxin_appzoo/tasks/sequence_labeling, 运行
cd wenxin_appzoo/wenxin_appzoo/tasks/sequence_labeling
fleetrun --log_dir log ./run_trainer.py --param_path "./examples/seqlab_ernie_3.0_msra_ner_ch.json" 1>log/lanch.log 2>&1
- 通过上述脚本调用json文件开启训练 (注意ERNIE3.0-1.5B单卡训练时也需fleetrun启动)。
- 训练阶段日志文件于log文件夹下,workerlog.N 保存了第N张卡的log日志内容,如遇到程序报错可以通过查看不同卡的workerlog.N定位到有效的报错信息。
- 训练模型保存于./output/cls_ernie_3.0_msra_ch文件夹下。
预测配置
- 预测分为两个阶段,首先需要将训练过程中保存的checkpoints格式参数转成预测推理的inference_model参数格式,
- 配置文件为seqlab_ernie_3.0_msra_ner_save_infer_from_ckpt.json,需要注意的是trainer部分,其他部分与训练配置是一致的
"trainer": {
"type" : "CustomTrainerErnie3",
"PADDLE_PLACE_TYPE": "cpu", ## 必须使用cpu保存
"PADDLE_IS_FLEET": 1,
"is_recompute": false,
"ramdom_seed": 1,
"use_amp": false,
"use_sharding": false,
"save_inference_model": true, ## 这里必须设置为True
"use_fast_executor": true,
"train_log_step": 10,
"is_do_train": 1,
"is_eval_dev": 0,
"is_eval_test": 0,
"eval_step": 500,
"save_model_step": 1000000000000,
"load_parameters": "", ## load_parameters中填入训练时保存的checkpoints的文件路径, 必需。
"load_checkpoint": "",
"pre_train_model": [
{
"name": "ernie_3.0_ch",
"params_path": "../../models_hub/ernie_3.0_ch_dir/params"
}
],
"output_path": "./output/cls_ernie_3.0_msra_ch"
- 完成inference保存后,可以进行预测,预测配置文件如下: (examples/seqlab_ernie_3.0_msra_ner_ch_infer.json)
{
"is_ernie3": true,
"dataset_reader": {
"predict_reader": {
"name": "predict_reader",
"type": "ErnieSequenceLabelReader",
"fields": [
{
"name": "text_a",
"data_type": "string",
"reader": null,
"tokenizer": null,
"need_convert": true,
"vocab_path": "",
"max_seq_len": 256,
"truncation_type": 0,
"padding_id": 0
}
],
"config": {
"data_path": "./data/msra_ner/predict",
"shuffle": false,
"batch_size": 1,
"epoch": 1,
"sampling_rate": 1.0,
"need_data_distribute": true,
"extra_params":{
"vocab_path":"../../models_hub/ernie_3.0_ch_dir/vocab.txt",
"label_map_config":"./data/msra_ner/label_map.json",
"max_seq_len":256,
"do_lower_case":true,
"in_tokens":false,
"tokenizer": "FullTokenizer",
"is_glyce": false
}
}
}
},
"inference": {
"type": "CustomSeqLabelInferenceErnie3", ## Inference过程中使用的reader
"output_path": "./output/predict_result.txt", ## 保存结果的文件
"output_server_path": "./output/predict_result_server.txt", ## 在traning_server为true的情况下,保存server产出的结果。
"PADDLE_PLACE_TYPE": "gpu",
"turn_on_trt": true,
"training_server": true,
"use_cache": false,
"num_labels": 7,
"thread_num": 2,
"inference_model_path": "./output/cls_ernie_3.0_msra_ch/save_inference_model/inference_step_1/", # 保存inference model的路径
"config_path": "../../models_hub/ernie_3.0_ch_dir/ernie_config.json",
"extra_param": {
"meta":{
"job_type": "sequence_labeling"
},
"max_seq_len": 256
}
}
}
开始预测
运行如下命令将checkpoint转化为inference model:
export CPU_NUM='1'
export CUDA_VISIBLE_DEVICES=0 # mask out other gpus for saving inference-model
fleetrun --log_dir log ./run_trainer.py --param_path "./examples/seqlab_ernie_3.0_msra_ner_save_infer_from_ckpt.json" 1>log/lanch.log 2>&1
- 脚本耗时大约20分钟。
- 预测模型保存于./output/cls_ernie_3.0_msra_ch/save_inference_model路径下
运行如下命令执行预测:
BASE_PATH="../../models_hub/ernie_3.0_ch_dir/infer_env/"
export CUDA_VISIBLE_DEVICES=0 # 屏蔽其它显卡
export PATH="${BASE_PATH}/py37/bin/:$PATH"
export PYTHONPATH="${BASE_PATH}/py37/"
# ERNIE 3.0百亿模型依赖TensorRT以fp16精度进行单卡预测,下面为所需的tensorRT库,以及对应的cuda和cuddn版本
export LD_LIBRARY_PATH=$BASE_PATH:${BASE_PATH}/cuda-11.0.3/lib64:${BASE_PATH}/cudnn-11.0-linux-x64-v8.0.5.39/lib64:${BASE_PATH}/TensorRT-7.2.1.6/lib:/home/opt/nvidia_lib:${BASE_PATH}/libs:$LD_LIBRARY_PATH
export FLAGS_allocator_strategy=auto_growth # for inference,没有这一行会报显存溢出的错误
mkdir -p log
fleetrun --log_dir log ./run_infer_ernie3.py.py --param_path "./examples/seqlab_ernie_3.0_msra_ner_ch_infer.json" 1>log/lanch.log 2>&1
- 预测结果保存于./output/predict_result.txt中,如果训练过程中training_server保持开启,则会额外产生一个./output/predict_result_server.txt文件用于保存server的预测结果