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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class MS_API DelegateModel {
  32. public:
  33. /// \brief Constructor of MindSpore Lite DelegateModel.
  34. DelegateModel(std::vector<kernel::Kernel *> *kernels, const std::vector<MSTensor> &inputs,
  35. const std::vector<MSTensor> &outputs,
  36. const std::map<kernel::Kernel *, const schema::Primitive *> &primitives, SchemaVersion version)
  37. : kernels_(kernels), inputs_(inputs), outputs_(outputs), primitives_(primitives), version_(version) {}
  38. /// \brief Destructor of MindSpore Lite DelegateModel.
  39. ~DelegateModel() = default;
  40. /// \brief Get Primitive of kernel::Kernel.
  41. ///
  42. /// \param[in] a kernel in DelegateModel kernels vector.
  43. ///
  44. /// \return The schema::Primitive of The kernel.
  45. const schema::Primitive *GetPrimitive(kernel::Kernel *kernel) const;
  46. /// \brief Get the begin iterator of the DelegateModel kernels vector.
  47. ///
  48. /// \return The begin iterator of the DelegateModel kernels vector.
  49. KernelIter BeginKernelIterator();
  50. /// \brief Get the end iterator of the DelegateModel kernels vector.
  51. ///
  52. /// \return The end iterator of the DelegateModel kernels vector.
  53. KernelIter EndKernelIterator();
  54. /// \brief Replace the continuous kernel supported by the delegate with a delegate graph kernel.
  55. ///
  56. /// \param[in] from Define the begin iterator of continuous kernel supported by the delegate.
  57. /// \param[in] end Define the end iterator of continuous kernel supported by the delegate.
  58. ///
  59. /// \return The next iterator after graph_kernel, point to the next kernel that is not visited.
  60. KernelIter Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel);
  61. /// \brief Get the input tensors of DelegateModel.
  62. ///
  63. /// \return The input tensor vector of DelegateModel.
  64. const std::vector<mindspore::MSTensor> &inputs() { return this->inputs_; }
  65. /// \brief Get the output tensors of DelegateModel.
  66. ///
  67. /// \return The ioutput tensor vector of DelegateModel.
  68. const std::vector<mindspore::MSTensor> &outputs() { return this->outputs_; }
  69. /// \brief Get the ms model version.
  70. ///
  71. /// \return The schema version for the primitives map.
  72. const SchemaVersion GetVersion() { return version_; }
  73. protected:
  74. std::vector<kernel::Kernel *> *kernels_;
  75. const std::vector<mindspore::MSTensor> &inputs_;
  76. const std::vector<mindspore::MSTensor> &outputs_;
  77. const std::map<kernel::Kernel *, const schema::Primitive *> &primitives_;
  78. SchemaVersion version_;
  79. };
  80. class MS_API Delegate {
  81. public:
  82. /// \brief Constructor of MindSpore Lite Delegate.
  83. Delegate() = default;
  84. /// \brief Destructor of MindSpore Lite Delegate.
  85. virtual ~Delegate() = default;
  86. /// \brief Init delegate.
  87. ///
  88. /// \note Init willed be called in Model::Build.
  89. ///
  90. /// \return Status. If Status is kLiteNotSupport, the program will return to the MindSpore Lite inner inference.
  91. virtual Status Init() = 0;
  92. /// \brief Build delegate graph for MindSpore Lite model.
  93. ///
  94. /// \note Build willed be called in Model::Build.
  95. ///
  96. /// \param[in] model Define the delegate model to be built.
  97. virtual Status Build(DelegateModel *model) = 0;
  98. };
  99. } // namespace mindspore
  100. #endif // MINDSPORE_INCLUDE_API_DELEGATE_H