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
[PIR] Add Common Subexpression Elimination pass #63960
Open
SigureMo
wants to merge
29
commits into
PaddlePaddle:develop
Choose a base branch
from
cattidea:pir/add-cse-pass
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+919
−6
Conversation
This file contains 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
你的PR提交成功,感谢你对开源项目的贡献! |
SigureMo
requested review from
wanghuancoder,
Aurelius84,
XiaoguangHu01 and
qili93
as code owners
May 9, 2024 17:16
SigureMo
changed the title
[WIP][PIR] Add Common Subexpression Elimination pass
[PIR] Add Common Subexpression Elimination pass
May 14, 2024
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
Execute Infrastructure
PR Types
Performance
Description
添加 CSE(公共子表达式消除)pass
为部分 OP 添加
SideEffectTrait
(包括 random OP,会改变全局状态,包括 print OP,会对 stdout 造成影响,这些都是 SideEffect,非 pure)目前 CSE 在动转静前反向 Program 分别 apply,需使用
FLAGS_enable_cse_in_dy2st=1
启用,本 PR 不默认启用,之后 PR 会考虑改为默认FLAGS_cse_max_count
用于 debug,快速二分哪个 OP 替换导致的精度问题,为实验性 flagCSE 具体方案如下:
对于如图所示 case,两个
op b
之所以能替换,根本原因是它们的输出是相同的,而这个相同的保证是它们的输入相同,且 OP(类型、属性)是相同的因此对于一个表达式树而言,是公共子表达式的判别条件即为「叶子结点相同」+「OP 拓扑相同」,为了能够计算这一点,对于两个
op b
而言,它会递归去计算其上游 OP(即op a
)是否相同,当然,op a
的输出的比较并不会用Value Id
,而是比较 OP 以及是该 OP 的第几个输出,当这两个相同的时候,就可以保证该 Value 是相同的这里实现是基于哈希的,
hash(op) = hash(inputs) ^ hash(op_name) ^ hash(op_attrs)
,hash(value) = hash(defining_op) ^ hash(idx)
,hash(terminate_value) = hash(value_id)
,实现这几条 hash 计算即可判断两个 OP 是否是公共子表达式由于 OP 是基于拓扑序遍历的,因此下游 OP 的上游 OP 信息一定会提前计算好,因此只需要
O(N)
即可计算全部 OP hash 信息,只有在发生哈希冲突的时候才需要 compare(本 PR 尚未实现)对于含子 block 的情况,子 block 会自动「继承」父 block 已经计算好的表达式信息,父子 block 类似于作用域的可见性,子 block 天然可见父 block 的表达式,那么就可以使用父 block 的表达式来替换子 block 的,反之则因为可见性而不支持
TODOs
本 PR
Value.impl()
作为 id之后 PR,非重要问题将会在下个 PR 修复
unordered_map
,以自动解决哈希冲突PCard-66972