blob: 19e93c3c7ef478311e1350b7a713aec39523376b [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,
Derek Lambertid466a542020-01-22 15:37:29 +000043 QuantizedSymm8PerAxis ARMNN_DEPRECATED_ENUM_MSG("Per Axis property inferred by number of scales in TensorInfo") = 6,
Keith Davise813d672021-04-22 10:10:34 +010044 QSymmS8 = 7,
Ryan OShea9add1202020-02-07 10:06:33 +000045 QAsymmS8 = 8,
Narumol Prangnawaratc3bf6ef2020-02-28 12:45:21 +000046 BFloat16 = 9,
Inki Daed4619e22020-09-10 15:33:54 +090047 Signed64 = 10,
Derek Lambertif90c56d2020-01-10 17:14:08 +000048
Derek Lamberti41e92b02020-01-21 13:43:21 +000049 QuantisedAsymm8 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QAsymmU8 instead.") = QAsymmU8,
50 QuantisedSymm16 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QSymmS16 instead.") = QSymmS16
telsoa014fcda012018-03-09 14:13:49 +000051};
52
Derek Lamberti0cff1632018-09-18 16:02:25 +010053enum class DataLayout
54{
55 NCHW = 1,
56 NHWC = 2
57};
58
Keith Davis4914d0c2021-08-18 17:14:05 +010059/// Define the behaviour of the internal profiler when outputting network details
60enum class ProfilingDetailsMethod
61{
62 Undefined = 0,
63 DetailsWithEvents = 1,
64 DetailsOnly = 2
65};
66
67
Keith Davise813d672021-04-22 10:10:34 +010068enum class QosExecPriority
69{
70 Low = 0,
71 Medium = 1,
72 High = 2
73};
74
telsoa014fcda012018-03-09 14:13:49 +000075enum class ActivationFunction
76{
77 Sigmoid = 0,
78 TanH = 1,
79 Linear = 2,
80 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000081 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000082 SoftReLu = 5,
83 LeakyReLu = 6,
84 Abs = 7,
85 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000086 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000087 Elu = 10,
88 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +000089};
90
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010091enum class ArgMinMaxFunction
92{
93 Min = 0,
94 Max = 1
95};
96
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010097enum class ComparisonOperation
98{
99 Equal = 0,
100 Greater = 1,
101 GreaterOrEqual = 2,
102 Less = 3,
103 LessOrEqual = 4,
104 NotEqual = 5
105};
106
James Conroyaba90cd2020-11-06 16:28:18 +0000107enum class LogicalBinaryOperation
108{
109 LogicalAnd = 0,
110 LogicalOr = 1
111};
112
josh minor4a3c6102020-01-06 16:40:46 -0600113enum class UnaryOperation
114{
James Conroyaba90cd2020-11-06 16:28:18 +0000115 Abs = 0,
116 Exp = 1,
117 Sqrt = 2,
118 Rsqrt = 3,
119 Neg = 4,
Teresa Charlin50de4fa2021-05-31 18:47:33 +0100120 LogicalNot = 5,
121 Log = 6,
122 Sin = 7
josh minor4a3c6102020-01-06 16:40:46 -0600123};
124
telsoa014fcda012018-03-09 14:13:49 +0000125enum class PoolingAlgorithm
126{
127 Max = 0,
128 Average = 1,
129 L2 = 2
130};
131
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000132enum class ReduceOperation
133{
134 Sum = 0,
135 Max = 1,
136 Mean = 2,
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100137 Min = 3,
138 Prod = 4
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000139};
140
Teresa Charlina9075df2019-06-27 15:41:57 +0100141enum class ResizeMethod
142{
143 Bilinear = 0,
144 NearestNeighbor = 1
145};
146
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100147enum class Dimensionality
148{
149 NotSpecified = 0,
150 Specified = 1,
151 Scalar = 2
152};
153
telsoa014fcda012018-03-09 14:13:49 +0000154///
155/// The padding method modifies the output of pooling layers.
156/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100157/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000158/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100159/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000160/// fields in the divisor of Average and L2 pooling, while
161/// Exclude does not.
162///
163enum class PaddingMethod
164{
telsoa01c577f2c2018-08-31 09:22:23 +0100165 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100166 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100167 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100168 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000169};
170
171enum class NormalizationAlgorithmChannel
172{
173 Across = 0,
174 Within = 1
175};
176
177enum class NormalizationAlgorithmMethod
178{
David Beckdcb751f2018-10-03 11:42:42 +0100179 /// Krichevsky 2012: Local Brightness Normalization
180 LocalBrightness = 0,
181 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100182 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000183};
184
185enum class OutputShapeRounding
186{
187 Floor = 0,
188 Ceiling = 1
189};
190
Teresa Charlincdc01492020-06-09 18:00:20 +0100191///
192/// The ShapeInferenceMethod modify how the output shapes are treated.
193/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
194/// and any mismatch is reported.
195/// When InferAndValidate is selected 2 actions must be performed: (1)infer output shape from inputs and (2)validate the
196/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
197/// specified explicitly, however this information can be calculated from the inputs.
198///
199enum class ShapeInferenceMethod
200{
201 /// Validate all output shapes
202 ValidateOnly = 0,
203 /// Infer missing output shapes and validate all output shapes
204 InferAndValidate = 1
205};
206
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100207/// Define the Memory Source to reduce copies
208enum class MemorySource : uint32_t
209{
210 Undefined = 0,
211 Malloc = 1,
212 DmaBuf = 2,
213 DmaBufProtected = 4
214};
215
David Beck9efb57d2018-11-05 13:40:33 +0000216/// Each backend should implement an IBackend.
217class IBackend
218{
219protected:
220 IBackend() {}
221 virtual ~IBackend() {}
222
223public:
224 virtual const BackendId& GetId() const = 0;
225};
226
227using IBackendSharedPtr = std::shared_ptr<IBackend>;
228using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
229
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000230/// BackendCapability class
231enum class BackendCapability : uint32_t
232{
233 /// Constant weights can be accessed through the descriptors,
234 /// On the other hand, non-const weights can be accessed through inputs.
235 NonConstWeights,
236
Sadik Armaganaede8ca2021-03-31 16:12:13 +0100237 /// Asynchronous Execution.
238 AsyncExecution,
239
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000240 // add new enum values here
241};
242
David Beckdcb751f2018-10-03 11:42:42 +0100243/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100244class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000245{
telsoa01c577f2c2018-08-31 09:22:23 +0100246protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000247 IDeviceSpec() {}
248 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100249public:
250 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000251};
252
253/// Type of identifiers for bindable layers (inputs, outputs).
254using LayerBindingId = int;
255
256class PermutationVector
257{
258public:
259 using ValueType = unsigned int;
260 using SizeType = unsigned int;
261 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
262 using ConstIterator = typename ArrayType::const_iterator;
263
telsoa01c577f2c2018-08-31 09:22:23 +0100264 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000265 /// when source and target potentially have different memory layouts.
266 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100267 /// 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 +0000268 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
269 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
270 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
271 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
272 /// [ 0, 2, 3, 1 ].
273 ///
274 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
275 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
276 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
277 /// [ 0, 3, 1, 2 ].
278 ///
279 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
280
281 PermutationVector(std::initializer_list<ValueType> dimMappings);
282
283 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
284
285 SizeType GetSize() const { return m_NumDimMappings; }
286
287 ConstIterator begin() const { return m_DimMappings.begin(); }
Colm Donelan41e764c2021-05-27 16:43:25 +0100288 /**
289 *
290 * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
291 */
292 ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
telsoa014fcda012018-03-09 14:13:49 +0000293
294 bool IsEqual(const PermutationVector& other) const
295 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100296 if (m_NumDimMappings != other.m_NumDimMappings) return false;
297 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
298 {
299 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
300 }
301 return true;
telsoa014fcda012018-03-09 14:13:49 +0000302 }
303
304 bool IsInverse(const PermutationVector& other) const
305 {
306 bool isInverse = (GetSize() == other.GetSize());
307 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
308 {
309 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
310 }
311 return isInverse;
312 }
313
314private:
315 ArrayType m_DimMappings;
316 /// Number of valid entries in @ref m_DimMappings
317 SizeType m_NumDimMappings;
318};
319
janeil013fec1ea2019-11-07 09:47:20 +0000320namespace profiling { class ProfilingGuid; }
321
telsoa01c577f2c2018-08-31 09:22:23 +0100322/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000323using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100324
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000325class ITensorHandle;
326
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000327/// Define the type of callback for the Debug layer to call
328/// @param guid - guid of layer connected to the input of the Debug layer
329/// @param slotIndex - index of the output slot connected to the input of the Debug layer
330/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
331using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000332
Keith Davise813d672021-04-22 10:10:34 +0100333/// Define a timer and associated inference ID for recording execution times
334using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
335using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
janeil01c4946c72019-11-07 09:32:28 +0000336
janeil01c4946c72019-11-07 09:32:28 +0000337
Finn Williamsb454c5c2021-02-09 15:56:23 +0000338/// This list uses X macro technique.
339/// See https://en.wikipedia.org/wiki/X_Macro for more info
340#define LIST_OF_LAYER_TYPE \
341 X(Activation) \
342 X(Addition) \
343 X(ArgMinMax) \
344 X(BatchNormalization) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100345 X(BatchToSpaceNd) \
Finn Williamsb454c5c2021-02-09 15:56:23 +0000346 X(Comparison) \
347 X(Concat) \
348 X(Constant) \
349 X(ConvertBf16ToFp32) \
350 X(ConvertFp16ToFp32) \
351 X(ConvertFp32ToBf16) \
352 X(ConvertFp32ToFp16) \
353 X(Convolution2d) \
354 X(Debug) \
355 X(DepthToSpace) \
356 X(DepthwiseConvolution2d) \
357 X(Dequantize) \
358 X(DetectionPostProcess) \
359 X(Division) \
360 X(ElementwiseUnary) \
361 X(FakeQuantization) \
362 X(Fill) \
363 X(Floor) \
364 X(FullyConnected) \
365 X(Gather) \
366 X(Input) \
367 X(InstanceNormalization) \
368 X(L2Normalization) \
369 X(LogicalBinary) \
370 X(LogSoftmax) \
371 X(Lstm) \
372 X(QLstm) \
373 X(Map) \
374 X(Maximum) \
375 X(Mean) \
376 X(MemCopy) \
377 X(MemImport) \
378 X(Merge) \
379 X(Minimum) \
380 X(Multiplication) \
381 X(Normalization) \
382 X(Output) \
383 X(Pad) \
384 X(Permute) \
385 X(Pooling2d) \
386 X(PreCompiled) \
387 X(Prelu) \
388 X(Quantize) \
389 X(QuantizedLstm) \
390 X(Reshape) \
391 X(Rank) \
392 X(Resize) \
393 X(Reduce) \
394 X(Slice) \
395 X(Softmax) \
396 X(SpaceToBatchNd) \
397 X(SpaceToDepth) \
398 X(Splitter) \
399 X(Stack) \
400 X(StandIn) \
401 X(StridedSlice) \
402 X(Subtraction) \
403 X(Switch) \
404 X(Transpose) \
405 X(TransposeConvolution2d) \
mathad01b392e982021-04-07 12:07:30 +0100406 X(Unmap) \
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100407 X(Cast) \
408 X(Shape) \
409 X(UnidirectionalSequenceLstm) \
410
411// New layers should be added at last to minimize instability.
Keith Davis3ae3f972021-05-21 16:33:48 +0100412
Finn Williamsb454c5c2021-02-09 15:56:23 +0000413/// When adding a new layer, adapt also the LastLayer enum value in the
414/// enum class LayerType below
415enum class LayerType
416{
417#define X(name) name,
418 LIST_OF_LAYER_TYPE
419#undef X
420 FirstLayer = Activation,
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100421 LastLayer = UnidirectionalSequenceLstm
Finn Williamsb454c5c2021-02-09 15:56:23 +0000422};
423
424const char* GetLayerTypeAsCString(LayerType type);
425
David Beck9df2d952018-10-10 15:11:44 +0100426} // namespace armnn