blob: 0dc1ba471f055cb46f61711e76a6d9142f02bbcd [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Teresa Charlin52664732020-06-29 16:27:03 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
Finn Williamsf24effa2020-07-03 10:12:03 +01007#include <armnn/BackendOptions.hpp>
Jim Flynn906f9462019-05-10 13:55:21 +01008#include <armnn/Deprecated.hpp>
David Beckf0b48452018-10-19 15:20:56 +01009#include <armnn/DescriptorsFwd.hpp>
jimfly01e9e7bfd2019-01-24 22:29:33 +000010#include <armnn/ILayerVisitor.hpp>
Finn Williamsb454c5c2021-02-09 15:56:23 +000011#include <armnn/IStrategy.hpp>
Matthew Bentham313e1c82019-03-25 17:37:47 +000012#include <armnn/NetworkFwd.hpp>
13#include <armnn/Optional.hpp>
14#include <armnn/TensorFwd.hpp>
David Beckf0b48452018-10-19 15:20:56 +010015#include <armnn/Types.hpp>
telsoa014fcda012018-03-09 14:13:49 +000016
17#include <memory>
telsoa01c577f2c2018-08-31 09:22:23 +010018#include <vector>
telsoa014fcda012018-03-09 14:13:49 +000019
20namespace armnn
21{
telsoa014fcda012018-03-09 14:13:49 +000022/// @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.
25class IInputSlot
26{
27public:
28 virtual const IOutputSlot* GetConnection() const = 0;
29 virtual IOutputSlot* GetConnection() = 0;
30
31protected:
telsoa01c577f2c2018-08-31 09:22:23 +010032 /// Not user deletable.
33 ~IInputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000034};
35
36/// @brief An output connection slot for a layer.
37/// The output slot may be connected to 1 or more input slots of subsequent layers in the graph.
38class IOutputSlot
39{
40public:
41 virtual unsigned int GetNumConnections() const = 0;
42 virtual const IInputSlot* GetConnection(unsigned int index) const = 0;
43 virtual IInputSlot* GetConnection(unsigned int index) = 0;
44
45 virtual void SetTensorInfo(const TensorInfo& tensorInfo) = 0;
46 virtual const TensorInfo& GetTensorInfo() const = 0;
47 virtual bool IsTensorInfoSet() const = 0;
48
49 virtual int Connect(IInputSlot& destination) = 0;
50 virtual void Disconnect(IInputSlot& slot) = 0;
51
Mike Kelly8c1701a2019-02-11 17:01:27 +000052 virtual unsigned int CalculateIndexOnOwner() const = 0;
53
54 virtual LayerGuid GetOwningLayerGuid() const = 0;
55
telsoa014fcda012018-03-09 14:13:49 +000056protected:
telsoa01c577f2c2018-08-31 09:22:23 +010057 /// Not user deletable.
58 ~IOutputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000059};
60
61/// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
62class IConnectableLayer
63{
64public:
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000065 /// Returns the name of the layer
telsoa014fcda012018-03-09 14:13:49 +000066 virtual const char* GetName() const = 0;
67
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000068 /// Returns the number of connectable input slots
telsoa014fcda012018-03-09 14:13:49 +000069 virtual unsigned int GetNumInputSlots() const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000070
71 /// Returns the number of connectable output slots
telsoa014fcda012018-03-09 14:13:49 +000072 virtual unsigned int GetNumOutputSlots() const = 0;
73
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000074 /// Get a const input slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000075 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000076
77 /// Get the input slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000078 virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
79
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000080 /// Get the const output slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000081 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000082
83 /// Get the output slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000084 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
85
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000086 /// Infer the shape of the output(s) based on the provided input shape(s)
telsoa01c577f2c2018-08-31 09:22:23 +010087 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
88
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000089 /// Returns the unique id of the layer
surmeh01bceff2f2018-03-29 16:29:27 +010090 virtual LayerGuid GetGuid() const = 0;
jimfly01e9e7bfd2019-01-24 22:29:33 +000091
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000092 /// Apply a visitor to this layer
jimfly01e9e7bfd2019-01-24 22:29:33 +000093 virtual void Accept(ILayerVisitor& visitor) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000094
Finn Williamsb454c5c2021-02-09 15:56:23 +000095 /// Apply a visitor to this layer
96 virtual void ExecuteStrategy(IStrategy& strategy) const = 0;
97
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000098 /// Provide a hint for the optimizer as to which backend to prefer for this layer
99 virtual void BackendSelectionHint(Optional<BackendId> backend) = 0;
Finn Williamsb454c5c2021-02-09 15:56:23 +0000100
101 /// Returns the armnn::LayerType of this layer
102 virtual LayerType GetType() const = 0;
103
telsoa014fcda012018-03-09 14:13:49 +0000104protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100105 /// Objects are not deletable via the handle
106 ~IConnectableLayer() {}
telsoa014fcda012018-03-09 14:13:49 +0000107};
108
telsoa014fcda012018-03-09 14:13:49 +0000109
telsoa01c577f2c2018-08-31 09:22:23 +0100110struct OptimizerOptions
111{
Matteo Martincigh49124022019-01-11 13:25:59 +0000112 OptimizerOptions()
113 : m_ReduceFp32ToFp16(false)
114 , m_Debug(false)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000115 , m_ReduceFp32ToBf16(false)
Teresa Charlincdc01492020-06-09 18:00:20 +0100116 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100117 , m_ImportEnabled(false)
Sadik Armagan045f6be2020-09-10 13:37:32 +0100118 , m_ModelOptions()
keidav01738c2e62018-12-11 16:14:20 +0000119 {}
telsoa01c577f2c2018-08-31 09:22:23 +0100120
Sadik Armagan045f6be2020-09-10 13:37:32 +0100121 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, bool importEnabled,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000122 ModelOptions modelOptions = {})
Narumol Prangnawaratea063df2020-08-21 10:03:49 +0100123 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
124 , m_Debug(debug)
125 , m_ReduceFp32ToBf16(reduceFp32ToBf16)
126 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
127 , m_ImportEnabled(importEnabled)
Sadik Armagan045f6be2020-09-10 13:37:32 +0100128 , m_ModelOptions(modelOptions)
Narumol Prangnawaratea063df2020-08-21 10:03:49 +0100129 {
130 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
131 {
132 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
133 }
134 }
135
Teresa Charlincdc01492020-06-09 18:00:20 +0100136 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16 = false,
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100137 ShapeInferenceMethod shapeInferenceMethod = armnn::ShapeInferenceMethod::ValidateOnly,
Sadik Armagan045f6be2020-09-10 13:37:32 +0100138 bool importEnabled = false, ModelOptions modelOptions = {})
telsoa01c577f2c2018-08-31 09:22:23 +0100139 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
keidav01738c2e62018-12-11 16:14:20 +0000140 , m_Debug(debug)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000141 , m_ReduceFp32ToBf16(reduceFp32ToBf16)
Teresa Charlincdc01492020-06-09 18:00:20 +0100142 , m_shapeInferenceMethod(shapeInferenceMethod)
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100143 , m_ImportEnabled(importEnabled)
Sadik Armagan045f6be2020-09-10 13:37:32 +0100144 , m_ModelOptions(modelOptions)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000145 {
146 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
147 {
148 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
149 }
150 }
telsoa01c577f2c2018-08-31 09:22:23 +0100151
152 // Reduce Fp32 data to Fp16 for faster processing
153 bool m_ReduceFp32ToFp16;
keidav01738c2e62018-12-11 16:14:20 +0000154
155 // Add debug data for easier troubleshooting
156 bool m_Debug;
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000157
158 // Reduce Fp32 data to Bf16 for faster processing
159 bool m_ReduceFp32ToBf16;
Teresa Charlincdc01492020-06-09 18:00:20 +0100160
161 // Infer output size when not available
162 ShapeInferenceMethod m_shapeInferenceMethod;
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100163
164 // Enable Import
165 bool m_ImportEnabled;
Sadik Armagan045f6be2020-09-10 13:37:32 +0100166
167 // Enable Model Options
168 ModelOptions m_ModelOptions;
telsoa01c577f2c2018-08-31 09:22:23 +0100169};
telsoa014fcda012018-03-09 14:13:49 +0000170
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000171class IWorkloadFactory;
172class NetworkImpl;
173using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
174using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
175
176/// Main network class which provides the interface for building up a neural network.
177/// This object is subsequently required by the IRuntime::Load() method.
178class INetwork
179{
180public:
181 static INetwork* CreateRaw(NetworkOptions networkOptions = {});
182 static INetworkPtr Create(NetworkOptions networkOptions = {});
183 static void Destroy(INetwork* network);
184
185 Status PrintGraph();
186
187 /// Adds an input layer to the network.
188 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
189 /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
190 /// @param name - Optional name for the layer.
191 /// @return - Interface for configuring the layer.
192 IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr);
193
194 /// Adds an ArgMinMax layer to the network.
195 /// @param desc - Parameters for the L2 normalization operation.
196 /// @param name - Optional name for the layer.
197 /// @return - Interface for configuring the layer.
198 IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
199 const char* name = nullptr);
200
mathad01b392e982021-04-07 12:07:30 +0100201 /// Adds a cast layer to the network.
202 /// @param name - Optional name for the layer.
203 /// @return - Interface for configuring the layer.
204 IConnectableLayer* AddCastLayer(const char* name = nullptr);
205
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000206 /// Add a Comparison layer to the network.
207 /// @param name - Optional name for the layer.
208 /// @param desc - Descriptor for the comparison operation.
209 /// @return - Interface for configuring the layer.
210 IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
211 const char* name = nullptr);
212
213 /// Adds a concatenation layer to the network.
214 /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
215 /// process. Number of Views must be equal to the number of inputs, and their order
216 /// must match - e.g. first view corresponds to the first input, second view to the
217 /// second input, etc....
218 /// @param name - Optional name for the layer.
219 /// @return - Interface for configuring the layer.
220 IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
221 const char* name = nullptr);
222
223 /// Adds a 2D convolution layer to the network.
224 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
225 /// @param weights - Tensor for the weights data.
226 /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
227 /// @param name - Optional name for the layer.
228 /// @return - Interface for configuring the layer.
229 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
230 const ConstTensor& weights,
231 const Optional<ConstTensor>& biases,
232 const char* name = nullptr);
233
234 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
235 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
236 const ConstTensor& weights,
237 const char* name = nullptr);
238
239 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
240 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
241 const ConstTensor& weights,
242 const ConstTensor& biases,
243 const char* name = nullptr);
244
245 /// Adds a depth to space layer to the network.
246 /// @param depthToSpaceDescriptor - Parameters for the depth to space operation.
247 /// @param name - Optional name for the layer.
248 /// @return - Interface for configuring the layer.
249 IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
250 const char* name = nullptr);
251
252 /// Adds a 2D depthwise convolution layer to the network.
253 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
254 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
255 /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
256 /// @param name - Optional name for the layer.
257 /// @return - Interface for configuring the layer.
258 IConnectableLayer* AddDepthwiseConvolution2dLayer(
259 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
260 const ConstTensor& weights,
261 const Optional<ConstTensor>& biases,
262 const char* name = nullptr);
263
264 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
265 IConnectableLayer* AddDepthwiseConvolution2dLayer(
266 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
267 const ConstTensor& weights,
268 const char* name = nullptr);
269
270 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
271 IConnectableLayer* AddDepthwiseConvolution2dLayer(
272 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
273 const ConstTensor& weights,
274 const ConstTensor& biases,
275 const char* name = nullptr);
276
277 /// Adds a Dequantize layer to the network.
278 /// @return - Interface for configuring the layer.
279 IConnectableLayer* AddDequantizeLayer(const char* name = nullptr);
280
281 /// Adds a Detection PostProcess layer to the network.
282 /// @param descriptor - Description of the Detection PostProcess layer.
283 /// @param anchors - Tensor for anchors.
284 /// @param name - Optional name for the layer.
285 /// @return - Interface for configuring the layer.
286 IConnectableLayer* AddDetectionPostProcessLayer(
287 const DetectionPostProcessDescriptor& descriptor,
288 const ConstTensor& anchors,
289 const char* name = nullptr);
290
291 /// Add an ElementwiseUnary layer to the network.
292 /// @param name - Optional name for the layer.
293 /// @param desc - Descriptor for the elementwiseUnary operation.
294 /// @return - Interface for configuring the layer.
295 IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
296 const char* name = nullptr);
297
298 /// Add an Fill layer to the network.
299 /// @param name - Optional name for the layer.
300 /// @param fillDescriptor - Descriptor for the fill operation.
301 /// @return - Interface for configuring the layer.
302 IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
303 const char* name = nullptr);
304
305 /// Adds a fully connected layer to the network.
306 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000307 /// @param weights -Optional Tensor for the weights data.
308 /// @param biases - Optional tensor for the bias data.
309 /// @param name - Optional name for the layer.
310 /// @return - Interface for configuring the layer.
311 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
312 const Optional<ConstTensor>& weights,
313 const Optional<ConstTensor>& biases,
314 const char* name = nullptr);
315
316 /// Adds a fully connected layer to the network.
317 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000318 /// @param weights - Tensor for the weights data.
319 /// @param biases - Optional tensor for the bias data.
320 /// @param name - Optional name for the layer.
321 /// @return - Interface for configuring the layer.
322 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
323 const ConstTensor& weights,
324 const Optional<ConstTensor>& biases,
325 const char* name = nullptr);
326
327 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
328 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
329 const ConstTensor& weights,
330 const char* name = nullptr);
331
332 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
333 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
334 const ConstTensor& weights,
335 const ConstTensor& biases,
336 const char* name = nullptr);
337
338 /// Adds a permute layer to the network.
339 /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
340 /// @param name - Optional name for the layer.
341 /// @return - Interface for configuring the layer.
342 IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
343 const char* name = nullptr);
344
345 /// Adds a batch to space ND layer to the network.
346 /// @param batchToSpaceNdDescriptor - Description of the layer.
347 /// @param name - Optional name for the layer.
348 /// @return - Interface for configuring the layer.
349 IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
350 const char* name = nullptr);
351
352 /// Adds a pooling layer to the network.
353 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
354 /// @param name - Optional name for the layer.
355 /// @return - Interface for configuring the layer.
356 IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
357 const char* name = nullptr);
358
359 /// Adds an activation layer to the network.
360 /// @param activationDescriptor - ActivationDescriptor to configure the activation.
361 /// @param name - Optional name for the layer.
362 /// @return - Interface for configuring the layer.
363 IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
364 const char* name = nullptr);
365
366 /// Adds a normalization layer to the network.
367 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
368 /// @param name - Optional name for the layer.
369 /// @return - Interface for configuring the layer.
370 IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
371 const char* name = nullptr);
372
373 /// Adds a slice layer to the network.
374 /// @param sliceDescriptor - SliceDescriptor to configure the slice operation.
375 /// @param name - Optional name for the layer.
376 /// @return - Interface for configuring the layer.
377 IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr);
378
379 /// Adds a softmax layer to the network.
380 /// If the data type is QAsymm8, then the output quantization parameters
381 /// must have a scale of 1/256 and an offset of 0
382 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
383 /// @param name - Optional name for the layer.
384 /// @return - Interface for configuring the layer.
385 IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
386 const char* name = nullptr);
387
388 /// Adds a splitter layer to the network.
389 /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
390 /// Number of Views must be equal to the number of outputs,
391 /// and their order must match - e.g. first view corresponds to
392 /// the first output, second view to the second output, etc....
393 /// @param name - Optional name for the layer.
394 /// @return - Interface for configuring the layer.
395 IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
396 const char* name = nullptr);
397
398 /// Adds a merge layer to the network.
399 /// @param name - Optional name for the layer.
400 /// @return - Interface for configuring the layer.
401 IConnectableLayer* AddMergeLayer(const char* name = nullptr);
402
403 /// Adds a concat layer to the network.
404 /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation
405 /// process. Number of Views must be equal to the number of inputs, and their order
406 /// must match - e.g. first view corresponds to the first input, second view to the
407 /// second input, etc....
408 /// @param name - Optional name for the layer.
409 /// @return - Interface for configuring the layer.
410 ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead")
411 IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor,
412 const char* name = nullptr);
413
414 /// Add absolute layer to the network.
415 /// @param name - Optional name for the layer.
416 /// @return - Interface for configuring the layer.
417 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
418 IConnectableLayer* AddAbsLayer(const char* name = nullptr);
419
420 /// Adds an addition layer to the network.
421 /// @param name - Optional name for the layer.
422 /// @return - Interface for configuring the layer.
423 IConnectableLayer* AddAdditionLayer(const char* name = nullptr);
424
425 /// Adds a multiplication layer to the network.
426 /// @param name - Optional name for the layer.
427 /// @return - Interface for configuring the layer.
428 IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr);
429
430 /// Adds a batch normalization layer to the network.
431 /// @param mean - Pre-calculated mean for each channel.
432 /// @param variance - Pre-calculated variance for each channel.
433 /// @param beta - Per-channel additive factor.
434 /// @param gamma - Per-channel multiplicative factor.
435 /// @return - Interface for configuring the layer.
436 /// @param name - Optional name for the layer.
437 IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
438 const ConstTensor& mean,
439 const ConstTensor& variance,
440 const ConstTensor& beta,
441 const ConstTensor& gamma,
442 const char* name = nullptr);
443
444 /// Adds a rank layer to the network.
445 /// @param name - Optional name for the layer.
446 /// @return - Interface for configuring the layer.
447 IConnectableLayer* AddRankLayer(const char* name = nullptr);
448
449 /// Adds a resize bilinear layer to the network.
450 /// @param resizeDesc - Parameters for the resize operation.
451 /// @param name - Optional name for the layer.
452 /// @return - Interface for configuring the layer.
453 ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead")
454 IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
455 const char* name = nullptr);
456
457 /// Adds a resize layer to the network.
458 /// @param resizeDescriptor - Parameters for the resize operation.
459 /// @param name - Optional name for the layer.
460 /// @return - Interface for configuring the layer.
461 IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
462 const char* name = nullptr);
463
464 /// Adds a reduce layer to the network.
465 /// @param ReduceDescriptor - Parameters for the reduce operation.
466 /// @param name - Optional name for the layer.
467 /// @return - Interface for configuring the layer.
468 IConnectableLayer* AddReduceLayer(const ReduceDescriptor& reduceDescriptor,
469 const char* name = nullptr);
470
471 /// Adds an instance normalization layer to the network.
472 /// @param desc - Parameters for the instance normalization operation.
473 /// @param name - Optional name for the layer.
474 /// @return - Interface for configuring the layer.
475 IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
476 const char* name = nullptr);
477
478 /// Adds an L2 normalization layer to the network.
479 /// Normalization is performed along dimension 1, but requires a 4d input.
480 /// @param desc - Parameters for the L2 normalization operation.
481 /// @param name - Optional name for the layer.
482 /// @return - Interface for configuring the layer.
483 IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
484 const char* name = nullptr);
485
486 /// Adds a log softmax layer to the network.
487 /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax.
488 /// @param name - Optional name for the layer.
489 /// @return - Interface for configuring the layer.
490 IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
491 const char* name = nullptr);
492
493 /// Adds a layer with no inputs and a single output, which always corresponds to
494 /// the passed in constant tensor.
495 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
496 /// its own copy of the tensor data, meaning the memory referenced by @a input can
497 /// be freed or reused after this function is called.
498 /// @param name - Optional name for the layer.
499 /// @return - Interface for configuring the layer.
500 IConnectableLayer* AddConstantLayer(const ConstTensor& input,
501 const char* name = nullptr);
502
503 /// Adds a reshape layer to the network.
504 /// @param reshapeDescriptor - Parameters for the reshape operation.
505 /// @param name - Optional name for the layer.
506 /// @return - Interface for configuring the layer.
507 IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
508 const char* name = nullptr);
509
510 /// Adds a space to batch layer to the network.
511 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
512 /// @param name - Optional name for the layer.
513 /// @return - Interface for configuring the layer.
514 IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
515 const char* name = nullptr);
516
517 /// Adds a space to depth layer to the network.
518 /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
519 /// @param name - Optional name for the layer.
520 /// @return - Interface for configuring the layer.
521 IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
522 const char* name = nullptr);
523
524 /// Adds a floor layer to the network.
525 /// @param name - Optional name for the layer.
526 /// @return - Interface for configuring the layer.
527 IConnectableLayer* AddFloorLayer(const char* name = nullptr);
528
529 /// Adds an output layer to the network.
530 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
531 /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
532 /// @param name - Optional name for the layer.
533 /// @return - Interface for configuring the layer.
534 IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr);
535
536 /// Add a Lstm layer to the network
537 /// @param descriptor - Parameters for the Lstm operation
538 /// @param params - Weights and biases for the LSTM cell
539 /// @param name - Optional name for the layer
540 /// @return - Interface for configuring the layer.
541 IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
542 const LstmInputParams& params,
543 const char* name = nullptr);
544
545 /// Adds a division layer to the network.
546 /// @param name - Optional name for the layer.
547 /// @return - Interface for configuring the layer.
548 IConnectableLayer* AddDivisionLayer(const char* name = nullptr);
549
550 /// Adds a subtraction layer to the network.
551 /// @param name - Optional name for the layer.
552 /// @return - Interface for configuring the layer.
553 IConnectableLayer* AddSubtractionLayer(const char* name = nullptr);
554
555 /// Add a Maximum layer to the network.
556 /// @param name - Optional name for the layer.
557 /// @return - Interface for configuring the layer.
558 IConnectableLayer* AddMaximumLayer(const char* name = nullptr);
559
560 /// Add a Mean layer to the network.
561 /// @param meanDescriptor - Parameters for the mean operation.
562 /// @param name - Optional name for the layer.
563 /// @return - Interface for configuring the layer.
564 IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr);
565
566 /// Adds a fully pad layer to the network.
567 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
568 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
569 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
570 /// @param name - Optional name for the layer.
571 /// @return - Interface for configuring the layer.
572 IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
573 const char* name = nullptr);
574
575 /// Add a quantize layer to the network
576 ///@param name - Optional name for the layer.
577 /// @return - Interface for configuring the layer.
578 IConnectableLayer* AddQuantizeLayer(const char* name = nullptr);
579
580 /// Adds a strided slice layer to the network.
581 /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
582 /// @param name - Optional name for the layer.
583 /// @return - Interface for configuring the layer.
584 IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
585 const char* name = nullptr);
586
587 /// Add a Minimum layer to the network.
588 /// @param name - Optional name for the layer.
589 /// @return - Interface for configuring the layer.
590 IConnectableLayer* AddMinimumLayer(const char* name = nullptr);
591
592 /// Add a Greater layer to the network.
593 /// @param name - Optional name for the layer.
594 /// @return - Interface for configuring the layer.
595 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
596 IConnectableLayer* AddGreaterLayer(const char* name = nullptr);
597
598 /// Add a Equal layer to the network.
599 /// @param name - Optional name for the layer.
600 /// @return - Interface for configuring the layer.
601 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
602 IConnectableLayer* AddEqualLayer(const char* name = nullptr);
603
604 /// Add Reciprocal of square root layer to the network.
605 /// @param name - Optional name for the layer.
606 /// @return - Interface for configuring the layer.
607 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
608 IConnectableLayer* AddRsqrtLayer(const char* name = nullptr);
609
610 /// Add Gather layer to the network.
611 /// @param name - Optional name for the layer.
612 /// @return - Interface for configuring the layer.
613 ARMNN_DEPRECATED_MSG("Use AddGatherLayer with descriptor instead")
614 IConnectableLayer* AddGatherLayer(const char* name = nullptr);
615
616 /// Add Gather layer to the network.
617 /// @param descriptor - Description of the gather layer.
618 /// @param name - Optional name for the layer.
619 /// @return - Interface for configuring the layer.
620 IConnectableLayer* AddGatherLayer(const GatherDescriptor& descriptor,
621 const char* name = nullptr);
622
623 /// Adds a switch layer to the network.
624 /// @param name - Optional name for the layer.
625 /// @return - Interface for configuring the layer.
626 IConnectableLayer* AddSwitchLayer(const char* name = nullptr);
627
628 /// Adds a PReLU layer to the network.
629 /// @param name - Optional name for the layer.
630 /// @return - Interface for configuring the layer.
631 IConnectableLayer* AddPreluLayer(const char* name = nullptr);
632
633 /// Adds a 2D transpose convolution layer to the network.
634 /// @param descriptor - Description of the 2D transpose convolution layer.
635 /// @param weights - Tensor for the weights data.
636 /// @param biases - Optional tensor for the bias data.
637 /// @param name - Optional name for the layer.
638 /// @return - Interface for configuring the layer.
639 IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
640 const ConstTensor& weights,
641 const Optional<ConstTensor>& biases,
642 const char* name = nullptr);
643
644 /// Adds a transpose layer to the network.
645 /// @param transposeDescriptor - TransposeDescriptor to configure the transpose.
646 /// @param name - Optional name for the layer.
647 /// @return - Interface for configuring the layer.
648 IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
649 const char* name = nullptr);
650
651 /// Adds a stack layer to the network.
652 /// @param descriptor - Description of the stack layer.
653 /// @param name - Optional name for the layer.
654 /// @return - Interface for configuring the layer.
655 IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
656 const char* name = nullptr);
657
658 /// Add a stand-in layer for a type unknown to the Arm NN framework.
659 /// Note: Due to the nature of this layer, no validation can be performed by the framework.
660 /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the
661 /// tensor sizes cannot be inferred.
662 /// @descriptor - Descriptor for the StandIn layer.
663 /// @return - Interface for configuring the layer.
664 IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
665 const char* name = nullptr);
666
667 /// Add a QuantizedLstm layer to the network
668 /// @param params - The weights and biases for the Quantized LSTM cell
669 /// @param name - Optional name for the layer
670 /// @return - Interface for configuring the layer.
671 IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
672 const char* name = nullptr);
673
674 /// Add a QLstm layer to the network
675 /// @param descriptor - Parameters for the QLstm operation
676 /// @param params - Weights and biases for the layer
677 /// @param name - Optional name for the layer
678 /// @return - Interface for configuring the layer.
679 IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
680 const LstmInputParams& params,
681 const char* name = nullptr);
682
683 /// Adds a Logical Binary layer to the network.
684 /// @param descriptor - Description of the Logical Binary layer.
685 /// @param name - Optional name for the layer.
686 /// @return - Interface for configuring the layer.
687 IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& descriptor,
688 const char* name = nullptr);
689
690 void Accept(ILayerVisitor& visitor) const;
691
692 void ExecuteStrategy(IStrategy& strategy) const;
693
694protected:
695 ~INetwork();
696
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000697 friend void VisitLayersTopologically(const INetwork* inputNetwork, IStrategy& strategy);
698 friend class TestConnectionPreservation;
699 friend TensorInfo GetInputTensorInfo(const INetwork* network);
700 friend IOptimizedNetworkPtr Optimize(const INetwork& network,
701 const std::vector<BackendId>& backendPreferences,
702 const IDeviceSpec& deviceSpec,
703 const OptimizerOptions& options,
704 Optional<std::vector<std::string>&> messages);
705
706 INetwork(NetworkOptions networkOptions = {});
707
708 std::unique_ptr<NetworkImpl> pNetworkImpl;
709};
710
Mike Kelly386ff1a2021-03-29 15:04:50 +0100711namespace experimental
712{
Sadik Armagana0042512021-03-30 11:05:36 +0100713class AsyncNetworkImpl;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100714class WorkingMemHandle;
715}
716
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000717struct BackendSettings;
718struct OptimizationResult;
719class OptimizedNetworkImpl;
720class IOptimizedNetwork
721{
722public:
723 static void Destroy(IOptimizedNetwork* network);
724
725 Status PrintGraph();
726 Status SerializeToDot(std::ostream& stream) const;
727
728 profiling::ProfilingGuid GetGuid() const;
729
730 IOptimizedNetwork(std::unique_ptr<Graph> graph);
731 IOptimizedNetwork(std::unique_ptr<OptimizedNetworkImpl> impl);
732 ~IOptimizedNetwork();
733
734protected:
735 friend class LoadedNetwork;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100736
Sadik Armagana0042512021-03-30 11:05:36 +0100737 friend class experimental::AsyncNetworkImpl;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100738 friend class experimental::WorkingMemHandle;
739
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000740 friend Graph& GetGraphForTesting(IOptimizedNetwork* optNetPtr);
741 friend ModelOptions& GetModelOptionsForTesting(IOptimizedNetwork* optNetPtr);
742 friend IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
743 const std::vector<BackendId>& backendPreferences,
744 const IDeviceSpec& deviceSpec,
745 const OptimizerOptions& options,
746 Optional<std::vector<std::string>&> messages);
747
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000748 IOptimizedNetwork(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions);
749
750 std::unique_ptr<OptimizedNetworkImpl> pOptimizedNetworkImpl;
751};
752
telsoa014fcda012018-03-09 14:13:49 +0000753/// Create an optimized version of the network
754/// @param network INetwork description of the network to be optimized.
telsoa01c577f2c2018-08-31 09:22:23 +0100755/// @param backendPreferences The choice of the backend ordered by user preferences.
756/// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
Rob Hughes23214432019-11-05 11:27:36 +0000757/// @param messages If there are failures or warnings a string describing same will be added to the vector
telsoa01c577f2c2018-08-31 09:22:23 +0100758/// @param options OptimizerOptions object with optimizer configuration options
telsoa014fcda012018-03-09 14:13:49 +0000759/// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
760/// armnn::Exception if process fails.
telsoa014fcda012018-03-09 14:13:49 +0000761
telsoa01c577f2c2018-08-31 09:22:23 +0100762IOptimizedNetworkPtr Optimize(const INetwork& network,
David Beckf0b48452018-10-19 15:20:56 +0100763 const std::vector<BackendId>& backendPreferences,
telsoa01c577f2c2018-08-31 09:22:23 +0100764 const IDeviceSpec& deviceSpec,
jimfly016b0b53d2018-10-08 14:43:01 +0100765 const OptimizerOptions& options = OptimizerOptions(),
Rob Hughes23214432019-11-05 11:27:36 +0000766 Optional<std::vector<std::string>&> messages = EmptyOptional());
telsoa014fcda012018-03-09 14:13:49 +0000767} //namespace armnn