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] Support constant_folding_pass on PIR #58753

Merged
merged 9 commits into from Nov 9, 2023

Conversation

yuanlehome
Copy link
Contributor

@yuanlehome yuanlehome commented Nov 7, 2023

PR types

Performance optimization

PR changes

Others

Description

前置PR: #58732

针对PIR,实现常量折叠pass并在swintransformer模型上跑通:

  • 该常量折叠pass可以指定place(当前支持cpu/gpu)进行折叠,相比旧IR的实现(只支持cpu),速度更快,并且理论上可以折叠掉gpu only的op
  • 该常量折叠pass还支持对while op内部进行折叠,相比旧IR的实现(不支持while op),折叠范围更广
  • swintransformer模型上op折叠率42%(4907/11470),折叠前后性能提升25.7%(70ms/52ms)
  • swintransformer模型跑旧IR旧执行器并且只应用旧的常量折叠pass,需要99ms

TODO:

  • 验证更多模型(while op)

Others

Pcard-71500

@yuanlehome yuanlehome changed the title [PIR] Fix dce pass [Inference] Support constant_folding_pass Nov 7, 2023
@yuanlehome yuanlehome changed the title [Inference] Support constant_folding_pass [Inference] Support constant_folding_pass on PIR Nov 7, 2023
pir::Program* new_program) const {
pir::Builder builder = pir::Builder(ir_context(), new_program->block());
std::string output_var_name =
"constant_folding@" + std::to_string((*counter_)++);
Copy link
Contributor

Choose a reason for hiding this comment

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

用counter做唯一标识输出var name感觉不是很安全

Copy link
Contributor Author

Choose a reason for hiding this comment

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

下面还有操作,拼接了被折叠掉的name
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

提醒了我,这里有个潜在问题,随着折叠的深度,name拼接后会越来越长,我修一下

Comment on lines +1130 to +1138
// pm.EnableIRPrinting(std::make_unique<pir::PassManager::IRPrinterOption>(
// [](pir::Pass *pass, pir::Operation *op) {
// return pass->name() == "constant_folding_pass";
// },
// [](pir::Pass *pass, pir::Operation *op) {
// return pass->name() == "constant_folding_pass";
// },
// true,
// true));
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的代码是否还需要?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

留着吧,给其他用户的使用示例~

Copy link
Contributor

@zyfncg zyfncg left a comment

Choose a reason for hiding this comment

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

swintransformer模型上op折叠率42%(4907/11470),折叠前后性能提升25.7%(70ms/52ms)

这里有没有原先的常量折叠Pass的折叠率数据作为对比?

@yuanlehome
Copy link
Contributor Author

yuanlehome commented Nov 8, 2023

swintransformer模型上op折叠率42%(4907/11470),折叠前后性能提升25.7%(70ms/52ms)

这里有没有原先的常量折叠Pass的折叠率数据作为对比?

你说旧IR的实现吧,这比起来其实没有意义,旧模型转换到pir模型后,总op数量和被折叠的数量都不一样。算一下旧的折叠率的话,37%

@zyfncg
Copy link
Contributor

zyfncg commented Nov 8, 2023

你说旧IR的实现吧,这比起来其实没有意义,旧模型转换到pir模型后,总op数量和被折叠的数量都不一样。算一下旧的折叠率的话,37%

从op数量看这个数据确实意义不大,我刚才把折叠率理解成权重数量在折叠前后的对比,新旧IR的权重数量是一致的,如果折叠逻辑对齐的话折叠掉的参数数量应该是相同的,在不相同的时候这里数据或许一定的参考价值。

auto* param_var = scope_->FindVar(param_name);
PADDLE_ENFORCE_NOT_NULL(
param_var,
phi::errors::InvalidArgument("Parameter var not in scope."));
deleted_vars_->push_back(param_name);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里需不需要判断下param有没有被其他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.

done, thanks!

@yuanlehome yuanlehome merged commit 918d38c into PaddlePaddle:develop Nov 9, 2023
28 checks passed
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
* fix shadow_output_op

* update

* rewrite constant_folding_pass

* update

* update

* fix compile

* fix dce

* enhance judgement
SecretXV pushed a commit to SecretXV/Paddle that referenced this pull request Nov 28, 2023
* fix shadow_output_op

* update

* rewrite constant_folding_pass

* update

* update

* fix compile

* fix dce

* enhance judgement
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.

None yet

3 participants