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 4.1 kB

4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. namespace mindspore {
  30. enum KernelType : int {
  31. UNKNOWN_KERNEL_TYPE = 0,
  32. AKG_KERNEL,
  33. AICPU_KERNEL,
  34. RT_KERNEL,
  35. HCCL_KERNEL,
  36. TBE_KERNEL,
  37. HOST_KERNEL
  38. };
  39. namespace kernel {
  40. // Supported fusion type
  41. enum FusionType {
  42. CONVLUTION = 0,
  43. ELEMWISE,
  44. COMMREDUCE,
  45. SEGMENT,
  46. OPAQUE,
  47. UNKNOWN_FUSION_TYPE = -1,
  48. };
  49. enum OpPattern {
  50. kCommonPattern = 0,
  51. kFormatAgnosticPattern = 1,
  52. kBroadcastPattern = 2,
  53. kReducePattern = 3,
  54. };
  55. // Backend processor
  56. enum Processor {
  57. UNKNOWN = -1,
  58. AICORE = 0,
  59. AICPU,
  60. CUDA,
  61. };
  62. struct FlexArray {
  63. size_t len;
  64. char contents[];
  65. };
  66. struct KernelJsonInfo {
  67. std::string bin_file_name;
  68. std::string bin_file_suffix;
  69. uint32_t block_dim;
  70. std::string kernel_name;
  71. std::string magic;
  72. std::vector<size_t> parameters;
  73. std::string sha256;
  74. std::vector<size_t> workspaces;
  75. uint32_t op_para_size;
  76. KernelJsonInfo() : block_dim(0), op_para_size(0) {}
  77. };
  78. class KernelPack {
  79. public:
  80. KernelPack() : json_(nullptr), kernel_(nullptr) {}
  81. KernelPack(const KernelPack &) = default;
  82. KernelJsonInfo kernel_json_info() const;
  83. bool LoadKernelMeta(const std::string &json_f, const std::string &processor);
  84. bool ReadFromJsonFile(const std::string &json_f, const std::string &processor);
  85. const std::string Serialize() const;
  86. const FlexArray *GetJson() const { return json_; }
  87. const FlexArray *GetKernel() const { return kernel_; }
  88. ~KernelPack() {
  89. if (json_) {
  90. delete[] json_;
  91. json_ = nullptr;
  92. }
  93. if (kernel_) {
  94. delete[] kernel_;
  95. kernel_ = nullptr;
  96. }
  97. }
  98. private:
  99. bool ReadFromJsonFileHelper(std::ifstream &kernelbin);
  100. void ParseKernelJson(const nlohmann::json &js);
  101. KernelJsonInfo kernel_json_info_;
  102. FlexArray *json_;
  103. FlexArray *kernel_;
  104. };
  105. using KernelPackPtr = std::shared_ptr<KernelPack>;
  106. /**
  107. * @brief base class for autotensor kernel and cce kernel.
  108. */
  109. struct Address {
  110. Address() : addr(nullptr), size(0) {}
  111. Address(void *address_addr, size_t address_size) : addr(address_addr), size(address_size) {}
  112. void *addr;
  113. size_t size;
  114. };
  115. using AddressPtr = std::shared_ptr<Address>;
  116. class KernelMod {
  117. public:
  118. virtual const std::vector<size_t> &GetInputSizeList() const = 0;
  119. virtual const std::vector<size_t> &GetOutputSizeList() const = 0;
  120. virtual const std::vector<size_t> &GetWorkspaceSizeList() const = 0;
  121. virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  122. const std::vector<AddressPtr> &outputs, void *stream_ptr) = 0;
  123. virtual device::DynamicKernelPtr GenDynamicKernel(const CNodePtr &cnode_ptr, void *stream_ptr) { return nullptr; }
  124. virtual std::vector<size_t> GenParameters() { return {}; }
  125. virtual void ReleaseResource() {}
  126. virtual ~KernelMod() = default;
  127. void set_kernel_name(const std::string &kernel_name) { kernel_name_ = kernel_name; }
  128. protected:
  129. std::string kernel_name_;
  130. };
  131. using KernelModPtr = std::shared_ptr<KernelMod>;
  132. } // namespace kernel
  133. } // namespace mindspore
  134. #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_KERNEL_H_