Publishing 2020.1 content
[platform/upstream/dldt.git] / inference-engine / include / ie_iextension.h
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief This is a header file for Inference Engine Extension Interface
7  *
8  * @file ie_iextension.h
9  */
10 #pragma once
11
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16
17 #include "details/ie_no_copy.hpp"
18 #include "ie_api.h"
19 #include "ie_error.hpp"
20 #include "ie_layers.h"
21 #include "ie_version.hpp"
22
23 /**
24  * @def INFERENCE_EXTENSION_API(TYPE)
25  * @brief Defines Inference Engine Extension API method
26  */
27
28 #if defined(_WIN32) && defined(IMPLEMENT_INFERENCE_EXTENSION_API)
29 #define INFERENCE_EXTENSION_API(TYPE) extern "C" __declspec(dllexport) TYPE
30 #else
31 #define INFERENCE_EXTENSION_API(TYPE) INFERENCE_ENGINE_API(TYPE)
32 #endif
33
34 namespace ngraph {
35
36 class OpSet;
37
38 }  // namespace ngraph
39
40 namespace InferenceEngine {
41
42 /**
43  * @struct DataConfig
44  * @brief This structure describes data configuration
45  */
46 struct DataConfig {
47     /**
48      * @brief Format of memory descriptor
49      */
50     TensorDesc desc;
51     /**
52      * @brief Index of in-place memory. If -1 memory cannot be in-place
53      */
54     int inPlace = -1;
55     /**
56      * @brief Flag for determination of the constant memory. If layer contains all constant memory we can calculate it
57      * on the load stage.
58      */
59     bool constant = false;
60 };
61
62 /**
63  * @struct LayerConfig
64  * @brief This structure describes Layer configuration
65  */
66 struct LayerConfig {
67     /**
68      * @brief Supported dynamic batch. If false, dynamic batch is not supported
69      */
70     bool dynBatchSupport = false;
71     /**
72      * @brief Vector of input data configs
73      */
74     std::vector<DataConfig> inConfs;
75     /**
76      * @brief Vector of output data configs
77      */
78     std::vector<DataConfig> outConfs;
79 };
80
81 /**
82  * @brief This class provides interface for extension implementations
83  */
84 class ILayerImpl {
85 public:
86     using Ptr = std::shared_ptr<ILayerImpl>;
87
88     /**
89      * @brief Destructor
90      */
91     virtual ~ILayerImpl() = default;
92
93     /**
94      * @brief Gets all supported configurations for the current layer
95      *
96      * @param conf Vector with supported configurations
97      * @param resp Response descriptor
98      * @return Status code
99      */
100     virtual StatusCode getSupportedConfigurations(std::vector<LayerConfig>& conf, ResponseDesc* resp) noexcept = 0;
101
102     /**
103      * @brief Initializes the implementation
104      *
105      * @param config Selected supported configuration
106      * @param resp Response descriptor
107      * @return Status code
108      */
109     virtual StatusCode init(LayerConfig& config, ResponseDesc* resp) noexcept = 0;
110 };
111
112 /**
113  * @brief This class provides interface for the implementation with the custom execution code
114  */
115 class ILayerExecImpl : public ILayerImpl {
116 public:
117     /**
118      * @brief Execute method
119      *
120      * @param inputs Vector of blobs with input memory
121      * @param outputs Vector of blobs with output memory
122      * @param resp Response descriptor
123      * @return Status code
124      */
125     virtual StatusCode execute(std::vector<Blob::Ptr>& inputs, std::vector<Blob::Ptr>& outputs,
126                                ResponseDesc* resp) noexcept = 0;
127 };
128
129 /**
130  * @brief This class provides interface for extension factories
131  */
132 class ILayerImplFactory {
133 public:
134     using Ptr = std::shared_ptr<ILayerImplFactory>;
135     using ImplCreator = std::function<ILayerImpl*()>;
136
137     /**
138      * @brief Destructor
139      */
140     virtual ~ILayerImplFactory() = default;
141
142     /**
143      * @brief Gets all possible implementations for the given cnn Layer
144      *
145      * @param impls the vector with implementations which is ordered by priority
146      * @param resp response descriptor
147      * @return status code
148      */
149     virtual StatusCode getImplementations(std::vector<ILayerImpl::Ptr>& impls, ResponseDesc* resp) noexcept = 0;
150 };
151
152 /**
153  * @class IShapeInferImpl
154  * @brief This class provides interface for the implementation with the custom execution code
155  */
156 class IShapeInferImpl {
157 public:
158     using Ptr = std::shared_ptr<IShapeInferImpl>;
159
160     virtual ~IShapeInferImpl() = default;
161
162     /**
163      * @brief check that reshape can be applied, that parameters and shapes are valid
164      */
165     virtual StatusCode inferShapes(const std::vector<Blob::CPtr>& /*inBlobs*/,
166                                    const std::map<std::string, std::string>& /*params*/,
167                                    const std::map<std::string, Blob::Ptr>& /*blobs*/,
168                                    std::vector<SizeVector>& /*outShapes*/, ResponseDesc* /*resp*/) noexcept {
169         return NOT_IMPLEMENTED;
170     }  // For backward-compatibility
171 };
172
173 /**
174  * @class IShapeInferExtension
175  * @brief This class is the reader extension interface to provide implementation for shape propagation
176  */
177 class IShapeInferExtension : public InferenceEngine::details::IRelease {
178 public:
179     /**
180      * @brief Sets logging callback.
181      *
182      * Logging is used to track what is going on inside.
183      *
184      * @param listener Logging sink
185      */
186     virtual void SetLogCallback(InferenceEngine::IErrorListener& listener) noexcept = 0;
187
188     /**
189      * @brief Gets extension version information and stores in versionInfo
190      *
191      * @param versionInfo Pointer to version info, will be set by plugin
192      */
193     virtual void GetVersion(const InferenceEngine::Version*& versionInfo) const noexcept = 0;
194
195     /**
196      * @brief Cleans resources up
197      */
198     virtual void Unload() noexcept = 0;
199
200     /**
201      * @brief Fills passed array with types of layers which shape infer implementations are included in the extension
202      *
203      * @param types Array to store the layer types
204      * @param size Size of the layer types array
205      * @param resp Response descriptor
206      * @return Status code
207      */
208     virtual StatusCode getShapeInferTypes(char**& types, unsigned int& size, ResponseDesc* resp) noexcept = 0;
209
210     /**
211      * @brief Gets shape propagation implementation for the given string-type of cnn Layer
212      *
213      * @param impl the vector with implementations which is ordered by priority
214      * @param resp response descriptor
215      * @return status code
216      */
217     virtual StatusCode getShapeInferImpl(IShapeInferImpl::Ptr& impl, const char* type, ResponseDesc* resp) noexcept = 0;
218 };
219
220 /**
221  * @brief This class is the main extension interface
222  */
223 class IExtension : public IShapeInferExtension {
224 public:
225     virtual StatusCode getFactoryFor(ILayerImplFactory*& factory, const CNNLayer* cnnLayer,
226                                      ResponseDesc* resp) noexcept = 0;
227
228     /**
229      * @brief Fills passed array with types of layers which kernel implementations are included in the extension
230      *
231      * @param types Array to store the layer types
232      * @param size Size of the layer types array
233      * @param resp Response descriptor
234      * @return Status code
235      */
236     virtual StatusCode getPrimitiveTypes(char**& types, unsigned int& size, ResponseDesc* resp) noexcept = 0;
237
238     StatusCode getShapeInferTypes(char**&, unsigned int&, ResponseDesc*) noexcept override {
239         return NOT_IMPLEMENTED;
240     };
241
242     StatusCode getShapeInferImpl(IShapeInferImpl::Ptr&, const char*, ResponseDesc*) noexcept override {
243         return NOT_IMPLEMENTED;
244     };
245
246     /**
247      * @brief Returns operation sets
248      * This method throws an exception if it was not implemented
249      * @return map of opset name to opset
250      */
251     virtual std::map<std::string, ngraph::OpSet> getOpSets() {
252         THROW_IE_EXCEPTION << "Method is not implemented!";
253     }
254 };
255
256 using IExtensionPtr = std::shared_ptr<IExtension>;
257 using IShapeInferExtensionPtr = std::shared_ptr<IShapeInferExtension>;
258
259 /**
260  * @brief Creates the default instance of the extension
261  *
262  * @param ext Extension interface
263  * @param resp Response description
264  * @return Status code
265  */
266 INFERENCE_EXTENSION_API(StatusCode) CreateExtension(IExtension*& ext, ResponseDesc* resp) noexcept;
267
268 /**
269  * @brief Creates the default instance of the shape infer extension
270  *
271  * @param ext Shape Infer Extension interface
272  * @param resp Response description
273  * @return Status code
274  */
275 INFERENCE_EXTENSION_API(StatusCode) CreateShapeInferExtension(IShapeInferExtension*& ext, ResponseDesc* resp) noexcept;
276
277 }  // namespace InferenceEngine