forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Prim] Add exp_double_grad, log_double_grad, abs_triple_grad, min_grad (
PaddlePaddle#63245) * add min_grad composite * support exp_double_grad, abs_triple_grad, log_double_grad composite OPs * add infermeta items * set_prim_all for TestLogDoubleGradCheck * change 2022 to 2024 in copyright header * remove 'log_grad' from vjp_interface_black_list
- Loading branch information
1 parent
066ae39
commit 23e6586
Showing
12 changed files
with
459 additions
and
3 deletions.
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
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
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
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,71 @@ | ||
# Copyright (c) 2024 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 numpy as np | ||
import parameterized as param | ||
|
||
import paddle | ||
from paddle.base import core | ||
|
||
core.set_prim_eager_enabled(True) | ||
|
||
|
||
@param.parameterized_class( | ||
('primal', 'axis', 'cotangent', 'dtype'), | ||
[ | ||
(np.random.rand(16, 32), [1], np.random.rand(16, 32), np.float32), | ||
(np.random.rand(16, 32), [0], np.random.rand(16, 32), np.float32), | ||
], | ||
) | ||
class TestMinGradComp(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.primal = cls.primal.astype(cls.dtype) | ||
|
||
def test_min_grad_comp(self): | ||
def actual(primal0, axis): | ||
core.set_prim_eager_enabled(True) | ||
paddle.disable_static() | ||
x = paddle.to_tensor(primal0, dtype='float32', stop_gradient=False) | ||
x.stop_gradient = False | ||
out = paddle.min(x, axis) | ||
res = paddle.grad(out, [x], create_graph=False) | ||
return res[0].numpy() | ||
|
||
def desired(primal0, axis): | ||
core.set_prim_eager_enabled(False) | ||
paddle.disable_static() | ||
x = paddle.to_tensor(primal0, dtype='float32', stop_gradient=False) | ||
x.stop_gradient = False | ||
out = paddle.min(x, axis) | ||
res = paddle.grad(out, [x], create_graph=False) | ||
return res[0].numpy() | ||
|
||
dx = actual(self.primal, self.axis) | ||
|
||
ddx = desired(self.primal, self.axis) | ||
|
||
np.testing.assert_allclose( | ||
actual=dx, | ||
desired=ddx, | ||
rtol=1e-6, | ||
atol=0, | ||
) | ||
core.set_prim_eager_enabled(False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.