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