blob: 71eb9ff8023748a24f759d9a79b8ab35f9478264 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// 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:
64 virtual const char* GetName() const = 0;
65
66 virtual unsigned int GetNumInputSlots() const = 0;
67 virtual unsigned int GetNumOutputSlots() const = 0;
68
69 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
70 virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
71
72 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
73 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
74
telsoa01c577f2c2018-08-31 09:22:23 +010075 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
76
surmeh01bceff2f2018-03-29 16:29:27 +010077 virtual LayerGuid GetGuid() const = 0;
jimfly01e9e7bfd2019-01-24 22:29:33 +000078
79 virtual void Accept(ILayerVisitor& visitor) const = 0;
telsoa014fcda012018-03-09 14:13:49 +000080protected:
telsoa01c577f2c2018-08-31 09:22:23 +010081 /// Objects are not deletable via the handle
82 ~IConnectableLayer() {}
telsoa014fcda012018-03-09 14:13:49 +000083};
84
85using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
86
87/// Main network class which provides the interface for building up a neural network.
88/// This object is subsequently required by the IRuntime::Load() method.
89class INetwork
90{
91public:
92 static INetwork* CreateRaw();
93 static INetworkPtr Create();
94 static void Destroy(INetwork* network);
95
96 virtual Status PrintGraph() = 0;
97
Jan Eilers99d9d4a2019-11-06 10:02:16 +000098 virtual profiling::ProfilingGuid GetGuid() const = 0;
99
telsoa01c577f2c2018-08-31 09:22:23 +0100100 /// Adds an input layer to the network.
101 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
telsoa014fcda012018-03-09 14:13:49 +0000102 /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
telsoa01c577f2c2018-08-31 09:22:23 +0100103 /// @param name - Optional name for the layer.
104 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000105 virtual IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr) = 0;
106
Nikhil Rajee391d52019-09-05 17:50:44 +0100107 /// Adds an ArgMinMax layer to the network.
108 /// @param desc - Parameters for the L2 normalization operation.
109 /// @param name - Optional name for the layer.
110 /// @return - Interface for configuring the layer.
111 virtual IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
112 const char* name = nullptr) = 0;
113
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100114 /// Add a Comparison layer to the network.
115 /// @param name - Optional name for the layer.
116 /// @param desc - Descriptor for the comparison operation.
117 /// @ return - Interface for configuring the layer.
118 virtual IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
119 const char* name = nullptr) = 0;
120
Jim Flynn906f9462019-05-10 13:55:21 +0100121 /// Adds a concatenation layer to the network.
Jim Flynne242f2d2019-05-22 14:24:13 +0100122 /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
123 /// process. Number of Views must be equal to the number of inputs, and their order
124 /// must match - e.g. first view corresponds to the first input, second view to the
125 /// second input, etc....
Jim Flynn906f9462019-05-10 13:55:21 +0100126 /// @param name - Optional name for the layer.
127 /// @return - Interface for configuring the layer.
Jim Flynne242f2d2019-05-22 14:24:13 +0100128 virtual IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
Jim Flynn906f9462019-05-10 13:55:21 +0100129 const char* name = nullptr) = 0;
130
telsoa01c577f2c2018-08-31 09:22:23 +0100131 /// Adds a 2D convolution layer to the network.
132 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
133 /// @param weights - Tensor for the weights data.
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000134 /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
telsoa01c577f2c2018-08-31 09:22:23 +0100135 /// @param name - Optional name for the layer.
136 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000137 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000138 const ConstTensor& weights,
139 const Optional<ConstTensor>& biases,
140 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000141
Matteo Martincighfc598e12019-05-14 10:36:13 +0100142 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
telsoa014fcda012018-03-09 14:13:49 +0000143 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000144 const ConstTensor& weights,
145 const char* name = nullptr) = 0;
146
Matteo Martincighfc598e12019-05-14 10:36:13 +0100147 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated")
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000148 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
149 const ConstTensor& weights,
150 const ConstTensor& biases,
151 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000152
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +0100153 /// Adds a depth to space layer to the network.
154 /// @param depthToSpaceDescriptor - Parameters for the depth to space operation.
155 /// @param name - Optional name for the layer.
156 /// @return - Interface for configuring the layer.
157 virtual IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
158 const char* name = nullptr) = 0;
159
telsoa01c577f2c2018-08-31 09:22:23 +0100160 /// Adds a 2D depthwise convolution layer to the network.
161 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
Rob Hughes144c01b2018-11-21 13:34:24 +0000162 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000163 /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
telsoa01c577f2c2018-08-31 09:22:23 +0100164 /// @param name - Optional name for the layer.
165 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000166 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
167 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
168 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000169 const Optional<ConstTensor>& biases,
telsoa014fcda012018-03-09 14:13:49 +0000170 const char* name = nullptr) = 0;
171
Matteo Martincighfc598e12019-05-14 10:36:13 +0100172 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000173 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
174 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
175 const ConstTensor& weights,
176 const char* name = nullptr) = 0;
177
Matteo Martincighfc598e12019-05-14 10:36:13 +0100178 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
telsoa014fcda012018-03-09 14:13:49 +0000179 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
180 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
181 const ConstTensor& weights,
182 const ConstTensor& biases,
183 const char* name = nullptr) = 0;
184
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +0000185 /// Adds a Dequantize layer to the network.
186 /// @return - Interface for configuring the layer.
187 virtual IConnectableLayer* AddDequantizeLayer(const char* name = nullptr) = 0;
188
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000189 /// Adds a Detection PostProcess layer to the network.
190 /// @param descriptor - Description of the Detection PostProcess layer.
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +0000191 /// @param anchors - Tensor for anchors.
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000192 /// @param name - Optional name for the layer.
193 /// @return - Interface for configuring the layer.
194 virtual IConnectableLayer* AddDetectionPostProcessLayer(
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +0000195 const DetectionPostProcessDescriptor& descriptor,
196 const ConstTensor& anchors,
197 const char* name = nullptr) = 0;
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000198
josh minor4a3c6102020-01-06 16:40:46 -0600199 /// Add an ElementwiseUnary layer to the network.
200 /// @param name - Optional name for the layer.
201 /// @param desc - Descriptor for the elementwiseUnary operation.
202 /// @ return - Interface for configuring the layer.
203 virtual IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
204 const char* name = nullptr) = 0;
205
telsoa01c577f2c2018-08-31 09:22:23 +0100206 /// Adds a fully connected layer to the network.
207 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
208 /// @param weights - Tensor for the weights data.
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000209 /// @param biases - Optional tensor for the bias data.
telsoa01c577f2c2018-08-31 09:22:23 +0100210 /// @param name - Optional name for the layer.
211 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000212 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000213 const ConstTensor& weights,
214 const Optional<ConstTensor>& biases,
215 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000216
Matteo Martincighfc598e12019-05-14 10:36:13 +0100217 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
telsoa014fcda012018-03-09 14:13:49 +0000218 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000219 const ConstTensor& weights,
220 const char* name = nullptr) = 0;
221
Matteo Martincighfc598e12019-05-14 10:36:13 +0100222 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated")
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000223 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
224 const ConstTensor& weights,
225 const ConstTensor& biases,
226 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000227
telsoa01c577f2c2018-08-31 09:22:23 +0100228 /// Adds a permute layer to the network.
229 /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
230 /// @param name - Optional name for the layer.
231 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000232 virtual IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
233 const char* name = nullptr) = 0;
234
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +0000235 /// Adds a batch to space ND layer to the network.
236 /// @param batchToSpaceNdDescriptor - Description of the layer.
237 /// @param name - Optional name for the layer.
238 /// @return - Interface for configuring the layer.
239 virtual IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
240 const char* name = nullptr) = 0;
241
telsoa01c577f2c2018-08-31 09:22:23 +0100242 /// Adds a pooling layer to the network.
243 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
244 /// @param name - Optional name for the layer.
245 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000246 virtual IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
247 const char* name = nullptr) = 0;
248
telsoa01c577f2c2018-08-31 09:22:23 +0100249 /// Adds an activation layer to the network.
250 /// @param activationDescriptor - ActivationDescriptor to configure the activation.
251 /// @param name - Optional name for the layer.
252 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000253 virtual IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
254 const char* name = nullptr) = 0;
255
telsoa01c577f2c2018-08-31 09:22:23 +0100256 /// Adds a normalization layer to the network.
257 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
258 /// @param name - Optional name for the layer.
259 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000260 virtual IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
261 const char* name = nullptr) = 0;
262
Aron Virginas-Tar636ab402019-09-16 14:27:45 +0100263 /// Adds a slice layer to the network.
264 /// @param sliceDescriptor - SliceDescriptor to configure the slice operation.
265 /// @param name - Optional name for the layer.
266 /// @return - Interface for configuring the layer.
267 virtual IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr) = 0;
268
telsoa01c577f2c2018-08-31 09:22:23 +0100269 /// Adds a softmax layer to the network.
David Monahanb8554702019-04-25 16:03:38 +0100270 /// If the data type is QAsymm8, then the output quantization parameters
271 /// must have a scale of 1/256 and an offset of 0
telsoa01c577f2c2018-08-31 09:22:23 +0100272 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
273 /// @param name - Optional name for the layer.
274 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000275 virtual IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
276 const char* name = nullptr) = 0;
277
telsoa01c577f2c2018-08-31 09:22:23 +0100278 /// Adds a splitter layer to the network.
Jim Flynne242f2d2019-05-22 14:24:13 +0100279 /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
telsoa01c577f2c2018-08-31 09:22:23 +0100280 /// Number of Views must be equal to the number of outputs,
281 /// and their order must match - e.g. first view corresponds to
282 /// the first output, second view to the second output, etc....
283 /// @param name - Optional name for the layer.
284 /// @return - Interface for configuring the layer.
Aron Virginas-Tar636ab402019-09-16 14:27:45 +0100285 virtual IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
286 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000287
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +0100288 /// Adds a merge layer to the network.
289 /// @param name - Optional name for the layer.
290 /// @return - Interface for configuring the layer.
291 virtual IConnectableLayer* AddMergeLayer(const char* name = nullptr) = 0;
292
Jim Flynne242f2d2019-05-22 14:24:13 +0100293 /// Adds a concat layer to the network.
294 /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation
295 /// process. Number of Views must be equal to the number of inputs, and their order
296 /// must match - e.g. first view corresponds to the first input, second view to the
297 /// second input, etc....
telsoa01c577f2c2018-08-31 09:22:23 +0100298 /// @param name - Optional name for the layer.
299 /// @return - Interface for configuring the layer.
Jim Flynn906f9462019-05-10 13:55:21 +0100300 ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead")
Jim Flynne242f2d2019-05-22 14:24:13 +0100301 virtual IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor,
telsoa014fcda012018-03-09 14:13:49 +0000302 const char* name = nullptr) = 0;
303
Kevin May868eb142019-09-04 17:29:31 +0100304 /// Add absolute layer to the network.
305 /// @param name - Optional name for the layer.
306 /// @ return - Interface for configuring the layer.
josh minor4a3c6102020-01-06 16:40:46 -0600307 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
Kevin May868eb142019-09-04 17:29:31 +0100308 virtual IConnectableLayer* AddAbsLayer(const char* name = nullptr) = 0;
309
telsoa01c577f2c2018-08-31 09:22:23 +0100310 /// Adds an addition layer to the network.
311 /// @param name - Optional name for the layer.
312 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000313 virtual IConnectableLayer* AddAdditionLayer(const char* name = nullptr) = 0;
314
telsoa01c577f2c2018-08-31 09:22:23 +0100315 /// Adds a multiplication layer to the network.
316 /// @param name - Optional name for the layer.
317 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000318 virtual IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr) = 0;
319
telsoa01c577f2c2018-08-31 09:22:23 +0100320 /// Adds a batch normalization layer to the network.
321 /// @param mean - Pre-calculated mean for each channel.
322 /// @param variance - Pre-calculated variance for each channel.
323 /// @param beta - Per-channel additive factor.
324 /// @param gamma - Per-channel multiplicative factor.
325 /// @return - Interface for configuring the layer.
326 /// @param name - Optional name for the layer.
telsoa014fcda012018-03-09 14:13:49 +0000327 virtual IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
328 const ConstTensor& mean,
329 const ConstTensor& variance,
330 const ConstTensor& beta,
331 const ConstTensor& gamma,
332 const char* name = nullptr) = 0;
333
telsoa01c577f2c2018-08-31 09:22:23 +0100334 /// Adds a resize bilinear layer to the network.
335 /// @param resizeDesc - Parameters for the resize operation.
336 /// @param name - Optional name for the layer.
337 /// @return - Interface for configuring the layer.
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +0100338 ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead")
telsoa014fcda012018-03-09 14:13:49 +0000339 virtual IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
340 const char* name = nullptr) = 0;
341
Teresa Charlina9075df2019-06-27 15:41:57 +0100342 /// Adds a resize layer to the network.
343 /// @param resizeDescriptor - Parameters for the resize operation.
344 /// @param name - Optional name for the layer.
345 /// @return - Interface for configuring the layer.
346 virtual IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
347 const char* name = nullptr) = 0;
348
Kevin Mayce5045a2019-10-02 14:07:47 +0100349 /// Adds an instance normalization layer to the network.
350 /// @param desc - Parameters for the instance normalization operation.
351 /// @param name - Optional name for the layer.
352 /// @return - Interface for configuring the layer.
353 virtual IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
354 const char* name = nullptr) = 0;
355
telsoa01c577f2c2018-08-31 09:22:23 +0100356 /// Adds an L2 normalization layer to the network.
telsoa014fcda012018-03-09 14:13:49 +0000357 /// Normalization is performed along dimension 1, but requires a 4d input.
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100358 /// @param desc - Parameters for the L2 normalization operation.
telsoa01c577f2c2018-08-31 09:22:23 +0100359 /// @param name - Optional name for the layer.
360 /// @return - Interface for configuring the layer.
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100361 virtual IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
362 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000363
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +0100364 /// Adds a log softmax layer to the network.
365 /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax.
366 /// @param name - Optional name for the layer.
367 /// @return - Interface for configuring the layer.
368 virtual IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
369 const char* name = nullptr) = 0;
370
telsoa014fcda012018-03-09 14:13:49 +0000371 /// Adds a layer with no inputs and a single output, which always corresponds to
372 /// the passed in constant tensor.
telsoa01c577f2c2018-08-31 09:22:23 +0100373 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
374 /// its own copy of the tensor data, meaning the memory referenced by @a input can
375 /// be freed or reused after this function is called.
376 /// @param name - Optional name for the layer.
377 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000378 virtual IConnectableLayer* AddConstantLayer(const ConstTensor& input,
Matteo Martincigh5b2159e2019-02-11 13:24:38 +0000379 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000380
telsoa01c577f2c2018-08-31 09:22:23 +0100381 /// Adds a reshape layer to the network.
382 /// @param reshapeDescriptor - Parameters for the reshape operation.
383 /// @param name - Optional name for the layer.
384 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000385 virtual IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
386 const char* name = nullptr) = 0;
387
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000388 /// Adds a space to batch layer to the network.
389 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
390 /// @param name - Optional name for the layer.
391 /// @return - Interface for configuring the layer.
392 virtual IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
393 const char* name = nullptr) = 0;
394
Aron Virginas-Tar972af152019-06-11 14:14:03 +0100395 /// Adds a space to depth layer to the network.
396 /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
397 /// @param name - Optional name for the layer.
398 /// @return - Interface for configuring the layer.
399 virtual IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
400 const char* name = nullptr) = 0;
401
telsoa01c577f2c2018-08-31 09:22:23 +0100402 /// Adds a floor layer to the network.
403 /// @param name - Optional name for the layer.
404 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000405 virtual IConnectableLayer* AddFloorLayer(const char* name = nullptr) = 0;
406
telsoa01c577f2c2018-08-31 09:22:23 +0100407 /// Adds an output layer to the network.
408 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
telsoa014fcda012018-03-09 14:13:49 +0000409 /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
telsoa01c577f2c2018-08-31 09:22:23 +0100410 /// @param name - Optional name for the layer.
411 /// @return - Interface for configuring the layer.
412 virtual IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr) = 0;
413
414 /// Add a Lstm layer to the network
James Conroyee18dc82019-07-17 11:27:46 +0100415 /// @param descriptor - Parameters for the Lstm operation
416 /// @param params - Weights and biases for the LSTM cell
417 /// @param name - Optional name for the layer
418 /// @return - Interface for configuring the layer.
telsoa01c577f2c2018-08-31 09:22:23 +0100419 virtual IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
420 const LstmInputParams& params,
421 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000422
Francis Murtaghe7a86a42018-08-29 12:42:10 +0100423 /// Adds a division layer to the network.
424 /// @param name - Optional name for the layer.
425 /// @return - Interface for configuring the layer.
426 virtual IConnectableLayer* AddDivisionLayer(const char* name = nullptr) = 0;
427
David Beck19526222018-09-12 16:00:08 +0100428 /// Adds a subtraction layer to the network.
429 /// @param name - Optional name for the layer.
430 /// @return - Interface for configuring the layer.
431 virtual IConnectableLayer* AddSubtractionLayer(const char* name = nullptr) = 0;
432
Nattapat Chaimanowong5a4304a2018-11-28 10:44:37 +0000433 /// Add a Maximum layer to the network.
434 /// @param name - Optional name for the layer.
435 /// @ return - Interface for configuring the layer.
436 virtual IConnectableLayer* AddMaximumLayer(const char* name = nullptr) = 0;
437
narpra0132b90462018-09-13 11:07:48 +0100438 /// Add a Mean layer to the network.
439 /// @param meanDescriptor - Parameters for the mean operation.
440 /// @param name - Optional name for the layer.
441 /// @ return - Interface for configuring the layer.
442 virtual IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr) = 0;
443
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +0100444 /// Adds a fully pad layer to the network.
445 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
446 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
447 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
448 /// @param name - Optional name for the layer.
449 /// @return - Interface for configuring the layer.
450 virtual IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
451 const char* name = nullptr) = 0;
452
Derek Lambertia9cca6a2019-03-25 15:41:58 +0000453 /// Add a quantize layer to the network
454 ///@param name - Optional name for the layer.
455 /// @return - Interface for configuring the layer.
456 virtual IConnectableLayer* AddQuantizeLayer(const char* name = nullptr) = 0;
457
Conor Kennedy430b5d82018-11-14 15:28:28 +0000458 /// Adds a strided slice layer to the network.
459 /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
460 /// @param name - Optional name for the layer.
461 /// @return - Interface for configuring the layer.
462 virtual IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
463 const char* name = nullptr) = 0;
464
kevmay0190539692018-11-29 08:40:19 +0000465 /// Add a Minimum layer to the network.
466 /// @param name - Optional name for the layer.
467 /// @ return - Interface for configuring the layer.
468 virtual IConnectableLayer* AddMinimumLayer(const char* name = nullptr) = 0;
469
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000470 /// Add a Greater layer to the network.
471 /// @param name - Optional name for the layer.
472 /// @ return - Interface for configuring the layer.
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100473 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000474 virtual IConnectableLayer* AddGreaterLayer(const char* name = nullptr) = 0;
475
FrancisMurtagh20995952018-12-17 12:11:36 +0000476 /// Add a Equal layer to the network.
477 /// @param name - Optional name for the layer.
478 /// @ return - Interface for configuring the layer.
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100479 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead")
FrancisMurtagh20995952018-12-17 12:11:36 +0000480 virtual IConnectableLayer* AddEqualLayer(const char* name = nullptr) = 0;
481
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +0000482 /// Add Reciprocal of square root layer to the network.
483 /// @param name - Optional name for the layer.
484 /// @ return - Interface for configuring the layer.
josh minor4a3c6102020-01-06 16:40:46 -0600485 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead")
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +0000486 virtual IConnectableLayer* AddRsqrtLayer(const char* name = nullptr) = 0;
487
narpra01b89b05f2019-01-16 09:53:09 +0000488 /// Add Gather layer to the network.
489 /// @param name - Optional name for the layer.
490 /// @ return - Interface for configuring the layer.
491 virtual IConnectableLayer* AddGatherLayer(const char* name = nullptr) = 0;
492
Sadik Armaganeff363d2019-04-05 15:25:46 +0100493 /// Adds a switch layer to the network.
494 /// @param name - Optional name for the layer.
495 /// @return - Interface for configuring the layer.
496 virtual IConnectableLayer* AddSwitchLayer(const char* name = nullptr) = 0;
497
Matteo Martincigh0e406ee2019-06-12 15:42:18 +0100498 /// Adds a PReLU layer to the network.
499 /// @param name - Optional name for the layer.
500 /// @return - Interface for configuring the layer.
501 virtual IConnectableLayer* AddPreluLayer(const char* name = nullptr) = 0;
502
Aron Virginas-Tar639fb042019-06-20 14:28:19 +0100503 /// Adds a 2D transpose convolution layer to the network.
504 /// @param descriptor - Description of the 2D transpose convolution layer.
505 /// @param weights - Tensor for the weights data.
506 /// @param biases - Optional tensor for the bias data.
507 /// @param name - Optional name for the layer.
508 /// @return - Interface for configuring the layer.
509 virtual IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
510 const ConstTensor& weights,
511 const Optional<ConstTensor>& biases,
512 const char* name = nullptr) = 0;
513
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000514 /// Adds a transpose layer to the network.
515 /// @param transposeDescriptor - TransposeDescriptor to configure the transpose.
516 /// @param name - Optional name for the layer.
517 /// @return - Interface for configuring the layer.
518 virtual IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
519 const char* name = nullptr) = 0;
520
Matthew Jackson2b8c1da2019-07-04 14:59:16 +0100521 /// Adds a stack layer to the network.
522 /// @param descriptor - Description of the stack layer.
523 /// @param name - Optional name for the layer.
524 /// @return - Interface for configuring the layer.
525 virtual IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
526 const char* name = nullptr) = 0;
527
Derek Lamberti013c3902019-10-21 10:46:16 +0100528 /// Add a stand-in layer for a type unknown to the Arm NN framework.
529 /// Note: Due to the nature of this layer, no validation can be performed by the framework.
530 /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the
531 /// tensor sizes cannot be inferred.
532 /// @descriptor - Descriptor for the StandIn layer.
533 /// @return - Interface for configuring the layer.
534 virtual IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
535 const char* name = nullptr) = 0;
536
James Conroyee18dc82019-07-17 11:27:46 +0100537 /// Add a QuantizedLstm layer to the network
538 /// @param params - The weights and biases for the Quantized LSTM cell
539 /// @param name - Optional name for the layer
540 /// @return - Interface for configuring the layer.
541 virtual IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
542 const char* name = nullptr) = 0;
543
Mike Kelly8c1701a2019-02-11 17:01:27 +0000544 virtual void Accept(ILayerVisitor& visitor) const = 0;
545
telsoa014fcda012018-03-09 14:13:49 +0000546protected:
547 ~INetwork() {}
548};
549
550using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
551
552class IOptimizedNetwork
553{
554public:
555 static void Destroy(IOptimizedNetwork* network);
556
557 virtual Status PrintGraph() = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100558 virtual Status SerializeToDot(std::ostream& stream) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000559
Jan Eilers99d9d4a2019-11-06 10:02:16 +0000560 virtual profiling::ProfilingGuid GetGuid() const = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100561
telsoa014fcda012018-03-09 14:13:49 +0000562protected:
563 ~IOptimizedNetwork() {}
564};
565
telsoa01c577f2c2018-08-31 09:22:23 +0100566struct OptimizerOptions
567{
Matteo Martincigh49124022019-01-11 13:25:59 +0000568 OptimizerOptions()
569 : m_ReduceFp32ToFp16(false)
570 , m_Debug(false)
keidav01738c2e62018-12-11 16:14:20 +0000571 {}
telsoa01c577f2c2018-08-31 09:22:23 +0100572
keidav01738c2e62018-12-11 16:14:20 +0000573 OptimizerOptions(bool reduceFp32ToFp16, bool debug)
telsoa01c577f2c2018-08-31 09:22:23 +0100574 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
keidav01738c2e62018-12-11 16:14:20 +0000575 , m_Debug(debug)
Matteo Martincigh49124022019-01-11 13:25:59 +0000576 {}
telsoa01c577f2c2018-08-31 09:22:23 +0100577
578 // Reduce Fp32 data to Fp16 for faster processing
579 bool m_ReduceFp32ToFp16;
keidav01738c2e62018-12-11 16:14:20 +0000580
581 // Add debug data for easier troubleshooting
582 bool m_Debug;
telsoa01c577f2c2018-08-31 09:22:23 +0100583};
telsoa014fcda012018-03-09 14:13:49 +0000584
585/// Create an optimized version of the network
586/// @param network INetwork description of the network to be optimized.
telsoa01c577f2c2018-08-31 09:22:23 +0100587/// @param backendPreferences The choice of the backend ordered by user preferences.
588/// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
Rob Hughes23214432019-11-05 11:27:36 +0000589/// @param messages If there are failures or warnings a string describing same will be added to the vector
telsoa01c577f2c2018-08-31 09:22:23 +0100590/// @param options OptimizerOptions object with optimizer configuration options
telsoa014fcda012018-03-09 14:13:49 +0000591/// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
592/// armnn::Exception if process fails.
telsoa014fcda012018-03-09 14:13:49 +0000593
telsoa01c577f2c2018-08-31 09:22:23 +0100594IOptimizedNetworkPtr Optimize(const INetwork& network,
David Beckf0b48452018-10-19 15:20:56 +0100595 const std::vector<BackendId>& backendPreferences,
telsoa01c577f2c2018-08-31 09:22:23 +0100596 const IDeviceSpec& deviceSpec,
jimfly016b0b53d2018-10-08 14:43:01 +0100597 const OptimizerOptions& options = OptimizerOptions(),
Rob Hughes23214432019-11-05 11:27:36 +0000598 Optional<std::vector<std::string>&> messages = EmptyOptional());
telsoa014fcda012018-03-09 14:13:49 +0000599} //namespace armnn