diff --git a/paddle/fluid/inference/tensorrt/convert/activation_op.cc b/paddle/fluid/inference/tensorrt/convert/activation_op.cc index 18de448690534656cdfe851c74a2b390264b1b6b..44349bab0372ad27e85c47a5e15af4f283f8ec46 100644 --- a/paddle/fluid/inference/tensorrt/convert/activation_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/activation_op.cc @@ -49,9 +49,10 @@ class ActivationOpConverter : public OpConverter { layer->setAlpha(0.); layer->setBeta(6.); } + g #endif - auto output_name = op_desc.Output("Out")[0]; + auto output_name = op_desc.Output("Out")[0]; RreplenishLayerAndOutput(layer, op_type_, {output_name}, test_mode); if (op_desc.HasAttr("out_scale")) { diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 20cae1bd946bf6a65b1d9743384d7d50e73b7c36..26e2316e514a4fa305e24ebfae86fd8d4e3ba719 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -56,7 +56,7 @@ from .tensor.creation import zeros_like #DEFINE_ALIAS # from .tensor.creation import eye #DEFINE_ALIAS from .tensor.creation import full #DEFINE_ALIAS # from .tensor.creation import linspace #DEFINE_ALIAS -# from .tensor.creation import full_like #DEFINE_ALIAS +from .tensor.creation import full_like #DEFINE_ALIAS # from .tensor.creation import triu #DEFINE_ALIAS # from .tensor.creation import tril #DEFINE_ALIAS # from .tensor.creation import meshgrid #DEFINE_ALIAS diff --git a/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py b/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py index 68eb5d479388e59845068a3fc4d6de3f855e9da8..b9b203048377b924d910684243b43b7b78f8828c 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py @@ -17,6 +17,7 @@ from __future__ import print_function import paddle import paddle.fluid as fluid import paddle.fluid.core as core +from paddle.fluid import Program, program_guard import paddle.compat as cpt import unittest import numpy as np @@ -96,6 +97,60 @@ class TestFillAnyLikeOpFloat16(TestFillAnyLikeOp): self.dtype = np.float16 +class TestFillAnyLikeOp_attr_out(unittest.TestCase): + """ Test fill_any_like op(whose API is full_like) for attr out. """ + + def test_attr_tensor_API(self): + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + fill_value = 2.0 + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.full_like(input, fill_value) + + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + self.assertTrue( + not (out_np - np.full_like(img, fill_value)).any(), + msg="full_like output is wrong, out = " + str(out_np)) + + +class TestFillAnyLikeOpError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + #for ci coverage + + input_data = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.full_like(input_data, 2.0) + + def test_input_dtype(): + paddle.full_like + + self.assertRaises( + ValueError, + paddle.full_like, + input=input_data, + fill_value=2, + dtype='uint4') + self.assertRaises( + TypeError, + paddle.full_like, + input=input_data, + fill_value=2, + dtype='int16') + + class ApiOnesLikeTest(unittest.TestCase): def test_out(self): with fluid.program_guard(fluid.Program()): diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 237d8450bdbe843961ef9e49cbb2a965d7588463..99b5453435fcc2ad52df631fb41cf137d762e4a2 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -31,7 +31,7 @@ from .creation import linspace #DEFINE_ALIAS # from .creation import eye #DEFINE_ALIAS from .creation import full # DEFINE_ALIAS # from .creation import linspace #DEFINE_ALIAS -# from .creation import full_like #DEFINE_ALIAS +from .creation import full_like #DEFINE_ALIAS from .creation import triu #DEFINE_ALIAS from .creation import tril #DEFINE_ALIAS # from .creation import meshgrid #DEFINE_ALIAS diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 739f7f789d8cd75aebb7dc11c4e098a39c9abd09..fc4fe296d08dc6d1c9a5ee4ebb58ab6d2b420a94 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -12,10 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function +from ..fluid.framework import Variable +from ..fluid.initializer import Constant +from ..fluid.layers import core +from ..fluid.layer_helper import LayerHelper +from ..fluid.data_feeder import check_variable_and_dtype, check_type, check_dtype, convert_dtype +from ..fluid.framework import convert_np_dtype_to_dtype_, in_dygraph_mode, _varbase_creator, device_guard, OpProtoHolder +from ..fluid.layers import fill_constant from paddle.common_ops_import import * # TODO: define functions to get create a tensor - __all__ = [ 'create_tensor', # 'create_lod_tensor', @@ -33,13 +40,64 @@ __all__ = [ # 'arrange', # 'eye', 'full', - # 'full_like', + 'full_like', 'triu', 'tril', - # 'meshgrid' + # 'meshgrid', ] +def full_like(input, + fill_value, + out=None, + dtype=None, + device=None, + stop_gradient=True, + name=None): + """ + **full_like** + This function creates a tensor filled with `fill_value` which has identical shape and dtype + with `input`. + Args: + input(Variable): The input tensor which specifies shape and dtype. + fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0. + out(Variable): The output tensor. + Returns: + out(Variable): The tensor variable storing the output. + Examples: + .. code-block:: python + import paddle + import paddle.fluid as fluid + import numpy as np + + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.full_like(input, 2.0) + exe = fluid.Executor(fluid.CPUPlace()) + exe.run(fluid.default_startup_program()) + img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output]) + print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] + """ + helper = LayerHelper("full_like", **locals()) + + if dtype is None: + dtype = 'float32' + + check_dtype(dtype, 'dtype', + ['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like') + + if out is None: + out = helper.create_variable_for_type_inference(dtype=dtype) + helper.append_op( + type='fill_any_like', + inputs={'X': [input]}, + attrs={'value': fill_value}, + outputs={'Out': [out]}) + out.stop_gradient = stop_gradient + + return out + + def linspace(start, stop, num, dtype, out=None, device=None, name=None): """ This OP return fixed number of evenly spaced values within a given interval.