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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <deque>
  19. #include "ir/anf.h"
  20. #include "ir/func_graph.h"
  21. #include "ir/manager.h"
  22. #include "backend/session/anf_runtime_algorithm.h"
  23. namespace mindspore {
  24. namespace opt {
  25. bool NodePass::Run(const FuncGraphPtr &func_graph) {
  26. MS_EXCEPTION_IF_NULL(func_graph);
  27. FuncGraphManagerPtr manager = func_graph->manager();
  28. MS_EXCEPTION_IF_NULL(manager);
  29. manager->AddFuncGraph(func_graph);
  30. std::unordered_set<AnfNodePtr> seen_node;
  31. std::deque<std::pair<AnfNodePtr, FuncGraphPtr>> todo{{func_graph->output(), func_graph}};
  32. bool changes = false;
  33. while (!todo.empty()) {
  34. AnfNodePtr node = todo.front().first;
  35. auto fg = todo.front().second;
  36. manager->AddFuncGraph(fg);
  37. todo.pop_front();
  38. if (seen_node.count(node) > 0 || !manager->all_nodes().contains(node)) {
  39. continue;
  40. }
  41. (void)seen_node.insert(node);
  42. TraceGuard guard(std::make_shared<TraceOpt>(node->debug_info()));
  43. AnfNodePtr new_node = Run(fg, node);
  44. bool change = (new_node != nullptr);
  45. if (new_node != nullptr && new_node != node) {
  46. (void)manager->Replace(node, new_node);
  47. // if replaced node is end_goto, refresh relative params in kernel graph
  48. auto kernel_graph = fg->cast<std::shared_ptr<session::KernelGraph>>();
  49. if (kernel_graph != nullptr && node->isa<CNode>()) {
  50. auto cnode = node->cast<CNodePtr>();
  51. MS_EXCEPTION_IF_NULL(cnode);
  52. auto end_label = kernel_graph->get_end_goto();
  53. if (cnode == end_label && AnfAlgo::GetCNodeName(cnode) == kLabelSwitchOpName) {
  54. kernel_graph->set_end_goto(new_node->cast<CNodePtr>());
  55. }
  56. }
  57. (void)seen_node.erase(node);
  58. } else if (new_node == nullptr) {
  59. new_node = node;
  60. }
  61. if (new_node && IsValueNode<FuncGraph>(new_node)) {
  62. auto const_func_graph = GetValueNode<FuncGraphPtr>(new_node);
  63. MS_EXCEPTION_IF_NULL(const_func_graph);
  64. if (!const_func_graph->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL)) {
  65. todo.push_back({const_func_graph->output(), const_func_graph});
  66. }
  67. } else if (new_node && new_node->isa<CNode>()) {
  68. if (AnfAlgo::IsGraphKernel(new_node)) {
  69. todo.push_back({new_node, func_graph});
  70. }
  71. auto cnode = new_node->cast<CNodePtr>();
  72. MS_EXCEPTION_IF_NULL(cnode);
  73. auto inputs = cnode->inputs();
  74. std::for_each(inputs.begin(), inputs.end(), [&fg, &todo](AnfNodePtr &node) {
  75. todo.emplace_back(std::pair<AnfNodePtr, FuncGraphPtr>(node, fg));
  76. });
  77. }
  78. changes = changes || change;
  79. }
  80. return changes;
  81. }
  82. } // namespace opt
  83. } // namespace mindspore