ArmNN
 21.05
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  /// Adds a cast layer to the network.
202  /// @param name - Optional name for the layer.
203  /// @return - Interface for configuring the layer.
204  IConnectableLayer* AddCastLayer(const char* name = nullptr);
205 
206  /// Add a Comparison layer to the network.
207  /// @param name - Optional name for the layer.
208  /// @param desc - Descriptor for the comparison operation.
209  /// @return - Interface for configuring the layer.
210  IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
211  const char* name = nullptr);
212 
213  /// Adds a concatenation layer to the network.
214  /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
215  /// process. Number of Views must be equal to the number of inputs, and their order
216  /// must match - e.g. first view corresponds to the first input, second view to the
217  /// second input, etc....
218  /// @param name - Optional name for the layer.
219  /// @return - Interface for configuring the layer.
220  IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
221  const char* name = nullptr);
222 
223  /// Adds a 2D convolution layer to the network.
224  /// @param convolution2dDescriptor - Description of the 2D convolution layer.
225  /// @param weights - Tensor for the weights data.
226  /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
227  /// @param name - Optional name for the layer.
228  /// @return - Interface for configuring the layer.
229  IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
230  const ConstTensor& weights,
231  const Optional<ConstTensor>& biases,
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 char* name = nullptr);
238 
239  ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
240  IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
241  const ConstTensor& weights,
242  const ConstTensor& biases,
243  const char* name = nullptr);
244 
245  /// Adds a depth to space layer to the network.
246  /// @param depthToSpaceDescriptor - Parameters for the depth to space operation.
247  /// @param name - Optional name for the layer.
248  /// @return - Interface for configuring the layer.
249  IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
250  const char* name = nullptr);
251 
252  /// Adds a 2D depthwise convolution layer to the network.
253  /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
254  /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
255  /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
256  /// @param name - Optional name for the layer.
257  /// @return - Interface for configuring the layer.
258  IConnectableLayer* AddDepthwiseConvolution2dLayer(
259  const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
260  const ConstTensor& weights,
261  const Optional<ConstTensor>& biases,
262  const char* name = nullptr);
263 
264  ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
265  IConnectableLayer* AddDepthwiseConvolution2dLayer(
266  const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
267  const ConstTensor& weights,
268  const char* name = nullptr);
269 
270  ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
271  IConnectableLayer* AddDepthwiseConvolution2dLayer(
272  const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
273  const ConstTensor& weights,
274  const ConstTensor& biases,
275  const char* name = nullptr);
276 
277  /// Adds a Dequantize layer to the network.
278  /// @return - Interface for configuring the layer.
279  IConnectableLayer* AddDequantizeLayer(const char* name = nullptr);
280 
281  /// Adds a Detection PostProcess layer to the network.
282  /// @param descriptor - Description of the Detection PostProcess layer.
283  /// @param anchors - Tensor for anchors.
284  /// @param name - Optional name for the layer.
285  /// @return - Interface for configuring the layer.
286  IConnectableLayer* AddDetectionPostProcessLayer(
287  const DetectionPostProcessDescriptor& descriptor,
288  const ConstTensor& anchors,
289  const char* name = nullptr);
290 
291  /// Add an ElementwiseUnary layer to the network.
292  /// @param name - Optional name for the layer.
293  /// @param desc - Descriptor for the elementwiseUnary operation.
294  /// @return - Interface for configuring the layer.
295  IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
296  const char* name = nullptr);
297 
298  /// Add an Fill layer to the network.
299  /// @param name - Optional name for the layer.
300  /// @param fillDescriptor - Descriptor for the fill operation.
301  /// @return - Interface for configuring the layer.
302  IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
303  const char* name = nullptr);
304 
305  /// Adds a fully connected layer to the network.
306  /// @param fullyConnectedDescriptor - Description of the fully connected layer.
307  /// @param weights -Optional Tensor for the weights data.
308  /// @param biases - Optional tensor for the bias data.
309  /// @param name - Optional name for the layer.
310  /// @return - Interface for configuring the layer.
311  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
312  const Optional<ConstTensor>& weights,
313  const Optional<ConstTensor>& biases,
314  const char* name = nullptr);
315 
316  /// Adds a fully connected layer to the network.
317  /// @param fullyConnectedDescriptor - Description of the fully connected layer.
318  /// @param weights - Tensor for the weights data.
319  /// @param biases - Optional tensor for the bias data.
320  /// @param name - Optional name for the layer.
321  /// @return - Interface for configuring the layer.
322  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
323  const ConstTensor& weights,
324  const Optional<ConstTensor>& biases,
325  const char* name = nullptr);
326 
327  ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
328  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
329  const ConstTensor& weights,
330  const char* name = nullptr);
331 
332  ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
333  IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
334  const ConstTensor& weights,
335  const ConstTensor& biases,
336  const char* name = nullptr);
337 
338  /// Adds a permute layer to the network.
339  /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
340  /// @param name - Optional name for the layer.
341  /// @return - Interface for configuring the layer.
342  IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
343  const char* name = nullptr);
344 
345  /// Adds a batch to space ND layer to the network.
346  /// @param batchToSpaceNdDescriptor - Description of the layer.
347  /// @param name - Optional name for the layer.
348  /// @return - Interface for configuring the layer.
349  IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
350  const char* name = nullptr);
351 
352  /// Adds a pooling layer to the network.
353  /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
354  /// @param name - Optional name for the layer.
355  /// @return - Interface for configuring the layer.
356  IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
357  const char* name = nullptr);
358 
359  /// Adds an activation layer to the network.
360  /// @param activationDescriptor - ActivationDescriptor to configure the activation.
361  /// @param name - Optional name for the layer.
362  /// @return - Interface for configuring the layer.
363  IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
364  const char* name = nullptr);
365 
366  /// Adds a normalization layer to the network.
367  /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
368  /// @param name - Optional name for the layer.
369  /// @return - Interface for configuring the layer.
370  IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
371  const char* name = nullptr);
372 
373  /// Adds a slice layer to the network.
374  /// @param sliceDescriptor - SliceDescriptor to configure the slice operation.
375  /// @param name - Optional name for the layer.
376  /// @return - Interface for configuring the layer.
377  IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr);
378 
379  /// Adds a softmax layer to the network.
380  /// If the data type is QAsymm8, then the output quantization parameters
381  /// must have a scale of 1/256 and an offset of 0
382  /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
383  /// @param name - Optional name for the layer.
384  /// @return - Interface for configuring the layer.
385  IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
386  const char* name = nullptr);
387 
388  /// Adds a splitter layer to the network.
389  /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
390  /// Number of Views must be equal to the number of outputs,
391  /// and their order must match - e.g. first view corresponds to
392  /// the first output, second view to the second output, etc....
393  /// @param name - Optional name for the layer.
394  /// @return - Interface for configuring the layer.
395  IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
396  const char* name = nullptr);
397 
398  /// Adds a merge layer to the network.
399  /// @param name - Optional name for the layer.
400  /// @return - Interface for configuring the layer.
401  IConnectableLayer* AddMergeLayer(const char* name = nullptr);
402 
403  /// Adds a concat layer to the network.
404  /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation
405  /// process. Number of Views must be equal to the number of inputs, and their order
406  /// must match - e.g. first view corresponds to the first input, second view to the
407  /// second input, etc....
408  /// @param name - Optional name for the layer.
409  /// @return - Interface for configuring the layer.
410  ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead")
411  IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor,
412  const char* name = nullptr);
413 
414  /// Add absolute layer to the network.
415  /// @param name - Optional name for the layer.
416  /// @return - Interface for configuring the layer.
417  ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
418  IConnectableLayer* AddAbsLayer(const char* name = nullptr);
419 
420  /// Adds an addition layer to the network.
421  /// @param name - Optional name for the layer.
422  /// @return - Interface for configuring the layer.
423  IConnectableLayer* AddAdditionLayer(const char* name = nullptr);
424 
425  /// Adds a multiplication layer to the network.
426  /// @param name - Optional name for the layer.
427  /// @return - Interface for configuring the layer.
428  IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr);
429 
430  /// Adds a batch normalization layer to the network.
431  /// @param mean - Pre-calculated mean for each channel.
432  /// @param variance - Pre-calculated variance for each channel.
433  /// @param beta - Per-channel additive factor.
434  /// @param gamma - Per-channel multiplicative factor.
435  /// @return - Interface for configuring the layer.
436  /// @param name - Optional name for the layer.
437  IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
438  const ConstTensor& mean,
439  const ConstTensor& variance,
440  const ConstTensor& beta,
441  const ConstTensor& gamma,
442  const char* name = nullptr);
443 
444  /// Adds a rank layer to the network.
445  /// @param name - Optional name for the layer.
446  /// @return - Interface for configuring the layer.
447  IConnectableLayer* AddRankLayer(const char* name = nullptr);
448 
449  /// Adds a resize bilinear layer to the network.
450  /// @param resizeDesc - Parameters for the resize operation.
451  /// @param name - Optional name for the layer.
452  /// @return - Interface for configuring the layer.
453  ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead")
454  IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
455  const char* name = nullptr);
456 
457  /// Adds a resize layer to the network.
458  /// @param resizeDescriptor - Parameters for the resize operation.
459  /// @param name - Optional name for the layer.
460  /// @return - Interface for configuring the layer.
461  IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
462  const char* name = nullptr);
463 
464  /// Adds a reduce layer to the network.
465  /// @param ReduceDescriptor - Parameters for the reduce operation.
466  /// @param name - Optional name for the layer.
467  /// @return - Interface for configuring the layer.
468  IConnectableLayer* AddReduceLayer(const ReduceDescriptor& reduceDescriptor,
469  const char* name = nullptr);
470 
471  /// Adds an instance normalization layer to the network.
472  /// @param desc - Parameters for the instance normalization operation.
473  /// @param name - Optional name for the layer.
474  /// @return - Interface for configuring the layer.
475  IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
476  const char* name = nullptr);
477 
478  /// Adds an L2 normalization layer to the network.
479  /// Normalization is performed along dimension 1, but requires a 4d input.
480  /// @param desc - Parameters for the L2 normalization operation.
481  /// @param name - Optional name for the layer.
482  /// @return - Interface for configuring the layer.
483  IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
484  const char* name = nullptr);
485 
486  /// Adds a log softmax layer to the network.
487  /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax.
488  /// @param name - Optional name for the layer.
489  /// @return - Interface for configuring the layer.
490  IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
491  const char* name = nullptr);
492 
493  /// Adds a layer with no inputs and a single output, which always corresponds to
494  /// the passed in constant tensor.
495  /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
496  /// its own copy of the tensor data, meaning the memory referenced by @a input can
497  /// be freed or reused after this function is called.
498  /// @param name - Optional name for the layer.
499  /// @return - Interface for configuring the layer.
500  IConnectableLayer* AddConstantLayer(const ConstTensor& input,
501  const char* name = nullptr);
502 
503  /// Adds a reshape layer to the network.
504  /// @param reshapeDescriptor - Parameters for the reshape operation.
505  /// @param name - Optional name for the layer.
506  /// @return - Interface for configuring the layer.
507  IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
508  const char* name = nullptr);
509 
510  /// Adds a space to batch layer to the network.
511  /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
512  /// @param name - Optional name for the layer.
513  /// @return - Interface for configuring the layer.
514  IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
515  const char* name = nullptr);
516 
517  /// Adds a space to depth layer to the network.
518  /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
519  /// @param name - Optional name for the layer.
520  /// @return - Interface for configuring the layer.
521  IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
522  const char* name = nullptr);
523 
524  /// Adds a floor layer to the network.
525  /// @param name - Optional name for the layer.
526  /// @return - Interface for configuring the layer.
527  IConnectableLayer* AddFloorLayer(const char* name = nullptr);
528 
529  /// Adds an output layer to the network.
530  /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
531  /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
532  /// @param name - Optional name for the layer.
533  /// @return - Interface for configuring the layer.
534  IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr);
535 
536  /// Add a Lstm layer to the network
537  /// @param descriptor - Parameters for the Lstm operation
538  /// @param params - Weights and biases for the LSTM cell
539  /// @param name - Optional name for the layer
540  /// @return - Interface for configuring the layer.
541  IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
542  const LstmInputParams& params,
543  const char* name = nullptr);
544 
545  /// Adds a division layer to the network.
546  /// @param name - Optional name for the layer.
547  /// @return - Interface for configuring the layer.
548  IConnectableLayer* AddDivisionLayer(const char* name = nullptr);
549 
550  /// Adds a subtraction layer to the network.
551  /// @param name - Optional name for the layer.
552  /// @return - Interface for configuring the layer.
553  IConnectableLayer* AddSubtractionLayer(const char* name = nullptr);
554 
555  /// Add a Maximum layer to the network.
556  /// @param name - Optional name for the layer.
557  /// @return - Interface for configuring the layer.
558  IConnectableLayer* AddMaximumLayer(const char* name = nullptr);
559 
560  /// Add a Mean layer to the network.
561  /// @param meanDescriptor - Parameters for the mean operation.
562  /// @param name - Optional name for the layer.
563  /// @return - Interface for configuring the layer.
564  IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr);
565 
566  /// Adds a fully pad layer to the network.
567  /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
568  /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
569  /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
570  /// @param name - Optional name for the layer.
571  /// @return - Interface for configuring the layer.
572  IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
573  const char* name = nullptr);
574 
575  /// Add a quantize layer to the network
576  ///@param name - Optional name for the layer.
577  /// @return - Interface for configuring the layer.
578  IConnectableLayer* AddQuantizeLayer(const char* name = nullptr);
579 
580  /// Adds a strided slice layer to the network.
581  /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
582  /// @param name - Optional name for the layer.
583  /// @return - Interface for configuring the layer.
584  IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
585  const char* name = nullptr);
586 
587  /// Add a Minimum layer to the network.
588  /// @param name - Optional name for the layer.
589  /// @return - Interface for configuring the layer.
590  IConnectableLayer* AddMinimumLayer(const char* name = nullptr);
591 
592  /// Add a Greater layer to the network.
593  /// @param name - Optional name for the layer.
594  /// @return - Interface for configuring the layer.
595  ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
596  IConnectableLayer* AddGreaterLayer(const char* name = nullptr);
597 
598  /// Add a Equal layer to the network.
599  /// @param name - Optional name for the layer.
600  /// @return - Interface for configuring the layer.
601  ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
602  IConnectableLayer* AddEqualLayer(const char* name = nullptr);
603 
604  /// Add Reciprocal of square root layer to the network.
605  /// @param name - Optional name for the layer.
606  /// @return - Interface for configuring the layer.
607  ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
608  IConnectableLayer* AddRsqrtLayer(const char* name = nullptr);
609 
610  /// Add Gather layer to the network.
611  /// @param name - Optional name for the layer.
612  /// @return - Interface for configuring the layer.
613  ARMNN_DEPRECATED_MSG("Use AddGatherLayer with descriptor instead")
614  IConnectableLayer* AddGatherLayer(const char* name = nullptr);
615 
616  /// Add Gather layer to the network.
617  /// @param descriptor - Description of the gather layer.
618  /// @param name - Optional name for the layer.
619  /// @return - Interface for configuring the layer.
620  IConnectableLayer* AddGatherLayer(const GatherDescriptor& descriptor,
621  const char* name = nullptr);
622 
623  /// Adds a switch layer to the network.
624  /// @param name - Optional name for the layer.
625  /// @return - Interface for configuring the layer.
626  IConnectableLayer* AddSwitchLayer(const char* name = nullptr);
627 
628  /// Adds a PReLU layer to the network.
629  /// @param name - Optional name for the layer.
630  /// @return - Interface for configuring the layer.
631  IConnectableLayer* AddPreluLayer(const char* name = nullptr);
632 
633  /// Adds a 2D transpose convolution layer to the network.
634  /// @param descriptor - Description of the 2D transpose convolution layer.
635  /// @param weights - Tensor for the weights data.
636  /// @param biases - Optional tensor for the bias data.
637  /// @param name - Optional name for the layer.
638  /// @return - Interface for configuring the layer.
639  IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
640  const ConstTensor& weights,
641  const Optional<ConstTensor>& biases,
642  const char* name = nullptr);
643 
644  /// Adds a transpose layer to the network.
645  /// @param transposeDescriptor - TransposeDescriptor to configure the transpose.
646  /// @param name - Optional name for the layer.
647  /// @return - Interface for configuring the layer.
648  IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
649  const char* name = nullptr);
650 
651  /// Adds a stack layer to the network.
652  /// @param descriptor - Description of the stack layer.
653  /// @param name - Optional name for the layer.
654  /// @return - Interface for configuring the layer.
655  IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
656  const char* name = nullptr);
657 
658  /// Add a stand-in layer for a type unknown to the Arm NN framework.
659  /// Note: Due to the nature of this layer, no validation can be performed by the framework.
660  /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the
661  /// tensor sizes cannot be inferred.
662  /// @descriptor - Descriptor for the StandIn layer.
663  /// @return - Interface for configuring the layer.
664  IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
665  const char* name = nullptr);
666 
667  /// Add a QuantizedLstm layer to the network
668  /// @param params - The weights and biases for the Quantized LSTM cell
669  /// @param name - Optional name for the layer
670  /// @return - Interface for configuring the layer.
671  IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
672  const char* name = nullptr);
673 
674  /// Add a QLstm layer to the network
675  /// @param descriptor - Parameters for the QLstm operation
676  /// @param params - Weights and biases for the layer
677  /// @param name - Optional name for the layer
678  /// @return - Interface for configuring the layer.
679  IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
680  const LstmInputParams& params,
681  const char* name = nullptr);
682 
683  /// Adds a Logical Binary layer to the network.
684  /// @param descriptor - Description of the Logical Binary layer.
685  /// @param name - Optional name for the layer.
686  /// @return - Interface for configuring the layer.
687  IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& descriptor,
688  const char* name = nullptr);
689 
690  void Accept(ILayerVisitor& visitor) const;
691 
692  void ExecuteStrategy(IStrategy& strategy) const;
693 
694 protected:
695  ~INetwork();
696 
697  friend void VisitLayersTopologically(const INetwork* inputNetwork, IStrategy& strategy);
698  friend class TestConnectionPreservation;
699  friend TensorInfo GetInputTensorInfo(const INetwork* network);
700  friend IOptimizedNetworkPtr Optimize(const INetwork& network,
701  const std::vector<BackendId>& backendPreferences,
702  const IDeviceSpec& deviceSpec,
703  const OptimizerOptions& options,
704  Optional<std::vector<std::string>&> messages);
705 
706  INetwork(NetworkOptions networkOptions = {});
707 
708  std::unique_ptr<NetworkImpl> pNetworkImpl;
709 };
710 
711 namespace experimental
712 {
713 class AsyncNetworkImpl;
714 class WorkingMemHandle;
715 }
716 
717 struct BackendSettings;
718 struct OptimizationResult;
721 {
722 public:
723  static void Destroy(IOptimizedNetwork* network);
724 
725  Status PrintGraph();
726  Status SerializeToDot(std::ostream& stream) const;
727 
728  profiling::ProfilingGuid GetGuid() const;
729 
730  IOptimizedNetwork(std::unique_ptr<Graph> graph);
731  IOptimizedNetwork(std::unique_ptr<OptimizedNetworkImpl> impl);
733 
734 protected:
735  friend class LoadedNetwork;
736 
737  friend class experimental::AsyncNetworkImpl;
739 
740  friend Graph& GetGraphForTesting(IOptimizedNetwork* optNetPtr);
742  friend IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
743  const std::vector<BackendId>& backendPreferences,
744  const IDeviceSpec& deviceSpec,
745  const OptimizerOptions& options,
746  Optional<std::vector<std::string>&> messages);
747 
748  IOptimizedNetwork(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions);
749 
750  std::unique_ptr<OptimizedNetworkImpl> pOptimizedNetworkImpl;
751 };
752 
753 /// Create an optimized version of the network
754 /// @param network INetwork description of the network to be optimized.
755 /// @param backendPreferences The choice of the backend ordered by user preferences.
756 /// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
757 /// @param messages If there are failures or warnings a string describing same will be added to the vector
758 /// @param options OptimizerOptions object with optimizer configuration options
759 /// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
760 /// armnn::Exception if process fails.
761 
762 IOptimizedNetworkPtr Optimize(const INetwork& network,
763  const std::vector<BackendId>& backendPreferences,
764  const IDeviceSpec& deviceSpec,
765  const OptimizerOptions& options = OptimizerOptions(),
766  Optional<std::vector<std::string>&> messages = EmptyOptional());
767 } //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.
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:243
A ResizeDescriptor for the ResizeLayer.
A StackDescriptor for the StackLayer.
A PadDescriptor for the PadLayer.
std::unique_ptr< NetworkImpl > pNetworkImpl
Definition: INetwork.hpp:708
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:1568
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:30
std::unique_ptr< OptimizedNetworkImpl > pOptimizedNetworkImpl
Definition: INetwork.hpp:750
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:233
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
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:188
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:455
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 })