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

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