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.9 kB

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