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.

delegate.h 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 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_DELEGATE_H
  17. #define MINDSPORE_INCLUDE_API_DELEGATE_H
  18. #include <map>
  19. #include <vector>
  20. #include <memory>
  21. #include "schema/model_generated.h"
  22. #include "include/api/kernel.h"
  23. #include "include/api/status.h"
  24. namespace mindspore {
  25. typedef enum {
  26. SCHEMA_INVALID = -1, /**< invalid version */
  27. SCHEMA_CUR, /**< current version for ms model defined in model.fbs*/
  28. SCHEMA_V0, /**< previous version for ms model defined in model_v0.fbs*/
  29. } SchemaVersion;
  30. using KernelIter = std::vector<kernel::Kernel *>::iterator;
  31. template <class T>
  32. class MS_API DelegateModel {
  33. public:
  34. /// \brief Constructor of MindSpore Lite DelegateModel.
  35. DelegateModel(std::vector<kernel::Kernel *> *kernels, const std::vector<MSTensor> &inputs,
  36. const std::vector<MSTensor> &outputs, const std::map<kernel::Kernel *, const T *> &primitives,
  37. SchemaVersion version)
  38. : kernels_(kernels), inputs_(inputs), outputs_(outputs), primitives_(primitives), version_(version) {}
  39. /// \brief Destructor of MindSpore Lite DelegateModel.
  40. ~DelegateModel() = default;
  41. /// \brief Get Primitive of kernel::Kernel.
  42. ///
  43. /// \param[in] a kernel in DelegateModel kernels vector.
  44. ///
  45. /// \return The Primitive of The kernel.
  46. const T *GetPrimitive(kernel::Kernel *kernel) const {
  47. if (primitives_.find(kernel) != primitives_.end()) {
  48. return primitives_.at(kernel);
  49. } else {
  50. return nullptr;
  51. }
  52. }
  53. /// \brief Get the begin iterator of the DelegateModel kernels vector.
  54. ///
  55. /// \return The begin iterator of the DelegateModel kernels vector.
  56. KernelIter BeginKernelIterator() { return kernels_->begin(); }
  57. /// \brief Get the end iterator of the DelegateModel kernels vector.
  58. ///
  59. /// \return The end iterator of the DelegateModel kernels vector.
  60. KernelIter EndKernelIterator() { return kernels_->end(); }
  61. /// \brief Replace the continuous kernel supported by the delegate with a delegate graph kernel.
  62. ///
  63. /// \param[in] from Define the begin iterator of continuous kernel supported by the delegate.
  64. /// \param[in] end Define the end iterator of continuous kernel supported by the delegate.
  65. ///
  66. /// \return The next iterator after graph_kernel, point to the next kernel that is not visited.
  67. KernelIter Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel) {
  68. size_t insert_index = from - BeginKernelIterator();
  69. if (insert_index >= kernels_->size()) {
  70. return BeginKernelIterator();
  71. }
  72. kernels_->erase(from, end);
  73. kernels_->insert(BeginKernelIterator() + insert_index, graph_kernel);
  74. return BeginKernelIterator() + insert_index + 1;
  75. }
  76. /// \brief Get the input tensors of DelegateModel.
  77. ///
  78. /// \return The input tensor vector of DelegateModel.
  79. const std::vector<mindspore::MSTensor> &inputs() { return this->inputs_; }
  80. /// \brief Get the output tensors of DelegateModel.
  81. ///
  82. /// \return The ioutput tensor vector of DelegateModel.
  83. const std::vector<mindspore::MSTensor> &outputs() { return this->outputs_; }
  84. /// \brief Get the ms model version.
  85. ///
  86. /// \return The schema version for the primitives map.
  87. SchemaVersion GetVersion() const { return version_; }
  88. protected:
  89. std::vector<kernel::Kernel *> *kernels_;
  90. const std::vector<mindspore::MSTensor> &inputs_;
  91. const std::vector<mindspore::MSTensor> &outputs_;
  92. const std::map<kernel::Kernel *, const T *> &primitives_;
  93. SchemaVersion version_;
  94. };
  95. class MS_API Delegate {
  96. public:
  97. /// \brief Constructor of MindSpore Lite Delegate.
  98. Delegate() = default;
  99. /// \brief Destructor of MindSpore Lite Delegate.
  100. virtual ~Delegate() = default;
  101. /// \brief Init delegate.
  102. ///
  103. /// \note Init willed be called in Model::Build.
  104. ///
  105. /// \return Status. If Status is kLiteNotSupport, the program will return to the MindSpore Lite inner inference.
  106. virtual Status Init() = 0;
  107. /// \brief Build delegate graph for MindSpore Lite model.
  108. ///
  109. /// \note Build willed be called in Model::Build.
  110. ///
  111. /// \param[in] model Define the delegate model to be built.
  112. virtual Status Build(DelegateModel<schema::Primitive> *model) = 0;
  113. };
  114. } // namespace mindspore
  115. #endif // MINDSPORE_INCLUDE_API_DELEGATE_H