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.

slice_op_test.cc 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. using namespace mindspore::dataset;
  20. using mindspore::LogStream;
  21. using mindspore::ExceptionType::NoExceptionType;
  22. using mindspore::MsLogLevel::INFO;
  23. class MindDataTestSliceOp : public UT::Common {
  24. protected:
  25. MindDataTestSliceOp() {}
  26. };
  27. TEST_F(MindDataTestSliceOp, TestOpBasic) {
  28. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBasic.";
  29. std::vector<uint64_t> labels = {1, 1, 3, 2};
  30. std::shared_ptr<Tensor> input;
  31. Tensor::CreateFromVector(labels, &input);
  32. std::shared_ptr<Tensor> output;
  33. Slice slice = Slice(1, 3);
  34. std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(slice)));
  35. Status s = op->Compute(input, &output);
  36. std::vector<uint64_t> out = {1, 3};
  37. std::shared_ptr<Tensor> expected;
  38. Tensor::CreateFromVector(out, &expected);
  39. EXPECT_TRUE(s.IsOk());
  40. ASSERT_TRUE(output->shape() == expected->shape());
  41. ASSERT_TRUE(output->type() == expected->type());
  42. MS_LOG(DEBUG) << *output << std::endl;
  43. MS_LOG(DEBUG) << *expected << std::endl;
  44. ASSERT_TRUE(*output == *expected);
  45. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  46. }
  47. TEST_F(MindDataTestSliceOp, TestOpNeg) {
  48. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpNeg.";
  49. std::vector<uint64_t> labels = {1, 1, 3, 6, 4, 2};
  50. std::shared_ptr<Tensor> input;
  51. Tensor::CreateFromVector(labels, &input);
  52. std::shared_ptr<Tensor> output;
  53. Slice slice = Slice(-1, -5, -1);
  54. std::unique_ptr<SliceOp> op(new SliceOp(slice));
  55. Status s = op->Compute(input, &output);
  56. std::vector<uint64_t> out = {2, 4, 6, 3};
  57. std::shared_ptr<Tensor> expected;
  58. Tensor::CreateFromVector(out, &expected);
  59. EXPECT_TRUE(s.IsOk());
  60. ASSERT_TRUE(output->shape() == expected->shape());
  61. ASSERT_TRUE(output->type() == expected->type());
  62. MS_LOG(DEBUG) << *output << std::endl;
  63. MS_LOG(DEBUG) << *expected << std::endl;
  64. ASSERT_TRUE(*output == *expected);
  65. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  66. }
  67. TEST_F(MindDataTestSliceOp, TestOp2D) {
  68. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOp2D.";
  69. std::vector<uint64_t> labels = {1, 1, 3, 2, 3, 2};
  70. std::shared_ptr<Tensor> input;
  71. Tensor::CreateFromVector(labels, TensorShape({2, 3}), &input);
  72. std::shared_ptr<Tensor> output;
  73. Slice slice1_ = Slice(0, 2);
  74. Slice slice2_ = Slice(0, 1);
  75. std::vector<SliceOption> slices_ = {SliceOption(slice1_), SliceOption(slice2_)};
  76. std::unique_ptr<SliceOp> op(new SliceOp(slices_));
  77. Status s = op->Compute(input, &output);
  78. std::vector<uint64_t> out = {1, 2};
  79. std::shared_ptr<Tensor> expected;
  80. Tensor::CreateFromVector(out, TensorShape({2, 1}), &expected);
  81. EXPECT_TRUE(s.IsOk());
  82. ASSERT_TRUE(output->shape() == expected->shape());
  83. ASSERT_TRUE(output->type() == expected->type());
  84. MS_LOG(DEBUG) << *output << std::endl;
  85. MS_LOG(DEBUG) << *expected << std::endl;
  86. ASSERT_TRUE(*output == *expected);
  87. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  88. }
  89. TEST_F(MindDataTestSliceOp, TestOp3D) {
  90. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOp3D.";
  91. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  92. std::shared_ptr<Tensor> input;
  93. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  94. std::shared_ptr<Tensor> output;
  95. Slice slice1_ = Slice(0, 1);
  96. Slice slice2_ = Slice(0, 2);
  97. Slice slice3_ = Slice(0, 2);
  98. std::vector<SliceOption> slices_ = {SliceOption(slice1_), SliceOption(slice2_), SliceOption(slice3_)};
  99. std::unique_ptr<SliceOp> op(new SliceOp(slices_));
  100. Status s = op->Compute(input, &output);
  101. std::vector<uint64_t> out = {1, 2, 3, 4};
  102. std::shared_ptr<Tensor> expected;
  103. Tensor::CreateFromVector(out, TensorShape({1, 2, 2}), &expected);
  104. EXPECT_TRUE(s.IsOk());
  105. ASSERT_TRUE(output->shape() == expected->shape());
  106. ASSERT_TRUE(output->type() == expected->type());
  107. MS_LOG(DEBUG) << *output << std::endl;
  108. MS_LOG(DEBUG) << *expected << std::endl;
  109. ASSERT_TRUE(*output == *expected);
  110. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  111. }
  112. TEST_F(MindDataTestSliceOp, TestOpReturnNothing) {
  113. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpReturnNothing.";
  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. std::shared_ptr<Tensor> output;
  118. Slice slice1_ = Slice(0, 1);
  119. Slice slice2_ = Slice(2, 1);
  120. Slice slice3_ = Slice(0, 2);
  121. std::vector<SliceOption> slices_ = {SliceOption(slice1_), SliceOption(slice2_), SliceOption(slice3_)};
  122. std::unique_ptr<SliceOp> op(new SliceOp(slices_));
  123. Status s = op->Compute(input, &output);
  124. std::vector<uint64_t> out = {};
  125. std::shared_ptr<Tensor> expected;
  126. Tensor::CreateFromVector(out, TensorShape({1, 0, 2}), &expected);
  127. EXPECT_TRUE(s.IsOk());
  128. ASSERT_TRUE(output->shape() == expected->shape());
  129. ASSERT_TRUE(output->type() == expected->type());
  130. MS_LOG(DEBUG) << *output << std::endl;
  131. MS_LOG(DEBUG) << *expected << std::endl;
  132. ASSERT_TRUE(*output == *expected);
  133. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  134. }
  135. TEST_F(MindDataTestSliceOp, TestOpPartialSlice) {
  136. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpPartialSlice.";
  137. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  138. std::shared_ptr<Tensor> input;
  139. Tensor::CreateFromVector(labels, TensorShape({4, 2}), &input);
  140. std::shared_ptr<Tensor> output;
  141. Slice slice1_ = Slice(0, 2);
  142. std::unique_ptr<SliceOp> op(new SliceOp(slice1_));
  143. Status s = op->Compute(input, &output);
  144. std::vector<uint64_t> out = {1, 2, 3, 4};
  145. std::shared_ptr<Tensor> expected;
  146. Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
  147. EXPECT_TRUE(s.IsOk());
  148. ASSERT_TRUE(output->shape() == expected->shape());
  149. ASSERT_TRUE(output->type() == expected->type());
  150. MS_LOG(DEBUG) << *output << std::endl;
  151. MS_LOG(DEBUG) << *expected << std::endl;
  152. ASSERT_TRUE(*output == *expected);
  153. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  154. }
  155. TEST_F(MindDataTestSliceOp, TestOpBool1) {
  156. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBool1.";
  157. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  158. std::shared_ptr<Tensor> input;
  159. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  160. std::shared_ptr<Tensor> output;
  161. std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(true)));
  162. Status s = op->Compute(input, &output);
  163. std::shared_ptr<Tensor> expected;
  164. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &expected);
  165. EXPECT_TRUE(s.IsOk());
  166. ASSERT_TRUE(output->shape() == expected->shape());
  167. ASSERT_TRUE(output->type() == expected->type());
  168. MS_LOG(DEBUG) << *output << std::endl;
  169. MS_LOG(DEBUG) << *expected << std::endl;
  170. ASSERT_TRUE(*output == *expected);
  171. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  172. }
  173. TEST_F(MindDataTestSliceOp, TestOpBool2) {
  174. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBool2.";
  175. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  176. std::shared_ptr<Tensor> input;
  177. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  178. std::shared_ptr<Tensor> output;
  179. std::unique_ptr<SliceOp> op(new SliceOp(true));
  180. Status s = op->Compute(input, &output);
  181. std::shared_ptr<Tensor> expected;
  182. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &expected);
  183. EXPECT_TRUE(s.IsOk());
  184. ASSERT_TRUE(output->shape() == expected->shape());
  185. ASSERT_TRUE(output->type() == expected->type());
  186. MS_LOG(DEBUG) << *output << std::endl;
  187. MS_LOG(DEBUG) << *expected << std::endl;
  188. ASSERT_TRUE(*output == *expected);
  189. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  190. }
  191. // testing passing in just indices
  192. TEST_F(MindDataTestSliceOp, TestOpIndices1) {
  193. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndices1.";
  194. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  195. std::shared_ptr<Tensor> input;
  196. Tensor::CreateFromVector(labels, TensorShape({3, 3}), &input);
  197. std::shared_ptr<Tensor> output;
  198. std::vector<SliceOption> indices;
  199. std::vector<dsize_t> index1 = {1, 2};
  200. std::vector<dsize_t> index2 = {0, 1};
  201. indices.emplace_back(SliceOption(index1));
  202. indices.emplace_back(SliceOption(index2));
  203. std::unique_ptr<SliceOp> op(new SliceOp(indices));
  204. Status s = op->Compute(input, &output);
  205. std::vector<uint64_t> out = {4, 5, 7, 8};
  206. std::shared_ptr<Tensor> expected;
  207. Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
  208. EXPECT_TRUE(s.IsOk());
  209. ASSERT_TRUE(output->shape() == expected->shape());
  210. ASSERT_TRUE(output->type() == expected->type());
  211. MS_LOG(DEBUG) << *output << std::endl;
  212. MS_LOG(DEBUG) << *expected << std::endl;
  213. ASSERT_TRUE(*output == *expected);
  214. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  215. }
  216. // testing passing in just indices
  217. TEST_F(MindDataTestSliceOp, TestOpIndices2) {
  218. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndices2.";
  219. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  220. std::shared_ptr<Tensor> input;
  221. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  222. std::shared_ptr<Tensor> output;
  223. std::vector<dsize_t> indices = {0};
  224. std::unique_ptr<SliceOp> op(new SliceOp(indices));
  225. Status s = op->Compute(input, &output);
  226. std::vector<uint64_t> out = {1, 2, 3, 4};
  227. std::shared_ptr<Tensor> expected;
  228. Tensor::CreateFromVector(out, TensorShape({1, 2, 2}), &expected);
  229. EXPECT_TRUE(s.IsOk());
  230. ASSERT_TRUE(output->shape() == expected->shape());
  231. ASSERT_TRUE(output->type() == expected->type());
  232. MS_LOG(DEBUG) << *output << std::endl;
  233. MS_LOG(DEBUG) << *expected << std::endl;
  234. ASSERT_TRUE(*output == *expected);
  235. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  236. }
  237. // Test Index Object
  238. TEST_F(MindDataTestSliceOp, TestOpSliceAndIndex) {
  239. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpSliceAndIndex.";
  240. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  241. std::shared_ptr<Tensor> input;
  242. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  243. std::shared_ptr<Tensor> output;
  244. std::vector<dsize_t> indices = {0};
  245. Slice slice = Slice(1);
  246. std::vector<SliceOption> slice_options = {SliceOption(indices), SliceOption(slice)};
  247. std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
  248. Status s = op->Compute(input, &output);
  249. std::vector<uint64_t> out = {1, 2};
  250. std::shared_ptr<Tensor> expected;
  251. Tensor::CreateFromVector(out, TensorShape({1, 1, 2}), &expected);
  252. EXPECT_TRUE(s.IsOk());
  253. ASSERT_TRUE(output->shape() == expected->shape());
  254. ASSERT_TRUE(output->type() == expected->type());
  255. MS_LOG(DEBUG) << *output << std::endl;
  256. MS_LOG(DEBUG) << *expected << std::endl;
  257. ASSERT_TRUE(*output == *expected);
  258. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  259. }
  260. TEST_F(MindDataTestSliceOp, TestOpLargerStep) {
  261. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpLargerStep.";
  262. std::vector<uint64_t> labels = {1, 2, 3, 4, 5};
  263. std::shared_ptr<Tensor> input;
  264. Tensor::CreateFromVector(labels, TensorShape({1, 5}), &input);
  265. std::shared_ptr<Tensor> output;
  266. Slice slice1_ = Slice(0, 1);
  267. Slice slice2_ = Slice(0, 4, 2);
  268. std::vector<SliceOption> slice_options = {SliceOption(slice1_), SliceOption(slice2_)};
  269. std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
  270. Status s = op->Compute(input, &output);
  271. std::vector<uint64_t> out = {1, 3};
  272. std::shared_ptr<Tensor> expected;
  273. Tensor::CreateFromVector(out, TensorShape({1, 2}), &expected);
  274. EXPECT_TRUE(s.IsOk());
  275. ASSERT_TRUE(output->shape() == expected->shape());
  276. ASSERT_TRUE(output->type() == expected->type());
  277. MS_LOG(DEBUG) << *output << std::endl;
  278. MS_LOG(DEBUG) << *expected << std::endl;
  279. ASSERT_TRUE(*output == *expected);
  280. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  281. }
  282. TEST_F(MindDataTestSliceOp, TestOpIndicesError1) {
  283. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesError1.";
  284. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  285. std::shared_ptr<Tensor> input;
  286. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  287. std::shared_ptr<Tensor> output;
  288. std::unique_ptr<SliceOp> op(new SliceOp(Slice()));
  289. Status s = op->Compute(input, &output);
  290. EXPECT_FALSE(s.IsOk());
  291. EXPECT_NE(s.ToString().find("Both indices and slices can not be empty."), std::string::npos);
  292. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  293. }
  294. TEST_F(MindDataTestSliceOp, TestOpIndicesError2) {
  295. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesError2.";
  296. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  297. std::shared_ptr<Tensor> input;
  298. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  299. std::shared_ptr<Tensor> output;
  300. SliceOption slice_option = SliceOption(Slice(2));
  301. std::vector<dsize_t> indices = {0};
  302. slice_option.indices_ = indices;
  303. std::unique_ptr<SliceOp> op(new SliceOp(slice_option));
  304. Status s = op->Compute(input, &output);
  305. EXPECT_FALSE(s.IsOk());
  306. EXPECT_NE(s.ToString().find("Both indices and slices can not be given."), std::string::npos);
  307. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  308. }
  309. TEST_F(MindDataTestSliceOp, TestOpIndicesError3) {
  310. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesError3.";
  311. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  312. std::shared_ptr<Tensor> input;
  313. Tensor::CreateFromVector(labels, TensorShape({8}), &input);
  314. std::shared_ptr<Tensor> output;
  315. std::vector<dsize_t> indices = {8};
  316. std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(indices)));
  317. Status s = op->Compute(input, &output);
  318. EXPECT_FALSE(s.IsOk());
  319. EXPECT_NE(s.ToString().find("Index 8 is out of bounds."), std::string::npos);
  320. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  321. }
  322. TEST_F(MindDataTestSliceOp, TestOpBasicString) {
  323. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpBasicString.";
  324. std::vector<std::string> labels = {"1", "1", "3", "2d"};
  325. std::shared_ptr<Tensor> input;
  326. Tensor::CreateFromVector(labels, &input);
  327. std::shared_ptr<Tensor> output;
  328. Slice slice = Slice(1, 3);
  329. std::unique_ptr<SliceOp> op(new SliceOp(slice));
  330. Status s = op->Compute(input, &output);
  331. std::vector<std::string> out = {"1", "3"};
  332. std::shared_ptr<Tensor> expected;
  333. Tensor::CreateFromVector(out, &expected);
  334. EXPECT_TRUE(s.IsOk());
  335. ASSERT_TRUE(output->shape() == expected->shape());
  336. ASSERT_TRUE(output->type() == expected->type());
  337. MS_LOG(DEBUG) << *output << std::endl;
  338. MS_LOG(DEBUG) << *expected << std::endl;
  339. ASSERT_TRUE(*output == *expected);
  340. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  341. }
  342. TEST_F(MindDataTestSliceOp, TestOp2DString) {
  343. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOp2DString.";
  344. std::vector<std::string> labels = {"1a", "1b", "3", "2", "3", "2"};
  345. std::shared_ptr<Tensor> input;
  346. Tensor::CreateFromVector(labels, TensorShape({2, 3}), &input);
  347. std::shared_ptr<Tensor> output;
  348. Slice slice1_ = Slice(0, 2);
  349. Slice slice2_ = Slice(0, 2);
  350. std::vector<SliceOption> slice_option = {SliceOption(slice1_), SliceOption(slice2_)};
  351. std::unique_ptr<SliceOp> op(new SliceOp(slice_option));
  352. Status s = op->Compute(input, &output);
  353. std::vector<std::string> out = {"1a", "1b", "2", "3"};
  354. std::shared_ptr<Tensor> expected;
  355. Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
  356. EXPECT_TRUE(s.IsOk());
  357. ASSERT_TRUE(output->shape() == expected->shape());
  358. ASSERT_TRUE(output->type() == expected->type());
  359. MS_LOG(DEBUG) << *output << std::endl;
  360. MS_LOG(DEBUG) << *expected << std::endl;
  361. ASSERT_TRUE(*output == *expected);
  362. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  363. }
  364. TEST_F(MindDataTestSliceOp, TestOpPartialSliceString) {
  365. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpPartialSliceString.";
  366. std::vector<std::string> labels = {"1a", "1b", "3", "2", "3", "2", "4", "66"};
  367. std::shared_ptr<Tensor> input;
  368. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  369. std::shared_ptr<Tensor> output;
  370. Slice slice1 = Slice(0, 2);
  371. Slice slice2 = Slice(0, 1);
  372. std::vector<SliceOption> slice_options = {SliceOption(slice1), SliceOption(slice2)};
  373. std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
  374. Status s = op->Compute(input, &output);
  375. std::vector<std::string> out = {"1a", "1b", "3", "2"};
  376. std::shared_ptr<Tensor> expected;
  377. Tensor::CreateFromVector(out, TensorShape({2, 1, 2}), &expected);
  378. EXPECT_TRUE(s.IsOk());
  379. ASSERT_TRUE(output->shape() == expected->shape());
  380. ASSERT_TRUE(output->type() == expected->type());
  381. MS_LOG(DEBUG) << *output << std::endl;
  382. MS_LOG(DEBUG) << *expected << std::endl;
  383. ASSERT_TRUE(*output == *expected);
  384. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  385. }
  386. TEST_F(MindDataTestSliceOp, TestOpIndicesString) {
  387. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesString.";
  388. std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
  389. std::shared_ptr<Tensor> input;
  390. Tensor::CreateFromVector(labels, TensorShape({3, 3}), &input);
  391. std::shared_ptr<Tensor> output;
  392. std::vector<dsize_t> index1 = {1, 2};
  393. std::vector<dsize_t> index2 = {0, 1};
  394. std::vector<SliceOption> slice_options = {SliceOption(index1), SliceOption(index2)};
  395. std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
  396. Status s = op->Compute(input, &output);
  397. std::vector<std::string> out = {"4", "5", "7", "8"};
  398. std::shared_ptr<Tensor> expected;
  399. Tensor::CreateFromVector(out, TensorShape({2, 2}), &expected);
  400. EXPECT_TRUE(s.IsOk());
  401. ASSERT_TRUE(output->shape() == expected->shape());
  402. ASSERT_TRUE(output->type() == expected->type());
  403. MS_LOG(DEBUG) << *output << std::endl;
  404. MS_LOG(DEBUG) << *expected << std::endl;
  405. ASSERT_TRUE(*output == *expected);
  406. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  407. }
  408. TEST_F(MindDataTestSliceOp, TestOpIndicesString2) {
  409. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesString2.";
  410. std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
  411. std::shared_ptr<Tensor> input;
  412. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  413. std::shared_ptr<Tensor> output;
  414. std::vector<dsize_t> indices = {0};
  415. std::unique_ptr<SliceOp> op(new SliceOp(indices));
  416. Status s = op->Compute(input, &output);
  417. std::vector<std::string> out = {"1", "2", "3", "4"};
  418. std::shared_ptr<Tensor> expected;
  419. Tensor::CreateFromVector(out, TensorShape({1, 2, 2}), &expected);
  420. EXPECT_TRUE(s.IsOk());
  421. ASSERT_TRUE(output->shape() == expected->shape());
  422. ASSERT_TRUE(output->type() == expected->type());
  423. MS_LOG(DEBUG) << *output << std::endl;
  424. MS_LOG(DEBUG) << *expected << std::endl;
  425. ASSERT_TRUE(*output == *expected);
  426. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  427. }
  428. TEST_F(MindDataTestSliceOp, TestOpSliceAndIndexString) {
  429. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpSliceAndIndexString.";
  430. std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
  431. std::shared_ptr<Tensor> input;
  432. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  433. std::shared_ptr<Tensor> output;
  434. std::vector<dsize_t> indices = {0};
  435. Slice slice = Slice(1);
  436. std::vector<SliceOption> slice_options = {SliceOption(indices), SliceOption(slice)};
  437. std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
  438. Status s = op->Compute(input, &output);
  439. std::vector<std::string> out = {"1", "2"};
  440. std::shared_ptr<Tensor> expected;
  441. Tensor::CreateFromVector(out, TensorShape({1, 1, 2}), &expected);
  442. EXPECT_TRUE(s.IsOk());
  443. ASSERT_TRUE(output->shape() == expected->shape());
  444. ASSERT_TRUE(output->type() == expected->type());
  445. MS_LOG(DEBUG) << *output << std::endl;
  446. MS_LOG(DEBUG) << *expected << std::endl;
  447. ASSERT_TRUE(*output == *expected);
  448. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  449. }
  450. TEST_F(MindDataTestSliceOp, TestOpLargerStepString) {
  451. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpLargerStepString.";
  452. std::vector<std::string> labels = {"1", "2", "3", "4", "5"};
  453. std::shared_ptr<Tensor> input;
  454. Tensor::CreateFromVector(labels, TensorShape({1, 5}), &input);
  455. std::shared_ptr<Tensor> output;
  456. Slice slice1_ = Slice(0, 1);
  457. Slice slice2_ = Slice(0, 4, 2);
  458. std::vector<SliceOption> slice_options = {SliceOption(slice1_), SliceOption(slice2_)};
  459. std::unique_ptr<SliceOp> op(new SliceOp(slice_options));
  460. Status s = op->Compute(input, &output);
  461. std::vector<std::string> out = {"1", "3"};
  462. std::shared_ptr<Tensor> expected;
  463. Tensor::CreateFromVector(out, TensorShape({1, 2}), &expected);
  464. EXPECT_TRUE(s.IsOk());
  465. ASSERT_TRUE(output->shape() == expected->shape());
  466. ASSERT_TRUE(output->type() == expected->type());
  467. MS_LOG(DEBUG) << *output << std::endl;
  468. MS_LOG(DEBUG) << *expected << std::endl;
  469. ASSERT_TRUE(*output == *expected);
  470. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  471. }
  472. TEST_F(MindDataTestSliceOp, TestOpIndicesErrorString1) {
  473. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesErrorString1.";
  474. std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
  475. std::shared_ptr<Tensor> input;
  476. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  477. std::shared_ptr<Tensor> output;
  478. std::unique_ptr<SliceOp> op(new SliceOp(Slice()));
  479. Status s = op->Compute(input, &output);
  480. EXPECT_FALSE(s.IsOk());
  481. EXPECT_NE(s.ToString().find("Both indices and slices can not be empty."), std::string::npos);
  482. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  483. }
  484. TEST_F(MindDataTestSliceOp, TestOpIndicesErrorString2) {
  485. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesErrorString2.";
  486. std::vector<std::string> labels = {"1", "2", "3", "4", "5", "6", "7", "8"};
  487. std::shared_ptr<Tensor> input;
  488. Tensor::CreateFromVector(labels, TensorShape({2, 2, 2}), &input);
  489. std::shared_ptr<Tensor> output;
  490. SliceOption slice_option = SliceOption(Slice(2));
  491. std::vector<dsize_t> indices = {0};
  492. slice_option.indices_ = indices;
  493. std::unique_ptr<SliceOp> op(new SliceOp(slice_option));
  494. Status s = op->Compute(input, &output);
  495. EXPECT_FALSE(s.IsOk());
  496. EXPECT_NE(s.ToString().find("Both indices and slices can not be given."), std::string::npos);
  497. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  498. }
  499. TEST_F(MindDataTestSliceOp, TestOpIndicesErrorString3) {
  500. MS_LOG(INFO) << "Doing MindDataTestSliceOp-TestOpIndicesErrorString3.";
  501. std::vector<uint64_t> labels = {1, 2, 3, 4, 5, 6, 7, 8};
  502. std::shared_ptr<Tensor> input;
  503. Tensor::CreateFromVector(labels, TensorShape({2, 4}), &input);
  504. std::shared_ptr<Tensor> output;
  505. std::vector<dsize_t> indices = {2};
  506. std::unique_ptr<SliceOp> op(new SliceOp(SliceOption(indices)));
  507. Status s = op->Compute(input, &output);
  508. EXPECT_FALSE(s.IsOk());
  509. EXPECT_NE(s.ToString().find("Index 2 is out of bounds."), std::string::npos);
  510. MS_LOG(INFO) << "MindDataTestSliceOp-TestOp end.";
  511. }