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.
Merge pull request PaddlePaddle#32 from cxxly/prim
add first-order autodiff composite logic for static graph
- Loading branch information
Showing
12 changed files
with
337 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
file( | ||
GLOB TEST_OPS | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
"test_*.py") | ||
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") | ||
set(GC_ENVS FLAGS_eager_delete_tensor_gb=0.0) | ||
|
||
foreach(TEST_OP ${TEST_OPS}) | ||
py_test_modules(${TEST_OP} MODULES ${TEST_OP} ENVS ${GC_ENVS}) | ||
endforeach() | ||
|
||
add_subdirectory(api) |
13 changes: 13 additions & 0 deletions
13
python/paddle/fluid/tests/unittests/prim/api/CMakeLists.txt
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,13 @@ | ||
file( | ||
GLOB TEST_OPS | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
"test_*.py") | ||
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") | ||
set(GC_ENVS FLAGS_eager_delete_tensor_gb=0.0) | ||
|
||
foreach(TEST_OP ${TEST_OPS}) | ||
py_test_modules(${TEST_OP} MODULES ${TEST_OP} ENVS ${GC_ENVS}) | ||
endforeach() | ||
|
||
add_subdirectory(comp) | ||
add_subdirectory(prim) |
12 changes: 12 additions & 0 deletions
12
python/paddle/fluid/tests/unittests/prim/api/comp/CMakeLists.txt
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,12 @@ | ||
file( | ||
GLOB TEST_OPS | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
"test_*.py") | ||
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") | ||
set(GC_ENVS FLAGS_eager_delete_tensor_gb=0.0) | ||
|
||
foreach(TEST_OP ${TEST_OPS}) | ||
py_test_modules(${TEST_OP} MODULES ${TEST_OP} ENVS ${GC_ENVS}) | ||
endforeach() | ||
|
||
set_tests_properties(test_tanh_grad_comp PROPERTIES TIMEOUT 60) |
78 changes: 78 additions & 0 deletions
78
python/paddle/fluid/tests/unittests/prim/api/comp/test_tanh_grad_comp.py
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,78 @@ | ||
# Copyright (c) 2022 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 os | ||
import unittest | ||
|
||
os.environ['FLAGS_prim_enabled'] = 'True' | ||
|
||
import autograd | ||
import autograd.numpy | ||
import numpy as np | ||
import parameterized as param | ||
|
||
import paddle | ||
|
||
|
||
@param.parameterized_class( | ||
('primal', 'cotangent', 'dtype'), | ||
[ | ||
(np.random.rand(10, 10), np.random.rand(10, 10), np.float32), | ||
], | ||
) | ||
class TestTanhGradComp(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.primal = cls.primal.astype(cls.dtype) | ||
cls.cotangent = cls.cotangent.astype(cls.dtype) | ||
|
||
def setUp(self): | ||
paddle.enable_static() | ||
|
||
def tearDown(self): | ||
paddle.disable_static() | ||
|
||
def test_tanh_grad_comp(self): | ||
def actual(primal, cotangent): | ||
mp, sp = paddle.static.Program(), paddle.static.Program() | ||
with paddle.static.program_guard(mp, sp): | ||
x = paddle.static.data('primal', primal.shape, primal.dtype) | ||
x.stop_gradient = False | ||
v = paddle.static.data( | ||
'cotangent', cotangent.shape, cotangent.dtype | ||
) | ||
y = paddle.tanh(x) | ||
x_cotangent = paddle.static.gradients(y, x, v) | ||
exe = paddle.static.Executor() | ||
exe.run(sp) | ||
return exe.run( | ||
program=mp, | ||
feed={'primal': primal, 'cotangent': cotangent}, | ||
fetch_list='composite_tmp_2', | ||
)[0] | ||
|
||
def desired(primal, cotangent): | ||
return autograd.make_vjp(autograd.numpy.tanh)(primal)[0](cotangent) | ||
|
||
np.testing.assert_allclose( | ||
actual=actual(self.primal, self.cotangent), | ||
desired=desired(self.primal, self.cotangent), | ||
rtol=1e-6, | ||
atol=0, | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
10 changes: 10 additions & 0 deletions
10
python/paddle/fluid/tests/unittests/prim/api/prim/CMakeLists.txt
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,10 @@ | ||
file( | ||
GLOB TEST_OPS | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
"test_*.py") | ||
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") | ||
set(GC_ENVS FLAGS_eager_delete_tensor_gb=0.0) | ||
|
||
foreach(TEST_OP ${TEST_OPS}) | ||
py_test_modules(${TEST_OP} MODULES ${TEST_OP} ENVS ${GC_ENVS}) | ||
endforeach() |
74 changes: 74 additions & 0 deletions
74
python/paddle/fluid/tests/unittests/prim/test_get_grad_op_desc_prim_disabled.py
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,74 @@ | ||
# Copyright (c) 2022 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 os | ||
import unittest | ||
|
||
os.environ['FLAGS_prim_enabled'] = 'False' | ||
|
||
import parameterized as param | ||
|
||
import paddle | ||
from paddle.fluid import core, framework | ||
|
||
|
||
@param.parameterized_class( | ||
( | ||
'fwd_type', | ||
'inputs', | ||
'outputs', | ||
'no_grad_var', | ||
'grad_sub_block', | ||
'desired_ops', | ||
), | ||
( | ||
('tanh', {'X': ['x']}, {'Out': ['y']}, set(), tuple(), ('tanh_grad',)), | ||
('empty', {}, {'Out': ['y']}, set(), tuple(), tuple()), | ||
), | ||
) | ||
class TestGetGradOpDescPrimEnabled(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
paddle.enable_static() | ||
block = framework.Block(framework.Program(), 0) | ||
block.append_op( | ||
type=cls.fwd_type, | ||
inputs={ | ||
n: [block.create_var(name=v, stop_gradient=False) for v in vs] | ||
for n, vs in cls.inputs.items() | ||
}, | ||
outputs={ | ||
n: [block.create_var(name=v, stop_gradient=False) for v in vs] | ||
for n, vs in cls.outputs.items() | ||
}, | ||
) | ||
cls.fwd = block.ops[0].desc | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
paddle.disable_static() | ||
|
||
def test_get_grad_op_desc(self): | ||
actual = tuple( | ||
desc.type() | ||
for desc in core.get_grad_op_desc( | ||
self.fwd, self.no_grad_var, self.grad_sub_block | ||
)[0] | ||
) | ||
self.assertEquals(actual, self.desired_ops) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.