ie_layer_builder.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @file
7  */
8 
9 #pragma once
10 
11 #include <ie_blob.h>
12 
13 #include <details/caseless.hpp>
14 #include <ie_network.hpp>
15 #include <ie_parameter.hpp>
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 namespace InferenceEngine {
22 namespace Builder {
23 
24 class Layer;
25 
26 /**
27  * @deprecated Use ngraph API instead.
28  * @brief This structure implements a holder for validators
29  */
31  /**
32  * @brief Caseless map connects type with validator
33  */
34  details::caseless_map<std::string, std::function<void(const std::shared_ptr<const Layer>&, bool)>> validators;
35 };
36 
37 IE_SUPPRESS_DEPRECATED_START
38 
39 /**
40  * @deprecated Use ngraph API instead.
41  * @brief This class implements a builder for IE Layer
42  */
43 class INFERENCE_ENGINE_NN_BUILDER_API_CLASS(Layer): public ILayer, public std::enable_shared_from_this<Layer> {
44 public:
45  /**
46  * @brief A shared pointer to the Layer builder
47  */
48  using Ptr = std::shared_ptr<Layer>;
49  /**
50  * @brief A shared pointer to the constant Layer builder
51  */
52  using CPtr = std::shared_ptr<const Layer>;
53 
54  /**
55  * @brief The constructor creates a Layer builder with layer type and layer name
56  * @param type Layer type
57  * @param name Layer name
58  */
59  explicit Layer(const std::string& type, const std::string& name = "");
60 
61  /**
62  * @brief The constructor creates a Layer builder from shared pointer to constant ILayer
63  * @param layer shared pointer to constant ILayer
64  */
65  explicit Layer(const ILayer::CPtr& layer);
66 
67  /**
68  * @brief The constructor creates a Layer builder with layer ID and layer builder
69  * @param id Layer ID
70  * @param layer layer builder
71  */
72  Layer(idx_t id, const Layer& layer);
73 
74  /**
75  * @brief Compares the given Layer builder with the current one
76  * @param rhs Layer builder to compare with
77  * @return true if the given Layer builder is equal to the current one, false - otherwise
78  */
79  bool operator==(const Layer& rhs) const {
80  return params == rhs.params;
81  }
82 
83  /**
84  * @brief Returns layer ID
85  * @return Layer ID
86  */
87  idx_t getId() const noexcept override;
88 
89  /**
90  * @brief Returns a constant reference to layer name
91  * @return Layer name
92  */
93  const std::string& getName() const noexcept override;
94 
95  /**
96  * @brief Sets layer name
97  * @param name Layer name
98  * @return Reference to Layer builder
99  */
100  Layer& setName(const std::string& name);
101 
102  /**
103  * @brief Returns a constant reference to layer type
104  * @return Layer type
105  */
106  const std::string& getType() const noexcept override;
107 
108  /**
109  * @brief Sets layer type
110  * @param type Layer type
111  * @return Reference to Layer builder
112  */
113  Layer& setType(const std::string& type);
114 
115  /**
116  * @brief Returns map of parameters
117  * @return map of parameters
118  */
119  const std::map<std::string, Parameter>& getParameters() const noexcept override;
120  /**
121  * @brief Returns map of parameters
122  * @return map of parameters
123  */
124  std::map<std::string, Parameter>& getParameters();
125 
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 vector of input ports
135  * @return Vector of input ports
136  */
137  const std::vector<Port>& getInputPorts() const noexcept override;
138 
139  /**
140  * @brief Returns vector of input ports
141  * @return Vector of input ports
142  */
143  std::vector<Port>& getInputPorts();
144 
145  /**
146  * @brief Sets input ports
147  * @param ports vector of ports
148  * @return Reference to Layer builder
149  */
150  Layer& setInputPorts(const std::vector<Port>& ports);
151 
152  /**
153  * @brief Returns vector of output ports
154  * @return Vector of output ports
155  */
156 
157  const std::vector<Port>& getOutputPorts() const noexcept override;
158  /**
159  * @brief Returns vector of output ports
160  * @return Vector of output ports
161  */
162  std::vector<Port>& getOutputPorts();
163 
164  /**
165  * @brief Sets output ports
166  * @param ports vector of ports
167  * @return Reference to Layer builder
168  */
169  Layer& setOutputPorts(const std::vector<Port>& ports);
170 
171  /**
172  * @brief Validates the current builder and generates ILayer object
173  * @return constant shared pointer to ILayer
174  */
175  const ILayer::CPtr build() const;
176 
177  /**
178  * @brief Validates layer builder
179  */
180  void validate(bool partial = false) const;
181 
182  /**
183  * @brief Registers a new validator for type
184  * @param type Layer type
185  * @param validator Layer validator
186  */
187  static void addValidator(const std::string& type, const std::function<void(const Layer::CPtr&, bool)>& validator);
188 
189 private:
190  idx_t id;
191  std::string type;
192  std::string name;
193  std::vector<Port> inPorts;
194  std::vector<Port> outPorts;
195  std::map<std::string, Parameter> params;
196  static std::shared_ptr<ValidatorsHolder> getValidatorsHolder();
197 };
198 
199 /**
200  * @deprecated Use ngraph API instead.
201  * @brief This class registers layer validators
202  */
204 public:
205  /**
206  * @brief The constructor registers new layer validator
207  * @param type Layer type
208  * @param validator Layer validator
209  */
210  explicit ValidatorRegisterBase(const std::string& type,
211  const std::function<void(const Layer::CPtr&, bool)>& validator) {
213  }
214 };
215 
216 IE_SUPPRESS_DEPRECATED_END
217 
218 #define REG_VALIDATOR_FOR(__type, __validator) \
219  static InferenceEngine::Builder::ValidatorRegisterBase _reg_##__type(#__type, __validator)
220 
221 } // namespace Builder
222 } // namespace InferenceEngine
This class implements a builder for IE Layer.
Definition: ie_layer_builder.hpp:43
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
std::shared_ptr< const Layer > CPtr
A shared pointer to the constant Layer builder.
Definition: ie_layer_builder.hpp:52
std::string name
Layer name.
Definition: ie_layers.h:42
std::shared_ptr< const ILayer > CPtr
A shared pointer to the const ILayer object.
Definition: ie_network.hpp:353
A header file for Blob and generic TBlob<>
ValidatorRegisterBase(const std::string &type, const std::function< void(const Layer::CPtr &, bool)> &validator)
The constructor registers new layer validator.
Definition: ie_layer_builder.hpp:210
details::caseless_map< std::string, std::function< void(const std::shared_ptr< const Layer > &, bool)> > validators
Caseless map connects type with validator.
Definition: ie_layer_builder.hpp:34
std::shared_ptr< Layer > Ptr
A shared pointer to the Layer builder.
Definition: ie_layer_builder.hpp:48
This structure implements a holder for validators.
Definition: ie_layer_builder.hpp:30
static void addValidator(const std::string &type, const std::function< void(const Layer::CPtr &, bool)> &validator)
Registers a new validator for type.
bool operator==(const Layer &rhs) const
Compares the given Layer builder with the current one.
Definition: ie_layer_builder.hpp:79
size_t idx_t
A type of network objects indexes.
Definition: ie_network.hpp:29
A header file for the Parameter class.
std::map< std::string, std::string > params
Map of pairs: (parameter name, parameter value)
Definition: ie_layers.h:367
This class is the main interface to describe the Inference Engine layer.
Definition: ie_network.hpp:346
A header file for the Inference Engine Network interface.
std::string type
Layer type.
Definition: ie_layers.h:47
This class registers layer validators.
Definition: ie_layer_builder.hpp:203