Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / include / builders / ie_layer_builder.hpp
1 // Copyright (C) 2018-2019 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_network.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 std::shared_ptr<const Layer>&, bool)>> validators;
29 };
30
31 /**
32  * @brief This class implements a builder for IE Layer
33  */
34 class INFERENCE_ENGINE_API_CLASS(Layer): public ILayer,
35         public std::enable_shared_from_this<Layer> {
36 public:
37     /**
38      * @brief A shared pointer to the Layer builder
39      */
40     using Ptr = std::shared_ptr<Layer>;
41     /**
42      * @brief A shared pointer to the constant Layer builder
43      */
44     using CPtr = std::shared_ptr<const Layer>;
45
46     /**
47      * @brief The constructor creates a Layer builder with layer type and layer name
48      * @param type Layer type
49      * @param name Layer name
50      */
51     explicit Layer(const std::string& type, const std::string& name = "");
52     /**
53      * @brief The constructor creates a Layer builder from shared pointer to constant ILayer
54      * @param layer shared pointer to constant ILayer
55      */
56     explicit Layer(const ILayer::CPtr& layer);
57     /**
58      * @brief The constructor creates a Layer builder with layer ID and layer builder
59      * @param id Layer ID
60      * @param layer layer builder
61      */
62     Layer(idx_t id, const Layer& layer);
63
64     /**
65      * @brief Compares the given Layer builder with the current one
66      * @param rhs Layer builder to compare with
67      * @return true if the given Layer builder is equal to the current one, false - otherwise
68      */
69     bool operator==(const Layer& rhs) const {
70         return params == rhs.params;
71     }
72
73     /**
74      * @brief Returns layer ID
75      * @return Layer ID
76      */
77     idx_t getId() const noexcept override;
78
79     /**
80      * @brief Returns a constant reference to layer name
81      * @return Layer name
82      */
83     const std::string& getName() const noexcept override;
84     /**
85      * @brief Sets layer name
86      * @param name Layer name
87      * @return Reference to Layer builder
88      */
89     Layer& setName(const std::string& name);
90
91     /**
92      * @brief Returns a constant reference to layer type
93      * @return Layer type
94      */
95     const std::string& getType() const noexcept override;
96     /**
97      * @brief Sets layer type
98      * @param type Layer type
99      * @return Reference to Layer builder
100      */
101     Layer& setType(const std::string& type);
102
103     /**
104      * @brief Returns map of parameters
105      * @return map of parameters
106      */
107     const std::map<std::string, Parameter>& getParameters() const noexcept override;
108     /**
109      * @brief Returns map of parameters
110      * @return map of parameters
111      */
112     std::map<std::string, Parameter>& getParameters();
113     /**
114      * @brief Sets parameters for layer
115      * @param params constant map of parameters
116      * @return Reference to Layer builder
117      */
118     Layer& setParameters(const std::map<std::string, Parameter>& params);
119
120     /**
121      * @brief Returns vector of input ports
122      * @return Vector of input ports
123      */
124     const std::vector<Port>& getInputPorts() const noexcept override;
125     /**
126      * @brief Returns vector of input ports
127      * @return Vector of input ports
128      */
129     std::vector<Port>& getInputPorts();
130     /**
131      * @brief Sets input ports
132      * @param ports vector of ports
133      * @return Reference to Layer builder
134      */
135     Layer& setInputPorts(const std::vector<Port> &ports);
136
137     /**
138      * @brief Returns vector of output ports
139      * @return Vector of output ports
140      */
141     const std::vector<Port>& getOutputPorts() const noexcept override;
142     /**
143      * @brief Returns vector of output ports
144      * @return Vector of output ports
145      */
146     std::vector<Port>& getOutputPorts();
147     /**
148      * @brief Sets output ports
149      * @param ports vector of ports
150      * @return Reference to Layer builder
151      */
152     Layer& setOutputPorts(const std::vector<Port> &ports);
153
154     /**
155      * @brief Validates the current builder and generates ILayer object
156      * @return constant shared pointer to ILayer
157      */
158     const ILayer::CPtr build() const;
159
160     /**
161      * @brief Validates layer builder
162      */
163     void validate(bool partial = false) const;
164
165     /**
166      * @brief Registers a new validator for type
167      * @param type Layer type
168      * @param validator Layer validator
169      */
170     static void addValidator(const std::string& type, const std::function<void(const Layer::CPtr&, bool)>& validator);
171
172 private:
173     idx_t id;
174     std::string type;
175     std::string name;
176     std::vector<Port> inPorts;
177     std::vector<Port> outPorts;
178     std::map<std::string, Parameter> params;
179     static std::shared_ptr<ValidatorsHolder> getValidatorsHolder();
180 };
181
182 /**
183  * @brief This class registers layer validators
184  */
185 class ValidatorRegisterBase {
186 public:
187     /**
188      * @brief The constructor registers new layer validator
189      * @param type Layer type
190      * @param validator Layer validator
191      */
192     explicit ValidatorRegisterBase(const std::string& type, const std::function<void(const Layer::CPtr&, bool)>& validator) {
193         InferenceEngine::Builder::Layer::addValidator(type, validator);
194     }
195 };
196
197 #define REG_VALIDATOR_FOR(__type, __validator) \
198 static InferenceEngine::Builder::ValidatorRegisterBase _reg_##__type(#__type, __validator)
199
200 }  // namespace Builder
201 }  // namespace InferenceEngine