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.

if_pass.cc 5.5 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "tools/optimizer/graph/if_pass.h"
  17. #include <vector>
  18. #include <memory>
  19. #include <algorithm>
  20. #include "mindspore/lite/include/errorcode.h"
  21. #include "mindspore/lite/src/ops/primitive_c.h"
  22. #include "tools/anf_importer/import_from_meta_graphT.h"
  23. #include "tools/optimizer/common/gllo_utils.h"
  24. #include "src/ops/primitive_c.h"
  25. #include "schema/inner/model_generated.h"
  26. #include "src/tensor.h"
  27. #include "src/common/log_adapter.h"
  28. #include "src/ops/switch.h"
  29. #include "src/ops/partial.h"
  30. namespace mindspore::opt {
  31. ValueNodePtr IfPass::GetSwitchAnfPrim() {
  32. std::unique_ptr<schema::PrimitiveT> switch_primitiveT(new (std::nothrow) schema::PrimitiveT);
  33. if (switch_primitiveT == nullptr) {
  34. MS_LOG(ERROR) << "new switch_primitiveT failed";
  35. return nullptr;
  36. }
  37. switch_primitiveT->value.type = schema::PrimitiveType_Switch;
  38. switch_primitiveT->value.value = new (std::nothrow) schema::SwitchT;
  39. if (switch_primitiveT->value.value == nullptr) {
  40. MS_LOG(ERROR) << "new MakeTupleT failed";
  41. return nullptr;
  42. }
  43. auto partial_prim = std::make_shared<lite::Partial>(switch_primitiveT.release());
  44. ValueNodePtr partial_anf_prim = NewValueNode(partial_prim);
  45. return partial_anf_prim;
  46. }
  47. void IfPass::ReplaceInput(const std::vector<AnfNodePtr> &node_list, AnfNodePtr new_input_cnode, std::string para_name) {
  48. for (auto &node : node_list) {
  49. if (utils::isa<CNodePtr>(node)) {
  50. auto cnode = utils::cast<CNodePtr>(node);
  51. for (size_t k = 0; k < cnode->inputs().size(); k++) {
  52. if (!utils::isa<ParameterPtr>(cnode->input(k))) {
  53. continue;
  54. }
  55. auto para_input = utils::cast<ParameterPtr>(cnode->input(k));
  56. if (para_input->name() == para_name) {
  57. cnode->set_input(k, new_input_cnode);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. bool IfPass::Run(const FuncGraphPtr &graph) {
  64. auto node_list = TopoSort(graph->get_return());
  65. for (auto &node : node_list) {
  66. if (!utils::isa<CNodePtr>(node)) {
  67. continue;
  68. }
  69. if (opt::GetCNodeType(node) != schema::PrimitiveType_If) {
  70. continue;
  71. }
  72. auto if_cnode = node->cast<CNodePtr>();
  73. MS_ASSERT(if_cnode != nullptr);
  74. if (if_cnode->inputs().size() < kIfMinInputSize) {
  75. MS_LOG(ERROR) << "if input is not right.";
  76. return false;
  77. }
  78. // the order is fixed.
  79. auto then_vnode = if_cnode->input(kIfThenIndex);
  80. auto else_vnode = if_cnode->input(kIfElseIndex);
  81. auto cond_vnode = if_cnode->input(kIfCondIndex);
  82. // else_vnode->cast<ValueNodePtr>()->set_value()
  83. auto then_fg = GetValueNode<std::shared_ptr<FuncGraph>>(then_vnode);
  84. auto else_fg = GetValueNode<std::shared_ptr<FuncGraph>>(else_vnode);
  85. if (then_fg == nullptr || else_fg == nullptr) {
  86. MS_LOG(ERROR) << "Get value as func_graph failed.";
  87. lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(RET_FAILED);
  88. return false;
  89. }
  90. // create then partial cnode
  91. std::vector<AnfNodePtr> then_partial_op_inputs{then_vnode};
  92. // create else partial cnode
  93. std::vector<AnfNodePtr> else_partial_op_inputs{else_vnode};
  94. // add if op input to then_cnode and else_cnode
  95. then_partial_op_inputs.insert(then_partial_op_inputs.end(), if_cnode->inputs().begin() + kIfMinInputSize,
  96. if_cnode->inputs().end());
  97. else_partial_op_inputs.insert(else_partial_op_inputs.end(), if_cnode->inputs().begin() + kIfMinInputSize,
  98. if_cnode->inputs().end());
  99. auto then_partial_node = graph->NewCNode(then_partial_op_inputs);
  100. then_partial_node->set_fullname_with_scope(node->fullname_with_scope() + "-partial-if-then");
  101. then_partial_node->set_abstract(then_fg->output()->abstract());
  102. auto else_partial_node = graph->NewCNode(else_partial_op_inputs);
  103. else_partial_node->set_fullname_with_scope(node->fullname_with_scope() + "-partial-if-else");
  104. // create switch cnode
  105. ValueNodePtr switch_anf_primitive = GetSwitchAnfPrim();
  106. if (switch_anf_primitive == nullptr) {
  107. MS_LOG(ERROR) << "GetSwitchAnfPrim failed.";
  108. return false;
  109. }
  110. // insert switch node
  111. std::vector<AnfNodePtr> switch_op_inputs = {switch_anf_primitive, then_partial_node, else_partial_node, cond_vnode};
  112. switch_op_inputs.insert(switch_op_inputs.end(), if_cnode->inputs().begin() + kIfMinInputSize,
  113. if_cnode->inputs().end());
  114. auto switch_cnode = graph->NewCNode(switch_op_inputs);
  115. switch_cnode->set_fullname_with_scope(node->fullname_with_scope() + "-Switch");
  116. switch_cnode->set_abstract(if_cnode->abstract());
  117. // create then partial cnode
  118. auto manager = graph->manager();
  119. auto node_users = manager->node_users()[if_cnode];
  120. for (auto &node_user : node_users) {
  121. manager->SetEdge(node_user.first, node_user.second, switch_cnode);
  122. }
  123. }
  124. return true;
  125. }
  126. } // namespace mindspore::opt