Skip to content

Commit 27d87b6

Browse files
authored
Update shape range setting method (#1599)
* update shape range setting method * fix readme * fix readme * update readme
1 parent 0334963 commit 27d87b6

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

examples/model_compression/pp-minilm/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ PP-MiniLM 压缩方案以面向预训练模型的任务无关知识蒸馏(Task-a
9595
├── inference # 预测目录
9696
│ └── infer.py # 预测脚本
9797
│ └── infer_all.sh # 批量预测量化模型启动脚本
98-
│ └── infer_perf.py # 量化模型性能测试脚本
9998
│ └── infer_perf.sh # 量化模型性能测试启动脚本
10099
├── data.py # 数据处理脚本
101100
├── pp-minilm.png # PP-MiniLM 方案流程图
@@ -337,7 +336,7 @@ cd ..
337336

338337
#### 运行方式
339338

340-
这里使用了动态 shape 功能,因此需要设置获取 shape 的范围。Paddle Inference 提供了相应的接口,即首先通过离线输入数据来统计出所有临时 tensor 的 shape 范围,TRT 子图的 tensor 输入 shape 范围可直接根据上一步 tune 出来的结果来设置,即可完成自动 shape 范围设置。统计完成后,只需设置统计结果路径,即可启用 `tuned_dynamic_shape` 功能。在本案例中,只需要先设置 `--collect_shape` 参数,运行 `infer.py`,然后再取消传入这个参数,再次运行 `infer.py`。例如:
339+
这里使用了动态 shape 功能,因此需要设置 TensorRT 子图输入shape 的范围。用户需要事先根据自己的模型结构和数据 shape 的范围,设置 TensorRT 子图输入的 shape 的最大、最小、以及最优的范围,其中最优范围可以按照数据分布选择最常见的来设置。动态 shape 的设置可以参考[官方文档](https://paddleinference.paddlepaddle.org.cn/optimize/paddle_trt.html#dynamic-shape)中的教程,以及本案例中 infer.py 脚本中的 160 行 - 206 行)。
341340

342341
INT8 预测运行脚本:
343342

@@ -347,8 +346,7 @@ cd inference
347346
export task=tnews
348347
export algo=mse
349348
export bs=4
350-
python infer.py --task_name ${task} --model_path ../quantization/${task}_quant_models/${algo}${bs}/int8 --int8 --use_trt --collect_shape # 生成shape range info文件
351-
python infer.py --task_name ${task} --model_path ../quantization/${task}_quant_models/${algo}${bs}/int8 --int8 --use_trt # load shape range info文件进行预测
349+
python infer.py --task_name ${task} --model_path ../quantization/${task}_quant_models/${algo}${bs}/int8 --int8 --use_trt
352350
```
353351
如果想要批量对量化模型进行预测并输出不同量化策略产出模型的精度,可以使用如下的脚本批量预测:
354352

@@ -359,7 +357,6 @@ sh infer_all.sh
359357
FP32 预测运行脚本:
360358

361359
```shell
362-
python infer.py --task_name ${task} --model_path $MODEL_PATH --use_trt --collect_shape
363360
python infer.py --task_name ${task} --model_path $MODEL_PATH --use_trt
364361
```
365362

@@ -378,7 +375,7 @@ cd ..
378375
```
379376

380377
下表后三行分别是微调后的模型、裁剪后的模型、量化后模型的总耗时情况。
381-
取 5 个非 `--collect_shape` 阶段打印出的时长取平均,可以发现借助 PaddleSlim 裁剪、量化后的模型是原 BERT<sub>base</sub>模型推理速度的 9.3 倍,其中裁剪后的模型是 BERT<sub>base</sub>推理速度的 2.6 倍。
378+
取 5 个测试时长取平均,可以发现借助 PaddleSlim 裁剪、量化后的模型是原 BERT<sub>base</sub>模型推理速度的 9.3 倍,其中裁剪后的模型是 BERT<sub>base</sub>推理速度的 2.6 倍。
382379

383380
| | 平均耗时(s) | 加速比 |
384381
| ----------------------- | ----------- | ------ |

examples/model_compression/pp-minilm/inference/infer.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,54 @@ def create_predictor(cls, args):
156156
use_calib_mode=False)
157157
print("Enable TensorRT is: {}".format(
158158
config.tensorrt_engine_enabled()))
159-
if args.collect_shape:
160-
config.collect_shape_range_info(
161-
os.path.join(
162-
os.path.dirname(args.model_path), args.task_name +
163-
'_shape_range_info.pbtxt'))
159+
160+
# Set min/max/opt tensor shape of each trt subgraph input.
161+
if args.int8:
162+
min_batch_size, max_batch_size, opt_batch_size = 16, 32, 32
163+
min_seq_len, max_seq_len, opt_seq_len = 31, 128, 32
164+
165+
min_input_shape = {
166+
"faster_tokenizer_2.tmp_0": [min_batch_size, min_seq_len],
167+
"faster_tokenizer_2.tmp_1": [min_batch_size, min_seq_len],
168+
"tmp_4": [min_batch_size, min_seq_len],
169+
"unsqueeze2_0.tmp_0": [min_batch_size, 1, 1, min_seq_len],
170+
}
171+
max_input_shape = {
172+
"faster_tokenizer_2.tmp_0": [max_batch_size, max_seq_len],
173+
"faster_tokenizer_2.tmp_1": [max_batch_size, max_seq_len],
174+
"tmp_4": [max_batch_size, max_seq_len],
175+
"unsqueeze2_0.tmp_0": [max_batch_size, 1, 1, max_seq_len],
176+
}
177+
opt_input_shape = {
178+
"faster_tokenizer_2.tmp_0": [opt_batch_size, opt_seq_len],
179+
"faster_tokenizer_2.tmp_1": [opt_batch_size, opt_seq_len],
180+
"tmp_4": [opt_batch_size, opt_seq_len],
181+
"unsqueeze2_0.tmp_0": [opt_batch_size, 1, 1, opt_seq_len],
182+
}
164183
else:
165-
config.enable_tuned_tensorrt_dynamic_shape(
166-
os.path.join(
167-
os.path.dirname(args.model_path),
168-
args.task_name + "_shape_range_info.pbtxt"), True)
184+
min_batch_size, max_batch_size, opt_batch_size = 16, 32, 32
185+
min_seq_len, max_seq_len, opt_seq_len = 31, 128, 32
186+
187+
min_input_shape = {
188+
"faster_tokenizer_1.tmp_0": [min_batch_size, min_seq_len],
189+
"faster_tokenizer_1.tmp_1": [min_batch_size, min_seq_len],
190+
"tmp_4": [min_batch_size, min_seq_len],
191+
"unsqueeze2_0.tmp_0": [min_batch_size, 1, 1, min_seq_len],
192+
}
193+
max_input_shape = {
194+
"faster_tokenizer_1.tmp_0": [max_batch_size, max_seq_len],
195+
"faster_tokenizer_1.tmp_1": [max_batch_size, max_seq_len],
196+
"tmp_4": [max_batch_size, max_seq_len],
197+
"unsqueeze2_0.tmp_0": [max_batch_size, 1, 1, max_seq_len],
198+
}
199+
opt_input_shape = {
200+
"faster_tokenizer_1.tmp_0": [opt_batch_size, opt_seq_len],
201+
"faster_tokenizer_1.tmp_1": [opt_batch_size, opt_seq_len],
202+
"tmp_4": [opt_batch_size, opt_seq_len],
203+
"unsqueeze2_0.tmp_0": [opt_batch_size, 1, 1, opt_seq_len],
204+
}
205+
config.set_trt_dynamic_shape_info(min_input_shape, max_input_shape,
206+
opt_input_shape)
169207

170208
predictor = paddle.inference.create_predictor(config)
171209

examples/model_compression/pp-minilm/inference/infer_all.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ do
1919
do
2020
for algo in abs_max avg hist mse
2121
do
22-
python infer.py --task_name ${task} --model_path ../quantization/${task}_quant_models/${algo}${bs}/int8 --int8 --use_trt --collect_shape
2322
python infer.py --task_name ${task} --model_path ../quantization/${task}_quant_models/${algo}${bs}/int8 --int8 --use_trt
2423
echo this is ${task}, ${algo}, ${bs}
2524
done

examples/model_compression/pp-minilm/inference/infer_perf.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
# limitations under the License.
1414

1515
export task=TNEWS
16-
python infer.py --task_name ${task} --model_path ../finetuning/ppminilm-6l-768h/models/${task}/1e-4_64/inference --use_trt --collect_shape --perf
17-
python infer.py --task_name ${task} --model_path ../pruning/pruned_models/${task}/0.75/sub_static/float --use_trt --collect_shape --perf
18-
python infer.py --task_name ${task} --model_path ../quantization/${task}_quant_models/mse4/int8 --int8 --use_trt --collect_shape --perf
1916

2017
echo Inference of orgin FP32 model
2118
for ((i=0;i<=4;i++));

0 commit comments

Comments
 (0)