From 6b5cff5462fd7c37d0da57510585a847f67ae7f4 Mon Sep 17 00:00:00 2001 From: 0x45f <23097963+0x45f@users.noreply.github.com> Date: Sat, 2 Apr 2022 15:48:08 +0800 Subject: [PATCH] Add UT for full_like after migration YAML (#41290) * Add UT for full_like after migration YAML * rename test class --- .../tests/unittests/test_full_like_op.py | 41 +++++++++++++++++++ python/paddle/tensor/creation.py | 5 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_full_like_op.py b/python/paddle/fluid/tests/unittests/test_full_like_op.py index 3ae2e9ff6bd..05a310a9c50 100644 --- a/python/paddle/fluid/tests/unittests/test_full_like_op.py +++ b/python/paddle/fluid/tests/unittests/test_full_like_op.py @@ -21,6 +21,7 @@ import paddle.compat as cpt import unittest import numpy as np from op_test import OpTest +from paddle.fluid.framework import convert_np_dtype_to_dtype_ class TestFullOp(unittest.TestCase): @@ -92,5 +93,45 @@ class TestFullOpError(unittest.TestCase): dtype='uint4') +class TestFullLikeOp1(OpTest): + # test basic + def setUp(self): + self.op_type = "fill_any_like" + self.python_api = paddle.full_like + self.init_data() + + x = np.zeros(self.shape) + out = np.full_like(x, self.fill_value, self.dtype) + + self.inputs = {'X': x} + self.outputs = {'Out': out} + self.attrs = { + 'value': self.fill_value, + 'dtype': convert_np_dtype_to_dtype_(self.dtype) + } + + def init_data(self): + self.fill_value = 5 + self.shape = [10, 10] + self.dtype = np.float32 + + def test_check_output(self): + self.check_output(check_eager=True) + + +class TestFullLikeOp2(TestFullLikeOp1): + def init_data(self): + self.fill_value = 1000 + self.shape = [1024, 1024] + self.dtype = np.float64 + + +class TestFullLikeOp3(TestFullLikeOp1): + def init_data(self): + self.fill_value = 8888 + self.shape = [5000, 5000] + self.dtype = np.int64 + + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 6e7e5678be0..ca16995f84d 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -224,7 +224,10 @@ def full_like(x, fill_value, dtype=None, name=None): if not isinstance(dtype, core.VarDesc.VarType): dtype = convert_np_dtype_to_dtype_(dtype) - if paddle.in_dynamic_mode(): + if in_dygraph_mode(): + return _C_ops.final_state_full_like(x, fill_value, dtype, x.place) + + if _in_legacy_dygraph(): return _C_ops.fill_any_like(x, 'value', fill_value, 'dtype', dtype) helper = LayerHelper("full_like", **locals()) -- GitLab