From 0dc485e6fe32248179a86db203d3a06c8f4d528c Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Fri, 7 Aug 2020 19:15:18 +0800 Subject: [PATCH] =?UTF-8?q?refine=20the=20value=20parameter's=20Tensor=20s?= =?UTF-8?q?upport=20of=20fill=5Fconstant=20Op=20test=3D=E2=80=A6=20(#25986?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/fluid/layers/tensor.py | 7 +++- .../tests/unittests/test_fill_constant_op.py | 41 +++++++++++++++++-- python/paddle/tensor/creation.py | 2 +- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index e33b34cc925..2d874b4806c 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -685,8 +685,9 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): """ attrs = {'force_cpu': force_cpu} + dtype = convert_dtype(dtype) if not isinstance(value, Variable): - if convert_dtype(dtype) in ['int64', 'int32']: + if dtype in ['int64', 'int32']: attrs['str_value'] = str(int(value)) else: attrs['str_value'] = str(float(value)) @@ -697,7 +698,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): out = _varbase_creator(dtype=dtype) if isinstance(value, Variable): - if convert_dtype(dtype) in ['int64', 'int32']: + if dtype in ['int64', 'int32']: attrs['str_value'] = str(int(value.numpy())) else: attrs['str_value'] = str(float(value.numpy())) @@ -712,6 +713,8 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): helper = LayerHelper("fill_constant", **locals()) inputs = {} if isinstance(value, Variable): + if convert_dtype(value.dtype) != dtype: + value = cast(value, dtype) inputs['ValueTensor'] = value check_dtype(dtype, 'dtype', diff --git a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py index 3eb761f925a..aefc809bd5c 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -269,18 +269,26 @@ class TestFillConstantAPI(unittest.TestCase): out_6 = fluid.layers.fill_constant( shape=shape_tensor_int64, dtype=np.float32, value=1.1) - val = fluid.layers.fill_constant(shape=[1], dtype=np.float32, value=1.1) + val1 = fluid.layers.fill_constant( + shape=[1], dtype=np.float32, value=1.1) + val2 = fluid.layers.fill_constant( + shape=[1], dtype=np.float64, value=1.1) out_7 = fluid.layers.fill_constant( - shape=shape_tensor_int64, dtype=np.float32, value=val) + shape=shape_tensor_int64, dtype=np.float32, value=val1) + + out_8 = fluid.layers.fill_constant( + shape=shape_tensor_int64, dtype=np.float32, value=val2) exe = fluid.Executor(place=fluid.CPUPlace()) - res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( + res_1, res_2, res_3, res_4, res_5, res_6, res_7, res_8 = exe.run( fluid.default_main_program(), feed={ "shape_tensor_int32": np.array([1, 2]).astype("int32"), "shape_tensor_int64": np.array([1, 2]).astype("int64"), }, - fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7]) + fetch_list=[ + out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8 + ]) assert np.array_equal(res_1, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_2, np.full([1, 2], 1.1, dtype="float32")) @@ -289,6 +297,31 @@ class TestFillConstantAPI(unittest.TestCase): assert np.array_equal(res_5, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_6, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_7, np.full([1, 2], 1.1, dtype="float32")) + assert np.array_equal(res_8, np.full([1, 2], 1.1, dtype="float32")) + + +class TestFillConstantImperative(unittest.TestCase): + def test_api(self): + with fluid.dygraph.guard(): + data1 = np.array([1, 2]).astype('int32') + data2 = np.array([1.1]).astype('float32') + shape = fluid.dygraph.to_variable(data1) + val = fluid.dygraph.to_variable(data2) + res1 = fluid.layers.fill_constant( + shape=[1, 2], dtype='float32', value=1.1) + res2 = fluid.layers.fill_constant( + shape=shape, dtype='float32', value=1.1) + res3 = fluid.layers.fill_constant( + shape=shape, dtype='float32', value=val) + assert np.array_equal( + res1.numpy(), np.full( + [1, 2], 1.1, dtype="float32")) + assert np.array_equal( + res2.numpy(), np.full( + [1, 2], 1.1, dtype="float32")) + assert np.array_equal( + res3.numpy(), np.full( + [1, 2], 1.1, dtype="float32")) class TestFillConstantOpError(unittest.TestCase): diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 8b07bddfc15..02c908be347 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -248,7 +248,7 @@ def zeros(shape, dtype=None, name=None): # shape is a Tensor shape = paddle.fill_constant(shape=[2], dtype='int32', value=2) - data3 = paddle.ones(shape=shape, dtype='int32') + data3 = paddle.zeros(shape=shape, dtype='int32') # [[0 0] # [0 0]] """ -- GitLab