• 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_BACKEND_KERNEL_COMPILER_KASH_KERNEL_PACK_H_
17 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_KASH_KERNEL_PACK_H_
18 #include <string>
19 #include <vector>
20 #include <memory>
21 #include "kernel/kernel.h"
22 
23 #ifdef _MSC_VER
24 #undef OPAQUE
25 #endif
26 
27 #ifdef OPAQUE
28 #undef OPAQUE
29 #endif
30 
31 namespace mindspore {
32 namespace kernel {
33 struct FlexArray {
34   size_t len;
35   char contents[];
36 };
37 
38 struct GlobalWorkspace {
39   size_t size{0};
40   size_t type{0};
41   bool is_overflow = false;
42 };
43 
44 struct NodeBaseInfo {
45   size_t workspace_num;
46   size_t input_num;
47   size_t output_num;
48   size_t offset_index;
49 };
50 
51 struct KernelJsonInfo {
52   std::string bin_file_name;
53   std::string bin_file_suffix;
54   uint32_t block_dim{0};
55   std::string kernel_name;
56   std::string magic;
57   std::vector<size_t> parameters;
58   std::string sha256;
59   std::vector<size_t> workspaces_type;
60   std::vector<size_t> workspaces;
61   GlobalWorkspace global_workspace;
62   bool has_kernel_list{false};
63   uint32_t op_para_size{0};
64   int32_t KBHit{0};
65   uint32_t mode_in_args_first_field{0};
66   uint32_t batch_bind_only{0};
67   uint32_t task_ration{0};
68   std::string core_type;
69   std::vector<std::vector<size_t>> args_remap;
70   AtomicInitInfo atomic_init_info;
71   NodeBaseInfo node_base_info;
72   KernelJsonInfo() = default;
73 };
74 
75 class BACKEND_EXPORT KernelPack {
76  public:
KernelPack()77   KernelPack() : json_(nullptr), kernel_(nullptr) {}
78   KernelPack(const KernelPack &) = default;
79   KernelPack &operator=(const KernelPack &) = default;
80   KernelJsonInfo kernel_json_info() const;
81   bool LoadKernelMeta(const std::string &json_f);
82   bool ReadFromJsonFile(const std::string &json_f, const std::string &processor);
GetJson()83   const FlexArray *GetJson() const { return json_; }
GetKernel()84   const FlexArray *GetKernel() const { return kernel_; }
~KernelPack()85   ~KernelPack() {
86     if (json_ != nullptr) {
87       delete[] json_;
88       json_ = nullptr;
89     }
90     if (kernel_ != nullptr) {
91       delete[] kernel_;
92       kernel_ = nullptr;
93     }
94   }
95 
96  private:
97   bool ReadFromJsonFileHelper(std::ifstream &kernel_bin);
98   void ParseKernelJson(const nlohmann::json &js);
99   static void ParseKernelName(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
100   static void ParseBinFileName(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
101   static void ParseBinFileSuffix(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
102   static void ParseMagic(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
103   static void ParseBlockDim(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
104   static void ParseTaskRatio(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
105   static void ParseCoreType(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
106   static void ParseParameters(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
107   static void ParseWorkSpace(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
108   static void ParseOpParaSize(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
109   static void ParseSHA256(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
110   static void ParseKBHit(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
111   static void ParseBatchBindOnly(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
112   static void ParseKernelList(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
113   static void ParseModeInArgsFirstField(const std::string &key, const nlohmann::json &js,
114                                         KernelJsonInfo *kernel_json_info);
115   static void ParseArgsRemap(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
116   static void ParseGlogbleWorkSpace(const std::string &key, const nlohmann::json &js, KernelJsonInfo *kernel_json_info);
117 
118   KernelJsonInfo kernel_json_info_;
119   FlexArray *json_;
120   FlexArray *kernel_;
121 };
122 using KernelPackPtr = std::shared_ptr<KernelPack>;
123 }  // namespace kernel
124 }  // namespace mindspore
125 
126 #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_KASH_KERNEL_PACK_H_
127