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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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<AnfNodePtr> todo{func_graph->output()};
  32. bool changes = false;
  33. while (!todo.empty()) {
  34. AnfNodePtr node = todo.front();
  35. todo.pop_front();
  36. if (seen_node.count(node) > 0 || !manager->all_nodes().contains(node)) {
  37. continue;
  38. }
  39. (void)seen_node.insert(node);
  40. TraceGuard guard(std::make_shared<TraceOpt>(node->debug_info()));
  41. AnfNodePtr new_node = Run(func_graph, node);
  42. bool change = (new_node != nullptr);
  43. if (new_node != nullptr && new_node != node) {
  44. (void)manager->Replace(node, new_node);
  45. // if replaced node is end_goto, refresh relative params in kernel graph
  46. auto kernel_graph = func_graph->cast<std::shared_ptr<session::KernelGraph>>();
  47. if (kernel_graph != nullptr && node->isa<CNode>()) {
  48. auto cnode = node->cast<CNodePtr>();
  49. MS_EXCEPTION_IF_NULL(cnode);
  50. auto end_label = kernel_graph->get_end_goto();
  51. if (cnode == end_label && AnfAlgo::GetCNodeName(cnode) == kLabelSwitchOpName) {
  52. kernel_graph->set_end_goto(new_node->cast<CNodePtr>());
  53. }
  54. }
  55. (void)seen_node.erase(node);
  56. } else if (new_node == nullptr) {
  57. new_node = node;
  58. }
  59. if (new_node && IsValueNode<FuncGraph>(new_node)) {
  60. auto const_func_graph = GetValueNode<FuncGraphPtr>(new_node);
  61. MS_EXCEPTION_IF_NULL(const_func_graph);
  62. if (!const_func_graph->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL)) {
  63. todo.push_back(const_func_graph->output());
  64. }
  65. } else if (new_node && new_node->isa<CNode>()) {
  66. if (AnfAlgo::IsGraphKernel(new_node)) {
  67. todo.push_back(new_node);
  68. }
  69. auto cnode = new_node->cast<CNodePtr>();
  70. MS_EXCEPTION_IF_NULL(cnode);
  71. auto inputs = cnode->inputs();
  72. (void)todo.insert(todo.end(), inputs.begin(), inputs.end());
  73. }
  74. changes = changes || change;
  75. }
  76. return changes;
  77. }
  78. } // namespace opt
  79. } // namespace mindspore