Skip to content

Commit e61ce8d

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/PaddleSlim into develop
2 parents c634106 + 9a00cbb commit e61ce8d

File tree

5 files changed

+90
-64
lines changed

5 files changed

+90
-64
lines changed

example/auto_compression/nlp/README.md

+30-18
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@
5656

5757
#### 3.1 准备环境
5858
- python >= 3.6
59-
- PaddlePaddle >= 2.4 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)下载安装)
60-
- PaddleSlim >= 2.4
61-
- PaddleNLP >= 2.3
59+
- PaddlePaddle ==2.5 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)下载安装)
60+
- PaddleSlim ==2.5
61+
- PaddleNLP ==2.6
6262

6363
安装paddlepaddle:
6464
```shell
6565
# CPU
66-
pip install paddlepaddle==2.4.1
66+
pip install paddlepaddle==2.5.0
6767
# GPU 以Ubuntu、CUDA 11.2为例
68-
python -m pip install paddlepaddle-gpu==2.4.1.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
68+
python -m pip install paddlepaddle-gpu==2.5.0.post116 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
6969
```
7070

7171
安装paddleslim:
@@ -95,7 +95,6 @@ pip install paddlenlp
9595
|:------:|:------:|:------:|:------:|:------:|:-----------:|:------:|:------:|
9696
| PP-MiniLM | [afqmc](https://bj.bcebos.com/v1/paddle-slim-models/act/afqmc.tar) | [tnews](https://bj.bcebos.com/v1/paddle-slim-models/act/tnews.tar) | [iflytek](https://bj.bcebos.com/v1/paddle-slim-models/act/iflytek.tar) | [cmnli](https://bj.bcebos.com/v1/paddle-slim-models/act/cmnli.tar) | [ ocnli](https://bj.bcebos.com/v1/paddle-slim-models/act/ocnli.tar) | [cluewsc2020](https://bj.bcebos.com/v1/paddle-slim-models/act/cluewsc.tar) | [csl](https://bj.bcebos.com/v1/paddle-slim-models/act/csl.tar) |
9797
| ERNIE 3.0-Medium | [afqmc](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/AFQMC.tar) | [tnews](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/TNEWS.tar) | [iflytek](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/IFLYTEK.tar) | [cmnli](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/CMNLI.tar) | [ocnli](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/OCNLI.tar) | [cluewsc2020](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/CLUEWSC2020.tar) | [csl](https://bj.bcebos.com/v1/paddle-slim-models/act/NLP/ernie3.0-medium/fp32_models/CSL.tar) |
98-
| UIE-base | [报销工单](https://bj.bcebos.com/v1/paddle-slim-models/act/uie_base.tar) |
9998

10099
从上表获得模型超链接, 并用以下命令下载推理模型文件:
101100

@@ -119,11 +118,6 @@ export CUDA_VISIBLE_DEVICES=0
119118
python run.py --config_path='./configs/pp-minilm/auto/afqmc.yaml' --save_dir='./save_afqmc_pruned/'
120119
```
121120

122-
自动压缩UIE系列模型需要使用 run_uie.py 脚本启动,会使用接口```paddleslim.auto_compression.AutoCompression```对模型进行自动压缩。配置config文件中训练部分的参数,将任务名称、模型类型、数据集名称、压缩参数传入,配置完成后便可对模型进行蒸馏量化训练。
123-
```shell
124-
export CUDA_VISIBLE_DEVICES=0
125-
python run_uie.py --config_path='./configs/uie/uie_base.yaml' --save_dir='./save_uie_qat/'
126-
```
127121

128122
如仅需验证模型精度,或验证压缩之后模型精度,在启动```run.py```脚本时,将配置文件中模型文件夹 ```model_dir``` 改为压缩之后保存的文件夹路径 ```./save_afqmc_pruned``` ,命令加上```--eval True```即可:
129123
```shell
@@ -217,8 +211,6 @@ QuantPost:
217211
218212
- TensorRT预测:
219213
220-
环境配置:如果使用 TesorRT 预测引擎,需安装 ```WITH_TRT=ON``` 的Paddle,下载地址:[Python预测库](https://paddleinference.paddlepaddle.org.cn/master/user_guides/download_lib.html#python)
221-
222214
首先下载量化好的模型:
223215
```shell
224216
wget https://bj.bcebos.com/v1/paddle-slim-models/act/save_ppminilm_afqmc_new_calib.tar
@@ -227,10 +219,30 @@ tar -xf save_ppminilm_afqmc_new_calib.tar
227219

228220
```shell
229221
python paddle_inference_eval.py \
230-
--model_path=save_ernie3_afqmc_new_cablib \
222+
--model_path=save_ppminilm_afqmc_new_calib \
223+
--model_filename=inference.pdmodel \
224+
--params_filename=inference.pdiparams \
225+
--task_name='afqmc' \
226+
--use_trt \
227+
--precision=int8
228+
```
229+
230+
- ERNIE 3.0-Medium:
231+
```shell
232+
python paddle_inference_eval.py \
233+
--model_path=TNEWS \
231234
--model_filename=infer.pdmodel \
232235
--params_filename=infer.pdiparams \
233-
--task_name='afqmc' \
236+
--task_name='tnews' \
237+
--use_trt \
238+
--precision=fp32
239+
```
240+
```shell
241+
python paddle_inference_eval.py \
242+
--model_path=save_tnews_pruned \
243+
--model_filename=infer.pdmodel \
244+
--params_filename=infer.pdiparams \
245+
--task_name='tnews' \
234246
--use_trt \
235247
--precision=int8
236248
```
@@ -239,9 +251,9 @@ python paddle_inference_eval.py \
239251

240252
```shell
241253
python paddle_inference_eval.py \
242-
--model_path=save_ernie3_afqmc_new_cablib \
243-
--model_filename=infer.pdmodel \
244-
--params_filename=infer.pdiparams \
254+
--model_path=save_ppminilm_afqmc_new_calib \
255+
--model_filename=inference.pdmodel \
256+
--params_filename=inference.pdiparams \
245257
--task_name='afqmc' \
246258
--device=cpu \
247259
--use_mkldnn=True \

example/auto_compression/nlp/configs/ernie3.0/tnews.yaml

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ Global:
66
dataset: clue
77
batch_size: 16
88
max_seq_length: 128
9-
TrainConfig:
10-
epochs: 6
11-
eval_iter: 1110
12-
learning_rate: 2.0e-5
13-
optimizer_builder:
14-
optimizer:
15-
type: AdamW
16-
weight_decay: 0.01
17-
origin_metric: 0.5700
9+
10+
# 剪枝
11+
Prune:
12+
prune_algo: transformer_pruner
13+
pruned_ratio: 0.25
14+
15+
# 离线量化
16+
QuantPost:
17+
activation_bits: 8
18+
quantize_op_types:
19+
- depthwise_conv2d
20+
- conv2d
21+
weight_bits: 8
22+

example/auto_compression/nlp/configs/pp-minilm/auto/afqmc.yaml

+7-13
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,11 @@ Global:
66
dataset: clue
77
batch_size: 16
88
max_seq_length: 128
9-
TransformerPrune:
10-
pruned_ratio: 0.25
11-
HyperParameterOptimization:
12-
Distillation:
9+
10+
#离线量化
1311
QuantPost:
14-
TrainConfig:
15-
epochs: 6
16-
eval_iter: 1070
17-
learning_rate: 2.0e-5
18-
optimizer_builder:
19-
optimizer:
20-
type: AdamW
21-
weight_decay: 0.01
22-
origin_metric: 0.7403
12+
activation_bits: 8
13+
quantize_op_types:
14+
- conv2d
15+
- depthwise_conv2d
16+
weight_bits: 8

example/auto_compression/nlp/paddle_inference_eval.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def parse_args():
9191
"--max_seq_length",
9292
default=128,
9393
type=int,
94-
help="The maximum total input sequence length after tokenization. Sequences longer "
94+
help=
95+
"The maximum total input sequence length after tokenization. Sequences longer "
9596
"than this will be truncated, sequences shorter will be padded.", )
9697
parser.add_argument(
9798
"--perf_warmup_steps",
@@ -107,7 +108,8 @@ def parse_args():
107108
type=str,
108109
default="fp32",
109110
choices=["fp32", "fp16", "int8"],
110-
help="The precision of inference. It can be 'fp32', 'fp16' or 'int8'. Default is 'fp16'.",
111+
help=
112+
"The precision of inference. It can be 'fp32', 'fp16' or 'int8'. Default is 'fp16'.",
111113
)
112114
parser.add_argument(
113115
"--use_mkldnn",
@@ -156,8 +158,7 @@ def _convert_example(example,
156158
}
157159
elif "target" in example: # wsc
158160
text, query, pronoun, query_idx, pronoun_idx = (
159-
example["text"],
160-
example["target"]["span1_text"],
161+
example["text"], example["target"]["span1_text"],
161162
example["target"]["span2_text"],
162163
example["target"]["span1_index"],
163164
example["target"]["span2_index"], )
@@ -209,6 +210,12 @@ def create_predictor(cls, args):
209210
config = paddle.inference.Config(
210211
os.path.join(args.model_path, args.model_filename),
211212
os.path.join(args.model_path, args.params_filename))
213+
config.switch_ir_debug(True)
214+
# 适用于ERNIE 3.0-Medium模型
215+
# config.exp_disable_tensorrt_ops(["elementwise_add"])
216+
# config.exp_disable_tensorrt_ops(["fused_embedding_eltwise_layernorm"])
217+
# config.exp_disable_tensorrt_ops(["tmp_3"])
218+
212219
if args.device == "gpu":
213220
# set GPU configs accordingly
214221
config.enable_use_gpu(100, 0)
@@ -239,8 +246,8 @@ def create_predictor(cls, args):
239246
dynamic_shape_file = os.path.join(args.model_path,
240247
"dynamic_shape.txt")
241248
if os.path.exists(dynamic_shape_file):
242-
config.enable_tuned_tensorrt_dynamic_shape(dynamic_shape_file,
243-
True)
249+
config.enable_tuned_tensorrt_dynamic_shape(
250+
dynamic_shape_file, True)
244251
print("trt set dynamic shape done!")
245252
else:
246253
config.collect_shape_range_info(dynamic_shape_file)
@@ -365,4 +372,4 @@ def main():
365372

366373
if __name__ == "__main__":
367374
paddle.set_device("cpu")
368-
main()
375+
main()

example/post_training_quantization/pytorch_yolo_series/README.md

+25-17
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,25 @@
4040
## 3. 离线量化流程
4141

4242
#### 3.1 准备环境
43-
- PaddlePaddle >= 2.3 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)下载安装)
44-
- PaddleSlim > 2.3版本
43+
- PaddlePaddle ==2.5 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)下载安装)
44+
- PaddleSlim == 2.5
4545
- X2Paddle >= 1.3.9
4646
- opencv-python
4747

4848

4949
(1)安装paddlepaddle:
5050
```shell
5151
# CPU
52-
pip install paddlepaddle
52+
python -m pip install paddlepaddle==2.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
5353
# GPU
54-
pip install paddlepaddle-gpu
54+
python -m pip install paddlepaddle-gpu==2.5.0.post116 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
5555
```
5656

5757
(2)安装paddleslim:
58+
注意,PaddleSlim这里setup.py需要更改 slim_version='2.5'
5859
```shell
59-
pip install paddleslim
60+
git clone -b release/2.5 https://github.com/PaddlePaddle/PaddleSlim.git & cd PaddleSlim
61+
python setup.py install
6062
```
6163

6264
#### 3.2 准备数据集
@@ -122,7 +124,7 @@ python eval.py --config_path=./configs/yolov5s_ptq.yaml
122124
#### 3.6 提高离线量化精度
123125

124126
###### 3.6.1 量化分析工具
125-
本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口```paddleslim.quant.AnalysisPTQ```,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。```paddleslim.quant.AnalysisPTQ```详解见[AnalysisPTQ.md](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/tutorials/quant/post_training_quantization.md)
127+
本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口```paddleslim.quant.AnalysisPTQ```,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。```paddleslim.quant.AnalysisPTQ```详解见[离线量化](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/tutorials/quant/post_training_quantization.md)
126128

127129

128130
由于YOLOv6离线量化效果较差,以YOLOv6为例,量化分析工具具体使用方法如下:
@@ -208,23 +210,24 @@ python fine_tune.py --config_path=./configs/yolov6s_fine_tune.yaml --simulate_ac
208210
## 4.预测部署
209211
预测部署可参考[YOLO系列模型自动压缩示例](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/pytorch_yolo_series)
210212
量化模型在GPU上可以使用TensorRT进行加速,在CPU上可以使用MKLDNN进行加速。
211-
| 参数名 | 含义 |
212-
| model_path | inference模型文件所在路径,该目录下需要有文件model.pdmodel和params.pdiparams两个文件 |
213+
| 参数名 | 含义 |
214+
|:------:|:------:|
215+
| model_path | inference 模型文件所在目录,该目录下需要有文件 model.pdmodel 和 model.pdiparams 两个文件 |
213216
| dataset_dir | 指定COCO数据集的目录,这是存储数据集的根目录 |
214217
| image_file | 如果只测试单张图片效果,直接根据image_file指定图片路径 |
215218
| val_image_dir | COCO数据集中验证图像的目录名,默认为val2017 |
216219
| val_anno_path | 指定COCO数据集的注释(annotation)文件路径,这是包含验证集标注信息的JSON文件,默认为annotations/instances_val2017.json |
217220
| benchmark | 指定是否运行性能基准测试。如果设置为True,程序将会进行性能测试 |
218-
| device | 使用GPU或者CPU预测,可选CPU/GPU/XPU,默认设置为GPU |
219-
| use_trt | 是否使用TensorRT进行预测|
220-
| use_mkldnn | 是否使用MKL-DNN加速库,注意use_mkldnn与use_gpu同时为True时,将忽略enable_mkldnn,而使用GPU预测|
221-
| use_dynamic_shape | 是否使用动态形状(dynamic_shape)功能 |
222-
| precision | fp32/fp16/int8|
221+
| device | 使用GPU或者CPU预测,可选CPU/GPU/XPU,默认设置为GPU |
222+
| use_trt | 是否使用 TesorRT 预测引擎 |
223+
| use_mkldnn | 是否启用```MKL-DNN```加速库,注意```use_mkldnn``````use_gpu```同时为```True```时,将忽略```enable_mkldnn```,而使用```GPU```预测 |
224+
| cpu_threads | CPU预测时,使用CPU线程数量,默认10 |
225+
| precision | 预测精度,包括`fp32/fp16/int8` |
223226
| arch | 指定所使用的模型架构的名称,例如YOLOv5 |
224227
| img_shape | 指定模型输入的图像尺寸 |
228+
| use_dynamic_shape | 是否使用动态shape,如果使用动态shape,则设置为True,否则设置为False |
225229
| batch_size | 指定模型输入的批处理大小 |
226-
| use_mkldnn | 指定是否使用MKLDNN加速(主要针对CPU)|
227-
| cpu_threads | 指定在CPU上使用的线程数 |
230+
228231

229232
首先,我们拥有的yolov6.onnx,我们需要把ONNX模型转成paddle模型,具体参考使用[X2Paddle迁移推理模型](https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/model_convert/convert_with_x2paddle_cn.html#x2paddle)
230233
- 安装X2Paddle
@@ -242,7 +245,7 @@ python setup.py install
242245
```shell
243246
x2paddle --framework=onnx --model=yolov6s.onnx --save_dir=yolov6_model
244247
```
245-
- TensorRT Python部署
248+
#### 4.1 TensorRT Python部署
246249
使用[paddle_inference_eval.py](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/example/auto_compression/pytorch_yolo_series/paddle_inference_eval.py)部署
247250
```shell
248251
python paddle_inference_eval.py --model_path=yolov6_model/inference_model --dataset_dir=datasets/coco --use_trt=True --precision=fp32 --arch=YOLOv6
@@ -251,7 +254,11 @@ python paddle_inference_eval.py --model_path=yolov6_model/inference_model --data
251254
```shell
252255
python paddle_inference_eval.py --model_path=yolov6s_ptq_out --dataset_dir==datasets/coco --use_trt=True --precision=int8 --arch=YOLOv6
253256
```
254-
- C++部署
257+
#### 4.2 MKLDNN Python部署
258+
```shell
259+
python paddle_inference_eval.py --model_path=yolov6_model/inference_model --dataset_dir=/work/GETR-Lite-paddle-new/inference/datasets/coco --device=CPU --use_mkldnn=True --precision=fp32 --arch=YOLOv6
260+
```
261+
#### 4.3 C++部署
255262
具体可参考[运行PP-YOLOE-l目标检测模型样例](https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/master/c%2B%2B/gpu/ppyoloe_crn_l)
256263
将compile.sh中DEMO_NAME修改为yolov6_test,并且将ppyoloe_crn_l.cc修改为yolov6_test.cc,根据环境修改相关配置库
257264
运行bash compile.sh编译样例。
@@ -272,5 +279,6 @@ python paddle_inference_eval.py --model_path=yolov6s_ptq_out --dataset_dir==data
272279
```shell
273280
./build/yolov6_test --model_file yolov6s_infer/model.pdmodel --params_file yolov6s_infer/model.pdiparams --run_mode=trt_int8
274281
```
282+
275283
## 5.FAQ
276284
- 如果想对模型进行自动压缩,可进入[YOLO系列模型自动压缩示例](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/pytorch_yolo_series)中进行实验。

0 commit comments

Comments
 (0)