[cherry-pick]C++ support register pass via PassDesc #36302
Merged
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
New features
PR changes
Others
Describe
(cherry picked from PR #36095)
PR主要功能:支持C++开发注册GeneratePass,简化针对fusion等子图优化场景开发方式。
背景
#35602 #35708 提供Python侧开发子图替换类Pass的方式:
本PR在C++侧提供类似Python开发注册GeneratePass的API。
方案设计
定义匹配/替换子图
Python侧支持使用两种方式用于开发Pass,类似定义program的方式来定义匹配/替换子图:
Python侧定义子图通过定义一个函数(或lambda)完成,函数参数为子图的输入数据,返回值为输出数据,一个示例如下:
Python侧可以直接生成子图ProgramDesc,而C++中需要实现相同的功能使用的API较为复杂,因此方案采用如下方式实现类似Python侧的子图定义功能
VarHelper
、OpHelper
、SubgraphHelper
三个辅助类型完成定义子图转换ProgramDesc功能;VAR_
、OP_
、SUBGRAPH_
宏创建对应上述三个辅助类型对象,从而尽可能简化代码量及内部细节;C++上定义子图示例如下:
注册GeneratePass
Python侧Pass注册使用装饰器
RegisterPass
完成,将注册函数传递到C++中,在获取Pass实例时调用获取protobuf序列化数据,注册代码示例如下:C++中使用类似的注册方式,使用宏
REGISTER_GENERATE_PASS
完成Pass的注册,其参数表示该pass的类型,代码示例如下: