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

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