diff --git a/mindspore/ccsrc/minddata/dataset/core/tensor.cc b/mindspore/ccsrc/minddata/dataset/core/tensor.cc index 842615f9e1..30a5b707a3 100644 --- a/mindspore/ccsrc/minddata/dataset/core/tensor.cc +++ b/mindspore/ccsrc/minddata/dataset/core/tensor.cc @@ -268,6 +268,10 @@ Status Tensor::CreateTensor(std::shared_ptr *ptr, py::array arr) { std::shared_ptr global_pool = GlobalContext::Instance()->mem_pool(); (*ptr)->data_allocator_ = std::make_unique>(global_pool); int64_t byte_size = (*ptr)->SizeInBytes(); + if (byte_size == 0) { + return Status::OK(); + } + RETURN_IF_NOT_OK((*ptr)->AllocateBuffer(byte_size)); unsigned char *data = static_cast(arr.request().ptr); diff --git a/tests/ut/python/dataset/test_dataset_numpy_slices.py b/tests/ut/python/dataset/test_dataset_numpy_slices.py index 791a567408..7306ef8d06 100644 --- a/tests/ut/python/dataset/test_dataset_numpy_slices.py +++ b/tests/ut/python/dataset/test_dataset_numpy_slices.py @@ -82,6 +82,16 @@ def test_numpy_slices_dict_1(): assert data[0] == res[i][0] assert data[1] == res[i][1] +def test_numpy_slices_dict_2(): + logger.info("Test Dictionary empty data.") + + np_data = {"a": [[]], "b": [[4]]} + ds = de.NumpySlicesDataset(np_data, shuffle=False) + res = [[], [4]] + + for _, data in enumerate(ds): + np.testing.assert_array_almost_equal(data[0], np.array(res[0])) + np.testing.assert_array_almost_equal(data[1], np.array(res[1])) def test_numpy_slices_tuple_1(): logger.info("Test slicing a list of tuple.")