• 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_UTILS_H_
18 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_TBE_UTILS_H_
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <utility>
23 #include <map>
24 #include <tuple>
25 #include <unordered_map>
26 #include <nlohmann/json.hpp>
27 
28 #include "backend/session/kernel_graph.h"
29 #include "ir/anf.h"
30 #include "backend/kernel_compiler/kernel.h"
31 
32 namespace mindspore {
33 namespace kernel {
34 namespace tbe {
35 using std::string;
36 using std::vector;
37 class TbeUtils {
38  public:
39   TbeUtils() = default;
40 
41   ~TbeUtils() = default;
42 
43   static void SaveJsonInfo(const std::string &json_name, const std::string &info);
44 
45   static void LoadCache();
46 
47   static void GenLicInfo(nlohmann::json *lic_info_json);
48 
49   static nlohmann::json GenSocInfo();
50 
51   static std::string GetSocVersion();
52 
53   static std::string GetOpDebugPath();
54 
55   static std::string GetBankPath();
56 
57   static std::string GetTuneDumpPath();
58 
59   static void SaveCompileInfo(const std::string &json_name, const std::string &build_res, bool *save_flag);
60 
61   static void GetCompileInfo(const AnfNodePtr &node, std::string *compile_info, bool *get_flag);
62 
63   static bool CheckOfflineTune();
64 
65   static KernelPackPtr SearchCache(const std::string &kernel_name, const bool is_akg = false);
66 
67   static KernelPackPtr InsertCache(const std::string &kernel_name, const std::string &processor,
68                                    const bool is_akg = false);
69 };
70 
71 struct KernelMetaInfo {
72   uintptr_t func_stub_;
73   uint32_t block_dim_;
74 };
75 using KernelMetaPtr = std::shared_ptr<KernelMetaInfo>;
76 
77 class KernelManager {
78  public:
79   static uintptr_t GenFuncStub(const KernelPack &kernel_pack, bool force_reload, uint32_t *block_dim,
80                                const bool dynamic_flag = false, void **handle = nullptr,
81                                std::string *origin_key = nullptr);
82   static std::string GetStubFuncName(const KernelPackPtr &kernel_pack);
83 
84  private:
85   KernelManager() = default;
86   ~KernelManager() = default;
87   static int BinaryRegister(const FlexArray &kernel_buffer, void **module, const string &magic,
88                             const bool dynamic_flag);
89   static std::unordered_map<string, KernelMetaPtr> info_table_;
90   static uintptr_t kernel_stub_gen_;
91 };
92 
93 class KernelMeta {
94  public:
95   static KernelMeta *GetInstance();
96   bool ReadIndex(const std::string &bin_dir);
97   KernelPackPtr GetKernelPack(const std::string &kernel_name, const bool is_akg = false);
98 
99  private:
100   KernelMeta() = default;
101   ~KernelMeta() = default;
102   std::unordered_map<std::string, std::string> kernel_index_map_{};
103   std::unordered_map<std::string, KernelPackPtr> kernel_pack_map_{};
104 };
105 }  // namespace tbe
106 }  // namespace kernel
107 }  // namespace mindspore
108 
109 #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_TBE_UTILS_H_
110