You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize inference performance of ERNIE on P40 GPU #165
+ /paddle/build_paddle/build_docker_manylinux_cuda90/paddle/fluid/inference/tests/api/samples/ernie_tester --logtostderr --model_dir=/data/ernie/model --data=/data/ernie/seq128_data/test_ds --repeat=1 --warmup_steps=1 --use_gpu=true --use_analysis=true --print_outputs=false --profile=false
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ernie
[ RUN ] ernie.profile
--- Running analysis [ir_graph_build_pass]
--- Running analysis [ir_graph_clean_pass]
--- Running analysis [ir_analysis_pass]
--- Running IR pass [is_test_pass]
--- Running IR pass [simplify_with_basic_ops_pass]
--- Running IR pass [fuse_elewise_add_act_pass]
--- detected 14 subgraphs
--- Running IR pass [conv_affine_channel_fuse_pass]
--- Running IR pass [conv_eltwiseadd_affine_channel_fuse_pass]
--- Running IR pass [conv_bn_fuse_pass]
--- Running IR pass [conv_eltwiseadd_bn_fuse_pass]
--- Running IR pass [conv_elementwise_add_act_fuse_pass]
--- Running IR pass [conv_elementwise_add2_act_fuse_pass]
--- Running IR pass [conv_elementwise_add_fuse_pass]
--- Running IR pass [transpose_flatten_concat_fuse_pass]
--- Running IR pass [runtime_context_cache_pass]
--- Running analysis [ir_params_sync_among_devices_pass]
WARNING: Logging before InitGoogleLogging() is written to STDERR
I0828 08:49:37.436837 7631 ir_params_sync_among_devices_pass.cc:41] Sync params from CPU to GPU
--- Running analysis [adjust_cudnn_workspace_size_pass]
--- Running analysis [inference_op_replace_pass]
--- Running analysis [ir_graph_to_program_pass]
I0828 08:49:37.726634 7631 analysis_predictor.cc:475] == optimize end ==
I0828 08:49:38.710613 7631 ernie_tester.cc:210] Load 5010 samples from /data/ernie/seq128_data/test_ds
W0828 08:49:39.109542 7631 device_context.cc:261] Please NOTE: device: 0, CUDA Capability: 61, Driver API Version: 10.1, Runtime API Version: 9.0
W0828 08:49:39.115780 7631 device_context.cc:269] device: 0, cuDNN Version: 7.3.
W0828 08:49:39.116070 7631 naive_executor.cc:43] The NaiveExecutor can not work properly if the cmake flag ON_INFER is not set.
W0828 08:49:39.116091 7631 naive_executor.cc:45] Unlike the training phase, all the scopes and variables will be reused to save the allocation overhead.
W0828 08:49:39.116104 7631 naive_executor.cc:48] Please re-compile the inference library by setting the cmake flag ON_INFER=ON if you are running Paddle Inference
W0828 08:49:39.127769 7631 naive_executor.cc:43] The NaiveExecutor can not work properly if the cmake flag ON_INFER is not set.
W0828 08:49:39.127822 7631 naive_executor.cc:45] Unlike the training phase, all the scopes and variables will be reused to save the allocation overhead.
W0828 08:49:39.127832 7631 naive_executor.cc:48] Please re-compile the inference library by setting the cmake flag ON_INFER=ON if you are running Paddle Inference
W0828 08:49:39.137923 7631 naive_executor.cc:43] The NaiveExecutor can not work properly if the cmake flag ON_INFER is not set.
W0828 08:49:39.137972 7631 naive_executor.cc:45] Unlike the training phase, all the scopes and variables will be reused to save the allocation overhead.
W0828 08:49:39.137981 7631 naive_executor.cc:48] Please re-compile the inference library by setting the cmake flag ON_INFER=ON if you are running Paddle Inference
W0828 08:49:39.147920 7631 naive_executor.cc:43] The NaiveExecutor can not work properly if the cmake flag ON_INFER is not set.
W0828 08:49:39.147967 7631 naive_executor.cc:45] Unlike the training phase, all the scopes and variables will be reused to save the allocation overhead.
W0828 08:49:39.147977 7631 naive_executor.cc:48] Please re-compile the inference library by setting the cmake flag ON_INFER=ON if you are running Paddle Inference
W0828 08:49:39.157951 7631 naive_executor.cc:43] The NaiveExecutor can not work properly if the cmake flag ON_INFER is not set.
W0828 08:49:39.157996 7631 naive_executor.cc:45] Unlike the training phase, all the scopes and variables will be reused to save the allocation overhead.
W0828 08:49:39.158005 7631 naive_executor.cc:48] Please re-compile the inference library by setting the cmake flag ON_INFER=ON if you are running Paddle Inference
I0828 08:50:21.810214 7631 ernie_tester.cc:345] Run 5010 samples, average latency: 8.60096 ms per sample.
I0828 08:50:21.810283 7631 ernie_tester.cc:350] Run 5009 samples, average latency [exclude 1 warmup steps]: 8.51942 ms per sample.
[ OK ] ernie.profile (46355 ms)
[----------] 1 test from ernie (46355 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (46356 ms total)
[ PASSED ] 1 test.
I0903 09:57:36.458448 29283 ernie_tester.cc:345] Run 5010 samples, average latency: 7.26373 ms per sample.
I0903 09:57:36.458526 29283 ernie_tester.cc:350] Run 5009 samples, average latency [exclude 1 warmup steps]: 6.87974 ms per sample.
以上测试结果基于版本2。
I0920 09:15:41.505820 25415 ernie_tester.cc:345] Run 5010 samples, average latency: 6.92085 ms per sample.
I0920 09:15:41.505903 25415 ernie_tester.cc:350] Run 5009 samples, average latency [exclude 1 warmup steps]: 6.51521 ms per sample.
--- Running IR pass [fc_elementwise_layernorm_fuse_pass]
--- detected 24 subgraphs
.
.
.
I0911 06:41:55.494792 7238 ernie_tester.cc:345] Run 5010 samples, average latency: 6.92849 ms per sample.
I0911 06:41:55.494873 7238 ernie_tester.cc:350] Run 5009 samples, average latency [exclude 1 warmup steps]: 6.54723 ms per sample.
Activity
Xreki commentedon Aug 14, 2019
Profile结果
Xreki commentedon Aug 14, 2019
TImeline分析
总体来说,GPU利用的比较满。

开始时GPU存在不少空闲

stack op里面有cuMalloc和cuFree操作,并且有2次cuda stream的同步操作

很多的reshape和transpose操作
softmax是否使用cudnn?
Xreki commentedon Aug 14, 2019
优化方案
Transpose(Reshape(X*W0+b0))Transpose(Reshape(X*W1+b1))Transpose(Reshape(X*W2+b2))*scale->Transpose(Reshape(X*W2*scale+b2*scale))。该分支经过变换之后,结构与第1、2分支完全一样Transpose(Reshape(X*[W0+W1+W2*scale]+[b0+b1+b2*scale]))elementwise + softmax融合,@zhaoyuchen2018
elementwise + layer_norm融合,@Xreki
LN+FC可以融合,融合以后,LN的gamma和beta都可以删除
Xreki commentedon Aug 14, 2019
Intel相关的一些工作
Xreki commentedon Aug 14, 2019
确定模型中的reshape操作是否必须的,是否可以移除。很多reshape的输入输出shape看起来是一样的。@Xreki
结论:不能直接移除
Xreki commentedon Aug 22, 2019
优化效果汇总
PaddlePaddle/Paddle#19733
Xreki commentedon Aug 28, 2019
在版本1的基础上,在预测了使用了
fuse_elewise_add_act_pass,可以融合elementwise_add+relu的计算,并成功匹配到14个子图,但总体性能下降。zhaoyuchen2018 commentedon Aug 29, 2019
融合3个path的fc,reshape,transpose,scale到一个path. P40上测试结果如下:

融合后的图如下所示:


融合前:
profile结果
Xreki commentedon Sep 3, 2019
stack与concat的功能类似。尝试:方法一:用
concat代替stack,运行出现错误,发现stack和concat输出的维度不一样,不可行stack输出的维度concat输出的维度方法二:参考
concat,重新实现stack以上测试结果基于版本2。
以上测试结果基于版本4。
Xreki commentedon Sep 9, 2019
实现fc的GPU kernel :PaddlePaddle/Paddle#19687
FC的融合:PaddlePaddle/Paddle#19733
Xreki commentedon Sep 11, 2019
fc+elementwise_add+layer_norm融合
Xreki commentedon Nov 20, 2019
multi-head attention融合
Before

After

Xreki commentedon Feb 4, 2020
QA测试结果
Seqlen128-data
Seqlen512-data
Xreki commentedon Feb 4, 2020
二期优化工作
负责人: @NHZlX
GPU平台信息:Tesla P4
软件信息:
优化工作
PaddlePaddle/Paddle#22424PaddlePaddle/Paddle#22486
PaddlePaddle/Paddle#22424PaddlePaddle/Paddle#22494
embedding_eltwise_layernorm_fuse_pass匹配的子图模式
P4的浮点计算能力