blob: f61a1fdca81d4c374777be55e17d7da355d8f464 [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
15namespace armnn
16{
17
Matthew Jacksondba634f2019-08-15 15:14:18 +010018constexpr unsigned int MaxNumOfTensorDimensions = 5U;
telsoa014fcda012018-03-09 14:13:49 +000019
Ryan OShea2bbfaa72020-02-12 16:15:27 +000020/// The lowest performance data capture interval we support is 10 miliseconds.
Colm Donelan02705242019-11-14 14:19:07 +000021constexpr unsigned int LOWEST_CAPTURE_PERIOD = 10000u;
22
Keith Davise813d672021-04-22 10:10:34 +010023/// Variable to control expire rate of priority queue
24constexpr unsigned int EXPIRE_RATE = 3U;
25
telsoa014fcda012018-03-09 14:13:49 +000026/// @enum Status enumeration
27/// @var Status::Successful
28/// @var Status::Failure
29enum class Status
30{
31 Success = 0,
32 Failure = 1
33};
34
35enum class DataType
36{
Keith Davise813d672021-04-22 10:10:34 +010037 Float16 = 0,
38 Float32 = 1,
Derek Lambertif90c56d2020-01-10 17:14:08 +000039 QAsymmU8 = 2,
ruoyan0120e984f2018-12-12 18:11:25 +000040 Signed32 = 3,
Keith Davise813d672021-04-22 10:10:34 +010041 Boolean = 4,
Derek Lambertif90c56d2020-01-10 17:14:08 +000042 QSymmS16 = 5,
Jan Eilers1b2654f2021-09-24 15:45:46 +010043 QSymmS8 = 6,
44 QAsymmS8 = 7,
45 BFloat16 = 8,
46 Signed64 = 9,
telsoa014fcda012018-03-09 14:13:49 +000047};
48
Derek Lamberti0cff1632018-09-18 16:02:25 +010049enum class DataLayout
50{
51 NCHW = 1,
Matthew Sloyanb63a3112021-09-08 13:05:51 +010052 NHWC = 2,
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +010053 NDHWC = 3,
54 NCDHW = 4
Derek Lamberti0cff1632018-09-18 16:02:25 +010055};
56
Keith Davis4914d0c2021-08-18 17:14:05 +010057/// Define the behaviour of the internal profiler when outputting network details
58enum class ProfilingDetailsMethod
59{
60 Undefined = 0,
61 DetailsWithEvents = 1,
62 DetailsOnly = 2
63};
64
65
Keith Davise813d672021-04-22 10:10:34 +010066enum class QosExecPriority
67{
68 Low = 0,
69 Medium = 1,
70 High = 2
71};
72
telsoa014fcda012018-03-09 14:13:49 +000073enum class ActivationFunction
74{
75 Sigmoid = 0,
76 TanH = 1,
77 Linear = 2,
78 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000079 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000080 SoftReLu = 5,
81 LeakyReLu = 6,
82 Abs = 7,
83 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000084 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000085 Elu = 10,
86 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +000087};
88
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010089enum class ArgMinMaxFunction
90{
91 Min = 0,
92 Max = 1
93};
94
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010095enum class ComparisonOperation
96{
97 Equal = 0,
98 Greater = 1,
99 GreaterOrEqual = 2,
100 Less = 3,
101 LessOrEqual = 4,
102 NotEqual = 5
103};
104
James Conroyaba90cd2020-11-06 16:28:18 +0000105enum class LogicalBinaryOperation
106{
107 LogicalAnd = 0,
108 LogicalOr = 1
109};
110
josh minor4a3c6102020-01-06 16:40:46 -0600111enum class UnaryOperation
112{
James Conroyaba90cd2020-11-06 16:28:18 +0000113 Abs = 0,
114 Exp = 1,
115 Sqrt = 2,
116 Rsqrt = 3,
117 Neg = 4,
Teresa Charlin50de4fa2021-05-31 18:47:33 +0100118 LogicalNot = 5,
119 Log = 6,
120 Sin = 7
josh minor4a3c6102020-01-06 16:40:46 -0600121};
122
telsoa014fcda012018-03-09 14:13:49 +0000123enum class PoolingAlgorithm
124{
125 Max = 0,
126 Average = 1,
127 L2 = 2
128};
129
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000130enum class ReduceOperation
131{
132 Sum = 0,
133 Max = 1,
134 Mean = 2,
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100135 Min = 3,
136 Prod = 4
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000137};
138
Teresa Charlina9075df2019-06-27 15:41:57 +0100139enum class ResizeMethod
140{
141 Bilinear = 0,
142 NearestNeighbor = 1
143};
144
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100145enum class Dimensionality
146{
147 NotSpecified = 0,
148 Specified = 1,
149 Scalar = 2
150};
151
telsoa014fcda012018-03-09 14:13:49 +0000152///
153/// The padding method modifies the output of pooling layers.
154/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100155/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000156/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100157/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000158/// fields in the divisor of Average and L2 pooling, while
159/// Exclude does not.
160///
161enum class PaddingMethod
162{
telsoa01c577f2c2018-08-31 09:22:23 +0100163 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100164 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100165 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100166 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000167};
168
Matthew Sloyan2e5d0b22021-10-21 14:05:31 +0100169///
170/// The padding mode controls whether the padding should be filled with constant values (Constant), or
171/// reflect the input, either including the border values (Symmetric) or not (Reflect).
172///
173enum class PaddingMode
174{
175 Constant = 0,
176 Reflect = 1,
177 Symmetric = 2
178};
179
telsoa014fcda012018-03-09 14:13:49 +0000180enum class NormalizationAlgorithmChannel
181{
182 Across = 0,
183 Within = 1
184};
185
186enum class NormalizationAlgorithmMethod
187{
David Beckdcb751f2018-10-03 11:42:42 +0100188 /// Krichevsky 2012: Local Brightness Normalization
189 LocalBrightness = 0,
190 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100191 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000192};
193
194enum class OutputShapeRounding
195{
196 Floor = 0,
197 Ceiling = 1
198};
199
Teresa Charlincdc01492020-06-09 18:00:20 +0100200///
201/// The ShapeInferenceMethod modify how the output shapes are treated.
202/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
203/// and any mismatch is reported.
Jan Eilersb1c62f12021-10-26 14:56:47 +0100204/// 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 +0100205/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
206/// specified explicitly, however this information can be calculated from the inputs.
207///
208enum class ShapeInferenceMethod
209{
210 /// Validate all output shapes
211 ValidateOnly = 0,
212 /// Infer missing output shapes and validate all output shapes
213 InferAndValidate = 1
214};
215
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100216/// Define the Memory Source to reduce copies
217enum class MemorySource : uint32_t
218{
219 Undefined = 0,
220 Malloc = 1,
221 DmaBuf = 2,
222 DmaBufProtected = 4
223};
224
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100225enum class MemBlockStrategyType
226{
Francis Murtaghca49a242021-09-28 15:30:31 +0100227 // MemBlocks can be packed on the Y axis only, overlap allowed on X axis.
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100228 // In other words MemBlocks with overlapping lifetimes cannot use the same MemBin,
229 // equivalent to blob or pooling memory management.
230 SingleAxisPacking = 0,
231
Francis Murtaghca49a242021-09-28 15:30:31 +0100232 // MemBlocks can be packed on either Y or X axis but cannot overlap on both.
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100233 // In other words MemBlocks with overlapping lifetimes can use the same MemBin,
234 // equivalent to offset or slab memory management.
235 MultiAxisPacking = 1
236};
237
David Beck9efb57d2018-11-05 13:40:33 +0000238/// Each backend should implement an IBackend.
239class IBackend
240{
241protected:
242 IBackend() {}
243 virtual ~IBackend() {}
244
245public:
246 virtual const BackendId& GetId() const = 0;
247};
248
249using IBackendSharedPtr = std::shared_ptr<IBackend>;
250using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
251
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000252/// BackendCapability class
253enum class BackendCapability : uint32_t
254{
255 /// Constant weights can be accessed through the descriptors,
256 /// On the other hand, non-const weights can be accessed through inputs.
257 NonConstWeights,
258
Sadik Armaganaede8ca2021-03-31 16:12:13 +0100259 /// Asynchronous Execution.
260 AsyncExecution,
261
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000262 // add new enum values here
263};
264
David Beckdcb751f2018-10-03 11:42:42 +0100265/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100266class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000267{
telsoa01c577f2c2018-08-31 09:22:23 +0100268protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000269 IDeviceSpec() {}
270 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100271public:
272 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000273};
274
275/// Type of identifiers for bindable layers (inputs, outputs).
276using LayerBindingId = int;
Finn Williamsf37b9702021-09-01 18:06:04 +0100277using ImportedInputId = unsigned int;
Finn Williams8636bc72021-10-02 15:06:39 +0100278using ImportedOutputId = unsigned int;
279
telsoa014fcda012018-03-09 14:13:49 +0000280
281class PermutationVector
282{
283public:
284 using ValueType = unsigned int;
285 using SizeType = unsigned int;
286 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
287 using ConstIterator = typename ArrayType::const_iterator;
288
telsoa01c577f2c2018-08-31 09:22:23 +0100289 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000290 /// when source and target potentially have different memory layouts.
291 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100292 /// 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 +0000293 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
294 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
295 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
296 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
297 /// [ 0, 2, 3, 1 ].
298 ///
299 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
300 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
301 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
302 /// [ 0, 3, 1, 2 ].
303 ///
304 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
305
306 PermutationVector(std::initializer_list<ValueType> dimMappings);
307
308 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
309
310 SizeType GetSize() const { return m_NumDimMappings; }
311
312 ConstIterator begin() const { return m_DimMappings.begin(); }
Colm Donelan41e764c2021-05-27 16:43:25 +0100313 /**
314 *
315 * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
316 */
317 ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
telsoa014fcda012018-03-09 14:13:49 +0000318
319 bool IsEqual(const PermutationVector& other) const
320 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100321 if (m_NumDimMappings != other.m_NumDimMappings) return false;
322 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
323 {
324 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
325 }
326 return true;
telsoa014fcda012018-03-09 14:13:49 +0000327 }
328
329 bool IsInverse(const PermutationVector& other) const
330 {
331 bool isInverse = (GetSize() == other.GetSize());
332 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
333 {
334 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
335 }
336 return isInverse;
337 }
338
339private:
340 ArrayType m_DimMappings;
341 /// Number of valid entries in @ref m_DimMappings
342 SizeType m_NumDimMappings;
343};
344
janeil013fec1ea2019-11-07 09:47:20 +0000345namespace profiling { class ProfilingGuid; }
346
telsoa01c577f2c2018-08-31 09:22:23 +0100347/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000348using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100349
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000350class ITensorHandle;
351
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000352/// Define the type of callback for the Debug layer to call
353/// @param guid - guid of layer connected to the input of the Debug layer
354/// @param slotIndex - index of the output slot connected to the input of the Debug layer
355/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
356using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000357
Keith Davise813d672021-04-22 10:10:34 +0100358/// Define a timer and associated inference ID for recording execution times
359using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
360using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
janeil01c4946c72019-11-07 09:32:28 +0000361
janeil01c4946c72019-11-07 09:32:28 +0000362
Finn Williamsb454c5c2021-02-09 15:56:23 +0000363/// This list uses X macro technique.
364/// See https://en.wikipedia.org/wiki/X_Macro for more info
365#define LIST_OF_LAYER_TYPE \
366 X(Activation) \
367 X(Addition) \
368 X(ArgMinMax) \
369 X(BatchNormalization) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100370 X(BatchToSpaceNd) \
Finn Williamsb454c5c2021-02-09 15:56:23 +0000371 X(Comparison) \
372 X(Concat) \
373 X(Constant) \
374 X(ConvertBf16ToFp32) \
375 X(ConvertFp16ToFp32) \
376 X(ConvertFp32ToBf16) \
377 X(ConvertFp32ToFp16) \
378 X(Convolution2d) \
379 X(Debug) \
380 X(DepthToSpace) \
381 X(DepthwiseConvolution2d) \
382 X(Dequantize) \
383 X(DetectionPostProcess) \
384 X(Division) \
385 X(ElementwiseUnary) \
386 X(FakeQuantization) \
387 X(Fill) \
388 X(Floor) \
389 X(FullyConnected) \
390 X(Gather) \
391 X(Input) \
392 X(InstanceNormalization) \
393 X(L2Normalization) \
394 X(LogicalBinary) \
395 X(LogSoftmax) \
396 X(Lstm) \
397 X(QLstm) \
398 X(Map) \
399 X(Maximum) \
400 X(Mean) \
401 X(MemCopy) \
402 X(MemImport) \
403 X(Merge) \
404 X(Minimum) \
405 X(Multiplication) \
406 X(Normalization) \
407 X(Output) \
408 X(Pad) \
409 X(Permute) \
410 X(Pooling2d) \
411 X(PreCompiled) \
412 X(Prelu) \
413 X(Quantize) \
414 X(QuantizedLstm) \
415 X(Reshape) \
416 X(Rank) \
417 X(Resize) \
418 X(Reduce) \
419 X(Slice) \
420 X(Softmax) \
421 X(SpaceToBatchNd) \
422 X(SpaceToDepth) \
423 X(Splitter) \
424 X(Stack) \
425 X(StandIn) \
426 X(StridedSlice) \
427 X(Subtraction) \
428 X(Switch) \
429 X(Transpose) \
430 X(TransposeConvolution2d) \
mathad01b392e982021-04-07 12:07:30 +0100431 X(Unmap) \
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100432 X(Cast) \
433 X(Shape) \
434 X(UnidirectionalSequenceLstm) \
Simon Obute51f67772021-09-03 15:50:13 +0100435 X(ChannelShuffle) \
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100436 X(Convolution3d) \
437
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100438// New layers should be added at last to minimize instability.
Keith Davis3ae3f972021-05-21 16:33:48 +0100439
Finn Williamsb454c5c2021-02-09 15:56:23 +0000440/// When adding a new layer, adapt also the LastLayer enum value in the
441/// enum class LayerType below
442enum class LayerType
443{
444#define X(name) name,
445 LIST_OF_LAYER_TYPE
446#undef X
447 FirstLayer = Activation,
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100448 LastLayer = UnidirectionalSequenceLstm
Finn Williamsb454c5c2021-02-09 15:56:23 +0000449};
450
451const char* GetLayerTypeAsCString(LayerType type);
452
David Beck9df2d952018-10-10 15:11:44 +0100453} // namespace armnn