• 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 #ifndef MINDSPORE_CCSRC_KERNEL_FRAMEWORK_UTILS_H_
17 #define MINDSPORE_CCSRC_KERNEL_FRAMEWORK_UTILS_H_
18 
19 #include <set>
20 #include <map>
21 #include <memory>
22 #include <vector>
23 #include <string>
24 #include <utility>
25 #include <unordered_map>
26 #include "include/common/utils/utils.h"
27 #include "kernel/kernel.h"
28 #include "kernel/oplib/opinfo.h"
29 #include "kernel/kash/kernel_pack.h"
30 #include "kernel/kernel_build_info.h"
31 #include "include/backend/device_address.h"
32 #include "ops/base_operator.h"
33 #include "kernel/common_utils.h"
34 
35 namespace mindspore {
36 namespace kernel {
37 constexpr auto kAkgKernelMeta = "akg_kernel_meta/";
38 constexpr auto kKernelMetaSuffix = "_kernel_meta/";
39 constexpr auto kJsonSuffix = ".json";
40 constexpr auto kInfoSuffix = ".info";
41 
42 class BACKEND_EXPORT KernelMeta {
43  public:
44   KernelMeta() = default;
45   void Initialize(const std::string &backend = "akg");
46   std::string Search(const std::string &kernel_name) const;
47   bool Insert(const std::string &kernel_name, const std::string &kernel_json);
kernel_meta_path()48   std::string kernel_meta_path() const { return kernel_meta_path_; }
initialized()49   bool initialized() const { return initialized_; }
GetInstance()50   static KernelMeta *GetInstance() {
51     static KernelMeta kernel_meta;
52     return &kernel_meta;
53   }
54   ~KernelMeta() = default;
55 
56  private:
57   bool initialized_ = false;
58   std::string kernel_meta_path_;
59   std::unordered_map<std::string, std::string> kernel_meta_map_;
60 };
61 
62 BACKEND_EXPORT std::string GetCompilerCachePath();
63 bool CheckCache(const std::string &kernel_name);
64 KernelPackPtr SearchCache(const std::string &kernel_name, const std::string &processor);
65 KernelPackPtr InsertCache(const std::string &kernel_name, const std::string &processor);
66 
67 BACKEND_EXPORT bool GetShapeSize(const ShapeVector &shape, const TypePtr &type_ptr, int64_t *size_i);
68 
69 BACKEND_EXPORT bool ParseMetadata(const CNodePtr &kernel_node, const std::shared_ptr<const OpInfo> &op_info_ptr,
70                                   Processor processor,
71                                   std::vector<std::shared_ptr<KernelBuildInfo>> *const kernel_info_list);
72 
73 BACKEND_EXPORT void SaveJsonInfo(const std::string &json_name, const std::string &info, const std::string &base_path);
74 
75 std::string GetProcessor(const AnfNodePtr &anf_node);
76 Processor GetProcessor(const string &processor);
77 Processor GetProcessorFromContext();
78 std::string GetStrProcessorFromContext();
79 
80 BACKEND_EXPORT std::vector<std::pair<AnfNodePtr, size_t>> GetOutputIndex(const std::vector<AnfNodePtr> &node_list,
81                                                                          const std::vector<AnfNodePtr> &input_list,
82                                                                          const std::vector<AnfNodePtr> &output_list);
83 BACKEND_EXPORT void GetValidKernelNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *node_list);
84 BACKEND_EXPORT void GetValidKernelNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *node_list,
85                                         std::vector<AnfNodePtr> *input_list, std::vector<AnfNodePtr> *output_list);
86 void GetFuncGraphOutputNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *output_list);
87 void GetGraphRealOutput(const FuncGraphPtr &func_graph, std::vector<std::pair<AnfNodePtr, size_t>> *node_list);
88 BACKEND_EXPORT bool IsWeightBoundary(const AnfNodePtr &node);
89 BACKEND_EXPORT std::vector<int64_t> GetReduceAttrAxis(const CNodePtr &cnode);
90 
91 struct KernelArgs {
92   std::vector<KernelTensorPtr> inputs;
93   std::vector<KernelTensorPtr> outputs;
94   std::map<uint32_t, tensor::TensorPtr> depend_tensor_map;  // dynamic shape kernel may need this map
95   // cppcheck-suppress unusedStructMember
96   constexpr static char key[] = "KernelArgs";
97 };
98 BACKEND_EXPORT KernelArgs AbstractArgsFromCNode(const CNodePtr &cnode);
99 BACKEND_EXPORT std::shared_ptr<KernelArgs> GetArgsFromCNode(const CNodePtr &cnode);
100 BACKEND_EXPORT void SetArgsToCNode(const CNodePtr &cnode, const KernelArgs &args);
101 
102 BACKEND_EXPORT BaseOperatorPtr CreateOperatorByCNode(const CNodePtr &cnode);
103 BACKEND_EXPORT void UpdateNodeShape(const CNodePtr &cnode);
104 
105 BACKEND_EXPORT void SetInputsByDependMap(const std::map<uint32_t, tensor::TensorPtr> &depend_tensor_map,
106                                          std::vector<KernelTensorPtr> *inputs, bool is_stored_in_device = false);
107 BACKEND_EXPORT void SetInputsByConstInputs(const CNodePtr &node,
108                                            std::map<uint32_t, tensor::TensorPtr> *inputs_tensor_map);
109 BACKEND_EXPORT bool CheckResizeCondition(const CNodePtr &node);
110 
GetKernelDepends(const CNodePtr & cnode)111 inline std::map<uint32_t, tensor::TensorPtr> GetKernelDepends(const CNodePtr &cnode) {
112   auto args = GetArgsFromCNode(cnode);
113   if (args) {
114     return args->depend_tensor_map;
115   }
116   return std::map<uint32_t, tensor::TensorPtr>();
117 }
118 
119 KernelObjectType StringToKernelObjectType(const std::string &object_type);
120 
121 BACKEND_EXPORT void UnfoldKernelBuildInfo(const CNodePtr &kernel_node);
122 BACKEND_EXPORT int64_t CalOutputTupleSize(const AnfNodePtr &node);
123 BACKEND_EXPORT void SetDynamicInputSizeAttr(const CNodePtr &cnode);
124 BACKEND_EXPORT bool IsDynamicParamKernel(const std::string &op_name);
125 BACKEND_EXPORT std::pair<std::string, ExceptionType> KernelObjectTypeNotSupportWarning(const CNodePtr &kernel_node);
126 BACKEND_EXPORT bool IsKernelObjectTypeNotSupportedError(const std::string &error_str);
127 BACKEND_EXPORT std::pair<std::vector<DataType>, std::vector<DataType>> GetInOutDataTypesFromKernelAttr(
128   const KernelAttr &kernel_attr);
129 }  // namespace kernel
130 }  // namespace mindspore
131 
132 #endif  // MINDSPORE_CCSRC_KERNEL_FRAMEWORK_UTILS_H_
133