• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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_FRONTEND_OPTIMIZER_PASS_H_
17 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_PASS_H_
18 #include <string>
19 #include <memory>
20 #include <unordered_map>
21 
22 #include "ir/anf.h"
23 #include "frontend/optimizer/pattern.h"
24 #include "pybind_api/api_register.h"
25 #include "pybind_api/export_flags.h"
26 
27 namespace mindspore {
28 namespace opt {
29 namespace python_pass {
30 class PythonPass;
31 using PythonPassPtr = std::shared_ptr<PythonPass>;
32 using NodeEquiv = std::unordered_map<std::string, AnfNodePtr>;
33 using NodeEquivPtr = std::shared_ptr<NodeEquiv>;
34 
35 class PythonPass {
36  public:
37   explicit PythonPass(const std::string &name, const PatternPtr &src, const PatternPtr &dst, bool run_only_once = false)
src_pattern_(src)38       : src_pattern_(src), dst_pattern_(dst), name_(name), run_only_once_(run_only_once) {}
39   ~PythonPass() = default;
40   bool Run(const FuncGraphPtr &func_graph, const MatchResultPtr &res);
name()41   std::string name() const { return name_; }
42   AnfNodePtr Run(const FuncGraphPtr &func_graph, const FuncGraphPtr &top_graph, const AnfNodePtr &node,
43                  const MatchResultPtr &res);
src_pattern()44   PatternPtr src_pattern() { return src_pattern_; }
dst_pattern()45   PatternPtr dst_pattern() { return dst_pattern_; }
46 
47  private:
48   PatternPtr src_pattern_;
49   PatternPtr dst_pattern_;
50   const std::string name_;
51   bool run_only_once_;
52 };
53 
54 using PythonPassPtr = std::shared_ptr<PythonPass>;
55 }  // namespace python_pass
56 }  // namespace opt
57 }  // namespace mindspore
58 #endif  // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_PASS_H_
59