blob: e7c17608ca5884059d8a32610b45e56fbe34a8b5 [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 Davise813d672021-04-22 10:10:34 +010059enum class QosExecPriority
60{
61 Low = 0,
62 Medium = 1,
63 High = 2
64};
65
telsoa014fcda012018-03-09 14:13:49 +000066enum class ActivationFunction
67{
68 Sigmoid = 0,
69 TanH = 1,
70 Linear = 2,
71 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000072 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000073 SoftReLu = 5,
74 LeakyReLu = 6,
75 Abs = 7,
76 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000077 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000078 Elu = 10,
79 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +000080};
81
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010082enum class ArgMinMaxFunction
83{
84 Min = 0,
85 Max = 1
86};
87
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010088enum class ComparisonOperation
89{
90 Equal = 0,
91 Greater = 1,
92 GreaterOrEqual = 2,
93 Less = 3,
94 LessOrEqual = 4,
95 NotEqual = 5
96};
97
James Conroyaba90cd2020-11-06 16:28:18 +000098enum class LogicalBinaryOperation
99{
100 LogicalAnd = 0,
101 LogicalOr = 1
102};
103
josh minor4a3c6102020-01-06 16:40:46 -0600104enum class UnaryOperation
105{
James Conroyaba90cd2020-11-06 16:28:18 +0000106 Abs = 0,
107 Exp = 1,
108 Sqrt = 2,
109 Rsqrt = 3,
110 Neg = 4,
Teresa Charlin50de4fa2021-05-31 18:47:33 +0100111 LogicalNot = 5,
112 Log = 6,
113 Sin = 7
josh minor4a3c6102020-01-06 16:40:46 -0600114};
115
telsoa014fcda012018-03-09 14:13:49 +0000116enum class PoolingAlgorithm
117{
118 Max = 0,
119 Average = 1,
120 L2 = 2
121};
122
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000123enum class ReduceOperation
124{
125 Sum = 0,
126 Max = 1,
127 Mean = 2,
128 Min = 3
129};
130
Teresa Charlina9075df2019-06-27 15:41:57 +0100131enum class ResizeMethod
132{
133 Bilinear = 0,
134 NearestNeighbor = 1
135};
136
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100137enum class Dimensionality
138{
139 NotSpecified = 0,
140 Specified = 1,
141 Scalar = 2
142};
143
telsoa014fcda012018-03-09 14:13:49 +0000144///
145/// The padding method modifies the output of pooling layers.
146/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100147/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000148/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100149/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000150/// fields in the divisor of Average and L2 pooling, while
151/// Exclude does not.
152///
153enum class PaddingMethod
154{
telsoa01c577f2c2018-08-31 09:22:23 +0100155 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100156 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100157 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100158 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000159};
160
161enum class NormalizationAlgorithmChannel
162{
163 Across = 0,
164 Within = 1
165};
166
167enum class NormalizationAlgorithmMethod
168{
David Beckdcb751f2018-10-03 11:42:42 +0100169 /// Krichevsky 2012: Local Brightness Normalization
170 LocalBrightness = 0,
171 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100172 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000173};
174
175enum class OutputShapeRounding
176{
177 Floor = 0,
178 Ceiling = 1
179};
180
Teresa Charlincdc01492020-06-09 18:00:20 +0100181///
182/// The ShapeInferenceMethod modify how the output shapes are treated.
183/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
184/// and any mismatch is reported.
185/// When InferAndValidate is selected 2 actions must be performed: (1)infer output shape from inputs and (2)validate the
186/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
187/// specified explicitly, however this information can be calculated from the inputs.
188///
189enum class ShapeInferenceMethod
190{
191 /// Validate all output shapes
192 ValidateOnly = 0,
193 /// Infer missing output shapes and validate all output shapes
194 InferAndValidate = 1
195};
196
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100197/// Define the Memory Source to reduce copies
198enum class MemorySource : uint32_t
199{
200 Undefined = 0,
201 Malloc = 1,
202 DmaBuf = 2,
203 DmaBufProtected = 4
204};
205
David Beck9efb57d2018-11-05 13:40:33 +0000206/// Each backend should implement an IBackend.
207class IBackend
208{
209protected:
210 IBackend() {}
211 virtual ~IBackend() {}
212
213public:
214 virtual const BackendId& GetId() const = 0;
215};
216
217using IBackendSharedPtr = std::shared_ptr<IBackend>;
218using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
219
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000220/// BackendCapability class
221enum class BackendCapability : uint32_t
222{
223 /// Constant weights can be accessed through the descriptors,
224 /// On the other hand, non-const weights can be accessed through inputs.
225 NonConstWeights,
226
Sadik Armaganaede8ca2021-03-31 16:12:13 +0100227 /// Asynchronous Execution.
228 AsyncExecution,
229
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000230 // add new enum values here
231};
232
David Beckdcb751f2018-10-03 11:42:42 +0100233/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100234class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000235{
telsoa01c577f2c2018-08-31 09:22:23 +0100236protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000237 IDeviceSpec() {}
238 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100239public:
240 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000241};
242
243/// Type of identifiers for bindable layers (inputs, outputs).
244using LayerBindingId = int;
245
246class PermutationVector
247{
248public:
249 using ValueType = unsigned int;
250 using SizeType = unsigned int;
251 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
252 using ConstIterator = typename ArrayType::const_iterator;
253
telsoa01c577f2c2018-08-31 09:22:23 +0100254 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000255 /// when source and target potentially have different memory layouts.
256 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100257 /// 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 +0000258 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
259 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
260 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
261 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
262 /// [ 0, 2, 3, 1 ].
263 ///
264 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
265 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
266 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
267 /// [ 0, 3, 1, 2 ].
268 ///
269 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
270
271 PermutationVector(std::initializer_list<ValueType> dimMappings);
272
273 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
274
275 SizeType GetSize() const { return m_NumDimMappings; }
276
277 ConstIterator begin() const { return m_DimMappings.begin(); }
Colm Donelan41e764c2021-05-27 16:43:25 +0100278 /**
279 *
280 * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
281 */
282 ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
telsoa014fcda012018-03-09 14:13:49 +0000283
284 bool IsEqual(const PermutationVector& other) const
285 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100286 if (m_NumDimMappings != other.m_NumDimMappings) return false;
287 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
288 {
289 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
290 }
291 return true;
telsoa014fcda012018-03-09 14:13:49 +0000292 }
293
294 bool IsInverse(const PermutationVector& other) const
295 {
296 bool isInverse = (GetSize() == other.GetSize());
297 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
298 {
299 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
300 }
301 return isInverse;
302 }
303
304private:
305 ArrayType m_DimMappings;
306 /// Number of valid entries in @ref m_DimMappings
307 SizeType m_NumDimMappings;
308};
309
janeil013fec1ea2019-11-07 09:47:20 +0000310namespace profiling { class ProfilingGuid; }
311
telsoa01c577f2c2018-08-31 09:22:23 +0100312/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000313using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100314
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000315class ITensorHandle;
316
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000317/// Define the type of callback for the Debug layer to call
318/// @param guid - guid of layer connected to the input of the Debug layer
319/// @param slotIndex - index of the output slot connected to the input of the Debug layer
320/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
321using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000322
Keith Davise813d672021-04-22 10:10:34 +0100323/// Define a timer and associated inference ID for recording execution times
324using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
325using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
janeil01c4946c72019-11-07 09:32:28 +0000326
janeil01c4946c72019-11-07 09:32:28 +0000327
Finn Williamsb454c5c2021-02-09 15:56:23 +0000328/// This list uses X macro technique.
329/// See https://en.wikipedia.org/wiki/X_Macro for more info
330#define LIST_OF_LAYER_TYPE \
331 X(Activation) \
332 X(Addition) \
333 X(ArgMinMax) \
334 X(BatchNormalization) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100335 X(BatchToSpaceNd) \
336 X(Cast) \
Finn Williamsb454c5c2021-02-09 15:56:23 +0000337 X(Comparison) \
338 X(Concat) \
339 X(Constant) \
340 X(ConvertBf16ToFp32) \
341 X(ConvertFp16ToFp32) \
342 X(ConvertFp32ToBf16) \
343 X(ConvertFp32ToFp16) \
344 X(Convolution2d) \
345 X(Debug) \
346 X(DepthToSpace) \
347 X(DepthwiseConvolution2d) \
348 X(Dequantize) \
349 X(DetectionPostProcess) \
350 X(Division) \
351 X(ElementwiseUnary) \
352 X(FakeQuantization) \
353 X(Fill) \
354 X(Floor) \
355 X(FullyConnected) \
356 X(Gather) \
357 X(Input) \
358 X(InstanceNormalization) \
359 X(L2Normalization) \
360 X(LogicalBinary) \
361 X(LogSoftmax) \
362 X(Lstm) \
363 X(QLstm) \
364 X(Map) \
365 X(Maximum) \
366 X(Mean) \
367 X(MemCopy) \
368 X(MemImport) \
369 X(Merge) \
370 X(Minimum) \
371 X(Multiplication) \
372 X(Normalization) \
373 X(Output) \
374 X(Pad) \
375 X(Permute) \
376 X(Pooling2d) \
377 X(PreCompiled) \
378 X(Prelu) \
379 X(Quantize) \
380 X(QuantizedLstm) \
381 X(Reshape) \
382 X(Rank) \
383 X(Resize) \
384 X(Reduce) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100385 X(Shape) \
Finn Williamsb454c5c2021-02-09 15:56:23 +0000386 X(Slice) \
387 X(Softmax) \
388 X(SpaceToBatchNd) \
389 X(SpaceToDepth) \
390 X(Splitter) \
391 X(Stack) \
392 X(StandIn) \
393 X(StridedSlice) \
394 X(Subtraction) \
395 X(Switch) \
396 X(Transpose) \
397 X(TransposeConvolution2d) \
mathad01b392e982021-04-07 12:07:30 +0100398 X(Unmap) \
Keith Davis3ae3f972021-05-21 16:33:48 +0100399
Finn Williamsb454c5c2021-02-09 15:56:23 +0000400/// When adding a new layer, adapt also the LastLayer enum value in the
401/// enum class LayerType below
402enum class LayerType
403{
404#define X(name) name,
405 LIST_OF_LAYER_TYPE
406#undef X
407 FirstLayer = Activation,
Keith Davis3ae3f972021-05-21 16:33:48 +0100408 LastLayer = Unmap
Finn Williamsb454c5c2021-02-09 15:56:23 +0000409};
410
411const char* GetLayerTypeAsCString(LayerType type);
412
David Beck9df2d952018-10-10 15:11:44 +0100413} // namespace armnn