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.
[PIR]Support PassStopGradients mechanism and Set stop_gradient as True (
PaddlePaddle#57255) * [PIR]Set stop_gradient as True and is_persistable as False * add PassStopGradients for build op * fix unittest * fix lint * fix uint32_t * fix ut
- Loading branch information
1 parent
3a75f1b
commit 4a3e44d
Showing
8 changed files
with
210 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 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 | ||
|
||
|
||
class TestAPI(unittest.TestCase): | ||
def setUp(self): | ||
paddle.enable_static() | ||
|
||
def assert_api(self, api_func, stop_gradient): | ||
main_program = paddle.static.Program() | ||
with paddle.static.program_guard(main_program): | ||
x = api_func() | ||
self.assertEqual(x.stop_gradient, stop_gradient) | ||
# test for setter | ||
x.stop_gradient = not stop_gradient | ||
self.assertEqual(x.stop_gradient, not stop_gradient) | ||
|
||
def test_full(self): | ||
api = lambda: paddle.full(shape=[2, 3], fill_value=1.0) | ||
self.assert_api(api, True) | ||
|
||
def test_data(self): | ||
api = lambda: paddle.static.data('x', [4, 4], dtype='float32') | ||
self.assert_api(api, True) | ||
|
||
# TODO(Aurelius84): Add more test cases after API is migrated. | ||
|
||
|
||
class TestParametes(unittest.TestCase): | ||
def setUp(self): | ||
paddle.enable_static() | ||
|
||
def test_create_param(self): | ||
main_program = paddle.static.Program() | ||
with paddle.static.program_guard(main_program): | ||
w = paddle.create_parameter(shape=[784, 200], dtype='float32') | ||
self.assertEqual(w.stop_gradient, False) | ||
self.assertEqual(w.is_persistable, True) | ||
|
||
# test for setter | ||
w.stop_gradient = True | ||
w.is_persistable = False | ||
self.assertEqual(w.stop_gradient, True) | ||
self.assertEqual(w.is_persistable, False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |