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

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