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