diff --git a/paddle/fluid/operators/controlflow/fetch_v2_op.cc b/paddle/fluid/operators/controlflow/fetch_v2_op.cc index 5a99dd695c02ba41a2dd3357d4caf0daa47adb23..81d23939f5a259460b07b57a5d2f1597d717c175 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 cfce6784ad52705e95a9042b94078bc9f2a70118..86060d6bf2de12797f1728d9abd77d38488639f9 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 0d60e0edeaabf60873b3d2de767a3bf0d76e6982..24ac8253d4384acbb980e4cb13023c89b4b564fa 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 44fe9939688b4bcc50bb600b8f0336220d3305ae..f6a1bac1a63563a186f5720aecf5908c6d7da2b5 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