未验证 提交 1f8d8a53 编写于 作者: Z zhongpu 提交者: GitHub

remove dygraph api to paddle.imperative, test=develop (#23628)

上级 fc1b140a
...@@ -36,6 +36,7 @@ batch = batch.batch ...@@ -36,6 +36,7 @@ batch = batch.batch
import paddle.sysconfig import paddle.sysconfig
import paddle.tensor import paddle.tensor
import paddle.nn import paddle.nn
import paddle.imperative
# TODO: define alias in tensor and framework directory # TODO: define alias in tensor and framework directory
# from .tensor.creation import create_.tensor #DEFINE_ALIAS # from .tensor.creation import create_.tensor #DEFINE_ALIAS
......
...@@ -21,6 +21,7 @@ from paddle.fluid import core ...@@ -21,6 +21,7 @@ from paddle.fluid import core
from paddle.fluid import Linear from paddle.fluid import Linear
from test_imperative_base import new_program_scope from test_imperative_base import new_program_scope
import paddle.fluid.dygraph_utils as dygraph_utils import paddle.fluid.dygraph_utils as dygraph_utils
import paddle
class MyLayer(fluid.Layer): class MyLayer(fluid.Layer):
...@@ -245,6 +246,24 @@ class TestImperative(unittest.TestCase): ...@@ -245,6 +246,24 @@ class TestImperative(unittest.TestCase):
self.assertTrue(tmp._grad_ivar() is None) self.assertTrue(tmp._grad_ivar() is None)
self.assertTrue(l0.weight._grad_ivar() is not 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): def test_sum_op(self):
x = np.ones([2, 2], np.float32) x = np.ones([2, 2], np.float32)
with fluid.dygraph.guard(): with fluid.dygraph.guard():
......
...@@ -17,6 +17,7 @@ from __future__ import print_function ...@@ -17,6 +17,7 @@ from __future__ import print_function
import unittest import unittest
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np import numpy as np
import paddle
class MyLayer(fluid.Layer): class MyLayer(fluid.Layer):
...@@ -31,12 +32,20 @@ class MyLayer(fluid.Layer): ...@@ -31,12 +32,20 @@ class MyLayer(fluid.Layer):
class TestImperativeContainer(unittest.TestCase): 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') data_np = np.random.uniform(-1, 1, [5, 1]).astype('float32')
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(data_np) x = fluid.dygraph.to_variable(data_np)
layerlist = fluid.dygraph.LayerList( layerlist = self.fluid_dygraph_list(
[fluid.dygraph.Linear(2**i, 2**(i + 1)) for i in range(6)]) ) if use_fluid_api else self.paddle_imperative_list()
size = len(layerlist) size = len(layerlist)
model = MyLayer(layerlist) model = MyLayer(layerlist)
...@@ -75,6 +84,10 @@ class TestImperativeContainer(unittest.TestCase): ...@@ -75,6 +84,10 @@ class TestImperativeContainer(unittest.TestCase):
self.assertListEqual(res8.shape, [5, 3**3]) self.assertListEqual(res8.shape, [5, 3**3])
res8.backward() res8.backward()
def test_layer_list(self):
self.layer_list(True)
self.layer_list(False)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -17,13 +17,25 @@ from __future__ import print_function ...@@ -17,13 +17,25 @@ from __future__ import print_function
import unittest import unittest
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np import numpy as np
import paddle
class MyLayer(fluid.Layer): class MyLayer(fluid.Layer):
def __init__(self, num_stacked_param): def __init__(self, num_stacked_param, use_fluid_api):
super(MyLayer, self).__init__() super(MyLayer, self).__init__()
# create ParameterList with iterable Parameters # 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( [fluid.layers.create_parameter(
shape=[2, 2], dtype='float32')] * num_stacked_param) shape=[2, 2], dtype='float32')] * num_stacked_param)
...@@ -42,12 +54,12 @@ class MyLayer(fluid.Layer): ...@@ -42,12 +54,12 @@ class MyLayer(fluid.Layer):
class TestImperativeContainerParameterList(unittest.TestCase): 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') data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32')
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(data_np) x = fluid.dygraph.to_variable(data_np)
num_stacked_param = 4 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) self.assertEqual(len(model.params), num_stacked_param)
res = model(x) res = model(x)
self.assertListEqual(res.shape, [5, 2]) self.assertListEqual(res.shape, [5, 2])
...@@ -67,6 +79,10 @@ class TestImperativeContainerParameterList(unittest.TestCase): ...@@ -67,6 +79,10 @@ class TestImperativeContainerParameterList(unittest.TestCase):
loss = fluid.layers.reduce_mean(res) loss = fluid.layers.reduce_mean(res)
loss.backward() loss.backward()
def test_paramter_list(self):
self.paramter_list(True)
self.paramter_list(False)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -43,7 +43,7 @@ class MLP(fluid.Layer): ...@@ -43,7 +43,7 @@ class MLP(fluid.Layer):
class TestDataParallelStateDict(unittest.TestCase): class TestDataParallelStateDict(unittest.TestCase):
def test_data_parallel_state_dict(self): def test_data_parallel_state_dict(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
strategy = dygraph.parallel.prepare_context() strategy = paddle.imperative.prepare_context()
mlp = MLP() mlp = MLP()
parallel_mlp = dygraph.parallel.DataParallel(mlp, strategy) parallel_mlp = dygraph.parallel.DataParallel(mlp, strategy)
......
...@@ -27,7 +27,6 @@ from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear ...@@ -27,7 +27,6 @@ from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear
from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph.base import to_variable
from test_imperative_base import new_program_scope from test_imperative_base import new_program_scope
from utils import DyGraphProgramDescTracerTestHelper, is_equal_program from utils import DyGraphProgramDescTracerTestHelper, is_equal_program
from paddle.fluid.dygraph import TracedLayer
class SimpleImgConvPool(fluid.dygraph.Layer): class SimpleImgConvPool(fluid.dygraph.Layer):
...@@ -154,7 +153,7 @@ class TestImperativeMnist(unittest.TestCase): ...@@ -154,7 +153,7 @@ class TestImperativeMnist(unittest.TestCase):
label.stop_gradient = True label.stop_gradient = True
if batch_id % 10 == 0: if batch_id % 10 == 0:
cost, traced_layer = TracedLayer.trace( cost, traced_layer = paddle.imperative.TracedLayer.trace(
mnist, inputs=img) mnist, inputs=img)
if program is not None: if program is not None:
self.assertTrue(program, traced_layer.program) self.assertTrue(program, traced_layer.program)
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import unittest import unittest
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle
class MyLayer(fluid.Layer): class MyLayer(fluid.Layer):
...@@ -66,7 +67,7 @@ class TestImperativeNamedParameters(unittest.TestCase): ...@@ -66,7 +67,7 @@ class TestImperativeNamedParameters(unittest.TestCase):
fc1 = fluid.Linear(10, 3) fc1 = fluid.Linear(10, 3)
fc2 = fluid.Linear(3, 10, bias_attr=False) fc2 = fluid.Linear(3, 10, bias_attr=False)
custom = MyLayer(3, 10) custom = MyLayer(3, 10)
model = fluid.dygraph.Sequential(fc1, fc2, custom) model = paddle.imperative.Sequential(fc1, fc2, custom)
named_parameters = list(model.named_parameters()) named_parameters = list(model.named_parameters())
expected_named_parameters = list() expected_named_parameters = list()
......
...@@ -26,6 +26,7 @@ from paddle.fluid.dygraph.learning_rate_scheduler import LearningRateDecay ...@@ -26,6 +26,7 @@ from paddle.fluid.dygraph.learning_rate_scheduler import LearningRateDecay
from test_imperative_base import new_program_scope from test_imperative_base import new_program_scope
import numpy as np import numpy as np
import six import six
import paddle
class SimpleLSTMRNN(fluid.Layer): class SimpleLSTMRNN(fluid.Layer):
...@@ -880,17 +881,18 @@ class TestDygraphPtbRnn(unittest.TestCase): ...@@ -880,17 +881,18 @@ class TestDygraphPtbRnn(unittest.TestCase):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding([10, 10]) emb = fluid.dygraph.Embedding([10, 10])
state_dict = emb.state_dict() 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')) os.path.join('saved_dy', 'emb_dy'))
self.assertTrue(opti_state_dict == None) 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')) 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')) os.path.join('saved_dy', 'emb_dy.pdopt'))
......
...@@ -21,9 +21,10 @@ from paddle.fluid.dygraph.nn import Embedding ...@@ -21,9 +21,10 @@ from paddle.fluid.dygraph.nn import Embedding
from paddle.fluid.optimizer import SGDOptimizer from paddle.fluid.optimizer import SGDOptimizer
import numpy as np import numpy as np
import paddle.fluid.core as core 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): def __init__(self, vocab_size, hidden_size, dtype):
super(SimpleNet, self).__init__() super(SimpleNet, self).__init__()
self.emb = fluid.dygraph.Embedding( self.emb = fluid.dygraph.Embedding(
...@@ -46,13 +47,13 @@ class TestSimpleNet(unittest.TestCase): ...@@ -46,13 +47,13 @@ class TestSimpleNet(unittest.TestCase):
for place in places: for place in places:
for dtype in ["float32", "float64"]: for dtype in ["float32", "float64"]:
for sort_sum_gradient in [True, False]: for sort_sum_gradient in [True, False]:
with fluid.dygraph.guard(place): with paddle.imperative.guard(place):
backward_strategy = fluid.dygraph.BackwardStrategy() backward_strategy = paddle.imperative.BackwardStrategy()
backward_strategy.sort_sum_gradient = sort_sum_gradient backward_strategy.sort_sum_gradient = sort_sum_gradient
# grad_clip = fluid.clip.GradientClipByGlobalNorm(5.0) # grad_clip = fluid.clip.GradientClipByGlobalNorm(5.0)
input_word = np.array([[1, 2], [2, 1]]).astype('int64') 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) simplenet = SimpleNet(20, 32, dtype)
adam = SGDOptimizer( adam = SGDOptimizer(
......
...@@ -12,15 +12,19 @@ ...@@ -12,15 +12,19 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# TODO: define api used to run in imperative mode # define api used to run in imperative mode
# __all__ = ['BackwardStrategy', __all__ = [
# 'guard', 'BackwardStrategy', 'guard', 'Layer', 'LayerList', 'load_dygraph',
# 'Layer', 'save_dygraph', 'prepare_context', 'to_variable', 'TracedLayer', 'no_grad',
# 'LayerList', 'ParameterList', 'Sequential'
# 'load_dygraph', ]
# 'save_dygraph',
# 'prepare_context', from paddle.fluid import core
# 'to_variable', from ..fluid.dygraph.base import guard, no_grad, to_variable
# 'TracedLayer', from ..fluid.dygraph.layers import Layer
# 'no_grad', from ..fluid.dygraph.container import LayerList, ParameterList, Sequential
# 'ParameterList'] 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
...@@ -177,7 +177,8 @@ packages=['paddle', ...@@ -177,7 +177,8 @@ packages=['paddle',
'paddle.fluid.incubate.fleet.utils', 'paddle.fluid.incubate.fleet.utils',
'paddle.nn', 'paddle.nn',
'paddle.nn.functional', 'paddle.nn.functional',
'paddle.nn.layer'] 'paddle.nn.layer',
'paddle.imperative']
with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f: with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f:
setup_requires = f.read().splitlines() setup_requires = f.read().splitlines()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册