From 39d85bfb4ba630c59b3e7adfe8689a13478458da Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Thu, 9 Jul 2020 11:27:56 +0800 Subject: [PATCH] Enable load program state in imperative mode (#24998) (#25441) * enable load_program_state run in imperative mode; test=develop * remove useless code; test=develop --- python/paddle/fluid/io.py | 3 ++- .../paddle/fluid/tests/unittests/test_static_save_load.py | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/io.py b/python/paddle/fluid/io.py index b9b172dbb8b..fbc84be2a06 100644 --- a/python/paddle/fluid/io.py +++ b/python/paddle/fluid/io.py @@ -191,7 +191,8 @@ def _load_program_scope(main=None, startup=None, scope=None): with paddle.fluid.scope_guard(scope): with paddle.fluid.program_guard(prog, startup_prog): with paddle.fluid.unique_name.guard(): - yield + with paddle.fluid.framework._dygraph_guard(None): + yield def _get_valid_program(main_program): diff --git a/python/paddle/fluid/tests/unittests/test_static_save_load.py b/python/paddle/fluid/tests/unittests/test_static_save_load.py index ac61ab756f0..72c99223433 100644 --- a/python/paddle/fluid/tests/unittests/test_static_save_load.py +++ b/python/paddle/fluid/tests/unittests/test_static_save_load.py @@ -1153,7 +1153,6 @@ class TestProgramStateOldSave(unittest.TestCase): # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) - #fluid.load(test_program, "./test_1", None ) program_state = fluid.load_program_state("test_program_1") fluid.set_program_state(main_program, program_state) @@ -1164,6 +1163,11 @@ class TestProgramStateOldSave(unittest.TestCase): base_t = base_map[var.name] self.assertTrue(np.array_equal(new_t, base_t)) + with fluid.dygraph.guard(place): + load_state = fluid.load_program_state("test_program_1") + for k, v in load_state.items(): + self.assertTrue(np.array_equal(base_map[k], v)) + class TestProgramStateOldSaveSingleModel(unittest.TestCase): def test_ptb_rnn_cpu_float32(self): -- GitLab