From bd777e0710ba51c66d1f37e9f73f60e405015a72 Mon Sep 17 00:00:00 2001 From: l00591931 Date: Mon, 15 Mar 2021 19:22:50 +0800 Subject: [PATCH] throw exception when tensor has zero in shape --- mindspore/core/ir/tensor.cc | 43 +++++++++++++++++++----- mindspore/core/ir/tensor.h | 2 ++ tests/st/ops/cpu/test_dropout_grad_op.py | 1 + tests/st/ops/gpu/test_add_op.py | 9 +++++ tests/st/pynative/test_tensor_index.py | 2 +- tests/ut/python/ir/test_tensor.py | 11 ++++++ tests/ut/python/ops/test_ops.py | 3 +- tests/ut/python/ops/test_tensor_slice.py | 4 +-- 8 files changed, 63 insertions(+), 12 deletions(-) diff --git a/mindspore/core/ir/tensor.cc b/mindspore/core/ir/tensor.cc index 5c9578496b..5f5b6d0e31 100644 --- a/mindspore/core/ir/tensor.cc +++ b/mindspore/core/ir/tensor.cc @@ -465,7 +465,9 @@ Tensor::Tensor(const Tensor &tensor) cache_tensor_ptr_(tensor.cache_tensor_ptr_), hashmap_tensor_ptr_(tensor.hashmap_tensor_ptr_), padding_type_(tensor.padding_type()), - device_event_(tensor.device_event_) {} + device_event_(tensor.device_event_) { + CheckShape(tensor.shape_); +} Tensor::Tensor(const Tensor &tensor, TypeId data_type) : MetaTensor(data_type, tensor.shape_), @@ -479,29 +481,43 @@ Tensor::Tensor(const Tensor &tensor, TypeId data_type) cache_tensor_ptr_(tensor.cache_tensor_ptr_), hashmap_tensor_ptr_(tensor.hashmap_tensor_ptr_), padding_type_(tensor.padding_type()), - device_event_(tensor.device_event_) {} + device_event_(tensor.device_event_) { + CheckShape(tensor.shape_); +} Tensor::Tensor(TypeId data_type, const ShapeVector &shape, TensorDataPtr data) - : MetaTensor(data_type, shape), data_(std::move(data)), id_(MakeId()) {} + : MetaTensor(data_type, shape), data_(std::move(data)), id_(MakeId()) { + CheckShape(shape); +} Tensor::Tensor(TypeId data_type, const ShapeVector &shape) - : Tensor(data_type, shape, MakeTensorData(data_type, shape)) {} + : Tensor(data_type, shape, MakeTensorData(data_type, shape)) { + CheckShape(shape); +} Tensor::Tensor(TypeId data_type, const ShapeVector &shape, void *data, size_t data_len) - : Tensor(data_type, shape, MakeTensorData(data_type, shape, data, data_len)) {} + : Tensor(data_type, shape, MakeTensorData(data_type, shape, data, data_len)) { + CheckShape(shape); +} Tensor::Tensor(TypeId data_type, const ShapeVector &shape, void *data, TypeId src_data_type) - : Tensor(data_type, shape, MakeTensorData(data_type, shape, data, src_data_type)) {} + : Tensor(data_type, shape, MakeTensorData(data_type, shape, data, src_data_type)) { + CheckShape(shape); +} Tensor::Tensor(const std::vector &input, const TypePtr &data_type) : MetaTensor(TypeIdOf(data_type, kNumberTypeInt32), {static_cast(input.size())}), data_(MakeTensorData(data_type_, shape_, input.data(), input.size())), - id_(MakeId()) {} + id_(MakeId()) { + CheckShape(shape_); +} Tensor::Tensor(const std::vector &input, const TypePtr &data_type) : MetaTensor(TypeIdOf(data_type, kNumberTypeFloat32), {static_cast(input.size())}), data_(MakeTensorData(data_type_, shape_, input.data(), input.size())), - id_(MakeId()) {} + id_(MakeId()) { + CheckShape(shape_); +} Tensor::Tensor(int64_t input, const TypePtr &data_type) : MetaTensor(TypeIdOf(data_type, kNumberTypeInt32), {}), @@ -606,6 +622,17 @@ std::string Tensor::ToStringRepr() const { return buf.str(); } +void Tensor::CheckShape(const ShapeVector &shape) const { + // Check tensor's shape, ignore one-dimensional tensor, including empty tensor with shape=(0,). + if (shape.size() > 1) { + for (const auto &s : shape) { + if (s == 0) { + MS_EXCEPTION(ValueError) << "Zero is not supported in the shape of Tensor. "; + } + } + } +} + void Tensor::data_sync(bool need_wait) const { if (need_wait) { Wait(); diff --git a/mindspore/core/ir/tensor.h b/mindspore/core/ir/tensor.h index c8c1a60d79..713cfed024 100644 --- a/mindspore/core/ir/tensor.h +++ b/mindspore/core/ir/tensor.h @@ -280,6 +280,8 @@ class Tensor : public MetaTensor { std::string ToStringRepr() const; + void CheckShape(const ShapeVector &shape) const; + bool is_init() const { return init_flag_; } void set_init_flag(bool flag) { init_flag_ = flag; } diff --git a/tests/st/ops/cpu/test_dropout_grad_op.py b/tests/st/ops/cpu/test_dropout_grad_op.py index c500c0d852..c7963727ef 100644 --- a/tests/st/ops/cpu/test_dropout_grad_op.py +++ b/tests/st/ops/cpu/test_dropout_grad_op.py @@ -107,6 +107,7 @@ def test_dropout_grad_004(): assert np.all(np.abs(diff) < error) +@pytest.mark.skip(reason='0 in shape is not support') @pytest.mark.level0 @pytest.mark.platform_x86_cpu @pytest.mark.env_onecard diff --git a/tests/st/ops/gpu/test_add_op.py b/tests/st/ops/gpu/test_add_op.py index 23259728e4..7b87626859 100644 --- a/tests/st/ops/gpu/test_add_op.py +++ b/tests/st/ops/gpu/test_add_op.py @@ -127,30 +127,39 @@ def add(nptype): assert (output[2].asnumpy() == expect2).all() assert (output[3].asnumpy() == expect3).all() + +@pytest.mark.skip(reason='0 in shape is not support') @pytest.mark.level0 @pytest.mark.platform_x86_gpu_training @pytest.mark.env_onecard def test_add_float64(): add(np.float64) + +@pytest.mark.skip(reason='0 in shape is not support') @pytest.mark.level0 @pytest.mark.platform_x86_gpu_training @pytest.mark.env_onecard def test_add_float32(): add(np.float32) + +@pytest.mark.skip(reason='0 in shape is not support') @pytest.mark.level0 @pytest.mark.platform_x86_gpu_training @pytest.mark.env_onecard def test_add_float16(): add(np.float16) + +@pytest.mark.skip(reason='0 in shape is not support') @pytest.mark.level0 @pytest.mark.platform_x86_gpu_training @pytest.mark.env_onecard def test_add_int64(): add(np.int64) +@pytest.mark.skip(reason='0 in shape is not support') @pytest.mark.level0 @pytest.mark.platform_x86_gpu_training @pytest.mark.env_onecard diff --git a/tests/st/pynative/test_tensor_index.py b/tests/st/pynative/test_tensor_index.py index 81b987511b..8221fa54ef 100644 --- a/tests/st/pynative/test_tensor_index.py +++ b/tests/st/pynative/test_tensor_index.py @@ -787,7 +787,7 @@ def test_tensor_assign_exception(): tck = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32) # Error for A[Slice] = Number # 1. A[Slice] = Number, Slice error - with pytest.raises(IndexError): + with pytest.raises(ValueError): net_e2(t, 2) # Error for A[Slice] = U, U is a Tensor diff --git a/tests/ut/python/ir/test_tensor.py b/tests/ut/python/ir/test_tensor.py index 9ed92b418d..e3025539ce 100644 --- a/tests/ut/python/ir/test_tensor.py +++ b/tests/ut/python/ir/test_tensor.py @@ -67,6 +67,17 @@ def test_tensor(): assert isinstance(t4, ms.Tensor) assert t4.dtype == ms.int64 +def test_tensor_empty(): + t = ms.Tensor(np.ones(0), ms.float32) + assert isinstance(t, ms.Tensor) + assert t.shape == (0,) + + +def test_tensor_shape_has_zero(): + with pytest.raises(ValueError): + t = ms.Tensor(np.ones((1, 0)), ms.float32) + print(t) + def test_tensor_type_float16(): t_float16 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float16)) diff --git a/tests/ut/python/ops/test_ops.py b/tests/ut/python/ops/test_ops.py index 750c76877c..9d93b40b58 100755 --- a/tests/ut/python/ops/test_ops.py +++ b/tests/ut/python/ops/test_ops.py @@ -14,6 +14,7 @@ # ============================================================================ """ test ops """ import functools +import pytest import numpy as np @@ -890,7 +891,7 @@ class StridedSliceNet(nn.Cell): out_3 = self.strided_slice_3(x, self.begins, self.ends, self.strides) + self.const_3 return out_0, out_1, out_2, out_3 - +@pytest.mark.skip(reason='0 in shape is not support') def test_strided_slice_const(): class StridedSLiceConstNet(nn.Cell): """StridedSLiceConstNet net definition""" diff --git a/tests/ut/python/ops/test_tensor_slice.py b/tests/ut/python/ops/test_tensor_slice.py index 9f3f4528ae..cecaf659aa 100644 --- a/tests/ut/python/ops/test_tensor_slice.py +++ b/tests/ut/python/ops/test_tensor_slice.py @@ -464,8 +464,8 @@ def test_tensor_assign(): net(Ta, b, Tck) net2(t, b, tck) # Error for A[Slice] = Number - # 1. A[Slice] = Number, Slice error - with pytest.raises(IndexError): + # 1. A[Slice] = Number, 0 in shape + with pytest.raises(ValueError): net_e2(t, Tensor(2, mstype.int32)) # Error for A[Slice] = U, U is a Tensor