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