-
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
support custom vjp trait #57106
Merged
cyber-pioneer
merged 40 commits into
PaddlePaddle:develop
from
lxd-cumt:custom_vjp_trait
Sep 13, 2023
Merged
support custom vjp trait #57106
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
7de4b36
test prim custom vjp in New IR
cyber-pioneer 94cea80
add a new CustomVjpTrait to represent whether an op has custom vjp
lxd-cumt 892b0cd
add has_custom_vjp_op_list to represent ops that have custom vjp
lxd-cumt 37d77c4
parse has_custom_vjp_op_list and autogen CustomVjpTrait for those ops
lxd-cumt 3945ade
add pybind to support checking whether an op has custom vjp in python…
lxd-cumt c5bd381
fix conflicts
lxd-cumt b8e9706
add test
lxd-cumt 31230dd
add test for add op custom vjp
lxd-cumt aae5244
add pybind to support checking whether an op has custom vjp in python…
lxd-cumt 60033d9
fix bugs
lxd-cumt 8338fca
polish code
lxd-cumt 09ca597
fix bugs
lxd-cumt 5f8935a
generate custom_vjp trait based on op list from gen.py
lxd-cumt 401d26b
delete has_custom_vjp_op_list
lxd-cumt 17d9529
fix bugs
lxd-cumt 4fd9fa3
use currently defined list CUSTOM_VJP and VJP_COMPS rather than defin…
lxd-cumt 93b2322
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt a0417b6
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt d6cb7b5
fix ctest
lxd-cumt 20dfa96
fix bugs
lxd-cumt ccbd60b
divide vjp into prim_vjp and custom vjp
lxd-cumt 87fa37b
fix conflicts
lxd-cumt c614b85
add code comments
lxd-cumt 7cfaa72
add code comments
lxd-cumt eee9c41
polish codes
lxd-cumt 3b0ae3e
polish code comments
lxd-cumt bfd1f5a
polish codes
lxd-cumt ff3861b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt d447056
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt ebc8754
fix conflicts with <rename ir into pir>
lxd-cumt 877cc7c
fix bugs
lxd-cumt 877b9ac
add code comments
lxd-cumt d96340d
fix bugs
lxd-cumt 8403398
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt 54952c1
add custom vjp trait support in new folder
lxd-cumt 3d75a25
fix bugs
lxd-cumt e3ffbc0
add another example for unit testing
lxd-cumt c26425e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt 219ca2a
fix bugs
lxd-cumt 1925e3f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
lxd-cumt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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. | ||
|
||
/* | ||
Custom VJP stands for manually implemented backward rules for composite | ||
operators. CustomVjpTrait will be added for those composite operators that | ||
defines custom vjp rules. Finally, by calling has_custom_vjp(op), users can | ||
check whether an operator has a CustomVjpTrait, and thus check whether a custom | ||
vjp rule is defined for that operator. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "paddle/pir/core/op_base.h" | ||
|
||
namespace paddle { | ||
namespace dialect { | ||
class CustomVjpTrait : public pir::OpTraitBase<CustomVjpTrait> { | ||
public: | ||
explicit CustomVjpTrait(pir::Operation *op) | ||
: pir::OpTraitBase<CustomVjpTrait>(op) {} | ||
}; | ||
|
||
} // namespace dialect | ||
} // namespace paddle | ||
|
||
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::CustomVjpTrait) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 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 unittest | ||
|
||
import paddle | ||
from paddle import ir, nn | ||
from paddle.base.core import has_custom_vjp | ||
|
||
paddle.enable_static() | ||
|
||
|
||
def get_gelu_program_new_ir(): | ||
main_program, start_program = ( | ||
paddle.static.Program(), | ||
paddle.static.Program(), | ||
) | ||
with paddle.static.program_guard(main_program, start_program): | ||
x = paddle.static.data('x', [2, 3, 3], dtype='float32') | ||
net = nn.GELU() | ||
out = net(x) | ||
newir_program = ir.translate_to_new_ir(main_program.desc) | ||
return newir_program | ||
|
||
|
||
def get_multiply_program_new_ir(): | ||
main_program, start_program = ( | ||
paddle.static.Program(), | ||
paddle.static.Program(), | ||
) | ||
with paddle.static.program_guard(main_program, start_program): | ||
x = paddle.static.data('x', [2, 3, 3], dtype='float32') | ||
y = paddle.static.data('y', [2, 3, 3], dtype='float32') | ||
out = paddle.multiply(x, y) | ||
newir_program = ir.translate_to_new_ir(main_program.desc) | ||
return newir_program | ||
|
||
|
||
class TestCustomVjpTrait(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 单测需要加一个反例 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okk,下个PR加入 |
||
def test_gelu_op_custom_vjp_trait(self): | ||
newir_program = get_gelu_program_new_ir() | ||
op = newir_program.global_block().ops[-1] | ||
self.assertEqual(op.name(), "pd_op.gelu") | ||
self.assertEqual(has_custom_vjp(op), True) | ||
|
||
def test_multiply_op_custom_vjp_trait(self): | ||
newir_program = get_multiply_program_new_ir() | ||
op = newir_program.global_block().ops[-1] | ||
self.assertEqual(op.name(), "pd_op.multiply") | ||
self.assertEqual(has_custom_vjp(op), False) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
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.
不可以使用相对路径导入的方式么?
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.
当前缺少__init__.py,使用相对路径的话需要新增才可以;
python代码规范里面也提到:
所以使用了绝对路径。