blob: 7f2e1921021a30f541b9cf4b3a5067803ca95469 [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,
53 NDHWC = 3
Derek Lamberti0cff1632018-09-18 16:02:25 +010054};
55
Keith Davis4914d0c2021-08-18 17:14:05 +010056/// Define the behaviour of the internal profiler when outputting network details
57enum class ProfilingDetailsMethod
58{
59 Undefined = 0,
60 DetailsWithEvents = 1,
61 DetailsOnly = 2
62};
63
64
Keith Davise813d672021-04-22 10:10:34 +010065enum class QosExecPriority
66{
67 Low = 0,
68 Medium = 1,
69 High = 2
70};
71
telsoa014fcda012018-03-09 14:13:49 +000072enum class ActivationFunction
73{
74 Sigmoid = 0,
75 TanH = 1,
76 Linear = 2,
77 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000078 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000079 SoftReLu = 5,
80 LeakyReLu = 6,
81 Abs = 7,
82 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000083 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000084 Elu = 10,
85 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +000086};
87
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010088enum class ArgMinMaxFunction
89{
90 Min = 0,
91 Max = 1
92};
93
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010094enum class ComparisonOperation
95{
96 Equal = 0,
97 Greater = 1,
98 GreaterOrEqual = 2,
99 Less = 3,
100 LessOrEqual = 4,
101 NotEqual = 5
102};
103
James Conroyaba90cd2020-11-06 16:28:18 +0000104enum class LogicalBinaryOperation
105{
106 LogicalAnd = 0,
107 LogicalOr = 1
108};
109
josh minor4a3c6102020-01-06 16:40:46 -0600110enum class UnaryOperation
111{
James Conroyaba90cd2020-11-06 16:28:18 +0000112 Abs = 0,
113 Exp = 1,
114 Sqrt = 2,
115 Rsqrt = 3,
116 Neg = 4,
Teresa Charlin50de4fa2021-05-31 18:47:33 +0100117 LogicalNot = 5,
118 Log = 6,
119 Sin = 7
josh minor4a3c6102020-01-06 16:40:46 -0600120};
121
telsoa014fcda012018-03-09 14:13:49 +0000122enum class PoolingAlgorithm
123{
124 Max = 0,
125 Average = 1,
126 L2 = 2
127};
128
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000129enum class ReduceOperation
130{
131 Sum = 0,
132 Max = 1,
133 Mean = 2,
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100134 Min = 3,
135 Prod = 4
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000136};
137
Teresa Charlina9075df2019-06-27 15:41:57 +0100138enum class ResizeMethod
139{
140 Bilinear = 0,
141 NearestNeighbor = 1
142};
143
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100144enum class Dimensionality
145{
146 NotSpecified = 0,
147 Specified = 1,
148 Scalar = 2
149};
150
telsoa014fcda012018-03-09 14:13:49 +0000151///
152/// The padding method modifies the output of pooling layers.
153/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100154/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000155/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100156/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000157/// fields in the divisor of Average and L2 pooling, while
158/// Exclude does not.
159///
160enum class PaddingMethod
161{
telsoa01c577f2c2018-08-31 09:22:23 +0100162 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100163 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100164 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100165 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000166};
167
168enum class NormalizationAlgorithmChannel
169{
170 Across = 0,
171 Within = 1
172};
173
174enum class NormalizationAlgorithmMethod
175{
David Beckdcb751f2018-10-03 11:42:42 +0100176 /// Krichevsky 2012: Local Brightness Normalization
177 LocalBrightness = 0,
178 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100179 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000180};
181
182enum class OutputShapeRounding
183{
184 Floor = 0,
185 Ceiling = 1
186};
187
Teresa Charlincdc01492020-06-09 18:00:20 +0100188///
189/// The ShapeInferenceMethod modify how the output shapes are treated.
190/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
191/// and any mismatch is reported.
192/// When InferAndValidate is selected 2 actions must be performed: (1)infer output shape from inputs and (2)validate the
193/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
194/// specified explicitly, however this information can be calculated from the inputs.
195///
196enum class ShapeInferenceMethod
197{
198 /// Validate all output shapes
199 ValidateOnly = 0,
200 /// Infer missing output shapes and validate all output shapes
201 InferAndValidate = 1
202};
203
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100204/// Define the Memory Source to reduce copies
205enum class MemorySource : uint32_t
206{
207 Undefined = 0,
208 Malloc = 1,
209 DmaBuf = 2,
210 DmaBufProtected = 4
211};
212
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100213enum class MemBlockStrategyType
214{
Francis Murtaghca49a242021-09-28 15:30:31 +0100215 // MemBlocks can be packed on the Y axis only, overlap allowed on X axis.
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100216 // In other words MemBlocks with overlapping lifetimes cannot use the same MemBin,
217 // equivalent to blob or pooling memory management.
218 SingleAxisPacking = 0,
219
Francis Murtaghca49a242021-09-28 15:30:31 +0100220 // MemBlocks can be packed on either Y or X axis but cannot overlap on both.
Sadik Armagan932cf3f2021-09-15 09:22:11 +0100221 // In other words MemBlocks with overlapping lifetimes can use the same MemBin,
222 // equivalent to offset or slab memory management.
223 MultiAxisPacking = 1
224};
225
David Beck9efb57d2018-11-05 13:40:33 +0000226/// Each backend should implement an IBackend.
227class IBackend
228{
229protected:
230 IBackend() {}
231 virtual ~IBackend() {}
232
233public:
234 virtual const BackendId& GetId() const = 0;
235};
236
237using IBackendSharedPtr = std::shared_ptr<IBackend>;
238using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
239
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000240/// BackendCapability class
241enum class BackendCapability : uint32_t
242{
243 /// Constant weights can be accessed through the descriptors,
244 /// On the other hand, non-const weights can be accessed through inputs.
245 NonConstWeights,
246
Sadik Armaganaede8ca2021-03-31 16:12:13 +0100247 /// Asynchronous Execution.
248 AsyncExecution,
249
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000250 // add new enum values here
251};
252
David Beckdcb751f2018-10-03 11:42:42 +0100253/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100254class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000255{
telsoa01c577f2c2018-08-31 09:22:23 +0100256protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000257 IDeviceSpec() {}
258 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100259public:
260 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000261};
262
263/// Type of identifiers for bindable layers (inputs, outputs).
264using LayerBindingId = int;
Finn Williamsf37b9702021-09-01 18:06:04 +0100265using ImportedInputId = unsigned int;
Finn Williams8636bc72021-10-02 15:06:39 +0100266using ImportedOutputId = unsigned int;
267
telsoa014fcda012018-03-09 14:13:49 +0000268
269class PermutationVector
270{
271public:
272 using ValueType = unsigned int;
273 using SizeType = unsigned int;
274 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
275 using ConstIterator = typename ArrayType::const_iterator;
276
telsoa01c577f2c2018-08-31 09:22:23 +0100277 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000278 /// when source and target potentially have different memory layouts.
279 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100280 /// 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 +0000281 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
282 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
283 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
284 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
285 /// [ 0, 2, 3, 1 ].
286 ///
287 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
288 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
289 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
290 /// [ 0, 3, 1, 2 ].
291 ///
292 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
293
294 PermutationVector(std::initializer_list<ValueType> dimMappings);
295
296 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
297
298 SizeType GetSize() const { return m_NumDimMappings; }
299
300 ConstIterator begin() const { return m_DimMappings.begin(); }
Colm Donelan41e764c2021-05-27 16:43:25 +0100301 /**
302 *
303 * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
304 */
305 ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
telsoa014fcda012018-03-09 14:13:49 +0000306
307 bool IsEqual(const PermutationVector& other) const
308 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100309 if (m_NumDimMappings != other.m_NumDimMappings) return false;
310 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
311 {
312 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
313 }
314 return true;
telsoa014fcda012018-03-09 14:13:49 +0000315 }
316
317 bool IsInverse(const PermutationVector& other) const
318 {
319 bool isInverse = (GetSize() == other.GetSize());
320 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
321 {
322 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
323 }
324 return isInverse;
325 }
326
327private:
328 ArrayType m_DimMappings;
329 /// Number of valid entries in @ref m_DimMappings
330 SizeType m_NumDimMappings;
331};
332
janeil013fec1ea2019-11-07 09:47:20 +0000333namespace profiling { class ProfilingGuid; }
334
telsoa01c577f2c2018-08-31 09:22:23 +0100335/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000336using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100337
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000338class ITensorHandle;
339
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000340/// Define the type of callback for the Debug layer to call
341/// @param guid - guid of layer connected to the input of the Debug layer
342/// @param slotIndex - index of the output slot connected to the input of the Debug layer
343/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
344using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000345
Keith Davise813d672021-04-22 10:10:34 +0100346/// Define a timer and associated inference ID for recording execution times
347using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
348using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
janeil01c4946c72019-11-07 09:32:28 +0000349
janeil01c4946c72019-11-07 09:32:28 +0000350
Finn Williamsb454c5c2021-02-09 15:56:23 +0000351/// This list uses X macro technique.
352/// See https://en.wikipedia.org/wiki/X_Macro for more info
353#define LIST_OF_LAYER_TYPE \
354 X(Activation) \
355 X(Addition) \
356 X(ArgMinMax) \
357 X(BatchNormalization) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100358 X(BatchToSpaceNd) \
Finn Williamsb454c5c2021-02-09 15:56:23 +0000359 X(Comparison) \
360 X(Concat) \
361 X(Constant) \
362 X(ConvertBf16ToFp32) \
363 X(ConvertFp16ToFp32) \
364 X(ConvertFp32ToBf16) \
365 X(ConvertFp32ToFp16) \
366 X(Convolution2d) \
367 X(Debug) \
368 X(DepthToSpace) \
369 X(DepthwiseConvolution2d) \
370 X(Dequantize) \
371 X(DetectionPostProcess) \
372 X(Division) \
373 X(ElementwiseUnary) \
374 X(FakeQuantization) \
375 X(Fill) \
376 X(Floor) \
377 X(FullyConnected) \
378 X(Gather) \
379 X(Input) \
380 X(InstanceNormalization) \
381 X(L2Normalization) \
382 X(LogicalBinary) \
383 X(LogSoftmax) \
384 X(Lstm) \
385 X(QLstm) \
386 X(Map) \
387 X(Maximum) \
388 X(Mean) \
389 X(MemCopy) \
390 X(MemImport) \
391 X(Merge) \
392 X(Minimum) \
393 X(Multiplication) \
394 X(Normalization) \
395 X(Output) \
396 X(Pad) \
397 X(Permute) \
398 X(Pooling2d) \
399 X(PreCompiled) \
400 X(Prelu) \
401 X(Quantize) \
402 X(QuantizedLstm) \
403 X(Reshape) \
404 X(Rank) \
405 X(Resize) \
406 X(Reduce) \
407 X(Slice) \
408 X(Softmax) \
409 X(SpaceToBatchNd) \
410 X(SpaceToDepth) \
411 X(Splitter) \
412 X(Stack) \
413 X(StandIn) \
414 X(StridedSlice) \
415 X(Subtraction) \
416 X(Switch) \
417 X(Transpose) \
418 X(TransposeConvolution2d) \
mathad01b392e982021-04-07 12:07:30 +0100419 X(Unmap) \
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100420 X(Cast) \
421 X(Shape) \
422 X(UnidirectionalSequenceLstm) \
Simon Obute51f67772021-09-03 15:50:13 +0100423 X(ChannelShuffle) \
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100424 X(Convolution3d) \
425
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100426// New layers should be added at last to minimize instability.
Keith Davis3ae3f972021-05-21 16:33:48 +0100427
Finn Williamsb454c5c2021-02-09 15:56:23 +0000428/// When adding a new layer, adapt also the LastLayer enum value in the
429/// enum class LayerType below
430enum class LayerType
431{
432#define X(name) name,
433 LIST_OF_LAYER_TYPE
434#undef X
435 FirstLayer = Activation,
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100436 LastLayer = UnidirectionalSequenceLstm
Finn Williamsb454c5c2021-02-09 15:56:23 +0000437};
438
439const char* GetLayerTypeAsCString(LayerType type);
440
David Beck9df2d952018-10-10 15:11:44 +0100441} // namespace armnn