blob: 49cd582e67242c390da3987834cab88b5a89cab6 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// Copyright © 2017 Arm Ltd. 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
Jim Flynn906f9462019-05-10 13:55:21 +01007#include <armnn/Deprecated.hpp>
David Beckf0b48452018-10-19 15:20:56 +01008#include <armnn/DescriptorsFwd.hpp>
jimfly01e9e7bfd2019-01-24 22:29:33 +00009#include <armnn/ILayerVisitor.hpp>
Matthew Bentham313e1c82019-03-25 17:37:47 +000010#include <armnn/NetworkFwd.hpp>
11#include <armnn/Optional.hpp>
12#include <armnn/TensorFwd.hpp>
David Beckf0b48452018-10-19 15:20:56 +010013#include <armnn/Types.hpp>
Matteo Martincighfc598e12019-05-14 10:36:13 +010014#include <armnn/Deprecated.hpp>
telsoa014fcda012018-03-09 14:13:49 +000015
16#include <memory>
telsoa01c577f2c2018-08-31 09:22:23 +010017#include <vector>
telsoa014fcda012018-03-09 14:13:49 +000018
19namespace armnn
20{
telsoa014fcda012018-03-09 14:13:49 +000021/// @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.
24class IInputSlot
25{
26public:
27 virtual const IOutputSlot* GetConnection() const = 0;
28 virtual IOutputSlot* GetConnection() = 0;
29
30protected:
telsoa01c577f2c2018-08-31 09:22:23 +010031 /// Not user deletable.
32 ~IInputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000033};
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.
37class IOutputSlot
38{
39public:
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
Mike Kelly8c1701a2019-02-11 17:01:27 +000051 virtual unsigned int CalculateIndexOnOwner() const = 0;
52
53 virtual LayerGuid GetOwningLayerGuid() const = 0;
54
telsoa014fcda012018-03-09 14:13:49 +000055protected:
telsoa01c577f2c2018-08-31 09:22:23 +010056 /// Not user deletable.
57 ~IOutputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000058};
59
60/// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
61class IConnectableLayer
62{
63public:
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000064 /// Returns the name of the layer
telsoa014fcda012018-03-09 14:13:49 +000065 virtual const char* GetName() const = 0;
66
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000067 /// Returns the number of connectable input slots
telsoa014fcda012018-03-09 14:13:49 +000068 virtual unsigned int GetNumInputSlots() const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000069
70 /// Returns the number of connectable output slots
telsoa014fcda012018-03-09 14:13:49 +000071 virtual unsigned int GetNumOutputSlots() const = 0;
72
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000073 /// Get a const input slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000074 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000075
76 /// Get the input slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000077 virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
78
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000079 /// Get the const output slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000080 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000081
82 /// Get the output slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000083 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
84
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000085 /// Infer the shape of the output(s) based on the provided input shape(s)
telsoa01c577f2c2018-08-31 09:22:23 +010086 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
87
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000088 /// Returns the unique id of the layer
surmeh01bceff2f2018-03-29 16:29:27 +010089 virtual LayerGuid GetGuid() const = 0;
jimfly01e9e7bfd2019-01-24 22:29:33 +000090
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000091 /// Apply a visitor to this layer
jimfly01e9e7bfd2019-01-24 22:29:33 +000092 virtual void Accept(ILayerVisitor& visitor) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000093
94 /// Provide a hint for the optimizer as to which backend to prefer for this layer
95 virtual void BackendSelectionHint(Optional<BackendId> backend) = 0;
telsoa014fcda012018-03-09 14:13:49 +000096protected:
telsoa01c577f2c2018-08-31 09:22:23 +010097 /// Objects are not deletable via the handle
98 ~IConnectableLayer() {}
telsoa014fcda012018-03-09 14:13:49 +000099};
100
101using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
102
103/// Main network class which provides the interface for building up a neural network.
104/// This object is subsequently required by the IRuntime::Load() method.
105class INetwork
106{
107public:
108 static INetwork* CreateRaw();
109 static INetworkPtr Create();
110 static void Destroy(INetwork* network);
111
112 virtual Status PrintGraph() = 0;
113
telsoa01c577f2c2018-08-31 09:22:23 +0100114 /// Adds an input layer to the network.
115 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
telsoa014fcda012018-03-09 14:13:49 +0000116 /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
telsoa01c577f2c2018-08-31 09:22:23 +0100117 /// @param name - Optional name for the layer.
118 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000119 virtual IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr) = 0;
120
Nikhil Rajee391d52019-09-05 17:50:44 +0100121 /// Adds an ArgMinMax layer to the network.
122 /// @param desc - Parameters for the L2 normalization operation.
123 /// @param name - Optional name for the layer.
124 /// @return - Interface for configuring the layer.
125 virtual IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
126 const char* name = nullptr) = 0;
127
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100128 /// Add a Comparison layer to the network.
129 /// @param name - Optional name for the layer.
130 /// @param desc - Descriptor for the comparison operation.
131 /// @ return - Interface for configuring the layer.
132 virtual IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
133 const char* name = nullptr) = 0;
134
Jim Flynn906f9462019-05-10 13:55:21 +0100135 /// Adds a concatenation layer to the network.
Jim Flynne242f2d2019-05-22 14:24:13 +0100136 /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
137 /// process. Number of Views must be equal to the number of inputs, and their order
138 /// must match - e.g. first view corresponds to the first input, second view to the
139 /// second input, etc....
Jim Flynn906f9462019-05-10 13:55:21 +0100140 /// @param name - Optional name for the layer.
141 /// @return - Interface for configuring the layer.
Jim Flynne242f2d2019-05-22 14:24:13 +0100142 virtual IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
Jim Flynn906f9462019-05-10 13:55:21 +0100143 const char* name = nullptr) = 0;
144
telsoa01c577f2c2018-08-31 09:22:23 +0100145 /// Adds a 2D convolution layer to the network.
146 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
147 /// @param weights - Tensor for the weights data.
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000148 /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
telsoa01c577f2c2018-08-31 09:22:23 +0100149 /// @param name - Optional name for the layer.
150 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000151 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000152 const ConstTensor& weights,
153 const Optional<ConstTensor>& biases,
154 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000155
Matteo Martincighfc598e12019-05-14 10:36:13 +0100156 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
telsoa014fcda012018-03-09 14:13:49 +0000157 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000158 const ConstTensor& weights,
159 const char* name = nullptr) = 0;
160
Matteo Martincighfc598e12019-05-14 10:36:13 +0100161 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000162 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
163 const ConstTensor& weights,
164 const ConstTensor& biases,
165 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000166
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +0100167 /// Adds a depth to space layer to the network.
168 /// @param depthToSpaceDescriptor - Parameters for the depth to space operation.
169 /// @param name - Optional name for the layer.
170 /// @return - Interface for configuring the layer.
171 virtual IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
172 const char* name = nullptr) = 0;
173
telsoa01c577f2c2018-08-31 09:22:23 +0100174 /// Adds a 2D depthwise convolution layer to the network.
175 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
Rob Hughes144c01b2018-11-21 13:34:24 +0000176 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000177 /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
telsoa01c577f2c2018-08-31 09:22:23 +0100178 /// @param name - Optional name for the layer.
179 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000180 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
181 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
182 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000183 const Optional<ConstTensor>& biases,
telsoa014fcda012018-03-09 14:13:49 +0000184 const char* name = nullptr) = 0;
185
Matteo Martincighfc598e12019-05-14 10:36:13 +0100186 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000187 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
188 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
189 const ConstTensor& weights,
190 const char* name = nullptr) = 0;
191
Matteo Martincighfc598e12019-05-14 10:36:13 +0100192 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
telsoa014fcda012018-03-09 14:13:49 +0000193 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
194 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
195 const ConstTensor& weights,
196 const ConstTensor& biases,
197 const char* name = nullptr) = 0;
198
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +0000199 /// Adds a Dequantize layer to the network.
200 /// @return - Interface for configuring the layer.
201 virtual IConnectableLayer* AddDequantizeLayer(const char* name = nullptr) = 0;
202
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000203 /// Adds a Detection PostProcess layer to the network.
204 /// @param descriptor - Description of the Detection PostProcess layer.
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +0000205 /// @param anchors - Tensor for anchors.
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000206 /// @param name - Optional name for the layer.
207 /// @return - Interface for configuring the layer.
208 virtual IConnectableLayer* AddDetectionPostProcessLayer(
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +0000209 const DetectionPostProcessDescriptor& descriptor,
210 const ConstTensor& anchors,
211 const char* name = nullptr) = 0;
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000212
josh minor4a3c6102020-01-06 16:40:46 -0600213 /// Add an ElementwiseUnary layer to the network.
214 /// @param name - Optional name for the layer.
215 /// @param desc - Descriptor for the elementwiseUnary operation.
Ryan OSheaec6c6802020-06-05 17:17:06 +0100216 /// @return - Interface for configuring the layer.
josh minor4a3c6102020-01-06 16:40:46 -0600217 virtual IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
218 const char* name = nullptr) = 0;
219
Ryan OSheaec6c6802020-06-05 17:17:06 +0100220 /// Add an Fill layer to the network.
221 /// @param name - Optional name for the layer.
222 /// @param fillDescriptor - Descriptor for the fill operation.
223 /// @return - Interface for configuring the layer.
224 virtual IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
225 const char* name = nullptr) = 0;
226
telsoa01c577f2c2018-08-31 09:22:23 +0100227 /// Adds a fully connected layer to the network.
228 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
229 /// @param weights - Tensor for the weights data.
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000230 /// @param biases - Optional tensor for the bias data.
telsoa01c577f2c2018-08-31 09:22:23 +0100231 /// @param name - Optional name for the layer.
232 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000233 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000234 const ConstTensor& weights,
235 const Optional<ConstTensor>& biases,
236 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000237
Matteo Martincighfc598e12019-05-14 10:36:13 +0100238 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
telsoa014fcda012018-03-09 14:13:49 +0000239 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000240 const ConstTensor& weights,
241 const char* name = nullptr) = 0;
242
Matteo Martincighfc598e12019-05-14 10:36:13 +0100243 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000244 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
245 const ConstTensor& weights,
246 const ConstTensor& biases,
247 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000248
telsoa01c577f2c2018-08-31 09:22:23 +0100249 /// Adds a permute layer to the network.
250 /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
251 /// @param name - Optional name for the layer.
252 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000253 virtual IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
254 const char* name = nullptr) = 0;
255
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +0000256 /// Adds a batch to space ND layer to the network.
257 /// @param batchToSpaceNdDescriptor - Description of the layer.
258 /// @param name - Optional name for the layer.
259 /// @return - Interface for configuring the layer.
260 virtual IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
261 const char* name = nullptr) = 0;
262
telsoa01c577f2c2018-08-31 09:22:23 +0100263 /// Adds a pooling layer to the network.
264 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
265 /// @param name - Optional name for the layer.
266 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000267 virtual IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
268 const char* name = nullptr) = 0;
269
telsoa01c577f2c2018-08-31 09:22:23 +0100270 /// Adds an activation layer to the network.
271 /// @param activationDescriptor - ActivationDescriptor to configure the activation.
272 /// @param name - Optional name for the layer.
273 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000274 virtual IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
275 const char* name = nullptr) = 0;
276
telsoa01c577f2c2018-08-31 09:22:23 +0100277 /// Adds a normalization layer to the network.
278 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
279 /// @param name - Optional name for the layer.
280 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000281 virtual IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
282 const char* name = nullptr) = 0;
283
Aron Virginas-Tar636ab402019-09-16 14:27:45 +0100284 /// Adds a slice layer to the network.
285 /// @param sliceDescriptor - SliceDescriptor to configure the slice operation.
286 /// @param name - Optional name for the layer.
287 /// @return - Interface for configuring the layer.
288 virtual IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr) = 0;
289
telsoa01c577f2c2018-08-31 09:22:23 +0100290 /// Adds a softmax layer to the network.
David Monahanb8554702019-04-25 16:03:38 +0100291 /// If the data type is QAsymm8, then the output quantization parameters
292 /// must have a scale of 1/256 and an offset of 0
telsoa01c577f2c2018-08-31 09:22:23 +0100293 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
294 /// @param name - Optional name for the layer.
295 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000296 virtual IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
297 const char* name = nullptr) = 0;
298
telsoa01c577f2c2018-08-31 09:22:23 +0100299 /// Adds a splitter layer to the network.
Jim Flynne242f2d2019-05-22 14:24:13 +0100300 /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
telsoa01c577f2c2018-08-31 09:22:23 +0100301 /// Number of Views must be equal to the number of outputs,
302 /// and their order must match - e.g. first view corresponds to
303 /// the first output, second view to the second output, etc....
304 /// @param name - Optional name for the layer.
305 /// @return - Interface for configuring the layer.
Aron Virginas-Tar636ab402019-09-16 14:27:45 +0100306 virtual IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
307 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000308
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +0100309 /// Adds a merge layer to the network.
310 /// @param name - Optional name for the layer.
311 /// @return - Interface for configuring the layer.
312 virtual IConnectableLayer* AddMergeLayer(const char* name = nullptr) = 0;
313
Jim Flynne242f2d2019-05-22 14:24:13 +0100314 /// Adds a concat layer to the network.
315 /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation
316 /// process. Number of Views must be equal to the number of inputs, and their order
317 /// must match - e.g. first view corresponds to the first input, second view to the
318 /// second input, etc....
telsoa01c577f2c2018-08-31 09:22:23 +0100319 /// @param name - Optional name for the layer.
320 /// @return - Interface for configuring the layer.
Jim Flynn906f9462019-05-10 13:55:21 +0100321 ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead")
Jim Flynne242f2d2019-05-22 14:24:13 +0100322 virtual IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor,
telsoa014fcda012018-03-09 14:13:49 +0000323 const char* name = nullptr) = 0;
324
Kevin May868eb142019-09-04 17:29:31 +0100325 /// Add absolute layer to the network.
326 /// @param name - Optional name for the layer.
327 /// @ return - Interface for configuring the layer.
josh minor4a3c6102020-01-06 16:40:46 -0600328 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
Kevin May868eb142019-09-04 17:29:31 +0100329 virtual IConnectableLayer* AddAbsLayer(const char* name = nullptr) = 0;
330
telsoa01c577f2c2018-08-31 09:22:23 +0100331 /// Adds an addition layer to the network.
332 /// @param name - Optional name for the layer.
333 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000334 virtual IConnectableLayer* AddAdditionLayer(const char* name = nullptr) = 0;
335
telsoa01c577f2c2018-08-31 09:22:23 +0100336 /// Adds a multiplication layer to the network.
337 /// @param name - Optional name for the layer.
338 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000339 virtual IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr) = 0;
340
telsoa01c577f2c2018-08-31 09:22:23 +0100341 /// Adds a batch normalization layer to the network.
342 /// @param mean - Pre-calculated mean for each channel.
343 /// @param variance - Pre-calculated variance for each channel.
344 /// @param beta - Per-channel additive factor.
345 /// @param gamma - Per-channel multiplicative factor.
346 /// @return - Interface for configuring the layer.
347 /// @param name - Optional name for the layer.
telsoa014fcda012018-03-09 14:13:49 +0000348 virtual IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
349 const ConstTensor& mean,
350 const ConstTensor& variance,
351 const ConstTensor& beta,
352 const ConstTensor& gamma,
353 const char* name = nullptr) = 0;
354
telsoa01c577f2c2018-08-31 09:22:23 +0100355 /// Adds a resize bilinear layer to the network.
356 /// @param resizeDesc - Parameters for the resize operation.
357 /// @param name - Optional name for the layer.
358 /// @return - Interface for configuring the layer.
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +0100359 ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead")
telsoa014fcda012018-03-09 14:13:49 +0000360 virtual IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
361 const char* name = nullptr) = 0;
362
Teresa Charlina9075df2019-06-27 15:41:57 +0100363 /// Adds a resize layer to the network.
364 /// @param resizeDescriptor - Parameters for the resize operation.
365 /// @param name - Optional name for the layer.
366 /// @return - Interface for configuring the layer.
367 virtual IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
368 const char* name = nullptr) = 0;
369
Kevin Mayce5045a2019-10-02 14:07:47 +0100370 /// Adds an instance normalization layer to the network.
371 /// @param desc - Parameters for the instance normalization operation.
372 /// @param name - Optional name for the layer.
373 /// @return - Interface for configuring the layer.
374 virtual IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
375 const char* name = nullptr) = 0;
376
telsoa01c577f2c2018-08-31 09:22:23 +0100377 /// Adds an L2 normalization layer to the network.
telsoa014fcda012018-03-09 14:13:49 +0000378 /// Normalization is performed along dimension 1, but requires a 4d input.
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100379 /// @param desc - Parameters for the L2 normalization operation.
telsoa01c577f2c2018-08-31 09:22:23 +0100380 /// @param name - Optional name for the layer.
381 /// @return - Interface for configuring the layer.
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100382 virtual IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
383 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000384
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +0100385 /// Adds a log softmax layer to the network.
386 /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax.
387 /// @param name - Optional name for the layer.
388 /// @return - Interface for configuring the layer.
389 virtual IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
390 const char* name = nullptr) = 0;
391
telsoa014fcda012018-03-09 14:13:49 +0000392 /// Adds a layer with no inputs and a single output, which always corresponds to
393 /// the passed in constant tensor.
telsoa01c577f2c2018-08-31 09:22:23 +0100394 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
395 /// its own copy of the tensor data, meaning the memory referenced by @a input can
396 /// be freed or reused after this function is called.
397 /// @param name - Optional name for the layer.
398 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000399 virtual IConnectableLayer* AddConstantLayer(const ConstTensor& input,
Matteo Martincigh5b2159e2019-02-11 13:24:38 +0000400 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000401
telsoa01c577f2c2018-08-31 09:22:23 +0100402 /// Adds a reshape layer to the network.
403 /// @param reshapeDescriptor - Parameters for the reshape operation.
404 /// @param name - Optional name for the layer.
405 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000406 virtual IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
407 const char* name = nullptr) = 0;
408
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000409 /// Adds a space to batch layer to the network.
410 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
411 /// @param name - Optional name for the layer.
412 /// @return - Interface for configuring the layer.
413 virtual IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
414 const char* name = nullptr) = 0;
415
Aron Virginas-Tar972af152019-06-11 14:14:03 +0100416 /// Adds a space to depth layer to the network.
417 /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
418 /// @param name - Optional name for the layer.
419 /// @return - Interface for configuring the layer.
420 virtual IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
421 const char* name = nullptr) = 0;
422
telsoa01c577f2c2018-08-31 09:22:23 +0100423 /// Adds a floor layer to the network.
424 /// @param name - Optional name for the layer.
425 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000426 virtual IConnectableLayer* AddFloorLayer(const char* name = nullptr) = 0;
427
telsoa01c577f2c2018-08-31 09:22:23 +0100428 /// Adds an output layer to the network.
429 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
telsoa014fcda012018-03-09 14:13:49 +0000430 /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
telsoa01c577f2c2018-08-31 09:22:23 +0100431 /// @param name - Optional name for the layer.
432 /// @return - Interface for configuring the layer.
433 virtual IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr) = 0;
434
435 /// Add a Lstm layer to the network
James Conroyee18dc82019-07-17 11:27:46 +0100436 /// @param descriptor - Parameters for the Lstm operation
437 /// @param params - Weights and biases for the LSTM cell
438 /// @param name - Optional name for the layer
439 /// @return - Interface for configuring the layer.
telsoa01c577f2c2018-08-31 09:22:23 +0100440 virtual IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
441 const LstmInputParams& params,
442 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000443
Francis Murtaghe7a86a42018-08-29 12:42:10 +0100444 /// Adds a division layer to the network.
445 /// @param name - Optional name for the layer.
446 /// @return - Interface for configuring the layer.
447 virtual IConnectableLayer* AddDivisionLayer(const char* name = nullptr) = 0;
448
David Beck19526222018-09-12 16:00:08 +0100449 /// Adds a subtraction layer to the network.
450 /// @param name - Optional name for the layer.
451 /// @return - Interface for configuring the layer.
452 virtual IConnectableLayer* AddSubtractionLayer(const char* name = nullptr) = 0;
453
Nattapat Chaimanowong5a4304a2018-11-28 10:44:37 +0000454 /// Add a Maximum layer to the network.
455 /// @param name - Optional name for the layer.
456 /// @ return - Interface for configuring the layer.
457 virtual IConnectableLayer* AddMaximumLayer(const char* name = nullptr) = 0;
458
narpra0132b90462018-09-13 11:07:48 +0100459 /// Add a Mean layer to the network.
460 /// @param meanDescriptor - Parameters for the mean operation.
461 /// @param name - Optional name for the layer.
462 /// @ return - Interface for configuring the layer.
463 virtual IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr) = 0;
464
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +0100465 /// Adds a fully pad layer to the network.
466 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
467 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
468 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
469 /// @param name - Optional name for the layer.
470 /// @return - Interface for configuring the layer.
471 virtual IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
472 const char* name = nullptr) = 0;
473
Derek Lambertia9cca6a2019-03-25 15:41:58 +0000474 /// Add a quantize layer to the network
475 ///@param name - Optional name for the layer.
476 /// @return - Interface for configuring the layer.
477 virtual IConnectableLayer* AddQuantizeLayer(const char* name = nullptr) = 0;
478
Conor Kennedy430b5d82018-11-14 15:28:28 +0000479 /// Adds a strided slice layer to the network.
480 /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
481 /// @param name - Optional name for the layer.
482 /// @return - Interface for configuring the layer.
483 virtual IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
484 const char* name = nullptr) = 0;
485
kevmay0190539692018-11-29 08:40:19 +0000486 /// Add a Minimum layer to the network.
487 /// @param name - Optional name for the layer.
488 /// @ return - Interface for configuring the layer.
489 virtual IConnectableLayer* AddMinimumLayer(const char* name = nullptr) = 0;
490
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000491 /// Add a Greater layer to the network.
492 /// @param name - Optional name for the layer.
493 /// @ return - Interface for configuring the layer.
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100494 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000495 virtual IConnectableLayer* AddGreaterLayer(const char* name = nullptr) = 0;
496
FrancisMurtagh20995952018-12-17 12:11:36 +0000497 /// Add a Equal layer to the network.
498 /// @param name - Optional name for the layer.
499 /// @ return - Interface for configuring the layer.
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100500 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
FrancisMurtagh20995952018-12-17 12:11:36 +0000501 virtual IConnectableLayer* AddEqualLayer(const char* name = nullptr) = 0;
502
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +0000503 /// Add Reciprocal of square root layer to the network.
504 /// @param name - Optional name for the layer.
505 /// @ return - Interface for configuring the layer.
josh minor4a3c6102020-01-06 16:40:46 -0600506 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +0000507 virtual IConnectableLayer* AddRsqrtLayer(const char* name = nullptr) = 0;
508
narpra01b89b05f2019-01-16 09:53:09 +0000509 /// Add Gather layer to the network.
510 /// @param name - Optional name for the layer.
511 /// @ return - Interface for configuring the layer.
512 virtual IConnectableLayer* AddGatherLayer(const char* name = nullptr) = 0;
513
Sadik Armaganeff363d2019-04-05 15:25:46 +0100514 /// Adds a switch layer to the network.
515 /// @param name - Optional name for the layer.
516 /// @return - Interface for configuring the layer.
517 virtual IConnectableLayer* AddSwitchLayer(const char* name = nullptr) = 0;
518
Matteo Martincigh0e406ee2019-06-12 15:42:18 +0100519 /// Adds a PReLU layer to the network.
520 /// @param name - Optional name for the layer.
521 /// @return - Interface for configuring the layer.
522 virtual IConnectableLayer* AddPreluLayer(const char* name = nullptr) = 0;
523
Aron Virginas-Tar639fb042019-06-20 14:28:19 +0100524 /// Adds a 2D transpose convolution layer to the network.
525 /// @param descriptor - Description of the 2D transpose convolution layer.
526 /// @param weights - Tensor for the weights data.
527 /// @param biases - Optional tensor for the bias data.
528 /// @param name - Optional name for the layer.
529 /// @return - Interface for configuring the layer.
530 virtual IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
531 const ConstTensor& weights,
532 const Optional<ConstTensor>& biases,
533 const char* name = nullptr) = 0;
534
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000535 /// Adds a transpose layer to the network.
536 /// @param transposeDescriptor - TransposeDescriptor to configure the transpose.
537 /// @param name - Optional name for the layer.
538 /// @return - Interface for configuring the layer.
539 virtual IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
540 const char* name = nullptr) = 0;
541
Matthew Jackson2b8c1da2019-07-04 14:59:16 +0100542 /// Adds a stack layer to the network.
543 /// @param descriptor - Description of the stack layer.
544 /// @param name - Optional name for the layer.
545 /// @return - Interface for configuring the layer.
546 virtual IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
547 const char* name = nullptr) = 0;
548
Derek Lamberti013c3902019-10-21 10:46:16 +0100549 /// Add a stand-in layer for a type unknown to the Arm NN framework.
550 /// Note: Due to the nature of this layer, no validation can be performed by the framework.
551 /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the
552 /// tensor sizes cannot be inferred.
553 /// @descriptor - Descriptor for the StandIn layer.
554 /// @return - Interface for configuring the layer.
555 virtual IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
556 const char* name = nullptr) = 0;
557
James Conroyee18dc82019-07-17 11:27:46 +0100558 /// Add a QuantizedLstm layer to the network
559 /// @param params - The weights and biases for the Quantized LSTM cell
560 /// @param name - Optional name for the layer
561 /// @return - Interface for configuring the layer.
562 virtual IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
563 const char* name = nullptr) = 0;
564
James Conroy586a9aa2020-03-20 08:49:33 +0000565 /// Add a QLstm layer to the network
566 /// @param descriptor - Parameters for the QLstm operation
567 /// @param params - Weights and biases for the layer
568 /// @param name - Optional name for the layer
569 /// @return - Interface for configuring the layer.
570 virtual IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
571 const LstmInputParams& params,
572 const char* name = nullptr) = 0;
573
Mike Kelly8c1701a2019-02-11 17:01:27 +0000574 virtual void Accept(ILayerVisitor& visitor) const = 0;
575
telsoa014fcda012018-03-09 14:13:49 +0000576protected:
577 ~INetwork() {}
578};
579
580using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
581
582class IOptimizedNetwork
583{
584public:
585 static void Destroy(IOptimizedNetwork* network);
586
587 virtual Status PrintGraph() = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100588 virtual Status SerializeToDot(std::ostream& stream) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000589
Jan Eilers99d9d4a2019-11-06 10:02:16 +0000590 virtual profiling::ProfilingGuid GetGuid() const = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100591
telsoa014fcda012018-03-09 14:13:49 +0000592protected:
593 ~IOptimizedNetwork() {}
594};
595
telsoa01c577f2c2018-08-31 09:22:23 +0100596struct OptimizerOptions
597{
Matteo Martincigh49124022019-01-11 13:25:59 +0000598 OptimizerOptions()
599 : m_ReduceFp32ToFp16(false)
600 , m_Debug(false)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000601 , m_ReduceFp32ToBf16(false)
Teresa Charlincdc01492020-06-09 18:00:20 +0100602 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
keidav01738c2e62018-12-11 16:14:20 +0000603 {}
telsoa01c577f2c2018-08-31 09:22:23 +0100604
Teresa Charlincdc01492020-06-09 18:00:20 +0100605 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16 = false,
606 ShapeInferenceMethod shapeInferenceMethod = armnn::ShapeInferenceMethod::ValidateOnly)
telsoa01c577f2c2018-08-31 09:22:23 +0100607 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
keidav01738c2e62018-12-11 16:14:20 +0000608 , m_Debug(debug)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000609 , m_ReduceFp32ToBf16(reduceFp32ToBf16)
Teresa Charlincdc01492020-06-09 18:00:20 +0100610 , m_shapeInferenceMethod(shapeInferenceMethod)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000611 {
612 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
613 {
614 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
615 }
616 }
telsoa01c577f2c2018-08-31 09:22:23 +0100617
618 // Reduce Fp32 data to Fp16 for faster processing
619 bool m_ReduceFp32ToFp16;
keidav01738c2e62018-12-11 16:14:20 +0000620
621 // Add debug data for easier troubleshooting
622 bool m_Debug;
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000623
624 // Reduce Fp32 data to Bf16 for faster processing
625 bool m_ReduceFp32ToBf16;
Teresa Charlincdc01492020-06-09 18:00:20 +0100626
627 // Infer output size when not available
628 ShapeInferenceMethod m_shapeInferenceMethod;
telsoa01c577f2c2018-08-31 09:22:23 +0100629};
telsoa014fcda012018-03-09 14:13:49 +0000630
631/// Create an optimized version of the network
632/// @param network INetwork description of the network to be optimized.
telsoa01c577f2c2018-08-31 09:22:23 +0100633/// @param backendPreferences The choice of the backend ordered by user preferences.
634/// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
Rob Hughes23214432019-11-05 11:27:36 +0000635/// @param messages If there are failures or warnings a string describing same will be added to the vector
telsoa01c577f2c2018-08-31 09:22:23 +0100636/// @param options OptimizerOptions object with optimizer configuration options
telsoa014fcda012018-03-09 14:13:49 +0000637/// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
638/// armnn::Exception if process fails.
telsoa014fcda012018-03-09 14:13:49 +0000639
telsoa01c577f2c2018-08-31 09:22:23 +0100640IOptimizedNetworkPtr Optimize(const INetwork& network,
David Beckf0b48452018-10-19 15:20:56 +0100641 const std::vector<BackendId>& backendPreferences,
telsoa01c577f2c2018-08-31 09:22:23 +0100642 const IDeviceSpec& deviceSpec,
jimfly016b0b53d2018-10-08 14:43:01 +0100643 const OptimizerOptions& options = OptimizerOptions(),
Rob Hughes23214432019-11-05 11:27:36 +0000644 Optional<std::vector<std::string>&> messages = EmptyOptional());
telsoa014fcda012018-03-09 14:13:49 +0000645} //namespace armnn