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.

functionalize_cond.h 2.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright 2020-2021 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_LITE_TOOLS_OPTIMIZER_GRAPH_FUNCTIONALIZE_COND_H_
  17. #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_FUNCTIONALIZE_COND_H_
  18. #include <string>
  19. #include <set>
  20. #include <vector>
  21. #include <map>
  22. #include "backend/optimizer/common/pass.h"
  23. #include "tools/converter/converter_flags.h"
  24. #include "tools/optimizer/common/gllo_utils.h"
  25. #include "tools/optimizer/graph/functionalize_control_op_pass.h"
  26. using mindspore::lite::converter::FmkType;
  27. namespace mindspore::opt {
  28. typedef enum { kThenBranch = 0, kElseBranch = 1 } BranchType;
  29. // Functionalize all the switch-merge nodes of a loop-free graph into single switch node.
  30. // Precondition: While loops must have been functionalized.
  31. class FunctionalizeCond {
  32. public:
  33. FunctionalizeCond(FuncGraphPtr fg, CNodePtr merge_node) : fg_(fg), merge_node_(merge_node) {}
  34. virtual ~FunctionalizeCond() = default;
  35. STATUS Process();
  36. private:
  37. STATUS GetSwitchBranchType(const CNodePtr &switch_cnode, BranchType *branch_type);
  38. STATUS BranchSubGraphAddNodes(const FuncGraphPtr &graph, const AnfNodePtr &root_node, BranchType branch_type);
  39. FuncGraphPtr CreateBranchGraph(const AnfNodePtr &node, std::string name, BranchType branch_type);
  40. int PosInInputNodes(const CNodePtr &node);
  41. STATUS IdentifySubgraphInput(const FuncGraphPtr &graph, std::string graph_name);
  42. CNodePtr CreateNewIf(const FuncGraphPtr &else_branch, const FuncGraphPtr &then_branch);
  43. STATUS VerifyPredictNode();
  44. FuncGraphPtr fg_ = nullptr;
  45. CNodePtr merge_node_ = nullptr;
  46. CNodePtr pred_node_ = nullptr;
  47. std::vector<CNodePtr> input_nodes_{};
  48. std::vector<AnfNodePtr> pred_nodes_{};
  49. };
  50. } // namespace mindspore::opt
  51. #endif // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_FUNCTIONALIZE_COND_H_