Publishing R5 content (#72)
[platform/upstream/dldt.git] / inference-engine / include / builders / ie_layer_builder.hpp
1 // Copyright (C) 2018 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <details/caseless.hpp>
8 #include <ie_parameter.hpp>
9 #include <ie_inetwork.hpp>
10 #include <ie_blob.h>
11 #include <string>
12 #include <vector>
13 #include <memory>
14 #include <map>
15
16 namespace InferenceEngine {
17 namespace Builder {
18
19 class Layer;
20
21 /**
22  * @brief This structure implements a holder for validators
23  */
24 struct ValidatorsHolder {
25     /**
26      * @brief Caseless map connects type with validator
27      */
28     details::caseless_map<std::string, std::function<void(const Layer&)>> validators;
29 };
30
31 /**
32  * @brief This class implements a builder for IE Layer
33  */
34 class INFERENCE_ENGINE_API_CLASS(Layer) {
35 public:
36     /**
37      * @brief The constructor creates a Layer builder with layer type and layer name
38      * @param type Layer type
39      * @param name Layer name
40      */
41     explicit Layer(const std::string& type, const std::string& name = "");
42     /**
43      * @brief The constructor creates a Layer builder from shared pointer to ILayer
44      * @param layer shared pointer to ILayer
45      */
46     explicit Layer(const ILayer::Ptr& layer);
47     /**
48      * @brief The constructor creates a Layer builder from shared pointer to constant ILayer
49      * @param layer shared pointer to constant ILayer
50      */
51     explicit Layer(const ILayer::CPtr& layer);
52     /**
53      * @brief The constructor creates a Layer builder with layer ID and layer builder
54      * @param id Layer ID
55      * @param layer layer builder
56      */
57     Layer(idx_t id, const Layer& layer);
58
59     /**
60      * @brief Returns layer builder ID
61      * @return ID
62      */
63     idx_t getId() const;
64
65     /**
66      * @brief Returns a reference to layer type
67      * @return Layer type
68      */
69     std::string& getType();
70     /**
71      * @brief Returns a reference to constant layer type
72      * @return constant layer type
73      */
74     const std::string& getType() const;
75     /**
76      * @brief Sets layer type
77      * @param type Layer type
78      * @return Reference to Layer builder
79      */
80     Layer& setType(const std::string& type);
81
82     /**
83      * @brief Returns a reference to layer name
84      * @return Layer name
85      */
86     std::string& getName();
87     /**
88      * @brief Returns a reference to constant layer name
89      * @return constant layer name
90      */
91     const std::string& getName() const;
92     /**
93      * @brief Sets layer name
94      * @param name Layer name
95      * @return Reference to Layer builder
96      */
97     Layer& setName(const std::string& name);
98
99     /**
100      * @brief Returns layer subgraph
101      * @return shared pointer to INetwork
102      */
103     INetwork::Ptr& getGraph();
104     /**
105      * @brief Returns constant layer subgraph
106      * @return constant shared pointer to INetwork
107      */
108     const INetwork::Ptr& getGraph() const;
109     /**
110      * @brief Sets layer subgraph
111      * @param graph constant shared pointer to INetwork
112      * @return Reference to Layer builder
113      */
114     Layer& setGraph(const INetwork::Ptr& graph);
115
116     /**
117      * @brief Returns map of parameters
118      * @return map of parameters
119      */
120     std::map<std::string, Parameter>& getParameters();
121     /**
122      * @brief Returns constant map of parameters
123      * @return constant map of parameters
124      */
125     const std::map<std::string, Parameter>& getParameters() const;
126     /**
127      * @brief Sets parameters for layer
128      * @param params constant map of parameters
129      * @return Reference to Layer builder
130      */
131     Layer& setParameters(const std::map<std::string, Parameter>& params);
132
133     /**
134      * @brief Returns map of internal blobs
135      * @return map of internal blobs
136      */
137     std::map<std::string, Blob::CPtr>& getConstantData();
138     /**
139      * @brief Returns constant map of internal blobs
140      * @return constant map of internal blobs
141      */
142     const std::map<std::string, Blob::CPtr>& getConstantData() const;
143     /**
144      * @brief Sets constant data for layer
145      * @param constData constant map of shared pointers to blobs
146      * @return Reference to Layer builder
147      */
148     Layer& setConstantData(const std::map<std::string, Blob::Ptr>& constData);
149     /**
150      * @brief Sets constant data for layer
151      * @param constData constant map of shared pointers to constant blobs
152      * @return Reference to Layer builder
153      */
154     Layer& setConstantData(const std::map<std::string, Blob::CPtr>& constData);
155     /**
156      * @brief Adds constant data for layer by name
157      * @param name Name of constant data
158      * @param data shared pointer to constant blob
159      * @return Reference to Layer builder
160      */
161     Layer& addConstantData(const std::string& name, const Blob::CPtr& data);
162
163     /**
164      * @brief Returns vector of input ports
165      * @return Vector of input ports
166      */
167     std::vector<Port>& getInputPorts();
168     /**
169      * @brief Returns constant vector of input ports
170      * @return constant vector of input ports
171      */
172     const std::vector<Port>& getInputPorts() const;
173     /**
174      * @brief Sets input ports
175      * @param ports vector of ports
176      * @return Reference to Layer builder
177      */
178     Layer& setInputPorts(const std::vector<Port> &ports);
179
180     /**
181      * @brief Returns vector of output ports
182      * @return Vector of output ports
183      */
184     std::vector<Port>& getOutputPorts();
185     /**
186      * @brief Returns constant vector of output ports
187      * @return constant vector of output ports
188      */
189     const std::vector<Port>& getOutputPorts() const;
190     /**
191      * @brief Sets output ports
192      * @param ports vector of ports
193      * @return Reference to Layer builder
194      */
195     Layer& setOutputPorts(const std::vector<Port> &ports);
196
197     /**
198      * @brief Validates the current builder and generates ILayer object
199      * @return constant shared pointer to ILayer
200      */
201     const ILayer::Ptr build() const;
202
203     /**
204      * @brief Validates layer builder
205      */
206     void validate() const;
207
208     /**
209      * @brief Registers a new validator for type
210      * @param type Layer type
211      * @param validator Layer validator
212      */
213     static void addValidator(const std::string& type, const std::function<void(const Layer&)>& validator);
214
215 private:
216     idx_t id;
217     std::string type;
218     std::string name;
219     INetwork::Ptr graph;
220     std::vector<Port> inPorts;
221     std::vector<Port> outPorts;
222     std::map<std::string, Parameter> params;
223     std::map<std::string, Blob::CPtr> constData;
224
225     static std::shared_ptr<ValidatorsHolder> getValidatorsHolder();
226 };
227
228 /**
229  * @brief This class registers layer validators
230  */
231 class ValidatorRegisterBase {
232 public:
233     /**
234      * @brief The constructor registers new layer validator
235      * @param type Layer type
236      * @param validator Layer validator
237      */
238     explicit ValidatorRegisterBase(const std::string& type, const std::function<void(const Layer&)>& validator) {
239         InferenceEngine::Builder::Layer::addValidator(type, validator);
240     }
241 };
242
243 #define REG_VALIDATOR_FOR(__type, __validator) \
244 static InferenceEngine::Builder::ValidatorRegisterBase _reg_##__type(#__type, __validator)
245
246 }  // namespace Builder
247 }  // namespace InferenceEngine