From 1f8d8a5358e6e6c6df0ede5bc90ecaaaf9c7c8c3 Mon Sep 17 00:00:00 2001 From: zhongpu <2013000149@qq.com> Date: Tue, 14 Apr 2020 20:53:18 +0800 Subject: [PATCH] remove dygraph api to paddle.imperative, test=develop (#23628) --- python/paddle/__init__.py | 1 + .../tests/unittests/test_imperative_basic.py | 19 +++++++++++++ .../test_imperative_container_layerlist.py | 19 +++++++++++-- ...test_imperative_container_parameterlist.py | 24 +++++++++++++--- .../test_imperative_data_parallel.py | 2 +- .../tests/unittests/test_imperative_mnist.py | 3 +- .../test_imperative_named_members.py | 3 +- .../unittests/test_imperative_save_load.py | 10 ++++--- .../test_imperative_selected_rows.py | 9 +++--- python/paddle/imperative/__init__.py | 28 +++++++++++-------- python/setup.py.in | 3 +- 11 files changed, 89 insertions(+), 32 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 9eac1a93f8..0a82b6189a 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -36,6 +36,7 @@ batch = batch.batch import paddle.sysconfig import paddle.tensor import paddle.nn +import paddle.imperative # TODO: define alias in tensor and framework directory # from .tensor.creation import create_.tensor #DEFINE_ALIAS diff --git a/python/paddle/fluid/tests/unittests/test_imperative_basic.py b/python/paddle/fluid/tests/unittests/test_imperative_basic.py index 47831341c4..a8f621fa62 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_basic.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_basic.py @@ -21,6 +21,7 @@ from paddle.fluid import core from paddle.fluid import Linear from test_imperative_base import new_program_scope import paddle.fluid.dygraph_utils as dygraph_utils +import paddle class MyLayer(fluid.Layer): @@ -245,6 +246,24 @@ class TestImperative(unittest.TestCase): self.assertTrue(tmp._grad_ivar() is None) self.assertTrue(l0.weight._grad_ivar() is not None) + def test_paddle_imperative_no_grad_guard(self): + data = np.array([[2, 3], [4, 5]]).astype('float32') + with fluid.dygraph.guard(): + l0 = fluid.Linear(2, 2) + self.assertTrue(l0.weight._grad_ivar() is None) + l1 = fluid.Linear(2, 2) + with paddle.imperative.no_grad(): + self.assertTrue(l1.weight.stop_gradient is False) + tmp = l1.weight * 2 + self.assertTrue(tmp.stop_gradient) + x = fluid.dygraph.to_variable(data) + y = l0(x) + tmp + o = l1(y) + o.backward() + + self.assertTrue(tmp._grad_ivar() is None) + self.assertTrue(l0.weight._grad_ivar() is not None) + def test_sum_op(self): x = np.ones([2, 2], np.float32) with fluid.dygraph.guard(): diff --git a/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py b/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py index 610f50603d..e106960141 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py @@ -17,6 +17,7 @@ from __future__ import print_function import unittest import paddle.fluid as fluid import numpy as np +import paddle class MyLayer(fluid.Layer): @@ -31,12 +32,20 @@ class MyLayer(fluid.Layer): class TestImperativeContainer(unittest.TestCase): - def test_layer_list(self): + def fluid_dygraph_list(self): + return fluid.dygraph.LayerList( + [fluid.dygraph.Linear(2**i, 2**(i + 1)) for i in range(6)]) + + def paddle_imperative_list(self): + return paddle.imperative.LayerList( + [fluid.dygraph.Linear(2**i, 2**(i + 1)) for i in range(6)]) + + def layer_list(self, use_fluid_api): data_np = np.random.uniform(-1, 1, [5, 1]).astype('float32') with fluid.dygraph.guard(): x = fluid.dygraph.to_variable(data_np) - layerlist = fluid.dygraph.LayerList( - [fluid.dygraph.Linear(2**i, 2**(i + 1)) for i in range(6)]) + layerlist = self.fluid_dygraph_list( + ) if use_fluid_api else self.paddle_imperative_list() size = len(layerlist) model = MyLayer(layerlist) @@ -75,6 +84,10 @@ class TestImperativeContainer(unittest.TestCase): self.assertListEqual(res8.shape, [5, 3**3]) res8.backward() + def test_layer_list(self): + self.layer_list(True) + self.layer_list(False) + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_container_parameterlist.py b/python/paddle/fluid/tests/unittests/test_imperative_container_parameterlist.py index 14dcc288b7..7503051d65 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_container_parameterlist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_container_parameterlist.py @@ -17,13 +17,25 @@ from __future__ import print_function import unittest import paddle.fluid as fluid import numpy as np +import paddle class MyLayer(fluid.Layer): - def __init__(self, num_stacked_param): + def __init__(self, num_stacked_param, use_fluid_api): super(MyLayer, self).__init__() # create ParameterList with iterable Parameters - self.params = fluid.dygraph.ParameterList( + self.params = self.fluid_dygraph_ParameterList( + num_stacked_param + ) if use_fluid_api else self.paddle_imperative_ParameterList( + num_stacked_param) + + def fluid_dygraph_ParameterList(self, num_stacked_param): + return fluid.dygraph.ParameterList( + [fluid.layers.create_parameter( + shape=[2, 2], dtype='float32')] * num_stacked_param) + + def paddle_imperative_ParameterList(self, num_stacked_param): + return paddle.imperative.ParameterList( [fluid.layers.create_parameter( shape=[2, 2], dtype='float32')] * num_stacked_param) @@ -42,12 +54,12 @@ class MyLayer(fluid.Layer): class TestImperativeContainerParameterList(unittest.TestCase): - def test_paramter_list(self): + def paramter_list(self, use_fluid_api): data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32') with fluid.dygraph.guard(): x = fluid.dygraph.to_variable(data_np) num_stacked_param = 4 - model = MyLayer(num_stacked_param) + model = MyLayer(num_stacked_param, use_fluid_api) self.assertEqual(len(model.params), num_stacked_param) res = model(x) self.assertListEqual(res.shape, [5, 2]) @@ -67,6 +79,10 @@ class TestImperativeContainerParameterList(unittest.TestCase): loss = fluid.layers.reduce_mean(res) loss.backward() + def test_paramter_list(self): + self.paramter_list(True) + self.paramter_list(False) + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py b/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py index d645a0a5ce..a61950f2dc 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py @@ -43,7 +43,7 @@ class MLP(fluid.Layer): class TestDataParallelStateDict(unittest.TestCase): def test_data_parallel_state_dict(self): with fluid.dygraph.guard(): - strategy = dygraph.parallel.prepare_context() + strategy = paddle.imperative.prepare_context() mlp = MLP() parallel_mlp = dygraph.parallel.DataParallel(mlp, strategy) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_mnist.py b/python/paddle/fluid/tests/unittests/test_imperative_mnist.py index 3bc28dac4a..a3c602646b 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_mnist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_mnist.py @@ -27,7 +27,6 @@ from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear from paddle.fluid.dygraph.base import to_variable from test_imperative_base import new_program_scope from utils import DyGraphProgramDescTracerTestHelper, is_equal_program -from paddle.fluid.dygraph import TracedLayer class SimpleImgConvPool(fluid.dygraph.Layer): @@ -154,7 +153,7 @@ class TestImperativeMnist(unittest.TestCase): label.stop_gradient = True if batch_id % 10 == 0: - cost, traced_layer = TracedLayer.trace( + cost, traced_layer = paddle.imperative.TracedLayer.trace( mnist, inputs=img) if program is not None: self.assertTrue(program, traced_layer.program) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_named_members.py b/python/paddle/fluid/tests/unittests/test_imperative_named_members.py index b610342ff1..1546dd3194 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_named_members.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_named_members.py @@ -15,6 +15,7 @@ import unittest import numpy as np import paddle.fluid as fluid +import paddle class MyLayer(fluid.Layer): @@ -66,7 +67,7 @@ class TestImperativeNamedParameters(unittest.TestCase): fc1 = fluid.Linear(10, 3) fc2 = fluid.Linear(3, 10, bias_attr=False) custom = MyLayer(3, 10) - model = fluid.dygraph.Sequential(fc1, fc2, custom) + model = paddle.imperative.Sequential(fc1, fc2, custom) named_parameters = list(model.named_parameters()) expected_named_parameters = list() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_save_load.py b/python/paddle/fluid/tests/unittests/test_imperative_save_load.py index 694ad077c0..734856ec0d 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_save_load.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_save_load.py @@ -26,6 +26,7 @@ from paddle.fluid.dygraph.learning_rate_scheduler import LearningRateDecay from test_imperative_base import new_program_scope import numpy as np import six +import paddle class SimpleLSTMRNN(fluid.Layer): @@ -880,17 +881,18 @@ class TestDygraphPtbRnn(unittest.TestCase): with fluid.dygraph.guard(): emb = fluid.dygraph.Embedding([10, 10]) state_dict = emb.state_dict() - fluid.save_dygraph(state_dict, os.path.join('saved_dy', 'emb_dy')) + paddle.imperative.save_dygraph(state_dict, + os.path.join('saved_dy', 'emb_dy')) - para_state_dict, opti_state_dict = fluid.load_dygraph( + para_state_dict, opti_state_dict = paddle.imperative.load_dygraph( os.path.join('saved_dy', 'emb_dy')) self.assertTrue(opti_state_dict == None) - para_state_dict, opti_state_dict = fluid.load_dygraph( + para_state_dict, opti_state_dict = paddle.imperative.load_dygraph( os.path.join('saved_dy', 'emb_dy.pdparams')) - para_state_dict, opti_state_dict = fluid.load_dygraph( + para_state_dict, opti_state_dict = paddle.imperative.load_dygraph( os.path.join('saved_dy', 'emb_dy.pdopt')) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_selected_rows.py b/python/paddle/fluid/tests/unittests/test_imperative_selected_rows.py index 727651f9cf..3c4e959c5c 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_selected_rows.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_selected_rows.py @@ -21,9 +21,10 @@ from paddle.fluid.dygraph.nn import Embedding from paddle.fluid.optimizer import SGDOptimizer import numpy as np import paddle.fluid.core as core +import paddle -class SimpleNet(fluid.Layer): +class SimpleNet(paddle.imperative.Layer): def __init__(self, vocab_size, hidden_size, dtype): super(SimpleNet, self).__init__() self.emb = fluid.dygraph.Embedding( @@ -46,13 +47,13 @@ class TestSimpleNet(unittest.TestCase): for place in places: for dtype in ["float32", "float64"]: for sort_sum_gradient in [True, False]: - with fluid.dygraph.guard(place): - backward_strategy = fluid.dygraph.BackwardStrategy() + with paddle.imperative.guard(place): + backward_strategy = paddle.imperative.BackwardStrategy() backward_strategy.sort_sum_gradient = sort_sum_gradient # grad_clip = fluid.clip.GradientClipByGlobalNorm(5.0) input_word = np.array([[1, 2], [2, 1]]).astype('int64') - input = to_variable(input_word) + input = paddle.imperative.to_variable(input_word) simplenet = SimpleNet(20, 32, dtype) adam = SGDOptimizer( diff --git a/python/paddle/imperative/__init__.py b/python/paddle/imperative/__init__.py index dde7af1259..6feef89a2d 100644 --- a/python/paddle/imperative/__init__.py +++ b/python/paddle/imperative/__init__.py @@ -12,15 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -# TODO: define api used to run in imperative mode -# __all__ = ['BackwardStrategy', -# 'guard', -# 'Layer', -# 'LayerList', -# 'load_dygraph', -# 'save_dygraph', -# 'prepare_context', -# 'to_variable', -# 'TracedLayer', -# 'no_grad', -# 'ParameterList'] +# define api used to run in imperative mode +__all__ = [ + 'BackwardStrategy', 'guard', 'Layer', 'LayerList', 'load_dygraph', + 'save_dygraph', 'prepare_context', 'to_variable', 'TracedLayer', 'no_grad', + 'ParameterList', 'Sequential' +] + +from paddle.fluid import core +from ..fluid.dygraph.base import guard, no_grad, to_variable +from ..fluid.dygraph.layers import Layer +from ..fluid.dygraph.container import LayerList, ParameterList, Sequential +from ..fluid.dygraph.checkpoint import load_dygraph, save_dygraph +from ..fluid.dygraph.parallel import prepare_context +from ..fluid.dygraph.jit import TracedLayer + +BackwardStrategy = core.BackwardStrategy diff --git a/python/setup.py.in b/python/setup.py.in index 2794539bfc..76776ab50a 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -177,7 +177,8 @@ packages=['paddle', 'paddle.fluid.incubate.fleet.utils', 'paddle.nn', 'paddle.nn.functional', - 'paddle.nn.layer'] + 'paddle.nn.layer', + 'paddle.imperative'] with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f: setup_requires = f.read().splitlines() -- GitLab