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.

node_pass.cc 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright 2019 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 "backend/optimizer/common/node_pass.h"
  17. #include <unordered_set>
  18. #include <unordered_map>
  19. #include <deque>
  20. #include "ir/anf.h"
  21. #include "ir/func_graph.h"
  22. #include "ir/manager.h"
  23. #include "backend/session/anf_runtime_algorithm.h"
  24. namespace mindspore {
  25. namespace opt {
  26. const size_t kSwitchBranchIndex = 2;
  27. const size_t kCallArgsIndex = 1;
  28. const size_t kPartialArgsIndex = 1;
  29. void AddOutputAndCallerToMap(const CNodePtr &cnode, std::unordered_map<AnfNodePtr, AnfNodePtr> *out_caller_map) {
  30. MS_EXCEPTION_IF_NULL(cnode);
  31. MS_EXCEPTION_IF_NULL(out_caller_map);
  32. auto inputs = cnode->inputs();
  33. if (AnfAlgo::CheckPrimitiveType(cnode, prim::kPrimSwitch)) {
  34. auto partial_node = dyn_cast<CNode>(inputs.at(kSwitchBranchIndex));
  35. MS_EXCEPTION_IF_NULL(partial_node);
  36. const auto &partial_inputs = partial_node->inputs();
  37. if (!IsPrimitive(partial_inputs.at(0), prim::kPrimPartial)) {
  38. MS_LOG(EXCEPTION) << "Invalid switch node: " << cnode->DebugString();
  39. }
  40. auto switch_subgraph = GetValueNode<FuncGraphPtr>(partial_inputs.at(kPartialArgsIndex));
  41. MS_EXCEPTION_IF_NULL(switch_subgraph);
  42. (*out_caller_map)[switch_subgraph->output()] = cnode;
  43. } else if (AnfAlgo::CheckPrimitiveType(cnode, prim::kPrimCall)) {
  44. auto call_subgraph = GetValueNode<FuncGraphPtr>(inputs.at(kCallArgsIndex));
  45. MS_EXCEPTION_IF_NULL(call_subgraph);
  46. (*out_caller_map)[call_subgraph->output()] = cnode;
  47. }
  48. }
  49. bool NodePass::Run(const FuncGraphPtr &func_graph) {
  50. MS_EXCEPTION_IF_NULL(func_graph);
  51. FuncGraphManagerPtr manager = func_graph->manager();
  52. MS_EXCEPTION_IF_NULL(manager);
  53. manager->AddFuncGraph(func_graph);
  54. std::unordered_map<AnfNodePtr, AnfNodePtr> subgraph_out_caller_map = {};
  55. std::unordered_set<AnfNodePtr> seen_node;
  56. std::deque<std::pair<AnfNodePtr, FuncGraphPtr>> todo{{func_graph->output(), func_graph}};
  57. bool changes = false;
  58. while (!todo.empty()) {
  59. AnfNodePtr node = todo.front().first;
  60. auto fg = todo.front().second;
  61. manager->AddFuncGraph(fg);
  62. todo.pop_front();
  63. if (seen_node.count(node) > 0 || !manager->all_nodes().contains(node)) {
  64. continue;
  65. }
  66. (void)seen_node.insert(node);
  67. TraceGuard guard(std::make_shared<TraceOpt>(node->debug_info()));
  68. AnfNodePtr new_node = Run(fg, node);
  69. bool change = (new_node != nullptr);
  70. if (new_node != nullptr && new_node != node) {
  71. auto find_iter = subgraph_out_caller_map.find(node);
  72. if (find_iter != subgraph_out_caller_map.end()) {
  73. find_iter->second->set_abstract(new_node->abstract());
  74. }
  75. (void)manager->Replace(node, new_node);
  76. // if replaced node is end_goto, refresh relative params in kernel graph
  77. auto kernel_graph = fg->cast<std::shared_ptr<session::KernelGraph>>();
  78. if (kernel_graph != nullptr && node->isa<CNode>()) {
  79. auto cnode = node->cast<CNodePtr>();
  80. MS_EXCEPTION_IF_NULL(cnode);
  81. auto end_label = kernel_graph->get_end_goto();
  82. if (cnode == end_label && AnfAlgo::GetCNodeName(cnode) == kLabelSwitchOpName) {
  83. kernel_graph->set_end_goto(new_node->cast<CNodePtr>());
  84. }
  85. }
  86. (void)seen_node.erase(node);
  87. } else if (new_node == nullptr) {
  88. new_node = node;
  89. }
  90. if (new_node && IsValueNode<FuncGraph>(new_node)) {
  91. auto const_func_graph = GetValueNode<FuncGraphPtr>(new_node);
  92. MS_EXCEPTION_IF_NULL(const_func_graph);
  93. if (!const_func_graph->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL)) {
  94. (void)todo.emplace_back(const_func_graph->output(), const_func_graph);
  95. }
  96. } else if (new_node && new_node->isa<CNode>()) {
  97. if (AnfAlgo::IsGraphKernel(new_node)) {
  98. (void)todo.emplace_back(new_node, func_graph);
  99. }
  100. auto cnode = new_node->cast<CNodePtr>();
  101. MS_EXCEPTION_IF_NULL(cnode);
  102. AddOutputAndCallerToMap(cnode, &subgraph_out_caller_map);
  103. auto inputs = cnode->inputs();
  104. (void)std::for_each(inputs.begin(), inputs.end(), [&fg, &todo](AnfNodePtr &node) {
  105. (void)todo.emplace_back(std::pair<AnfNodePtr, FuncGraphPtr>(node, fg));
  106. });
  107. }
  108. changes = changes || change;
  109. }
  110. return changes;
  111. }
  112. } // namespace opt
  113. } // namespace mindspore