Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Inference] inplace all reshape op #49146

Merged
merged 10 commits into from
Jan 5, 2023

Conversation

jiweibo
Copy link
Contributor

@jiweibo jiweibo commented Dec 18, 2022

PR types

Performance optimization

PR changes

Others

Describe

inplace reshape op.

不可以inplace的Reshape2规则如下:

  1. 存在下图所示状况:reshape2的X输入被多个算子用到;
    TNT_small模型上会有问题,存在以下结构的错误转换;判断reshape2算子输入是否被多个算子用到,如果是则不转换。

image

image

  1. reshape2的输入或输出在其他block被用到;

此外本pr修复了reshape infer shape func中不支持inplace的逻辑;


发现可以进一步优化的场景 TODO:

  1. 多个reshape算子串联:批量模型测试过程中发现在LeVit模型中存在多个reshape2算子串联,可通过fuse仅保留最后一个Reshape;
  2. 存在无意义的reshape操作可以删去:如输入dims[-1, 128],reshape的shape属性也为[-1, 128]这样的identity场景;

@paddle-bot
Copy link

paddle-bot bot commented Dec 18, 2022

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@@ -88,6 +88,7 @@ pass_library(conv_elementwise_add_act_fuse_pass inference)
pass_library(conv_elementwise_add2_act_fuse_pass inference)
pass_library(conv_elementwise_add_fuse_pass inference)
pass_library(transpose_flatten_concat_fuse_pass inference)
pass_library(inplace_op_var_pass inference)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass的名字通用?不是只针对reshape?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前已知的 reshape和squeeze操作都可以inplace,可能还有其他算子

AddStatis(found_subgraph_count);
}

void InplaceOpVarPass::MapToReshape(ir::Graph* graph) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map?是不是考虑改变下map_depthwise_conv_to_conv_pass的功能,将其改成一个通用的pass专门做op的映射?然后将这块放进去?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

收到,下个pr将map类的操作规范到一个pass里

def generate_shape(val):
return np.array(val).astype(np.int32)

x_shape = draw(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个单测被执行了吗?draw从哪里import的?

Copy link
Contributor Author

@jiweibo jiweibo Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

执行了,draw是autoscan单测框架引入的hypothesis库提供的

Copy link
Contributor

@zhangjun zhangjun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jiweibo jiweibo merged commit 017af74 into PaddlePaddle:develop Jan 5, 2023
@jiweibo jiweibo deleted the inplace_reshape branch January 5, 2023 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants