ArmNN
 21.02
INetwork.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
8 #include <armnn/Deprecated.hpp>
10 #include <armnn/ILayerVisitor.hpp>
11 #include <armnn/IStrategy.hpp>
12 #include <armnn/NetworkFwd.hpp>
13 #include <armnn/Optional.hpp>
14 #include <armnn/TensorFwd.hpp>
15 #include <armnn/Types.hpp>
16 
17 #include <memory>
18 #include <vector>
19 
20 namespace armnn
21 {
22 /// @brief An input connection slot for a layer.
23 /// The input slot can be connected to an output slot of the preceding layer in the graph.
24 /// Only one connection to the input slot is allowed.
26 {
27 public:
28  virtual const IOutputSlot* GetConnection() const = 0;
29  virtual IOutputSlot* GetConnection() = 0;
30 
31 protected:
32  /// Not user deletable.
34 };
35 
36 /// @brief An output connection slot for a layer.
37 /// The output slot may be connected to 1 or more input slots of subsequent layers in the graph.
39 {
40 public:
41  virtual unsigned int GetNumConnections() const = 0;
42  virtual const IInputSlot* GetConnection(unsigned int index) const = 0;
43  virtual IInputSlot* GetConnection(unsigned int index) = 0;
44 
45  virtual void SetTensorInfo(const TensorInfo& tensorInfo) = 0;
46  virtual const TensorInfo& GetTensorInfo() const = 0;
47  virtual bool IsTensorInfoSet() const = 0;
48 
49  virtual int Connect(IInputSlot& destination) = 0;
50  virtual void Disconnect(IInputSlot& slot) = 0;
51 
52  virtual unsigned int CalculateIndexOnOwner() const = 0;
53 
54  virtual LayerGuid GetOwningLayerGuid() const = 0;
55 
56 protected:
57  /// Not user deletable.
59 };
60 
61 /// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
63 {
64 public:
65  /// Returns the name of the layer
66  virtual const char* GetName() const = 0;
67 
68  /// Returns the number of connectable input slots
69  virtual unsigned int GetNumInputSlots() const = 0;
70 
71  /// Returns the number of connectable output slots
72  virtual unsigned int GetNumOutputSlots() const = 0;
73 
74  /// Get a const input slot handle by slot index
75  virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
76 
77  /// Get the input slot handle by slot index
78  virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
79 
80  /// Get the const output slot handle by slot index
81  virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
82 
83  /// Get the output slot handle by slot index
84  virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
85 
86  /// Infer the shape of the output(s) based on the provided input shape(s)
87  virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
88 
89  /// Returns the unique id of the layer
90  virtual LayerGuid GetGuid() const = 0;
91 
92  /// Apply a visitor to this layer
93  virtual void Accept(ILayerVisitor& visitor) const = 0;
94 
95  /// Apply a visitor to this layer
96  virtual void ExecuteStrategy(IStrategy& strategy) const = 0;
97 
98  /// Provide a hint for the optimizer as to which backend to prefer for this layer
99  virtual void BackendSelectionHint(Optional<BackendId> backend) = 0;
100 
101  /// Returns the armnn::LayerType of this layer
102  virtual LayerType GetType() const = 0;
103 
104 protected:
105  /// Objects are not deletable via the handle
107 };
108 
109 
111 {
113  : m_ReduceFp32ToFp16(false)
114  , m_Debug(false)
115  , m_ReduceFp32ToBf16(false)
116  , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
117  , m_ImportEnabled(false)
118  , m_ModelOptions()
119  {}
120 
121  OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, bool importEnabled,
122  ModelOptions modelOptions = {})
123  : m_ReduceFp32ToFp16(reduceFp32ToFp16)
124  , m_Debug(debug)
125  , m_ReduceFp32ToBf16(reduceFp32ToBf16)
126  , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
127  , m_ImportEnabled(importEnabled)
128  , m_ModelOptions(modelOptions)
129  {
130  if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
131  {
132  throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
133  }
134  }
135 
136  OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16 = false,
138  bool importEnabled = false, ModelOptions modelOptions = {})
139  : m_ReduceFp32ToFp16(reduceFp32ToFp16)
140  , m_Debug(debug)
141  , m_ReduceFp32ToBf16(reduceFp32ToBf16)
142  , m_shapeInferenceMethod(shapeInferenceMethod)
143  , m_ImportEnabled(importEnabled)
144  , m_ModelOptions(modelOptions)
145  {
146  if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
147  {
148  throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
149  }
150  }
151 
152  // Reduce Fp32 data to Fp16 for faster processing
154 
155  // Add debug data for easier troubleshooting
156  bool m_Debug;
157 
158  // Reduce Fp32 data to Bf16 for faster processing
160 
161  // Infer output size when not available
163 
164  // Enable Import
166 
167  // Enable Model Options
169 };
170 
171 class IWorkloadFactory;
172 class NetworkImpl;
173 using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
174 using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
175 
176 /// Main network class which provides the interface for building up a neural network.
177 /// This object is subsequently required by the IRuntime::Load() method.
178 class INetwork
179 {
180 public:
181  static INetwork* CreateRaw(NetworkOptions networkOptions = {});
182  static INetworkPtr Create(NetworkOptions networkOptions = {});
183  static void Destroy(INetwork* network);
184 
185  Status PrintGraph();
186 
187  /// Adds an input layer to the network.
188  /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
189  /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
190  /// @param name - Optional name for the layer.
191  /// @return - Interface for configuring the layer.
192  IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr);
193 
194  /// Adds an ArgMinMax layer to the network.
195  /// @param desc - Parameters for the L2 normalization operation.
196  /// @param name - Optional name for the layer.
197  /// @return - Interface for configuring the layer.
198  IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
199  const char* name = nullptr);
200 
201  /// Add a Comparison layer to the network.
202  /// @param name - Optional name for the layer.
203  /// @param desc - Descriptor for the comparison operation.
204  /// @return - Interface for configuring the layer.
205  IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
206  const char* name = nullptr);
207 
208  /// Adds a concatenation layer to the network.
209  /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
210  /// process. Number of Views must be equal to the number of inputs, and their order
211  /// must match - e.g. first view corresponds to the first input, second view to the
212  /// second input, etc....
213  /// @param name - Optional name for the layer.
214  /// @return - Interface for configuring the layer.
215  IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
216  const char* name = nullptr);
217 
218  /// Adds a 2D convolution layer to the network.
219  /// @param convolution2dDescriptor - Description of the 2D convolution layer.
220  /// @param weights - Tensor for the weights data.
221  /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
222  /// @param name - Optional name for the layer.
223  /// @return - Interface for configuring the layer.
224  IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
225  const ConstTensor& weights,
226  const Optional<ConstTensor>& biases,
227  const char* name = nullptr);
228 
229  ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
230  IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
231  const ConstTensor& weights,
232  const char* name = nullptr);
233 
234  ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
235  IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
236  const ConstTensor& weights,
237  const ConstTensor& biases,
238  const char* name = nullptr);
239 
240  /// Adds a depth to space layer to the network.
241  /// @param depthToSpaceDescriptor - Parameters for the depth to space operation.
242  /// @param name - Optional name for the layer.
243  /// @return - Interface for configuring the layer.
244  IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
245  const char* name = nullptr);
246 
247  /// Adds a 2D depthwise convolution layer to the network.
248  /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
249  /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
250  /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
251  /// @param name - Optional name for the layer.
252  /// @return - Interface for configuring the layer.
253  IConnectableLayer* AddDepthwiseConvolution2dLayer(
254  const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
255  const ConstTensor& weights,
256  const Optional<ConstTensor>& biases,
257  const char* name = nullptr);
258 
259  ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
260  IConnectableLayer* AddDepthwiseConvolution2dLayer(
261  const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
262  const ConstTensor& weights,
263  const char* name = nullptr);
264 
265  ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
266  IConnectableLayer* AddDepthwiseConvolution2dLayer(
267  const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
268  const ConstTensor& weights,
269  const ConstTensor& biases,
270  const char* name = nullptr);
271 
272  /// Adds a Dequantize layer to the network.
273  /// @return - Interface for configuring the layer.
274  IConnectableLayer* AddDequantizeLayer(const char* name = nullptr);
275 
276  /// Adds a Detection PostProcess layer to the network.
277  /// @param descriptor - Description of the Detection PostProcess layer.
278  /// @param anchors - Tensor for anchors.
279  /// @param name - Optional name for the layer.
280  /// @return - Interface for configuring the layer.
281  IConnectableLayer* AddDetectionPostProcessLayer(
282  const DetectionPostProcessDescriptor& descriptor,
283  const ConstTensor& anchors,
284  const char* name = nullptr);
285 
286  /// Add an ElementwiseUnary layer to the network.
287  /// @param name - Optional name for the layer.
288  /// @param desc - Descriptor for the elementwiseUnary operation.
289  /// @return - Interface for configuring the layer.
290  IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
291  const char* name = nullptr);
292 
293  /// Add an Fill layer to the network.
294  /// @param name - Optional name for the layer.
295  /// @param fillDescriptor - Descriptor for the fill operation.
296  /// @return - Interface for configuring the layer.
297  IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
298  const char* name = nullptr);
299 
300  /// Adds a fully connected layer to the network.
301  /// @param fullyConnectedDescriptor - Description of the fully connected layer.
302  /// @param weights - Tensor for the weights data.
303  /// @param biases - Optional tensor for the bias data.
304  /// @param name - Optional name for the layer.
305  /// @return - Interface for configuring the layer.
306  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
307  const ConstTensor& weights,
308  const Optional<ConstTensor>& biases,
309  const char* name = nullptr);
310 
311  ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
312  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
313  const ConstTensor& weights,
314  const char* name = nullptr);
315 
316  ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
317  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
318  const ConstTensor& weights,
319  const ConstTensor& biases,
320  const char* name = nullptr);
321 
322  /// Adds a permute layer to the network.
323  /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
324  /// @param name - Optional name for the layer.
325  /// @return - Interface for configuring the layer.
326  IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
327  const char* name = nullptr);
328 
329  /// Adds a batch to space ND layer to the network.
330  /// @param batchToSpaceNdDescriptor - Description of the layer.
331  /// @param name - Optional name for the layer.
332  /// @return - Interface for configuring the layer.
333  IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
334  const char* name = nullptr);
335 
336  /// Adds a pooling layer to the network.
337  /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
338  /// @param name - Optional name for the layer.
339  /// @return - Interface for configuring the layer.
340  IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
341  const char* name = nullptr);
342 
343  /// Adds an activation layer to the network.
344  /// @param activationDescriptor - ActivationDescriptor to configure the activation.
345  /// @param name - Optional name for the layer.
346  /// @return - Interface for configuring the layer.
347  IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
348  const char* name = nullptr);
349 
350  /// Adds a normalization layer to the network.
351  /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
352  /// @param name - Optional name for the layer.
353  /// @return - Interface for configuring the layer.
354  IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
355  const char* name = nullptr);
356 
357  /// Adds a slice layer to the network.
358  /// @param sliceDescriptor - SliceDescriptor to configure the slice operation.
359  /// @param name - Optional name for the layer.
360  /// @return - Interface for configuring the layer.
361  IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr);
362 
363  /// Adds a softmax layer to the network.
364  /// If the data type is QAsymm8, then the output quantization parameters
365  /// must have a scale of 1/256 and an offset of 0
366  /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
367  /// @param name - Optional name for the layer.
368  /// @return - Interface for configuring the layer.
369  IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
370  const char* name = nullptr);
371 
372  /// Adds a splitter layer to the network.
373  /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
374  /// Number of Views must be equal to the number of outputs,
375  /// and their order must match - e.g. first view corresponds to
376  /// the first output, second view to the second output, etc....
377  /// @param name - Optional name for the layer.
378  /// @return - Interface for configuring the layer.
379  IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
380  const char* name = nullptr);
381 
382  /// Adds a merge layer to the network.
383  /// @param name - Optional name for the layer.
384  /// @return - Interface for configuring the layer.
385  IConnectableLayer* AddMergeLayer(const char* name = nullptr);
386 
387  /// Adds a concat layer to the network.
388  /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation
389  /// process. Number of Views must be equal to the number of inputs, and their order
390  /// must match - e.g. first view corresponds to the first input, second view to the
391  /// second input, etc....
392  /// @param name - Optional name for the layer.
393  /// @return - Interface for configuring the layer.
394  ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead")
395  IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor,
396  const char* name = nullptr);
397 
398  /// Add absolute layer to the network.
399  /// @param name - Optional name for the layer.
400  /// @return - Interface for configuring the layer.
401  ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
402  IConnectableLayer* AddAbsLayer(const char* name = nullptr);
403 
404  /// Adds an addition layer to the network.
405  /// @param name - Optional name for the layer.
406  /// @return - Interface for configuring the layer.
407  IConnectableLayer* AddAdditionLayer(const char* name = nullptr);
408 
409  /// Adds a multiplication layer to the network.
410  /// @param name - Optional name for the layer.
411  /// @return - Interface for configuring the layer.
412  IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr);
413 
414  /// Adds a batch normalization layer to the network.
415  /// @param mean - Pre-calculated mean for each channel.
416  /// @param variance - Pre-calculated variance for each channel.
417  /// @param beta - Per-channel additive factor.
418  /// @param gamma - Per-channel multiplicative factor.
419  /// @return - Interface for configuring the layer.
420  /// @param name - Optional name for the layer.
421  IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
422  const ConstTensor& mean,
423  const ConstTensor& variance,
424  const ConstTensor& beta,
425  const ConstTensor& gamma,
426  const char* name = nullptr);
427 
428  /// Adds a rank layer to the network.
429  /// @param name - Optional name for the layer.
430  /// @return - Interface for configuring the layer.
431  IConnectableLayer* AddRankLayer(const char* name = nullptr);
432 
433  /// Adds a resize bilinear layer to the network.
434  /// @param resizeDesc - Parameters for the resize operation.
435  /// @param name - Optional name for the layer.
436  /// @return - Interface for configuring the layer.
437  ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead")
438  IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
439  const char* name = nullptr);
440 
441  /// Adds a resize layer to the network.
442  /// @param resizeDescriptor - Parameters for the resize operation.
443  /// @param name - Optional name for the layer.
444  /// @return - Interface for configuring the layer.
445  IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
446  const char* name = nullptr);
447 
448  /// Adds a reduce layer to the network.
449  /// @param ReduceDescriptor - Parameters for the reduce operation.
450  /// @param name - Optional name for the layer.
451  /// @return - Interface for configuring the layer.
452  IConnectableLayer* AddReduceLayer(const ReduceDescriptor& reduceDescriptor,
453  const char* name = nullptr);
454 
455  /// Adds an instance normalization layer to the network.
456  /// @param desc - Parameters for the instance normalization operation.
457  /// @param name - Optional name for the layer.
458  /// @return - Interface for configuring the layer.
459  IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
460  const char* name = nullptr);
461 
462  /// Adds an L2 normalization layer to the network.
463  /// Normalization is performed along dimension 1, but requires a 4d input.
464  /// @param desc - Parameters for the L2 normalization operation.
465  /// @param name - Optional name for the layer.
466  /// @return - Interface for configuring the layer.
467  IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
468  const char* name = nullptr);
469 
470  /// Adds a log softmax layer to the network.
471  /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax.
472  /// @param name - Optional name for the layer.
473  /// @return - Interface for configuring the layer.
474  IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
475  const char* name = nullptr);
476 
477  /// Adds a layer with no inputs and a single output, which always corresponds to
478  /// the passed in constant tensor.
479  /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
480  /// its own copy of the tensor data, meaning the memory referenced by @a input can
481  /// be freed or reused after this function is called.
482  /// @param name - Optional name for the layer.
483  /// @return - Interface for configuring the layer.
484  IConnectableLayer* AddConstantLayer(const ConstTensor& input,
485  const char* name = nullptr);
486 
487  /// Adds a reshape layer to the network.
488  /// @param reshapeDescriptor - Parameters for the reshape operation.
489  /// @param name - Optional name for the layer.
490  /// @return - Interface for configuring the layer.
491  IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
492  const char* name = nullptr);
493 
494  /// Adds a space to batch layer to the network.
495  /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
496  /// @param name - Optional name for the layer.
497  /// @return - Interface for configuring the layer.
498  IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
499  const char* name = nullptr);
500 
501  /// Adds a space to depth layer to the network.
502  /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
503  /// @param name - Optional name for the layer.
504  /// @return - Interface for configuring the layer.
505  IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
506  const char* name = nullptr);
507 
508  /// Adds a floor layer to the network.
509  /// @param name - Optional name for the layer.
510  /// @return - Interface for configuring the layer.
511  IConnectableLayer* AddFloorLayer(const char* name = nullptr);
512 
513  /// Adds an output layer to the network.
514  /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
515  /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
516  /// @param name - Optional name for the layer.
517  /// @return - Interface for configuring the layer.
518  IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr);
519 
520  /// Add a Lstm layer to the network
521  /// @param descriptor - Parameters for the Lstm operation
522  /// @param params - Weights and biases for the LSTM cell
523  /// @param name - Optional name for the layer
524  /// @return - Interface for configuring the layer.
525  IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
526  const LstmInputParams& params,
527  const char* name = nullptr);
528 
529  /// Adds a division layer to the network.
530  /// @param name - Optional name for the layer.
531  /// @return - Interface for configuring the layer.
532  IConnectableLayer* AddDivisionLayer(const char* name = nullptr);
533 
534  /// Adds a subtraction layer to the network.
535  /// @param name - Optional name for the layer.
536  /// @return - Interface for configuring the layer.
537  IConnectableLayer* AddSubtractionLayer(const char* name = nullptr);
538 
539  /// Add a Maximum layer to the network.
540  /// @param name - Optional name for the layer.
541  /// @return - Interface for configuring the layer.
542  IConnectableLayer* AddMaximumLayer(const char* name = nullptr);
543 
544  /// Add a Mean layer to the network.
545  /// @param meanDescriptor - Parameters for the mean operation.
546  /// @param name - Optional name for the layer.
547  /// @return - Interface for configuring the layer.
548  IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr);
549 
550  /// Adds a fully pad layer to the network.
551  /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
552  /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
553  /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
554  /// @param name - Optional name for the layer.
555  /// @return - Interface for configuring the layer.
556  IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
557  const char* name = nullptr);
558 
559  /// Add a quantize layer to the network
560  ///@param name - Optional name for the layer.
561  /// @return - Interface for configuring the layer.
562  IConnectableLayer* AddQuantizeLayer(const char* name = nullptr);
563 
564  /// Adds a strided slice layer to the network.
565  /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
566  /// @param name - Optional name for the layer.
567  /// @return - Interface for configuring the layer.
568  IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
569  const char* name = nullptr);
570 
571  /// Add a Minimum layer to the network.
572  /// @param name - Optional name for the layer.
573  /// @return - Interface for configuring the layer.
574  IConnectableLayer* AddMinimumLayer(const char* name = nullptr);
575 
576  /// Add a Greater layer to the network.
577  /// @param name - Optional name for the layer.
578  /// @return - Interface for configuring the layer.
579  ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
580  IConnectableLayer* AddGreaterLayer(const char* name = nullptr);
581 
582  /// Add a Equal layer to the network.
583  /// @param name - Optional name for the layer.
584  /// @return - Interface for configuring the layer.
585  ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
586  IConnectableLayer* AddEqualLayer(const char* name = nullptr);
587 
588  /// Add Reciprocal of square root layer to the network.
589  /// @param name - Optional name for the layer.
590  /// @return - Interface for configuring the layer.
591  ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
592  IConnectableLayer* AddRsqrtLayer(const char* name = nullptr);
593 
594  /// Add Gather layer to the network.
595  /// @param name - Optional name for the layer.
596  /// @return - Interface for configuring the layer.
597  ARMNN_DEPRECATED_MSG("Use AddGatherLayer with descriptor instead")
598  IConnectableLayer* AddGatherLayer(const char* name = nullptr);
599 
600  /// Add Gather layer to the network.
601  /// @param descriptor - Description of the gather layer.
602  /// @param name - Optional name for the layer.
603  /// @return - Interface for configuring the layer.
604  IConnectableLayer* AddGatherLayer(const GatherDescriptor& descriptor,
605  const char* name = nullptr);
606 
607  /// Adds a switch layer to the network.
608  /// @param name - Optional name for the layer.
609  /// @return - Interface for configuring the layer.
610  IConnectableLayer* AddSwitchLayer(const char* name = nullptr);
611 
612  /// Adds a PReLU layer to the network.
613  /// @param name - Optional name for the layer.
614  /// @return - Interface for configuring the layer.
615  IConnectableLayer* AddPreluLayer(const char* name = nullptr);
616 
617  /// Adds a 2D transpose convolution layer to the network.
618  /// @param descriptor - Description of the 2D transpose convolution layer.
619  /// @param weights - Tensor for the weights data.
620  /// @param biases - Optional tensor for the bias data.
621  /// @param name - Optional name for the layer.
622  /// @return - Interface for configuring the layer.
623  IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
624  const ConstTensor& weights,
625  const Optional<ConstTensor>& biases,
626  const char* name = nullptr);
627 
628  /// Adds a transpose layer to the network.
629  /// @param transposeDescriptor - TransposeDescriptor to configure the transpose.
630  /// @param name - Optional name for the layer.
631  /// @return - Interface for configuring the layer.
632  IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
633  const char* name = nullptr);
634 
635  /// Adds a stack layer to the network.
636  /// @param descriptor - Description of the stack layer.
637  /// @param name - Optional name for the layer.
638  /// @return - Interface for configuring the layer.
639  IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
640  const char* name = nullptr);
641 
642  /// Add a stand-in layer for a type unknown to the Arm NN framework.
643  /// Note: Due to the nature of this layer, no validation can be performed by the framework.
644  /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the
645  /// tensor sizes cannot be inferred.
646  /// @descriptor - Descriptor for the StandIn layer.
647  /// @return - Interface for configuring the layer.
648  IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
649  const char* name = nullptr);
650 
651  /// Add a QuantizedLstm layer to the network
652  /// @param params - The weights and biases for the Quantized LSTM cell
653  /// @param name - Optional name for the layer
654  /// @return - Interface for configuring the layer.
655  IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
656  const char* name = nullptr);
657 
658  /// Add a QLstm layer to the network
659  /// @param descriptor - Parameters for the QLstm operation
660  /// @param params - Weights and biases for the layer
661  /// @param name - Optional name for the layer
662  /// @return - Interface for configuring the layer.
663  IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
664  const LstmInputParams& params,
665  const char* name = nullptr);
666 
667  /// Adds a Logical Binary layer to the network.
668  /// @param descriptor - Description of the Logical Binary layer.
669  /// @param name - Optional name for the layer.
670  /// @return - Interface for configuring the layer.
671  IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& descriptor,
672  const char* name = nullptr);
673 
674  void Accept(ILayerVisitor& visitor) const;
675 
676  void ExecuteStrategy(IStrategy& strategy) const;
677 
678 protected:
679  ~INetwork();
680 
681  friend class NetworkQuantizer;
682  friend void VisitLayersTopologically(const INetwork* inputNetwork, IStrategy& strategy);
683  friend class TestConnectionPreservation;
684  friend TensorInfo GetInputTensorInfo(const INetwork* network);
685  friend IOptimizedNetworkPtr Optimize(const INetwork& network,
686  const std::vector<BackendId>& backendPreferences,
687  const IDeviceSpec& deviceSpec,
688  const OptimizerOptions& options,
689  Optional<std::vector<std::string>&> messages);
690 
691  INetwork(NetworkOptions networkOptions = {});
692 
693  std::unique_ptr<NetworkImpl> pNetworkImpl;
694 };
695 
696 struct BackendSettings;
697 struct OptimizationResult;
700 {
701 public:
702  static void Destroy(IOptimizedNetwork* network);
703 
704  Status PrintGraph();
705  Status SerializeToDot(std::ostream& stream) const;
706 
707  profiling::ProfilingGuid GetGuid() const;
708 
709  IOptimizedNetwork(std::unique_ptr<Graph> graph);
710  IOptimizedNetwork(std::unique_ptr<OptimizedNetworkImpl> impl);
712 
713 protected:
714  friend class LoadedNetwork;
715  friend Graph& GetGraphForTesting(IOptimizedNetwork* optNetPtr);
717  friend IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
718  const std::vector<BackendId>& backendPreferences,
719  const IDeviceSpec& deviceSpec,
720  const OptimizerOptions& options,
721  Optional<std::vector<std::string>&> messages);
722 
723  IOptimizedNetwork(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions);
724 
725  std::unique_ptr<OptimizedNetworkImpl> pOptimizedNetworkImpl;
726 };
727 
728 /// Create an optimized version of the network
729 /// @param network INetwork description of the network to be optimized.
730 /// @param backendPreferences The choice of the backend ordered by user preferences.
731 /// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
732 /// @param messages If there are failures or warnings a string describing same will be added to the vector
733 /// @param options OptimizerOptions object with optimizer configuration options
734 /// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
735 /// armnn::Exception if process fails.
736 
737 IOptimizedNetworkPtr Optimize(const INetwork& network,
738  const std::vector<BackendId>& backendPreferences,
739  const IDeviceSpec& deviceSpec,
740  const OptimizerOptions& options = OptimizerOptions(),
741  Optional<std::vector<std::string>&> messages = EmptyOptional());
742 } //namespace armnn
ModelOptions m_ModelOptions
Definition: INetwork.hpp:168
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:62
ShapeInferenceMethod m_shapeInferenceMethod
Definition: INetwork.hpp:162
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
~IConnectableLayer()
Objects are not deletable via the handle.
Definition: INetwork.hpp:106
A ReshapeDescriptor for the ReshapeLayer.
TensorInfo GetInputTensorInfo(const INetwork *network)
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:78
DataLayout::NCHW false
std::vector< BackendOptions > ModelOptions
A Convolution2dDescriptor for the Convolution2dLayer.
Main network class which provides the interface for building up a neural network. ...
Definition: INetwork.hpp:178
std::vector< BackendOptions > NetworkOptions
A LogicalBinaryDescriptor for the LogicalBinaryLayer.
Copyright (c) 2021 ARM Limited and Contributors.
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
Private implementation of INetwork.
Definition: Network.hpp:31
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:210
A ResizeDescriptor for the ResizeLayer.
A StackDescriptor for the StackLayer.
A PadDescriptor for the PadLayer.
std::unique_ptr< NetworkImpl > pNetworkImpl
Definition: INetwork.hpp:693
An LstmDescriptor for the LstmLayer.
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1502
An output connection slot for a layer.
Definition: INetwork.hpp:38
A L2NormalizationDescriptor for the L2NormalizationLayer.
An ArgMinMaxDescriptor for ArgMinMaxLayer.
Definition: Descriptors.hpp:56
An OriginsDescriptor for the ConcatLayer.
A ReduceDescriptor for the REDUCE operators.
A FullyConnectedDescriptor for the FullyConnectedLayer.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
Validate all output shapes.
A GatherDescriptor for the GatherLayer.
Status
enumeration
Definition: Types.hpp:26
std::unique_ptr< OptimizedNetworkImpl > pOptimizedNetworkImpl
Definition: INetwork.hpp:725
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:174
A StandInDescriptor for the StandIn layer.
A QLstmDescriptor for the QLstmLayer.
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:200
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:25
A SliceDescriptor for the SliceLayer.
Graph & GetGraphForTesting(IOptimizedNetwork *optNet)
Definition: TestUtils.cpp:25
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, bool importEnabled, ModelOptions modelOptions={})
Definition: INetwork.hpp:121
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
Definition: Descriptors.hpp:98
~IInputSlot()
Not user deletable.
Definition: INetwork.hpp:33
void VisitLayersTopologically(const INetwork *inputNetwork, IStrategy &visitor)
A MeanDescriptor for the MeanLayer.
virtual const IOutputSlot * GetConnection() const =0
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
ModelOptions & GetModelOptionsForTesting(IOptimizedNetwork *optNet)
Definition: TestUtils.cpp:30
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:12
#define ARMNN_DEPRECATED_MSG(message)
Definition: Deprecated.hpp:43
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173
~IOutputSlot()
Not user deletable.
Definition: INetwork.hpp:58
A Pooling2dDescriptor for the Pooling2dLayer.
A NormalizationDescriptor for the NormalizationLayer.
An InstanceNormalizationDescriptor for InstanceNormalizationLayer.
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
A SoftmaxDescriptor for the SoftmaxLayer.
ShapeInferenceMethod
The ShapeInferenceMethod modify how the output shapes are treated.
Definition: Types.hpp:177
An input connection slot for a layer.
Definition: INetwork.hpp:25
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16=false, ShapeInferenceMethod shapeInferenceMethod=armnn::ShapeInferenceMethod::ValidateOnly, bool importEnabled=false, ModelOptions modelOptions={})
Definition: INetwork.hpp:136
A FillDescriptor for the FillLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
A PermuteDescriptor for the PermuteLayer.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:419
std::vector< float > anchors({ 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 10.5f, 1.0f, 1.0f, 0.5f, 10.5f, 1.0f, 1.0f, 0.5f, 100.5f, 1.0f, 1.0f })