2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include <armnn/Deprecated.hpp>
8 #include <armnn/DescriptorsFwd.hpp>
9 #include <armnn/ILayerVisitor.hpp>
10 #include <armnn/NetworkFwd.hpp>
11 #include <armnn/Optional.hpp>
12 #include <armnn/TensorFwd.hpp>
13 #include <armnn/Types.hpp>
14 #include <armnn/Deprecated.hpp>
21 /// @brief An input connection slot for a layer.
22 /// The input slot can be connected to an output slot of the preceding layer in the graph.
23 /// Only one connection to the input slot is allowed.
27 virtual const IOutputSlot* GetConnection() const = 0;
28 virtual IOutputSlot* GetConnection() = 0;
31 /// Not user deletable.
35 /// @brief An output connection slot for a layer.
36 /// The output slot may be connected to 1 or more input slots of subsequent layers in the graph.
40 virtual unsigned int GetNumConnections() const = 0;
41 virtual const IInputSlot* GetConnection(unsigned int index) const = 0;
42 virtual IInputSlot* GetConnection(unsigned int index) = 0;
44 virtual void SetTensorInfo(const TensorInfo& tensorInfo) = 0;
45 virtual const TensorInfo& GetTensorInfo() const = 0;
46 virtual bool IsTensorInfoSet() const = 0;
48 virtual int Connect(IInputSlot& destination) = 0;
49 virtual void Disconnect(IInputSlot& slot) = 0;
51 virtual unsigned int CalculateIndexOnOwner() const = 0;
53 virtual LayerGuid GetOwningLayerGuid() const = 0;
56 /// Not user deletable.
60 /// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
61 class IConnectableLayer
64 virtual const char* GetName() const = 0;
66 virtual unsigned int GetNumInputSlots() const = 0;
67 virtual unsigned int GetNumOutputSlots() const = 0;
69 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
70 virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
72 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
73 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
75 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
77 virtual LayerGuid GetGuid() const = 0;
79 virtual void Accept(ILayerVisitor& visitor) const = 0;
81 /// Objects are not deletable via the handle
82 ~IConnectableLayer() {}
85 using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
87 /// Main network class which provides the interface for building up a neural network.
88 /// This object is subsequently required by the IRuntime::Load() method.
92 static INetwork* CreateRaw();
93 static INetworkPtr Create();
94 static void Destroy(INetwork* network);
96 virtual Status PrintGraph() = 0;
98 /// Adds an input layer to the network.
99 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
100 /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
101 /// @param name - Optional name for the layer.
102 /// @return - Interface for configuring the layer.
103 virtual IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr) = 0;
105 /// Adds an ArgMinMax layer to the network.
106 /// @param desc - Parameters for the L2 normalization operation.
107 /// @param name - Optional name for the layer.
108 /// @return - Interface for configuring the layer.
109 virtual IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
110 const char* name = nullptr) = 0;
112 /// Adds a concatenation layer to the network.
113 /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
114 /// process. Number of Views must be equal to the number of inputs, and their order
115 /// must match - e.g. first view corresponds to the first input, second view to the
116 /// second input, etc....
117 /// @param name - Optional name for the layer.
118 /// @return - Interface for configuring the layer.
119 virtual IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
120 const char* name = nullptr) = 0;
122 /// Adds a 2D convolution layer to the network.
123 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
124 /// @param weights - Tensor for the weights data.
125 /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
126 /// @param name - Optional name for the layer.
127 /// @return - Interface for configuring the layer.
128 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
129 const ConstTensor& weights,
130 const Optional<ConstTensor>& biases,
131 const char* name = nullptr) = 0;
133 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
134 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
135 const ConstTensor& weights,
136 const char* name = nullptr) = 0;
138 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
139 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
140 const ConstTensor& weights,
141 const ConstTensor& biases,
142 const char* name = nullptr) = 0;
144 /// Adds a 2D depthwise convolution layer to the network.
145 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
146 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
147 /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
148 /// @param name - Optional name for the layer.
149 /// @return - Interface for configuring the layer.
150 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
151 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
152 const ConstTensor& weights,
153 const Optional<ConstTensor>& biases,
154 const char* name = nullptr) = 0;
156 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
157 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
158 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
159 const ConstTensor& weights,
160 const char* name = nullptr) = 0;
162 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
163 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
164 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
165 const ConstTensor& weights,
166 const ConstTensor& biases,
167 const char* name = nullptr) = 0;
169 /// Adds a Dequantize layer to the network.
170 /// @return - Interface for configuring the layer.
171 virtual IConnectableLayer* AddDequantizeLayer(const char* name = nullptr) = 0;
173 /// Adds a Detection PostProcess layer to the network.
174 /// @param descriptor - Description of the Detection PostProcess layer.
175 /// @param anchors - Tensor for anchors.
176 /// @param name - Optional name for the layer.
177 /// @return - Interface for configuring the layer.
178 virtual IConnectableLayer* AddDetectionPostProcessLayer(
179 const DetectionPostProcessDescriptor& descriptor,
180 const ConstTensor& anchors,
181 const char* name = nullptr) = 0;
183 /// Adds a fully connected layer to the network.
184 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
185 /// @param weights - Tensor for the weights data.
186 /// @param biases - Optional tensor for the bias data.
187 /// @param name - Optional name for the layer.
188 /// @return - Interface for configuring the layer.
189 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
190 const ConstTensor& weights,
191 const Optional<ConstTensor>& biases,
192 const char* name = nullptr) = 0;
194 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
195 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
196 const ConstTensor& weights,
197 const char* name = nullptr) = 0;
199 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
200 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
201 const ConstTensor& weights,
202 const ConstTensor& biases,
203 const char* name = nullptr) = 0;
205 /// Adds a permute layer to the network.
206 /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
207 /// @param name - Optional name for the layer.
208 /// @return - Interface for configuring the layer.
209 virtual IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
210 const char* name = nullptr) = 0;
212 /// Adds a batch to space ND layer to the network.
213 /// @param batchToSpaceNdDescriptor - Description of the layer.
214 /// @param name - Optional name for the layer.
215 /// @return - Interface for configuring the layer.
216 virtual IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
217 const char* name = nullptr) = 0;
219 /// Adds a pooling layer to the network.
220 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
221 /// @param name - Optional name for the layer.
222 /// @return - Interface for configuring the layer.
223 virtual IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
224 const char* name = nullptr) = 0;
226 /// Adds an activation layer to the network.
227 /// @param activationDescriptor - ActivationDescriptor to configure the activation.
228 /// @param name - Optional name for the layer.
229 /// @return - Interface for configuring the layer.
230 virtual IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
231 const char* name = nullptr) = 0;
233 /// Adds a normalization layer to the network.
234 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
235 /// @param name - Optional name for the layer.
236 /// @return - Interface for configuring the layer.
237 virtual IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
238 const char* name = nullptr) = 0;
240 /// Adds a softmax layer to the network.
241 /// If the data type is QAsymm8, then the output quantization parameters
242 /// must have a scale of 1/256 and an offset of 0
243 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
244 /// @param name - Optional name for the layer.
245 /// @return - Interface for configuring the layer.
246 virtual IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
247 const char* name = nullptr) = 0;
249 /// Adds a splitter layer to the network.
250 /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
251 /// Number of Views must be equal to the number of outputs,
252 /// and their order must match - e.g. first view corresponds to
253 /// the first output, second view to the second output, etc....
254 /// @param name - Optional name for the layer.
255 /// @return - Interface for configuring the layer.
256 virtual IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor
257 , const char* name = nullptr) = 0;
259 /// Adds a merge layer to the network.
260 /// @param name - Optional name for the layer.
261 /// @return - Interface for configuring the layer.
262 virtual IConnectableLayer* AddMergeLayer(const char* name = nullptr) = 0;
264 /// Adds a concat layer to the network.
265 /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation
266 /// process. Number of Views must be equal to the number of inputs, and their order
267 /// must match - e.g. first view corresponds to the first input, second view to the
268 /// second input, etc....
269 /// @param name - Optional name for the layer.
270 /// @return - Interface for configuring the layer.
271 ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead")
272 virtual IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor,
273 const char* name = nullptr) = 0;
275 /// Add absolute layer to the network.
276 /// @param name - Optional name for the layer.
277 /// @ return - Interface for configuring the layer.
278 virtual IConnectableLayer* AddAbsLayer(const char* name = nullptr) = 0;
280 /// Adds an addition layer to the network.
281 /// @param name - Optional name for the layer.
282 /// @return - Interface for configuring the layer.
283 virtual IConnectableLayer* AddAdditionLayer(const char* name = nullptr) = 0;
285 /// Adds a multiplication layer to the network.
286 /// @param name - Optional name for the layer.
287 /// @return - Interface for configuring the layer.
288 virtual IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr) = 0;
290 /// Adds a batch normalization layer to the network.
291 /// @param mean - Pre-calculated mean for each channel.
292 /// @param variance - Pre-calculated variance for each channel.
293 /// @param beta - Per-channel additive factor.
294 /// @param gamma - Per-channel multiplicative factor.
295 /// @return - Interface for configuring the layer.
296 /// @param name - Optional name for the layer.
297 virtual IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
298 const ConstTensor& mean,
299 const ConstTensor& variance,
300 const ConstTensor& beta,
301 const ConstTensor& gamma,
302 const char* name = nullptr) = 0;
304 /// Adds a resize bilinear layer to the network.
305 /// @param resizeDesc - Parameters for the resize operation.
306 /// @param name - Optional name for the layer.
307 /// @return - Interface for configuring the layer.
308 ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead")
309 virtual IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
310 const char* name = nullptr) = 0;
312 /// Adds a resize layer to the network.
313 /// @param resizeDescriptor - Parameters for the resize operation.
314 /// @param name - Optional name for the layer.
315 /// @return - Interface for configuring the layer.
316 virtual IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
317 const char* name = nullptr) = 0;
319 /// Adds an L2 normalization layer to the network.
320 /// Normalization is performed along dimension 1, but requires a 4d input.
321 /// @param desc - Parameters for the L2 normalization operation.
322 /// @param name - Optional name for the layer.
323 /// @return - Interface for configuring the layer.
324 virtual IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
325 const char* name = nullptr) = 0;
327 /// Adds a layer with no inputs and a single output, which always corresponds to
328 /// the passed in constant tensor.
329 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
330 /// its own copy of the tensor data, meaning the memory referenced by @a input can
331 /// be freed or reused after this function is called.
332 /// @param name - Optional name for the layer.
333 /// @return - Interface for configuring the layer.
334 virtual IConnectableLayer* AddConstantLayer(const ConstTensor& input,
335 const char* name = nullptr) = 0;
337 /// Adds a reshape layer to the network.
338 /// @param reshapeDescriptor - Parameters for the reshape operation.
339 /// @param name - Optional name for the layer.
340 /// @return - Interface for configuring the layer.
341 virtual IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
342 const char* name = nullptr) = 0;
344 /// Adds a space to batch layer to the network.
345 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
346 /// @param name - Optional name for the layer.
347 /// @return - Interface for configuring the layer.
348 virtual IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
349 const char* name = nullptr) = 0;
351 /// Adds a space to depth layer to the network.
352 /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
353 /// @param name - Optional name for the layer.
354 /// @return - Interface for configuring the layer.
355 virtual IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
356 const char* name = nullptr) = 0;
358 /// Adds a floor layer to the network.
359 /// @param name - Optional name for the layer.
360 /// @return - Interface for configuring the layer.
361 virtual IConnectableLayer* AddFloorLayer(const char* name = nullptr) = 0;
363 /// Adds an output layer to the network.
364 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
365 /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
366 /// @param name - Optional name for the layer.
367 /// @return - Interface for configuring the layer.
368 virtual IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr) = 0;
370 /// Add a Lstm layer to the network
371 /// @param descriptor - Parameters for the Lstm operation
372 /// @param params - Weights and biases for the LSTM cell
373 /// @param name - Optional name for the layer
374 /// @return - Interface for configuring the layer.
375 virtual IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
376 const LstmInputParams& params,
377 const char* name = nullptr) = 0;
379 /// Adds a division layer to the network.
380 /// @param name - Optional name for the layer.
381 /// @return - Interface for configuring the layer.
382 virtual IConnectableLayer* AddDivisionLayer(const char* name = nullptr) = 0;
384 /// Adds a subtraction layer to the network.
385 /// @param name - Optional name for the layer.
386 /// @return - Interface for configuring the layer.
387 virtual IConnectableLayer* AddSubtractionLayer(const char* name = nullptr) = 0;
389 /// Add a Maximum layer to the network.
390 /// @param name - Optional name for the layer.
391 /// @ return - Interface for configuring the layer.
392 virtual IConnectableLayer* AddMaximumLayer(const char* name = nullptr) = 0;
394 /// Add a Mean layer to the network.
395 /// @param meanDescriptor - Parameters for the mean operation.
396 /// @param name - Optional name for the layer.
397 /// @ return - Interface for configuring the layer.
398 virtual IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr) = 0;
400 /// Adds a fully pad layer to the network.
401 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
402 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
403 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
404 /// @param name - Optional name for the layer.
405 /// @return - Interface for configuring the layer.
406 virtual IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
407 const char* name = nullptr) = 0;
409 /// Add a quantize layer to the network
410 ///@param name - Optional name for the layer.
411 /// @return - Interface for configuring the layer.
412 virtual IConnectableLayer* AddQuantizeLayer(const char* name = nullptr) = 0;
414 /// Adds a strided slice layer to the network.
415 /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
416 /// @param name - Optional name for the layer.
417 /// @return - Interface for configuring the layer.
418 virtual IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
419 const char* name = nullptr) = 0;
421 /// Add a Minimum layer to the network.
422 /// @param name - Optional name for the layer.
423 /// @ return - Interface for configuring the layer.
424 virtual IConnectableLayer* AddMinimumLayer(const char* name = nullptr) = 0;
426 /// Add a Greater layer to the network.
427 /// @param name - Optional name for the layer.
428 /// @ return - Interface for configuring the layer.
429 virtual IConnectableLayer* AddGreaterLayer(const char* name = nullptr) = 0;
431 /// Add a Equal layer to the network.
432 /// @param name - Optional name for the layer.
433 /// @ return - Interface for configuring the layer.
434 virtual IConnectableLayer* AddEqualLayer(const char* name = nullptr) = 0;
436 /// Add Reciprocal of square root layer to the network.
437 /// @param name - Optional name for the layer.
438 /// @ return - Interface for configuring the layer.
439 virtual IConnectableLayer* AddRsqrtLayer(const char* name = nullptr) = 0;
441 /// Add Gather layer to the network.
442 /// @param name - Optional name for the layer.
443 /// @ return - Interface for configuring the layer.
444 virtual IConnectableLayer* AddGatherLayer(const char* name = nullptr) = 0;
446 /// Adds a switch layer to the network.
447 /// @param name - Optional name for the layer.
448 /// @return - Interface for configuring the layer.
449 virtual IConnectableLayer* AddSwitchLayer(const char* name = nullptr) = 0;
451 /// Adds a PReLU layer to the network.
452 /// @param name - Optional name for the layer.
453 /// @return - Interface for configuring the layer.
454 virtual IConnectableLayer* AddPreluLayer(const char* name = nullptr) = 0;
456 /// Adds a 2D transpose convolution layer to the network.
457 /// @param descriptor - Description of the 2D transpose convolution layer.
458 /// @param weights - Tensor for the weights data.
459 /// @param biases - Optional tensor for the bias data.
460 /// @param name - Optional name for the layer.
461 /// @return - Interface for configuring the layer.
462 virtual IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
463 const ConstTensor& weights,
464 const Optional<ConstTensor>& biases,
465 const char* name = nullptr) = 0;
467 /// Adds a stack layer to the network.
468 /// @param descriptor - Description of the stack layer.
469 /// @param name - Optional name for the layer.
470 /// @return - Interface for configuring the layer.
471 virtual IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
472 const char* name = nullptr) = 0;
474 /// Add a QuantizedLstm layer to the network
475 /// @param params - The weights and biases for the Quantized LSTM cell
476 /// @param name - Optional name for the layer
477 /// @return - Interface for configuring the layer.
478 virtual IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
479 const char* name = nullptr) = 0;
481 virtual void Accept(ILayerVisitor& visitor) const = 0;
487 using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
489 class IOptimizedNetwork
492 static void Destroy(IOptimizedNetwork* network);
494 virtual Status PrintGraph() = 0;
495 virtual Status SerializeToDot(std::ostream& stream) const = 0;
499 ~IOptimizedNetwork() {}
502 struct OptimizerOptions
505 : m_ReduceFp32ToFp16(false)
509 OptimizerOptions(bool reduceFp32ToFp16, bool debug)
510 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
514 // Reduce Fp32 data to Fp16 for faster processing
515 bool m_ReduceFp32ToFp16;
517 // Add debug data for easier troubleshooting
521 /// Create an optimized version of the network
522 /// @param network INetwork description of the network to be optimized.
523 /// @param backendPreferences The choice of the backend ordered by user preferences.
524 /// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
525 /// @param errMessages if there are failures or warnings a string describing same will be added to the vector
526 /// @param options OptimizerOptions object with optimizer configuration options
527 /// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
528 /// armnn::Exception if process fails.
530 IOptimizedNetworkPtr Optimize(const INetwork& network,
531 const std::vector<BackendId>& backendPreferences,
532 const IDeviceSpec& deviceSpec,
533 const OptimizerOptions& options = OptimizerOptions(),
534 Optional<std::vector<std::string>&> errMessages = EmptyOptional());