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