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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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<Visitor>())),
  52. child_primitive_vars_(std::make_shared<PrimitiveVarMap>()) {}
  53. ~MultipleOutputPatternProcessPass() override = default;
  54. virtual BaseRef DefineAnotherPattern() const = 0;
  55. // check two patterns whether share the same nodes or not
  56. virtual bool IsShareNodes(const EquivPtr &equiv1, const EquivPtr &equiv2) const = 0;
  57. protected:
  58. bool MatchAnotherPattern(const AnfNodePtr &node, const EquivPtr &equiv) const;
  59. PatternEngine child_pattern_engine_;
  60. PrimitiveVarMapPtr child_primitive_vars_;
  61. };
  62. class GraphOptimizer {
  63. public:
  64. explicit GraphOptimizer(const std::string &name = "graph_optimizer") : name_(name) {}
  65. virtual ~GraphOptimizer() = default;
  66. void AddPassManager(const PassManagerPtr &pass_manager);
  67. FuncGraphPtr Optimize(const FuncGraphPtr &func_graph, bool run_only_once = true);
  68. private:
  69. const std::string name_ = "graph_optimizer";
  70. std::vector<PassManagerPtr> pass_managers_{};
  71. bool run_only_once_ = true;
  72. };
  73. } // namespace opt
  74. } // namespace mindspore
  75. #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_OPTIMIZER_H_