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