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

[DRR] C++ DRR (Declarative Rewrite Rule) of Paddle #55859

Merged
merged 232 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
232 commits
Select commit Hold shift + click to select a range
e41ee6d
fix cudnn 8.7+ bug on cudnnConvolutionBiasActivationForward
yuanlehome Jul 13, 2023
9ecefe9
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Jul 14, 2023
d092636
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Jul 17, 2023
b74a6df
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Jul 24, 2023
9ffdf8e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Aug 1, 2023
2ebb74e
Merge branch 'develop' of https://github.com/yuanlehome/Paddle into d…
yuanlehome Aug 1, 2023
4350ddb
add drr_rewrite_pattern.h and remove_redundent_reshape demo
yuanlehome Aug 1, 2023
665458d
add drr_context and pattern_graph class
zyfncg Aug 1, 2023
5068af5
Merge pull request #2 from zyfncg/my_drr
yuanlehome Aug 1, 2023
577161d
add test case
zyfncg Aug 1, 2023
4d90c86
fix cmake file
zyfncg Aug 1, 2023
37d179b
Merge pull request #3 from zyfncg/my_drr
yuanlehome Aug 1, 2023
445210e
fix compile bug
zyfncg Aug 2, 2023
29c69f0
fix runtime bug and refine code
zyfncg Aug 2, 2023
4691895
Merge pull request #4 from zyfncg/my_drr
yuanlehome Aug 2, 2023
ac346c0
add MatchContext
zyfncg Aug 3, 2023
984ca9d
update code
zyfncg Aug 7, 2023
0dc6c0c
add impl of tensor_interface
zyfncg Aug 7, 2023
b86c832
fix compile bug
zyfncg Aug 8, 2023
727a5c1
Merge pull request #6 from zyfncg/drr_attr
yuanlehome Aug 8, 2023
61ff236
change smart ptr to pointor
gongshaotian Aug 8, 2023
b988494
change smart to pointor
gongshaotian Aug 8, 2023
e5deff3
change smart to pointor
gongshaotian Aug 8, 2023
4608b5f
Replace 'weak_ptr' with pointer
gongshaotian Aug 9, 2023
2d4df1a
modify weak_ptr use count==0 judgment logic
gongshaotian Aug 9, 2023
dd6bd6d
change smart to pointor
gongshaotian Aug 8, 2023
7279d74
Merge pull request #7 from gongshaotian/drr
yuanlehome Aug 9, 2023
344645a
Merge branch 'develop' into drr
yuanlehome Aug 9, 2023
728d856
Merge branch 'drr' of https://github.com/gongshaotian/Paddle into drr
gongshaotian Aug 9, 2023
d1299b9
add match
yuanlehome Aug 9, 2023
a1c1e3f
add match
yuanlehome Aug 9, 2023
43e1f02
remove OperationInterface
yuanlehome Aug 9, 2023
68f2863
update
yuanlehome Aug 9, 2023
5d1af8c
Add Rewrite impl of DrrRewritePattern
zyfncg Aug 10, 2023
cfa6b02
Merge branch 'drr' of https://github.com/gongshaotian/Paddle into drr
gongshaotian Aug 10, 2023
7abe8db
refine code
yuanlehome Aug 10, 2023
016b55e
fix compile bug
zyfncg Aug 10, 2023
08f6549
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_ir
zyfncg Aug 10, 2023
b2b9998
rename ir_value to get in IrValue
zyfncg Aug 10, 2023
4d0d26a
fix header include
zyfncg Aug 10, 2023
4099c7a
Merge pull request #8 from zyfncg/drr_ir
yuanlehome Aug 10, 2023
a84b91c
add CreateOperation template demo
yuanlehome Aug 11, 2023
dedf54b
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr
gongshaotian Aug 11, 2023
ced158f
Add GraphTopo class in pattern_graph
gongshaotian Aug 11, 2023
1e3d00e
Reimplementing the GraphTopo class using queue
gongshaotian Aug 11, 2023
9ced727
Reimplementing the GraphTopo class using queue
gongshaotian Aug 11, 2023
d0a932d
Optimize the access method of visited tensor
gongshaotian Aug 14, 2023
78bf7b6
Considering that the inputs of opcall may be empty
gongshaotian Aug 14, 2023
98e5d52
Merge pull request #10 from gongshaotian/drr_queue_topo
yuanlehome Aug 15, 2023
a98230c
Overloading the operator() method of Op, supporting dual tensor inputs
gongshaotian Aug 15, 2023
9e68c4d
support attr
zyfncg Aug 15, 2023
e52472f
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_ir
zyfncg Aug 15, 2023
78b60df
Merge commit 'refs/pull/11/head' of https://github.com/yuanlehome/Pad…
zyfncg Aug 15, 2023
b929f26
Merge pull request #11 from gongshaotian/op_extension
yuanlehome Aug 15, 2023
2425095
1. Add Op class support for multi input and multi output function. 2.…
gongshaotian Aug 16, 2023
7306e85
1. Add transferOP in createOption func
gongshaotian Aug 16, 2023
998c30d
fix bug
zyfncg Aug 16, 2023
1d80ce9
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_ir
zyfncg Aug 16, 2023
0f1b187
fix NotifyOperationRemoved
zyfncg Aug 17, 2023
341fd65
refine code
zyfncg Aug 17, 2023
331d9a6
Fix axis bug in perm
gongshaotian Aug 17, 2023
385b7d1
mupdate share_ptr
zyfncg Aug 17, 2023
25d4a0a
Merge pull request #13 from zyfncg/drr_ir
yuanlehome Aug 17, 2023
7b8cba9
Merge branch 'PaddlePaddle:develop' into drr
yuanlehome Aug 17, 2023
25bd0c0
update
yuanlehome Aug 17, 2023
a3b3895
refine drr_test ut
yuanlehome Aug 17, 2023
8312452
merge drr-pr#13
gongshaotian Aug 17, 2023
82242b7
Modify according to review
gongshaotian Aug 17, 2023
c148e81
modify reshape_op
zyfncg Aug 17, 2023
b66f611
Merge commit 'refs/pull/14/head' of https://github.com/yuanlehome/Pad…
zyfncg Aug 17, 2023
5282144
Merge pull request #14 from gongshaotian/op_extension
yuanlehome Aug 17, 2023
c99a9cd
format code
yuanlehome Aug 17, 2023
15464bb
support vector<int> for attr
zyfncg Aug 17, 2023
63383f5
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_opt
zyfncg Aug 18, 2023
2a7c68f
fix drr test
yuanlehome Aug 18, 2023
3ddb601
Merge pull request #15 from zyfncg/drr_opt
yuanlehome Aug 18, 2023
1af27c5
refine code
yuanlehome Aug 18, 2023
6b9e789
Resolve compilation loop dependencies
yuanlehome Aug 18, 2023
fb22619
add RequireNativeCall
zyfncg Aug 18, 2023
11a452f
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_n…
zyfncg Aug 18, 2023
8587d7c
support native_call in drr api
zyfncg Aug 18, 2023
cead1b7
Merge pull request #16 from zyfncg/drr_native_call
yuanlehome Aug 18, 2023
56f6605
temp tensor prefix fix
yuanlehome Aug 18, 2023
ea94061
refine code
yuanlehome Aug 21, 2023
674acbe
suport Tensor Assgin API in ResultPattern
zyfncg Aug 21, 2023
b2059bc
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_opt
zyfncg Aug 21, 2023
0f531dc
refine test code
zyfncg Aug 21, 2023
1c3ab40
Merge pull request #17 from zyfncg/drr_opt
yuanlehome Aug 21, 2023
49d287c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Aug 22, 2023
78fecfa
refactor ther drr_pattern class
zyfncg Aug 22, 2023
bb1da4b
refine test case
zyfncg Aug 22, 2023
9eeb511
rename DrrPatternBuilder to DrrPatternBase
zyfncg Aug 22, 2023
f0751d6
Merge pull request #18 from zyfncg/drr_pass
yuanlehome Aug 22, 2023
ef3b5ca
fix compile bug
zyfncg Aug 22, 2023
3327a3e
adjust include
yuanlehome Aug 22, 2023
ce5929d
Add log info in DrrRewritePattern
zyfncg Aug 22, 2023
f3b71c2
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Aug 22, 2023
e92436e
Merge pull request #19 from zyfncg/drr_pass
yuanlehome Aug 22, 2023
7a862b4
use ir::get_type_name
yuanlehome Aug 22, 2023
16f066a
use ir::get_type_name
yuanlehome Aug 22, 2023
a1d8c7e
support compute attrbute in drr pattern
zyfncg Aug 23, 2023
7009ac4
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_attr
zyfncg Aug 23, 2023
cd8af21
refine code
zyfncg Aug 23, 2023
524f15f
Merge pull request #20 from zyfncg/drr_attr
yuanlehome Aug 23, 2023
fed2f8b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Aug 24, 2023
adcc73c
Add fusion testing code for fullOp and expandOp
Aug 25, 2023
35c9390
Standardize code format
gongshaotian Aug 25, 2023
da03ecb
Replace IR_THROW() with PADDLE_THROW()
gongshaotian Aug 25, 2023
c251ca9
Merge pull request #21 from gongshaotian/drr
yuanlehome Aug 25, 2023
9964567
refine code
yuanlehome Aug 28, 2023
4749ecd
add attention fuse demo
yuanlehome Aug 28, 2023
d0c17ea
update
yuanlehome Aug 29, 2023
78c33b2
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Aug 29, 2023
cc642a5
fix compile error
yuanlehome Aug 29, 2023
df6f824
add multihead_matmul fuse pattern
yuanlehome Aug 29, 2023
5aeec24
fix multihead_matmul
yuanlehome Aug 29, 2023
e959e60
Update drr_attention_fuse_test.cc
gongshaotian Aug 29, 2023
5b6717d
Merge pull request #22 from gongshaotian/drr
yuanlehome Aug 30, 2023
ae3acc9
fix drr_attention_fuse_test compile
yuanlehome Aug 30, 2023
51ba73c
add fused_gemm_epilogue in drr
zyfncg Aug 30, 2023
98b57e1
attr support std::vector<int64_t>
yuanlehome Aug 30, 2023
ed332d3
add debug log
yuanlehome Aug 30, 2023
dc7aae6
update
yuanlehome Aug 30, 2023
f8fe52d
fix some bug
zyfncg Aug 31, 2023
d591592
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Aug 31, 2023
f3c1d88
Merge pull request #23 from zyfncg/drr_pass
yuanlehome Sep 1, 2023
728d65f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 1, 2023
9235c31
fix confilct
zyfncg Sep 1, 2023
91a5036
Merge pull request #24 from zyfncg/drr_pass
yuanlehome Sep 1, 2023
185e0b2
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr
yuanlehome Sep 1, 2023
8ed5165
support subgraph replace in source pattern graph for drr
zyfncg Sep 4, 2023
ae5950a
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 4, 2023
92a3606
Improve the implementation of Drr and multihead_matmul_fuse_pass
yuanlehome Sep 4, 2023
58804eb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 4, 2023
0882e9f
add ReorderBlockOpsPass
zyfncg Sep 5, 2023
55d4a95
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Sep 5, 2023
789d8c8
fix drr_attention_fuse_pass
yuanlehome Sep 5, 2023
6311786
update
yuanlehome Sep 5, 2023
ede8fff
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 6, 2023
cf62d06
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Sep 6, 2023
31b7407
Merge pull request #26 from zyfncg/merge_dev
yuanlehome Sep 6, 2023
b320ba4
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 6, 2023
749ebce
update reorder_block_ops_pass
zyfncg Sep 6, 2023
4c6f31e
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Sep 6, 2023
8550e00
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 6, 2023
e3c8a76
revert fusedgemm
yuanlehome Sep 6, 2023
6693c8e
update
yuanlehome Sep 6, 2023
623ed05
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 6, 2023
c424305
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 7, 2023
b17942f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 7, 2023
1a05a2b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 7, 2023
7260c1e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 8, 2023
fb5f482
Add Bottom2UpMatch() func
gongshaotian Sep 8, 2023
6e88522
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Sep 8, 2023
4fa16af
merge code
zyfncg Sep 8, 2023
f641a9a
fix bug
gongshaotian Sep 10, 2023
1afe1fc
add log & fix bug
gongshaotian Sep 11, 2023
9c9c16b
refine cpp type trait
zyfncg Sep 11, 2023
0833a06
Merge pull request #25 from zyfncg/drr_pass
yuanlehome Sep 11, 2023
bd3faea
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Sep 11, 2023
fe1325f
using oprand() & num_oprand() replace oprands()
gongshaotian Sep 11, 2023
c610650
fix conflict
yuanlehome Sep 11, 2023
ef3e954
Merge branch 'drr' of https://github.com/gongshaotian/Paddle into drr
gongshaotian Sep 11, 2023
2b59813
fix compile
yuanlehome Sep 11, 2023
f4e9728
fix pd.xxx to pd_op.xxx
yuanlehome Sep 11, 2023
2b2eba9
fix bug of delete op in drr
zyfncg Sep 11, 2023
bceb977
Merge pull request #29 from zyfncg/drr_pass
yuanlehome Sep 11, 2023
64bcf90
add PatternGraphMatchV2 & FindOutputOp func
gongshaotian Sep 13, 2023
ef7700e
refactor ir operation creator
zyfncg Sep 13, 2023
7876fe8
fix include pir
gongshaotian Sep 13, 2023
d18b803
Merge branch 'temp' into drr
gongshaotian Sep 13, 2023
2ad0975
fix ir
gongshaotian Sep 13, 2023
288b33c
merging
gongshaotian Sep 13, 2023
67a2dfe
Split out dfsvisitor func from FindOutputOp func
gongshaotian Sep 13, 2023
a517dad
fix bug
gongshaotian Sep 14, 2023
b00ddd1
fix output op in source pattern bug
gongshaotian Sep 14, 2023
235be4f
Debugging drr_test drr_attention_fuse_test passed!
gongshaotian Sep 14, 2023
1c8b077
Debugging drr_fuse_linear_test passed!
gongshaotian Sep 15, 2023
5f24312
Optimize the PatternGraphMatchV2 function interface and overload the …
gongshaotian Sep 18, 2023
aca0d4b
Modify comments and function names
gongshaotian Sep 18, 2023
08cf893
auto code-gen for creating ir operation in drr
zyfncg Sep 18, 2023
c6e6b29
delete debug log
zyfncg Sep 18, 2023
a03896e
optimize the interface of MatchFromOutputToInput()
gongshaotian Sep 18, 2023
b3c7d26
Optimize SourcePatternGraph::OutputNodes judgment logic
gongshaotian Sep 18, 2023
9de0a04
polish code
zyfncg Sep 18, 2023
9f59cdd
Merge pull request #30 from zyfncg/drr_pass
yuanlehome Sep 18, 2023
46a734a
using default operator=() in MatchContextImpl
gongshaotian Sep 18, 2023
5239e7d
Merge pull request #32 from gongshaotian/drr_dfs_test
yuanlehome Sep 18, 2023
10dba83
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Sep 20, 2023
43d5e48
Merge pull request #33 from zyfncg/merge_dev
yuanlehome Sep 20, 2023
224e4b5
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Sep 21, 2023
85622b9
fix merge conflict
zyfncg Sep 22, 2023
b731d39
Merge pull request #34 from zyfncg/drr_pass
yuanlehome Sep 22, 2023
0fe5784
create test case: drr_same_name_test
gongshaotian Sep 25, 2023
ba9f314
fix duplicate binding of ir op bug
gongshaotian Sep 25, 2023
dbef1ff
Merge branch 'drr' of https://github.com/gongshaotian/Paddle into drr
gongshaotian Sep 25, 2023
b3bcc66
Rename drr_same_name_test to drr_same_type_binding_test & Add graphic…
gongshaotian Sep 26, 2023
17b39f9
Merge pull request #35 from gongshaotian/drr
yuanlehome Sep 26, 2023
8d31696
Merge branch 'develop' into drr
yuanlehome Sep 26, 2023
78eb5bc
refactor logic of insert point for creating new operation in drr
zyfncg Sep 27, 2023
91240ea
Merge pull request #36 from zyfncg/drr_pass
yuanlehome Sep 27, 2023
a984879
update
yuanlehome Oct 8, 2023
3df2f26
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 8, 2023
d624336
fix compile error
yuanlehome Oct 8, 2023
4993a22
fix some bug
zyfncg Oct 10, 2023
8df68b8
Merge pull request #37 from zyfncg/drr_pass
yuanlehome Oct 10, 2023
3b6cc17
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 10, 2023
3e166f2
fix codestyle
yuanlehome Oct 10, 2023
d3bc65d
fix bug
zyfncg Oct 10, 2023
c985ec7
Merge pull request #38 from zyfncg/drr_pass
yuanlehome Oct 10, 2023
b58a9d9
Update anchor node judgment logic
gongshaotian Oct 10, 2023
bffd25d
Merge pull request #39 from gongshaotian/drr
yuanlehome Oct 10, 2023
affa0fb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 10, 2023
e9db735
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 11, 2023
4bac4b1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 11, 2023
3aabfe8
fix bug of link pir
zyfncg Oct 13, 2023
77e8138
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Oct 13, 2023
7422169
Merge pull request #40 from zyfncg/drr_pass
yuanlehome Oct 13, 2023
a21c96b
fix codestyle
yuanlehome Oct 13, 2023
6330309
self review v1
yuanlehome Oct 17, 2023
8a0f5b4
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 17, 2023
2c6fb1e
refine code format
zyfncg Oct 17, 2023
2866cb5
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr_pass
zyfncg Oct 17, 2023
c80faa8
Merge pull request #41 from zyfncg/drr_pass
yuanlehome Oct 17, 2023
b0f9ce0
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr
yuanlehome Oct 17, 2023
e9bb397
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yuanlehome Oct 17, 2023
55573f8
set thread_local for count in op class
zyfncg Oct 17, 2023
ad3764f
fix compile on mac
yuanlehome Oct 17, 2023
6d4ab15
remove unused .h in value.cc
yuanlehome Oct 17, 2023
0430ba0
Merge pull request #42 from zyfncg/drr_pass
yuanlehome Oct 17, 2023
a1c093b
Merge branch 'drr' of https://github.com/yuanlehome/Paddle into drr
yuanlehome Oct 17, 2023
61e4d31
fix compile
yuanlehome Oct 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/fluid/pir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(dialect)
add_subdirectory(transforms)
add_subdirectory(drr)
166 changes: 166 additions & 0 deletions paddle/fluid/pir/dialect/op_generator/op_creator_drr_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

import yaml
from op_gen import OpCompatParser, OpInfoParser, to_pascal_case

CPP_FILE_TEMPLATE = """
#include "paddle/fluid/pir/drr/ir_operation_factory.h"

#include "paddle/fluid/pir/dialect/operator/ir/pd_op.h"
#include "paddle/fluid/pir/dialect/operator/ir/manual_op.h"

namespace pir {{
namespace drr {{

void OperationFactory::RegisterGeneratedOpCreator() {{
{body}
}}

}} // namespace drr
}} // namespace pir

"""

NORMAL_FUNCTION_TEMPLATE = """
RegisterOperationCreator(
"{op_name}",
[](const std::vector<Value>& inputs,
const pir::AttributeMap& attrs,
pir::PatternRewriter& rewriter) {{
return rewriter.Build<paddle::dialect::{op_class_name}>(
{params_code});
}});
"""

MUTABLE_ATTR_FUNCTION_TEMPLATE = """
RegisterOperationCreator(
"{op_name}",
[](const std::vector<Value>& inputs,
const pir::AttributeMap& attrs,
pir::PatternRewriter& rewriter) {{
// mutable_attr is tensor
if (inputs.size() > {inputs_num}) {{
return rewriter.Build<paddle::dialect::{op_class_name}>(
{params_code_with_mutable_attr});
}} else {{
return rewriter.Build<paddle::dialect::{op_class_name}>(
{params_code_no_mutable_attr});
}}
}});
"""


class OpCreatorCodeGen:
def __init__(self, op_yaml_files, op_compat_yaml_file, dialect_name):
self.op_info_items = self.parse_yaml(op_yaml_files, op_compat_yaml_file)
self.dialect_name = dialect_name

def parse_yaml(self, op_yaml_files, op_compat_yaml_file):
op_compat_parser = OpCompatParser(op_compat_yaml_file)

op_yaml_items = []
for yaml_file in op_yaml_files:
with open(yaml_file, "r") as f:
ops = yaml.safe_load(f)
op_yaml_items = op_yaml_items + ops
op_info_items = []
for op in op_yaml_items:
op_compat_item = op_compat_parser.get_compat(op['name'])
if (
op_compat_item is not None
and op_compat_item['op'] == "pow"
and 'scalar' in op_compat_item
):
op_compat_item = op_compat_item.pop('scalar')
op_info_items.append(OpInfoParser(op, op_compat_item))
return op_info_items

def gen_cpp_file_code(self, cpp_file_path):
body_code = ""
for op_info_item in self.op_info_items:
if op_info_item.infer_meta_map is None:
continue
for phi_op_name in op_info_item.op_phi_name:
ir_op_name = self.dialect_name + "." + phi_op_name
params_no_mutable_attr = []
for i in range(len(op_info_item.input_name_list)):
params_no_mutable_attr.append(
f"inputs[{i}].dyn_cast<pir::OpResult>()"
)
if len(op_info_item.attribute_name_list) > 0:
params_no_mutable_attr.append("attrs")

if len(op_info_item.mutable_attribute_name_list) == 0:
body_code += NORMAL_FUNCTION_TEMPLATE.format(
op_name=ir_op_name,
op_class_name=(to_pascal_case(phi_op_name) + "Op"),
params_code=", ".join(params_no_mutable_attr),
)
else:
params_with_mutable_attr = []
for i in range(
len(op_info_item.input_name_list)
+ len(op_info_item.mutable_attribute_name_list)
):
params_with_mutable_attr.append(
f"inputs[{i}].dyn_cast<pir::OpResult>()"
)
if len(op_info_item.attribute_name_list) > len(
op_info_item.mutable_attribute_name_list
):
# TODO(zyfncg): Currently Op::Build Interface doesn't support this case.
continue
# params_with_mutable_attr.append("attrs")

body_code += MUTABLE_ATTR_FUNCTION_TEMPLATE.format(
op_name=ir_op_name,
inputs_num=len(op_info_item.input_name_list),
op_class_name=(to_pascal_case(phi_op_name) + "Op"),
params_code_with_mutable_attr=",".join(
params_with_mutable_attr
),
params_code_no_mutable_attr=", ".join(
params_no_mutable_attr
),
)

with open(cpp_file_path, 'w') as f:
f.write(CPP_FILE_TEMPLATE.format(body=body_code))


def ParseArguments():
parser = argparse.ArgumentParser(
description='Generate Op Creator Files By Yaml'
)
parser.add_argument('--op_yaml_files', type=str)
parser.add_argument('--op_compat_yaml_file', type=str)
parser.add_argument('--dialect_name', type=str)
parser.add_argument('--op_creator_file', type=str)
return parser.parse_args()


if __name__ == '__main__':
args = ParseArguments()
op_yaml_files = args.op_yaml_files.split(",")
op_compat_yaml_file = args.op_compat_yaml_file
op_creator_file = args.op_creator_file
dialect_name = args.dialect_name

code_gen = OpCreatorCodeGen(
op_yaml_files, op_compat_yaml_file, dialect_name
)
code_gen.gen_cpp_file_code(op_creator_file)
65 changes: 65 additions & 0 deletions paddle/fluid/pir/drr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
file(GLOB DRR_SRCS "*.cc" "api/*.cc")

set(op_creator_gen_file
${PADDLE_SOURCE_DIR}/paddle/fluid/pir/dialect/op_generator/op_creator_drr_gen.py
)
set(op_compat_yaml_file ${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/op_compat.yaml)
set(op_forward_yaml_file1
Copy link
Contributor

Choose a reason for hiding this comment

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

目前在如下目录中还存在一个 yaml 文件,该 yaml 文件中定义了 pir 单独定义的算子,是否需要纳入该自动生成体系中?:paddle/fluid/pir/dialect/operator/ir/ops.yaml

Copy link
Contributor

Choose a reason for hiding this comment

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

image

paddle/fluid/pir/dialect/operator/ir/ops.yaml应该是加入进来了,对应的是这两个解析后的文件吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可能需要,这个我们后面加上~

${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/ops.parsed.yaml
)
set(op_forward_yaml_file2
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/legacy_ops.parsed.yaml
)
set(op_backward_yaml_file1
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/backward_ops.parsed.yaml
)
set(op_backward_yaml_file2
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/legacy_backward_ops.parsed.yaml
)
set(fused_op_forward_yaml_file
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/fused_ops.parsed.yaml
)
set(fused_op_backward_yaml_file
${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/fused_backward.parsed.yaml
)

set(parsed_op_dir
${PADDLE_SOURCE_DIR}/paddle/fluid/pir/dialect/operator/ir/generated)

set(op_yaml_file3 ${parsed_op_dir}/ops.parsed.yaml)
set(op_yaml_file4 ${parsed_op_dir}/ops_backward.parsed.yaml)

set(op_yaml_files
${op_forward_yaml_file1},${op_forward_yaml_file2},${op_backward_yaml_file1},${op_backward_yaml_file2},${fused_op_forward_yaml_file},${fused_op_backward_yaml_file},${op_yaml_file3},${op_yaml_file4}
)

set(op_creator_file
${PADDLE_BINARY_DIR}/paddle/fluid/pir/drr/ir_op_factory_generated.cc)
set(op_creator_file_tmp ${op_creator_file}.tmp)

set(dialect_name pd_op)

add_custom_command(
OUTPUT ${op_creator_file}
COMMAND
${PYTHON_EXECUTABLE} ${op_creator_gen_file} --op_yaml_files ${op_yaml_files}
--op_compat_yaml_file ${op_compat_yaml_file} --dialect_name ${dialect_name}
--op_creator_file ${op_creator_file_tmp}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${op_creator_file_tmp}
${op_creator_file}
COMMENT "copy_if_different ${op_creator_file}"
DEPENDS ${op_creator_gen_file}
${op_forward_yaml_file1}
${op_forward_yaml_file2}
${op_backward_yaml_file1}
${op_backward_yaml_file2}
${op_compat_yaml_file}
${op_yaml_file3}
${op_yaml_file4}
pd_op_dialect_op
VERBATIM)

cc_library(
drr
SRCS ${DRR_SRCS} ${op_creator_file}
DEPS pd_op_dialect pir)
41 changes: 41 additions & 0 deletions paddle/fluid/pir/drr/api/drr_pattern_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/fluid/pir/drr/api/drr_pattern_context.h"
#include "paddle/fluid/pir/drr/drr_rewrite_pattern.h"

namespace pir {
namespace drr {

template <typename DrrPattern>
class DrrPatternBase {
public:
virtual ~DrrPatternBase() = default;

// Define the Drr Pattern.
virtual void operator()(pir::drr::DrrPatternContext* ctx) const = 0;

std::unique_ptr<DrrRewritePattern<DrrPattern>> Build(
pir::IrContext* ir_context, pir::PatternBenefit benefit = 1) const {
DrrPatternContext drr_context;
this->operator()(&drr_context);
return std::make_unique<DrrRewritePattern<DrrPattern>>(
drr_context, ir_context, benefit);
}
};

} // namespace drr
} // namespace pir
Loading