Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / include / ie_network.hpp
1 // Copyright (C) 2018-2019 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  * This class describes port data
138  */
139 class INFERENCE_ENGINE_API_CLASS(PortData) {
140 public:
141     /**
142      * @brief A shared pointer to the PortData object.
143      */
144     using Ptr = std::shared_ptr<PortData>;
145
146     /**
147      * @brief Default constructor
148      */
149     PortData();
150
151     /**
152      * Creates port data with precision and shape
153      * @param shape Dimensions
154      * @param precision Precision
155      */
156     PortData(const SizeVector& shape, const Precision& precision);
157
158     /**
159      * @brief virtual destructor
160      */
161     virtual ~PortData() = default;
162
163     /**
164      * @brief Returns data
165      * @return Blob with data
166      */
167     const Blob::Ptr& getData() const;
168
169     /**
170      * @brief Sets data
171      * @param data Blob with data
172      */
173     void setData(const Blob::Ptr& data);
174
175     /**
176      * @brief Returns data parameters
177      * @return Map of parameters
178      */
179     const std::map<std::string, Parameter>& getParameters() const noexcept;
180
181     /**
182      * @brief Sets new shapes for data
183      * @param shape New shapes
184      */
185     void setShape(const SizeVector& shape);
186
187 private:
188     Blob::Ptr data;
189     std::map<std::string, Parameter> parameters;
190
191     void createData(const TensorDesc& desc);
192 };
193
194 /**
195  * @brief This class is the main object to describe the Inference Engine port.
196  */
197 class INFERENCE_ENGINE_API_CLASS(Port) {
198 public:
199     /**
200      * @brief Default constructor of a port object.
201      */
202     Port();
203     /**
204      * @brief Constructor of a port object with shapes.
205      * @param shapes port shapes
206      * @param precision Port precision
207      */
208     explicit Port(const SizeVector& shapes,
209                   const Precision& precision = Precision::UNSPECIFIED);
210
211     /**
212      * @brief Copy constructor.
213      * @param port object to copy
214      */
215     Port(const Port& port);
216
217     /**
218      * @brief Virtual destructor
219      */
220     virtual ~Port() = default;
221
222     /**
223      * @brief Compares the given Port with the current one
224      * @param rhs Port to compare with
225      * @return true if the given Port is equal to the current one, false - otherwise
226      */
227     bool operator== (const Port& rhs) const;
228
229     /**
230      * @brief Compares the given Port with the current one
231      * @param rhs Port to compare with
232      * @return true if the given Port is NOT equal to the current one, false - otherwise
233      */
234     bool operator!= (const Port& rhs) const;
235
236     /**
237      * @brief Returns a constant reference to a vector with shapes.
238      * Shapes should be initialized if shape is empty.
239      * @return constant reference to shapes
240      */
241     const SizeVector& shape() const noexcept;
242
243     /**
244      * @brief Sets new shapes for current port
245      * @param shape New shapes
246      */
247     void setShape(const SizeVector& shape);
248
249     /**
250      * @brief Returns a constant reference to parameters
251      * @return Map with parameters
252      */
253     const std::map<std::string, Parameter>& getParameters() const noexcept;
254
255     /**
256      * @brief Sets new parameters for current port
257      * @param params New parameters
258      */
259     void setParameters(const std::map<std::string, Parameter>& params) noexcept;
260
261     /**
262      * @brief Sets the new parameter for current port
263      * @param name Name of parameter
264      * @param param New value
265      */
266     void setParameter(const std::string& name, const Parameter& param);
267
268     /**
269      * @brief Returns port data
270      * @return Port data
271      */
272     const PortData::Ptr& getData() const noexcept;
273
274     /**
275      * @brief Sets new port data for current port
276      * @param data Port data
277      */
278     void setData(const PortData::Ptr& data);
279
280 private:
281     std::map<std::string, Parameter> parameters;
282     PortData::Ptr data;
283 };
284
285 class INetwork;
286 template <class T>
287 class INetwotkIterator;
288
289 /**
290  * @brief This class is the main interface to describe the Inference Engine layer.
291  * All methods here are constant and do not throw exceptions.
292  */
293 class ILayer {
294 public:
295     /**
296      * @brief A shared pointer to the const ILayer object
297      */
298     using CPtr = std::shared_ptr<const ILayer>;
299
300     /**
301      * @brief Virtual destructor for the layer interface
302      */
303     virtual ~ILayer() = default;
304
305     /**
306      * @brief Returns a id of the layer.
307      * @return Layer id
308      */
309     virtual idx_t getId() const noexcept = 0;
310
311     /**
312      * @brief Returns a layer name.
313      * @return Layer name
314      */
315     virtual const std::string& getName() const noexcept = 0;
316
317     /**
318      * @brief Returns a layer type.
319      * @return Layer type
320      */
321     virtual const std::string& getType() const noexcept = 0;
322
323     /**
324      * @brief Returns a constant smart pointer reference to a Parameters interface.
325      * @return Parameters interface smart pointer
326      */
327     virtual const std::map<std::string, Parameter>& getParameters() const noexcept = 0;
328
329     /**
330      * @brief Returns a constant reference to a vector with input ports.
331      * @return Vector of input ports
332      */
333     virtual const std::vector<Port>& getInputPorts() const noexcept = 0;
334
335     /**
336      * @brief Returns a constant reference to a vector with output ports.
337      * @return Vector of output ports
338      */
339     virtual const std::vector<Port>& getOutputPorts() const noexcept = 0;
340 };
341
342 namespace details {
343
344 template<class NT, class LT>
345 class INetworkIterator;
346
347 }  // namespace details
348
349 /**
350  * @brief This class is the main interface to describe the Inference Engine network.
351  *
352  * All methods here are constant and do not throw exceptions.
353  */
354 class INetwork {
355 public:
356     /**
357      * @brief A shared pointer to the constant INetwork object.
358      */
359     using CPtr = std::shared_ptr<const INetwork>;
360     /**
361      * @brief A constant iterator for INetwork definition
362      */
363     using const_iterator = details::INetworkIterator<const INetwork, const ILayer>;
364
365     /**
366      * @brief Virtual destructor for the network interface
367      */
368     virtual ~INetwork() = default;
369
370     /**
371      * @brief Begin network iterator
372      * @return const INetwork iterator
373      */
374     virtual const_iterator begin() const noexcept = 0;
375
376     /**
377      * @brief End network iterator
378      * @return const INetwork iterator
379      */
380     virtual const_iterator end() const noexcept = 0;
381
382     /**
383      * @brief Returns a number of layers in the network.
384      * @return Layers count
385      */
386     virtual size_t size() const noexcept = 0;
387
388     /**
389      * @brief Returns a constant smart pointer to a Layer interface.
390      * If the layer is missing, returns nullptr.
391      * @param id Id of the Layer
392      * @return Layer interface smart pointer
393      */
394     virtual const ILayer::CPtr getLayer(idx_t id) const noexcept = 0;
395
396     /**
397      * @brief Returns a constant vector of input layers.
398      * @return Vector of input layers
399      */
400     virtual const std::vector<ILayer::CPtr> getInputs() const noexcept = 0;
401
402     /**
403      * @brief Returns a constant vector of output layers.
404      * @return Vector of output layers
405      */
406     virtual const std::vector<ILayer::CPtr> getOutputs() const noexcept = 0;
407
408     /**
409      * @brief Returns a constant vector of connections for specific layer.
410      * If the layer is missing, returns empty vector.
411      * @param layerId layer index
412      * @return Vector of connections
413      */
414     virtual const std::vector<Connection> getLayerConnections(idx_t layerId) const noexcept = 0;
415
416     /**
417      * @brief Returns a network name.
418      * @return Network name
419      */
420     virtual const std::string& getName() const noexcept = 0;
421
422     /**
423      * @brief Returns a network context
424      * @return const reference to Context
425      */
426     virtual const Context& getContext() const noexcept = 0;
427 };
428
429 }  // namespace InferenceEngine
430
431 #include <details/ie_inetwork_iterator.hpp>