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.

pass.cc 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "minddata/dataset/engine/opt/pass.h"
  17. #include "minddata/dataset/engine/datasetops/batch_op.h"
  18. #include "minddata/dataset/engine/datasetops/build_vocab_op.h"
  19. #include "minddata/dataset/engine/datasetops/build_sentence_piece_vocab_op.h"
  20. #include "minddata/dataset/engine/datasetops/cache_op.h"
  21. #include "minddata/dataset/engine/datasetops/cache_merge_op.h"
  22. #include "minddata/dataset/engine/datasetops/cache_lookup_op.h"
  23. #include "minddata/dataset/engine/datasetops/dataset_op.h"
  24. #include "minddata/dataset/engine/datasetops/device_queue_op.h"
  25. #include "minddata/dataset/engine/datasetops/epoch_ctrl_op.h"
  26. #include "minddata/dataset/engine/datasetops/map_op/map_op.h"
  27. #include "minddata/dataset/engine/datasetops/project_op.h"
  28. #include "minddata/dataset/engine/datasetops/rename_op.h"
  29. #include "minddata/dataset/engine/datasetops/repeat_op.h"
  30. #include "minddata/dataset/engine/datasetops/skip_op.h"
  31. #include "minddata/dataset/engine/datasetops/shuffle_op.h"
  32. #include "minddata/dataset/engine/datasetops/source/celeba_op.h"
  33. #include "minddata/dataset/engine/datasetops/source/cifar_op.h"
  34. #include "minddata/dataset/engine/datasetops/source/coco_op.h"
  35. #include "minddata/dataset/engine/datasetops/source/manifest_op.h"
  36. #ifndef ENABLE_ANDROID
  37. #include "minddata/dataset/engine/datasetops/source/mindrecord_op.h"
  38. #endif
  39. #include "minddata/dataset/engine/datasetops/source/mnist_op.h"
  40. #include "minddata/dataset/engine/datasetops/source/random_data_op.h"
  41. #ifndef ENABLE_ANDROID
  42. #include "minddata/dataset/engine/datasetops/source/tf_reader_op.h"
  43. #endif
  44. #include "minddata/dataset/engine/datasetops/source/voc_op.h"
  45. #ifdef ENABLE_PYTHON
  46. #include "minddata/dataset/engine/datasetops/filter_op.h"
  47. #include "minddata/dataset/engine/datasetops/source/generator_op.h"
  48. #endif
  49. #include "minddata/dataset/engine/datasetops/source/image_folder_op.h"
  50. #include "minddata/dataset/engine/datasetops/take_op.h"
  51. #include "minddata/dataset/engine/datasetops/zip_op.h"
  52. namespace mindspore {
  53. namespace dataset {
  54. // Driver method for TreePass
  55. Status TreePass::Run(ExecutionTree *tree, bool *modified) {
  56. if (tree == nullptr || modified == nullptr) {
  57. return Status(StatusCode::kUnexpectedError, "Null pointer passed to TreePass");
  58. }
  59. return this->RunOnTree(tree, modified);
  60. }
  61. // Driver method for NodePass
  62. Status NodePass::Run(ExecutionTree *tree, bool *modified) {
  63. if (tree == nullptr || modified == nullptr) {
  64. return Status(StatusCode::kUnexpectedError, "Null pointer passed to NodePass");
  65. }
  66. std::shared_ptr<DatasetOp> root = tree->root();
  67. if (traversalOrder_ == Order::DFS) {
  68. // DFS
  69. return DFSNodeVisit(root, modified);
  70. } else if (traversalOrder_ == Order::BFS) {
  71. // BFS
  72. return BFSNodeVisit(root, modified);
  73. }
  74. return Status::OK();
  75. }
  76. // Helper function to perform DFS visit
  77. Status NodePass::DFSNodeVisit(std::shared_ptr<DatasetOp> node, bool *modified) {
  78. RETURN_IF_NOT_OK(node->PreAccept(this, modified));
  79. for (const auto &c : node->Children()) {
  80. RETURN_IF_NOT_OK(this->DFSNodeVisit(c, modified));
  81. }
  82. return node->Accept(this, modified);
  83. }
  84. // Helper function to perform BFS visit
  85. Status NodePass::BFSNodeVisit(std::shared_ptr<DatasetOp> root, bool *modified) {
  86. // Initialize bfs queue with root
  87. std::queue<std::shared_ptr<DatasetOp>> bfsQueue;
  88. bfsQueue.push(root);
  89. // BFS loop
  90. while (!bfsQueue.empty()) {
  91. // Pop the front of the bfs queue
  92. auto curNode = bfsQueue.front();
  93. bfsQueue.pop();
  94. // Run node pass
  95. RETURN_IF_NOT_OK(curNode->Accept(this, modified));
  96. // Push children into bfs queue
  97. for (const auto &c : curNode->Children()) {
  98. bfsQueue.push(c);
  99. }
  100. }
  101. return Status::OK();
  102. }
  103. Status NodePass::RunOnNode(std::shared_ptr<BatchOp> node, bool *modified) {
  104. // Fallback to base class visitor by default
  105. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  106. }
  107. Status NodePass::RunOnNode(std::shared_ptr<MapOp> node, bool *modified) {
  108. // Fallback to base class visitor by default
  109. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  110. }
  111. Status NodePass::RunOnNode(std::shared_ptr<ProjectOp> node, bool *modified) {
  112. // Fallback to base class visitor by default
  113. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  114. }
  115. Status NodePass::RunOnNode(std::shared_ptr<RenameOp> node, bool *modified) {
  116. // Fallback to base class visitor by default
  117. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  118. }
  119. Status NodePass::RunOnNode(std::shared_ptr<SkipOp> node, bool *modified) {
  120. // Fallback to base class visitor by default
  121. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  122. }
  123. Status NodePass::RunOnNode(std::shared_ptr<ShuffleOp> node, bool *modified) {
  124. // Fallback to base class visitor by default
  125. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  126. }
  127. #ifndef ENABLE_ANDROID
  128. Status NodePass::RunOnNode(std::shared_ptr<MindRecordOp> node, bool *modified) {
  129. // Fallback to base class visitor by default
  130. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  131. }
  132. Status NodePass::RunOnNode(std::shared_ptr<TFReaderOp> node, bool *modified) {
  133. // Fallback to base class visitor by default
  134. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  135. }
  136. #endif
  137. #ifdef ENABLE_PYTHON
  138. Status NodePass::RunOnNode(std::shared_ptr<FilterOp> node, bool *modified) {
  139. // Fallback to base class visitor by default
  140. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  141. }
  142. Status NodePass::RunOnNode(std::shared_ptr<GeneratorOp> node, bool *modified) {
  143. // Fallback to base class visitor by default
  144. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  145. }
  146. Status NodePass::RunOnNode(std::shared_ptr<ManifestOp> node, bool *modified) {
  147. // Fallback to base class visitor by default
  148. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  149. }
  150. Status NodePass::RunOnNode(std::shared_ptr<VOCOp> node, bool *modified) {
  151. // Fallback to base class visitor by default
  152. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  153. }
  154. #endif
  155. Status NodePass::RunOnNode(std::shared_ptr<RandomDataOp> node, bool *modified) {
  156. // Fallback to base class visitor by default
  157. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  158. }
  159. Status NodePass::RunOnNode(std::shared_ptr<TakeOp> node, bool *modified) {
  160. // Fallback to base class visitor by default
  161. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  162. }
  163. Status NodePass::RunOnNode(std::shared_ptr<ZipOp> node, bool *modified) {
  164. // Fallback to base class visitor by default
  165. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  166. }
  167. Status NodePass::RunOnNode(std::shared_ptr<DeviceQueueOp> node, bool *modified) {
  168. // Fallback to base class visitor by default
  169. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  170. }
  171. Status NodePass::RunOnNode(std::shared_ptr<ImageFolderOp> node, bool *modified) {
  172. // Fallback to base class visitor by default
  173. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  174. }
  175. Status NodePass::RunOnNode(std::shared_ptr<CacheOp> node, bool *modified) {
  176. // Fallback to base class visitor by default
  177. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  178. }
  179. Status NodePass::RunOnNode(std::shared_ptr<MnistOp> node, bool *modified) {
  180. // Fallback to base class visitor by default
  181. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  182. }
  183. Status NodePass::RunOnNode(std::shared_ptr<CifarOp> node, bool *modified) {
  184. // Fallback to base class visitor by default
  185. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  186. }
  187. Status NodePass::RunOnNode(std::shared_ptr<CelebAOp> node, bool *modified) {
  188. // Fallback to base class visitor by default
  189. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  190. }
  191. Status NodePass::RunOnNode(std::shared_ptr<CocoOp> node, bool *modified) {
  192. // Fallback to base class visitor by default
  193. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  194. }
  195. Status NodePass::RunOnNode(std::shared_ptr<RepeatOp> node, bool *modified) {
  196. // Fallback to base class visitor by default
  197. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  198. }
  199. Status NodePass::RunOnNode(std::shared_ptr<CacheMergeOp> node, bool *modified) {
  200. // Fallback to base class visitor by default
  201. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  202. }
  203. Status NodePass::RunOnNode(std::shared_ptr<CacheLookupOp> node, bool *modified) {
  204. // Fallback to base class visitor by default
  205. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  206. }
  207. Status NodePass::RunOnNode(std::shared_ptr<EpochCtrlOp> node, bool *modified) {
  208. // Fallback to base class visitor by default
  209. return RunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  210. }
  211. Status NodePass::PreRunOnNode(std::shared_ptr<RepeatOp> node, bool *modified) {
  212. // Fallback to base class visitor by default
  213. return PreRunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  214. }
  215. Status NodePass::PreRunOnNode(std::shared_ptr<CacheOp> node, bool *modified) {
  216. // Fallback to base class visitor by default
  217. return PreRunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  218. }
  219. Status NodePass::PreRunOnNode(std::shared_ptr<CacheMergeOp> node, bool *modified) {
  220. // Fallback to base class visitor by default
  221. return PreRunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  222. }
  223. Status NodePass::PreRunOnNode(std::shared_ptr<EpochCtrlOp> node, bool *modified) {
  224. // Fallback to base class visitor by default
  225. return PreRunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  226. }
  227. Status NodePass::PreRunOnNode(std::shared_ptr<BuildVocabOp> node, bool *modified) {
  228. // Fallback to base class visitor by default
  229. return PreRunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  230. }
  231. Status NodePass::PreRunOnNode(std::shared_ptr<BuildSentencePieceVocabOp> node, bool *modified) {
  232. // Fallback to base class visitor by default
  233. return PreRunOnNode(std::static_pointer_cast<DatasetOp>(node), modified);
  234. }
  235. } // namespace dataset
  236. } // namespace mindspore