-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pre-save hooks of jit.save 1. Added pre_save_hooks features to jit.save. 2. Added related unittests * Added jit pre_save_hooks functions's alias to paddle.jit and copyright. * Make jit.save_pre_hook style be consisent with Paddle's rule. * Fixed arguments passing bug in run_save_pre_hooks * Added API Documents * Move clear and run_pre_save_hooks as internal methonds only. * Made register_save_pre_hook as an internal function.
- Loading branch information
1 parent
d368647
commit e91f7c0
Showing
3 changed files
with
162 additions
and
1 deletion.
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
59 changes: 59 additions & 0 deletions
59
python/paddle/fluid/tests/unittests/test_jit_pre_save_hooks.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,59 @@ | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# Copyright (c) 2021 NVIDIA Corporation. 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. | ||
|
||
from __future__ import print_function | ||
|
||
import unittest | ||
|
||
import paddle | ||
from paddle.fluid.dygraph.jit import _run_save_pre_hooks, _clear_save_pre_hooks, _register_save_pre_hook | ||
|
||
_counter = 0 | ||
|
||
|
||
class TestPreSaveHooks(unittest.TestCase): | ||
def test_pre_save_hook_functions(self): | ||
def fake_func(*args, **kwgs): | ||
global _counter | ||
_counter += 1 | ||
|
||
remove_handler = _register_save_pre_hook(fake_func) | ||
self.assertEqual(len(paddle.fluid.dygraph.jit._save_pre_hooks), 1) | ||
self.assertTrue( | ||
paddle.fluid.dygraph.jit._save_pre_hooks[0] is fake_func) | ||
|
||
# Test of avoiding redundancy hanging | ||
remove_handler = _register_save_pre_hook(fake_func) | ||
self.assertEqual(len(paddle.fluid.dygraph.jit._save_pre_hooks), 1) | ||
self.assertTrue( | ||
paddle.fluid.dygraph.jit._save_pre_hooks[0] is fake_func) | ||
|
||
remove_handler.remove() | ||
self.assertEqual(len(paddle.fluid.dygraph.jit._save_pre_hooks), 0) | ||
|
||
remove_handler = _register_save_pre_hook(fake_func) | ||
_clear_save_pre_hooks() | ||
self.assertEqual(len(paddle.fluid.dygraph.jit._save_pre_hooks), 0) | ||
|
||
global _counter | ||
_counter = 0 | ||
remove_handler = _register_save_pre_hook(fake_func) | ||
func_with_hook = _run_save_pre_hooks(fake_func) | ||
func_with_hook(None, None) | ||
self.assertEqual(_counter, 2) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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