Skip to content

Commit 2cfec5c

Browse files
authored
add the ernie pretrain config (#3962)
* add the model zoo for the ernie 3.0 * temp add * add the qa prediction * add the config for the ernie * add the predictor for the paddlenlp * add the server for the token、seq * add the qa handler * add the qa server * update the readme for the ernie 3.0 * update the code for the paddlenlp * update the code for qa server * update the code for the trition serving * add the function for the compress * update the code for the paddlenlp * update the code for the token * update the code for token cls * update the code for ernie model
1 parent 08e6b3c commit 2cfec5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1617
-2604
lines changed

model_zoo/ernie-3.0/README.md

Lines changed: 77 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
* [模型介绍](#模型介绍)
55
* [在线蒸馏技术](#在线蒸馏技术)
66
* [模型效果](#模型效果)
7-
* [微调](#微调)
7+
* [开始运行](#开始运行)
8+
* [环境要求](#环境要求)
9+
* [数据准备](#数据准备)
10+
* [模型训练](#模型训练)
11+
* [模型预测](#模型预测)
812
* [模型压缩](#模型压缩)
913
* [环境依赖](#环境依赖)
1014
* [模型压缩 API 使用](#模型压缩API使用)
@@ -17,20 +21,17 @@
1721
* [部署](#部署)
1822
* [Python 部署](#Python部署)
1923
* [服务化部署](#服务化部署)
20-
* [Paddle2ONNX 部署](#Paddle2ONNX部署)
2124
* [Notebook教程](#Notebook教程)
2225
* [参考文献](#参考文献)
2326

2427
<a name="模型介绍"></a>
2528

2629
## 模型介绍
2730

28-
本次开源的模型是在文心大模型ERNIE 3.0 基础上通过**在线蒸馏技术**得到的轻量级模型,模型结构与 ERNIE 2.0 保持一致,相比 ERNIE 2.0 具有更强的中文效果。
31+
本次开源的模型是在文心大模型ERNIE 3.0, 文心大模型ERNIE 3.0 作为百亿参数知识增强的大模型,除了从海量文本数据中学习词汇、结构、语义等知识外,还从大规模知识图谱中学习。 基础上通过**在线蒸馏技术**得到的轻量级模型,模型结构与 ERNIE 2.0 保持一致,相比 ERNIE 2.0 具有更强的中文效果。
2932

3033
相关技术详解可参考文章[《解析全球最大中文单体模型鹏城-百度·文心技术细节》](https://www.jiqizhixin.com/articles/2021-12-08-9)
3134

32-
<a name="在线蒸馏技术"></a>
33-
3435
### 在线蒸馏技术
3536

3637
在线蒸馏技术在模型学习的过程中周期性地将知识信号传递给若干个学生模型同时训练,从而在蒸馏阶段一次性产出多种尺寸的学生模型。相对传统蒸馏技术,该技术极大节省了因大模型额外蒸馏计算以及多个学生的重复知识传递带来的算力消耗。
@@ -47,7 +48,8 @@
4748

4849
<a name="模型效果"></a>
4950

50-
## 模型效果
51+
52+
### 模型效果
5153

5254
本项目开源 **ERNIE 3.0 _Base_****ERNIE 3.0 _Medium_****ERNIE 3.0 _Mini_****ERNIE 3.0 _Micro_****ERNIE 3.0 _Nano_** 五个模型:
5355

@@ -1277,45 +1279,65 @@ batch_size=32 和 1,预测精度为 FP16 时,GPU 下的效果-时延图:
12771279
<br />
12781280

12791281

1280-
以下是本项目目录结构及说明:
1281-
1282-
```shell
1282+
<a name="代码结构"></a>
1283+
## 代码结构
1284+
以下是本项目代码结构
1285+
```text
12831286
.
12841287
├── run_seq_cls.py # 分类任务的微调脚本
12851288
├── run_token_cls.py # 序列标注任务的微调脚本
12861289
├── run_qa.py # 阅读理解任务的微调脚本
12871290
├── compress_seq_cls.py # 分类任务的压缩脚本
12881291
├── compress_token_cls.py # 序列标注任务的压缩脚本
12891292
├── compress_qa.py # 阅读理解任务的压缩脚本
1290-
├── config.yml # 压缩配置文件
1291-
├── infer.py # 支持 CLUE 分类、CLUE CMRC2018、MSRA_NER 任务的预测脚本
1293+
├── utils.py # 训练工具脚本
1294+
├── configs # 压缩配置文件夹
1295+
│ └── default.yml # 默认配置文件
12921296
├── deploy # 部署目录
1293-
│ └── python
1294-
│ └── ernie_predictor.py
1297+
│ └── predictor # onnx离线部署
12951298
│ └── infer_cpu.py
12961299
│ └── infer_gpu.py
12971300
│ └── README.md
1298-
│ └── serving
1299-
│ └── seq_cls_rpc_client.py
1300-
│ └── seq_cls_service.py
1301-
│ └── seq_cls_config.yml
1302-
│ └── token_cls_rpc_client.py
1303-
│ └── token_cls_service.py
1304-
│ └── token_cls_config.yml
1301+
│ └── requirements_cpu.txt
1302+
│ └── requirements_gpu.txt
1303+
│ └── simple_serving # 基于PaddleNLP SimpleServing 服务化部署
1304+
│ └── client_qa.py
1305+
│ └── client_seq_cls.py
1306+
│ └── client_token_cls.py
13051307
│ └── README.md
1306-
│ └── paddle2onnx
1307-
│ └── ernie_predictor.py
1308-
│ └── infer.py
1308+
│ └── server_qa.py
1309+
│ └── server_seq_cls.py
1310+
│ └── server_token_cls.py
1311+
│ └── triton_serving # 基于Triton Serving 服务化部署
1312+
│ └── models
13091313
│ └── README.md
1310-
└── README.md # 文档,本文件
1314+
│ └── seq_cls_grpc_client.py
1315+
│ └── token_cls_grpc_client.py
1316+
└── README.md # 文档
13111317
1312-
```
1318+
<a name="开始运行"></a>
1319+
## 开始运行
1320+
下面提供以CLUE数据集进行模型微调相关训练、预测、部署的代码, CLUE数据集是中文语言理解测评基准数据集,包括了文本分类、文本推理、实体抽取、问答等相关数据集。
13131321
1314-
<a name="微调"></a>
1322+
### 环境要求
1323+
- python >= 3.7
1324+
- paddlepaddle >= 2.3
1325+
- paddlenlp >= 2.4
1326+
- paddleslim >= 2.4
13151327
1316-
## 微调
1328+
### 数据准备
1329+
此次微调数据主要是以CLUE benchmark 数据集为主, CLUE benchmark 包括了文本分类、实体抽取、问答三大类数据集,而 CLUE benchmark 数据目前已经集成在PaddleNLP的datasets里面,可以通过下面的方式来使用数据集
1330+
1331+
```python
1332+
from paddlenlp.datasets import load_dataset
1333+
1334+
# Load the clue Tnews dataset
1335+
train_ds, test_ds = load_dataset('clue', 'tnews', splits=('train', 'test'))
1336+
1337+
```
13171338

1318-
ERNIE 3.0 发布的预训练模型还不能直接在下游任务上直接使用,需要使用具体任务上的数据对预训练模型进行微调。
1339+
<a name="模型训练"></a>
1340+
## 模型训练
13191341

13201342
使用 PaddleNLP 只需要一行代码可以拿到 ERNIE 3.0 系列模型,之后可以在自己的下游数据下进行微调,从而获得具体任务上效果更好的模型。
13211343

@@ -1341,33 +1363,31 @@ qa_model = AutoModelForQuestionAnswering.from_pretrained("ernie-3.0-medium-zh")
13411363
```shell
13421364
# 分类任务
13431365
# 该脚本共支持 CLUE 中 7 个分类任务,超参不全相同,因此分类任务中的超参配置利用 config.yml 配置
1344-
python run_seq_cls.py \
1345-
--task_name tnews \
1346-
--model_name_or_path ernie-3.0-medium-zh \
1347-
--do_train
1366+
python run_seq_cls.py --model_name_or_path ernie-3.0-medium-zh --dataset afqmc --output_dir ./best_models --export_model_dir best_models/ --do_train --do_eval --do_export --config=configs/default.yml
1367+
1368+
# 序列标注任务
1369+
python run_token_cls.py --model_name_or_path ernie-3.0-medium-zh --dataset msra_ner --output_dir ./best_models --export_model_dir best_models/ --do_train --do_eval --do_export --config=configs/default.yml
1370+
1371+
# 阅读理解任务
1372+
python run_qa.py --model_name_or_path ernie-3.0-medium-zh --dataset cmrc2018 --output_dir ./best_models --export_model_dir best_models/ --do_train --do_eval --do_export --config=configs/default.yml
1373+
```
1374+
1375+
<a name="模型预测"></a>
1376+
## 模型预测
1377+
1378+
```shell
1379+
# 分类任务
1380+
# 该脚本共支持 CLUE 中 7 个分类任务,超参不全相同,因此分类任务中的超参配置利用 config.yml 配置
1381+
python run_seq_cls.py --model_name_or_path best_models/afqmc/ --dataset afqmc --output_dir ./best_models --do_predict --config=configs/default.yml
13481382

13491383
# 序列标注任务
1350-
python run_token_cls.py \
1351-
--task_name msra_ner \
1352-
--model_name_or_path ernie-3.0-medium-zh \
1353-
--do_train \
1354-
--num_train_epochs 3 \
1355-
--learning_rate 0.00005 \
1356-
--save_steps 100 \
1357-
--batch_size 32 \
1358-
--max_seq_length 128 \
1359-
--remove_unused_columns False
1384+
python run_token_cls.py --model_name_or_path best_models/msra_ner/ --dataset msra_ner --output_dir ./best_models --do_predict --config=configs/default.yml
13601385

13611386
# 阅读理解任务
1362-
python run_qa.py \
1363-
--model_name_or_path ernie-3.0-medium-zh \
1364-
--do_train \
1365-
--learning_rate 0.00003 \
1366-
--num_train_epochs 8 \
1367-
--batch_size 24 \
1368-
--max_seq_length 512
1387+
python run_qa.py --model_name_or_path best_models/cmrc2018/ --dataset cmrc2018 --output_dir ./best_models --do_predict --config=configs/default.yml
13691388
```
13701389

1390+
13711391
<a name="模型压缩"></a>
13721392

13731393
## 模型压缩
@@ -1405,73 +1425,23 @@ trainer = Trainer(
14051425
trainer.compress()
14061426

14071427
```
1408-
使用压缩 API 基于 Trainer 需要先初始化一个 Trainer 实例,然后调用 `compress()` 启动压缩。
1409-
1410-
假设上述代码位于脚本 compress.py 中,可这样调用:
1411-
1412-
```shell
1413-
python compress.py \
1414-
--dataset "clue cluewsc2020" \
1415-
--model_name_or_path best_models/CLUEWSC2020 \
1416-
--output_dir ./compress_models \
1417-
--per_device_train_batch_size 32 \
1418-
--per_device_eval_batch_size 32 \
1419-
--width_mult_list 0.75 \
1420-
--batch_size_list 4 8 16 \
1421-
--batch_num_list 1 \
1422-
```
1423-
1424-
可以通过传入命令行参数来控制模型压缩的一些超参数,压缩 API 可以传入的超参数可参考[文档](../../docs/compression.md)
1428+
压缩 API 可以传入的超参数可参考[文档](../../docs/compression.md)
14251429

14261430
本项目提供了压缩 API 在分类(包含文本分类、文本匹配、自然语言推理、代词消歧等任务)、序列标注、阅读理解三大场景下的使用样例,可以分别参考 `compress_seq_cls.py``compress_token_cls.py``compress_qa.py`,启动方式如下:
14271431

14281432
```shell
14291433
# 分类任务
1430-
# 该脚本共支持 CLUE 中 7 个分类任务,超参不全相同,因此分类任务中的超参配置利用 config.yml 配置
1431-
python compress_seq_cls.py \
1432-
--dataset "clue tnews" \
1433-
--model_name_or_path best_models/TNEWS \
1434-
--output_dir ./
1434+
# 该脚本共支持 CLUE 中 7 个分类任务,超参不全相同,因此分类任务中的超参配置利用 configs/defalut.yml 配置
1435+
python compress_seq_cls.py --model_name_or_path best_models/afqmc/ --dataset afqmc --output_dir ./best_models/afqmc --config=configs/default.yml
14351436

14361437
# 序列标注任务
1437-
python compress_token_cls.py \
1438-
--dataset "msra_ner" \
1439-
--model_name_or_path best_models/MSRA_NER \
1440-
--output_dir ./ \
1441-
--max_seq_length 128 \
1442-
--per_device_train_batch_size 32 \
1443-
--per_device_eval_batch_size 32 \
1444-
--learning_rate 0.00005 \
1445-
--remove_unused_columns False \
1446-
--num_train_epochs 3
1438+
python compress_token_cls.py --model_name_or_path best_models/msra_ner/ --dataset msra_ner --output_dir ./best_models/msra_ner --config=configs/default.yml
14471439

14481440
# 阅读理解任务
1449-
python compress_qa.py \
1450-
--dataset "clue cmrc2018" \
1451-
--model_name_or_path best_models/CMRC2018 \
1452-
--output_dir ./ \
1453-
--max_answer_length 50 \
1454-
--max_seq_length 512 \
1455-
--learning_rate 0.00003 \
1456-
--num_train_epochs 8 \
1457-
--per_device_train_batch_size 24 \
1458-
--per_device_eval_batch_size 24 \
1441+
python compress_qa.py --model_name_or_path best_models/cmrc2018/ --dataset cmrc2018 --output_dir ./best_models/cmrc2018 --config=configs/default.yml
14591442

14601443
```
14611444

1462-
一行代码验证上面模型压缩后模型的精度:
1463-
1464-
```shell
1465-
# 原模型
1466-
python infer.py --task_name tnews --model_path best_models/TNEWS/compress/inference/infer --use_trt
1467-
# 裁剪后
1468-
python infer.py --task_name tnews --model_path best_models/TNEWS/compress/0.75/float --use_trt
1469-
# 量化后
1470-
python infer.py --task_name tnews --model_path best_models/TNEWS/compress/0.75/hist16/int8 --use_trt --precision int8
1471-
1472-
```
1473-
其中 --model_path 参数需要传入静态图模型的路径和前缀名。
1474-
14751445

14761446
<a name="压缩效果"></a>
14771447

@@ -1599,30 +1569,18 @@ AutoTokenizer.from_pretrained("ernie-3.0-medium-zh", use_fast=True)
15991569

16001570
### Python 部署
16011571

1602-
Python部署请参考:[Python 部署指南](./deploy/python/README.md)
1572+
Python部署请参考:[Python 部署指南](./deploy/predictor/README.md)
16031573

16041574
<a name="服务化部署"></a>
16051575

16061576
### 服务化部署
16071577

1608-
- [Triton Inference Server 服务化部署指南](./deploy/triton/README.md)
1609-
- [Paddle Serving 服务化部署指南](./deploy/serving/README.md)
1610-
1611-
<a name="Paddle2ONNX部署"></a>
1612-
1613-
### Paddle2ONNX 部署
1614-
1615-
ONNX 导出及 ONNXRuntime 部署请参考:[ONNX 导出及 ONNXRuntime 部署指南](./deploy/paddle2onnx/README.md)
1616-
1617-
1618-
### Paddle Lite 移动端部署
1619-
1620-
即将支持,敬请期待
1578+
- [Triton Inference Server 服务化部署指南](./deploy/triton_serving/README.md)
1579+
- [PaddleNLp SimpleServing 服务化部署指南](./deploy/simple_serving/README.md)
16211580

16221581

16231582
<a name="参考文献"></a>
16241583

1625-
16261584
## Notebook教程
16271585

16281586
- [【快速上手ERNIE 3.0】中文情感分析实战](https://aistudio.baidu.com/aistudio/projectdetail/3955163)

model_zoo/ernie-3.0/compress_qa.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,62 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
import sys
1715
from functools import partial
1816

1917
import paddle
2018
import paddle.nn.functional as F
21-
22-
from paddlenlp.data import DataCollatorWithPadding
23-
from paddlenlp.trainer import PdArgumentParser, CompressionArguments, Trainer
24-
from paddlenlp.trainer import EvalPrediction, get_last_checkpoint
25-
from paddlenlp.transformers import AutoTokenizer, AutoModelForQuestionAnswering
26-
from paddlenlp.utils.log import logger
27-
from datasets import load_metric, load_dataset
28-
29-
sys.path.append("../ernie-1.0/finetune")
30-
from question_answering import (
19+
from datasets import load_dataset
20+
from utils import (
21+
DataArguments,
22+
ModelArguments,
3123
QuestionAnsweringTrainer,
32-
CrossEntropyLossForSQuAD,
24+
load_config,
3325
prepare_train_features,
3426
prepare_validation_features,
3527
)
36-
from utils import ALL_DATASETS, DataArguments, ModelArguments
28+
29+
from paddlenlp.data import DataCollatorWithPadding
30+
from paddlenlp.metrics.squad import compute_prediction
31+
from paddlenlp.trainer import CompressionArguments, EvalPrediction, PdArgumentParser
32+
from paddlenlp.transformers import ErnieForQuestionAnswering, ErnieTokenizer
3733

3834

3935
def main():
4036
parser = PdArgumentParser((ModelArguments, DataArguments, CompressionArguments))
4137
model_args, data_args, compression_args = parser.parse_args_into_dataclasses()
4238

39+
# Load model and data config
40+
model_args, data_args, compression_args = load_config(
41+
model_args.config, "QuestionAnswering", data_args.dataset, model_args, data_args, compression_args
42+
)
43+
4344
paddle.set_device(compression_args.device)
4445
data_args.dataset = data_args.dataset.strip()
4546

4647
# Log model and data config
4748
compression_args.print_config(model_args, "Model")
4849
compression_args.print_config(data_args, "Data")
4950

50-
dataset_config = data_args.dataset.split(" ")
51-
raw_datasets = load_dataset(
52-
dataset_config[0], None if len(dataset_config) <= 1 else dataset_config[1], cache_dir=model_args.cache_dir
53-
)
51+
raw_datasets = load_dataset("clue", data_args.dataset)
5452

5553
label_list = getattr(raw_datasets["train"], "label_list", None)
5654
data_args.label_list = label_list
5755

5856
# Define tokenizer, model, loss function.
59-
tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path)
60-
model = AutoModelForQuestionAnswering.from_pretrained(model_args.model_name_or_path)
61-
62-
loss_fct = CrossEntropyLossForSQuAD()
57+
tokenizer = ErnieTokenizer.from_pretrained(model_args.model_name_or_path)
58+
model = ErnieForQuestionAnswering.from_pretrained(model_args.model_name_or_path)
6359

6460
# Preprocessing the datasets.
6561
# Preprocessing is slighlty different for training and evaluation.
66-
column_names = raw_datasets["train"].column_names
67-
68-
column_names = raw_datasets["validation"].column_names
6962

70-
train_dataset = raw_datasets["train"]
63+
raw_datasets = load_dataset("clue", data_args.dataset)
64+
column_names = raw_datasets["train"].column_names
65+
label_list = getattr(raw_datasets["train"], "label_list", None)
66+
data_args.label_list = label_list
7167
# Create train feature from dataset
7268
with compression_args.main_process_first(desc="train dataset map pre-processing"):
7369
# Dataset pre-process
70+
train_dataset = raw_datasets["train"]
7471
train_dataset = train_dataset.map(
7572
partial(prepare_train_features, tokenizer=tokenizer, args=data_args),
7673
batched=True,
@@ -79,8 +76,8 @@ def main():
7976
load_from_cache_file=not data_args.overwrite_cache,
8077
desc="Running tokenizer on train dataset",
8178
)
82-
eval_examples = raw_datasets["validation"]
8379
with compression_args.main_process_first(desc="evaluate dataset map pre-processing"):
80+
eval_examples = raw_datasets["validation"]
8481
eval_dataset = eval_examples.map(
8582
partial(prepare_validation_features, tokenizer=tokenizer, args=data_args),
8683
batched=True,

0 commit comments

Comments
 (0)