1 /** 2 * Copyright 2019 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <string> 17 #include "./securec.h" 18 #include "minddata/dataset/core/data_type.h" 19 #include "minddata/dataset/core/tensor_shape.h" 20 #include "minddata/dataset/engine/data_schema.h" 21 #include "common/common.h" 22 #include "utils/ms_utils.h" 23 #include "gtest/gtest.h" 24 #include "utils/log_adapter.h" 25 26 namespace common = mindspore::common; 27 28 using namespace mindspore::dataset; 29 using mindspore::LogStream; 30 using mindspore::ExceptionType::NoExceptionType; 31 using mindspore::MsLogLevel::INFO; 32 33 class MindDataTestTensorShape : public UT::Common { 34 public: 35 MindDataTestTensorShape() = default; 36 }; 37 38 TEST_F(MindDataTestTensorShape, TestBasics) { 39 std::vector<dsize_t> vec = {4, 5, 6}; 40 TensorShape t(vec); 41 ASSERT_EQ(t.Rank(), 3); 42 ASSERT_EQ(t.Size(), 3); 43 ASSERT_EQ(t.known(), true); 44 ASSERT_EQ(t.empty(), false); 45 ASSERT_EQ(t.NumOfElements(), 120); 46 for (dsize_t i = 0; i < t.Rank(); i++) { 47 ASSERT_EQ(t[i], vec[i]); 48 } 49 ASSERT_EQ(vec, t.AsVector()); 50 ASSERT_EQ(t.IsValidIndex({0, 0, 0}), true); 51 ASSERT_EQ(t.IsValidIndex({3, 4, 5}), true); 52 ASSERT_EQ(t.IsValidIndex({3, 4, 6}), false); 53 ASSERT_EQ(t.IsValidIndex({4, 5, 6}), false); 54 ASSERT_EQ(t.IsValidIndex({4, 5, 6}), false); 55 ASSERT_EQ(t.IsValidIndex({3, 3}), false); 56 ASSERT_EQ(t.IsValidIndex({-3, -3, -1}), false); 57 ASSERT_EQ(t.IsValidIndex({-1, 4, 5}), false); 58 TensorShape t2({4, 5, 6}); 59 ASSERT_EQ(t, t2); 60 TensorShape t3({0}); 61 ASSERT_EQ(t3.Size(), 1); 62 ASSERT_EQ(t3.NumOfElements(), 0); 63 t3 = TensorShape({0, 5, 6}); 64 ASSERT_EQ(t3.Size(), 3); 65 ASSERT_EQ(t3.NumOfElements(), 0); 66 } 67 68 TEST_F(MindDataTestTensorShape, TestScalars) { 69 TensorShape t = TensorShape::CreateScalar(); 70 ASSERT_EQ(t.Rank(), 0); 71 ASSERT_EQ(t.AsVector(), std::vector<dsize_t>{}); 72 ASSERT_EQ(t.known(), true); 73 TensorShape t2(std::vector<dsize_t>{}); 74 ASSERT_EQ(t, t2); 75 ASSERT_EQ(t.NumOfElements(), 1); 76 } 77 78 TEST_F(MindDataTestTensorShape, TestDims) { 79 TensorShape t = TensorShape::CreateScalar(); 80 t = t.AppendDim(1); 81 t = t.AppendDim(2); 82 t = t.AppendDim(3); 83 ASSERT_EQ(t, TensorShape({1, 2, 3})); 84 TensorShape t2 = TensorShape::CreateScalar(); 85 t2 = t2.PrependDim(3); 86 t2 = t2.PrependDim(2); 87 t2 = t2.PrependDim(1); 88 ASSERT_EQ(t, t2); 89 TensorShape t3({4, 5, 6}); 90 t3 = t3.InsertDim(0, 1); // 1, 4, 5, 6 91 t3 = t3.InsertDim(2, 2); // 1, 4, 2, 5, 6 92 t3 = t3.InsertDim(4, 3); // 1, 4, 2, 5, 3, 6 93 ASSERT_EQ(t3, TensorShape({1, 4, 2, 5, 3, 6})); 94 } 95 96 TEST_F(MindDataTestTensorShape, TestUnknown) { 97 TensorShape t1({-1, 5, 6}); 98 ASSERT_EQ(t1.AsVector(), std::vector<dsize_t>({-1, 5, 6})); 99 ASSERT_EQ(t1.known(), false); 100 TensorShape t2({5, 6}); 101 t2 = t2.PrependDim(-1); 102 ASSERT_EQ(t1, t2); 103 TensorShape t3 = TensorShape::CreateUnknownRankShape(); 104 ASSERT_EQ(t3.known(), false); 105 ASSERT_EQ(t3.Size(), 0); 106 TensorShape t4 = TensorShape::CreateUnknownShapeWithRank(3); 107 ASSERT_EQ(t4, TensorShape({-1, -1, -1})); 108 } 109 110 // Test materializing a TensorShape by calling method on a given column descriptor 111 TEST_F(MindDataTestTensorShape, TestColDescriptor) { 112 int32_t rank = 0; // not used 113 int32_t num_elements = 0; 114 115 // Has no shape 116 ColDescriptor c1("col1", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank); 117 TensorShape generated_shape1 = TensorShape::CreateUnknownRankShape(); 118 num_elements = 4; 119 Status rc = c1.MaterializeTensorShape(num_elements, &generated_shape1); 120 ASSERT_TRUE(rc.IsOk()); 121 MS_LOG(INFO) << "generated_shape1: " << common::SafeCStr(generated_shape1.ToString()) << "."; 122 ASSERT_EQ(TensorShape({4}), generated_shape1); 123 124 // Has shape <DIM_UNKNOWN> i.e. <*> 125 TensorShape requested_shape2({TensorShape::kDimUnknown}); 126 ColDescriptor c2("col2", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape2); 127 TensorShape generated_shape2 = TensorShape::CreateUnknownRankShape(); 128 num_elements = 5; 129 rc = c2.MaterializeTensorShape(num_elements, &generated_shape2); 130 ASSERT_TRUE(rc.IsOk()); 131 MS_LOG(INFO) << "generated_shape2: " << common::SafeCStr(generated_shape2.ToString()) << "."; 132 ASSERT_EQ(TensorShape({5}), generated_shape2); 133 134 // Compute unknown dimension <*,4> 135 TensorShape requested_shape3({TensorShape::kDimUnknown, 4}); 136 ColDescriptor c3("col3", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape3); 137 TensorShape generated_shape3 = TensorShape::CreateUnknownRankShape(); 138 num_elements = 12; 139 rc = c3.MaterializeTensorShape(num_elements, &generated_shape3); 140 ASSERT_TRUE(rc.IsOk()); 141 MS_LOG(INFO) << "generated_shape3: " << common::SafeCStr(generated_shape3.ToString()) << "."; 142 ASSERT_EQ(TensorShape({3, 4}), generated_shape3); 143 144 // Compute unknown dimension <3,*,4> 145 TensorShape requested_shape4({3, TensorShape::kDimUnknown, 4}); 146 ColDescriptor c4("col4", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape4); 147 TensorShape generated_shape4 = TensorShape::CreateUnknownRankShape(); 148 num_elements = 24; 149 rc = c4.MaterializeTensorShape(num_elements, &generated_shape4); 150 ASSERT_TRUE(rc.IsOk()); 151 MS_LOG(INFO) << "generated_shape4: " << common::SafeCStr(generated_shape4.ToString()) << "."; 152 ASSERT_EQ(TensorShape({3, 2, 4}), generated_shape4); 153 154 // requested and generated should be the same! <2,3,4> 155 TensorShape requested_shape5({2, 3, 4}); 156 ColDescriptor c5("col5", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape5); 157 TensorShape generated_shape5 = TensorShape::CreateUnknownRankShape(); 158 num_elements = 24; 159 rc = c5.MaterializeTensorShape(num_elements, &generated_shape5); 160 ASSERT_TRUE(rc.IsOk()); 161 MS_LOG(INFO) << "generated_shape5: " << common::SafeCStr(generated_shape5.ToString()) << "."; 162 ASSERT_EQ(requested_shape5, generated_shape5); 163 164 // expect fail due to multiple unknown dimensions 165 TensorShape requested_shape6({2, TensorShape::kDimUnknown, TensorShape::kDimUnknown}); 166 ColDescriptor c6("col6", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape6); 167 TensorShape generated_shape6 = TensorShape::CreateUnknownRankShape(); 168 num_elements = 24; 169 rc = c6.MaterializeTensorShape(num_elements, &generated_shape6); 170 ASSERT_FALSE(rc.IsOk()); 171 172 // expect fail because the requested shape element count does not match with num elements 173 TensorShape requested_shape7({2, 3, 3}); 174 ColDescriptor c7("col7", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape7); 175 TensorShape generated_shape7 = TensorShape::CreateUnknownRankShape(); 176 num_elements = 24; 177 rc = c7.MaterializeTensorShape(num_elements, &generated_shape7); 178 ASSERT_FALSE(rc.IsOk()); 179 } 180 181 TEST_F(MindDataTestTensorShape, TestInvalid) { 182 ASSERT_EQ(TensorShape({kDeMaxDim - 1, kDeMaxDim - 1, kDeMaxDim - 1}), TensorShape::CreateUnknownRankShape()); 183 } 184