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