• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2022 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_CORE_GRAPH_KERNEL_EXPANDER_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_EXPANDER_H_
18 #include <memory>
19 #include <vector>
20 #include <string>
21 #include "include/backend/optimizer/pass.h"
22 #include "ir/func_graph.h"
23 #include "backend/common/graph_kernel/core/expander.h"
24 #include "backend/common/graph_kernel/expanders/op_desc_registry.h"
25 
26 namespace mindspore::graphkernel {
27 class GraphKernelExpander : public opt::Pass {
28  public:
GraphKernelExpander()29   GraphKernelExpander() : Pass("graph_kernel_expander") {}
GraphKernelExpander(const std::string & name)30   explicit GraphKernelExpander(const std::string &name) : Pass(name) {}
31   ~GraphKernelExpander() override = default;
32   bool Run(const FuncGraphPtr &func_graph) override;
33 
34  protected:
35   AnfNodePtr CreateExpandedNode(const CNodePtr &node, const std::string &name) const;
36   virtual ExpanderPtr InitExpander(const AnfNodePtr &node) = 0;
37   virtual std::vector<PrimitivePtr> InitOpList() = 0;
38   bool DoExpand(const FuncGraphPtr &func_graph);
CanExpand(const CNodePtr & node)39   virtual bool CanExpand(const CNodePtr &node) const {
40     return std::any_of(expand_ops_.begin(), expand_ops_.end(),
41                        [&node](const PrimitivePtr &prim) { return IsPrimitiveCNode(node, prim); });
42   }
PreProcessAllNode(const CNodePtr & node)43   virtual void PreProcessAllNode(const CNodePtr &node) {}
44 
45  private:
46   std::vector<PrimitivePtr> expand_ops_;
47 };
48 }  // namespace mindspore::graphkernel
49 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_EXPANDER_H_
50