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

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