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.

pass_manager.h 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_PASS_MANAGER_H_
  17. #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_PASS_MANAGER_H_
  18. #include <utility>
  19. #include <vector>
  20. #include <string>
  21. #include <memory>
  22. #include <map>
  23. #include "backend/optimizer/common/pass.h"
  24. #include "backend/optimizer/common/node_pass.h"
  25. namespace mindspore {
  26. namespace opt {
  27. class CacheManager {
  28. public:
  29. CacheManager() {}
  30. ~CacheManager() = default;
  31. void Update(const AnfNodePtr &node);
  32. TypeId GetOutputType(const AnfNodePtr &node, size_t index);
  33. std::vector<size_t> GetOutputShape(const AnfNodePtr &node, size_t index);
  34. private:
  35. std::map<AnfNodePtr, std::map<size_t, TypeId>> type_map_;
  36. std::map<AnfNodePtr, std::map<size_t, std::vector<size_t>>> shape_map_;
  37. };
  38. using CacheManagerPtr = std::shared_ptr<CacheManager>;
  39. // @brief For optimization passes management
  40. class PassManager {
  41. public:
  42. explicit PassManager(const std::string &name = "pm", bool run_only_once = true)
  43. : name_(name), passes_{}, run_only_once_(run_only_once), cache_manager_(std::make_shared<CacheManager>()) {}
  44. virtual ~PassManager() = default;
  45. // Get all the passes added by AddPass
  46. const std::vector<PassPtr> &Passes() const;
  47. // Add graph pass, the pass object will be freed when pass manager freed.
  48. virtual void AddPass(const PassPtr &pass);
  49. // Run passes added in pass manager on the input graph
  50. // @param [in out] graph The graph to be optimized
  51. // @return true, graph changed
  52. // @return false, graph not changed
  53. virtual bool Run(const FuncGraphPtr &func_graph) const;
  54. // Run the given graph passes on the input graph
  55. // @param [in out] graph The graph to be optimized
  56. // @param [in] passes The given graph passes
  57. // @return true, graph changed
  58. // @return false, graph not changed
  59. virtual bool Run(const FuncGraphPtr &func_graph, const std::vector<PassPtr> &passes) const;
  60. std::string name() const { return name_; }
  61. protected:
  62. virtual bool RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const;
  63. virtual std::string GetPassFullname(size_t pass_id, const PassPtr &pass) const;
  64. virtual void DumpPassIR(const FuncGraphPtr &func_graph, const std::string &pass_fullname) const;
  65. const std::string name_;
  66. std::vector<PassPtr> passes_;
  67. bool run_only_once_;
  68. CacheManagerPtr cache_manager_;
  69. };
  70. using PassManagerPtr = std::shared_ptr<PassManager>;
  71. } // namespace opt
  72. } // namespace mindspore
  73. #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_PASS_MANAGER_H_