[Inference] add add_shadow_output_after_dead_parameter_pass#68476
Merged
yuanlehome merged 4 commits intoPaddlePaddle:developfrom Sep 27, 2024
Merged
Conversation
f380c85 to
bf44ca0
Compare
12 tasks
Contributor
vivienfanghuagood
left a comment
There was a problem hiding this comment.
一定需要新增shadow_output才能避免被DCE裁剪吗?
Contributor
Author
目前没有想到更好的解决思路。 |
zyfncg
reviewed
Sep 27, 2024
Comment on lines
+23
to
+37
| class AddShadowOutputAfterDeadParameterPattern | ||
| : public pir::OpRewritePattern<pir::ParameterOp> { | ||
| public: | ||
| using pir::OpRewritePattern<pir::ParameterOp>::OpRewritePattern; | ||
| bool MatchAndRewrite( | ||
| pir::ParameterOp op, | ||
| pir::PatternRewriter& rewriter) const override { // NOLINT | ||
| if (!op->use_empty()) { | ||
| return false; | ||
| } | ||
| rewriter.SetInsertionPointToBlockEnd(op->GetParent()); | ||
| rewriter.Build<pir::ShadowOutputOp>(op->result(0), op.param_name()); | ||
| return true; | ||
| } | ||
| }; |
Contributor
There was a problem hiding this comment.
这样是不是等于所有ParameterOp都不会DCE了?
Contributor
Author
There was a problem hiding this comment.
不是,推理这边是把这个pass放在所有pass之前执行的,只会对原始模型里的ParameterOp不DCE
zyfncg
approved these changes
Sep 27, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Category
Inference
PR Types
Others
Description
pcard-71500
为什么需要这么一个Pass?
为了适配paddle.nn.Layer.register_buffer的功能。register_buffer会在模型结构中产出一个“dead”的builtin.parameter_op,也就是说它的输出没有被使用到(这是因为动转静保存模型时并不会对这类op做裁剪PR#68442),在IR中我们认为其是dead code。如果不加以处理,在推理的IR优化过程中会被dead_code_elimination_pass给删除掉,这是不符合预期的行为。因此添加add_shadow_output_after_dead_parameter_pass,在这个dead op后添加一个shadow_output op用来标识parameter_op的输出有被使用到,因此就不会被dead_code_elimination_pass给误删除了。