blob: bc41003c57e466946a271e9372c9b9f70adcdc4c [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// Copyright © 2017 Arm Ltd. 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>
David Beckdcb751f2018-10-03 11:42:42 +01009#include <memory>
janeil01c4946c72019-11-07 09:32:28 +000010#include <stdint.h>
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
telsoa014fcda012018-03-09 14:13:49 +000023/// @enum Status enumeration
24/// @var Status::Successful
25/// @var Status::Failure
26enum class Status
27{
28 Success = 0,
29 Failure = 1
30};
31
32enum class DataType
33{
telsoa01c577f2c2018-08-31 09:22:23 +010034 Float16 = 0,
ruoyan0120e984f2018-12-12 18:11:25 +000035 Float32 = 1,
Derek Lambertif90c56d2020-01-10 17:14:08 +000036 QAsymmU8 = 2,
ruoyan0120e984f2018-12-12 18:11:25 +000037 Signed32 = 3,
Nattapat Chaimanowongcd5ac232019-03-19 12:26:36 +000038 Boolean = 4,
Derek Lambertif90c56d2020-01-10 17:14:08 +000039 QSymmS16 = 5,
Derek Lambertid466a542020-01-22 15:37:29 +000040 QuantizedSymm8PerAxis ARMNN_DEPRECATED_ENUM_MSG("Per Axis property inferred by number of scales in TensorInfo") = 6,
Derek Lambertif90c56d2020-01-10 17:14:08 +000041 QSymmS8 = 7,
Ryan OShea9add1202020-02-07 10:06:33 +000042 QAsymmS8 = 8,
Narumol Prangnawaratc3bf6ef2020-02-28 12:45:21 +000043 BFloat16 = 9,
Inki Daed4619e22020-09-10 15:33:54 +090044 Signed64 = 10,
Derek Lambertif90c56d2020-01-10 17:14:08 +000045
Derek Lamberti41e92b02020-01-21 13:43:21 +000046 QuantisedAsymm8 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QAsymmU8 instead.") = QAsymmU8,
47 QuantisedSymm16 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QSymmS16 instead.") = QSymmS16
telsoa014fcda012018-03-09 14:13:49 +000048};
49
Derek Lamberti0cff1632018-09-18 16:02:25 +010050enum class DataLayout
51{
52 NCHW = 1,
53 NHWC = 2
54};
55
telsoa014fcda012018-03-09 14:13:49 +000056enum class ActivationFunction
57{
58 Sigmoid = 0,
59 TanH = 1,
60 Linear = 2,
61 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000062 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000063 SoftReLu = 5,
64 LeakyReLu = 6,
65 Abs = 7,
66 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000067 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000068 Elu = 10,
69 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +000070};
71
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010072enum class ArgMinMaxFunction
73{
74 Min = 0,
75 Max = 1
76};
77
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010078enum class ComparisonOperation
79{
80 Equal = 0,
81 Greater = 1,
82 GreaterOrEqual = 2,
83 Less = 3,
84 LessOrEqual = 4,
85 NotEqual = 5
86};
87
James Conroyaba90cd2020-11-06 16:28:18 +000088enum class LogicalBinaryOperation
89{
90 LogicalAnd = 0,
91 LogicalOr = 1
92};
93
josh minor4a3c6102020-01-06 16:40:46 -060094enum class UnaryOperation
95{
James Conroyaba90cd2020-11-06 16:28:18 +000096 Abs = 0,
97 Exp = 1,
98 Sqrt = 2,
99 Rsqrt = 3,
100 Neg = 4,
101 LogicalNot = 5
josh minor4a3c6102020-01-06 16:40:46 -0600102};
103
telsoa014fcda012018-03-09 14:13:49 +0000104enum class PoolingAlgorithm
105{
106 Max = 0,
107 Average = 1,
108 L2 = 2
109};
110
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000111enum class ReduceOperation
112{
113 Sum = 0,
114 Max = 1,
115 Mean = 2,
116 Min = 3
117};
118
Teresa Charlina9075df2019-06-27 15:41:57 +0100119enum class ResizeMethod
120{
121 Bilinear = 0,
122 NearestNeighbor = 1
123};
124
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100125enum class Dimensionality
126{
127 NotSpecified = 0,
128 Specified = 1,
129 Scalar = 2
130};
131
telsoa014fcda012018-03-09 14:13:49 +0000132///
133/// The padding method modifies the output of pooling layers.
134/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100135/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000136/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100137/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000138/// fields in the divisor of Average and L2 pooling, while
139/// Exclude does not.
140///
141enum class PaddingMethod
142{
telsoa01c577f2c2018-08-31 09:22:23 +0100143 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100144 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100145 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100146 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000147};
148
149enum class NormalizationAlgorithmChannel
150{
151 Across = 0,
152 Within = 1
153};
154
155enum class NormalizationAlgorithmMethod
156{
David Beckdcb751f2018-10-03 11:42:42 +0100157 /// Krichevsky 2012: Local Brightness Normalization
158 LocalBrightness = 0,
159 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100160 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000161};
162
163enum class OutputShapeRounding
164{
165 Floor = 0,
166 Ceiling = 1
167};
168
Teresa Charlincdc01492020-06-09 18:00:20 +0100169///
170/// The ShapeInferenceMethod modify how the output shapes are treated.
171/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
172/// and any mismatch is reported.
173/// When InferAndValidate is selected 2 actions must be performed: (1)infer output shape from inputs and (2)validate the
174/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
175/// specified explicitly, however this information can be calculated from the inputs.
176///
177enum class ShapeInferenceMethod
178{
179 /// Validate all output shapes
180 ValidateOnly = 0,
181 /// Infer missing output shapes and validate all output shapes
182 InferAndValidate = 1
183};
184
Francis Murtagh73d3e2e2021-04-29 14:23:04 +0100185/// Define the Memory Source to reduce copies
186enum class MemorySource : uint32_t
187{
188 Undefined = 0,
189 Malloc = 1,
190 DmaBuf = 2,
191 DmaBufProtected = 4
192};
193
David Beck9efb57d2018-11-05 13:40:33 +0000194/// Each backend should implement an IBackend.
195class IBackend
196{
197protected:
198 IBackend() {}
199 virtual ~IBackend() {}
200
201public:
202 virtual const BackendId& GetId() const = 0;
203};
204
205using IBackendSharedPtr = std::shared_ptr<IBackend>;
206using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
207
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000208/// BackendCapability class
209enum class BackendCapability : uint32_t
210{
211 /// Constant weights can be accessed through the descriptors,
212 /// On the other hand, non-const weights can be accessed through inputs.
213 NonConstWeights,
214
Sadik Armaganaede8ca2021-03-31 16:12:13 +0100215 /// Asynchronous Execution.
216 AsyncExecution,
217
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000218 // add new enum values here
219};
220
David Beckdcb751f2018-10-03 11:42:42 +0100221/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100222class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000223{
telsoa01c577f2c2018-08-31 09:22:23 +0100224protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000225 IDeviceSpec() {}
226 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100227public:
228 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000229};
230
231/// Type of identifiers for bindable layers (inputs, outputs).
232using LayerBindingId = int;
233
234class PermutationVector
235{
236public:
237 using ValueType = unsigned int;
238 using SizeType = unsigned int;
239 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
240 using ConstIterator = typename ArrayType::const_iterator;
241
telsoa01c577f2c2018-08-31 09:22:23 +0100242 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000243 /// when source and target potentially have different memory layouts.
244 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100245 /// 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 +0000246 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
247 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
248 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
249 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
250 /// [ 0, 2, 3, 1 ].
251 ///
252 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
253 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
254 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
255 /// [ 0, 3, 1, 2 ].
256 ///
257 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
258
259 PermutationVector(std::initializer_list<ValueType> dimMappings);
260
261 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
262
263 SizeType GetSize() const { return m_NumDimMappings; }
264
265 ConstIterator begin() const { return m_DimMappings.begin(); }
266 ConstIterator end() const { return m_DimMappings.end(); }
267
268 bool IsEqual(const PermutationVector& other) const
269 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100270 if (m_NumDimMappings != other.m_NumDimMappings) return false;
271 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
272 {
273 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
274 }
275 return true;
telsoa014fcda012018-03-09 14:13:49 +0000276 }
277
278 bool IsInverse(const PermutationVector& other) const
279 {
280 bool isInverse = (GetSize() == other.GetSize());
281 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
282 {
283 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
284 }
285 return isInverse;
286 }
287
288private:
289 ArrayType m_DimMappings;
290 /// Number of valid entries in @ref m_DimMappings
291 SizeType m_NumDimMappings;
292};
293
janeil013fec1ea2019-11-07 09:47:20 +0000294namespace profiling { class ProfilingGuid; }
295
telsoa01c577f2c2018-08-31 09:22:23 +0100296/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000297using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100298
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000299class ITensorHandle;
300
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000301/// Define the type of callback for the Debug layer to call
302/// @param guid - guid of layer connected to the input of the Debug layer
303/// @param slotIndex - index of the output slot connected to the input of the Debug layer
304/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
305using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000306
janeil01c4946c72019-11-07 09:32:28 +0000307
308namespace profiling
309{
310
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000311static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63;
312
janeil01c4946c72019-11-07 09:32:28 +0000313class ProfilingGuid
314{
315public:
Sadik Armagan3184c902020-03-18 10:57:30 +0000316 ProfilingGuid() : m_Guid(0) {}
317
janeil01c4946c72019-11-07 09:32:28 +0000318 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
319
320 operator uint64_t() const { return m_Guid; }
321
322 bool operator==(const ProfilingGuid& other) const
323 {
324 return m_Guid == other.m_Guid;
325 }
326
327 bool operator!=(const ProfilingGuid& other) const
328 {
329 return m_Guid != other.m_Guid;
330 }
331
332 bool operator<(const ProfilingGuid& other) const
333 {
334 return m_Guid < other.m_Guid;
335 }
336
337 bool operator<=(const ProfilingGuid& other) const
338 {
339 return m_Guid <= other.m_Guid;
340 }
341
342 bool operator>(const ProfilingGuid& other) const
343 {
344 return m_Guid > other.m_Guid;
345 }
346
347 bool operator>=(const ProfilingGuid& other) const
348 {
349 return m_Guid >= other.m_Guid;
350 }
351
352protected:
353 uint64_t m_Guid;
354};
355
356/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
357struct ProfilingDynamicGuid : public ProfilingGuid
358{
359 using ProfilingGuid::ProfilingGuid;
360};
361
362struct ProfilingStaticGuid : public ProfilingGuid
363{
364 using ProfilingGuid::ProfilingGuid;
365};
366
367} // namespace profiling
368
Finn Williamsb454c5c2021-02-09 15:56:23 +0000369/// This list uses X macro technique.
370/// See https://en.wikipedia.org/wiki/X_Macro for more info
371#define LIST_OF_LAYER_TYPE \
372 X(Activation) \
373 X(Addition) \
374 X(ArgMinMax) \
375 X(BatchNormalization) \
376 X(BatchToSpaceNd) \
377 X(Comparison) \
378 X(Concat) \
379 X(Constant) \
380 X(ConvertBf16ToFp32) \
381 X(ConvertFp16ToFp32) \
382 X(ConvertFp32ToBf16) \
383 X(ConvertFp32ToFp16) \
384 X(Convolution2d) \
385 X(Debug) \
386 X(DepthToSpace) \
387 X(DepthwiseConvolution2d) \
388 X(Dequantize) \
389 X(DetectionPostProcess) \
390 X(Division) \
391 X(ElementwiseUnary) \
392 X(FakeQuantization) \
393 X(Fill) \
394 X(Floor) \
395 X(FullyConnected) \
396 X(Gather) \
397 X(Input) \
398 X(InstanceNormalization) \
399 X(L2Normalization) \
400 X(LogicalBinary) \
401 X(LogSoftmax) \
402 X(Lstm) \
403 X(QLstm) \
404 X(Map) \
405 X(Maximum) \
406 X(Mean) \
407 X(MemCopy) \
408 X(MemImport) \
409 X(Merge) \
410 X(Minimum) \
411 X(Multiplication) \
412 X(Normalization) \
413 X(Output) \
414 X(Pad) \
415 X(Permute) \
416 X(Pooling2d) \
417 X(PreCompiled) \
418 X(Prelu) \
419 X(Quantize) \
420 X(QuantizedLstm) \
421 X(Reshape) \
422 X(Rank) \
423 X(Resize) \
424 X(Reduce) \
425 X(Slice) \
426 X(Softmax) \
427 X(SpaceToBatchNd) \
428 X(SpaceToDepth) \
429 X(Splitter) \
430 X(Stack) \
431 X(StandIn) \
432 X(StridedSlice) \
433 X(Subtraction) \
434 X(Switch) \
435 X(Transpose) \
436 X(TransposeConvolution2d) \
mathad01b392e982021-04-07 12:07:30 +0100437 X(Unmap) \
438 X(Cast)
Finn Williamsb454c5c2021-02-09 15:56:23 +0000439/// When adding a new layer, adapt also the LastLayer enum value in the
440/// enum class LayerType below
441enum class LayerType
442{
443#define X(name) name,
444 LIST_OF_LAYER_TYPE
445#undef X
446 FirstLayer = Activation,
mathad01b392e982021-04-07 12:07:30 +0100447 LastLayer = Cast
Finn Williamsb454c5c2021-02-09 15:56:23 +0000448};
449
450const char* GetLayerTypeAsCString(LayerType type);
451
David Beck9df2d952018-10-10 15:11:44 +0100452} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000453
454
455namespace std
456{
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000457/// make ProfilingGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000458template<>
459struct hash<armnn::profiling::ProfilingGuid>
460{
461 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
462 {
463 return hash<uint64_t>()(uint64_t(guid));
464 }
465};
466
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000467/// make ProfilingDynamicGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000468template<>
469struct hash<armnn::profiling::ProfilingDynamicGuid>
470{
471 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
472 {
473 return hash<uint64_t>()(uint64_t(guid));
474 }
475};
476
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000477/// make ProfilingStaticGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000478template<>
479struct hash<armnn::profiling::ProfilingStaticGuid>
480{
481 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
482 {
483 return hash<uint64_t>()(uint64_t(guid));
484 }
485};
janeil013fec1ea2019-11-07 09:47:20 +0000486} // namespace std