[cherry-pick]Verify the correctness of graph rewrited by GeneratePass #36453
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 types
Others
PR changes
Others
Describe
(cherry picked from PR #36116)
验证GeneratePass在计算子图替换后计算结果正确性,并对相关问题进行修复。
验证场景
本PR中验证其中1、2、3.1场景,对于3.2、3.3于后续PR支持及验证
PR涉及功能验证及问题修复
1. 完整的Python注册Pass到完成获取Pass实例对Graph进行子图替换
功能验证点:
RegisterPass
注册的Pass,通过core.get_pass
可以获取到实例且能对Graph进行操作。存在问题及解决方案:
RegisterPassHelper
类成员中保存每个实例,用以确保Python侧对象的生命周期。2. 简单的fusion场景
2.1 Paddle Python API开发时无需指定:input_specs、算子属性
以融合两个add算子为例,简单的示意图如下:
使用Paddle API创建该Pass代码如下:
为验证该Pass可以正确替换子图并得到相同的计算结果,使用如下图中的计算图来验证:
对于上面的计算图验证点如下:
2.2 辅助类型开发时无需指定:匹配/替换子图中的属性限制
仍使用2.1中示例的Pass及测试方式,不同的是使用辅助类型注册Pass,代码如下:
2.3 替换子图可以为仅有输入输出情况,意味着该Pass将删除匹配子图
以删除连续两个
transpose
算子为例,简单的示意图如下:使用辅助类型注册该Pass代码如下:
为验证该Pass可以正确替换子图并得到相同的计算结果,使用如下图中的计算图来验证:
对于上面的计算图验证点如下:
2.4 匹配图与替换图可任意使用一种方式开发
仍使用2.1中示例的Pass及测试方式,不同的是使用辅助类型注册Pass,代码如下:
3.1 Paddle Python API开发时指定且参与匹配替换限制:input_specs、算子属性
3.1.1 示例1:融合多个matmul算子
该示例Pass的简单示意图如下:
3.1.2 示例2:融合连续的transpose算子
与2.3中示例类似,改用Paddle Python API定义子图,并且指定输入的shape及transpose算子的参数设置。