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

[CINN Frontend] Optimize user interface, polish fuse_pass classes #55705

Merged
merged 2 commits into from
Jul 28, 2023

Conversation

jiahy0825
Copy link
Contributor

@jiahy0825 jiahy0825 commented Jul 25, 2023

PR types

Function optimization

PR changes

Others

Description

Pcard-73448

背景与意义

Fusion pass 的初步改造已经完成,然而仍有部分遗留 TODO 待解决,例如 pass 的所有逻辑都写在了文件中:paddle/cinn/hlir/pass/general_fusion_merge_pass.cc。会造成多个问题:

  1. 单个文件有两千多行,可读性非常差,维护成本高;
  2. 用户为 pass 添加融合策略时,难以找到参考实现;
  3. 文件中有非常多的类,类之间没有做拆分,导致类关系不够直观。

因此,迫切需要对该文件进行拆分,本 PR 将 pass 中的每个类都拆分成单独的文件,放到了单独的 general_fusion_merge_pass 文件夹内统一管理,每个类对应一个同名的 .h 文件,便于后续维护,类继承结构如下图所示:

PassUML

新增融合策略

用户新增融合策略时,只需要参考 general_fusion_merge_pass 文件夹中的 .cc 文件,完成三处定义,即可为 fusion_merge pass 新增一个融合策略,例如 default_horizontal_fuse_pass.cc 中定义的融合策略如下:

namespace cinn {
namespace hlir {
namespace pass {

class DefaultHorizontalFusePass final : public HorizontalFusePass {
 public:
  DefaultHorizontalFusePass() : HorizontalFusePass() {}

  int Benefit() const override {
    // 1. 融合优先级
  }

  void operator()(LightwareFusePassCtx* ctx) const override {
    // 2. 融合策略
  }
};

}  // namespace pass
}  // namespace hlir
}  // namespace cinn

// 3. 注册融合策略
CINN_REGISTER_FUSION_PASS(DefaultHorizontalFusePass,
                          cinn::hlir::pass::DefaultHorizontalFusePass);

Background and meaning

The initial version of the fusion pass has been completed, but some legacy TODOs are still unresolved. For example, all the pass logic is written in the file: paddle/cinn/hlir/pass/general_fusion_merge_pass.cc. This may cause multiple problems:

  1. A single file has more than 2,000 lines, the readability is very poor, and the maintenance cost is high;
  2. It is difficult to find the reference implementation when the user adds a fusion strategy for the pass;
  3. There are a lot of classes in one file, and no split between the classes, which makes the class relationship not intuitive enough.

Therefore, it is urgent to split the file. This PR splits each class in the pass into a separate file and puts it in a separate general_fusion_merge_pass folder for unified management. Each class corresponds to a . h file, which is convenient for subsequent maintenance. The class inheritance structure is shown in the figure above.

Add fusion strategy

When users add a fusion strategy, they only need to refer to the .cc file in the general_fusion_merge_pass folder and complete the three definitions to add a fusion strategy for the fusion_merge pass, such as the fusion strategy defined in default_horizontal_fuse_pass.cc as above.

@jiahy0825 jiahy0825 force-pushed the polish-fusion-class branch from 50e40a3 to 466af18 Compare July 27, 2023 02:42
@jiahy0825 jiahy0825 force-pushed the polish-fusion-class branch from 466af18 to 60d89e1 Compare July 27, 2023 02:44
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@jiahy0825 jiahy0825 merged commit 9c10149 into PaddlePaddle:develop Jul 28, 2023
wz1qqx pushed a commit to wz1qqx/Paddle that referenced this pull request Jul 31, 2023
…ddlePaddle#55705)

* [CINN Frontend] Optimize user interface, polish fuse_pass classes"

* Fix some compile error
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