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

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