diff --git a/paddle/fluid/pybind/op_function_generator.h b/paddle/fluid/pybind/op_function_generator.h index bc84863d7d60782d5b3648eb53f5c9df16295999..972e8aafab758bdc04f5dd527c2b7ce3f1585e52 100644 --- a/paddle/fluid/pybind/op_function_generator.h +++ b/paddle/fluid/pybind/op_function_generator.h @@ -125,6 +125,11 @@ std::map> op_ins_map = { {"X", "Scale", "Bias", "Mean", "Variance", "MomentumTensor"}}, {"inplace_abn", {"X", "Scale", "Bias", "Mean", "Variance", "MomentumTensor"}}, + {"linear_interp", {"X", "OutSize"}}, + {"bilinear_interp", {"X", "OutSize"}}, + {"trilinear_interp", {"X", "OutSize"}}, + {"nearest_interp", {"X", "OutSize"}}, + {"bicubic_interp", {"X", "OutSize"}}, }; // NOTE(zhiqiu): Like op_ins_map. @@ -270,6 +275,7 @@ std::map> op_passing_outs_map = { {"split", {"Out"}}, {"concat", {"Out"}}, {"fused_multi_transformer", {"CacheKVOut"}}, + {"group_norm", {"Mean", "Variance"}}, }; // NOTE(pangyoki): Tensor View Strategy. diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index a3310f1a46ce44d39b10ceed5a8827eea2fdb287..4d985097088f852d064344f49d0d6decb22f81f4 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -3016,9 +3016,15 @@ class GroupNorm(layers.Layer): is_bias=True) def forward(self, input): - if in_dygraph_mode(): + mean_out = self._helper.create_variable_for_type_inference( + dtype=self._dtype, stop_gradient=True) + variance_out = self._helper.create_variable_for_type_inference( + dtype=self._dtype, stop_gradient=True) + + if _non_static_mode(): attrs = ('epsilon', self._epsilon, 'groups', self._groups) - out, _, _ = _C_ops.group_norm(input, self.weight, self.bias, *attrs) + out, _, _ = _C_ops.group_norm(input, self.weight, self.bias, + mean_out, variance_out, *attrs) return dygraph_utils._append_activation_in_dygraph(out, self._act) else: @@ -3029,10 +3035,6 @@ class GroupNorm(layers.Layer): inputs['Scale'] = self.weight # create output - mean_out = self._helper.create_variable_for_type_inference( - dtype=self._dtype, stop_gradient=True) - variance_out = self._helper.create_variable_for_type_inference( - dtype=self._dtype, stop_gradient=True) group_norm_out = self._helper.create_variable_for_type_inference( dtype=self._dtype) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 757b1a2da95b99ce90faf82fa3f4f9eeef98ab39..bd453b3ddaa0050e664015d016c0c709e8687036 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -3600,6 +3600,10 @@ class Block(object): attrs = kwargs.get("attrs", {}) inplace_map = kwargs.get("inplace_map", None) type = kwargs.get("type", None) + warnings.warn( + "Op `%s` is executed through `append_op` under the dynamic mode, " + "the corresponding API implementation needs to be upgraded to " + "using `_C_ops` method." % type, DeprecationWarning) op = Operator( block=self, desc=None, diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 97506ead5fad4db942208eacf680db34a2554fb6..3391654f93117be82d68888909867e8566a689fd 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -7793,10 +7793,18 @@ def image_resize(input, } if out_shape is not None: - if isinstance(out_shape, Variable): + if isinstance(out_shape, Variable) and not _non_static_mode(): out_shape.stop_gradient = True inputs['OutSize'] = out_shape else: + if _non_static_mode(): + if isinstance(out_shape, Variable): + out_shape = list(out_shape.numpy()) + else: + out_shape = list(out_shape) + for i, dim in enumerate(out_shape): + if isinstance(dim, Variable): + out_shape[i] = dim.numpy()[0] if not (_is_list_or_turple_(out_shape)): raise TypeError( "out_shape should be a list or tuple or Variable.") @@ -7863,7 +7871,9 @@ def image_resize(input, attrs['out_w'] = out_shape[2] else: - if isinstance(scale, Variable): + if _non_static_mode() and isinstance(scale, Variable): + scale = scale.numpy() + elif isinstance(scale, Variable): scale.stop_gradient = True inputs["Scale"] = scale elif isinstance(scale, float) or isinstance(scale, int): @@ -7883,6 +7893,26 @@ def image_resize(input, inputs["OutSize"] = actual_shape elif actual_shape is not None: raise TypeError("actual_shape should either be Variable or None.") + + if _non_static_mode(): + attr_list = [] + for k, v in attrs.items(): + attr_list.append(k) + attr_list.append(v) + dy_attr = tuple(attr_list) + + if resample_type == "linear": + out = _C_ops.linear_interp(input, actual_shape, *dy_attr) + elif resample_type == "bilinear": + out = _C_ops.bilinear_interp(input, actual_shape, *dy_attr) + elif resample_type == "trilinear": + out = _C_ops.trilinear_interp(input, actual_shape, *dy_attr) + elif resample_type == "nearest": + out = _C_ops.nearest_interp(input, actual_shape, *dy_attr) + elif resample_type == "bicubic": + out = _C_ops.bicubic_interp(input, actual_shape, *dy_attr) + return out + out = helper.create_variable_for_type_inference(dtype) helper.append_op( type='{}_interp'.format(resample_type), diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index b02c154584e9c8dca6f070de1b1c06346c296c02..3b1fcc15ab95fb933ce149c837a2d7c20ca42874 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -681,14 +681,19 @@ def assign(input, output=None): "saving it to file and 'load_op' to load it") if output is None: output = helper.create_variable_for_type_inference(dtype=dtype) - helper.append_op( - type='assign_value', - outputs={'Out': [output]}, - attrs={ - 'dtype': dtype, - 'shape': list(input.shape), - value_name: values - }) + if _non_static_mode(): + _C_ops.assign_value(output, 'shape', + list(input.shape), 'dtype', dtype, value_name, + values) + else: + helper.append_op( + type='assign_value', + outputs={'Out': [output]}, + attrs={ + 'dtype': dtype, + 'shape': list(input.shape), + value_name: values + }) if is_inplace and _non_static_mode(): output._bump_inplace_version() diff --git a/python/paddle/nn/layer/norm.py b/python/paddle/nn/layer/norm.py index 7c3e3ad8dee9f66bffe07249e17a64cc4d8fa513..6cdfc36d5d61f1e51ff5452964b54bf9595c82bb 100644 --- a/python/paddle/nn/layer/norm.py +++ b/python/paddle/nn/layer/norm.py @@ -32,7 +32,7 @@ import six from ...fluid.dygraph import BatchNorm # noqa: F401 from ...fluid.dygraph import SpectralNorm # noqa: F401 -from ...framework import get_default_dtype, set_default_dtype +from ...framework import get_default_dtype, set_default_dtype, _non_static_mode from ..initializer import Constant from ...framework import ParamAttr @@ -404,6 +404,25 @@ class GroupNorm(Layer): self.bias.stop_gradient = self._bias_attr != None and self._bias_attr.learning_rate == 0. def forward(self, input): + mean_out = self._helper.create_variable_for_type_inference( + dtype=input.dtype, stop_gradient=True) + variance_out = self._helper.create_variable_for_type_inference( + dtype=input.dtype, stop_gradient=True) + + if _non_static_mode(): + pre_act, _, _ = _C_ops.group_norm( + input, + self.weight, + self.bias, + mean_out, + variance_out, + 'epsilon', + self._epsilon, + 'groups', + self._num_groups, ) + return dygraph_utils._append_activation_in_dygraph( + pre_act, act=None) + inputs = {'X': input} if self.bias is not None: inputs['Bias'] = self.bias @@ -411,10 +430,6 @@ class GroupNorm(Layer): inputs['Scale'] = self.weight # create output - mean_out = self._helper.create_variable_for_type_inference( - dtype=input.dtype, stop_gradient=True) - variance_out = self._helper.create_variable_for_type_inference( - dtype=input.dtype, stop_gradient=True) group_norm_out = self._helper.create_variable_for_type_inference( dtype=input.dtype) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index d3430ba81b8599ff6c696656f49a5a0a6cc8408b..e37ca981f851c5f686890f1ab4233af8eb3992db 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1568,14 +1568,19 @@ def assign(x, output=None): if output is None: output = helper.create_variable_for_type_inference( dtype=input.dtype) - helper.append_op( - type='assign_value', - outputs={'Out': [output]}, - attrs={ - 'dtype': dtype, - 'shape': list(input.shape), - value_name: values - }) + if _non_static_mode(): + _C_ops.assign_value(output, 'shape', + list(input.shape), 'dtype', dtype, value_name, + values) + else: + helper.append_op( + type='assign_value', + outputs={'Out': [output]}, + attrs={ + 'dtype': dtype, + 'shape': list(input.shape), + value_name: values + }) if is_inplace and _in_legacy_dygraph(): output._bump_inplace_version()