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.

tree_modifying_function_test.cc 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /**
  2. * Copyright 2021 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 <memory>
  17. #include <string>
  18. #include "common/common.h"
  19. #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
  20. #include "minddata/dataset/engine/ir/datasetops/skip_node.h"
  21. #include "minddata/dataset/engine/ir/datasetops/take_node.h"
  22. #include "minddata/dataset/engine/ir/datasetops/repeat_node.h"
  23. #include "minddata/dataset/include/datasets.h"
  24. using namespace mindspore::dataset;
  25. class MindDataTestTreeModifying : public UT::DatasetOpTesting {
  26. public:
  27. MindDataTestTreeModifying() = default;
  28. };
  29. TEST_F(MindDataTestTreeModifying, AppendChild) {
  30. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-AppendChild";
  31. /*
  32. * Input tree:
  33. * ds4
  34. * / \
  35. * ds3 ds2
  36. * |
  37. * ds1
  38. *
  39. * ds4->AppendChild(ds6) yields this tree
  40. *
  41. * _ ds4 _
  42. * / | \
  43. * ds3 ds2 ds6
  44. * |
  45. * ds1
  46. */
  47. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  48. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  49. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  50. std::shared_ptr<Dataset> ds6 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  51. std::shared_ptr<Dataset> ds3 = ds1->Take(10);
  52. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  53. Status rc;
  54. std::shared_ptr<DatasetNode> root = ds4->IRNode();
  55. auto ir_tree = std::make_shared<TreeAdapter>();
  56. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  57. EXPECT_EQ(rc, Status::OK());
  58. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  59. std::shared_ptr<DatasetNode> ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  60. // You can inspect the plan by sending *ir_tree->RootIRNode() to std::cout
  61. std::shared_ptr<DatasetNode> node_to_insert = ds6->IRNode();
  62. rc = ds4_node->AppendChild(node_to_insert);
  63. EXPECT_EQ(rc, Status::OK());
  64. EXPECT_TRUE( ds4_node->Children()[2] == node_to_insert);
  65. }
  66. TEST_F(MindDataTestTreeModifying, InsertChildAt01) {
  67. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-InsertChildAt01";
  68. /*
  69. * Input tree:
  70. * ds4
  71. * / \
  72. * ds3 ds2
  73. * | |
  74. * ds1 ds5
  75. *
  76. * Case 1: ds4->InsertChildAt(1, ds6) yields this tree
  77. *
  78. * _ ds4 _
  79. * / | \
  80. * ds3 ds6 ds2
  81. * | |
  82. * ds1 ds5
  83. *
  84. * Case 2: ds4->InsertChildAt(0, ds6) yields this tree
  85. *
  86. * _ ds4 _
  87. * / | \
  88. * ds6 ds3 ds2
  89. * | |
  90. * ds1 ds5
  91. *
  92. * Case 3: ds4->InsertChildAt(2, ds6) yields this tree
  93. *
  94. * _ ds4 _
  95. * / | \
  96. * ds3 ds2 ds6
  97. * | |
  98. * ds1 ds5
  99. *
  100. */
  101. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  102. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  103. std::shared_ptr<Dataset> ds3 = ds1->Take(10);
  104. std::shared_ptr<Dataset> ds5 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  105. std::shared_ptr<Dataset> ds2 = ds5->Repeat(4);
  106. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  107. Status rc;
  108. std::shared_ptr<DatasetNode> root = ds4->IRNode();
  109. auto ir_tree = std::make_shared<TreeAdapter>();
  110. // Case 1:
  111. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  112. EXPECT_EQ(rc, Status::OK());
  113. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  114. std::shared_ptr<DatasetNode> ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  115. std::shared_ptr<Dataset> ds6 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  116. std::shared_ptr<DatasetNode> ds6_to_insert = ds6->IRNode();
  117. std::shared_ptr<DatasetNode> ds2_node = ds4_node->Children()[1];
  118. rc = ds4_node->InsertChildAt(1, ds6_to_insert);
  119. EXPECT_EQ(rc, Status::OK());
  120. EXPECT_TRUE( ds4_node->Children()[1] == ds6_to_insert);
  121. EXPECT_TRUE( ds4_node->Children()[2] == ds2_node);
  122. // Case 2:
  123. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  124. EXPECT_EQ(rc, Status::OK());
  125. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  126. ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  127. ds6 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  128. ds6_to_insert = ds6->IRNode();
  129. std::shared_ptr<DatasetNode> ds3_node = ds4_node->Children()[0];
  130. rc = ds4_node->InsertChildAt(0, ds6_to_insert);
  131. EXPECT_EQ(rc, Status::OK());
  132. EXPECT_TRUE( ds4_node->Children()[0] == ds6_to_insert);
  133. EXPECT_TRUE( ds4_node->Children()[1] == ds3_node);
  134. // Case 3:
  135. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  136. EXPECT_EQ(rc, Status::OK());
  137. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  138. ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  139. ds6 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  140. ds6_to_insert = ds6->IRNode();
  141. rc = ds4_node->InsertChildAt(2, ds6_to_insert);
  142. EXPECT_EQ(rc, Status::OK());
  143. EXPECT_TRUE( ds4_node->Children()[2] == ds6_to_insert);
  144. }
  145. TEST_F(MindDataTestTreeModifying, InsertChildAt04) {
  146. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-InsertChildAt04";
  147. /*
  148. * Input tree:
  149. * ds4
  150. * / \
  151. * ds3 ds2
  152. * | |
  153. * ds1 ds5
  154. *
  155. */
  156. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  157. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  158. std::shared_ptr<Dataset> ds3 = ds1->Take(10);
  159. std::shared_ptr<Dataset> ds5 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  160. std::shared_ptr<Dataset> ds2 = ds5->Repeat(4);
  161. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  162. Status rc;
  163. std::shared_ptr<DatasetNode> root = ds4->IRNode();
  164. auto ir_tree = std::make_shared<TreeAdapter>();
  165. // Case 4: ds4->InsertChildAt(3, ds6) raises an error
  166. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  167. EXPECT_EQ(rc, Status::OK());
  168. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  169. std::shared_ptr<DatasetNode> ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  170. std::shared_ptr<Dataset> ds6 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  171. std::shared_ptr<DatasetNode> ds6_to_insert = ds6->IRNode();
  172. std::shared_ptr<DatasetNode> ds3_node = ds4_node->Children()[0];
  173. std::shared_ptr<DatasetNode> ds2_node = ds4_node->Children()[1];
  174. rc = ds4_node->InsertChildAt(3, ds6_to_insert);
  175. EXPECT_NE(rc, Status::OK());
  176. EXPECT_TRUE( ds4_node->Children()[0] == ds3_node);
  177. EXPECT_TRUE( ds4_node->Children()[1] == ds2_node);
  178. // Case 5: ds4->InsertChildAt(-1, ds6) raises an error
  179. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  180. EXPECT_EQ(rc, Status::OK());
  181. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  182. ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  183. ds6 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  184. ds6_to_insert = ds6->IRNode();
  185. ds3_node = ds4_node->Children()[0];
  186. ds2_node = ds4_node->Children()[1];
  187. rc = ds4_node->InsertChildAt(-1, ds6_to_insert);
  188. EXPECT_NE(rc, Status::OK());
  189. EXPECT_TRUE( ds4_node->Children()[0] == ds3_node);
  190. EXPECT_TRUE( ds4_node->Children()[1] == ds2_node);
  191. }
  192. TEST_F(MindDataTestTreeModifying, InsertAbove01) {
  193. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-InsertAbove01";
  194. /*
  195. * Insert the input <node> above this node
  196. * Input tree:
  197. * ds4
  198. * / \
  199. * ds3 ds2
  200. * |
  201. * ds1
  202. *
  203. * Case 1: If we want to insert a new node ds5 between ds4 and ds3, use
  204. * ds3->InsertAbove(ds5)
  205. *
  206. * ds4
  207. * / \
  208. * ds5 ds2
  209. * |
  210. * ds3
  211. * |
  212. * ds1
  213. *
  214. * Case 2: Likewise, ds2->InsertAbove(ds6) yields
  215. *
  216. * ds4
  217. * / \
  218. * ds3 ds6
  219. * | |
  220. * ds1 ds2
  221. *
  222. * Case 3: We can insert a new node between ds3 and ds1 by ds1->InsertAbove(ds7)
  223. *
  224. * ds4
  225. * / \
  226. * ds3 ds2
  227. * |
  228. * ds7
  229. * |
  230. * ds1
  231. *
  232. */
  233. // Case 1
  234. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  235. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  236. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  237. std::shared_ptr<Dataset> ds3 = ds1->Take(10);
  238. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  239. Status rc;
  240. std::shared_ptr<DatasetNode> root = ds4->IRNode();
  241. auto ir_tree = std::make_shared<TreeAdapter>();
  242. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  243. EXPECT_EQ(rc, Status::OK());
  244. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  245. std::shared_ptr<DatasetNode> ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  246. std::shared_ptr<DatasetNode> ds3_node = ds4_node->Children()[0];
  247. std::shared_ptr<SkipNode> ds5_to_insert = std::make_shared<SkipNode>(nullptr, 1);
  248. rc = ds3_node->InsertAbove(ds5_to_insert);
  249. EXPECT_EQ(rc, Status::OK());
  250. EXPECT_TRUE(ds5_to_insert->Children()[0] == ds3_node);
  251. EXPECT_TRUE( ds4_node->Children()[0] == ds5_to_insert);
  252. }
  253. TEST_F(MindDataTestTreeModifying, InsertAbove02) {
  254. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-InsertAbove02";
  255. // Case 2
  256. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  257. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  258. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  259. std::shared_ptr<Dataset> ds3 = ds1->Take(10);
  260. std::shared_ptr<Dataset> ds4 = ds3 + ds2;
  261. Status rc;
  262. std::shared_ptr<DatasetNode> root = ds4->IRNode();
  263. auto ir_tree = std::make_shared<TreeAdapter>();
  264. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  265. EXPECT_EQ(rc, Status::OK());
  266. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  267. std::shared_ptr<DatasetNode> ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  268. std::shared_ptr<DatasetNode> ds2_node = ds4_node->Children()[1];
  269. std::shared_ptr<TakeNode> ds6_to_insert = std::make_shared<TakeNode>(nullptr, 12);
  270. rc = ds2_node->InsertAbove(ds6_to_insert);
  271. EXPECT_EQ(rc, Status::OK());
  272. EXPECT_TRUE(ds6_to_insert->Children()[0] == ds2_node);
  273. EXPECT_TRUE( ds4_node->Children()[1] == ds6_to_insert);
  274. }
  275. TEST_F(MindDataTestTreeModifying, InsertAbove03) {
  276. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-InsertAbove03";
  277. // Case 3
  278. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  279. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  280. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  281. std::shared_ptr<Dataset> ds3 = ds1->Take(10);
  282. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  283. Status rc;
  284. std::shared_ptr<DatasetNode> root = ds4->IRNode();
  285. auto ir_tree = std::make_shared<TreeAdapter>();
  286. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  287. EXPECT_EQ(rc, Status::OK());
  288. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  289. std::shared_ptr<DatasetNode> ds4_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  290. std::shared_ptr<DatasetNode> ds3_node = ds4_node->Children()[0];
  291. std::shared_ptr<DatasetNode> ds1_node = ds3_node->Children()[0];
  292. std::shared_ptr<RepeatNode> ds7_to_insert = std::make_shared<RepeatNode>(nullptr, 3);
  293. rc = ds1_node->InsertAbove(ds7_to_insert);
  294. EXPECT_TRUE(ds7_to_insert->Children()[0] == ds1_node);
  295. EXPECT_TRUE( ds3_node->Children()[0] == ds7_to_insert);
  296. }
  297. TEST_F(MindDataTestTreeModifying, Drop01) {
  298. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-Drop01";
  299. /*
  300. * Drop() detaches this node from the tree it is in. Calling Drop() from a standalone node is a no-op.
  301. *
  302. * Input tree:
  303. * ds10
  304. * / \
  305. * ds9 ds6
  306. * | / | \
  307. * ds8 ds5 ds4 ds1
  308. * | / \
  309. * ds7 ds3 ds2
  310. *
  311. * Case 1: When the node has no child and no sibling, Drop() detaches the node from its tree.
  312. *
  313. * ds7->Drop() yields the tree below:
  314. *
  315. * ds10
  316. * / \
  317. * ds9 ds6
  318. * | / | \
  319. * ds8 ds5 ds4 ds1
  320. * / \
  321. * ds3 ds2
  322. *
  323. * Case 2: When the node has one child and no sibling, Drop() detaches the node from its tree and the node's child
  324. * becomes its parent's child.
  325. *
  326. * ds8->Drop() yields the tree below:
  327. *
  328. * ds10
  329. * / \
  330. * ds9 ds6
  331. * | / | \
  332. * ds7 ds5 ds4 ds1
  333. * / \
  334. * ds3 ds2
  335. *
  336. */
  337. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  338. std::shared_ptr<Dataset> ds7 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  339. std::shared_ptr<Dataset> ds8 = ds7->Take(20);
  340. std::shared_ptr<Dataset> ds9 = ds8->Skip(1);
  341. std::shared_ptr<Dataset> ds3 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  342. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  343. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  344. std::shared_ptr<Dataset> ds6 = ds4->Take(13);
  345. std::shared_ptr<Dataset> ds10 = ds9 + ds6;
  346. Status rc;
  347. std::shared_ptr<DatasetNode> root = ds10->IRNode();
  348. auto ir_tree = std::make_shared<TreeAdapter>();
  349. // Case 1
  350. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  351. EXPECT_EQ(rc, Status::OK());
  352. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  353. std::shared_ptr<DatasetNode> ds10_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  354. std::shared_ptr<DatasetNode> ds9_node = ds10_node->Children()[0];
  355. std::shared_ptr<DatasetNode> ds8_node = ds9_node->Children()[0];
  356. std::shared_ptr<DatasetNode> ds7_node = ds8_node->Children()[0];
  357. rc = ds7_node->Drop();
  358. EXPECT_EQ(rc, Status::OK());
  359. // ds8 becomes a childless node
  360. EXPECT_TRUE(ds8_node->Children().empty());
  361. EXPECT_TRUE(ds7_node->Children().empty());
  362. // Case 2
  363. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  364. EXPECT_EQ(rc, Status::OK());
  365. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  366. ds10_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  367. ds9_node = ds10_node->Children()[0];
  368. ds8_node = ds9_node->Children()[0];
  369. ds7_node = ds8_node->Children()[0];
  370. rc = ds8_node->Drop();
  371. EXPECT_EQ(rc, Status::OK());
  372. // ds7 becomes a child of ds9
  373. EXPECT_TRUE(ds9_node->Children()[0] == ds7_node);
  374. EXPECT_TRUE(ds8_node->Children().empty());
  375. }
  376. TEST_F(MindDataTestTreeModifying, Drop03) {
  377. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-Drop03";
  378. /* Case 3: When the node has more than one child and no sibling, Drop() detaches the node from its tree and the node's
  379. * children become its parent's children.
  380. *
  381. * When the input tree is
  382. * ds10
  383. * / \
  384. * ds9 ds6
  385. * | |
  386. * ds8 ds4
  387. * | / \
  388. * ds7 ds3 ds2
  389. *
  390. *
  391. * ds4->Drop() will raise an error because we cannot add the children of an n-ary operator (ds4) to a unary operator
  392. * (ds6).
  393. *
  394. */
  395. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  396. std::shared_ptr<Dataset> ds7 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  397. std::shared_ptr<Dataset> ds8 = ds7->Take(20);
  398. std::shared_ptr<Dataset> ds9 = ds8->Skip(1);
  399. std::shared_ptr<Dataset> ds3 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  400. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  401. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  402. std::shared_ptr<Dataset> ds6 = ds4->Take(13);
  403. std::shared_ptr<Dataset> ds10 = ds9 + ds6;
  404. Status rc;
  405. std::shared_ptr<DatasetNode> root = ds10->IRNode();
  406. auto ir_tree = std::make_shared<TreeAdapter>();
  407. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  408. EXPECT_EQ(rc, Status::OK());
  409. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  410. std::shared_ptr<DatasetNode> ds10_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  411. std::shared_ptr<DatasetNode> ds6_node = ds10_node->Children()[1];
  412. std::shared_ptr<DatasetNode> ds4_node = ds6_node->Children()[0];
  413. std::shared_ptr<DatasetNode> ds3_node = ds4_node->Children()[0];
  414. std::shared_ptr<DatasetNode> ds2_node = ds4_node->Children()[1];
  415. rc = ds4_node->Drop();
  416. EXPECT_NE(rc, Status::OK());
  417. }
  418. TEST_F(MindDataTestTreeModifying, Drop04) {
  419. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-Drop04";
  420. /* Case 4: When the node has no child but has siblings, Drop() detaches the node from its tree and its siblings will be
  421. * squeezed left.
  422. *
  423. * Input tree:
  424. * ds10
  425. * / \
  426. * ds9 ds6
  427. * | / | \
  428. * ds8 ds5 ds4 ds1
  429. * | / \
  430. * ds7 ds3 ds2
  431. *
  432. * ds5->Drop() yields the tree below:
  433. *
  434. * ds10
  435. * / \
  436. * ds9 ds6
  437. * | / \
  438. * ds8 ds4 ds1
  439. * | / \
  440. * ds7 ds3 ds2
  441. *
  442. */
  443. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  444. std::shared_ptr<Dataset> ds7 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  445. std::shared_ptr<Dataset> ds8 = ds7->Take(20);
  446. std::shared_ptr<Dataset> ds9 = ds8->Skip(1);
  447. std::shared_ptr<Dataset> ds3 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  448. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  449. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  450. std::shared_ptr<Dataset> ds5 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  451. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  452. std::shared_ptr<Dataset> ds6 = ds5->Concat({ds4, ds1});
  453. std::shared_ptr<Dataset> ds10 = ds9 + ds6;
  454. Status rc;
  455. std::shared_ptr<DatasetNode> root = ds10->IRNode();
  456. auto ir_tree = std::make_shared<TreeAdapter>();
  457. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  458. EXPECT_EQ(rc, Status::OK());
  459. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  460. std::shared_ptr<DatasetNode> ds10_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  461. std::shared_ptr<DatasetNode> ds6_node = ds10_node->Children()[1];
  462. std::shared_ptr<DatasetNode> ds5_node = ds6_node->Children()[0];
  463. std::shared_ptr<DatasetNode> ds4_node = ds6_node->Children()[1];
  464. EXPECT_TRUE(ds5_node->IsDataSource());
  465. EXPECT_TRUE(ds6_node->IsNaryOperator());
  466. rc = ds5_node->Drop();
  467. EXPECT_EQ(rc, Status::OK());
  468. EXPECT_TRUE(ds6_node->Children().size() == 2);
  469. EXPECT_TRUE(ds6_node->Children()[0] == ds4_node);
  470. EXPECT_TRUE(ds5_node->Children().empty());
  471. }
  472. TEST_F(MindDataTestTreeModifying, Drop05) {
  473. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-Drop05";
  474. /*
  475. * Case 5: When the node has only one child but has siblings, Drop() detaches the node from its tree and the node's
  476. * children become its parent's children.
  477. *
  478. * Input tree:
  479. * ds10
  480. * / \
  481. * ds9 ds6
  482. * | / | \
  483. * ds8 ds5 ds4 ds1
  484. * | |
  485. * ds7 ds3
  486. *
  487. * ds4->Drop() yields the tree below:
  488. *
  489. * ds10
  490. * / \
  491. * ds9 ds6
  492. * | / | \
  493. * ds8 ds5 ds3 ds1
  494. * |
  495. * ds7
  496. *
  497. */
  498. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  499. std::shared_ptr<Dataset> ds7 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  500. std::shared_ptr<Dataset> ds8 = ds7->Take(20);
  501. std::shared_ptr<Dataset> ds9 = ds8->Skip(1);
  502. std::shared_ptr<Dataset> ds3 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  503. std::shared_ptr<Dataset> ds4 = ds3->Skip(1);
  504. std::shared_ptr<Dataset> ds5 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  505. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  506. std::shared_ptr<Dataset> ds6 = ds5->Concat({ds4, ds1});
  507. std::shared_ptr<Dataset> ds10 = ds9 + ds6;
  508. Status rc;
  509. std::shared_ptr<DatasetNode> root = ds10->IRNode();
  510. auto ir_tree = std::make_shared<TreeAdapter>();
  511. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  512. EXPECT_EQ(rc, Status::OK());
  513. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  514. std::shared_ptr<DatasetNode> ds10_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  515. std::shared_ptr<DatasetNode> ds6_node = ds10_node->Children()[1];
  516. std::shared_ptr<DatasetNode> ds4_node = ds6_node->Children()[1];
  517. std::shared_ptr<DatasetNode> ds3_node = ds4_node->Children()[0];
  518. rc = ds4_node->Drop();
  519. EXPECT_EQ(rc, Status::OK());
  520. EXPECT_TRUE(ds6_node->Children().size() == 3);
  521. EXPECT_TRUE(ds6_node->Children()[1] == ds3_node);
  522. EXPECT_TRUE(ds4_node->Children().empty());
  523. }
  524. TEST_F(MindDataTestTreeModifying, Drop06) {
  525. MS_LOG(INFO) << "Doing MindDataTestTreeModifying-Drop06";
  526. /*
  527. * Case 6: When the node has more than one child and more than one sibling, Drop() will raise an error.
  528. * If we want to drop ds4 from the input tree, ds4->Drop() will not work. We will have to do it
  529. * with a combination of Drop(), InsertChildAt()
  530. *
  531. * Input tree:
  532. * ds10
  533. * / \
  534. * ds9 ds6
  535. * | / | \
  536. * ds8 ds5 ds4 ds1
  537. * | / \
  538. * ds7 ds3 ds2
  539. *
  540. * If we want to form this tree below:
  541. *
  542. * ds10
  543. * / \
  544. * ds9 ds6_____
  545. * | / | | \
  546. * ds8 ds5 ds3 ds2 ds1
  547. * |
  548. * ds7
  549. *
  550. */
  551. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  552. std::shared_ptr<Dataset> ds7 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  553. std::shared_ptr<Dataset> ds8 = ds7->Take(20);
  554. std::shared_ptr<Dataset> ds9 = ds8->Skip(1);
  555. std::shared_ptr<Dataset> ds3 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  556. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  557. std::shared_ptr<Dataset> ds4 = ds3->Concat({ds2});
  558. std::shared_ptr<Dataset> ds5 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  559. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
  560. std::shared_ptr<Dataset> ds6 = ds5->Concat({ds4, ds1}); // ds1 is put after (ds5, ds4)!!!
  561. std::shared_ptr<Dataset> ds10 = ds9 + ds6;
  562. Status rc;
  563. std::shared_ptr<DatasetNode> root = ds10->IRNode();
  564. auto ir_tree = std::make_shared<TreeAdapter>();
  565. rc = ir_tree->Compile(root); // Compile adds a new RootNode to the top of the tree
  566. EXPECT_EQ(rc, Status::OK());
  567. // Descend two levels as Compile adds the root node and the epochctrl node on top of ds4
  568. std::shared_ptr<DatasetNode> ds10_node = ir_tree->RootIRNode()->Children()[0]->Children()[0];
  569. std::shared_ptr<DatasetNode> ds6_node = ds10_node->Children()[1];
  570. std::shared_ptr<DatasetNode> ds4_node = ds6_node->Children()[1];
  571. rc = ds4_node->Drop();
  572. EXPECT_NE(rc, Status::OK());
  573. }