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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_PRE_ACTIVATE_COMMON_OPTIMIZER_H_
  17. #define MINDSPORE_CCSRC_PRE_ACTIVATE_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 "pre_activate/common/pass_manager.h"
  26. #include "pre_activate/common/pattern_engine.h"
  27. #include "utils/graph_utils.h"
  28. #include "common/utils.h"
  29. namespace mindspore {
  30. namespace opt {
  31. using PatternListType = std::initializer_list<BaseRef>;
  32. class PatternProcessPass : public NodePass {
  33. public:
  34. explicit PatternProcessPass(const std::string &name = "", bool multigraph = true);
  35. ~PatternProcessPass() override = default;
  36. virtual const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const = 0;
  37. virtual const BaseRef DefinePattern() const;
  38. AnfNodePtr Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) override;
  39. private:
  40. void Build();
  41. AnfNodePtr pattern_ = nullptr;
  42. bool multigraph_ = true;
  43. PatternEngine pattern_engine_;
  44. PrimitiveVarMapPtr primitive_vars_;
  45. };
  46. class GraphOptimizer {
  47. public:
  48. explicit GraphOptimizer(const std::string &name = "graph_optimizer") : name_(name) {}
  49. virtual ~GraphOptimizer() = default;
  50. void AddPassManager(const PassManagerPtr &pass_manager);
  51. FuncGraphPtr Optimize(const FuncGraphPtr &func_graph, bool run_only_once = true);
  52. private:
  53. const std::string name_ = "graph_optimizer";
  54. std::vector<PassManagerPtr> pass_managers_{};
  55. bool run_only_once_ = true;
  56. };
  57. } // namespace opt
  58. } // namespace mindspore
  59. #endif // MINDSPORE_CCSRC_PRE_ACTIVATE_COMMON_OPTIMIZER_H_