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.h 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_OPT_PASS_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_OPT_PASS_H_
  18. #include <memory>
  19. #include <queue>
  20. #include "minddata/dataset/engine/execution_tree.h"
  21. #include "minddata/dataset/util/status.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. class BatchOp;
  25. class MapOp;
  26. class ProjectOp;
  27. class RenameOp;
  28. class SkipOp;
  29. class ShuffleOp;
  30. class AlbumOp;
  31. class RandomDataOp;
  32. class RepeatOp;
  33. class TakeOp;
  34. class ZipOp;
  35. class DeviceQueueOp;
  36. class ImageFolderOp;
  37. class MnistOp;
  38. class ManifestOp;
  39. class CifarOp;
  40. class VOCOp;
  41. class CocoOp;
  42. class CelebAOp;
  43. class EpochCtrlOp;
  44. class BuildVocabOp;
  45. class ConcatOp;
  46. #ifndef ENABLE_ANDROID
  47. class MindRecordOp;
  48. class TFReaderOp;
  49. class CacheOp;
  50. class CacheMergeOp;
  51. class CacheLookupOp;
  52. class BuildSentencePieceVocabOp;
  53. class ClueOp;
  54. class CsvOp;
  55. class TextFileOp;
  56. #endif
  57. #ifdef ENABLE_PYTHON
  58. class FilterOp;
  59. class GeneratorOp;
  60. #endif
  61. // The base class Pass is the basic unit of tree transformation.
  62. // The actual implementation of the passes will be derived from here.
  63. class Pass : public std::enable_shared_from_this<Pass> {
  64. public:
  65. // Run the transformation pass against the execution tree.
  66. // @param tree - Pointer to the execution tree to be transformed.
  67. // @param modified - Pointer to the modified flag,
  68. virtual Status Run(ExecutionTree *tree, bool *modified) = 0;
  69. virtual ~Pass() = default;
  70. };
  71. // TreePass is a basic Pass class which performs transformation on ExecutionTree directly.
  72. class TreePass : public Pass {
  73. public:
  74. /// \brief Run the transformation pass against the execution tree.
  75. /// \param[inout] tree Pointer to the execution tree to be transformed.
  76. /// \param[inout] modified Indicate if the tree was modified
  77. Status Run(ExecutionTree *tree, bool *modified) final;
  78. /// \brief Derived classes may implement the runOnTree function to implement tree transformation.
  79. /// "modified" flag needs to be set to true if tree is modified during the pass execution.
  80. /// \param[inout] tree The tree to operate on.
  81. /// \param[inout] Indicate of the tree was modified.
  82. /// \return Status The error code return
  83. virtual Status RunOnTree(ExecutionTree *tree, bool *modified) { return Status::OK(); }
  84. };
  85. // NodePass is a basic Pass class which performs transformation on Node visiting.
  86. // NodePass implements Visitor design pattern.
  87. class NodePass : public Pass {
  88. public:
  89. // Tree traversal order
  90. enum Order { DFS, BFS };
  91. // Constructor
  92. // Default DFS traversal
  93. explicit NodePass(Order order = Order::DFS) { traversalOrder_ = order; }
  94. ~NodePass() = default;
  95. /// \brief Run the transformation pass against the execution tree
  96. /// \param[inout] tree Pointer to the execution tree to be transformed
  97. /// \param[inout] modified Indicator if the tree was changed
  98. Status Run(ExecutionTree *tree, bool *modified) final;
  99. /// \brief Derived classes may implement the PreRunOnNode function to implement any initial visit work on the way down
  100. /// a tree traversal. "modified" flag needs to be set to true if tree is modified during the pass execution
  101. /// \param[in] node The node being visited
  102. /// \param[out] modified Indicator if the node was changed at all
  103. /// \return Status The error code return
  104. virtual Status PreRunOnNode(std::shared_ptr<DatasetOp> node, bool *modified) { return Status::OK(); }
  105. /// \brief Derived classes may implement the RunOnNode function to implement node level tree transformation
  106. /// "modified" flag needs to be set to true if tree is modified during the pass execution
  107. /// \param[in] node The node being visited
  108. /// \param[out] modified Indicator if the node was changed at all.
  109. /// \return Status The error code return
  110. virtual Status RunOnNode(std::shared_ptr<DatasetOp> node, bool *modified) { return Status::OK(); }
  111. // Visit methods to be overridden.
  112. // Note that member template can not be virtual, any op which wants to work with NodePass should declare RunOnNode
  113. // of its own type and override "Accept" from DatasetOp.
  114. virtual Status RunOnNode(std::shared_ptr<BatchOp> node, bool *modified);
  115. virtual Status RunOnNode(std::shared_ptr<MapOp> node, bool *modified);
  116. virtual Status RunOnNode(std::shared_ptr<ProjectOp> node, bool *modified);
  117. virtual Status RunOnNode(std::shared_ptr<RenameOp> node, bool *modified);
  118. virtual Status RunOnNode(std::shared_ptr<SkipOp> node, bool *modified);
  119. virtual Status RunOnNode(std::shared_ptr<ShuffleOp> node, bool *modified);
  120. virtual Status RunOnNode(std::shared_ptr<RandomDataOp> node, bool *modified);
  121. virtual Status RunOnNode(std::shared_ptr<AlbumOp> node, bool *modified);
  122. virtual Status RunOnNode(std::shared_ptr<TakeOp> node, bool *modified);
  123. virtual Status RunOnNode(std::shared_ptr<ZipOp> node, bool *modified);
  124. virtual Status RunOnNode(std::shared_ptr<DeviceQueueOp> node, bool *modified);
  125. virtual Status RunOnNode(std::shared_ptr<ImageFolderOp> node, bool *modified);
  126. virtual Status RunOnNode(std::shared_ptr<MnistOp> node, bool *modified);
  127. virtual Status RunOnNode(std::shared_ptr<CifarOp> node, bool *modified);
  128. virtual Status RunOnNode(std::shared_ptr<CocoOp> node, bool *modified);
  129. virtual Status RunOnNode(std::shared_ptr<CelebAOp> node, bool *modified);
  130. virtual Status RunOnNode(std::shared_ptr<RepeatOp> node, bool *modified);
  131. virtual Status RunOnNode(std::shared_ptr<EpochCtrlOp> node, bool *modified);
  132. virtual Status PreRunOnNode(std::shared_ptr<RepeatOp> node, bool *modified);
  133. virtual Status PreRunOnNode(std::shared_ptr<EpochCtrlOp> node, bool *modified);
  134. virtual Status PreRunOnNode(std::shared_ptr<BuildVocabOp> node, bool *modified);
  135. virtual Status PreRunOnNode(std::shared_ptr<ZipOp> node, bool *modified);
  136. virtual Status PreRunOnNode(std::shared_ptr<MapOp> node, bool *modified);
  137. virtual Status PreRunOnNode(std::shared_ptr<ConcatOp> node, bool *modified);
  138. #ifndef ENABLE_ANDROID
  139. virtual Status RunOnNode(std::shared_ptr<MindRecordOp> node, bool *modified);
  140. virtual Status RunOnNode(std::shared_ptr<TFReaderOp> node, bool *modified);
  141. virtual Status RunOnNode(std::shared_ptr<CacheMergeOp> node, bool *modified);
  142. virtual Status RunOnNode(std::shared_ptr<CacheLookupOp> node, bool *modified);
  143. virtual Status RunOnNode(std::shared_ptr<CacheOp> node, bool *modified);
  144. virtual Status RunOnNode(std::shared_ptr<ClueOp> node, bool *modified);
  145. virtual Status RunOnNode(std::shared_ptr<CsvOp> node, bool *modified);
  146. virtual Status RunOnNode(std::shared_ptr<TextFileOp> node, bool *modified);
  147. virtual Status PreRunOnNode(std::shared_ptr<CacheOp> node, bool *modified);
  148. virtual Status PreRunOnNode(std::shared_ptr<CacheMergeOp> node, bool *modified);
  149. virtual Status PreRunOnNode(std::shared_ptr<BuildSentencePieceVocabOp> node, bool *modified);
  150. #endif
  151. #ifdef ENABLE_PYTHON
  152. virtual Status RunOnNode(std::shared_ptr<FilterOp> node, bool *modified);
  153. virtual Status RunOnNode(std::shared_ptr<ManifestOp> node, bool *modified);
  154. virtual Status RunOnNode(std::shared_ptr<GeneratorOp> node, bool *modified);
  155. virtual Status RunOnNode(std::shared_ptr<VOCOp> node, bool *modified);
  156. virtual Status PreRunOnNode(std::shared_ptr<FilterOp> node, bool *modified);
  157. #endif
  158. private:
  159. // Helper function to perform DFS visit
  160. Status DFSNodeVisit(std::shared_ptr<DatasetOp> node, bool *modified);
  161. // Helper function to perform BFS visit
  162. Status BFSNodeVisit(std::shared_ptr<DatasetOp> root, bool *modified);
  163. // Tree traversal order of the NodePass
  164. Order traversalOrder_;
  165. };
  166. } // namespace dataset
  167. } // namespace mindspore
  168. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_OPT_PASS_H_