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 13 kB

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