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

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