blob: a804f55468a450b09906788215d8c9ea37a20092 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Teresa Charlin50de4fa2021-05-31 18:47:33 +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
7#include <array>
Matthew Bentham47bfac42019-03-25 12:30:56 +00008#include <functional>
janeil01c4946c72019-11-07 09:32:28 +00009#include <stdint.h>
Keith Davise813d672021-04-22 10:10:34 +010010#include <chrono>
Jim Flynn44db7c32019-03-22 15:58:39 +000011#include "BackendId.hpp"
12#include "Exceptions.hpp"
Derek Lambertif90c56d2020-01-10 17:14:08 +000013#include "Deprecated.hpp"
telsoa014fcda012018-03-09 14:13:49 +000014
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000015namespace arm
16{
17namespace pipe
18{
19
20class ProfilingGuid;
21
22} // namespace armn
23} // namespace pipe
24
25/// Define LayerGuid type.
26using LayerGuid = arm::pipe::ProfilingGuid;
27
telsoa014fcda012018-03-09 14:13:49 +000028namespace armnn
29{
30
Matthew Jacksondba634f2019-08-15 15:14:18 +010031constexpr unsigned int MaxNumOfTensorDimensions = 5U;
telsoa014fcda012018-03-09 14:13:49 +000032
Ryan OShea2bbfaa72020-02-12 16:15:27 +000033/// The lowest performance data capture interval we support is 10 miliseconds.
Colm Donelan02705242019-11-14 14:19:07 +000034constexpr unsigned int LOWEST_CAPTURE_PERIOD = 10000u;
35
Keith Davise813d672021-04-22 10:10:34 +010036/// Variable to control expire rate of priority queue
37constexpr unsigned int EXPIRE_RATE = 3U;
38
telsoa014fcda012018-03-09 14:13:49 +000039/// @enum Status enumeration
40/// @var Status::Successful
41/// @var Status::Failure
42enum class Status
43{
44 Success = 0,
45 Failure = 1
46};
47
48enum class DataType
49{
Keith Davise813d672021-04-22 10:10:34 +010050 Float16 = 0,
51 Float32 = 1,
Derek Lambertif90c56d2020-01-10 17:14:08 +000052 QAsymmU8 = 2,
ruoyan0120e984f2018-12-12 18:11:25 +000053 Signed32 = 3,
Keith Davise813d672021-04-22 10:10:34 +010054 Boolean = 4,
Derek Lambertif90c56d2020-01-10 17:14:08 +000055 QSymmS16 = 5,
Jan Eilers1b2654f2021-09-24 15:45:46 +010056 QSymmS8 = 6,
57 QAsymmS8 = 7,
58 BFloat16 = 8,
59 Signed64 = 9,
telsoa014fcda012018-03-09 14:13:49 +000060};
61
Derek Lamberti0cff1632018-09-18 16:02:25 +010062enum class DataLayout
63{
64 NCHW = 1,
Matthew Sloyanb63a3112021-09-08 13:05:51 +010065 NHWC = 2,
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +010066 NDHWC = 3,
67 NCDHW = 4
Derek Lamberti0cff1632018-09-18 16:02:25 +010068};
69
Keith Davis4914d0c2021-08-18 17:14:05 +010070/// Define the behaviour of the internal profiler when outputting network details
71enum class ProfilingDetailsMethod
72{
73 Undefined = 0,
74 DetailsWithEvents = 1,
75 DetailsOnly = 2
76};
77
78
Keith Davise813d672021-04-22 10:10:34 +010079enum class QosExecPriority
80{
81 Low = 0,
82 Medium = 1,
83 High = 2
84};
85
telsoa014fcda012018-03-09 14:13:49 +000086enum class ActivationFunction
87{
88 Sigmoid = 0,
89 TanH = 1,
90 Linear = 2,
91 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000092 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000093 SoftReLu = 5,
94 LeakyReLu = 6,
95 Abs = 7,
96 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000097 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000098 Elu = 10,
99 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +0000100};
101
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +0100102enum class ArgMinMaxFunction
103{
104 Min = 0,
105 Max = 1
106};
107
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100108enum class ComparisonOperation
109{
110 Equal = 0,
111 Greater = 1,
112 GreaterOrEqual = 2,
113 Less = 3,
114 LessOrEqual = 4,
115 NotEqual = 5
116};
117
James Conroyaba90cd2020-11-06 16:28:18 +0000118enum class LogicalBinaryOperation
119{
120 LogicalAnd = 0,
121 LogicalOr = 1
122};
123
josh minor4a3c6102020-01-06 16:40:46 -0600124enum class UnaryOperation
125{
James Conroyaba90cd2020-11-06 16:28:18 +0000126 Abs = 0,
127 Exp = 1,
128 Sqrt = 2,
129 Rsqrt = 3,
130 Neg = 4,
Teresa Charlin50de4fa2021-05-31 18:47:33 +0100131 LogicalNot = 5,
132 Log = 6,
133 Sin = 7
josh minor4a3c6102020-01-06 16:40:46 -0600134};
135
telsoa014fcda012018-03-09 14:13:49 +0000136enum class PoolingAlgorithm
137{
138 Max = 0,
139 Average = 1,
140 L2 = 2
141};
142
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000143enum class ReduceOperation
144{
145 Sum = 0,
146 Max = 1,
147 Mean = 2,
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100148 Min = 3,
149 Prod = 4
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000150};
151
Teresa Charlina9075df2019-06-27 15:41:57 +0100152enum class ResizeMethod
153{
154 Bilinear = 0,
155 NearestNeighbor = 1
156};
157
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100158enum class Dimensionality
159{
160 NotSpecified = 0,
161 Specified = 1,
162 Scalar = 2
163};
164
telsoa014fcda012018-03-09 14:13:49 +0000165///
166/// The padding method modifies the output of pooling layers.
167/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100168/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000169/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100170/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000171/// fields in the divisor of Average and L2 pooling, while
172/// Exclude does not.
173///
174enum class PaddingMethod
175{
telsoa01c577f2c2018-08-31 09:22:23 +0100176 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100177 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100178 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100179 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000180};
181
Matthew Sloyan2e5d0b22021-10-21 14:05:31 +0100182///
183/// The padding mode controls whether the padding should be filled with constant values (Constant), or
184/// reflect the input, either including the border values (Symmetric) or not (Reflect).
185///
186enum class PaddingMode
187{
188 Constant = 0,
189 Reflect = 1,
190 Symmetric = 2
191};
192
telsoa014fcda012018-03-09 14:13:49 +0000193enum class NormalizationAlgorithmChannel
194{
195 Across = 0,
196 Within = 1
197};
198
199enum class NormalizationAlgorithmMethod
200{
David Beckdcb751f2018-10-03 11:42:42 +0100201 /// Krichevsky 2012: Local Brightness Normalization
202 LocalBrightness = 0,
203 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100204 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000205};
206
207enum class OutputShapeRounding
208{
209 Floor = 0,
210 Ceiling = 1
211};
212
Teresa Charlincdc01492020-06-09 18:00:20 +0100213///
214/// The ShapeInferenceMethod modify how the output shapes are treated.
215/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
216/// and any mismatch is reported.
Jan Eilersb1c62f12021-10-26 14:56:47 +0100217/// When InferAndValidate is selected 2 actions are performed: (1)infer output shape from inputs and (2)validate the
Teresa Charlincdc01492020-06-09 18:00:20 +0100218/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
219/// specified explicitly, however this information can be calculated from the inputs.
220///
221enum class ShapeInferenceMethod
222{
223 /// Validate all output shapes
224 ValidateOnly = 0,
225 /// Infer missing output shapes and validate all output shapes
226 InferAndValidate = 1
227};
228
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100229/// Define the Memory Source to reduce copies
230enum class MemorySource : uint32_t
231{
232 Undefined = 0,
233 Malloc = 1,
234 DmaBuf = 2,
David Monahan6642b8a2021-11-04 16:31:46 +0000235 DmaBufProtected = 4,
236 Gralloc = 5
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100237};
238
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100239enum class MemBlockStrategyType
240{
Francis Murtaghca49a242021-09-28 15:30:31 +0100241 // MemBlocks can be packed on the Y axis only, overlap allowed on X axis.
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100242 // In other words MemBlocks with overlapping lifetimes cannot use the same MemBin,
243 // equivalent to blob or pooling memory management.
244 SingleAxisPacking = 0,
245
Francis Murtaghca49a242021-09-28 15:30:31 +0100246 // MemBlocks can be packed on either Y or X axis but cannot overlap on both.
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100247 // In other words MemBlocks with overlapping lifetimes can use the same MemBin,
248 // equivalent to offset or slab memory management.
249 MultiAxisPacking = 1
250};
251
David Beck9efb57d2018-11-05 13:40:33 +0000252/// Each backend should implement an IBackend.
253class IBackend
254{
255protected:
256 IBackend() {}
257 virtual ~IBackend() {}
258
259public:
260 virtual const BackendId& GetId() const = 0;
261};
262
263using IBackendSharedPtr = std::shared_ptr<IBackend>;
264using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
265
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000266/// BackendCapability class
267enum class BackendCapability : uint32_t
268{
269 /// Constant weights can be accessed through the descriptors,
270 /// On the other hand, non-const weights can be accessed through inputs.
271 NonConstWeights,
272
Sadik Armaganaede8ca2021-03-31 16:12:13 +0100273 /// Asynchronous Execution.
274 AsyncExecution,
275
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000276 // add new enum values here
277};
278
David Beckdcb751f2018-10-03 11:42:42 +0100279/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100280class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000281{
telsoa01c577f2c2018-08-31 09:22:23 +0100282protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000283 IDeviceSpec() {}
284 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100285public:
286 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000287};
288
289/// Type of identifiers for bindable layers (inputs, outputs).
290using LayerBindingId = int;
Finn Williamsf37b9702021-09-01 18:06:04 +0100291using ImportedInputId = unsigned int;
Finn Williams8636bc72021-10-02 15:06:39 +0100292using ImportedOutputId = unsigned int;
293
telsoa014fcda012018-03-09 14:13:49 +0000294
295class PermutationVector
296{
297public:
298 using ValueType = unsigned int;
299 using SizeType = unsigned int;
300 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
301 using ConstIterator = typename ArrayType::const_iterator;
302
telsoa01c577f2c2018-08-31 09:22:23 +0100303 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000304 /// when source and target potentially have different memory layouts.
305 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100306 /// E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels),
telsoa014fcda012018-03-09 14:13:49 +0000307 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
308 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
309 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
310 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
311 /// [ 0, 2, 3, 1 ].
312 ///
313 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
314 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
315 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
316 /// [ 0, 3, 1, 2 ].
317 ///
318 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
319
320 PermutationVector(std::initializer_list<ValueType> dimMappings);
321
Cathal Corbettbddb2582021-11-23 11:49:23 +0000322 ///
323 /// Indexing method with out-of-bounds error checking for the m_DimMappings array.
324 /// @param i - integer value corresponding to index of m_DimMappings array to retrieve element from.
325 /// @return element at index i of m_DimMappings array.
326 /// @throws InvalidArgumentException when indexing out-of-bounds index of m_DimMappings array.
327 ///
328 ValueType operator[](SizeType i) const
329 {
330 if (i >= GetSize())
331 {
332 throw InvalidArgumentException("Invalid indexing of PermutationVector of size " + std::to_string(GetSize())
333 + " at location [" + std::to_string(i) + "].");
334 }
335 return m_DimMappings.at(i);
336 }
telsoa014fcda012018-03-09 14:13:49 +0000337
338 SizeType GetSize() const { return m_NumDimMappings; }
339
340 ConstIterator begin() const { return m_DimMappings.begin(); }
Colm Donelan41e764c2021-05-27 16:43:25 +0100341 /**
342 *
343 * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
344 */
345 ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
telsoa014fcda012018-03-09 14:13:49 +0000346
347 bool IsEqual(const PermutationVector& other) const
348 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100349 if (m_NumDimMappings != other.m_NumDimMappings) return false;
350 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
351 {
352 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
353 }
354 return true;
telsoa014fcda012018-03-09 14:13:49 +0000355 }
356
357 bool IsInverse(const PermutationVector& other) const
358 {
359 bool isInverse = (GetSize() == other.GetSize());
360 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
361 {
362 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
363 }
364 return isInverse;
365 }
366
367private:
368 ArrayType m_DimMappings;
369 /// Number of valid entries in @ref m_DimMappings
370 SizeType m_NumDimMappings;
371};
372
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000373class ITensorHandle;
374
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000375/// Define the type of callback for the Debug layer to call
376/// @param guid - guid of layer connected to the input of the Debug layer
377/// @param slotIndex - index of the output slot connected to the input of the Debug layer
378/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
379using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000380
Keith Davise813d672021-04-22 10:10:34 +0100381/// Define a timer and associated inference ID for recording execution times
382using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
383using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
janeil01c4946c72019-11-07 09:32:28 +0000384
janeil01c4946c72019-11-07 09:32:28 +0000385
Finn Williamsb454c5c2021-02-09 15:56:23 +0000386/// This list uses X macro technique.
387/// See https://en.wikipedia.org/wiki/X_Macro for more info
388#define LIST_OF_LAYER_TYPE \
389 X(Activation) \
390 X(Addition) \
391 X(ArgMinMax) \
392 X(BatchNormalization) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100393 X(BatchToSpaceNd) \
Finn Williamsb454c5c2021-02-09 15:56:23 +0000394 X(Comparison) \
395 X(Concat) \
396 X(Constant) \
397 X(ConvertBf16ToFp32) \
398 X(ConvertFp16ToFp32) \
399 X(ConvertFp32ToBf16) \
400 X(ConvertFp32ToFp16) \
401 X(Convolution2d) \
402 X(Debug) \
403 X(DepthToSpace) \
404 X(DepthwiseConvolution2d) \
405 X(Dequantize) \
406 X(DetectionPostProcess) \
407 X(Division) \
408 X(ElementwiseUnary) \
409 X(FakeQuantization) \
410 X(Fill) \
411 X(Floor) \
412 X(FullyConnected) \
413 X(Gather) \
414 X(Input) \
415 X(InstanceNormalization) \
416 X(L2Normalization) \
417 X(LogicalBinary) \
418 X(LogSoftmax) \
419 X(Lstm) \
420 X(QLstm) \
421 X(Map) \
422 X(Maximum) \
423 X(Mean) \
424 X(MemCopy) \
425 X(MemImport) \
426 X(Merge) \
427 X(Minimum) \
428 X(Multiplication) \
429 X(Normalization) \
430 X(Output) \
431 X(Pad) \
432 X(Permute) \
433 X(Pooling2d) \
434 X(PreCompiled) \
435 X(Prelu) \
436 X(Quantize) \
437 X(QuantizedLstm) \
438 X(Reshape) \
439 X(Rank) \
440 X(Resize) \
441 X(Reduce) \
442 X(Slice) \
443 X(Softmax) \
444 X(SpaceToBatchNd) \
445 X(SpaceToDepth) \
446 X(Splitter) \
447 X(Stack) \
448 X(StandIn) \
449 X(StridedSlice) \
450 X(Subtraction) \
451 X(Switch) \
452 X(Transpose) \
453 X(TransposeConvolution2d) \
mathad01b392e982021-04-07 12:07:30 +0100454 X(Unmap) \
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100455 X(Cast) \
456 X(Shape) \
457 X(UnidirectionalSequenceLstm) \
Simon Obute51f67772021-09-03 15:50:13 +0100458 X(ChannelShuffle) \
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100459 X(Convolution3d) \
Tamás Nyíri7b885b32021-10-26 14:47:57 +0100460 X(Pooling3d) \
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100461
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100462// New layers should be added at last to minimize instability.
Keith Davis3ae3f972021-05-21 16:33:48 +0100463
Finn Williamsb454c5c2021-02-09 15:56:23 +0000464/// When adding a new layer, adapt also the LastLayer enum value in the
465/// enum class LayerType below
466enum class LayerType
467{
468#define X(name) name,
469 LIST_OF_LAYER_TYPE
470#undef X
471 FirstLayer = Activation,
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100472 LastLayer = UnidirectionalSequenceLstm
Finn Williamsb454c5c2021-02-09 15:56:23 +0000473};
474
475const char* GetLayerTypeAsCString(LayerType type);
476
David Beck9df2d952018-10-10 15:11:44 +0100477} // namespace armnn