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