• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_H_
18 #include <memory>
19 #include <vector>
20 #include <string>
21 #include <nlohmann/json.hpp>
22 #include "backend/optimizer/common/pass.h"
23 #include "ir/func_graph.h"
24 
25 namespace mindspore {
26 namespace opt {
27 class Expander {
28  public:
29   virtual AnfNodePtr Run(const AnfNodePtr &node) = 0;
30 };
31 using ExpanderPtr = std::shared_ptr<Expander>;
32 
33 class PyExpander : public Expander {
34  public:
35   AnfNodePtr Run(const AnfNodePtr &node) override;
36 
37  protected:
38   virtual bool ExpandJsonInfo(const AnfNodePtr &node, nlohmann::json *kernel_json);
39   virtual AnfNodePtr CreateExpandGraphKernel(const FuncGraphPtr &new_func_graph, const CNodePtr &old_node);
40   virtual FuncGraphPtr CreateExpandFuncGraph(const CNodePtr &node);
41 };
42 
43 class DefaultExpander : public PyExpander {
44  protected:
45   FuncGraphPtr CreateExpandFuncGraph(const CNodePtr &node) override;
46 };
47 
48 class ComplexOpExpander : public DefaultExpander {
49  protected:
50   bool ExpandJsonInfo(const AnfNodePtr &node, nlohmann::json *kernel_json);
51 };
52 class GraphKernelExpander : public Pass {
53  public:
GraphKernelExpander()54   GraphKernelExpander() : Pass("graph_kernel_expander") {}
GraphKernelExpander(const std::string & name)55   explicit GraphKernelExpander(const std::string &name) : Pass(name) {}
56   ~GraphKernelExpander() override = default;
57   bool Run(const FuncGraphPtr &func_graph) override;
58 
59  protected:
60   virtual ExpanderPtr GetExpander(const AnfNodePtr &node);
61   virtual bool DoExpand(const FuncGraphPtr &func_graph);
CanExpand(const CNodePtr & node)62   virtual bool CanExpand(const CNodePtr &node) const {
63     return std::any_of(expand_ops_.begin(), expand_ops_.end(),
64                        [&node](const PrimitivePtr &prim) { return IsPrimitiveCNode(node, prim); });
65   }
66 
67  private:
68   std::vector<PrimitivePtr> expand_ops_;
69 };
70 class GraphKernelComplexExpander : public GraphKernelExpander {
71  public:
GraphKernelComplexExpander()72   GraphKernelComplexExpander() : GraphKernelExpander("graph_kernel_complex_expander") {}
73   ~GraphKernelComplexExpander() override = default;
74   bool Run(const FuncGraphPtr &func_graph) override;
75 
76  protected:
77   ExpanderPtr GetExpander(const AnfNodePtr &node) override;
78   bool CanExpand(const CNodePtr &node) const override;
79 };
80 }  // namespace opt
81 }  // namespace mindspore
82 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_H_
83