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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 DATASET_ENGINE_OPT_PASS_H_
  17. #define DATASET_ENGINE_OPT_PASS_H_
  18. #include <memory>
  19. #include <queue>
  20. #include "dataset/engine/execution_tree.h"
  21. #include "dataset/util/status.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. class BatchOp;
  25. class MapOp;
  26. class ProjectOp;
  27. class RenameOp;
  28. class FilterOp;
  29. class SkipOp;
  30. class ShuffleOp;
  31. class GeneratorOp;
  32. class MindRecordOp;
  33. class TFReaderOp;
  34. class TakeOp;
  35. class ZipOp;
  36. class DeviceQueueOp;
  37. class ImageFolderOp;
  38. // The base class Pass is the basic unit of tree transformation.
  39. // The actual implementation of the passes will be derived from here.
  40. class Pass : public std::enable_shared_from_this<Pass> {
  41. public:
  42. // Run the transformation pass against the execution tree.
  43. // @param tree - Pointer to the execution tree to be transformed.
  44. // @param modified - Pointer to the modified flag,
  45. virtual Status Run(ExecutionTree *tree, bool *modified) = 0;
  46. };
  47. // TreePass is a basic Pass class which performs transformation on ExecutionTree directly.
  48. class TreePass : public Pass {
  49. public:
  50. /// \brief Run the transformation pass against the execution tree.
  51. /// \param[inout] tree Pointer to the execution tree to be transformed.
  52. /// \param[inout] modified Indicate if the tree was modified
  53. Status Run(ExecutionTree *tree, bool *modified) final;
  54. /// \brief Derived classes may implement the runOnTree function to implement tree transformation.
  55. /// "modified" flag needs to be set to true if tree is modified during the pass execution.
  56. /// \param[inout] tree The tree to operate on.
  57. /// \param[inout] Indicate of the tree was modified.
  58. /// \return Status The error code return
  59. virtual Status RunOnTree(ExecutionTree *tree, bool *modified) { return Status::OK(); }
  60. };
  61. // NodePass is a basic Pass class which performs transformation on Node visiting.
  62. // NodePass implements Visitor design pattern.
  63. class NodePass : public Pass {
  64. public:
  65. // Tree traversal order
  66. enum Order { DFS, BFS };
  67. // Constructor
  68. // Default DFS traversal
  69. explicit NodePass(Order order = Order::DFS) { traversalOrder_ = order; }
  70. ~NodePass() = default;
  71. /// \brief Run the transformation pass against the execution tree
  72. /// \param[inout] tree Pointer to the execution tree to be transformed
  73. /// \param[inout] modified Indicator if the tree was changed
  74. Status Run(ExecutionTree *tree, bool *modified) final;
  75. /// \brief Derived classes may implement the PreRunOnNode function to implement any initial visit work on the way down
  76. /// a tree traversal. "modified" flag needs to be set to true if tree is modified during the pass execution
  77. /// \param[in] node The node being visited
  78. /// \param[out] modified Indicator if the node was changed at all
  79. /// \return Status The error code return
  80. virtual Status PreRunOnNode(std::shared_ptr<DatasetOp> node, bool *modified) { return Status::OK(); }
  81. /// \brief Derived classes may implement the RunOnNode function to implement node level tree transformation
  82. /// "modified" flag needs to be set to true if tree is modified during the pass execution
  83. /// \param[in] node The node being visited
  84. /// \param[out] modified Indicator if the node was changed at all.
  85. /// \return Status The error code return
  86. virtual Status RunOnNode(std::shared_ptr<DatasetOp> node, bool *modified) { return Status::OK(); }
  87. // Visit methods to be overridden.
  88. // Note that member template can not be virtual, any op which wants to work with NodePass should declare RunOnNode
  89. // of its own type and override "Accept" from DatasetOp.
  90. virtual Status RunOnNode(std::shared_ptr<BatchOp> node, bool *modified);
  91. virtual Status RunOnNode(std::shared_ptr<MapOp> node, bool *modified);
  92. virtual Status RunOnNode(std::shared_ptr<ProjectOp> node, bool *modified);
  93. virtual Status RunOnNode(std::shared_ptr<RenameOp> node, bool *modified);
  94. virtual Status RunOnNode(std::shared_ptr<FilterOp> node, bool *modified);
  95. virtual Status RunOnNode(std::shared_ptr<SkipOp> node, bool *modified);
  96. virtual Status RunOnNode(std::shared_ptr<ShuffleOp> node, bool *modified);
  97. virtual Status RunOnNode(std::shared_ptr<GeneratorOp> node, bool *modified);
  98. virtual Status RunOnNode(std::shared_ptr<MindRecordOp> node, bool *modified);
  99. virtual Status RunOnNode(std::shared_ptr<TFReaderOp> node, bool *modified);
  100. virtual Status RunOnNode(std::shared_ptr<TakeOp> node, bool *modified);
  101. virtual Status RunOnNode(std::shared_ptr<ZipOp> node, bool *modified);
  102. virtual Status RunOnNode(std::shared_ptr<DeviceQueueOp> node, bool *modified);
  103. virtual Status RunOnNode(std::shared_ptr<ImageFolderOp> node, bool *modified);
  104. private:
  105. // Helper function to perform DFS visit
  106. Status DFSNodeVisit(std::shared_ptr<DatasetOp> node, bool *modified);
  107. // Helper function to perform BFS visit
  108. Status BFSNodeVisit(std::shared_ptr<DatasetOp> root, bool *modified);
  109. // Tree traversal order of the NodePass
  110. Order traversalOrder_;
  111. };
  112. } // namespace dataset
  113. } // namespace mindspore
  114. #endif // DATASET_ENGINE_OPT_PASS_H_