move imperative to dygraph, test=develop

revert-16555-model_data_cryption_link_all_lib
lujun 6 years ago
parent cc29bec6e6
commit 1c9aaeebe0

@ -34,7 +34,7 @@ from . import io
from . import evaluator
from . import initializer
from . import layers
from . import imperative
from . import dygraph
from . import contrib
from . import nets
from . import optimizer
@ -71,7 +71,7 @@ __all__ = framework.__all__ + executor.__all__ + \
'initializer',
'layers',
'contrib',
'imperative',
'dygraph',
'transpiler',
'nets',
'optimizer',

@ -22,7 +22,7 @@ __all__ = ['enabled', 'guard', 'to_variable']
def enabled():
return framework._in_imperative_mode()
return framework._in_dygraph_mode()
@signature_safe_contextmanager
@ -39,14 +39,14 @@ def guard(place=None):
with framework.program_guard(train, startup):
with framework.unique_name.guard():
with framework._imperative_guard(tracer):
with framework._imperative_place_guard(place):
with framework._dygraph_guard(tracer):
with framework._dygraph_place_guard(place):
yield
def to_variable(value, block=None, name=None):
if isinstance(value, np.ndarray):
assert enabled(), "to_variable could only be called in imperative mode"
assert enabled(), "to_variable could only be called in dygraph mode"
if not block:
block = framework.default_main_program().current_block()

@ -68,7 +68,7 @@ def save_persistables(vardict, dirname, filename=None):
dy_loss, last_hidden, last_cell = ptb_model(x, y, init_hidden,
init_cell)
param_path = "./my_paddle_model"
fluid.imperative.checkpoint.save_persistables(ptb_model.state_dict(), dirname=param_path,
fluid.dygraph.save_persistables(ptb_model.state_dict(), dirname=param_path,
layer=ptb_model)
"""
if isinstance(vardict, collections.OrderedDict):
@ -97,17 +97,17 @@ def load_persistables(vardict, dirname, filename=None):
Examples:
.. code-block:: python
my_layer = layer(fluid.imperative.Layer)
my_layer = layer(fluid.dygraph.Layer)
param_path = "./my_paddle_model"
param_dict = fluid.imperative.checkpoint.load_persistables(my_layer.parameters(), param_path)
param_dict = fluid.dygraph.load_persistables(my_layer.parameters(), param_path)
param_1 = param_dict['PtbModel_0.w_1']
or:
my_layer = layer(fluid.imperative.Layer)
my_layer = layer(fluid.dygraph.Layer)
param_path = "./my_paddle_model"
filename = "model.file"
param_dict = fluid.imperative.checkpoint.load_persistables(my_layer.state_dict(), param_path,
param_dict = fluid.dygraph.load_persistables(my_layer.state_dict(), param_path,
filename=filename)
param_1 = param_dict['PtbModel_0.w_1']

@ -16,7 +16,7 @@ from __future__ import print_function
import copy
import six
from ..framework import Parameter, _in_imperative_mode
from ..framework import Parameter, _in_dygraph_mode
from ..param_attr import ParamAttr
from .. import core
from six.moves import zip

@ -283,7 +283,7 @@ class PyLayer(core.PyLayer):
@classmethod
def __call__(cls, *inputs):
tracer = framework._imperative_tracer()
tracer = framework._dygraph_tracer()
block = framework.default_main_program().current_block()
ivar_inputs = [x._ivar for x in inputs]

@ -133,7 +133,7 @@ class Conv2D(layers.Layer):
outputs={'Out': [pre_act]},
attrs={'axis': 1})
# Currently, we don't support inplace in imperative mode
# Currently, we don't support inplace in dygraph mode
return self._helper.append_activation(pre_act, act=self._act)
@ -265,7 +265,7 @@ class FC(layers.Layer):
attrs={'axis': self._num_flatten_dims})
else:
pre_activation = pre_bias
# Currently, we don't support inplace in imperative mode
# Currently, we don't support inplace in dygraph mode
return self._helper.append_activation(pre_activation, act=self._act)
@ -387,7 +387,7 @@ class BatchNorm(layers.Layer):
"use_global_stats": self._use_global_stats
})
# Currently, we don't support inplace in imperative mode
# Currently, we don't support inplace in dygraph mode
return self._helper.append_activation(batch_norm_out, self._act)
@ -426,7 +426,7 @@ class Embedding(layers.Layer):
dict_size = len(dataset.ids)
input = fluid.layers.data(name='ids', shape=[32, 32], dtype='float32')
embedding = fluid.imperative.Embedding(size=[dict_size, 16])
embedding = fluid.dygraph.Embedding(size=[dict_size, 16])
fc = embedding(input)
"""

@ -24,12 +24,12 @@ __all__ = ['Tracer']
def release_op(op):
del framework._imperative_tracer()._ops[op._trace_id]
del framework._dygraph_tracer()._ops[op._trace_id]
class Tracer(core.Tracer):
"""
Python wrapper of imperative tracer
Python wrapper of dygraph tracer
"""
def __init__(self, block):

@ -75,20 +75,20 @@ GRAD_VAR_SUFFIX = core.kGradVarSuffix()
ZERO_VAR_SUFFIX = core.kZeroVarSuffix()
CONTROL_DEP_VAR_PREFIX = core.kControlDepVarName()
_imperative_tracer_ = None
_imperative_current_expected_place_ = None
_dygraph_tracer_ = None
_dygraph_current_expected_place_ = None
def _in_imperative_mode():
return _imperative_tracer_ is not None
def _in_dygraph_mode():
return _dygraph_tracer_ is not None
def _imperative_tracer():
return _imperative_tracer_
def _dygraph_tracer():
return _dygraph_tracer_
def _current_expected_place():
return _imperative_current_expected_place_
return _dygraph_current_expected_place_
def _cpu_num():
@ -396,7 +396,7 @@ class Variable(object):
if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)
if _in_imperative_mode():
if _in_dygraph_mode():
# record vars in tracer rather than blocks
self._ivar = kwargs.get("ivar", None)
if not self._ivar:
@ -406,7 +406,7 @@ class Variable(object):
_current_expected_place(), stop_gradient, True
if persistable else False)
if persistable:
_imperative_tracer().trace_var(name, self)
_dygraph_tracer().trace_var(name, self)
else:
self.error_clip = error_clip
@ -515,8 +515,8 @@ class Variable(object):
Returns:
str: The debug string.
"""
if _in_imperative_mode():
# TODO(panyx0718): add more imperative debug info.
if _in_dygraph_mode():
# TODO(panyx0718): add more dygraph debug info.
return 'name %s, dtype: %s shape: %s' % (self.name, self.dtype,
self.shape)
@ -548,42 +548,42 @@ class Variable(object):
@property
def _stop_gradient(self):
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.stop_gradient
else:
return self.stop_gradient
@_stop_gradient.setter
def _stop_gradient(self, s):
if _in_imperative_mode():
if _in_dygraph_mode():
self._ivar.stop_gradient = s
else:
self.stop_gradient = s
@property
def persistable(self):
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.persistable
else:
return self.desc.persistable()
@persistable.setter
def persistable(self, p):
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.persistable
else:
self.desc.set_persistable(p)
@property
def name(self):
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.name
else:
return cpt.to_text(self.desc.name())
@name.setter
def name(self, new_name):
if _in_imperative_mode():
if _in_dygraph_mode():
self._ivar.name = new_name
else:
self.desc.set_name(new_name)
@ -591,26 +591,26 @@ class Variable(object):
@property
def shape(self):
# convert to tuple, make it as same as numpy API.
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.shape
else:
return tuple(self.desc.shape())
@property
def dtype(self):
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.dtype
else:
return self.desc.dtype()
@property
def lod_level(self):
# TODO(minqiyang): Support lod_level in imperative mode
# TODO(minqiyang): Support lod_level in dygraph mode
return self.desc.lod_level()
@property
def type(self):
if _in_imperative_mode():
if _in_dygraph_mode():
return self._ivar.dtype
else:
return self.desc.type()
@ -918,7 +918,7 @@ class Operator(object):
inputs=None,
outputs=None,
attrs=None):
if _in_imperative_mode():
if _in_dygraph_mode():
if type is None:
raise ValueError(
"`type` to initialized an Operator can not be None.")
@ -1037,7 +1037,7 @@ class Operator(object):
for arg in out_args:
out_arg_names.append(cpt.to_text(arg.name))
# TODO(minqiyang): could we remove variable's op in static mode?
if not _in_imperative_mode():
if not _in_dygraph_mode():
arg.op = self
self.desc.set_output(out_proto.name, out_arg_names)
@ -1083,7 +1083,7 @@ class Operator(object):
@property
def type(self):
if _in_imperative_mode():
if _in_dygraph_mode():
return self.iop.type
else:
return self.desc.type()
@ -1626,7 +1626,7 @@ class Block(object):
Returns:
Operator: the append Operator.
"""
if _in_imperative_mode():
if _in_dygraph_mode():
op = Operator(
block=self,
desc=None,
@ -1638,9 +1638,8 @@ class Block(object):
# record ops in tracer rather than blocks
#
# TODO(minqiyang): add op stop_gradient support in static mode too.
# currently, we only support stop_gradient in imperative mode.
_imperative_tracer().trace_op(op,
kwargs.get("stop_gradient", False))
# currently, we only support stop_gradient in dygraph mode.
_dygraph_tracer().trace_op(op, kwargs.get("stop_gradient", False))
else:
op_desc = self.desc.append_op()
op = Operator(
@ -1699,7 +1698,7 @@ class Block(object):
return self.ops[start:end]
def _prepend_op(self, *args, **kwargs):
if _in_imperative_mode():
if _in_dygraph_mode():
op = Operator(
self,
None,
@ -1707,8 +1706,7 @@ class Block(object):
inputs=kwargs.get("inputs", None),
outputs=kwargs.get("outputs", None),
attrs=kwargs.get("attrs", None))
_imperative_tracer().trace_op(op,
kwargs.get("stop_gradient", False))
_dygraph_tracer().trace_op(op, kwargs.get("stop_gradient", False))
else:
op_desc = self.desc._prepend_op()
op = Operator(
@ -3541,22 +3539,22 @@ def _get_var(name, program=None):
@signature_safe_contextmanager
def _imperative_guard(tracer):
global _imperative_tracer_
tmp_trace = _imperative_tracer_
_imperative_tracer_ = tracer
def _dygraph_guard(tracer):
global _dygraph_tracer_
tmp_trace = _dygraph_tracer_
_dygraph_tracer_ = tracer
yield
_imperative_tracer_ = tmp_trace
_dygraph_tracer_ = tmp_trace
@signature_safe_contextmanager
def _imperative_place_guard(place):
global _imperative_current_expected_place_
tmp_place = _imperative_current_expected_place_
_imperative_current_expected_place_ = place
def _dygraph_place_guard(place):
global _dygraph_current_expected_place_
tmp_place = _dygraph_current_expected_place_
_dygraph_current_expected_place_ = place
yield
_imperative_current_expected_place_ = tmp_place
_dygraph_current_expected_place_ = tmp_place

@ -165,7 +165,7 @@ class ConstantInitializer(Initializer):
'force_cpu': self._force_cpu or force_init_on_cpu()
},
stop_gradient=True)
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -245,7 +245,7 @@ class UniformInitializer(Initializer):
attrs={"in_dtype": out_var.dtype,
"out_dtype": var.dtype})
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -324,7 +324,7 @@ class NormalInitializer(Initializer):
outputs={"Out": var},
attrs={"in_dtype": out_var.dtype,
"out_dtype": var.dtype})
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -403,7 +403,7 @@ class TruncatedNormalInitializer(Initializer):
outputs={"Out": var},
attrs={"in_dtype": out_var.dtype,
"out_dtype": var.dtype})
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -509,7 +509,7 @@ class XavierInitializer(Initializer):
"seed": self._seed
},
stop_gradient=True)
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -610,7 +610,7 @@ class MSRAInitializer(Initializer):
"seed": self._seed
},
stop_gradient=True)
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -709,7 +709,7 @@ class BilinearInitializer(Initializer):
'shape': list(shape),
value_name: values
})
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op
@ -768,7 +768,7 @@ class NumpyArrayInitializer(Initializer):
value_name: values
},
stop_gradient=True)
if not framework._in_imperative_mode():
if not framework._in_dygraph_mode():
var.op = op
return op

@ -17,7 +17,7 @@ from .param_attr import ParamAttr
from .initializer import Constant
from . import layers
from . import backward
from .imperative import Layer, nn
from .dygraph import Layer, nn
from . import executor
from . import core

@ -17,7 +17,7 @@ from __future__ import print_function
import copy
import six
from .framework import Parameter, dtype_is_floating, _in_imperative_mode
from .framework import Parameter, dtype_is_floating, _in_dygraph_mode
from . import unique_name
from paddle.fluid.initializer import Constant, Xavier
from .param_attr import ParamAttr
@ -30,9 +30,9 @@ class LayerHelper(LayerHelperBase):
def __init__(self, layer_type, **kwargs):
self.kwargs = kwargs
name = self.kwargs.get('name', None)
# TODO(panyx0718, minqiyang): imperative mode
# TODO(panyx0718, minqiyang): dygraph mode
# can not use both `layer_type` and `name`. Deprecate LayerHelper
# and write a Helper for imperative mode.
# and write a Helper for dygraph mode.
if name is None:
self.kwargs['name'] = unique_name.generate(layer_type)

@ -17,7 +17,7 @@ from __future__ import print_function
import copy
import numpy as np
from .framework import Variable, default_main_program, default_startup_program, _in_imperative_mode, _current_expected_place
from .framework import Variable, default_main_program, default_startup_program, _in_dygraph_mode, _current_expected_place
from . import unique_name
from .param_attr import ParamAttr, WeightNormParamAttr
from . import core
@ -54,8 +54,8 @@ class LayerHelperBase(object):
Return Variable construct from value
"""
if isinstance(value, np.ndarray):
assert _in_imperative_mode(
), "to_variable could only be called in imperative mode"
assert _in_dygraph_mode(
), "to_variable could only be called in dygraph mode"
if not block:
block = default_main_program().current_block()
@ -302,8 +302,8 @@ class LayerHelperBase(object):
param = self._create_weight_normalize(attr, shape, dtype)
WeightNormParamAttr.params_with_weight_norm.append(param)
return param
if _in_imperative_mode():
# In imperative mode, we want the returned parameter to be
if _in_dygraph_mode():
# In dygraph mode, we want the returned parameter to be
# initialized so that it can be used imperatively.
return self.main_program.global_block().create_parameter(
dtype=dtype,
@ -370,7 +370,7 @@ class LayerHelperBase(object):
initializer: initializer to use
"""
assert isinstance(var, Variable)
if _in_imperative_mode():
if _in_dygraph_mode():
initializer(var, var.block)
else:
self.startup_program.global_block().create_var(

@ -23,8 +23,8 @@ import os
import inspect
from ..layer_helper import LayerHelper
from ..initializer import Normal, Constant, NumpyArrayInitializer
from ..framework import Variable, OpProtoHolder, _in_imperative_mode
from ..imperative import base
from ..framework import Variable, OpProtoHolder, _in_dygraph_mode
from ..dygraph import base
from ..param_attr import ParamAttr
from .layer_function_generator import autodoc, templatedoc, _generate_doc_string_
from .tensor import concat, assign
@ -32,7 +32,7 @@ from . import utils
from .. import unique_name
from functools import reduce
from .. import core
from ..imperative import layers
from ..dygraph import layers
__all__ = [
'fc',
@ -296,7 +296,6 @@ def fc(input,
data_2 = fluid.layers.data(name="data_2", shape=[24, 36], dtype="float32")
fc = fluid.layers.fc(input=[data_1, data_2], size=1000, act="tanh")
"""
helper = LayerHelper("fc", **locals())
dtype = helper.input_dtype()
@ -3279,6 +3278,8 @@ def layer_norm(input,
>>> dtype='float32')
>>> x = fluid.layers.layer_norm(input=data, begin_norm_axis=1)
"""
assert _in_dygraph_mode(
) is not True, "please use FC instead of fc in dygraph mode!"
helper = LayerHelper('layer_norm', **locals())
dtype = helper.input_dtype()
@ -6405,8 +6406,8 @@ def squeeze(input, axes, name=None):
x = layers.data(name='x', shape=[5, 1, 10])
y = layers.sequeeze(input=x, axes=[1])
"""
assert not _in_imperative_mode(), (
"squeeze layer is not supported in imperative mode yet.")
assert not _in_dygraph_mode(), (
"squeeze layer is not supported in dygraph mode yet.")
helper = LayerHelper("squeeze", **locals())
out = helper.create_variable_for_type_inference(dtype=input.dtype)
x_shape = helper.create_variable_for_type_inference(dtype=input.dtype)
@ -9144,7 +9145,7 @@ def _elementwise_op(helper):
op_type = helper.layer_type
x = helper.kwargs.get('x', None)
y = helper.kwargs.get('y', None)
if _in_imperative_mode():
if _in_dygraph_mode():
x = base.to_variable(x)
y = base.to_variable(y)

@ -20,7 +20,6 @@ from ..framework import convert_np_dtype_to_dtype_
from ..framework import Variable
from ..initializer import Constant, force_init_on_cpu
from ..core import VarDesc
from ..imperative import base as imperative_base
from .layer_function_generator import templatedoc
import numpy

@ -30,7 +30,6 @@ from .initializer import Constant
from .layer_helper import LayerHelper
from .layers import ops
from .regularizer import append_regularization_ops
from .imperative import base as imperative_base
from paddle.fluid import core
from paddle.fluid.layers import tensor
from functools import reduce
@ -169,7 +168,7 @@ class Optimizer(object):
name = self._name + "_" + name
if (name in self._accumulators and
param.name in self._accumulators[name]):
if framework._in_imperative_mode():
if framework._in_dygraph_mode():
return self._accumulators[name][param.name]
raise Exception("Accumulator {} already exists for parameter {}".
format(name, param.name))
@ -396,11 +395,11 @@ class Optimizer(object):
"""
self._dtype = loss.dtype
optimize_ops = []
if framework._in_imperative_mode():
if framework._in_dygraph_mode():
if parameter_list is not None:
parameters = parameter_list
else:
parameters = framework._imperative_tracer().all_parameters()
parameters = framework._dygraph_tracer().all_parameters()
params_grads = []
for param in parameters:

@ -262,14 +262,14 @@ class OpTest(unittest.TestCase):
if isinstance(value, tuple):
data = value[0]
lod = value[1]
v = fluid.imperative.base.to_variable(value=data)
v = fluid.dygraph.base.to_variable(value=data)
v._ivar.value().get_tensor().set_recursive_sequence_lengths(lod)
return v
else:
return fluid.imperative.base.to_variable(value)
return fluid.dygraph.base.to_variable(value)
def _calc_imperative_output(self, place, parallel=False, no_check_set=None):
with fluid.imperative.base.guard(place=place):
def _calc_dygraph_output(self, place, parallel=False, no_check_set=None):
with fluid.dygraph.base.guard(place=place):
block = fluid.default_main_program().global_block()
# prepare input variable
@ -316,7 +316,7 @@ class OpTest(unittest.TestCase):
return outputs
def _calc_output(self, place, parallel=False, no_check_set=None):
def _calc_output(self, place, parallel=False, no_check_set=None, loss=None):
program = Program()
block = program.global_block()
self._append_ops(block)
@ -329,8 +329,14 @@ class OpTest(unittest.TestCase):
use_cuda = False
if isinstance(place, fluid.CUDAPlace(0)):
use_cuda = True
executor = fluid.ParallelExecutor(
use_cuda=use_cuda, loss_name=loss.name, main_program=program)
if loss:
executor = fluid.ParallelExecutor(
use_cuda=use_cuda,
loss_name=loss.name,
main_program=program)
else:
executor = fluid.ParallelExecutor(
use_cuda=use_cuda, main_program=program)
else:
executor = Executor(place)
@ -364,9 +370,9 @@ class OpTest(unittest.TestCase):
atol,
no_check_set=None,
equal_nan=False,
check_imperative=False):
if check_imperative:
imperative_outs = self._calc_imperative_output(
check_dygraph=False):
if check_dygraph:
dygraph_outs = self._calc_dygraph_output(
place, no_check_set=no_check_set)
outs, fetch_list = self._calc_output(place, no_check_set=no_check_set)
@ -393,8 +399,8 @@ class OpTest(unittest.TestCase):
type(sub_out))
for item in sub_out:
sub_out_name, expect = item[0], item[1]
if check_imperative:
imperative_actual = imperative_outs[sub_out_name][0]
if check_dygraph:
imperative_actual = dygraph_outs[sub_out_name][0]
imperative_actual_t = np.array(
imperative_actual._ivar.value().get_tensor())
idx = find_actual(sub_out_name, fetch_list)
@ -407,7 +413,7 @@ class OpTest(unittest.TestCase):
actual_t, expect_t, atol=atol, equal_nan=equal_nan),
"Output (" + sub_out_name + ") has diff at " +
str(place))
if check_imperative:
if check_dygraph:
self.assertTrue(
np.allclose(
imperative_actual_t,
@ -415,21 +421,21 @@ class OpTest(unittest.TestCase):
atol=atol,
equal_nan=equal_nan),
"Output (" + sub_out_name + ") has diff at " +
str(place) + " in imperative mode")
str(place) + " in dygraph mode")
if isinstance(expect, tuple):
self.assertListEqual(
actual.recursive_sequence_lengths(), expect[1],
"Output (" + sub_out_name +
") has different lod at " + str(place))
if check_imperative:
if check_dygraph:
self.assertListEqual(
imperative_actual._ivar.value().get_tensor()
.recursive_sequence_lengths(), expect[1],
"Output (" + out_name + ") has different lod at " +
str(place) + " in imperative mode")
str(place) + " in dygraph mode")
else:
if check_imperative:
imperative_actual = imperative_outs[out_name][0]
if check_dygraph:
imperative_actual = dygraph_outs[out_name][0]
imperative_actual_t = np.array(
imperative_actual._ivar.value().get_tensor())
idx = find_actual(out_name, fetch_list)
@ -443,7 +449,7 @@ class OpTest(unittest.TestCase):
"Output (" + out_name + ") has diff at " + str(place) +
"\nExpect " + str(expect_t) + "\n" + "But Got" +
str(actual_t) + " in class " + self.__class__.__name__)
if check_imperative:
if check_dygraph:
self.assertTrue(
np.allclose(
imperative_actual_t,
@ -458,12 +464,12 @@ class OpTest(unittest.TestCase):
self.assertListEqual(actual.recursive_sequence_lengths(),
expect[1], "Output (" + out_name +
") has different lod at " + str(place))
if check_imperative:
if check_dygraph:
self.assertListEqual(
imperative_actual._ivar.value().get_tensor()
.recursive_sequence_lengths(), expect[1],
"Output (" + out_name + ") has different lod at " +
str(place) + " in imperative mode")
str(place) + " in dygraph mode")
def _get_places(self):
if self.dtype == np.float16:
@ -490,11 +496,11 @@ class OpTest(unittest.TestCase):
atol=1e-5,
no_check_set=None,
equal_nan=False,
check_imperative=False):
check_dygraph=False):
places = self._get_places()
for place in places:
self.check_output_with_place(place, atol, no_check_set, equal_nan,
check_imperative)
check_dygraph)
def check_output_customized(self, checker):
places = self._get_places()

@ -18,7 +18,7 @@ import numpy as np
import paddle.fluid as fluid
class L1(fluid.imperative.Layer):
class L1(fluid.dygraph.Layer):
def __init__(self, prefix):
super(L1, self).__init__(prefix)
self._param_attr = fluid.ParamAttr(
@ -32,7 +32,7 @@ class L1(fluid.imperative.Layer):
return self.w1 + self.w2
class L2(fluid.imperative.Layer):
class L2(fluid.dygraph.Layer):
def __init__(self, prefix):
super(L2, self).__init__(prefix)
self.layer1 = L1(self.full_name())
@ -42,7 +42,7 @@ class L2(fluid.imperative.Layer):
return self.layer1() + self.layer2()
class L3(fluid.imperative.Layer):
class L3(fluid.dygraph.Layer):
def __init__(self, prefix):
super(L3, self).__init__(prefix)
self.layer1 = L2(self.full_name())
@ -54,7 +54,7 @@ class L3(fluid.imperative.Layer):
class TestBaseLayer(unittest.TestCase):
def test_one_level(self):
with fluid.imperative.guard():
with fluid.dygraph.guard():
l = L1('test_one_level')
ret = l()
self.assertEqual(l.w1.name, "test_one_level/L1_0.w_0")
@ -62,7 +62,7 @@ class TestBaseLayer(unittest.TestCase):
self.assertTrue(np.allclose(ret._numpy(), 0.2 * np.ones([2, 2])))
def test_three_level(self):
with fluid.imperative.guard():
with fluid.dygraph.guard():
l = L3('test_three_level')
names = [p.name for p in l.parameters()]
ret = l()

@ -156,7 +156,7 @@ class TestGRUOp(OpTest):
}
def test_check_output(self):
self.check_output(atol=1e-8, check_imperative=True)
self.check_output(atol=1e-8, check_dygraph=True)
def test_check_grad(self):
self.check_grad(['Input', 'H0', 'Weight', 'Bias'], ['Hidden'])

@ -18,11 +18,11 @@ import numpy as np
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.imperative.nn import FC
from paddle.fluid.dygraph.nn import FC
from test_imperative_base import new_program_scope
class MyLayer(fluid.imperative.Layer):
class MyLayer(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(MyLayer, self).__init__(name_scope)
@ -34,7 +34,7 @@ class MyLayer(fluid.imperative.Layer):
return [x]
class MyPyLayer(fluid.imperative.PyLayer):
class MyPyLayer(fluid.dygraph.PyLayer):
def __init__(self):
super(MyPyLayer, self).__init__()
@ -48,7 +48,7 @@ class MyPyLayer(fluid.imperative.PyLayer):
return np.array(dout) * (1 - np.square(np.array(out)))
class MLP(fluid.imperative.Layer):
class MLP(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(MLP, self).__init__(name_scope)
self._fc1 = FC(self.full_name(),
@ -71,7 +71,7 @@ class MLP(fluid.imperative.Layer):
return x
class SimpleRNNCell(fluid.imperative.Layer):
class SimpleRNNCell(fluid.dygraph.Layer):
def __init__(self, name_scope, step_input_size, hidden_size, output_size,
param_attr):
super(SimpleRNNCell, self).__init__(name_scope)
@ -159,7 +159,7 @@ class SimpleRNNCell(fluid.imperative.Layer):
return reduce_out, hidden
class SimpleRNN(fluid.imperative.Layer):
class SimpleRNN(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(SimpleRNN, self).__init__(name_scope)
self.seq_len = 4
@ -194,10 +194,10 @@ class SimpleRNN(fluid.imperative.Layer):
class TestImperative(unittest.TestCase):
def test_sum_op(self):
x = np.ones([2, 2], np.float32)
with fluid.imperative.guard():
with fluid.dygraph.guard():
inputs = []
for _ in range(10):
inputs.append(fluid.imperative.base.to_variable(x))
inputs.append(fluid.dygraph.base.to_variable(x))
ret = fluid.layers.sums(inputs)
loss = fluid.layers.reduce_sum(ret)
loss._backward()
@ -205,17 +205,17 @@ class TestImperative(unittest.TestCase):
self.assertTrue(np.allclose(inputs[0]._gradient(), x))
def test_layer(self):
with fluid.imperative.guard():
with fluid.dygraph.guard():
cl = core.Layer()
cl.forward([])
l = fluid.imperative.Layer("l")
l = fluid.dygraph.Layer("l")
self.assertRaises(NotImplementedError, l.forward, [])
def test_pylayer_func_id(self):
with fluid.imperative.guard():
with fluid.dygraph.guard():
class PyLayer1(fluid.imperative.PyLayer):
class PyLayer1(fluid.dygraph.PyLayer):
def __init__(self):
super(PyLayer1, self).__init__()
@ -227,7 +227,7 @@ class TestImperative(unittest.TestCase):
def backward(input):
return input
class PyLayer2(fluid.imperative.PyLayer):
class PyLayer2(fluid.dygraph.PyLayer):
def __init__(self):
super(PyLayer2, self).__init__()
@ -241,21 +241,21 @@ class TestImperative(unittest.TestCase):
py_layer_1 = PyLayer1()
py_layer_2 = PyLayer2()
py_layer_1(fluid.imperative.base.to_variable(np.ones([2, 2])))
py_layer_2(fluid.imperative.base.to_variable(np.ones([2, 2])))
py_layer_1(fluid.dygraph.base.to_variable(np.ones([2, 2])))
py_layer_2(fluid.dygraph.base.to_variable(np.ones([2, 2])))
id = py_layer_1.forward_id
self.assertGreater(id, 0)
self.assertEqual(py_layer_1.backward_id, id + 1)
self.assertEqual(py_layer_2.forward_id, id + 2)
self.assertEqual(py_layer_2.backward_id, id + 3)
py_layer_1(fluid.imperative.base.to_variable(np.ones([2, 2])))
py_layer_1(fluid.dygraph.base.to_variable(np.ones([2, 2])))
self.assertEqual(py_layer_1.forward_id, id)
def test_pylayer(self):
np_inp = np.ones([2, 2], np.float32)
with fluid.imperative.guard():
with fluid.dygraph.guard():
my_py_layer = MyPyLayer()
var_inp = fluid.imperative.base.to_variable(np_inp)
var_inp = fluid.dygraph.base.to_variable(np_inp)
outs = my_py_layer(var_inp)
dy_out = np.sum(outs[0]._numpy())
outs[0]._backward()
@ -282,8 +282,8 @@ class TestImperative(unittest.TestCase):
def test_layer_in_out(self):
np_inp = np.array([1.0, 2.0, -1.0], dtype=np.float32)
with fluid.imperative.guard():
var_inp = fluid.imperative.base.to_variable(np_inp)
with fluid.dygraph.guard():
var_inp = fluid.dygraph.base.to_variable(np_inp)
l = MyLayer("my_layer")
x = l(var_inp)[0]
self.assertIsNotNone(x)
@ -310,8 +310,8 @@ class TestImperative(unittest.TestCase):
def test_mlp(self):
np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
with fluid.imperative.guard():
var_inp = fluid.imperative.base.to_variable(np_inp)
with fluid.dygraph.guard():
var_inp = fluid.dygraph.base.to_variable(np_inp)
mlp = MLP("mlp")
out = mlp(var_inp)
dy_out = out._numpy()
@ -353,8 +353,8 @@ class TestImperative(unittest.TestCase):
[10.0, 11.0, 12.0]])
np_inp = np_inp.reshape((1, 4, 3))
np_inp = np_inp.astype(np.float32)
with fluid.imperative.guard():
var_inp = fluid.imperative.base.to_variable(np_inp)
with fluid.dygraph.guard():
var_inp = fluid.dygraph.base.to_variable(np_inp)
var_inp = fluid.layers.reshape(var_inp, shape=[1, 4, 3])
simple_rnn = SimpleRNN("simple_rnn")
outs, pre_hiddens = simple_rnn.forward(var_inp)

@ -18,11 +18,11 @@ import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.optimizer import SGDOptimizer
from paddle.fluid.imperative.nn import Conv2D, Pool2D, FC
from paddle.fluid.imperative.base import to_variable
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, FC
from paddle.fluid.dygraph.base import to_variable
class SimpleImgConvPool(fluid.imperative.Layer):
class SimpleImgConvPool(fluid.dygraph.Layer):
def __init__(self,
name_scope,
num_channels,
@ -71,7 +71,7 @@ class SimpleImgConvPool(fluid.imperative.Layer):
return x
class MNIST(fluid.imperative.Layer):
class MNIST(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(MNIST, self).__init__(name_scope)
@ -98,12 +98,12 @@ class MNIST(fluid.imperative.Layer):
return x
class TestImperativeCheckpoint(unittest.TestCase):
class TestDygraphCheckpoint(unittest.TestCase):
def save_load_persistables(self):
seed = 90
epoch_num = 1
with fluid.imperative.guard():
with fluid.dygraph.guard():
fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed
@ -135,14 +135,14 @@ class TestImperativeCheckpoint(unittest.TestCase):
avg_loss._backward()
sgd.minimize(avg_loss)
fluid.imperative.save_persistables(mnist, "save_dir")
fluid.dygraph.save_persistables(mnist, "save_dir")
mnist.clear_gradients()
for param in mnist.parameters():
dy_param_init_value[param.name] = param._numpy()
mnist.load_dict(
fluid.imperative.load_persistables(mnist, "save_dir"))
fluid.dygraph.load_persistables(mnist, "save_dir"))
restore = mnist.parameters()

@ -22,7 +22,7 @@ import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from test_imperative_base import new_program_scope
from paddle.fluid.imperative.base import to_variable
from paddle.fluid.dygraph.base import to_variable
# Can use Amusic dataset as the DeepCF describes.
DATA_PATH = os.environ.get('DATA_PATH', '')
@ -32,11 +32,11 @@ NUM_BATCHES = int(os.environ.get('NUM_BATCHES', 5))
NUM_EPOCHES = int(os.environ.get('NUM_EPOCHES', 1))
class DMF(fluid.imperative.Layer):
class DMF(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(DMF, self).__init__(name_scope)
self._user_latent = fluid.imperative.FC(self.full_name(), 256)
self._item_latent = fluid.imperative.FC(self.full_name(), 256)
self._user_latent = fluid.dygraph.FC(self.full_name(), 256)
self._item_latent = fluid.dygraph.FC(self.full_name(), 256)
self._user_layers = []
self._item_layers = []
@ -45,12 +45,12 @@ class DMF(fluid.imperative.Layer):
self._user_layers.append(
self.add_sublayer(
'user_layer_%d' % i,
fluid.imperative.FC(
fluid.dygraph.FC(
self.full_name(), self._hid_sizes[i], act='relu')))
self._item_layers.append(
self.add_sublayer(
'item_layer_%d' % i,
fluid.imperative.FC(
fluid.dygraph.FC(
self.full_name(), self._hid_sizes[i], act='relu')))
def forward(self, users, items):
@ -63,18 +63,18 @@ class DMF(fluid.imperative.Layer):
return fluid.layers.elementwise_mul(users, items)
class MLP(fluid.imperative.Layer):
class MLP(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(MLP, self).__init__(name_scope)
self._user_latent = fluid.imperative.FC(self.full_name(), 256)
self._item_latent = fluid.imperative.FC(self.full_name(), 256)
self._user_latent = fluid.dygraph.FC(self.full_name(), 256)
self._item_latent = fluid.dygraph.FC(self.full_name(), 256)
self._match_layers = []
self._hid_sizes = [128, 64]
for i in range(len(self._hid_sizes)):
self._match_layers.append(
self.add_sublayer(
'match_layer_%d' % i,
fluid.imperative.FC(
fluid.dygraph.FC(
self.full_name(), self._hid_sizes[i], act='relu')))
self._mat
@ -88,7 +88,7 @@ class MLP(fluid.imperative.Layer):
return match_vec
class DeepCF(fluid.imperative.Layer):
class DeepCF(fluid.dygraph.Layer):
def __init__(self, name_scope, num_users, num_items, matrix):
super(DeepCF, self).__init__(name_scope)
self._num_users = num_users
@ -103,7 +103,7 @@ class DeepCF(fluid.imperative.Layer):
self._mlp = MLP(self.full_name())
self._dmf = DMF(self.full_name())
self._match_fc = fluid.imperative.FC(self.full_name(), 1, act='sigmoid')
self._match_fc = fluid.dygraph.FC(self.full_name(), 1, act='sigmoid')
def forward(self, users, items):
# users_emb = self._user_emb(users)
@ -191,7 +191,7 @@ def load_data(DATA_PATH):
np.expand_dims(labels_np, -1), num_users, num_items, matrix
class TestImperativeDeepCF(unittest.TestCase):
class TestDygraphDeepCF(unittest.TestCase):
def test_deefcf(self):
seed = 90
if DATA_PATH:
@ -237,7 +237,7 @@ class TestImperativeDeepCF(unittest.TestCase):
fetch_list=[loss])[0]
sys.stderr.write('static loss %s\n' % static_loss)
with fluid.imperative.guard():
with fluid.dygraph.guard():
fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed

@ -22,12 +22,12 @@ import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.optimizer import SGDOptimizer
from paddle.fluid.imperative.nn import Conv2D, Pool2D, FC
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, FC
from test_imperative_base import new_program_scope
from paddle.fluid.imperative.base import to_variable
from paddle.fluid.dygraph.base import to_variable
class Discriminator(fluid.imperative.Layer):
class Discriminator(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(Discriminator, self).__init__(name_scope)
self._fc1 = FC(self.full_name(), size=32, act='elu')
@ -38,7 +38,7 @@ class Discriminator(fluid.imperative.Layer):
return self._fc2(x)
class Generator(fluid.imperative.Layer):
class Generator(fluid.dygraph.Layer):
def __init__(self, name_scope):
super(Generator, self).__init__(name_scope)
self._fc1 = FC(self.full_name(), size=64, act='elu')
@ -51,7 +51,7 @@ class Generator(fluid.imperative.Layer):
return self._fc3(x)
class TestImperativeGAN(unittest.TestCase):
class TestDygraphGAN(unittest.TestCase):
def test_gan_float32(self):
seed = 90
@ -130,7 +130,7 @@ class TestImperativeGAN(unittest.TestCase):
scope.find_var(param.name).get_tensor())
dy_params = dict()
with fluid.imperative.guard():
with fluid.dygraph.guard():
fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed

@ -22,16 +22,16 @@ import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.optimizer import AdamOptimizer
from paddle.fluid.imperative.nn import Conv2D, Pool2D, FC
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, FC
from test_imperative_base import new_program_scope
from paddle.fluid.imperative.base import to_variable
from paddle.fluid.dygraph.base import to_variable
def gen_data():
pass
class GraphConv(fluid.imperative.Layer):
class GraphConv(fluid.dygraph.Layer):
def __init__(self, name_scope, in_features, out_features):
super(GraphConv, self).__init__(name_scope)
@ -50,7 +50,7 @@ class GraphConv(fluid.imperative.Layer):
return fluid.layers.matmul(adj, support) + self.bias
class GCN(fluid.imperative.Layer):
class GCN(fluid.dygraph.Layer):
def __init__(self, name_scope, num_hidden):
super(GCN, self).__init__(name_scope)
self.gc = GraphConv(self.full_name(), num_hidden, 32)
@ -61,7 +61,7 @@ class GCN(fluid.imperative.Layer):
return self.gc2(x, adj)
class TestImperativeGNN(unittest.TestCase):
class TestDygraphGNN(unittest.TestCase):
def test_gnn_float32(self):
seed = 90
@ -115,7 +115,7 @@ class TestImperativeGNN(unittest.TestCase):
static_weight = np.array(
scope.find_var(model.gc.weight.name).get_tensor())
with fluid.imperative.guard():
with fluid.dygraph.guard():
fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save