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