• 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 
17 #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_TBE_KERNEL_BUILD_H_
18 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_TBE_KERNEL_BUILD_H_
19 
20 #include <string>
21 #include <unordered_map>
22 #include <memory>
23 #include <map>
24 #include <utility>
25 #include <vector>
26 #include <nlohmann/json.hpp>
27 #include "ir/dtype.h"
28 #include "backend/kernel_compiler/kernel.h"
29 #include "backend/kernel_compiler/oplib/oplib.h"
30 #include "backend/kernel_compiler/tbe/tbe_adapter.h"
31 
32 namespace mindspore {
33 namespace kernel {
34 // kernel operate type used for generate json
35 
36 class TbeKernelBuild {
37   enum FusionDataType { kFusionNormal = 0, kFusionAddN, kFusionReLUGradV2, kFusionAdd };
38 
39  public:
40   static bool GetIOSize2(const nlohmann::json &kernel_json, std::vector<size_t> *input_size_list,
41                          std::vector<size_t> *output_size_list, const AnfNodePtr &anf_node);
42   static bool GetIOSize(const nlohmann::json &kernel_json, std::vector<size_t> *input_size_list,
43                         std::vector<size_t> *output_size_list);
44   // Ub Fuison
45   static bool GenFusionScopeJson(const std::vector<AnfNodePtr> &input_nodes,
46                                  const std::vector<AnfNodePtr> &compute_nodes, nlohmann::json *fusion_json,
47                                  std::string *fusion_kernel_name);
48   static bool GetIOSize(const nlohmann::json &fusion_op_list, const std::vector<AnfNodePtr> &output_nodes,
49                         std::vector<size_t> *input_size_list, std::vector<size_t> *output_size_list);
50   static void CalInputSize(const nlohmann::json &fusion_op_list, std::vector<size_t> *input_size_list);
51   static bool CalOutputSize(const nlohmann::json &fusion_op_list,
52                             const std::vector<mindspore::AnfNodePtr> &output_nodes,
53                             std::vector<size_t> *output_size_list);
54 
55  private:
56   TbeKernelBuild() = default;
57   ~TbeKernelBuild() = default;
58   static bool GenFusionDataInputJson(const std::shared_ptr<mindspore::AnfNode> &data_input,
59                                      const std::map<const AnfNodePtr, FusionDataType> &spec_data_input,
60                                      nlohmann::json *data_str, size_t *index);
61   static bool GenFusionComputeJson(const mindspore::AnfNodePtr &compute_node,
62                                    std::vector<std::vector<mindspore::AnfNodePtr>>::iterator *layer_iter,
63                                    nlohmann::json *compute_op_str, std::string *fusion_kernel_name, size_t *index);
64   static bool GenFusionComputeInputJson(const mindspore::CNodePtr &cnode,
65                                         std::vector<std::vector<mindspore::AnfNodePtr>>::iterator *layer_iter,
66                                         std::vector<nlohmann::json> *input_desc_list, size_t *index);
67   static std::vector<size_t> GetDescOutputIndex(const std::vector<int64_t> &output_used_nums);
68   static bool GenFusionComputeOutputJson(const mindspore::CNodePtr &cnode,
69                                          std::vector<nlohmann::json> *output_desc_list,
70                                          std::vector<nlohmann::json> *output_data_desc_list);
71   static void GenPreDescJson(nlohmann::json *output_desc);
72   static void GenFusionComputeCommonJson(const mindspore::CNodePtr &cnode, nlohmann::json *compute_op_str,
73                                          std::string *fusion_kernel_name);
74   static void GenDescJson(const std::shared_ptr<mindspore::AnfNode> &anf_node, size_t node_out_idx,
75                           size_t desc_output_idx, nlohmann::json *output_desc,
76                           FusionDataType fusion_data_type = kFusionNormal);
77   static void GenFusionOutputDescJson(const std::shared_ptr<mindspore::AnfNode> &anf_node, size_t node_out_idx,
78                                       size_t desc_output_idx, nlohmann::json *output_desc,
79                                       nlohmann::json *output_data_desc);
80   static void GenSuffixDescJson(nlohmann::json *output_desc);
81   static void GenReusedOutputDesc(const std::shared_ptr<mindspore::AnfNode> &anf_node, size_t index,
82                                   size_t output_index, nlohmann::json *output_desc, const size_t out_size);
83   static size_t GetIOSizeImpl(const nlohmann::json &desc);
84   static bool GetSpecInputLayers(const std::string &op_name, const std::vector<mindspore::AnfNodePtr> &reorder_layer,
85                                  std::map<const AnfNodePtr, FusionDataType> *spec_data_input);
86   static bool GetInputLayers(const std::vector<mindspore::AnfNodePtr> &input_nodes,
87                              const std::vector<mindspore::AnfNodePtr> &compute_nodes,
88                              std::vector<std::vector<mindspore::AnfNodePtr>> *input_layers,
89                              std::map<const AnfNodePtr, FusionDataType> *spec_data_input);
90   static bool IsDynamicInput(const CNodePtr &cnode);
91   static size_t GetOptionalInput(const CNodePtr &cnode, bool is_dynamic_input);
92   static std::string GetRealOpType(const std::string &origin_type);
93   static std::string GetNodeFusionType(const CNodePtr &cnode);
94 };
95 
96 class TbeKernelJsonCreator {
97  public:
creater_type_(creater_type)98   explicit TbeKernelJsonCreator(kCreaterType creater_type = SINGLE_BUILD) : creater_type_(creater_type) {}
99   ~TbeKernelJsonCreator() = default;
100   bool GenTbeSingleKernelJson(const std::shared_ptr<AnfNode> &anf_node, nlohmann::json *kernel_json);
json_name()101   std::string json_name() { return json_name_; }
102   void GenTbeAttrJson(const std::shared_ptr<AnfNode> &anf_node, const std::shared_ptr<OpInfo> &op_info,
103                       nlohmann::json *attrs_json);
104   static string GetSocVersion();
105 
106  private:
107   bool GenTbeInputsJson(const std::shared_ptr<AnfNode> &anf_node, const std::shared_ptr<OpInfo> &op_info,
108                         nlohmann::json *inputs_json);
109   bool GenTbeOutputsJson(const std::shared_ptr<AnfNode> &anf_node, const std::shared_ptr<OpInfo> &op_info,
110                          nlohmann::json *outputs_json);
111   void GenSocInfo(nlohmann::json *soc_info_json);
112   static bool ParseAttrValue(const std::string &type, const ValuePtr &value, nlohmann::json *attr_obj);
113   static void ParseAttrDefaultValue(const std::string &type, const std::string &value, nlohmann::json *attr_obj);
114   bool GenInputDescJson(const std::shared_ptr<AnfNode> &anf_node, size_t real_input_index, bool value,
115                         const std::shared_ptr<OpIOInfo> &input_ptr, const string &op_input_name, size_t input_i,
116                         std::vector<nlohmann::json> *input_list);
117   bool GenOutputDescJson(const std::shared_ptr<AnfNode> &anf_node,
118                          const std::vector<std::shared_ptr<OpIOInfo>> &outputs_ptr, nlohmann::json *outputs_json);
119   bool GenInputList(const std::shared_ptr<AnfNode> &anf_node, size_t input_tensor_num,
120                     const std::shared_ptr<OpIOInfo> &input_ptr, size_t *real_input_index, string *op_input_name,
121                     std::vector<nlohmann::json> *input_list);
122   void GenOutputList(const std::shared_ptr<AnfNode> &anf_node, const size_t &output_obj_num,
123                      const std::shared_ptr<OpIOInfo> &output_ptr, size_t *output_idx,
124                      std::vector<nlohmann::json> *output_list);
125   void GenValidInputDescJson(const std::shared_ptr<AnfNode> &anf_node, size_t real_input_index, bool value,
126                              const std::shared_ptr<OpIOInfo> &input_ptr, const string &op_input_name, size_t input_i,
127                              std::vector<nlohmann::json> *input_list);
128   std::vector<size_t> GetDeviceInputShape(const AnfNodePtr &anf_node, size_t real_index) const;
129   std::string GetDeviceInputType(const AnfNodePtr &anf_node, size_t real_index) const;
130   std::string GetDeviceInputFormat(const AnfNodePtr &anf_node, size_t real_index) const;
131   std::vector<size_t> GetDeviceOutputShape(const AnfNodePtr &anf_node, size_t real_index) const;
132   std::string GetDeviceOutputType(const AnfNodePtr &anf_node, size_t real_index) const;
133   std::string GetDeviceOutputFormat(const AnfNodePtr &anf_node, size_t real_index) const;
134 
135   kCreaterType creater_type_;
136   std::string json_name_;
137   std::string json_info_;
138 };
139 }  // namespace kernel
140 }  // namespace mindspore
141 
142 #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_TBE_KERNEL_BUILD_H_
143