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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. AnfNodePtr TransformMergeBranches(const AnfNodePtr &true_output_node, const AnfNodePtr &false_output_node,
  78. const AbstractBasePtr &true_graph_output_abs,
  79. const AbstractBasePtr &false_graph_output_abs, const AnfNodePtr &cond,
  80. const FuncGraphPtr &func_graph);
  81. } // namespace internal
  82. // {{prim::kPrimSwitch, X, G1, G2}, Xs}
  83. class ConvertSwitchReplacement : public OptimizerCaller {
  84. public:
  85. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  86. if (!node->isa<CNode>() || node->func_graph() == nullptr) {
  87. return nullptr;
  88. }
  89. PatternNode<AnfNodePtr> cond, true_br, false_br;
  90. auto ConvertSwitchLambda = [&node, &cond, &true_br, &false_br]() -> AnfNodePtr {
  91. auto g1_ = GetValueNode<FuncGraphPtr>(true_br.GetNode(node));
  92. auto g2_ = GetValueNode<FuncGraphPtr>(false_br.GetNode(node));
  93. auto x_ = cond.GetNode(node);
  94. // for switch replace method, only graphs without graph inside can be replaced
  95. for (auto &item : g1_->value_nodes()) {
  96. auto value_node = item.first;
  97. if (IsValueNode<FuncGraph>(value_node)) {
  98. return nullptr;
  99. }
  100. }
  101. for (auto &item : g2_->value_nodes()) {
  102. auto value_node = item.first;
  103. if (IsValueNode<FuncGraph>(value_node)) {
  104. return nullptr;
  105. }
  106. }
  107. auto true_output = g1_->output()->abstract();
  108. auto false_output = g2_->output()->abstract();
  109. auto trans_g1 = internal::TransformGraphCondTrueBranchNodes(g1_, x_);
  110. auto trans_g2 = internal::TransformGraphCondFalseBranchNodes(g2_, x_);
  111. std::vector<AnfNodePtr> params;
  112. auto fg = node->func_graph();
  113. auto cloned_g1 = InlineClone(trans_g1, fg, params);
  114. auto cloned_g2 = InlineClone(trans_g2, fg, params);
  115. auto nnode = internal::TransformMergeBranches(cloned_g1, cloned_g2, true_output, false_output, x_, fg);
  116. return nnode;
  117. };
  118. MATCH_REPLACE_LAMBDA_IF(
  119. node, PCNode(PPrimitive(prim::kPrimSwitch, cond, true_br, false_br)).MinExtraNodes(0), ConvertSwitchLambda,
  120. true_br.CheckFunc(IsValueNode<FuncGraph>, node) && false_br.CheckFunc(IsValueNode<FuncGraph>, node));
  121. return nullptr;
  122. }
  123. };
  124. } // namespace irpass
  125. } // namespace opt
  126. } // namespace mindspore
  127. #endif // #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_BRANCH_CULLING_H_