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.

optimizer.cc 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/optimizer.h"
  17. #include <functional>
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <utility>
  23. #include <initializer_list>
  24. #include "backend/optimizer/common/pass_manager.h"
  25. #include "backend/session/anf_runtime_algorithm.h"
  26. #include "ir/manager.h"
  27. namespace mindspore {
  28. namespace opt {
  29. PatternProcessPass::PatternProcessPass(const std::string &name, bool multigraph)
  30. : NodePass(name),
  31. multigraph_(multigraph),
  32. pattern_engine_(PatternEngine(std::make_shared<DefaultVisitor>(),
  33. std::function<bool(const BaseRef &, const BaseRef &)>(AnfEqual),
  34. std::function<bool(const BaseRef &, const BaseRef &)>(CNodeTypeEqual))),
  35. primitive_vars_(std::make_shared<PrimitiveVarMap>()) {}
  36. const BaseRef PatternProcessPass::DefinePattern() const {
  37. VarPtr X = std::make_shared<Var>();
  38. return BaseRef({X});
  39. }
  40. void PatternProcessPass::Build() {
  41. VarPtr fg = std::make_shared<Var>("RootG");
  42. BaseRef pattern = std::move(DefinePattern());
  43. pattern_ = SexpToNode(pattern, fg, primitive_vars_.get(), multigraph_);
  44. }
  45. AnfNodePtr PatternProcessPass::Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
  46. if (pattern_ == nullptr) {
  47. Build();
  48. }
  49. auto empty_equiv = std::make_shared<Equiv>();
  50. MS_EXCEPTION_IF_NULL(primitive_vars_);
  51. EquivPtr equiv = pattern_engine_.Match(pattern_, node, *primitive_vars_, empty_equiv);
  52. if (equiv != nullptr && !equiv->empty()) {
  53. return Process(func_graph, node, equiv);
  54. }
  55. return nullptr;
  56. }
  57. bool MultipleOutputPatternProcessPass::MatchAnotherPattern(const AnfNodePtr &node, const EquivPtr &equiv) const {
  58. MS_EXCEPTION_IF_NULL(node);
  59. MS_EXCEPTION_IF_NULL(equiv);
  60. VarPtr fg = std::make_shared<Var>("RootG");
  61. auto empty_equiv = std::make_shared<Equiv>();
  62. MS_EXCEPTION_IF_NULL(child_primitive_vars_);
  63. EquivPtr another_equiv =
  64. child_pattern_engine_.Match(SexpToNode(DefineAnotherPattern(), fg, child_primitive_vars_.get(), true), node,
  65. *child_primitive_vars_, empty_equiv);
  66. if (another_equiv != nullptr && !another_equiv->empty()) {
  67. return IsShareNodes(equiv, another_equiv);
  68. }
  69. return false;
  70. }
  71. void GraphOptimizer::AddPassManager(const PassManagerPtr &pass_manager) {
  72. if (pass_manager != nullptr) {
  73. pass_managers_.push_back(pass_manager);
  74. }
  75. }
  76. FuncGraphPtr GraphOptimizer::Optimize(const FuncGraphPtr &func_graph, bool run_only_once) {
  77. MS_EXCEPTION_IF_NULL(func_graph);
  78. run_only_once_ = (pass_managers_.size() == 1) ? true : run_only_once;
  79. // Performance risk by creating new manager each time
  80. auto manager = Manage(func_graph, true);
  81. bool changed = true;
  82. while (changed) {
  83. changed = false;
  84. for (size_t i = 0; i < pass_managers_.size(); ++i) {
  85. const PassManagerPtr &pm = pass_managers_[i];
  86. if (pm != nullptr && pm->Run(func_graph)) {
  87. changed = true;
  88. }
  89. }
  90. if (run_only_once_) {
  91. break;
  92. }
  93. }
  94. std::vector<FuncGraphPtr> func_graphs;
  95. func_graphs.push_back(func_graph);
  96. manager->KeepRoots(func_graphs);
  97. (void)TopoSort(func_graph->get_return());
  98. return func_graph;
  99. }
  100. } // namespace opt
  101. } // namespace mindspore