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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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<Visitor>())),
  33. primitive_vars_(std::make_shared<PrimitiveVarMap>()) {}
  34. const BaseRef PatternProcessPass::DefinePattern() const {
  35. VarPtr X = std::make_shared<Var>();
  36. return BaseRef({X});
  37. }
  38. void PatternProcessPass::Build() {
  39. VarPtr fg = std::make_shared<Var>("RootG");
  40. BaseRef pattern = std::move(DefinePattern());
  41. pattern_ = SexpToNode(pattern, fg, primitive_vars_.get(), multigraph_);
  42. }
  43. AnfNodePtr PatternProcessPass::Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
  44. if (pattern_ == nullptr) {
  45. Build();
  46. }
  47. auto primitive = GetCNodePrimitive(pattern_);
  48. if (IsPrimitiveCNode(node, primitive)) {
  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. }
  56. return nullptr;
  57. }
  58. bool MultipleOutputPatternProcessPass::MatchAnotherPattern(const AnfNodePtr &node, const EquivPtr &equiv) const {
  59. MS_EXCEPTION_IF_NULL(node);
  60. MS_EXCEPTION_IF_NULL(equiv);
  61. VarPtr fg = std::make_shared<Var>("RootG");
  62. auto empty_equiv = std::make_shared<Equiv>();
  63. MS_EXCEPTION_IF_NULL(child_primitive_vars_);
  64. EquivPtr another_equiv =
  65. child_pattern_engine_.Match(SexpToNode(DefineAnotherPattern(), fg, child_primitive_vars_.get(), true), node,
  66. *child_primitive_vars_, empty_equiv);
  67. if (another_equiv != nullptr && !another_equiv->empty()) {
  68. return IsShareNodes(equiv, another_equiv);
  69. }
  70. return false;
  71. }
  72. void GraphOptimizer::AddPassManager(const PassManagerPtr &pass_manager) {
  73. if (pass_manager != nullptr) {
  74. pass_managers_.push_back(pass_manager);
  75. }
  76. }
  77. FuncGraphPtr GraphOptimizer::Optimize(const FuncGraphPtr &func_graph, bool run_only_once) {
  78. MS_EXCEPTION_IF_NULL(func_graph);
  79. run_only_once_ = (pass_managers_.size() == 1) ? true : run_only_once;
  80. // Performance risk by creating new manager each time
  81. // cppcheck-suppress *
  82. auto manager = Manage(func_graph, true);
  83. bool changed = true;
  84. while (changed) {
  85. changed = false;
  86. for (size_t i = 0; i < pass_managers_.size(); ++i) {
  87. const PassManagerPtr &pm = pass_managers_[i];
  88. if (pm != nullptr && pm->Run(func_graph)) {
  89. changed = true;
  90. }
  91. }
  92. if (run_only_once_) {
  93. break;
  94. }
  95. }
  96. std::vector<FuncGraphPtr> func_graphs;
  97. func_graphs.push_back(func_graph);
  98. (void)TopoSort(func_graph->get_return());
  99. return func_graph;
  100. }
  101. } // namespace opt
  102. } // namespace mindspore