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.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. kDynamicFormatPattern = 4,
  55. };
  56. // Backend processor
  57. enum Processor {
  58. UNKNOWN = -1,
  59. AICORE = 0,
  60. AICPU,
  61. CUDA,
  62. };
  63. struct FlexArray {
  64. size_t len;
  65. char contents[];
  66. };
  67. struct KernelJsonInfo {
  68. std::string bin_file_name;
  69. std::string bin_file_suffix;
  70. uint32_t block_dim;
  71. std::string kernel_name;
  72. std::string magic;
  73. std::vector<size_t> parameters;
  74. std::string sha256;
  75. std::vector<size_t> workspaces;
  76. uint32_t op_para_size;
  77. KernelJsonInfo() : block_dim(0), op_para_size(0) {}
  78. };
  79. class KernelPack {
  80. public:
  81. KernelPack() : json_(nullptr), kernel_(nullptr) {}
  82. KernelPack(const KernelPack &) = default;
  83. KernelJsonInfo kernel_json_info() const;
  84. bool LoadKernelMeta(const std::string &json_f, const std::string &processor);
  85. bool ReadFromJsonFile(const std::string &json_f, const std::string &processor);
  86. const std::string Serialize() const;
  87. const FlexArray *GetJson() const { return json_; }
  88. const FlexArray *GetKernel() const { return kernel_; }
  89. ~KernelPack() {
  90. if (json_) {
  91. delete[] json_;
  92. json_ = nullptr;
  93. }
  94. if (kernel_) {
  95. delete[] kernel_;
  96. kernel_ = nullptr;
  97. }
  98. }
  99. private:
  100. bool ReadFromJsonFileHelper(std::ifstream &kernelbin);
  101. void ParseKernelJson(const nlohmann::json &js);
  102. KernelJsonInfo kernel_json_info_;
  103. FlexArray *json_;
  104. FlexArray *kernel_;
  105. };
  106. using KernelPackPtr = std::shared_ptr<KernelPack>;
  107. /**
  108. * @brief base class for autotensor kernel and cce kernel.
  109. */
  110. struct Address {
  111. void *addr;
  112. size_t size;
  113. };
  114. using AddressPtr = std::shared_ptr<Address>;
  115. class KernelMod {
  116. public:
  117. virtual const std::vector<size_t> &GetInputSizeList() const = 0;
  118. virtual const std::vector<size_t> &GetOutputSizeList() const = 0;
  119. virtual const std::vector<size_t> &GetWorkspaceSizeList() const = 0;
  120. virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  121. const std::vector<AddressPtr> &outputs, void *stream_ptr) = 0;
  122. virtual device::DynamicKernelPtr GenDynamicKernel(const CNodePtr &cnode_ptr, void *stream_ptr) { return nullptr; }
  123. virtual std::vector<size_t> GenParameters() { return {}; }
  124. virtual void ReleaseResource() {}
  125. virtual ~KernelMod() = default;
  126. void set_kernel_name(const std::string &kernel_name) { kernel_name_ = kernel_name; }
  127. protected:
  128. std::string kernel_name_;
  129. };
  130. using KernelModPtr = std::shared_ptr<KernelMod>;
  131. } // namespace kernel
  132. } // namespace mindspore
  133. #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_KERNEL_H_