diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 9de4dd41ac2ff7ee569c1e19978b7eb8036da529..83a3b550df72c6337e2b3a38ff8df96487f06396 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 47831341c489e1741923a6fe0938618fc5a508c3..a8f621fa6274c37e829bbf5555ab9ac9d67c33c9 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 610f50603da40e3a2b0dfd87b7d7d9defe0735cb..e10696014104510ce9b79671845dec2bb26715fe 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 14dcc288b710282efce2a475fdca0ec459e5f562..7503051d658588f101fc84cccc2f902fdadb0e14 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 d645a0a5ceb60f89e50eac3a83ff00f7b1e3927a..a61950f2dc0775fcbad5fd970ee95ed5ebf1c558 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 3bc28dac4a4d570e988e35cbd46370252eb4f8bf..a3c602646b700556cea53a9b06295e38baf705dd 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 b610342ff173af74a16fc5326b8f1f2322024672..1546dd31945d32aecfd61fd140a27f4f90262d87 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 694ad077c024f8f7144c606d1f3819f377fd034b..734856ec0d66c6b17f801eca73cbbea3f421cb6b 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 727651f9cf550bce4821cab511d65b59bbd5f827..3c4e959c5cc1ff1fb032e4cac1a88307b78028e2 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 dde7af125948cc84eea9b27b2d7c55af329ee537..6feef89a2d07a59d5577d481282c826ab7a47488 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 2921b4ff4c3acff2bee6cca1a3cebcd02d1d3e9c..2e6cc0c8c84a6db66dd4dc2d3c7d841e86dcfe75 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -148,7 +148,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()