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.

branch_culling.h 5.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_FRONTEND_OPTIMIZER_IRPASS_BRANCH_CULLING_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_BRANCH_CULLING_H_
  18. #include <vector>
  19. #include <algorithm>
  20. #include "ir/func_graph.h"
  21. #include "ir/func_graph_cloner.h"
  22. #include "frontend/optimizer/optimizer_caller.h"
  23. #include "ir/pattern_matcher.h"
  24. #include "frontend/operator/ops.h"
  25. #include "frontend/optimizer/irpass.h"
  26. namespace mindspore {
  27. namespace opt {
  28. namespace irpass {
  29. // {prim::kPrimSwitch, true, X, Y}
  30. // {prim::kPrimSwitch, false, X, Y}
  31. class SwitchSimplify : public OptimizerCaller {
  32. public:
  33. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  34. PatternNode<AnfNodePtr> cond, true_br, false_br;
  35. auto SwitchSimplLambda = [&node, &cond, &true_br, &false_br]() -> AnfNodePtr {
  36. auto cond_value_ = GetValue<bool>(GetValueNode(cond.GetNode(node)));
  37. if (cond_value_) {
  38. return true_br.GetNode(node);
  39. }
  40. return false_br.GetNode(node);
  41. };
  42. MATCH_REPLACE_LAMBDA_IF(node, PPrimitive(prim::kPrimSwitch, cond, true_br, false_br), SwitchSimplLambda,
  43. cond.CheckFunc(IsValueNode<BoolImm>, node));
  44. return nullptr;
  45. }
  46. };
  47. // {prim::kPrimTupleGetItem, {prim::kPrimSwitch, X0, X1, X2}, C} =>
  48. // {prim::kPrimSwitch, X0, {prim::kPrimTupleGetItem, X1, C}, {prim::kPrimTupleGetItem, X2, C}}
  49. class FloatTupleGetItemSwitch : public OptimizerCaller {
  50. public:
  51. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  52. PatternNode<AnfNodePtr> cond, true_br, false_br, x;
  53. MATCH_REPLACE_IF(node,
  54. PPrimitive(prim::kPrimTupleGetItem, PPrimitive(prim::kPrimSwitch, cond, true_br, false_br), x),
  55. PPrimitive(prim::kPrimSwitch, cond, PPrimitive(prim::kPrimTupleGetItem, true_br, x),
  56. PPrimitive(prim::kPrimTupleGetItem, false_br, x)),
  57. x.CheckFunc(IsVNode, node));
  58. return nullptr;
  59. }
  60. };
  61. // {prim::kPrimEnvGetItem, {prim::kPrimSwitch, X1, X2, X3}, X4, X5} =>
  62. // {prim::kPrimSwitch, X1, {prim::kPrimEnvGetItem, X2, X4, X5}, {prim::kPrimEnvGetItem, X3, X4, X5}}
  63. class FloatEnvGetItemSwitch : public OptimizerCaller {
  64. public:
  65. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  66. PatternNode<AnfNodePtr> cond, true_br, false_br, x, x2;
  67. MATCH_REPLACE(node,
  68. PPrimitive(prim::kPrimEnvGetItem, PPrimitive(prim::kPrimSwitch, cond, true_br, false_br), x, x2),
  69. PPrimitive(prim::kPrimSwitch, cond, PPrimitive(prim::kPrimEnvGetItem, true_br, x, x2),
  70. PPrimitive(prim::kPrimEnvGetItem, false_br, x, x2)));
  71. return nullptr;
  72. }
  73. };
  74. namespace internal {
  75. FuncGraphPtr TransformGraphCondTrueBranchNodes(const FuncGraphPtr &graph, const AnfNodePtr &cond);
  76. FuncGraphPtr TransformGraphCondFalseBranchNodes(const FuncGraphPtr &graph, const AnfNodePtr &cond);
  77. // block_nodes[0]: condition node
  78. // block_nodes[1]: true branch node
  79. // block_nodes[2]: false branch node
  80. // branch_output_abs[0]: true branch abstract
  81. // branch_output_abs[1]: false branch abstract
  82. AnfNodePtr TransformMergeBranches(const std::vector<AnfNodePtr> &block_nodes,
  83. const std::vector<AbstractBasePtr> &branch_output_abs,
  84. const FuncGraphPtr &func_graph);
  85. } // namespace internal
  86. // {{prim::kPrimSwitch, X, G1, G2}, Xs}
  87. class ConvertSwitchReplacement {
  88. public:
  89. ConvertSwitchReplacement() = default;
  90. virtual ~ConvertSwitchReplacement() = default;
  91. bool operator()(const FuncGraphPtr &root, const OptimizerPtr &optimizer) {
  92. AnfNodePtr ret = root->get_return();
  93. MS_EXCEPTION_IF_NULL(ret);
  94. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  95. bool change = false;
  96. for (auto &node : all_nodes) {
  97. if (CheckSwitchWrapNode(node)) {
  98. TransformSwitchBranchReplace(node);
  99. change = true;
  100. }
  101. }
  102. return change;
  103. }
  104. private:
  105. // Determine whether there are graphs inside the branch graph.
  106. bool CheckSwitchBranch(const AnfNodePtr &node);
  107. // Determine whether node matches {{prim::kPrimSwitch, X, G1, G2}, Xs}.
  108. bool CheckSwitchWrapNode(const AnfNodePtr &node);
  109. // Replace switch branch.
  110. void TransformSwitchBranchReplace(const AnfNodePtr &node);
  111. };
  112. // {prim::kPrimSwitch, {prim::kPrimDepend, ValueNode, X}, G1, G2} ->
  113. // {prim::kPrimDepend, {prim::kPrimSwitch, ValueNode, G1, G2}, X}
  114. class ExchangeSwitchDependValue : public OptimizerCaller {
  115. public:
  116. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  117. if (!node->isa<CNode>() || node->func_graph() == nullptr) {
  118. return nullptr;
  119. }
  120. ScopePtr scope = node->cast<CNodePtr>()->scope();
  121. ScopeGuard scope_guard(scope);
  122. PatternNode<AnfNodePtr> cond, true_br, false_br, v, x;
  123. MATCH_REPLACE_IF(node, PPrimitive(prim::kPrimSwitch, PPrimitive(prim::kPrimDepend, v, x), true_br, false_br),
  124. PPrimitive(prim::kPrimDepend, PPrimitive(prim::kPrimSwitch, v, true_br, false_br), x),
  125. IsVNode(v.GetNode(node)));
  126. return nullptr;
  127. }
  128. };
  129. } // namespace irpass
  130. } // namespace opt
  131. } // namespace mindspore
  132. #endif // #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_BRANCH_CULLING_H_