Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / inference-engine / include / ie_inetwork.hpp
1 // Copyright (C) 2018 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief a header file for the Inference Engine Network interface
7  * @file ie_inetwork.hpp
8  */
9 #pragma once
10
11 #include <utility>
12 #include <string>
13 #include <memory>
14 #include <vector>
15 #include <map>
16 #include <ie_parameter.hpp>
17 #include <ie_context.hpp>
18 #include <ie_layouts.h>
19 #include <ie_blob.h>
20
21 namespace InferenceEngine {
22
23 /**
24  * @brief A type of network objects indexes.
25  */
26 using idx_t = size_t;
27
28 /**
29  * @brief This class contains a pair from layerId and port index
30  */
31 class PortInfo {
32 public:
33     /**
34      * @brief The constructor creates a PortInfo object for port 0
35      * @param layerID Layer id
36      */
37     PortInfo(idx_t layerID): layer(layerID), port(0) {}  // NOLINT
38
39     /**
40      * @brief The constructor creates a PortInfo object
41      * @param layerID Layer id
42      * @param portID Port id
43      */
44     PortInfo(idx_t layerID, idx_t portID): layer(layerID), port(portID) {}
45
46     /**
47      * @brief Get layer id
48      * @return Layer id
49      */
50     idx_t layerId() const {
51         return layer;
52     }
53
54     /**
55      * @brief Get port id
56      * @return Port id
57      */
58     idx_t portId() const {
59         return port;
60     }
61
62     /**
63      * @brief Compares the given PortInfo object with the current one
64      * @param portInfo PortInfo object to compare with
65      * @return true if the given PortInfo object is equal to the current one, false - otherwise
66      */
67     bool operator==(const PortInfo& portInfo) const {
68         return layer == portInfo.layerId() && port == portInfo.portId();
69     }
70
71     /**
72      * @brief Checks if the given PortInfo object is not equal to the current one
73      * @param portInfo PortInfo object to compare with
74      * @return true if the given PortInfo object is not equal to the current one, false - otherwise
75      */
76     bool operator!=(const PortInfo& portInfo) const {
77         return !(*this == portInfo);
78     }
79
80 private:
81     idx_t layer;
82     idx_t port;
83 };
84
85 /**
86  * @brief This class is the main object to describe the Inference Engine connection.
87  */
88 class Connection {
89 public:
90     /**
91      * @brief Constructor of a connection object.
92      * @param input pair of the index of input layer and the index of output port
93      * @param output pair of the index of output layer and the index of input port
94      */
95     Connection(const PortInfo& input, const PortInfo& output): input(input), output(output) {}
96
97     /**
98      * @brief Compares the given Connection with the current one
99      * @param connection Connection to compare with
100      * @return true if the given Connection is equal to the current one, false - otherwise
101      */
102     bool operator==(const Connection& connection) const {
103         return input == connection.from() && output == connection.to();
104     }
105
106     /**
107      * @brief Checks if the given Connection is not equal to the current one
108      * @param connection Connection to compare with
109      * @return true if the given Connection is not equal to the current one, false - otherwise
110      */
111     bool operator!=(const Connection& connection) const {
112         return !(*this == connection);
113     }
114
115     /**
116      * Returns a constant reference to a pair of input layer index and output port index.
117      * @return pair of the index of input layer and the index of output port
118      */
119     const PortInfo& from() const {
120         return input;
121     }
122
123     /**
124      * Returns a constant reference to a pair of output layer index and input port index.
125      * @return pair of the index of output layer and the index of input port
126      */
127     const PortInfo& to() const {
128         return output;
129     }
130
131 private:
132     PortInfo input;
133     PortInfo output;
134 };
135
136 /**
137  * @brief This class is the main object to describe the Inference Engine port.
138  */
139 class Port {
140 public:
141     /**
142      * @brief Default constructor of a port object.
143      */
144     Port() = default;
145     /**
146      * @brief Constructor of a port object with shapes.
147      * @param shapes port shapes
148      */
149     explicit Port(const SizeVector& shapes): pShapes(shapes) {}
150
151     /**
152      * @brief Copy constructor.
153      * @param port object to copy
154      */
155     Port(const Port& port) {
156         this->pShapes = port.pShapes;
157     }
158
159     /**
160      * @brief Returns a constant reference to a vector with shapes.
161      * Shapes should be initialized if shape is empty.
162      * @return constant reference to shapes
163      */
164     const SizeVector& shape() const noexcept {
165         return pShapes;
166     }
167
168     /**
169      * @brief Returns a reference to a vector with shapes.
170      * Shapes should be initialized if shape is empty.
171      * @return reference to shapes
172      */
173     SizeVector& shape() noexcept {
174         return pShapes;
175     }
176
177 private:
178     SizeVector pShapes;
179 };
180
181 /**
182  * @brief This class is the main interface to describe the Inference Engine layer parameters.
183  * All methods here are constant and do not throw exceptions.
184  */
185 class IParameters {
186 public:
187     /**
188      * @brief A shared pointer to the IParameters object.
189      */
190     using Ptr = std::shared_ptr<IParameters>;
191
192     /**
193      * @brief Virtual destructor for the parameters interface
194      */
195     virtual ~IParameters() = default;
196
197     /**
198      * @brief Returns a constant reference to a map with parameters.
199      * @return Map of parameters
200      */
201     virtual const std::map<std::string, Parameter>& getParameters() const noexcept = 0;
202
203     /**
204      * @brief Returns a constant reference to a constant pointers to constant data.
205      * @return Map of constant pointers to constant data
206      */
207     virtual const std::map<std::string, Blob::CPtr>& getConstantData() const noexcept = 0;
208 };
209
210 class INetwork;
211 template <class T>
212 class INetwotkIterator;
213
214 /**
215  * @brief This class is the main interface to describe the Inference Engine layer.
216  * All methods here are constant and do not throw exceptions.
217  */
218 class ILayer {
219 public:
220     /**
221      * @brief A shared pointer to the ILayer object
222      */
223     using Ptr = std::shared_ptr<ILayer>;
224     /**
225      * @brief A shared pointer to the const ILayer object
226      */
227     using CPtr = std::shared_ptr<const ILayer>;
228
229     /**
230      * @brief Virtual destructor for the layer interface
231      */
232     virtual ~ILayer() = default;
233
234     /**
235      * @brief Returns a id of the layer.
236      * @return Layer id
237      */
238     virtual idx_t getId() const noexcept = 0;
239
240     /**
241      * @brief Returns a layer name.
242      * @return Layer name
243      */
244     virtual const std::string& getName() const noexcept = 0;
245
246     /**
247      * @brief Returns a layer type.
248      * @return Layer type
249      */
250     virtual const std::string& getType() const noexcept = 0;
251
252     /**
253      * @brief Returns a constant smart pointer reference to a Network interface.
254      * @return Network interface smart pointer
255      */
256      virtual const std::shared_ptr<INetwork>& getGraph() const noexcept = 0;
257
258     /**
259      * @brief Returns a constant smart pointer reference to a Parameters interface.
260      * @return Parameters interface smart pointer
261      */
262     virtual const IParameters::Ptr& getParameters() const noexcept = 0;
263
264     /**
265      * @brief Returns a constant reference to a vector with input ports.
266      * @return Vector of input ports
267      */
268     virtual const std::vector<Port>& getInputPorts() const noexcept = 0;
269
270     /**
271      * @brief Returns a constant reference to a vector with output ports.
272      * @return Vector of output ports
273      */
274     virtual const std::vector<Port>& getOutputPorts() const noexcept = 0;
275 };
276
277 namespace details {
278
279 template<class NT, class LT>
280 class INetworkIterator;
281
282 }  // namespace details
283
284 /**
285  * @brief This class is the main interface to describe the Inference Engine network.
286  *
287  * All methods here are constant and do not throw exceptions.
288  */
289 class INetwork {
290 public:
291     /**
292      * @brief A shared pointer to the INetwork object.
293      */
294     using Ptr = std::shared_ptr<INetwork>;
295     /**
296      * @brief A constant iterator for INetwork objects definition
297      */
298     using const_iterator = details::INetworkIterator<const INetwork, const ILayer>;
299
300     /**
301      * @brief Virtual destructor for the network interface
302      */
303     virtual ~INetwork() = default;
304
305     /**
306      * @brief Begin network iterator
307      * @return const INetwork iterator
308      */
309     virtual const_iterator begin() const noexcept = 0;
310
311     /**
312      * @brief End network iterator
313      * @return const INetwork iterator
314      */
315     virtual const_iterator end() const noexcept = 0;
316
317     /**
318      * @brief Returns a number of layers in the network.
319      * @return Layers count
320      */
321     virtual size_t size() const noexcept = 0;
322
323     /**
324      * @brief Returns a constant smart pointer to a Layer interface.
325      * If the layer is missing, returns nullptr.
326      * @param id Id of the Layer
327      * @return Layer interface smart pointer
328      */
329     virtual const ILayer::Ptr getLayer(idx_t id) const noexcept = 0;
330
331     /**
332      * @brief Returns a constant vector of input layers.
333      * @return Vector of input layers
334      */
335     virtual const std::vector<ILayer::Ptr> getInputs() const noexcept = 0;
336
337     /**
338      * @brief Returns a constant vector of output layers.
339      * @return Vector of output layers
340      */
341     virtual const std::vector<ILayer::Ptr> getOutputs() const noexcept = 0;
342
343     /**
344      * @brief Returns a constant vector of connections for specific layer.
345      * If the layer is missing, returns empty vector.
346      * @param layerId layer index
347      * @return Vector of connections
348      */
349     virtual const std::vector<Connection> getLayerConnections(idx_t layerId) const noexcept = 0;
350
351     /**
352      * @brief Returns a network name.
353      * @return Network name
354      */
355     virtual const std::string& getName() const noexcept = 0;
356
357     /**
358      * @brief Returns a network context
359      * @return const reference to Context
360      */
361     virtual const Context& getContext() const noexcept = 0;
362 };
363
364 }  // namespace InferenceEngine
365
366 #include <details/ie_inetwork_iterator.hpp>