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