From 2d6694437f5186d138b58d7b3c9268cc593523c3 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Sun, 27 Sep 2020 05:52:35 +0000 Subject: [PATCH 1/6] update 2.0 api. --- python/paddle/fluid/executor.py | 8 +++---- python/paddle/fluid/layers/nn.py | 36 +++++++++++++++------------- python/paddle/fluid/layers/tensor.py | 10 ++++---- python/paddle/fluid/param_attr.py | 17 ++++++------- python/paddle/static/__init__.py | 3 +++ python/paddle/static/nn/__init__.py | 2 ++ tools/wlist.json | 1 - 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index 3dc30767e5aa4..ae4985983e4e4 100644 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -94,12 +94,12 @@ def scope_guard(scope): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle import numpy - new_scope = fluid.Scope() - with fluid.scope_guard(new_scope): - fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace()) + new_scope = paddle.static.Scope() + with paddle.static.scope_guard(new_scope): + paddle.static.global_scope().var("data").get_tensor().set(paddle.ones((2, 2)), paddle.CPUPlace()) numpy.array(new_scope.find_var("data").get_tensor()) """ diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 3e7d10f8d1a02..bbbc952ec81de 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -13510,7 +13510,7 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): """ :api_attr: Static Graph - This OP is used to register customized Python OP to Paddle Fluid. The design + This OP is used to register customized Python OP to Paddle. The design principe of py_func is that LodTensor and numpy array can be converted to each other easily. So you can use Python and numpy API to register a python OP. @@ -13564,7 +13564,7 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): .. code-block:: python # example 1: - import paddle.fluid as fluid + import paddle import six # Creates a forward function, LodTensor can be input directly without @@ -13583,34 +13583,36 @@ def debug_func(x): print(x) def create_tmp_var(name, dtype, shape): - return fluid.default_main_program().current_block().create_var( + return paddle.static.default_main_program().current_block().create_var( name=name, dtype=dtype, shape=shape) def simple_net(img, label): hidden = img for idx in six.moves.range(4): - hidden = fluid.layers.fc(hidden, size=200) + hidden = paddle.static.nn.fc(hidden, size=200) new_hidden = create_tmp_var(name='hidden_{}'.format(idx), dtype=hidden.dtype, shape=hidden.shape) # User-defined forward and backward - hidden = fluid.layers.py_func(func=tanh, x=hidden, + hidden = paddle.static.nn.py_func(func=tanh, x=hidden, out=new_hidden, backward_func=tanh_grad, skip_vars_in_backward_input=hidden) # User-defined debug functions that print out the input LodTensor - fluid.layers.py_func(func=debug_func, x=hidden, out=None) + paddle.static.nn.py_func(func=debug_func, x=hidden, out=None) - prediction = fluid.layers.fc(hidden, size=10, act='softmax') - loss = fluid.layers.cross_entropy(input=prediction, label=label) - return fluid.layers.mean(loss) + prediction = paddle.static.nn.fc(hidden, size=10, act='softmax') + loss = paddle.static.nn.cross_entropy(input=prediction, label=label) + return paddle.mean(loss) # example 2: # This example shows how to turn LoDTensor into numpy array and # use numpy API to register an Python OP - import paddle.fluid as fluid + import paddle import numpy as np + paddle.enable_static() + def element_wise_add(x, y): # LodTensor must be actively converted to numpy array, otherwise, # numpy.shape can't be used. @@ -13628,24 +13630,24 @@ def element_wise_add(x, y): return result def create_tmp_var(name, dtype, shape): - return fluid.default_main_program().current_block().create_var( + return paddle.static.default_main_program().current_block().create_var( name=name, dtype=dtype, shape=shape) def py_func_demo(): - start_program = fluid.default_startup_program() - main_program = fluid.default_main_program() + start_program = paddle.static.default_startup_program() + main_program = paddle.static.default_main_program() # Input of the forward function - x = fluid.data(name='x', shape=[2,3], dtype='int32') - y = fluid.data(name='y', shape=[2,3], dtype='int32') + x = paddle.data(name='x', shape=[2,3], dtype='int32') + y = paddle.data(name='y', shape=[2,3], dtype='int32') # Output of the forward function, name/dtype/shape must be specified output = create_tmp_var('output','int32', [3,1]) # Multiple Variable should be passed in the form of tuple(Variale) or list[Variale] - fluid.layers.py_func(func=element_wise_add, x=[x,y], out=output) + paddle.static.nn.py_func(func=element_wise_add, x=[x,y], out=output) - exe=fluid.Executor(fluid.CPUPlace()) + exe=paddle.static.Executor(paddle.CPUPlace()) exe.run(start_program) # Feed numpy array to main_program diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 2fba578ec077f..58d015c89dd77 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -103,9 +103,8 @@ def create_parameter(shape, Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle.fluid.layers as layers - W = layers.create_parameter(shape=[784, 200], dtype='float32') + import paddle + W = paddle.static.create_parameter(shape=[784, 200], dtype='float32') """ check_type(shape, 'shape', (list, tuple, numpy.ndarray), 'create_parameter') for item in shape: @@ -161,9 +160,8 @@ def create_global_var(shape, Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle.fluid.layers as layers - var = layers.create_global_var(shape=[2,3], value=1.0, dtype='float32', + import paddle + var = paddle.static.create_global_var(shape=[2,3], value=1.0, dtype='float32', persistable=True, force_cpu=True, name='new_var') """ check_type(shape, 'shape', (list, tuple, numpy.ndarray), diff --git a/python/paddle/fluid/param_attr.py b/python/paddle/fluid/param_attr.py index 8e0470bededd4..f43fd73a4e22c 100644 --- a/python/paddle/fluid/param_attr.py +++ b/python/paddle/fluid/param_attr.py @@ -61,15 +61,16 @@ class ParamAttr(object): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle - w_param_attrs = fluid.ParamAttr(name="fc_weight", - learning_rate=0.5, - regularizer=fluid.regularizer.L2Decay(1.0), - trainable=True) + w_param_attrs = paddle.ParamAttr(name="fc_weight", + learning_rate=0.5, + regularizer=paddle.regularizer.L2Decay(1.0), + trainable=True) print(w_param_attrs.name) # "fc_weight" - x = fluid.data(name='X', shape=[None, 1], dtype='float32') - y_predict = fluid.layers.fc(input=x, size=10, param_attr=w_param_attrs) + paddle.enable_static() + x = paddle.data(name='X', shape=[None, 1], dtype='float32') + y_predict = paddle.static.nn.fc(input=x, size=10, param_attr=w_param_attrs) """ def __init__(self, @@ -202,7 +203,7 @@ def _to_kwargs(self, with_initializer=False): class WeightNormParamAttr(ParamAttr): """ - :api_attr: Static Graph + :api_attr: Static Graph Note: Please use 'paddle.nn.utils.weight_norm' in dygraph mode. diff --git a/python/paddle/static/__init__.py b/python/paddle/static/__init__.py index e0a9bc6eec3ba..7b778ef4e7dc4 100644 --- a/python/paddle/static/__init__.py +++ b/python/paddle/static/__init__.py @@ -23,6 +23,7 @@ ] from . import nn +from .fluid import Scope #DEFINE_ALIAS from .input import data #DEFINE_ALIAS from .input import InputSpec #DEFINE_ALIAS from ..fluid.executor import Executor #DEFINE_ALIAS @@ -48,3 +49,5 @@ from ..fluid.io import load_inference_model #DEFINE_ALIAS from ..fluid.io import load_program_state #DEFINE_ALIAS from ..fluid.io import set_program_state #DEFINE_ALIAS +from ..fluid.layers import create_parameter #DEFINE_ALIAS +from ..fluid.layers import create_global_var #DEFINE_ALIAS diff --git a/python/paddle/static/nn/__init__.py b/python/paddle/static/nn/__init__.py index 91da0926b1870..6601baed7ecef 100644 --- a/python/paddle/static/nn/__init__.py +++ b/python/paddle/static/nn/__init__.py @@ -32,6 +32,7 @@ 'multi_box_head', 'nce', 'prelu', + 'py_func', 'row_conv', 'spectral_norm', ] @@ -54,6 +55,7 @@ from ...fluid.layers import multi_box_head #DEFINE_ALIAS from ...fluid.layers import nce #DEFINE_ALIAS from ...fluid.layers import prelu #DEFINE_ALIAS +from ...fluid.layers import py_func #DEFINE_ALIAS from ...fluid.layers import row_conv #DEFINE_ALIAS from ...fluid.layers import spectral_norm #DEFINE_ALIAS diff --git a/tools/wlist.json b/tools/wlist.json index 9b36ac6adc7f7..32cd21b3ee2d4 100644 --- a/tools/wlist.json +++ b/tools/wlist.json @@ -279,7 +279,6 @@ "thresholded_relu", "group_norm", "random_crop", - "py_func", "row_conv", "hard_shrink", "ssd_loss", From 0dfc404ec8b0272740a035ad1dd6f0b5afc19afd Mon Sep 17 00:00:00 2001 From: jiweibo Date: Sun, 27 Sep 2020 06:12:22 +0000 Subject: [PATCH 2/6] update. --- python/paddle/fluid/executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index ae4985983e4e4..e314f6966a564 100644 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -99,7 +99,7 @@ def scope_guard(scope): new_scope = paddle.static.Scope() with paddle.static.scope_guard(new_scope): - paddle.static.global_scope().var("data").get_tensor().set(paddle.ones((2, 2)), paddle.CPUPlace()) + paddle.static.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), paddle.CPUPlace()) numpy.array(new_scope.find_var("data").get_tensor()) """ From 5eda19e829622ac9a95a78ae62ae1e6e06e47c51 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Sun, 27 Sep 2020 07:02:35 +0000 Subject: [PATCH 3/6] fix error. --- python/paddle/static/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/static/__init__.py b/python/paddle/static/__init__.py index 7b778ef4e7dc4..52535391d074d 100644 --- a/python/paddle/static/__init__.py +++ b/python/paddle/static/__init__.py @@ -23,7 +23,7 @@ ] from . import nn -from .fluid import Scope #DEFINE_ALIAS +from ..fluid import Scope #DEFINE_ALIAS from .input import data #DEFINE_ALIAS from .input import InputSpec #DEFINE_ALIAS from ..fluid.executor import Executor #DEFINE_ALIAS From 1536d2930981d5179b18794821621da4db2ba083 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Sun, 27 Sep 2020 08:30:53 +0000 Subject: [PATCH 4/6] data -> static.data --- python/paddle/fluid/layers/nn.py | 4 ++-- python/paddle/fluid/param_attr.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index bbbc952ec81de..e1a7bddbfae43 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -13638,8 +13638,8 @@ def py_func_demo(): main_program = paddle.static.default_main_program() # Input of the forward function - x = paddle.data(name='x', shape=[2,3], dtype='int32') - y = paddle.data(name='y', shape=[2,3], dtype='int32') + x = paddle.static.data(name='x', shape=[2,3], dtype='int32') + y = paddle.static.data(name='y', shape=[2,3], dtype='int32') # Output of the forward function, name/dtype/shape must be specified output = create_tmp_var('output','int32', [3,1]) diff --git a/python/paddle/fluid/param_attr.py b/python/paddle/fluid/param_attr.py index f43fd73a4e22c..17a09fb6c0c3b 100644 --- a/python/paddle/fluid/param_attr.py +++ b/python/paddle/fluid/param_attr.py @@ -69,7 +69,7 @@ class ParamAttr(object): trainable=True) print(w_param_attrs.name) # "fc_weight" paddle.enable_static() - x = paddle.data(name='X', shape=[None, 1], dtype='float32') + x = paddle.static.data(name='X', shape=[None, 1], dtype='float32') y_predict = paddle.static.nn.fc(input=x, size=10, param_attr=w_param_attrs) """ From fac3b55acdd8a98171235f8b53dba2583d9ed10d Mon Sep 17 00:00:00 2001 From: jiweibo Date: Mon, 28 Sep 2020 02:48:23 +0000 Subject: [PATCH 5/6] update. --- python/paddle/fluid/executor.py | 1 + python/paddle/fluid/layers/nn.py | 24 +++++++++++++----------- python/paddle/fluid/layers/tensor.py | 2 ++ python/paddle/fluid/param_attr.py | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index e314f6966a564..7d067b6347844 100644 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -96,6 +96,7 @@ def scope_guard(scope): import paddle import numpy + paddle.enable_static() new_scope = paddle.static.Scope() with paddle.static.scope_guard(new_scope): diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index e1a7bddbfae43..2c84aa482d787 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -13511,14 +13511,14 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): :api_attr: Static Graph This OP is used to register customized Python OP to Paddle. The design - principe of py_func is that LodTensor and numpy array can be converted to each + principe of py_func is that Tensor and numpy array can be converted to each other easily. So you can use Python and numpy API to register a python OP. The forward function of the registered OP is ``func`` and the backward function of that is ``backward_func``. Paddle will call ``func`` at forward runtime and call ``backward_func`` at backward runtime(if ``backward_func`` is not None). - ``x`` is the input of ``func``, whose type must be LoDTensor; ``out`` is - the output of ``func``, whose type can be either LoDTensor or numpy array. + ``x`` is the input of ``func``, whose type must be Tensor; ``out`` is + the output of ``func``, whose type can be either Tensor or numpy array. The input of the backward function ``backward_func`` is ``x``, ``out`` and the gradient of ``out``. If some variables of ``out`` have no gradient, the @@ -13536,14 +13536,14 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): func (callable): The forward function of the registered OP. When the network is running, the forward output ``out`` will be calculated according to this function and the forward input ``x``. In ``func`` , it's suggested that we - actively convert LoDTensor into a numpy array, so that we can use Python and + actively convert Tensor into a numpy array, so that we can use Python and numpy API arbitrarily. If not, some operations of numpy may not be compatible. x (Variable|tuple(Variale)|list[Variale]): The input of the forward function ``func``. - It can be Variable|tuple(Variale)|list[Variale], where Variable is LoDTensor or + It can be Variable|tuple(Variale)|list[Variale], where Variable is Tensor or Tenosor. In addition, Multiple Variable should be passed in the form of tuple(Variale) or list[Variale]. out (Variable|tuple(Variale)|list[Variale]): The output of the forward function ``func``, - it can be Variable|tuple(Variale)|list[Variale], where Variable can be either LoDTensor + it can be Variable|tuple(Variale)|list[Variale], where Variable can be either Tensor or numpy array. Since Paddle cannot automatically infer the shape and type of ``out``, you must create ``out`` in advance. backward_func (callable, optional): The backward function of the registered OP. @@ -13567,13 +13567,15 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): import paddle import six - # Creates a forward function, LodTensor can be input directly without + paddle.enable_static() + + # Creates a forward function, Tensor can be input directly without # being converted into numpy array. def tanh(x): return np.tanh(x) # Skip x in backward function and return the gradient of x - # LodTensor must be actively converted to numpy array, otherwise, + # Tensor must be actively converted to numpy array, otherwise, # operations such as +/- can't be used. def tanh_grad(y, dy): return np.array(dy) * (1 - np.square(np.array(y))) @@ -13598,7 +13600,7 @@ def simple_net(img, label): out=new_hidden, backward_func=tanh_grad, skip_vars_in_backward_input=hidden) - # User-defined debug functions that print out the input LodTensor + # User-defined debug functions that print out the input Tensor paddle.static.nn.py_func(func=debug_func, x=hidden, out=None) prediction = paddle.static.nn.fc(hidden, size=10, act='softmax') @@ -13606,7 +13608,7 @@ def simple_net(img, label): return paddle.mean(loss) # example 2: - # This example shows how to turn LoDTensor into numpy array and + # This example shows how to turn Tensor into numpy array and # use numpy API to register an Python OP import paddle import numpy as np @@ -13614,7 +13616,7 @@ def simple_net(img, label): paddle.enable_static() def element_wise_add(x, y): - # LodTensor must be actively converted to numpy array, otherwise, + # Tensor must be actively converted to numpy array, otherwise, # numpy.shape can't be used. x = np.array(x) y = np.array(y) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 58d015c89dd77..c633f7022d75e 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -104,6 +104,7 @@ def create_parameter(shape, .. code-block:: python import paddle + paddle.enable_static() W = paddle.static.create_parameter(shape=[784, 200], dtype='float32') """ check_type(shape, 'shape', (list, tuple, numpy.ndarray), 'create_parameter') @@ -161,6 +162,7 @@ def create_global_var(shape, .. code-block:: python import paddle + paddle.enable_static() var = paddle.static.create_global_var(shape=[2,3], value=1.0, dtype='float32', persistable=True, force_cpu=True, name='new_var') """ diff --git a/python/paddle/fluid/param_attr.py b/python/paddle/fluid/param_attr.py index 17a09fb6c0c3b..3cdc5913946cd 100644 --- a/python/paddle/fluid/param_attr.py +++ b/python/paddle/fluid/param_attr.py @@ -62,13 +62,13 @@ class ParamAttr(object): .. code-block:: python import paddle + paddle.enable_static() w_param_attrs = paddle.ParamAttr(name="fc_weight", learning_rate=0.5, regularizer=paddle.regularizer.L2Decay(1.0), trainable=True) print(w_param_attrs.name) # "fc_weight" - paddle.enable_static() x = paddle.static.data(name='X', shape=[None, 1], dtype='float32') y_predict = paddle.static.nn.fc(input=x, size=10, param_attr=w_param_attrs) """ From 3ddf3eb44ee37f7a20b840e44fa7a9e5ec95d55f Mon Sep 17 00:00:00 2001 From: jiweibo Date: Mon, 28 Sep 2020 12:43:57 +0000 Subject: [PATCH 6/6] update for dynamic demo. --- python/paddle/fluid/param_attr.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/python/paddle/fluid/param_attr.py b/python/paddle/fluid/param_attr.py index 3cdc5913946cd..1d7f60fb1d7bb 100644 --- a/python/paddle/fluid/param_attr.py +++ b/python/paddle/fluid/param_attr.py @@ -64,13 +64,12 @@ class ParamAttr(object): import paddle paddle.enable_static() - w_param_attrs = paddle.ParamAttr(name="fc_weight", - learning_rate=0.5, - regularizer=paddle.regularizer.L2Decay(1.0), - trainable=True) - print(w_param_attrs.name) # "fc_weight" - x = paddle.static.data(name='X', shape=[None, 1], dtype='float32') - y_predict = paddle.static.nn.fc(input=x, size=10, param_attr=w_param_attrs) + weight_attr = paddle.ParamAttr(name="weight", + learning_rate=0.5, + regularizer=paddle.regularizer.L2Decay(1.0), + trainable=True) + print(weight_attr.name) # "weight" + paddle.nn.Linear(3, 4, weight_attr=weight_attr) """ def __init__(self,