You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensorshape_test.cc 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. namespace common = mindspore::common;
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::MsLogLevel::INFO;
  30. class MindDataTestTensorShape : public UT::Common {
  31. public:
  32. MindDataTestTensorShape() = default;
  33. };
  34. TEST_F(MindDataTestTensorShape, TestBasics) {
  35. std::vector<dsize_t> vec = {4, 5, 6};
  36. TensorShape t(vec);
  37. ASSERT_EQ(t.Rank(), 3);
  38. ASSERT_EQ(t.Size(), 3);
  39. ASSERT_EQ(t.known(), true);
  40. ASSERT_EQ(t.empty(), false);
  41. ASSERT_EQ(t.NumOfElements(), 120);
  42. for (dsize_t i = 0; i < t.Rank(); i++) {
  43. ASSERT_EQ(t[i], vec[i]);
  44. }
  45. ASSERT_EQ(vec, t.AsVector());
  46. ASSERT_EQ(t.IsValidIndex({0, 0, 0}), true);
  47. ASSERT_EQ(t.IsValidIndex({3, 4, 5}), true);
  48. ASSERT_EQ(t.IsValidIndex({3, 4, 6}), false);
  49. ASSERT_EQ(t.IsValidIndex({4, 5, 6}), false);
  50. ASSERT_EQ(t.IsValidIndex({4, 5, 6}), false);
  51. ASSERT_EQ(t.IsValidIndex({3, 3}), false);
  52. ASSERT_EQ(t.IsValidIndex({-3, -3, -1}), false);
  53. ASSERT_EQ(t.IsValidIndex({-1, 4, 5}), false);
  54. TensorShape t2({4, 5, 6});
  55. ASSERT_EQ(t, t2);
  56. TensorShape t3({0});
  57. ASSERT_EQ(t3.Size(), 1);
  58. ASSERT_EQ(t3.NumOfElements(), 0);
  59. t3 = TensorShape({0, 5, 6});
  60. ASSERT_EQ(t3.Size(), 3);
  61. ASSERT_EQ(t3.NumOfElements(), 0);
  62. }
  63. TEST_F(MindDataTestTensorShape, TestScalars) {
  64. TensorShape t = TensorShape::CreateScalar();
  65. ASSERT_EQ(t.Rank(), 0);
  66. ASSERT_EQ(t.AsVector(), std::vector<dsize_t>{});
  67. ASSERT_EQ(t.known(), true);
  68. TensorShape t2(std::vector<dsize_t>{});
  69. ASSERT_EQ(t, t2);
  70. ASSERT_EQ(t.NumOfElements(), 1);
  71. }
  72. TEST_F(MindDataTestTensorShape, TestDims) {
  73. TensorShape t = TensorShape::CreateScalar();
  74. t = t.AppendDim(1);
  75. t = t.AppendDim(2);
  76. t = t.AppendDim(3);
  77. ASSERT_EQ(t, TensorShape({1, 2, 3}));
  78. TensorShape t2 = TensorShape::CreateScalar();
  79. t2 = t2.PrependDim(3);
  80. t2 = t2.PrependDim(2);
  81. t2 = t2.PrependDim(1);
  82. ASSERT_EQ(t, t2);
  83. TensorShape t3({4, 5, 6});
  84. t3 = t3.InsertDim(0, 1); // 1, 4, 5, 6
  85. t3 = t3.InsertDim(2, 2); // 1, 4, 2, 5, 6
  86. t3 = t3.InsertDim(4, 3); // 1, 4, 2, 5, 3, 6
  87. ASSERT_EQ(t3, TensorShape({1, 4, 2, 5, 3, 6}));
  88. }
  89. TEST_F(MindDataTestTensorShape, TestUnknown) {
  90. TensorShape t1({-1, 5, 6});
  91. ASSERT_EQ(t1.AsVector(), std::vector<dsize_t>({-1, 5, 6}));
  92. ASSERT_EQ(t1.known(), false);
  93. TensorShape t2({5, 6});
  94. t2 = t2.PrependDim(-1);
  95. ASSERT_EQ(t1, t2);
  96. TensorShape t3 = TensorShape::CreateUnknownRankShape();
  97. ASSERT_EQ(t3.known(), false);
  98. ASSERT_EQ(t3.Size(), 0);
  99. TensorShape t4 = TensorShape::CreateUnknownShapeWithRank(3);
  100. ASSERT_EQ(t4, TensorShape({-1, -1, -1}));
  101. }
  102. // Test materializing a TensorShape by calling method on a given column descriptor
  103. TEST_F(MindDataTestTensorShape, TestColDescriptor) {
  104. int32_t rank = 0; // not used
  105. int32_t num_elements = 0;
  106. // Has no shape
  107. ColDescriptor c1("col1", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank);
  108. TensorShape generated_shape1 = TensorShape::CreateUnknownRankShape();
  109. num_elements = 4;
  110. Status rc = c1.MaterializeTensorShape(num_elements, &generated_shape1);
  111. ASSERT_TRUE(rc.IsOk());
  112. MS_LOG(INFO) << "generated_shape1: " << common::SafeCStr(generated_shape1.ToString()) << ".";
  113. ASSERT_EQ(TensorShape({4}), generated_shape1);
  114. // Has shape <DIM_UNKNOWN> i.e. <*>
  115. TensorShape requested_shape2({TensorShape::kDimUnknown});
  116. ColDescriptor c2("col2", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape2);
  117. TensorShape generated_shape2 = TensorShape::CreateUnknownRankShape();
  118. num_elements = 5;
  119. rc = c2.MaterializeTensorShape(num_elements, &generated_shape2);
  120. ASSERT_TRUE(rc.IsOk());
  121. MS_LOG(INFO) << "generated_shape2: " << common::SafeCStr(generated_shape2.ToString()) << ".";
  122. ASSERT_EQ(TensorShape({5}), generated_shape2);
  123. // Compute unknown dimension <*,4>
  124. TensorShape requested_shape3({TensorShape::kDimUnknown, 4});
  125. ColDescriptor c3("col3", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape3);
  126. TensorShape generated_shape3 = TensorShape::CreateUnknownRankShape();
  127. num_elements = 12;
  128. rc = c3.MaterializeTensorShape(num_elements, &generated_shape3);
  129. ASSERT_TRUE(rc.IsOk());
  130. MS_LOG(INFO) << "generated_shape3: " << common::SafeCStr(generated_shape3.ToString()) << ".";
  131. ASSERT_EQ(TensorShape({3, 4}), generated_shape3);
  132. // Compute unknown dimension <3,*,4>
  133. TensorShape requested_shape4({3, TensorShape::kDimUnknown, 4});
  134. ColDescriptor c4("col4", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape4);
  135. TensorShape generated_shape4 = TensorShape::CreateUnknownRankShape();
  136. num_elements = 24;
  137. rc = c4.MaterializeTensorShape(num_elements, &generated_shape4);
  138. ASSERT_TRUE(rc.IsOk());
  139. MS_LOG(INFO) << "generated_shape4: " << common::SafeCStr(generated_shape4.ToString()) << ".";
  140. ASSERT_EQ(TensorShape({3, 2, 4}), generated_shape4);
  141. // requested and generated should be the same! <2,3,4>
  142. TensorShape requested_shape5({2, 3, 4});
  143. ColDescriptor c5("col5", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape5);
  144. TensorShape generated_shape5 = TensorShape::CreateUnknownRankShape();
  145. num_elements = 24;
  146. rc = c5.MaterializeTensorShape(num_elements, &generated_shape5);
  147. ASSERT_TRUE(rc.IsOk());
  148. MS_LOG(INFO) << "generated_shape5: " << common::SafeCStr(generated_shape5.ToString()) << ".";
  149. ASSERT_EQ(requested_shape5, generated_shape5);
  150. // expect fail due to multiple unknown dimensions
  151. TensorShape requested_shape6({2, TensorShape::kDimUnknown, TensorShape::kDimUnknown});
  152. ColDescriptor c6("col6", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape6);
  153. TensorShape generated_shape6 = TensorShape::CreateUnknownRankShape();
  154. num_elements = 24;
  155. rc = c6.MaterializeTensorShape(num_elements, &generated_shape6);
  156. ASSERT_FALSE(rc.IsOk());
  157. // expect fail because the requested shape element count does not match with num elements
  158. TensorShape requested_shape7({2, 3, 3});
  159. ColDescriptor c7("col7", DataType(DataType::DE_INT8), TensorImpl::kFlexible, rank, &requested_shape7);
  160. TensorShape generated_shape7 = TensorShape::CreateUnknownRankShape();
  161. num_elements = 24;
  162. rc = c7.MaterializeTensorShape(num_elements, &generated_shape7);
  163. ASSERT_FALSE(rc.IsOk());
  164. }
  165. TEST_F(MindDataTestTensorShape, TestInvalid) {
  166. ASSERT_EQ(TensorShape({kDeMaxDim - 1, kDeMaxDim - 1, kDeMaxDim - 1}), TensorShape::CreateUnknownRankShape());
  167. }