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 "common/common.h"
17 #include "minddata/dataset/kernels/data/slice_op.h"
18 #include "utils/log_adapter.h"
19
20 using namespace mindspore::dataset;
21 using mindspore::LogStream;
22 using mindspore::ExceptionType::NoExceptionType;
23 using mindspore::MsLogLevel::INFO;
24
25 class MindDataTestSliceOp : public UT::Common {
26 protected:
MindDataTestSliceOp()27 MindDataTestSliceOp() {}
28 };
29
TEST_F(MindDataTestSliceOp,TestOpBasic)30 TEST_F(MindDataTestSliceOp, TestOpBasic) {
31 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBasic.";
32 std::vector<uint64_t> labels = {1, 1, 3, 2};
33 std::shared_ptr<Tensor> input;
34 Tensor::CreateFromVector(labels, &input);
35
36 std::shared_ptr<Tensor> output;
37 Slice slice = Slice(1, 3);
38 std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(slice)));
39 Status s = op->Compute(input, &output);
40
41 std::vector<uint64_t> out = {1, 3};
42 std::shared_ptr<Tensor> expected;
43 Tensor::CreateFromVector(out, &expected);
44
45 EXPECT_TRUE(s.IsOk());
46
47 ASSERT_TRUE(output->shape() == expected->shape());
48 ASSERT_TRUE(output->type() == expected->type());
49
50 MS_LOG(DEBUG) << *output << std::endl;
51 MS_LOG(DEBUG) << *expected << std::endl;
52
53 ASSERT_TRUE(*output == *expected);
54 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
55 }
56
TEST_F(MindDataTestSliceOp,TestOpNeg)57 TEST_F(MindDataTestSliceOp, TestOpNeg) {
58 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpNeg.";
59 std::vector<uint64_t> labels = {1, 1, 3, 6, 4, 2};
60 std::shared_ptr<Tensor> input;
61 Tensor::CreateFromVector(labels, &input);
62
63 std::shared_ptr<Tensor> output;
64 Slice slice = Slice(-1, -5, -1);
65 std::unique_ptr<SliceOp> op(new SliceOp(slice));
66 Status s = op->Compute(input, &output);
67
68 std::vector<uint64_t> out = {2, 4, 6, 3};
69 std::shared_ptr<Tensor> expected;
70 Tensor::CreateFromVector(out, &expected);
71
72 EXPECT_TRUE(s.IsOk());
73 ASSERT_TRUE(output->shape() == expected->shape());
74 ASSERT_TRUE(output->type() == expected->type());
75
76 MS_LOG(DEBUG) << *output << std::endl;
77 MS_LOG(DEBUG) << *expected << std::endl;
78
79 ASSERT_TRUE(*output == *expected);
80 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
81 }
82
TEST_F(MindDataTestSliceOp,TestOp2D)83 TEST_F(MindDataTestSliceOp, TestOp2D) {
84 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOp2D.";
85 std::vector<uint64_t> labels = {1, 1, 3, 2, 3, 2};
86 std::shared_ptr<Tensor> input;
87 Tensor::CreateFromVector(labels, TensorShape({2, 3}), &input);
88
89 std::shared_ptr<Tensor> output;
90 Slice slice1_ = Slice(0, 2);
91 Slice slice2_ = Slice(0, 1);
92
93 std::vector<SliceOption> slices_ = {SliceOption(slice1_), SliceOption(slice2_)};
94 std::unique_ptr<SliceOp> op(new SliceOp(slices_));
95 Status s = op->Compute(input, &output);
96
97 std::vector<uint64_t> out = {1, 2};
98 std::shared_ptr<Tensor> expected;
99 Tensor::CreateFromVector(out, TensorShape({2, 1}), &expected);
100
101 EXPECT_TRUE(s.IsOk());
102 ASSERT_TRUE(output->shape() == expected->shape());
103 ASSERT_TRUE(output->type() == expected->type());
104
105 MS_LOG(DEBUG) << *output << std::endl;
106 MS_LOG(DEBUG) << *expected << std::endl;
107
108 ASSERT_TRUE(*output == *expected);
109 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
110 }
111
TEST_F(MindDataTestSliceOp,TestOp3D)112 TEST_F(MindDataTestSliceOp, TestOp3D) {
113 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOp3D.";
114 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
115 std::shared_ptr<Tensor> input;
116 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
117
118 std::shared_ptr<Tensor> output;
119 Slice slice1_ = Slice(0, 1);
120 Slice slice2_ = Slice(0, 2);
121 Slice slice3_ = Slice(0, 2);
122 std::vector<SliceOption> slices_ = {SliceOption(slice1_), SliceOption(slice2_), SliceOption(slice3_)};
123 std::unique_ptr<SliceOp> op(new SliceOp(slices_));
124 Status s = op->Compute(input, &output);
125
126 std::vector<uint64_t> out = {1, 2, 3, 4};
127 std::shared_ptr<Tensor> expected;
128 Tensor::CreateFromVector(out, TensorShape({1, 2, 2}), &expected);
129
130 EXPECT_TRUE(s.IsOk());
131
132 ASSERT_TRUE(output->shape() == expected->shape());
133 ASSERT_TRUE(output->type() == expected->type());
134 MS_LOG(DEBUG) << *output << std::endl;
135 MS_LOG(DEBUG) << *expected << std::endl;
136
137 ASSERT_TRUE(*output == *expected);
138 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
139 }
140
TEST_F(MindDataTestSliceOp,TestOpReturnNothing)141 TEST_F(MindDataTestSliceOp, TestOpReturnNothing) {
142 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpReturnNothing.";
143 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
144 std::shared_ptr<Tensor> input;
145 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
146
147 std::shared_ptr<Tensor> output;
148 Slice slice1_ = Slice(0, 1);
149 Slice slice2_ = Slice(2, 1);
150 Slice slice3_ = Slice(0, 2);
151 std::vector<SliceOption> slices_ = {SliceOption(slice1_), SliceOption(slice2_), SliceOption(slice3_)};
152 std::unique_ptr<SliceOp> op(new SliceOp(slices_));
153 Status s = op->Compute(input, &output);
154
155 std::vector<uint64_t> out = {};
156 std::shared_ptr<Tensor> expected;
157 Tensor::CreateFromVector(out, TensorShape({1, 0, 2}), &expected);
158
159 EXPECT_TRUE(s.IsOk());
160 ASSERT_TRUE(output->shape() == expected->shape());
161 ASSERT_TRUE(output->type() == expected->type());
162
163 MS_LOG(DEBUG) << *output << std::endl;
164 MS_LOG(DEBUG) << *expected << std::endl;
165
166 ASSERT_TRUE(*output == *expected);
167 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
168 }
169
TEST_F(MindDataTestSliceOp,TestOpPartialSlice)170 TEST_F(MindDataTestSliceOp, TestOpPartialSlice) {
171 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpPartialSlice.";
172 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
173 std::shared_ptr<Tensor> input;
174 Tensor::CreateFromVector(labels, TensorShape({4, 2}), &input);
175
176 std::shared_ptr<Tensor> output;
177 Slice slice1_ = Slice(0, 2);
178 std::unique_ptr<SliceOp> op(new SliceOp(slice1_));
179 Status s = op->Compute(input, &output);
180
181 std::vector<uint64_t> out = {1, 2, 3, 4};
182 std::shared_ptr<Tensor> expected;
183 Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
184
185 EXPECT_TRUE(s.IsOk());
186 ASSERT_TRUE(output->shape() == expected->shape());
187 ASSERT_TRUE(output->type() == expected->type());
188
189 MS_LOG(DEBUG) << *output << std::endl;
190 MS_LOG(DEBUG) << *expected << std::endl;
191
192 ASSERT_TRUE(*output == *expected);
193 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
194 }
195
TEST_F(MindDataTestSliceOp,TestOpBool1)196 TEST_F(MindDataTestSliceOp, TestOpBool1) {
197 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBool1.";
198 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
199 std::shared_ptr<Tensor> input;
200 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
201
202 std::shared_ptr<Tensor> output;
203 std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(true)));
204 Status s = op->Compute(input, &output);
205
206 std::shared_ptr<Tensor> expected;
207 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &expected);
208
209 EXPECT_TRUE(s.IsOk());
210 ASSERT_TRUE(output->shape() == expected->shape());
211 ASSERT_TRUE(output->type() == expected->type());
212
213 MS_LOG(DEBUG) << *output << std::endl;
214 MS_LOG(DEBUG) << *expected << std::endl;
215
216 ASSERT_TRUE(*output == *expected);
217 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
218 }
219
TEST_F(MindDataTestSliceOp,TestOpBool2)220 TEST_F(MindDataTestSliceOp, TestOpBool2) {
221 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBool2.";
222 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
223 std::shared_ptr<Tensor> input;
224 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
225
226 std::shared_ptr<Tensor> output;
227 std::unique_ptr<SliceOp> op(new SliceOp(true));
228 Status s = op->Compute(input, &output);
229
230 std::shared_ptr<Tensor> expected;
231 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &expected);
232
233 EXPECT_TRUE(s.IsOk());
234 ASSERT_TRUE(output->shape() == expected->shape());
235 ASSERT_TRUE(output->type() == expected->type());
236
237 MS_LOG(DEBUG) << *output << std::endl;
238 MS_LOG(DEBUG) << *expected << std::endl;
239
240 ASSERT_TRUE(*output == *expected);
241 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
242 }
243
244 // testing passing in just indices
TEST_F(MindDataTestSliceOp,TestOpIndices1)245 TEST_F(MindDataTestSliceOp, TestOpIndices1) {
246 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndices1.";
247 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8, 9};
248 std::shared_ptr<Tensor> input;
249 Tensor::CreateFromVector(labels, TensorShape({3, 3}), &input);
250
251 std::shared_ptr<Tensor> output;
252 std::vector<SliceOption> indices;
253 std::vector<dsize_t> index1 = {1, 2};
254 std::vector<dsize_t> index2 = {0, 1};
255 indices.emplace_back(SliceOption(index1));
256 indices.emplace_back(SliceOption(index2));
257 std::unique_ptr<SliceOp> op(new SliceOp(indices));
258 Status s = op->Compute(input, &output);
259
260 std::vector<uint64_t> out = {4, 5, 7, 8};
261 std::shared_ptr<Tensor> expected;
262 Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
263
264 EXPECT_TRUE(s.IsOk());
265 ASSERT_TRUE(output->shape() == expected->shape());
266 ASSERT_TRUE(output->type() == expected->type());
267
268 MS_LOG(DEBUG) << *output << std::endl;
269 MS_LOG(DEBUG) << *expected << std::endl;
270
271 ASSERT_TRUE(*output == *expected);
272 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
273 }
274
275 // testing passing in just indices
TEST_F(MindDataTestSliceOp,TestOpIndices2)276 TEST_F(MindDataTestSliceOp, TestOpIndices2) {
277 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndices2.";
278 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
279 std::shared_ptr<Tensor> input;
280 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
281
282 std::shared_ptr<Tensor> output;
283 std::vector<dsize_t> indices = {0};
284 std::unique_ptr<SliceOp> op(new SliceOp(indices));
285 Status s = op->Compute(input, &output);
286
287 std::vector<uint64_t> out = {1, 2, 3, 4};
288
289 std::shared_ptr<Tensor> expected;
290 Tensor::CreateFromVector(out, TensorShape({1, 2, 2}), &expected);
291
292 EXPECT_TRUE(s.IsOk());
293 ASSERT_TRUE(output->shape() == expected->shape());
294 ASSERT_TRUE(output->type() == expected->type());
295
296 MS_LOG(DEBUG) << *output << std::endl;
297 MS_LOG(DEBUG) << *expected << std::endl;
298
299 ASSERT_TRUE(*output == *expected);
300 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
301 }
302
303 // Test Index Object
TEST_F(MindDataTestSliceOp,TestOpSliceAndIndex)304 TEST_F(MindDataTestSliceOp, TestOpSliceAndIndex) {
305 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpSliceAndIndex.";
306 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
307 std::shared_ptr<Tensor> input;
308 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
309
310 std::shared_ptr<Tensor> output;
311 std::vector<dsize_t> indices = {0};
312 Slice slice = Slice(1);
313 std::vector<SliceOption> slice_options = {SliceOption(indices), SliceOption(slice)};
314 std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
315 Status s = op->Compute(input, &output);
316
317 std::vector<uint64_t> out = {1, 2};
318 std::shared_ptr<Tensor> expected;
319 Tensor::CreateFromVector(out, TensorShape({1, 1, 2}), &expected);
320
321 EXPECT_TRUE(s.IsOk());
322 ASSERT_TRUE(output->shape() == expected->shape());
323 ASSERT_TRUE(output->type() == expected->type());
324
325 MS_LOG(DEBUG) << *output << std::endl;
326 MS_LOG(DEBUG) << *expected << std::endl;
327
328 ASSERT_TRUE(*output == *expected);
329 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
330 }
331
TEST_F(MindDataTestSliceOp,TestOpLargerStep)332 TEST_F(MindDataTestSliceOp, TestOpLargerStep) {
333 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpLargerStep.";
334 std::vector<uint64_t> labels = {1, 2, 3, 4, 5};
335 std::shared_ptr<Tensor> input;
336 Tensor::CreateFromVector(labels, TensorShape({1, 5}), &input);
337
338 std::shared_ptr<Tensor> output;
339 Slice slice1_ = Slice(0, 1);
340 Slice slice2_ = Slice(0, 4, 2);
341
342 std::vector<SliceOption> slice_options = {SliceOption(slice1_), SliceOption(slice2_)};
343 std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
344 Status s = op->Compute(input, &output);
345
346 std::vector<uint64_t> out = {1, 3};
347 std::shared_ptr<Tensor> expected;
348
349 Tensor::CreateFromVector(out, TensorShape({1, 2}), &expected);
350
351 EXPECT_TRUE(s.IsOk());
352 ASSERT_TRUE(output->shape() == expected->shape());
353 ASSERT_TRUE(output->type() == expected->type());
354
355 MS_LOG(DEBUG) << *output << std::endl;
356 MS_LOG(DEBUG) << *expected << std::endl;
357
358 ASSERT_TRUE(*output == *expected);
359 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
360 }
361
TEST_F(MindDataTestSliceOp,TestOpIndicesError1)362 TEST_F(MindDataTestSliceOp, TestOpIndicesError1) {
363 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesError1.";
364 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
365 std::shared_ptr<Tensor> input;
366 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
367
368 std::shared_ptr<Tensor> output;
369 std::unique_ptr<SliceOp> op(new SliceOp(Slice()));
370 Status s = op->Compute(input, &output);
371
372 EXPECT_FALSE(s.IsOk());
373 EXPECT_NE(s.ToString().find("Both indices and slices can not be empty."), std::string::npos);
374
375 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
376 }
377
TEST_F(MindDataTestSliceOp,TestOpIndicesError2)378 TEST_F(MindDataTestSliceOp, TestOpIndicesError2) {
379 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesError2.";
380 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
381 std::shared_ptr<Tensor> input;
382 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
383
384 std::shared_ptr<Tensor> output;
385 SliceOption slice_option = SliceOption(Slice(2));
386 std::vector<dsize_t> indices = {0};
387 slice_option.indices_ = indices;
388 std::unique_ptr<SliceOp> op(new SliceOp(slice_option));
389 Status s = op->Compute(input, &output);
390
391 EXPECT_FALSE(s.IsOk());
392 EXPECT_NE(s.ToString().find("Both indices and slices can not be given."), std::string::npos);
393
394 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
395 }
396
TEST_F(MindDataTestSliceOp,TestOpIndicesError3)397 TEST_F(MindDataTestSliceOp, TestOpIndicesError3) {
398 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesError3.";
399 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
400 std::shared_ptr<Tensor> input;
401 Tensor::CreateFromVector(labels, TensorShape({8}), &input);
402
403 std::shared_ptr<Tensor> output;
404 std::vector<dsize_t> indices = {8};
405
406 std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(indices)));
407 Status s = op->Compute(input, &output);
408
409 EXPECT_FALSE(s.IsOk());
410 EXPECT_NE(s.ToString().find("Index 8 is out of bounds."), std::string::npos);
411
412 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
413 }
414
TEST_F(MindDataTestSliceOp,TestOpBasicString)415 TEST_F(MindDataTestSliceOp, TestOpBasicString) {
416 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBasicString.";
417 std::vector<std::string> labels = {"1", "1", "3", "2d"};
418 std::shared_ptr<Tensor> input;
419 Tensor::CreateFromVector(labels, &input);
420
421 std::shared_ptr<Tensor> output;
422 Slice slice = Slice(1, 3);
423 std::unique_ptr<SliceOp> op(new SliceOp(slice));
424 Status s = op->Compute(input, &output);
425
426 std::vector<std::string> out = {"1", "3"};
427 std::shared_ptr<Tensor> expected;
428 Tensor::CreateFromVector(out, &expected);
429
430 EXPECT_TRUE(s.IsOk());
431 ASSERT_TRUE(output->shape() == expected->shape());
432 ASSERT_TRUE(output->type() == expected->type());
433
434 MS_LOG(DEBUG) << *output << std::endl;
435 MS_LOG(DEBUG) << *expected << std::endl;
436
437 ASSERT_TRUE(*output == *expected);
438 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
439 }
440
TEST_F(MindDataTestSliceOp,TestOp2DString)441 TEST_F(MindDataTestSliceOp, TestOp2DString) {
442 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOp2DString.";
443 std::vector<std::string> labels = {"1a", "1b", "3", "2", "3", "2"};
444 std::shared_ptr<Tensor> input;
445 Tensor::CreateFromVector(labels, TensorShape({2, 3}), &input);
446
447 std::shared_ptr<Tensor> output;
448 Slice slice1_ = Slice(0, 2);
449 Slice slice2_ = Slice(0, 2);
450
451 std::vector<SliceOption> slice_option = {SliceOption(slice1_), SliceOption(slice2_)};
452 std::unique_ptr<SliceOp> op(new SliceOp(slice_option));
453 Status s = op->Compute(input, &output);
454
455 std::vector<std::string> out = {"1a", "1b", "2", "3"};
456 std::shared_ptr<Tensor> expected;
457 Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
458
459 EXPECT_TRUE(s.IsOk());
460 ASSERT_TRUE(output->shape() == expected->shape());
461 ASSERT_TRUE(output->type() == expected->type());
462
463 MS_LOG(DEBUG) << *output << std::endl;
464 MS_LOG(DEBUG) << *expected << std::endl;
465
466 ASSERT_TRUE(*output == *expected);
467 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
468 }
469
TEST_F(MindDataTestSliceOp,TestOpPartialSliceString)470 TEST_F(MindDataTestSliceOp, TestOpPartialSliceString) {
471 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpPartialSliceString.";
472 std::vector<std::string> labels = {"1a", "1b", "3", "2", "3", "2", "4", "66"};
473 std::shared_ptr<Tensor> input;
474 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
475
476 std::shared_ptr<Tensor> output;
477 Slice slice1 = Slice(0, 2);
478 Slice slice2 = Slice(0, 1);
479
480 std::vector<SliceOption> slice_options = {SliceOption(slice1), SliceOption(slice2)};
481 std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
482 Status s = op->Compute(input, &output);
483
484 std::vector<std::string> out = {"1a", "1b", "3", "2"};
485 std::shared_ptr<Tensor> expected;
486 Tensor::CreateFromVector(out, TensorShape({2, 1, 2}), &expected);
487
488 EXPECT_TRUE(s.IsOk());
489 ASSERT_TRUE(output->shape() == expected->shape());
490 ASSERT_TRUE(output->type() == expected->type());
491
492 MS_LOG(DEBUG) << *output << std::endl;
493 MS_LOG(DEBUG) << *expected << std::endl;
494
495 ASSERT_TRUE(*output == *expected);
496 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
497 }
498
TEST_F(MindDataTestSliceOp,TestOpIndicesString)499 TEST_F(MindDataTestSliceOp, TestOpIndicesString) {
500 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesString.";
501 std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
502 std::shared_ptr<Tensor> input;
503 Tensor::CreateFromVector(labels, TensorShape({3, 3}), &input);
504
505 std::shared_ptr<Tensor> output;
506 std::vector<dsize_t> index1 = {1, 2};
507 std::vector<dsize_t> index2 = {0, 1};
508 std::vector<SliceOption> slice_options = {SliceOption(index1), SliceOption(index2)};
509
510 std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
511 Status s = op->Compute(input, &output);
512
513 std::vector<std::string> out = {"4", "5", "7", "8"};
514 std::shared_ptr<Tensor> expected;
515 Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
516
517 EXPECT_TRUE(s.IsOk());
518 ASSERT_TRUE(output->shape() == expected->shape());
519 ASSERT_TRUE(output->type() == expected->type());
520
521 MS_LOG(DEBUG) << *output << std::endl;
522 MS_LOG(DEBUG) << *expected << std::endl;
523
524 ASSERT_TRUE(*output == *expected);
525 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
526 }
527
TEST_F(MindDataTestSliceOp,TestOpIndicesString2)528 TEST_F(MindDataTestSliceOp, TestOpIndicesString2) {
529 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesString2.";
530 std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
531 std::shared_ptr<Tensor> input;
532 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
533
534 std::shared_ptr<Tensor> output;
535 std::vector<dsize_t> indices = {0};
536 std::unique_ptr<SliceOp> op(new SliceOp(indices));
537 Status s = op->Compute(input, &output);
538
539 std::vector<std::string> out = {"1", "2", "3", "4"};
540
541 std::shared_ptr<Tensor> expected;
542 Tensor::CreateFromVector(out, TensorShape({1, 2, 2}), &expected);
543
544 EXPECT_TRUE(s.IsOk());
545 ASSERT_TRUE(output->shape() == expected->shape());
546 ASSERT_TRUE(output->type() == expected->type());
547
548 MS_LOG(DEBUG) << *output << std::endl;
549 MS_LOG(DEBUG) << *expected << std::endl;
550
551 ASSERT_TRUE(*output == *expected);
552 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
553 }
554
TEST_F(MindDataTestSliceOp,TestOpSliceAndIndexString)555 TEST_F(MindDataTestSliceOp, TestOpSliceAndIndexString) {
556 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpSliceAndIndexString.";
557 std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
558 std::shared_ptr<Tensor> input;
559 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
560
561 std::shared_ptr<Tensor> output;
562 std::vector<dsize_t> indices = {0};
563 Slice slice = Slice(1);
564 std::vector<SliceOption> slice_options = {SliceOption(indices), SliceOption(slice)};
565 std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
566 Status s = op->Compute(input, &output);
567
568 std::vector<std::string> out = {"1", "2"};
569 std::shared_ptr<Tensor> expected;
570 Tensor::CreateFromVector(out, TensorShape({1, 1, 2}), &expected);
571
572 EXPECT_TRUE(s.IsOk());
573 ASSERT_TRUE(output->shape() == expected->shape());
574 ASSERT_TRUE(output->type() == expected->type());
575
576 MS_LOG(DEBUG) << *output << std::endl;
577 MS_LOG(DEBUG) << *expected << std::endl;
578
579 ASSERT_TRUE(*output == *expected);
580 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
581 }
582
TEST_F(MindDataTestSliceOp,TestOpLargerStepString)583 TEST_F(MindDataTestSliceOp, TestOpLargerStepString) {
584 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpLargerStepString.";
585 std::vector<std::string> labels = {"1", "2", "3", "4", "5"};
586 std::shared_ptr<Tensor> input;
587 Tensor::CreateFromVector(labels, TensorShape({1, 5}), &input);
588
589 std::shared_ptr<Tensor> output;
590 Slice slice1_ = Slice(0, 1);
591 Slice slice2_ = Slice(0, 4, 2);
592
593 std::vector<SliceOption> slice_options = {SliceOption(slice1_), SliceOption(slice2_)};
594 std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
595 Status s = op->Compute(input, &output);
596
597 std::vector<std::string> out = {"1", "3"};
598 std::shared_ptr<Tensor> expected;
599
600 Tensor::CreateFromVector(out, TensorShape({1, 2}), &expected);
601
602 EXPECT_TRUE(s.IsOk());
603 ASSERT_TRUE(output->shape() == expected->shape());
604 ASSERT_TRUE(output->type() == expected->type());
605
606 MS_LOG(DEBUG) << *output << std::endl;
607 MS_LOG(DEBUG) << *expected << std::endl;
608
609 ASSERT_TRUE(*output == *expected);
610 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
611 }
612
TEST_F(MindDataTestSliceOp,TestOpIndicesErrorString1)613 TEST_F(MindDataTestSliceOp, TestOpIndicesErrorString1) {
614 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesErrorString1.";
615 std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
616 std::shared_ptr<Tensor> input;
617 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
618
619 std::shared_ptr<Tensor> output;
620 std::unique_ptr<SliceOp> op(new SliceOp(Slice()));
621 Status s = op->Compute(input, &output);
622
623 EXPECT_FALSE(s.IsOk());
624 EXPECT_NE(s.ToString().find("Both indices and slices can not be empty."), std::string::npos);
625
626 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
627 }
628
TEST_F(MindDataTestSliceOp,TestOpIndicesErrorString2)629 TEST_F(MindDataTestSliceOp, TestOpIndicesErrorString2) {
630 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesErrorString2.";
631 std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
632 std::shared_ptr<Tensor> input;
633 Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
634
635 std::shared_ptr<Tensor> output;
636 SliceOption slice_option = SliceOption(Slice(2));
637 std::vector<dsize_t> indices = {0};
638 slice_option.indices_ = indices;
639 std::unique_ptr<SliceOp> op(new SliceOp(slice_option));
640 Status s = op->Compute(input, &output);
641
642 EXPECT_FALSE(s.IsOk());
643 EXPECT_NE(s.ToString().find("Both indices and slices can not be given."), std::string::npos);
644
645 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
646 }
647
TEST_F(MindDataTestSliceOp,TestOpIndicesErrorString3)648 TEST_F(MindDataTestSliceOp, TestOpIndicesErrorString3) {
649 MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesErrorString3.";
650 std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
651 std::shared_ptr<Tensor> input;
652 Tensor::CreateFromVector(labels, TensorShape({2, 4}), &input);
653
654 std::shared_ptr<Tensor> output;
655 std::vector<dsize_t> indices = {2};
656
657 std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(indices)));
658 Status s = op->Compute(input, &output);
659
660 EXPECT_FALSE(s.IsOk());
661 EXPECT_NE(s.ToString().find("Index 2 is out of bounds."), std::string::npos);
662
663 MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
664 }
665