From ea22fdb0aecfc51258c04a73ab801783b7e163d9 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 17 Mar 2023 10:36:54 +0800 Subject: [PATCH] support fetch empty tensor on CPUPlace (#51735) * support fetch empty tensor on CPUPlace * fix the shape in unittest of empty output --- paddle/fluid/operators/controlflow/fetch_v2_op.cc | 6 ++---- .../standalone_executor/test_standalone_executor.py | 13 +++++++++++++ .../fluid/tests/unittests/test_matrix_nms_op.py | 6 +++++- .../fluid/tests/unittests/test_multiclass_nms_op.py | 12 +++++++++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/paddle/fluid/operators/controlflow/fetch_v2_op.cc b/paddle/fluid/operators/controlflow/fetch_v2_op.cc index 5a99dd695c0..81d23939f5a 100644 --- a/paddle/fluid/operators/controlflow/fetch_v2_op.cc +++ b/paddle/fluid/operators/controlflow/fetch_v2_op.cc @@ -34,7 +34,7 @@ namespace operators { static void DeepCopy(const phi::DenseTensor &src_item, const std::string &fetch_var_name, phi::DenseTensor *dst_item) { - if (src_item.IsInitialized() && src_item.numel() > 0) { + if (src_item.IsInitialized()) { #ifdef PADDLE_WITH_MKLDNN // Conversion from MKL-DNN to Paddle if (src_item.layout() == phi::DataLayout::ONEDNN) { @@ -58,9 +58,7 @@ static void DeepCopy(const phi::DenseTensor &src_item, paddle::framework::TensorCopySync(src_item, platform::CPUPlace(), dst_item); #endif } else { - // Not copy, if the src tensor is empty. - dst_item->clear(); - dst_item->Resize({0}); + VLOG(4) << "No copy"; } dst_item->set_lod(src_item.lod()); } diff --git a/python/paddle/fluid/tests/unittests/standalone_executor/test_standalone_executor.py b/python/paddle/fluid/tests/unittests/standalone_executor/test_standalone_executor.py index cfce6784ad5..86060d6bf2d 100644 --- a/python/paddle/fluid/tests/unittests/standalone_executor/test_standalone_executor.py +++ b/python/paddle/fluid/tests/unittests/standalone_executor/test_standalone_executor.py @@ -378,6 +378,19 @@ class TestException(unittest.TestCase): ) +class TestFetchEmptyTensor(unittest.TestCase): + def test_fetch(self): + places = [paddle.CPUPlace()] + if paddle.fluid.core.is_compiled_with_cuda(): + places.append(paddle.CUDAPlace(0)) + for place in places: + with paddle.static.program_guard(paddle.static.Program()): + out = paddle.empty([3, 0]) + exe = paddle.static.Executor(place) + res = exe.run(fetch_list=[out]) + self.assertEqual(res[0].shape, (3, 0)) + + class TestInplaceApiWithDataTransform(unittest.TestCase): def test_increment(self): if paddle.fluid.core.is_compiled_with_cuda(): diff --git a/python/paddle/fluid/tests/unittests/test_matrix_nms_op.py b/python/paddle/fluid/tests/unittests/test_matrix_nms_op.py index 0d60e0edeaa..24ac8253d43 100644 --- a/python/paddle/fluid/tests/unittests/test_matrix_nms_op.py +++ b/python/paddle/fluid/tests/unittests/test_matrix_nms_op.py @@ -270,7 +270,11 @@ class TestMatrixNMSOp(OpTest): ) empty = len(det_outs) == 0 - det_outs = np.array([], dtype=np.float32) if empty else det_outs + det_outs = ( + np.array([], dtype=np.float32).reshape([0, BOX_SIZE + 2]) + if empty + else det_outs + ) index_outs = np.array([], dtype=np.float32) if empty else index_outs nmsed_outs = det_outs.astype('float32') diff --git a/python/paddle/fluid/tests/unittests/test_multiclass_nms_op.py b/python/paddle/fluid/tests/unittests/test_multiclass_nms_op.py index 44fe9939688..f6a1bac1a63 100644 --- a/python/paddle/fluid/tests/unittests/test_multiclass_nms_op.py +++ b/python/paddle/fluid/tests/unittests/test_multiclass_nms_op.py @@ -626,7 +626,9 @@ class TestMulticlassNMS2Op(TestMulticlassNMSOp): det_outs = np.array(det_outs) nmsed_outs = ( - det_outs[:, :-1].astype('float32') if len(det_outs) else det_outs + det_outs[:, :-1].astype('float32') + if len(det_outs) + else np.array([], dtype=np.float32).reshape([0, BOX_SIZE + 2]) ) index_outs = ( det_outs[:, -1:].astype('int') if len(det_outs) else det_outs @@ -696,7 +698,9 @@ class TestMulticlassNMS2LoDInput(TestMulticlassNMSLoDInput): det_outs = np.array(det_outs) nmsed_outs = ( - det_outs[:, :-1].astype('float32') if len(det_outs) else det_outs + det_outs[:, :-1].astype('float32') + if len(det_outs) + else np.array([], dtype=np.float32).reshape([0, BOX_SIZE + 2]) ) index_outs = ( det_outs[:, -1:].astype('int') if len(det_outs) else det_outs @@ -768,7 +772,9 @@ class TestMulticlassNMS3Op(TestMulticlassNMS2Op): det_outs = np.array(det_outs) nmsed_outs = ( - det_outs[:, :-1].astype('float32') if len(det_outs) else det_outs + det_outs[:, :-1].astype('float32') + if len(det_outs) + else np.array([], dtype=np.float32).reshape([0, BOX_SIZE + 2]) ) index_outs = ( det_outs[:, -1:].astype('int') if len(det_outs) else det_outs -- GitLab