ArmNN
 25.02
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Types.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2018-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <array>
8 #include <functional>
9 #include <stdint.h>
10 #include <chrono>
11 #include "BackendId.hpp"
12 #include "Exceptions.hpp"
13 #include "Deprecated.hpp"
14 
15 namespace arm
16 {
17 namespace pipe
18 {
19 
20 class ProfilingGuid;
21 
22 } // namespace arm
23 } // namespace pipe
24 
25 /// Define LayerGuid type.
26 using LayerGuid = arm::pipe::ProfilingGuid;
27 
28 namespace armnn
29 {
30 
31 constexpr unsigned int MaxNumOfTensorDimensions = 5U;
32 
33 /// The lowest performance data capture interval we support is 10 miliseconds.
34 constexpr unsigned int LOWEST_CAPTURE_PERIOD = 10000u;
35 
36 /// Variable to control expire rate of priority queue
37 constexpr unsigned int EXPIRE_RATE = 3U;
38 
39 /// @enum Status enumeration
40 /// @var Status::Successful
41 /// @var Status::Failure
42 enum class Status
43 {
44  Success = 0,
45  Failure = 1
46 };
47 
48 enum class DataType
49 {
50  Float16 = 0,
51  Float32 = 1,
52  QAsymmU8 = 2,
53  Signed32 = 3,
54  Boolean = 4,
55  QSymmS16 = 5,
56  QSymmS8 = 6,
57  QAsymmS8 = 7,
58  BFloat16 = 8,
59  Signed64 = 9,
60 };
61 
62 enum class DataLayout
63 {
64  NCHW = 1,
65  NHWC = 2,
66  NDHWC = 3,
67  NCDHW = 4
68 };
69 
70 /// Define the behaviour of the internal profiler when outputting network details
72 {
73  Undefined = 0,
75  DetailsOnly = 2
76 };
77 
78 
79 enum class QosExecPriority
80 {
81  Low = 0,
82  Medium = 1,
83  High = 2
84 };
85 
87 {
88  Sigmoid = 0,
89  TanH = 1,
90  Linear = 2,
91  ReLu = 3,
92  BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
93  SoftReLu = 5,
94  LeakyReLu = 6,
95  Abs = 7,
96  Sqrt = 8,
97  Square = 9,
98  Elu = 10,
99  HardSwish = 11,
100  Gelu = 12
101 };
102 
104 {
105  Min = 0,
106  Max = 1
107 };
108 
110 {
111  Equal = 0,
112  Greater = 1,
113  GreaterOrEqual = 2,
114  Less = 3,
115  LessOrEqual = 4,
116  NotEqual = 5
117 };
118 
120 {
121  LogicalAnd = 0,
122  LogicalOr = 1
123 };
124 
125 enum class UnaryOperation
126 {
127  Abs = 0,
128  Exp = 1,
129  Sqrt = 2,
130  Rsqrt = 3,
131  Neg = 4,
132  LogicalNot = 5,
133  Log = 6,
134  Sin = 7,
135  Ceil = 8
136 };
137 
138 enum class BinaryOperation
139 {
140  Add = 0,
141  Div = 1,
142  Maximum = 2,
143  Minimum = 3,
144  Mul = 4,
145  Sub = 5,
146  SqDiff = 6,
147  Power = 7,
148  FloorDiv = 8
149 };
150 
152 {
153  Max = 0,
154  Average = 1,
155  L2 = 2
156 };
157 
158 enum class ReduceOperation
159 {
160  Sum = 0,
161  Max = 1,
162  Mean = 2,
163  Min = 3,
164  Prod = 4
165 };
166 
167 enum class ResizeMethod
168 {
169  Bilinear = 0,
170  NearestNeighbor = 1
171 };
172 
173 enum class Dimensionality
174 {
175  NotSpecified = 0,
176  Specified = 1,
177  Scalar = 2
178 };
179 
180 ///
181 /// The padding method modifies the output of pooling layers.
182 /// In both supported methods, the values are ignored (they are
183 /// not even zeroes, which would make a difference for max pooling
184 /// a tensor with negative values). The difference between
185 /// IgnoreValue and Exclude is that the former counts the padding
186 /// fields in the divisor of Average and L2 pooling, while
187 /// Exclude does not.
188 ///
189 enum class PaddingMethod
190 {
191  /// The padding fields count, but are ignored
192  IgnoreValue = 0,
193  /// The padding fields don't count and are ignored
194  Exclude = 1
195 };
196 
197 ///
198 /// The padding mode controls whether the padding should be filled with constant values (Constant), or
199 /// reflect the input, either including the border values (Symmetric) or not (Reflect).
200 ///
201 enum class PaddingMode
202 {
203  Constant = 0,
204  Reflect = 1,
205  Symmetric = 2
206 };
207 
209 {
210  Across = 0,
211  Within = 1
212 };
213 
215 {
216  /// Krichevsky 2012: Local Brightness Normalization
217  LocalBrightness = 0,
218  /// Jarret 2009: Local Contrast Normalization
219  LocalContrast = 1
220 };
221 
223 {
224  Floor = 0,
225  Ceiling = 1
226 };
227 
228 ///
229 /// The ShapeInferenceMethod modify how the output shapes are treated.
230 /// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
231 /// and any mismatch is reported.
232 /// When InferAndValidate is selected 2 actions are performed: (1)infer output shape from inputs and (2)validate the
233 /// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
234 /// specified explicitly, however this information can be calculated from the inputs.
235 ///
237 {
238  /// Validate all output shapes
239  ValidateOnly = 0,
240  /// Infer missing output shapes and validate all output shapes
241  InferAndValidate = 1
242 };
243 
244 /// Define the Memory Source to reduce copies
245 enum class MemorySource : uint32_t
246 {
247  Undefined = 0,
248  Malloc = 1,
249  DmaBuf = 2,
250  DmaBufProtected = 4,
251  Gralloc = 8
252 };
253 
255 {
256  // MemBlocks can be packed on the Y axis only, overlap allowed on X axis.
257  // In other words MemBlocks with overlapping lifetimes cannot use the same MemBin,
258  // equivalent to blob or pooling memory management.
259  SingleAxisPacking = 0,
260 
261  // MemBlocks can be packed on either Y or X axis but cannot overlap on both.
262  // In other words MemBlocks with overlapping lifetimes can use the same MemBin,
263  // equivalent to offset or slab memory management.
264  MultiAxisPacking = 1
265 };
266 
267 enum class FusedKernelType
268 {
269  AddMulAdd = 0
270 };
271 
272 /// Each backend should implement an IBackend.
273 class IBackend
274 {
275 protected:
276  IBackend() {}
277  virtual ~IBackend() {}
278 
279 public:
280  virtual const BackendId& GetId() const = 0;
281 };
282 
283 using IBackendSharedPtr = std::shared_ptr<IBackend>;
284 using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
285 
286 /// BackendCapability class
287 enum class BackendCapability : uint32_t
288 {
289  /// Constant weights can be accessed through the descriptors,
290  /// On the other hand, non-const weights can be accessed through inputs.
292 
293  /// False: Backends that can only be used if all layers in the graph are supported on that backend.
294  /// True: Backends that can be combined with other backends to run a graph.
295  AllOrNothing,
296 
297  // add new enum values here
298 };
299 
300 /// Device specific knowledge to be passed to the optimizer.
302 {
303 protected:
305  virtual ~IDeviceSpec() {}
306 public:
307  virtual const BackendIdSet& GetSupportedBackends() const = 0;
308 };
309 
310 /// Type of identifiers for bindable layers (inputs, outputs).
311 using LayerBindingId = int;
312 using ImportedInputId = unsigned int;
313 using ImportedOutputId = unsigned int;
314 
315 
317 {
318 public:
319  using ValueType = unsigned int;
320  using SizeType = unsigned int;
321  using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
322  using ConstIterator = typename ArrayType::const_iterator;
323 
324  /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
325  /// when source and target potentially have different memory layouts.
326  ///
327  /// E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels),
328  /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
329  /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
330  /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
331  /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
332  /// [ 0, 2, 3, 1 ].
333  ///
334  /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
335  /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
336  /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
337  /// [ 0, 3, 1, 2 ].
338  ///
339  PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
340 
341  PermutationVector(std::initializer_list<ValueType> dimMappings);
342 
343  ///
344  /// Indexing method with out-of-bounds error checking for the m_DimMappings array.
345  /// @param i - integer value corresponding to index of m_DimMappings array to retrieve element from.
346  /// @return element at index i of m_DimMappings array.
347  /// @throws InvalidArgumentException when indexing out-of-bounds index of m_DimMappings array.
348  ///
350  {
351  if (i >= GetSize())
352  {
353  throw InvalidArgumentException("Invalid indexing of PermutationVector of size " + std::to_string(GetSize())
354  + " at location [" + std::to_string(i) + "].");
355  }
356  return m_DimMappings.at(i);
357  }
358 
359  SizeType GetSize() const { return m_NumDimMappings; }
360 
361  ConstIterator begin() const { return m_DimMappings.begin(); }
362  /**
363  *
364  * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
365  */
366  ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
367 
368  bool IsEqual(const PermutationVector& other) const
369  {
370  if (m_NumDimMappings != other.m_NumDimMappings) return false;
371  for (unsigned int i = 0; i < m_NumDimMappings; ++i)
372  {
373  if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
374  }
375  return true;
376  }
377 
378  bool IsInverse(const PermutationVector& other) const
379  {
380  bool isInverse = (GetSize() == other.GetSize());
381  for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
382  {
383  isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
384  }
385  return isInverse;
386  }
387 
388 private:
389  ArrayType m_DimMappings;
390  /// Number of valid entries in @ref m_DimMappings
391  SizeType m_NumDimMappings;
392 };
393 
394 class ITensorHandle;
395 
396 /// Define the type of callback for the Debug layer to call
397 /// @param guid - guid of layer connected to the input of the Debug layer
398 /// @param slotIndex - index of the output slot connected to the input of the Debug layer
399 /// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
400 using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
401 
402 /// Define a timer and associated inference ID for recording execution times
403 using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
404 using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
405 
406 
407 /// This list uses X macro technique.
408 /// See https://en.wikipedia.org/wiki/X_Macro for more info
409 // New layers should be added at last position to minimize instability.
410 #define LIST_OF_LAYER_TYPE \
411  X(Activation) \
412  X(Addition) \
413  X(ArgMinMax) \
414  X(BatchNormalization) \
415  X(BatchToSpaceNd) \
416  X(Comparison) \
417  X(Concat) \
418  X(Constant) \
419  X(ConvertFp16ToFp32) \
420  X(ConvertFp32ToFp16) \
421  X(Convolution2d) \
422  X(Debug) \
423  X(DepthToSpace) \
424  X(DepthwiseConvolution2d) \
425  X(Dequantize) \
426  X(DetectionPostProcess) \
427  X(Division) \
428  X(ElementwiseUnary) \
429  X(FakeQuantization) \
430  X(Fill) \
431  X(Floor) \
432  X(FullyConnected) \
433  X(Gather) \
434  X(Input) \
435  X(InstanceNormalization) \
436  X(L2Normalization) \
437  X(LogicalBinary) \
438  X(LogSoftmax) \
439  X(Lstm) \
440  X(QLstm) \
441  X(Map) \
442  X(Maximum) \
443  X(Mean) \
444  X(MemCopy) \
445  X(MemImport) \
446  X(Merge) \
447  X(Minimum) \
448  X(Multiplication) \
449  X(Normalization) \
450  X(Output) \
451  X(Pad) \
452  X(Permute) \
453  X(Pooling2d) \
454  X(PreCompiled) \
455  X(Prelu) \
456  X(Quantize) \
457  X(QuantizedLstm) \
458  X(Reshape) \
459  X(Rank) \
460  X(Resize) \
461  X(Reduce) \
462  X(Slice) \
463  X(Softmax) \
464  X(SpaceToBatchNd) \
465  X(SpaceToDepth) \
466  X(Splitter) \
467  X(Stack) \
468  X(StandIn) \
469  X(StridedSlice) \
470  X(Subtraction) \
471  X(Switch) \
472  X(Transpose) \
473  X(TransposeConvolution2d) \
474  X(Unmap) \
475  X(Cast) \
476  X(Shape) \
477  X(UnidirectionalSequenceLstm) \
478  X(ChannelShuffle) \
479  X(Convolution3d) \
480  X(Pooling3d) \
481  X(GatherNd) \
482  X(BatchMatMul) \
483  X(ElementwiseBinary) \
484  X(ReverseV2) \
485  X(Tile) \
486  X(Fused) \
487  X(BroadcastTo) \
488  X(ScatterNd) \
489 // New layers should be added at last position to minimize instability.
490 
491 /// When adding a new layer, adapt also the LastLayer enum value in the
492 /// enum class LayerType below
493 enum class LayerType
494 {
495 #define X(name) name,
497 #undef X
500 };
501 
503 {
504  Update = 0,
505  Add = 1,
506  Sub = 2,
507  Max = 3,
508  Min = 4,
509  Mul = 5
510 };
511 
512 const char* GetLayerTypeAsCString(LayerType type);
513 
514 } // namespace armnn
#define LIST_OF_LAYER_TYPE
This list uses X macro technique.
Definition: Types.hpp:410
arm::pipe::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:26
Each backend should implement an IBackend.
Definition: Types.hpp:274
virtual ~IBackend()
Definition: Types.hpp:277
virtual const BackendId & GetId() const =0
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:302
virtual const BackendIdSet & GetSupportedBackends() const =0
virtual ~IDeviceSpec()
Definition: Types.hpp:305
typename ArrayType::const_iterator ConstIterator
Definition: Types.hpp:322
unsigned int SizeType
Definition: Types.hpp:320
PermutationVector(const ValueType *dimMappings, SizeType numDimMappings)
Definition: Descriptors.cpp:20
SizeType GetSize() const
Definition: Types.hpp:359
ConstIterator end() const
Definition: Types.hpp:366
ValueType operator[](SizeType i) const
Indexing method with out-of-bounds error checking for the m_DimMappings array.
Definition: Types.hpp:349
unsigned int ValueType
Definition: Types.hpp:319
bool IsEqual(const PermutationVector &other) const
Definition: Types.hpp:368
bool IsInverse(const PermutationVector &other) const
Definition: Types.hpp:378
std::array< ValueType, MaxNumOfTensorDimensions > ArrayType
Definition: Types.hpp:321
ConstIterator begin() const
Definition: Types.hpp:361
Copyright (c) 2021 ARM Limited and Contributors.
PaddingMode
The padding mode controls whether the padding should be filled with constant values (Constant),...
Definition: Types.hpp:202
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:246
std::function< void(LayerGuid guid, unsigned int slotIndex, ITensorHandle *tensorHandle)> DebugCallbackFunction
Define the type of callback for the Debug layer to call.
Definition: Types.hpp:400
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:193
unsigned int ImportedInputId
Definition: Types.hpp:312
UnaryOperation
Definition: Types.hpp:126
ComparisonOperation
Definition: Types.hpp:110
LogicalBinaryOperation
Definition: Types.hpp:120
PaddingMethod
The padding method modifies the output of pooling layers.
Definition: Types.hpp:190
@ Exclude
The padding fields don't count and are ignored.
@ IgnoreValue
The padding fields count, but are ignored.
std::chrono::high_resolution_clock::time_point HighResolutionClock
Define a timer and associated inference ID for recording execution times.
Definition: Types.hpp:403
std::pair< HighResolutionClock, HighResolutionClock > InferenceTimingPair
Definition: Types.hpp:404
FusedKernelType
Definition: Types.hpp:268
ActivationFunction
Definition: Types.hpp:87
@ BoundedReLu
min(a, max(b, input)) ReLu1 & ReLu6.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:494
std::unique_ptr< IBackend, void(*)(IBackend *backend)> IBackendUniquePtr
Definition: Types.hpp:284
constexpr unsigned int LOWEST_CAPTURE_PERIOD
The lowest performance data capture interval we support is 10 miliseconds.
Definition: Types.hpp:34
constexpr unsigned int EXPIRE_RATE
Variable to control expire rate of priority queue.
Definition: Types.hpp:37
Status
enumeration
Definition: Types.hpp:43
MemBlockStrategyType
Definition: Types.hpp:255
ScatterNdFunction
Definition: Types.hpp:503
Dimensionality
Definition: Types.hpp:174
QosExecPriority
Definition: Types.hpp:80
PoolingAlgorithm
Definition: Types.hpp:152
ResizeMethod
Definition: Types.hpp:168
BackendCapability
BackendCapability class.
Definition: Types.hpp:288
@ NonConstWeights
Constant weights can be accessed through the descriptors, On the other hand, non-const weights can be...
@ AllOrNothing
False: Backends that can only be used if all layers in the graph are supported on that backend.
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:311
ReduceOperation
Definition: Types.hpp:159
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
NormalizationAlgorithmChannel
Definition: Types.hpp:209
BinaryOperation
Definition: Types.hpp:139
unsigned int ImportedOutputId
Definition: Types.hpp:313
DataLayout
Definition: Types.hpp:63
NormalizationAlgorithmMethod
Definition: Types.hpp:215
@ LocalContrast
Jarret 2009: Local Contrast Normalization.
@ LocalBrightness
Krichevsky 2012: Local Brightness Normalization.
DataType
Definition: Types.hpp:49
const char * GetLayerTypeAsCString(LayerType type)
OutputShapeRounding
Definition: Types.hpp:223
ShapeInferenceMethod
The ShapeInferenceMethod modify how the output shapes are treated.
Definition: Types.hpp:237
@ InferAndValidate
Infer missing output shapes and validate all output shapes.
@ ValidateOnly
Validate all output shapes.
ProfilingDetailsMethod
Define the behaviour of the internal profiler when outputting network details.
Definition: Types.hpp:72
std::shared_ptr< IBackend > IBackendSharedPtr
Definition: Types.hpp:283
ArgMinMaxFunction
Definition: Types.hpp:104