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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "schema/model_generated.h"
  22. #include "include/api/types.h"
  23. #include "include/api/context.h"
  24. namespace mindspore::kernel {
  25. /// \brief The Kernel class is used to define a MindSpore Kernel.
  26. class Kernel {
  27. public:
  28. Kernel() = default;
  29. /// \brief Constructor.
  30. ///
  31. /// \param[in] inputs define the input tensors for kernel.
  32. /// \param[in] outputs define the output tensors for kernel.
  33. /// \param[in] primitive define the primitive of kernel generated by flatbuffers.
  34. /// \param[in] ctx define the context for kernel.
  35. Kernel(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs,
  36. const schema::Primitive *primitive, const mindspore::Context *ctx)
  37. : context_(ctx), inputs_(std::move(inputs)), outputs_(std::move(outputs)), primitive_(primitive) {
  38. if (primitive != nullptr) {
  39. type_ = primitive->value_type();
  40. }
  41. }
  42. /// \brief Destructor.
  43. virtual ~Kernel() = default;
  44. /// \brief prepare for executing kernel.
  45. ///
  46. /// \return result code.
  47. virtual int Prepare() = 0;
  48. /// \brief execute the kernel.
  49. ///
  50. /// \return result code.
  51. virtual int Execute() = 0;
  52. /// \brief resize the kernel input shape, memory need to refresh.
  53. ///
  54. /// \return result code.
  55. virtual int ReSize() = 0;
  56. /// \brief set kernel's input tensors.
  57. ///
  58. /// \param[in] in_tensors define the input tensors.
  59. virtual void set_inputs(const std::vector<mindspore::MSTensor> &in_tensors) { this->inputs_ = in_tensors; }
  60. /// \brief set kernel's input tensor.
  61. ///
  62. /// \param[in] in_tensor define the input tensor.
  63. /// \param[in] index define the index of the input tensor.
  64. virtual void set_input(mindspore::MSTensor in_tensor, int index) { this->inputs_[index] = in_tensor; }
  65. /// \brief set kernel's output tensors.
  66. ///
  67. /// \param[in] out_tensors define the output tensors.
  68. virtual void set_outputs(const std::vector<mindspore::MSTensor> &out_tensors) { this->outputs_ = out_tensors; }
  69. /// \brief set kernel's output tensor.
  70. ///
  71. /// \param[in] out_tensor define the output tensor.
  72. /// \param[in] index define the index of the output tensor.
  73. virtual void set_output(mindspore::MSTensor out_tensor, int index) { this->outputs_[index] = out_tensor; }
  74. /// \brief obtain kernel's input tensors.
  75. ///
  76. /// \return input tensors.
  77. virtual const std::vector<mindspore::MSTensor> &inputs() { return this->inputs_; }
  78. /// \brief obtain kernel's output tensors.
  79. ///
  80. /// \return output tensors.
  81. virtual const std::vector<mindspore::MSTensor> &outputs() { return this->outputs_; }
  82. /// \brief obtain kernel's name.
  83. ///
  84. /// \return kernel's name.
  85. std::string name() const { return this->name_; }
  86. /// \brief set kernel's name.
  87. ///
  88. /// \param[in] name define the kernel's name.
  89. void set_name(const std::string &name) { this->name_ = name; }
  90. /// \brief obtain kernel's context.
  91. ///
  92. /// \return kernel's context.
  93. const mindspore::Context *context() const { return this->context_; }
  94. /// \brief obtain kernel's type.
  95. ///
  96. /// \return kernel's type.
  97. virtual schema::PrimitiveType type() const { return type_; }
  98. /// \brief obtain the primitive of kernel generated by flatbuffers.
  99. ///
  100. /// \return the primitive of kernel generated by flatbuffers.
  101. const schema::Primitive *primitive() const { return this->primitive_; }
  102. protected:
  103. std::string name_;
  104. const mindspore::Context *context_ = nullptr;
  105. std::vector<mindspore::MSTensor> inputs_;
  106. std::vector<mindspore::MSTensor> outputs_;
  107. schema::PrimitiveType type_ = schema::PrimitiveType_NONE;
  108. const schema::Primitive *primitive_ = nullptr;
  109. };
  110. } // namespace mindspore::kernel
  111. #endif // MINDSPORE_INCLUDE_API_KERNEL_H