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.

test_ops_strided_slice.cc 8.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Copyright 2020 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 <vector>
  17. #include <memory>
  18. #include "common/common_test.h"
  19. #include "ops/strided_slice.h"
  20. #include "ir/dtype/type.h"
  21. #include "ir/value.h"
  22. #include "abstract/dshape.h"
  23. #include "utils/tensor_construct_utils.h"
  24. namespace mindspore {
  25. namespace ops {
  26. namespace {
  27. template <typename T>
  28. void SetTensorData(void *data, std::vector<T> num) {
  29. MS_EXCEPTION_IF_NULL(data);
  30. auto tensor_data = reinterpret_cast<T *>(data);
  31. MS_EXCEPTION_IF_NULL(tensor_data);
  32. for (size_t index = 0; index < num.size(); ++index) {
  33. *tensor_data = num[index];
  34. }
  35. }
  36. } // namespace
  37. class TestStridedSlice : public UT::Common {
  38. public:
  39. TestStridedSlice() {}
  40. void SetUp() {}
  41. void TearDown() {}
  42. };
  43. TEST_F(TestStridedSlice, test_ops_stridedslice1) {
  44. auto stridedslice = std::make_shared<StridedSlice>();
  45. stridedslice->Init(0, 0, 0, 0, 0);
  46. EXPECT_EQ(stridedslice->get_begin_mask(), 0);
  47. EXPECT_EQ(stridedslice->get_end_mask(), 0);
  48. EXPECT_EQ(stridedslice->get_ellipsis_mask(), 0);
  49. EXPECT_EQ(stridedslice->get_new_axis_mask(), 0);
  50. EXPECT_EQ(stridedslice->get_shrink_axis_mask(), 0);
  51. auto tensor_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3, 3, 3});
  52. auto begin = MakeValue(std::vector<int64_t>{1, 0, 0});
  53. auto end = MakeValue(std::vector<int64_t>{2, 1, 3});
  54. auto strides = MakeValue(std::vector<int64_t>{1, 1, 1});
  55. MS_EXCEPTION_IF_NULL(tensor_x);
  56. MS_EXCEPTION_IF_NULL(begin);
  57. MS_EXCEPTION_IF_NULL(end);
  58. MS_EXCEPTION_IF_NULL(strides);
  59. auto abstract =
  60. stridedslice->Infer({tensor_x->ToAbstract(), begin->ToAbstract(), end->ToAbstract(), strides->ToAbstract()});
  61. MS_EXCEPTION_IF_NULL(abstract);
  62. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  63. auto shape_ptr = abstract->BuildShape();
  64. MS_EXCEPTION_IF_NULL(shape_ptr);
  65. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  66. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  67. MS_EXCEPTION_IF_NULL(shape);
  68. auto shape_vec = shape->shape();
  69. auto type = abstract->BuildType();
  70. MS_EXCEPTION_IF_NULL(type);
  71. EXPECT_EQ(type->isa<TensorType>(), true);
  72. auto tensor_type = type->cast<TensorTypePtr>();
  73. MS_EXCEPTION_IF_NULL(tensor_type);
  74. auto data_type = tensor_type->element();
  75. MS_EXCEPTION_IF_NULL(data_type);
  76. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  77. EXPECT_EQ(shape_vec.size(), 3);
  78. EXPECT_EQ(shape_vec[0], 1);
  79. EXPECT_EQ(shape_vec[1], 1);
  80. EXPECT_EQ(shape_vec[2], 3);
  81. }
  82. /*
  83. TEST_F(TestStridedSlice, test_ops_stridedslice2) {
  84. auto stridedslice = std::make_shared<StridedSlice>();
  85. stridedslice->Init(0, 0, 0, 0, 0);
  86. EXPECT_EQ(stridedslice->get_begin_mask(), 0);
  87. EXPECT_EQ(stridedslice->get_end_mask(), 0);
  88. EXPECT_EQ(stridedslice->get_ellipsis_mask(), 0);
  89. EXPECT_EQ(stridedslice->get_new_axis_mask(), 0);
  90. EXPECT_EQ(stridedslice->get_shrink_axis_mask(), 0);
  91. auto tensor_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3,3,3});
  92. auto begin = MakeValue(std::vector<int64_t>{1,0,0});
  93. auto end = MakeValue(std::vector<int64_t>{2,2,3});
  94. auto strides =MakeValue(std::vector<int64_t>{1,1,1});
  95. MS_EXCEPTION_IF_NULL(tensor_x);
  96. MS_EXCEPTION_IF_NULL(begin);
  97. MS_EXCEPTION_IF_NULL(end);
  98. MS_EXCEPTION_IF_NULL(strides);
  99. auto abstract =
  100. stridedslice->Infer({tensor_x->ToAbstract(),begin->ToAbstract(),end->ToAbstract(),strides->ToAbstract()});
  101. MS_EXCEPTION_IF_NULL(abstract);
  102. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  103. auto shape_ptr = abstract->BuildShape();
  104. MS_EXCEPTION_IF_NULL(shape_ptr);
  105. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  106. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  107. MS_EXCEPTION_IF_NULL(shape);
  108. auto shape_vec = shape->shape();
  109. auto type = abstract->BuildType();
  110. MS_EXCEPTION_IF_NULL(type);
  111. EXPECT_EQ(type->isa<TensorType>(), true);
  112. auto tensor_type = type->cast<TensorTypePtr>();
  113. MS_EXCEPTION_IF_NULL(tensor_type);
  114. auto data_type = tensor_type->element();
  115. MS_EXCEPTION_IF_NULL(data_type);
  116. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  117. EXPECT_EQ(shape_vec.size(), 3);
  118. EXPECT_EQ(shape_vec[0], 1);
  119. EXPECT_EQ(shape_vec[1], 2);
  120. EXPECT_EQ(shape_vec[2], 3);
  121. }
  122. TEST_F(TestStridedSlice, test_ops_stridedslice3) {
  123. auto stridedslice = std::make_shared<StridedSlice>();
  124. stridedslice->Init(0, 0, 0, 0, 0);
  125. EXPECT_EQ(stridedslice->get_begin_mask(), 0);
  126. EXPECT_EQ(stridedslice->get_end_mask(), 0);
  127. EXPECT_EQ(stridedslice->get_ellipsis_mask(), 0);
  128. EXPECT_EQ(stridedslice->get_new_axis_mask(), 0);
  129. EXPECT_EQ(stridedslice->get_shrink_axis_mask(), 0);
  130. auto tensor_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3,3,3});
  131. auto begin = MakeValue(std::vector<int64_t>{1,0,0});
  132. auto end = MakeValue(std::vector<int64_t>{2,-3,3});
  133. auto strides =MakeValue(std::vector<int64_t>{1,-1,1});
  134. MS_EXCEPTION_IF_NULL(tensor_x);
  135. MS_EXCEPTION_IF_NULL(begin);
  136. MS_EXCEPTION_IF_NULL(end);
  137. MS_EXCEPTION_IF_NULL(strides);
  138. auto abstract =
  139. stridedslice->Infer({tensor_x->ToAbstract(),begin->ToAbstract(),end->ToAbstract(),strides->ToAbstract()});
  140. MS_EXCEPTION_IF_NULL(abstract);
  141. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  142. auto shape_ptr = abstract->BuildShape();
  143. MS_EXCEPTION_IF_NULL(shape_ptr);
  144. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  145. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  146. MS_EXCEPTION_IF_NULL(shape);
  147. auto shape_vec = shape->shape();
  148. auto type = abstract->BuildType();
  149. MS_EXCEPTION_IF_NULL(type);
  150. EXPECT_EQ(type->isa<TensorType>(), true);
  151. auto tensor_type = type->cast<TensorTypePtr>();
  152. MS_EXCEPTION_IF_NULL(tensor_type);
  153. auto data_type = tensor_type->element();
  154. MS_EXCEPTION_IF_NULL(data_type);
  155. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  156. EXPECT_EQ(shape_vec.size(), 3);
  157. EXPECT_EQ(shape_vec[0], 1);
  158. EXPECT_EQ(shape_vec[1], 2);
  159. EXPECT_EQ(shape_vec[2], 3);
  160. }
  161. TEST_F(TestStridedSlice, test_ops_stridedslice4) {
  162. auto stridedslice = std::make_shared<StridedSlice>();
  163. stridedslice->Init(0, 0, 0, 0, 0);
  164. EXPECT_EQ(stridedslice->get_begin_mask(), 0);
  165. EXPECT_EQ(stridedslice->get_end_mask(), 0);
  166. EXPECT_EQ(stridedslice->get_ellipsis_mask(), 0);
  167. EXPECT_EQ(stridedslice->get_new_axis_mask(), 0);
  168. EXPECT_EQ(stridedslice->get_shrink_axis_mask(), 0);
  169. auto tensor_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{5});
  170. auto begin = MakeValue(std::vector<int64_t>{1});
  171. auto end = MakeValue(std::vector<int64_t>{-2});
  172. auto strides =MakeValue(std::vector<int64_t>{1});
  173. MS_EXCEPTION_IF_NULL(tensor_x);
  174. MS_EXCEPTION_IF_NULL(begin);
  175. MS_EXCEPTION_IF_NULL(end);
  176. MS_EXCEPTION_IF_NULL(strides);
  177. auto abstract =
  178. stridedslice->Infer({tensor_x->ToAbstract(),begin->ToAbstract(),end->ToAbstract(),strides->ToAbstract()});
  179. MS_EXCEPTION_IF_NULL(abstract);
  180. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  181. auto shape_ptr = abstract->BuildShape();
  182. MS_EXCEPTION_IF_NULL(shape_ptr);
  183. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  184. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  185. MS_EXCEPTION_IF_NULL(shape);
  186. auto shape_vec = shape->shape();
  187. auto type = abstract->BuildType();
  188. MS_EXCEPTION_IF_NULL(type);
  189. EXPECT_EQ(type->isa<TensorType>(), true);
  190. auto tensor_type = type->cast<TensorTypePtr>();
  191. MS_EXCEPTION_IF_NULL(tensor_type);
  192. auto data_type = tensor_type->element();
  193. MS_EXCEPTION_IF_NULL(data_type);
  194. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  195. EXPECT_EQ(shape_vec.size(), 1);
  196. EXPECT_EQ(shape_vec[0], 2);
  197. }*/
  198. } // namespace ops
  199. } // namespace mindspore