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.

execution_tree_test.cc 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Copyright 2019 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 <string>
  17. #include "minddata/dataset/util/circular_pool.h"
  18. #include "minddata/dataset/core/client.h"
  19. #include "minddata/dataset/engine/execution_tree.h"
  20. #include "minddata/dataset/engine/datasetops/shuffle_op.h"
  21. #include "minddata/dataset/engine/datasetops/source/tf_reader_op.h"
  22. #include "minddata/dataset/engine/jagged_connector.h"
  23. #include "common/common.h"
  24. #include "gtest/gtest.h"
  25. #include "utils/log_adapter.h"
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::MsLogLevel::INFO;
  30. class MindDataTestExecutionTree : public UT::DatasetOpTesting {
  31. public:
  32. };
  33. // Construct some tree nodes and play with them
  34. TEST_F(MindDataTestExecutionTree, TestExecutionTree1) {
  35. MS_LOG(INFO) << "Doing MindDataTestExecutionTree1.";
  36. // Start with an empty execution tree
  37. auto my_tree = std::make_shared<ExecutionTree>();
  38. ASSERT_NE(my_tree, nullptr);
  39. uint32_t shuffle_size = 32;
  40. uint32_t connector_size = 8;
  41. std::shared_ptr<ShuffleOp> leaf_op1 = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  42. ASSERT_NE(leaf_op1, nullptr);
  43. my_tree->AssociateNode(leaf_op1);
  44. shuffle_size = 16;
  45. std::shared_ptr<ShuffleOp> leaf_op2 = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  46. ASSERT_NE(leaf_op2, nullptr);
  47. my_tree->AssociateNode(leaf_op2);
  48. shuffle_size = 8;
  49. std::shared_ptr<ShuffleOp> parent_op = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  50. ASSERT_NE(parent_op, nullptr);
  51. my_tree->AssociateNode(parent_op);
  52. // It's up to you if you want to use move semantic or not here.
  53. // By using move, we transfer ownership of the child to the parent.
  54. // If you do not use move,
  55. // we get a reference count bump to the pointer and you have
  56. // your own pointer to it, plus the parent has a copy of the pointer.
  57. parent_op->AddChild(std::move(leaf_op1));
  58. parent_op->AddChild(std::move(leaf_op2));
  59. shuffle_size = 4;
  60. std::shared_ptr<DatasetOp> root_op = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  61. my_tree->AssignRoot(root_op);
  62. root_op->AddChild(parent_op);
  63. ASSERT_NE(root_op, nullptr);
  64. // Testing Iterator
  65. MS_LOG(INFO) << "Testing Tree Iterator from root.";
  66. for (auto itr = my_tree->begin(); itr != my_tree->end(); ++itr) {
  67. itr->Print(std::cout, false);
  68. }
  69. MS_LOG(INFO) << "Finished testing Tree Iterator from root.";
  70. MS_LOG(INFO) << "Testing Tree Iterator from parentOp.";
  71. for (auto itr = my_tree->begin(parent_op); itr != my_tree->end(); ++itr) {
  72. itr->Print(std::cout, false);
  73. }
  74. MS_LOG(INFO) << "Finished testing Tree Iterator from parentOp.";
  75. // At this point, since move semantic was used,
  76. // I don't have any operator access myself now.
  77. // Ownership is fully transferred into the tree.
  78. // explicitly drive tree destruction rather than
  79. // wait for descoping (to examine in debugger
  80. // just to see it work)
  81. my_tree.reset();
  82. MS_LOG(INFO) << "Done.";
  83. }
  84. // Construct some tree nodes and play with them
  85. TEST_F(MindDataTestExecutionTree, TestExecutionTree2) {
  86. MS_LOG(INFO) << "Doing MindDataTestExecutionTree2.";
  87. Status rc;
  88. auto my_tree = std::make_shared<ExecutionTree>();
  89. std::string dataset_path = datasets_root_path_ + "/testDataset1/testDataset1.data";
  90. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  91. auto op_connector_size = config_manager->op_connector_size();
  92. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  93. std::vector<std::string> columns_to_load = {};
  94. std::vector<std::string> files = {dataset_path};
  95. std::shared_ptr<TFReaderOp> my_tfreader_op = std::make_shared<TFReaderOp>(
  96. 1, 2, 0, files, std::move(schema), op_connector_size, columns_to_load, false, 1, 0, false);
  97. rc = my_tfreader_op->Init();
  98. ASSERT_OK(rc);
  99. rc = my_tree->AssociateNode(my_tfreader_op);
  100. ASSERT_OK(rc);
  101. rc = my_tree->AssignRoot(my_tfreader_op);
  102. ASSERT_OK(rc);
  103. // prepare the tree
  104. rc = my_tree->Prepare();
  105. ASSERT_OK(rc);
  106. // Launch the tree execution to kick off threads
  107. // and start running the pipeline
  108. MS_LOG(INFO) << "Launching my tree.";
  109. rc = my_tree->Launch();
  110. ASSERT_OK(rc);
  111. // Simulate a parse of data from our pipeline.
  112. std::shared_ptr<DatasetOp> root_node = my_tree->root();
  113. // Start the loop of reading from our pipeline using iterator
  114. MS_LOG(INFO) << "Testing DatasetIterator in testTree2.";
  115. DatasetIterator di(my_tree);
  116. TensorRow buffer;
  117. rc = di.FetchNextTensorRow(&buffer);
  118. EXPECT_TRUE(rc.IsOk());
  119. while (!buffer.empty()) {
  120. rc = di.FetchNextTensorRow(&buffer);
  121. EXPECT_TRUE(rc.IsOk());
  122. }
  123. }
  124. // Construct some tree nodes and play with them
  125. TEST_F(MindDataTestExecutionTree, TestExecutionTree3) { MS_LOG(INFO) << "Doing MindDataTestExecutionTree3."; }