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.4 kB

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