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

[PIR+CINN]Fix cinn_op.GroupOp insert bug for WriteAfterRead #62529

Merged
merged 7 commits into from
Mar 10, 2024

Conversation

Aurelius84
Copy link
Contributor

@Aurelius84 Aurelius84 commented Mar 7, 2024

PR types

Bug fixes

PR changes

Others

Description

Pcard-67164

一、问题描述

在CINN后端中,需要对「支持」融合的算子进行图遍历,识别并圈定为GroupOp。遍历过程是基于「图语义」的,意味着待融合的算子在「Program」层面可能相距比较远,而且它们之间夹杂着各自依赖的上游Op。因此如何选取融合后算子的「插入点」是一个非常重要的位置,若插入不恰当,极其容易引入类似编程语言中「使用未定义变量」的错误。

举个例子,如下图的「融合Group」列图中的算子E位置就是错的。

image

二、解决思路

由于融合Group仅发生在Block级别,因此我们只需关心Block内部的算子之间顺序是否「合法」即可,此PR中的思路包含如下定义:

  • 融合算子:即图中「绿色」方框,指可被图遍历,若有连接边则可以融合成一个大GroupOp
  • 上游算子:即图中「灰色」方框,指融合后Group算子的全部上游依赖算子(站在Group上,即『读入』语义)
  • 下游算子,即图中「红色」方框,指融合后Group算子的全部下游依赖算子(站在Group上,即『写出』语义)

整个思路包含三个关键步骤:

  1. 解析整个Group算子的上下游直接依赖的算子
  2. 选取『第一个下游算子』作为『候选』插入Group算子点,在②步骤后,会将GroupOp插入此算子前面(即图中D)
  3. 解析『第一个下游算子』后面所有直接和间接依赖且位置在『D之后』的的『上游算子列表』,依次插入D前面

为什么不处理『D之前』的上游算子列表?
答:从Program的Op-by-Op语义上,对于固定的算子Group,只需要保证所有『读入』的算子在前面,『写出』的算子在后面即可,故我们选择『第一个下游算子D』作为候选插入点,则『D之前』的算子顺序都是正确的,无需处理。

只调整Group算子直接依赖的上游算子是不是就可以了?
答:不可以,要递归解析上游依赖&依赖....所有算子,但限制在:

  • 必须也是此Block内的
  • 必须也是『D之后』的

Copy link

paddle-bot bot commented Mar 7, 2024

你的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.

@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Mar 10, 2024
@PaddlePaddle PaddlePaddle unlocked this conversation Mar 10, 2024
@Aurelius84 Aurelius84 closed this Mar 10, 2024
@Aurelius84 Aurelius84 reopened this Mar 10, 2024
@Aurelius84 Aurelius84 closed this Mar 10, 2024
@Aurelius84 Aurelius84 reopened this Mar 10, 2024
Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

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

LGTM for rectify in pir/include/operaton.h.

@Aurelius84 Aurelius84 merged commit 04f5f59 into PaddlePaddle:develop Mar 10, 2024
30 checks passed
hitywt pushed a commit to hitywt/Paddle that referenced this pull request Mar 11, 2024
…ddle#62529)

* [PIR+CINN]Fix cinn_op.GroupOp insert bug for WriteAfterRead

* fix bug

* refine code

* fix cond typo

* fix std::distance

* add strong verify after build_cinn_pass

* fix typo
hitywt pushed a commit to hitywt/Paddle that referenced this pull request Mar 11, 2024
…ddle#62529)

* [PIR+CINN]Fix cinn_op.GroupOp insert bug for WriteAfterRead

* fix bug

* refine code

* fix cond typo

* fix std::distance

* add strong verify after build_cinn_pass

* fix typo
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.

3 participants