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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_OPTIMIZER_H_
  17. #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_OPTIMIZER_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include <unordered_map>
  22. #include "ir/anf.h"
  23. #include "ir/func_graph.h"
  24. #include "ir/primitive.h"
  25. #include "backend/optimizer/common/pass_manager.h"
  26. #include "backend/optimizer/common/pattern_engine.h"
  27. #include "ir/graph_utils.h"
  28. #include "utils/ms_utils.h"
  29. #include "backend/optimizer/common/helper.h"
  30. namespace mindspore {
  31. namespace opt {
  32. using PatternListType = std::initializer_list<BaseRef>;
  33. class PatternProcessPass : public NodePass {
  34. public:
  35. explicit PatternProcessPass(const std::string &name = "", bool multigraph = true);
  36. ~PatternProcessPass() override = default;
  37. virtual const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const = 0;
  38. virtual const BaseRef DefinePattern() const;
  39. AnfNodePtr Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) override;
  40. private:
  41. void Build();
  42. AnfNodePtr pattern_ = nullptr;
  43. bool multigraph_ = true;
  44. PatternEngine pattern_engine_;
  45. PrimitiveVarMapPtr primitive_vars_;
  46. };
  47. class MultipleOutputPatternProcessPass : public PatternProcessPass {
  48. public:
  49. explicit MultipleOutputPatternProcessPass(const std::string &name = "", bool multigraph = true)
  50. : PatternProcessPass(name, multigraph),
  51. child_pattern_engine_(PatternEngine(std::make_shared<DefaultVisitor>(),
  52. std::function<bool(const BaseRef &, const BaseRef &)>(AnfEqual),
  53. std::function<bool(const BaseRef &, const BaseRef &)>(CNodeTypeEqual))),
  54. child_primitive_vars_(std::make_shared<PrimitiveVarMap>()) {}
  55. ~MultipleOutputPatternProcessPass() override = default;
  56. virtual BaseRef DefineAnotherPattern() const = 0;
  57. // check two patterns whether share the same nodes or not
  58. virtual bool IsShareNodes(const EquivPtr &equiv1, const EquivPtr &equiv2) const = 0;
  59. protected:
  60. bool MatchAnotherPattern(const AnfNodePtr &node, const EquivPtr &equiv) const;
  61. PatternEngine child_pattern_engine_;
  62. PrimitiveVarMapPtr child_primitive_vars_;
  63. };
  64. class GraphOptimizer {
  65. public:
  66. explicit GraphOptimizer(const std::string &name = "graph_optimizer") : name_(name) {}
  67. virtual ~GraphOptimizer() = default;
  68. void AddPassManager(const PassManagerPtr &pass_manager);
  69. FuncGraphPtr Optimize(const FuncGraphPtr &func_graph, bool run_only_once = true);
  70. private:
  71. const std::string name_ = "graph_optimizer";
  72. std::vector<PassManagerPtr> pass_managers_{};
  73. bool run_only_once_ = true;
  74. };
  75. } // namespace opt
  76. } // namespace mindspore
  77. #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_OPTIMIZER_H_