From d066d6f9940851b903d46f1a850ee39d419365c1 Mon Sep 17 00:00:00 2001 From: Huihuang Zheng Date: Fri, 20 Mar 2020 11:26:25 +0800 Subject: [PATCH] [Dy2Stat] Change layers.data to fluid.data and Test Var Created In Loop (#23103) As the title --- .../dygraph_to_static/cache_program.py | 6 ++--- .../dygraph_to_static/variable_trans_func.py | 2 +- .../unittests/dygraph_to_static/test_loop.py | 23 ++++++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/cache_program.py b/python/paddle/fluid/dygraph/dygraph_to_static/cache_program.py index 1024adf3c1..83e83e8595 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/cache_program.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/cache_program.py @@ -20,8 +20,8 @@ import numpy import six from paddle.fluid import framework -from paddle.fluid.layers import io from paddle.fluid import core, executor +from paddle.fluid.data import data from paddle.fluid.dygraph.dygraph_to_static import convert_to_static __all__ = ['AutoTracer'] @@ -170,9 +170,9 @@ class ProgramCache(object): batch_data, numpy.ndarray ), "Input {} should be numpy.ndarray, but received {}.".format( feed_name, type(batch_data)) - feed_layer = io.data( + feed_layer = data( name=feed_name, - shape=list(batch_data.shape[1:]), + shape=[-1] + list(batch_data.shape[1:]), dtype=str(batch_data.dtype)) self._inputs.append(feed_layer) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py b/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py index ccf8991545..88f744cfe6 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py @@ -29,7 +29,7 @@ def to_static_variable_gast_node(name): def create_static_variable_gast_node(name): - func_code = "{} = fluid.layers.data(name='{}', shape=[-1], dtype='float32')".format( + func_code = "{} = fluid.data(name='{}', shape=[-1], dtype='float32')".format( name, name) return gast.parse(func_code).body[0] diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_loop.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_loop.py index 920161db53..906474db09 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_loop.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_loop.py @@ -50,6 +50,12 @@ def while_loop_bool_op(x): return i +def var_create_in_for_loop(max_len): + for i in range(max_len): + ret = fluid.layers.zeros(shape=[3, 4, 5], dtype='float64') + return ret + + class TestNameVisitor(unittest.TestCase): def setUp(self): self.loop_funcs = [while_loop_dyfunc, for_loop_dyfunc] @@ -119,11 +125,15 @@ class TestTransformForLoop(unittest.TestCase): self.place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda( ) else fluid.CPUPlace() self.len = 100 + self._init_dyfunc() + + def _init_dyfunc(self): + self.dyfunc = for_loop_dyfunc def _run_static(self): main_program = fluid.Program() with fluid.program_guard(main_program): - static_func = dygraph_to_static_graph(for_loop_dyfunc) + static_func = dygraph_to_static_graph(self.dyfunc) out = static_func(self.len) exe = fluid.Executor(self.place) ret = exe.run(main_program, fetch_list=out) @@ -131,18 +141,19 @@ class TestTransformForLoop(unittest.TestCase): def _run_dygraph(self): with fluid.dygraph.guard(self.place): - ret = for_loop_dyfunc(self.len) + ret = self.dyfunc(self.len) return ret.numpy() def test_ast_to_func(self): static_numpy = self._run_static() - self.assertTrue( - np.allclose( - np.full( - shape=(1), fill_value=2, dtype=np.int32), static_numpy)) self._run_dygraph() self.assertTrue(np.allclose(self._run_dygraph(), self._run_static())) +class TestVarCreateInForLoop(TestTransformForLoop): + def _init_dyfunc(self): + self.dyfunc = var_create_in_for_loop + + if __name__ == '__main__': unittest.main() -- GitLab