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

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