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.cc 12 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "minddata/dataset/engine/execution_tree.h"
  17. #include <iostream>
  18. #include <string>
  19. #include "minddata/dataset/engine/datasetops/dataset_op.h"
  20. #include "minddata/dataset/engine/datasetops/shuffle_op.h"
  21. #include "minddata/dataset/engine/datasetops/device_queue_op.h"
  22. #include "minddata/dataset/util/task_manager.h"
  23. #include "minddata/dataset/engine/opt/pass.h"
  24. #include "minddata/dataset/engine/opt/pre/removal_pass.h"
  25. #ifndef ENABLE_ANDROID
  26. #include "minddata/dataset/engine/opt/pre/cache_transform_pass.h"
  27. #include "minddata/dataset/engine/opt/post/repeat_pass.h"
  28. #include "minddata/dataset/engine/opt/pre/cache_error_pass.h"
  29. #endif
  30. #include "minddata/dataset/engine/opt/pre/epoch_injection_pass.h"
  31. #include "mindspore/ccsrc/minddata/dataset/engine/opt/optional/tensor_op_fusion_pass.h"
  32. #include "minddata/dataset/engine/perf/profiling.h"
  33. #include "minddata/dataset/engine/perf/monitor.h"
  34. namespace mindspore {
  35. namespace dataset {
  36. // Constructor
  37. ExecutionTree::ExecutionTree() : id_count_(0) {
  38. tg_ = std::make_unique<TaskGroup>();
  39. tree_state_ = kDeTStateInit;
  40. prepare_flags_ = kDePrepNone;
  41. profiling_manager_ = std::make_unique<ProfilingManager>(this);
  42. optimize_ = common::GetEnv("OPTIMIZE") == "true" ? true : false;
  43. }
  44. // Destructor
  45. ExecutionTree::~ExecutionTree() {
  46. #ifdef ENABLE_TDTQUE
  47. DeviceQueueOp *op = dynamic_cast<DeviceQueueOp *>(root_.get());
  48. if (op != nullptr) {
  49. op->StopWaiting();
  50. }
  51. #endif
  52. (void)tg_->ServiceStop();
  53. }
  54. // Associates a DatasetOp with this tree. This assigns a valid node id to the operator and
  55. // provides it with a link to the tree. A node cannot form any relationships (parent/child) with
  56. // other nodes unless they are associated with the same tree.
  57. Status ExecutionTree::AssociateNode(const std::shared_ptr<DatasetOp> &op) {
  58. // If we are already a part of the tree, no-op
  59. if (op->tree_ == this) {
  60. return Status::OK();
  61. }
  62. if (tree_state_ != kDeTStateInit && tree_state_ != kDeTStateBuilding && tree_state_ != kDeTStatePrepare) {
  63. std::string err_msg =
  64. "Invalid tree state for adding a node. Current state: " + std::to_string(static_cast<int>(tree_state_)) +
  65. " Expected states: " + std::to_string(static_cast<int>(kDeTStateInit)) + " or " +
  66. std::to_string(static_cast<int>(kDeTStateBuilding)) + " or " + std::to_string(static_cast<int>(kDeTStatePrepare));
  67. RETURN_STATUS_UNEXPECTED(err_msg);
  68. }
  69. // Enter the building state if we were not already there
  70. tree_state_ = kDeTStateBuilding;
  71. // Assign an id to the operator
  72. op->set_id(id_count_);
  73. id_count_++;
  74. // Assign our tree into the op so that each op has a link back to the tree
  75. op->set_tree(this);
  76. return Status::OK();
  77. }
  78. // Sets the root node of the tree
  79. Status ExecutionTree::AssignRoot(const std::shared_ptr<DatasetOp> &op) {
  80. // Tree must be in building state before we can assign root to it
  81. if (tree_state_ != kDeTStateBuilding) {
  82. std::string err_msg =
  83. "Invalid tree state for assigning a root node. Current state: " + std::to_string(static_cast<int>(tree_state_)) +
  84. " Expected state: " + std::to_string(static_cast<int>(kDeTStateBuilding));
  85. RETURN_STATUS_UNEXPECTED(err_msg);
  86. }
  87. // If they didn't already call AssociateNode for this node before calling AssignRoot,
  88. // then do so now.
  89. if (op->operator_id_ == DatasetOp::kInvalidOperatorId) {
  90. RETURN_IF_NOT_OK(this->AssociateNode(op));
  91. }
  92. // Then add it as the root.
  93. root_ = op;
  94. return Status::OK();
  95. }
  96. // A print method typically used for debugging
  97. void ExecutionTree::Print(std::ostream &out, const std::shared_ptr<DatasetOp> &op) const {
  98. out << "Execution tree summary:\n"
  99. << "-----------------------\n";
  100. this->PrintNode(out, op == nullptr ? root_ : op, "", true, false);
  101. out << "\nExecution tree operator details:\n"
  102. << "--------------------------------\n";
  103. this->PrintNode(out, op == nullptr ? root_ : op, "", true, true);
  104. }
  105. // A helper functions for doing the recursive printing
  106. void ExecutionTree::PrintNode(std::ostream &out, const std::shared_ptr<DatasetOp> &dataset_op, std::string indent,
  107. bool last, bool detailed) const {
  108. // Decide which printer to use based on detailed arg.
  109. if (!detailed) {
  110. out << indent << "+- " << *dataset_op;
  111. indent += (last ? " " : "| ");
  112. } else {
  113. dataset_op->Print(out, detailed);
  114. }
  115. // Descend to children
  116. for (int32_t i = 0; i < dataset_op->child_.size(); ++i) {
  117. this->PrintNode(out, dataset_op->child_[i], indent, (i == (dataset_op->child_.size() - 1)), detailed);
  118. }
  119. }
  120. // Start the execution of the tree
  121. Status ExecutionTree::Launch() {
  122. // Tree must be built and prepared before it can be launched!
  123. if (tree_state_ != kDeTStateReady) {
  124. std::string err_msg =
  125. "Invalid tree state for launching tree. Current state: " + std::to_string(static_cast<int>(tree_state_)) +
  126. " Expected state: " + std::to_string(static_cast<int>(kDeTStateReady));
  127. RETURN_STATUS_UNEXPECTED(err_msg);
  128. }
  129. std::ostringstream ss;
  130. ss << *this;
  131. // Profiling infrastructures need to be initialized before Op launching
  132. if (profiling_manager_->IsProfilingEnable()) {
  133. // Setup profiling manager
  134. RETURN_IF_NOT_OK(profiling_manager_->Initialize());
  135. // Launch Monitor Thread
  136. RETURN_IF_NOT_OK(profiling_manager_->LaunchMonitor());
  137. }
  138. MS_LOG(DEBUG) << "Printing the tree before launch tasks:\n" << ss.str();
  139. for (auto itr = this->begin(); itr != this->end(); ++itr) {
  140. // An inlined operator is one that has an output connector size of 0, and it does not
  141. // require a thread to execute. Instead, the work of this operator is executed inlined
  142. // from the tree node directly above it (or in the case of a root node, it runs from within
  143. // the launching tree/user thread. Do not exec any thread for an inlined op.
  144. itr->state_ = DatasetOp::OpState::kDeOpRunning;
  145. if (!itr->inlined()) {
  146. RETURN_IF_NOT_OK(tg_->CreateAsyncTask("Op launched, OperatorId:" + std::to_string(itr->id()), std::ref(*itr)));
  147. // Set the state of the Operator as running. This only matters in Leaf ops, CacheOp and TakeOp
  148. }
  149. }
  150. tree_state_ = kDeTStateExecuting;
  151. return Status::OK();
  152. }
  153. // A function that traverse the tree in postorder then save the results in nodes
  154. void ExecutionTree::Iterator::PostOrderTraverse(const std::shared_ptr<DatasetOp> &node) {
  155. if (node == nullptr) {
  156. return;
  157. }
  158. for (int32_t i = 0; i < node->child_.size(); ++i) {
  159. PostOrderTraverse(node->child_[i]);
  160. }
  161. nodes_.push_back(node);
  162. }
  163. ExecutionTree::Iterator::Iterator(const std::shared_ptr<DatasetOp> &root) : ind_(0) {
  164. // post-order traverse the tree, if root is null, it return
  165. PostOrderTraverse(root);
  166. nodes_.emplace_back(nullptr);
  167. }
  168. // Given the number of workers, launches the worker entry function for each. Essentially a
  169. // wrapper for the TaskGroup handling that is stored inside the execution tree.
  170. Status ExecutionTree::LaunchWorkers(int32_t num_workers, std::function<Status(uint32_t)> func) {
  171. // Launch the workers
  172. for (int32_t i = 0; i < num_workers; ++i) {
  173. RETURN_IF_NOT_OK(tg_->CreateAsyncTask("Parallel Op Worker", std::bind(func, i)));
  174. }
  175. return Status::OK();
  176. }
  177. // The driver of the prepare phase of the execution tree.
  178. // Prepare phase consists of three sub phases
  179. //
  180. // 1. PrepareTreePreAction()
  181. // Compulsory transformation/action pre optimization.
  182. // For example, CacheOp Insertion
  183. //
  184. // 2. Optimize()
  185. // Optimization transformation/action, optional
  186. // For example, MapOp Fusion
  187. //
  188. // 3. PrepareTreePostAction()
  189. // Compulsory transformation/action post optimization.
  190. // For example, repeatOp inlining
  191. //
  192. // @return Status - The error code return
  193. Status ExecutionTree::Prepare(int32_t num_epochs) {
  194. num_epochs_ = num_epochs;
  195. // Pre optimization compulsory transformation
  196. RETURN_IF_NOT_OK(this->PrepareTreePreAction());
  197. // If optional optimizations are enabled
  198. if (optimize_) {
  199. RETURN_IF_NOT_OK(this->Optimize());
  200. }
  201. // Post optimization compulsory transformation
  202. RETURN_IF_NOT_OK(this->PrepareTreePostAction());
  203. // Existing transformation implementation, will be removed later
  204. RETURN_IF_NOT_OK(this->PrepareDeprecated());
  205. return Status::OK();
  206. }
  207. Status ExecutionTree::PrepareTreePreAction() {
  208. bool modified = false;
  209. std::vector<std::unique_ptr<Pass>> pre_actions;
  210. // Construct pre actions
  211. MS_LOG(INFO) << "Running pre pass loops.";
  212. #ifndef ENABLE_ANDROID
  213. pre_actions.push_back(std::make_unique<CacheErrorPass>());
  214. #endif
  215. pre_actions.push_back(std::make_unique<EpochInjectionPass>());
  216. pre_actions.push_back(std::make_unique<RemovalPass>());
  217. #ifndef ENABLE_ANDROID
  218. pre_actions.push_back(std::make_unique<CacheTransformPass>());
  219. #endif
  220. // Apply pre action passes
  221. for (auto &pass : pre_actions) {
  222. RETURN_IF_NOT_OK(pass->Run(this, &modified));
  223. }
  224. MS_LOG(INFO) << "Pre passes complete.";
  225. return Status::OK();
  226. }
  227. Status ExecutionTree::PrepareTreePostAction() {
  228. // The tree is ready to be prepared.
  229. tree_state_ = kDeTStatePrepare;
  230. bool modified = false;
  231. std::vector<std::unique_ptr<Pass>> post_actions;
  232. // Construct pre actions
  233. MS_LOG(INFO) << "Running post pass loops.";
  234. #ifndef ENABLE_ANDROID
  235. post_actions.push_back(std::make_unique<RepeatPass>());
  236. #endif
  237. // Apply post action passes
  238. for (auto &pass : post_actions) {
  239. RETURN_IF_NOT_OK(pass->Run(this, &modified));
  240. }
  241. MS_LOG(INFO) << "Post passes complete.";
  242. return Status::OK();
  243. }
  244. Status ExecutionTree::Optimize() {
  245. // Vector of optimizations, currently only 1, add more as necessary
  246. std::vector<std::unique_ptr<NodePass>> optimizations;
  247. optimizations.push_back(std::make_unique<TensorOpFusionPass>());
  248. // vector of flags for each optimization
  249. std::vector<bool> modified(optimizations.size(), false);
  250. for (auto i = 0; i < optimizations.size(); i++) {
  251. auto m = false;
  252. optimizations[i]->Run(this, &m);
  253. modified[i] = m;
  254. }
  255. return Status::OK();
  256. }
  257. // The driver of the prepare phase of the execution tree. The prepare phase will recursively
  258. // walk the tree to perform modifications to the tree or specific nodes within the tree to get
  259. // it ready for execution.
  260. //
  261. // This driver is deprecated.
  262. Status ExecutionTree::PrepareDeprecated() {
  263. // Tree must be in pending prepare state before we can assign root to it
  264. if (tree_state_ != kDeTStatePrepare) {
  265. std::string err_msg =
  266. "Invalid tree state for preparing the tree. Current state: " + std::to_string(static_cast<int>(tree_state_)) +
  267. " Expected state: " + std::to_string(static_cast<int>(kDeTStatePrepare));
  268. RETURN_STATUS_UNEXPECTED(err_msg);
  269. }
  270. if (root_ == nullptr) {
  271. RETURN_STATUS_UNEXPECTED("Please assign one operator as the root of this tree.");
  272. }
  273. // Start the recursive prepare
  274. RETURN_IF_NOT_OK(this->PrepareNode(root_));
  275. tree_state_ = kDeTStateReady;
  276. return Status::OK();
  277. }
  278. // Recursive function used during prepare phase to visit a node and drive any pre- and post-
  279. // node actions during a tree walk.
  280. Status ExecutionTree::PrepareNode(const std::shared_ptr<DatasetOp> &dataset_op) {
  281. // execute PreAction
  282. RETURN_IF_NOT_OK(dataset_op->PrepareNodePreAction());
  283. // Before going down into children, make any prepare flags updates based on this operator.
  284. uint32_t op_prep_flags = dataset_op->PrepareFlags();
  285. BitSet(&prepare_flags_, op_prep_flags);
  286. // Now, descend to children
  287. for (const auto &i : dataset_op->child_) {
  288. RETURN_IF_NOT_OK(this->PrepareNode(i));
  289. }
  290. // No more children, now we execute any prepare actions before going back up the
  291. // the tree on recursive function
  292. RETURN_IF_NOT_OK(dataset_op->PrepareNodePostAction());
  293. // Then clear the flags from this op now that we have prepared it.
  294. BitClear(&prepare_flags_, op_prep_flags);
  295. return Status::OK();
  296. }
  297. } // namespace dataset
  298. } // namespace mindspore