• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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 
17 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CONVERT_INPUT_AND_ATTR_H_
18 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CONVERT_INPUT_AND_ATTR_H_
19 
20 #include <set>
21 #include <string>
22 #include <memory>
23 #include "include/backend/optimizer/pass.h"
24 #include "ops/op_def.h"
25 
26 namespace mindspore::graphkernel {
27 class ConvertFrontEndToGraphKernel : public opt::Pass {
28  public:
ConvertFrontEndToGraphKernel()29   ConvertFrontEndToGraphKernel() : Pass("convert_input_to_attr") {}
30   ~ConvertFrontEndToGraphKernel() override = default;
31   bool Run(const FuncGraphPtr &func_graph) override;
32 
33  private:
34   bool Process(const CNodePtr &cnode, const ops::OpDefPtr &op_def, const PrimitivePtr &primitive);
35   void AddConstInputToAttr(const CNodePtr &cnode, const size_t input_index, const std::string &arg_name,
36                            const std::string &arg_handler, const PrimitivePtr &primitive);
37 };
38 class ConvertGraphKernelToFrontEnd : public opt::Pass {
39  public:
ConvertGraphKernelToFrontEnd()40   ConvertGraphKernelToFrontEnd() : Pass("convert_attr_to_input") {}
41   ~ConvertGraphKernelToFrontEnd() override = default;
42   bool Run(const FuncGraphPtr &func_graph) override;
43   static bool Process(const AnfNodePtr &node);
44 
45  private:
46   static void AddAttrToInput(const CNodePtr &cnode, const std::string &arg_name, const std::string &arg_handler,
47                              const PrimitivePtr &primitive, size_t pos);
48   static bool ConvertInputsType(const CNodePtr &cnode, size_t idx, ops::OP_DTYPE fe_arg_type);
49 };
50 
51 class OpDefAdapter {
52  public:
53   /// \brief Check whether need to convert the node from GraphKernel format to frontend format,
54   /// includes input/attr and kernel object.
55   static bool NeedConvertGK2FE(const AnfNodePtr &node);
56   static bool NeedConvertInputAndAttr(const AnfNodePtr &node);
57 };
58 }  // namespace mindspore::graphkernel
59 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CONVERT_INPUT_AND_ATTR_H_
60