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