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