You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

kernel.h 6.2 kB

4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * Copyright 2019-2020 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_KERNEL_H_
  17. #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_KERNEL_H_
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include "nlohmann/json.hpp"
  22. #include "ir/anf.h"
  23. #include "ir/dtype.h"
  24. #include "utils/utils.h"
  25. #include "ir/tensor.h"
  26. #include "abstract/dshape.h"
  27. #include "utils/log_adapter.h"
  28. #include "runtime/device/executor/dynamic_kernel.h"
  29. #ifdef _MSC_VER
  30. #undef OPAQUE
  31. #endif
  32. namespace mindspore {
  33. enum KernelType : int {
  34. UNKNOWN_KERNEL_TYPE = 0,
  35. AKG_KERNEL,
  36. AICPU_KERNEL,
  37. RT_KERNEL,
  38. HCCL_KERNEL,
  39. TBE_KERNEL,
  40. HOST_KERNEL,
  41. CPU_KERNEL,
  42. GPU_KERNEL,
  43. };
  44. namespace kernel {
  45. // Supported fusion type
  46. enum FusionType {
  47. CONV = 0,
  48. ELEMWISE,
  49. COMMREDUCE,
  50. SEGMENT,
  51. OPAQUE,
  52. BN_UPDATE_GRAD,
  53. BN_GRAD_REDUCE,
  54. LAYER_NORM_GRAD,
  55. L2LOSS_MUL_ADDN,
  56. PURE_BROADCAST,
  57. INPLACE,
  58. MATMUL,
  59. MATMUL_V2,
  60. GEMM,
  61. CONV2D_BACKPROP_INPUT,
  62. CONV2D_BACKPROP_FILTER,
  63. CONV3D_BACKPROP_INPUT,
  64. CONV3D_BACKPROP_FILTER,
  65. CUBE_LAYER_NORM,
  66. BN_REDUCE,
  67. BN_UPDATE,
  68. SOFTMAX_CROSS_ENTROPY_WITH_LOGITS,
  69. L2_NORMALIZE,
  70. SOFTMAX,
  71. L2_LOSS,
  72. ASCEND_QUANT,
  73. ASCEND_DEQUANT,
  74. ASCEND_ANTI_QUANT,
  75. STRIDED_READ,
  76. STRIDED_WRITE,
  77. ASCEND_DEQUANT_S16,
  78. ASCEND_REQUANT,
  79. ASCEND_REQUANT_S16,
  80. MAX_POOL,
  81. DEPTHWISECONV,
  82. CONV3D,
  83. POOL2D,
  84. POOL3D,
  85. READ_SELECT,
  86. WRITE_SELECT,
  87. COSINE_EMBEDDING_LOSS,
  88. DILATION_PATTERN,
  89. BROAD_CAST,
  90. BATCH_MATMUL,
  91. CONFUSION_TRANSPOSE,
  92. UNKNOWN_FUSION_TYPE = -1,
  93. };
  94. enum OpPattern {
  95. kCommonPattern = 0,
  96. kFormatAgnosticPattern = 1,
  97. kBroadcastPattern = 2,
  98. kReducePattern = 3,
  99. };
  100. // Backend processor
  101. enum Processor {
  102. UNKNOWN = -1,
  103. AICORE = 0,
  104. AICPU,
  105. CUDA,
  106. CPU,
  107. };
  108. struct FlexArray {
  109. size_t len;
  110. char contents[];
  111. };
  112. struct KernelJsonInfo {
  113. std::string bin_file_name;
  114. std::string bin_file_suffix;
  115. uint32_t block_dim;
  116. std::string kernel_name;
  117. std::string magic;
  118. std::vector<size_t> parameters;
  119. std::string sha256;
  120. std::vector<size_t> workspaces;
  121. uint32_t op_para_size;
  122. KernelJsonInfo() : block_dim(0), op_para_size(0) {}
  123. };
  124. class KernelPack {
  125. public:
  126. KernelPack() : json_(nullptr), kernel_(nullptr) {}
  127. KernelPack(const KernelPack &) = default;
  128. KernelJsonInfo kernel_json_info() const;
  129. bool LoadKernelMeta(const std::string &json_f);
  130. bool ReadFromJsonFile(const std::string &json_f, const std::string &processor);
  131. const FlexArray *GetJson() const { return json_; }
  132. const FlexArray *GetKernel() const { return kernel_; }
  133. ~KernelPack() {
  134. if (json_ != nullptr) {
  135. delete[] json_;
  136. json_ = nullptr;
  137. }
  138. if (kernel_ != nullptr) {
  139. delete[] kernel_;
  140. kernel_ = nullptr;
  141. }
  142. }
  143. private:
  144. bool ReadFromJsonFileHelper(std::ifstream &kernel_bin);
  145. void ParseKernelJson(const nlohmann::json &js);
  146. KernelJsonInfo kernel_json_info_;
  147. FlexArray *json_;
  148. FlexArray *kernel_;
  149. };
  150. using KernelPackPtr = std::shared_ptr<KernelPack>;
  151. /**
  152. * @brief base class for autotensor kernel and cce kernel.
  153. */
  154. struct Address {
  155. Address() : addr(nullptr), size(0) {}
  156. Address(void *address_addr, size_t address_size) : addr(address_addr), size(address_size) {}
  157. void *addr;
  158. size_t size;
  159. };
  160. using AddressPtr = std::shared_ptr<Address>;
  161. using AddressPtrList = std::vector<AddressPtr>;
  162. // The memory info of kernel launch.
  163. struct KernelLaunchInfo {
  164. AddressPtrList inputs_;
  165. AddressPtrList outputs_;
  166. AddressPtrList workspaces_;
  167. };
  168. class KernelMod {
  169. public:
  170. virtual const std::vector<size_t> &GetInputSizeList() const = 0;
  171. virtual const std::vector<size_t> &GetOutputSizeList() const = 0;
  172. virtual const std::vector<size_t> &GetWorkspaceSizeList() const = 0;
  173. bool Launch(const KernelLaunchInfo &kernel_launch_address, void *stream_ptr) {
  174. return Launch(kernel_launch_address.inputs_, kernel_launch_address.workspaces_, kernel_launch_address.outputs_,
  175. stream_ptr);
  176. }
  177. virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  178. const std::vector<AddressPtr> &outputs, void *stream_ptr) = 0;
  179. virtual device::DynamicKernelPtr GenDynamicKernel(const CNodePtr &cnode_ptr, void *stream_ptr) { return nullptr; }
  180. virtual std::vector<size_t> GenParameters() { return {}; }
  181. virtual void ReleaseResource() {}
  182. virtual ~KernelMod() = default;
  183. void set_unique_name(const std::string &unique_name) { unique_name_ = unique_name; }
  184. void set_fullname(const std::string &fullname) { fullname_ = fullname; }
  185. void set_is_monad(bool is_monad) { is_monad_ = is_monad; }
  186. void set_inputs_addr(const std::vector<AddressPtr> &addr) { inputs_addr_ = addr; }
  187. void set_workspaces_addr(const std::vector<AddressPtr> &addr) { workspaces_addr_ = addr; }
  188. void set_outputs_addr(const std::vector<AddressPtr> &addr) { outputs_addr_ = addr; }
  189. const std::vector<AddressPtr> &GetInputsAddr() { return inputs_addr_; }
  190. const std::vector<AddressPtr> &GetWorkSpacesAddr() { return workspaces_addr_; }
  191. const std::vector<AddressPtr> &GetOutputsAddr() { return outputs_addr_; }
  192. void SetStream(void *stream) { stream_ = stream; }
  193. void *GetStream() const { return stream_; }
  194. protected:
  195. std::string kernel_name_;
  196. std::string unique_name_;
  197. std::string fullname_;
  198. bool is_monad_{false};
  199. void *stream_{nullptr};
  200. private:
  201. std::vector<AddressPtr> inputs_addr_;
  202. std::vector<AddressPtr> workspaces_addr_;
  203. std::vector<AddressPtr> outputs_addr_;
  204. };
  205. using KernelModPtr = std::shared_ptr<KernelMod>;
  206. } // namespace kernel
  207. } // namespace mindspore
  208. #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_KERNEL_H_