-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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] Strong constraint branch #58719
[CINN] Strong constraint branch #58719
Conversation
0d9def1
to
8f5f9ba
Compare
由于原 PR ( #57543 )的 cla 检查一直处于 pending 状态,将代码合并为一个 commit 并迁移至本 PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
迁移原来 PR #57543 中的 comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for OpLower
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* Strong Constraint Branch * Change UpdateOpLoweredFuncKey location (PaddlePaddle#86) * Remove useless parameter (PaddlePaddle#87) * Change codes according to comments (PaddlePaddle#89) * Delete useless code (PaddlePaddle#91)
* Strong Constraint Branch * Change UpdateOpLoweredFuncKey location (PaddlePaddle#86) * Remove useless parameter (PaddlePaddle#87) * Change codes according to comments (PaddlePaddle#89) * Delete useless code (PaddlePaddle#91)
PR types
New features
PR changes
Others
Description
pcard-76996
一、背景概述
前端 fusion_merge pass 决定了 Group 划分,Group 划分对生成的 kernel 性能有着至关重要的影响。前端的 Group 圈定了哪些 OP 应该放到一个 kernel 里,Group 相当于是对 OP 的一个 view,真正的代码生成逻辑在后端开展。在实际工作时发现,如果 fusion_merge 内的融合策略发生了变化,生成出来的某些 Group,在和后端对接时,程序会直接崩溃,这极为容易引发用户的 panic。
对上述情况进行梳理,可以发现存在的两个关键问题:
现在前后端耦合程度高,问题排查难。需要有一个前后端公认的协议来作为它们之间的桥梁
在强约束分支的设计里,MapExpr 正是这一座桥梁。为了 MapExpr 的正确生成,我们发展了一套关于下标方程组的理论,从理论上保障融合正确性、完备性
二、架构设计
在工程实现上,强约束分支可总结为如下五个关键模块:
三、子模块介绍
3.1 联立器
Q: 如何构造约束方程组?多个方程组之间如何联立?
A: 为算子挂载GenerateEquations多态方法,构造算子的约束方程组
核心代码:
代码示例
OpEquationContext 为用户提供下列接口,基本可以满足构造方程组的需求

3.2 划分器
Q: 如何基于约束方程组,实现算子融合?
A: 约束方程组等价转换为 EquationGraph,在图上基于 AnchorTensor 进行遍历,遍历到的节点表示可以放到同一个 IGroup 内。
核心代码:
从 AnchorTensor 开始对 EquationGraph 进行遍历,遍历到的所有 Tensor 可以放到同一组内。上述过程对 Tensor 进行了划分,实际上融合的粒度却是算子,如何利用划分好的 op 集合,实现算子融合?
IGroup 融合:即算子融合。如果算子的输入输出 Tensor 均在划分出来的 op 集合内,则表明算子属于当前的 IGroup(即 Inline Group,表示 IGroup 内的算子可以内联在一起)。
3.3 判别器
Q: 如何判断约束方程组是否有解?
A: 方程组有解,需要确保:如果有多条路径可以遍历到同一节点,该节点对应的解是唯一的
核心代码:
不可解的例子
3.4 求解器
Q: 约束方程组的解如何定义?
A: 根据约束方程组,求解出每个 Tensor 对应的下标索引表达式。该表达式指明 Tensor 下标和调度描述符的换算关系
Q: 如何求解约束方程组?
A: 从 Schedule Descriptor 开始,遍历 Equation Graph,在遍历的过程中,计算每个 Tensor 对应的表达式
核心代码:
3.5 生成器
Q: 如何根据方程组的解,完成代码生成?
A: 前置步骤已经提供了充分的信息:IGroup 的划分方式 + 每个 Tensor 索引的表达式,本组件只需要根据 MapExpr 的格式,按部就班生成即可。
核心代码:
四、如何运行
4.1 开启 FLAGS_cinn_enable_map_expr:
4.2 执行 python 脚本:
五、输出预览
5.1 简单示例:以 Tensor x 和 y 为输入,执行 elementwise_add 和 reduce 两个算子
5.2 输出结果
5.3 按行解析各字段含义