From 9396f2868c3560d8f96aa03a31bce4a916ac1822 Mon Sep 17 00:00:00 2001 From: Aganlengzi Date: Fri, 12 Nov 2021 10:28:02 +0800 Subject: [PATCH] [NPU] fix fill_constant and test_memcpy_op_npu (#37144) --- paddle/fluid/operators/fill_constant_op.cc | 6 +++++- .../npu/test_fill_constant_op_npu.py | 21 +++++++++++++++++++ .../tests/unittests/npu/test_memcpy_op_npu.py | 4 ++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/operators/fill_constant_op.cc b/paddle/fluid/operators/fill_constant_op.cc index aea149fbedc..480a08bfb7e 100644 --- a/paddle/fluid/operators/fill_constant_op.cc +++ b/paddle/fluid/operators/fill_constant_op.cc @@ -87,6 +87,9 @@ class FillConstantOp : public framework::OperatorWithKernel { case 3: kt.place_ = platform::XPUPlace(); break; + case 4: + kt.place_ = platform::NPUPlace(); + break; default: PADDLE_THROW(platform::errors::Unimplemented( "Could NOT determine the place of variable, place_type = %d .", @@ -164,7 +167,8 @@ class FillConstantOpMaker : public framework::OpProtoAndCheckerMaker { "0: CPUPlace. " "1: CUDAPlace. " "2: CUDAPinnedPlace. " - "3: XPUPlace. ") + "3: XPUPlace. " + "4: NPUPlace. ") .SetDefault(-1); AddOutput("Out", "(Tensor) Tensor of specified shape will be filled " diff --git a/python/paddle/fluid/tests/unittests/npu/test_fill_constant_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_fill_constant_op_npu.py index a3e781c990e..152a4548055 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_fill_constant_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_fill_constant_op_npu.py @@ -144,5 +144,26 @@ class TestFillConstantBool(OpTest): self.check_output_with_place(self.place) +class TestFillConstantWithPlaceType(OpTest): + def setUp(self): + self.set_npu() + self.place = paddle.NPUPlace(0) + self.op_type = "fill_constant" + self.init_dtype() + + self.inputs = {} + self.attrs = {'shape': [123, 92], 'value': 3.8, 'place_type': 4} + self.outputs = {'Out': np.full((123, 92), 3.8)} + + def set_npu(self): + self.__class__.use_npu = True + + def init_dtype(self): + self.dtype = np.float32 + + def test_check_output(self): + self.check_output_with_place(self.place) + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/npu/test_memcpy_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_memcpy_op_npu.py index a421e266734..6500a8c8cd8 100755 --- a/python/paddle/fluid/tests/unittests/npu/test_memcpy_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_memcpy_op_npu.py @@ -54,7 +54,7 @@ class TestMemcpy_FillConstant(unittest.TestCase): "shape": [10, 10], "dtype": npu_var.dtype, "value": 1.0, - "place_type": 1 + "place_type": 4 }) main_program.global_block().append_op( type="fill_constant", @@ -63,7 +63,7 @@ class TestMemcpy_FillConstant(unittest.TestCase): "shape": [10, 10], "dtype": cpu_var.dtype, "value": 0.0, - "place_type": 2 + "place_type": 0 }) return main_program, npu_var, cpu_var -- GitLab