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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright 2021 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_INCLUDE_API_KERNEL_H
  17. #define MINDSPORE_INCLUDE_API_KERNEL_H
  18. #include <vector>
  19. #include <string>
  20. #include <utility>
  21. #include <map>
  22. #include "schema/model_generated.h"
  23. #include "include/api/types.h"
  24. #include "include/api/context.h"
  25. namespace mindspore::kernel {
  26. /// \brief The Kernel class is used to define a MindSpore Kernel.
  27. class MS_API Kernel {
  28. public:
  29. Kernel() = default;
  30. /// \brief Constructor.
  31. ///
  32. /// \param[in] inputs define the input tensors for kernel.
  33. /// \param[in] outputs define the output tensors for kernel.
  34. /// \param[in] primitive define the primitive of kernel generated by flatbuffers.
  35. /// \param[in] ctx define the context for kernel.
  36. Kernel(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs,
  37. const schema::Primitive *primitive, const mindspore::Context *ctx)
  38. : context_(ctx), inputs_(std::move(inputs)), outputs_(std::move(outputs)), primitive_(primitive) {
  39. Initialize();
  40. }
  41. /// \brief Destructor.
  42. virtual ~Kernel() = default;
  43. /// \brief prepare for executing kernel.
  44. ///
  45. /// \return result code.
  46. virtual int Prepare() = 0;
  47. /// \brief execute the kernel.
  48. ///
  49. /// \return result code.
  50. virtual int Execute() = 0;
  51. /// \brief resize the kernel input shape, memory need to refresh.
  52. ///
  53. /// \return result code.
  54. virtual int ReSize() = 0;
  55. /// \brief set kernel's input tensors.
  56. ///
  57. /// \param[in] in_tensors define the input tensors.
  58. virtual void set_inputs(const std::vector<mindspore::MSTensor> &in_tensors) { this->inputs_ = in_tensors; }
  59. /// \brief set kernel's input tensor.
  60. ///
  61. /// \param[in] in_tensor define the input tensor.
  62. /// \param[in] index define the index of the input tensor.
  63. virtual void set_input(mindspore::MSTensor in_tensor, int index) { this->inputs_[index] = in_tensor; }
  64. /// \brief set kernel's output tensors.
  65. ///
  66. /// \param[in] out_tensors define the output tensors.
  67. virtual void set_outputs(const std::vector<mindspore::MSTensor> &out_tensors) { this->outputs_ = out_tensors; }
  68. /// \brief set kernel's output tensor.
  69. ///
  70. /// \param[in] out_tensor define the output tensor.
  71. /// \param[in] index define the index of the output tensor.
  72. virtual void set_output(mindspore::MSTensor out_tensor, int index) { this->outputs_[index] = out_tensor; }
  73. /// \brief obtain kernel's input tensors.
  74. ///
  75. /// \return input tensors.
  76. virtual const std::vector<mindspore::MSTensor> &inputs() { return this->inputs_; }
  77. /// \brief obtain kernel's output tensors.
  78. ///
  79. /// \return output tensors.
  80. virtual const std::vector<mindspore::MSTensor> &outputs() { return this->outputs_; }
  81. /// \brief obtain kernel's name.
  82. ///
  83. /// \return kernel's name.
  84. std::string name() const { return this->name_; }
  85. /// \brief set kernel's name.
  86. ///
  87. /// \param[in] name define the kernel's name.
  88. void set_name(const std::string &name) { this->name_ = name; }
  89. /// \brief obtain kernel's context.
  90. ///
  91. /// \return kernel's context.
  92. const mindspore::Context *context() const { return this->context_; }
  93. /// \brief obtain kernel's type.
  94. ///
  95. /// \return kernel's type.
  96. virtual schema::PrimitiveType type() const { return type_; }
  97. /// \brief obtain kernel's quant type.
  98. ///
  99. /// \return kernel's quant type.
  100. virtual schema::QuantType quant_type() const { return quant_type_; }
  101. /// \brief obtain the primitive of kernel generated by flatbuffers.
  102. ///
  103. /// \return the primitive of kernel generated by flatbuffers.
  104. const schema::Primitive *primitive() const { return this->primitive_; }
  105. /// \brief get kernel's attribute.
  106. ///
  107. /// \param[in] key define the kernel's attribute key.
  108. std::string GetAttr(const std::string &key) const {
  109. auto iter = attrs_.find(key);
  110. if (iter != attrs_.end()) {
  111. return iter->second;
  112. }
  113. return "";
  114. }
  115. /// \brief set kernel's config.
  116. ///
  117. /// \param[in] config define the kernel's config.
  118. void SetConfig(const std::map<std::string, std::map<std::string, std::string>> *config) {
  119. config_ = config;
  120. }
  121. /// \brief set kernel's config.
  122. ///
  123. /// \param[in] config define the kernel's config.
  124. std::map<std::string, std::string> GetConfig(const std::string &section) const {
  125. if (config_ == nullptr) {
  126. return std::map<std::string, std::string>();
  127. }
  128. auto iter = config_->find(section);
  129. if (iter != config_->end()) {
  130. return iter->second;
  131. }
  132. return std::map<std::string, std::string>();
  133. }
  134. protected:
  135. /// \brief set kernel's attribute
  136. ///
  137. /// \param[in] key define the kernel's attribute key.
  138. /// \param[in] value define the kernel's attribute value.
  139. void SetAttr(const std::string &key, const std::string &value) { attrs_[key] = value; }
  140. std::string name_;
  141. const mindspore::Context *context_ = nullptr;
  142. std::vector<mindspore::MSTensor> inputs_;
  143. std::vector<mindspore::MSTensor> outputs_;
  144. schema::PrimitiveType type_ = schema::PrimitiveType_NONE;
  145. const schema::Primitive *primitive_ = nullptr;
  146. std::map<std::string, std::string> attrs_;
  147. const std::map<std::string, std::map<std::string, std::string>> *config_;
  148. schema::QuantType quant_type_ = schema::QuantType_QUANT_NONE;
  149. private:
  150. void Initialize();
  151. };
  152. } // namespace mindspore::kernel
  153. #endif // MINDSPORE_INCLUDE_API_KERNEL_H