ArmNN
 21.08
Types.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 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 armnn
16 {
17 
18 constexpr unsigned int MaxNumOfTensorDimensions = 5U;
19 
20 /// The lowest performance data capture interval we support is 10 miliseconds.
21 constexpr unsigned int LOWEST_CAPTURE_PERIOD = 10000u;
22 
23 /// Variable to control expire rate of priority queue
24 constexpr unsigned int EXPIRE_RATE = 3U;
25 
26 /// @enum Status enumeration
27 /// @var Status::Successful
28 /// @var Status::Failure
29 enum class Status
30 {
31  Success = 0,
32  Failure = 1
33 };
34 
35 enum class DataType
36 {
37  Float16 = 0,
38  Float32 = 1,
39  QAsymmU8 = 2,
40  Signed32 = 3,
41  Boolean = 4,
42  QSymmS16 = 5,
43  QuantizedSymm8PerAxis ARMNN_DEPRECATED_ENUM_MSG("Per Axis property inferred by number of scales in TensorInfo") = 6,
44  QSymmS8 = 7,
45  QAsymmS8 = 8,
46  BFloat16 = 9,
47  Signed64 = 10,
48 
49  QuantisedAsymm8 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QAsymmU8 instead.") = QAsymmU8,
50  QuantisedSymm16 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QSymmS16 instead.") = QSymmS16
51 };
52 
53 enum class DataLayout
54 {
55  NCHW = 1,
56  NHWC = 2
57 };
58 
59 enum class QosExecPriority
60 {
61  Low = 0,
62  Medium = 1,
63  High = 2
64 };
65 
67 {
68  Sigmoid = 0,
69  TanH = 1,
70  Linear = 2,
71  ReLu = 3,
72  BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
73  SoftReLu = 5,
74  LeakyReLu = 6,
75  Abs = 7,
76  Sqrt = 8,
77  Square = 9,
78  Elu = 10,
79  HardSwish = 11
80 };
81 
83 {
84  Min = 0,
85  Max = 1
86 };
87 
89 {
90  Equal = 0,
91  Greater = 1,
92  GreaterOrEqual = 2,
93  Less = 3,
94  LessOrEqual = 4,
95  NotEqual = 5
96 };
97 
99 {
100  LogicalAnd = 0,
101  LogicalOr = 1
102 };
103 
104 enum class UnaryOperation
105 {
106  Abs = 0,
107  Exp = 1,
108  Sqrt = 2,
109  Rsqrt = 3,
110  Neg = 4,
111  LogicalNot = 5,
112  Log = 6,
113  Sin = 7
114 };
115 
117 {
118  Max = 0,
119  Average = 1,
120  L2 = 2
121 };
122 
123 enum class ReduceOperation
124 {
125  Sum = 0,
126  Max = 1,
127  Mean = 2,
128  Min = 3
129 };
130 
131 enum class ResizeMethod
132 {
133  Bilinear = 0,
134  NearestNeighbor = 1
135 };
136 
137 enum class Dimensionality
138 {
139  NotSpecified = 0,
140  Specified = 1,
141  Scalar = 2
142 };
143 
144 ///
145 /// The padding method modifies the output of pooling layers.
146 /// In both supported methods, the values are ignored (they are
147 /// not even zeroes, which would make a difference for max pooling
148 /// a tensor with negative values). The difference between
149 /// IgnoreValue and Exclude is that the former counts the padding
150 /// fields in the divisor of Average and L2 pooling, while
151 /// Exclude does not.
152 ///
153 enum class PaddingMethod
154 {
155  /// The padding fields count, but are ignored
156  IgnoreValue = 0,
157  /// The padding fields don't count and are ignored
158  Exclude = 1
159 };
160 
162 {
163  Across = 0,
164  Within = 1
165 };
166 
168 {
169  /// Krichevsky 2012: Local Brightness Normalization
170  LocalBrightness = 0,
171  /// Jarret 2009: Local Contrast Normalization
172  LocalContrast = 1
173 };
174 
176 {
177  Floor = 0,
178  Ceiling = 1
179 };
180 
181 ///
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 ///
190 {
191  /// Validate all output shapes
192  ValidateOnly = 0,
193  /// Infer missing output shapes and validate all output shapes
194  InferAndValidate = 1
195 };
196 
197 /// Define the Memory Source to reduce copies
198 enum class MemorySource : uint32_t
199 {
200  Undefined = 0,
201  Malloc = 1,
202  DmaBuf = 2,
203  DmaBufProtected = 4
204 };
205 
206 /// Each backend should implement an IBackend.
207 class IBackend
208 {
209 protected:
210  IBackend() {}
211  virtual ~IBackend() {}
212 
213 public:
214  virtual const BackendId& GetId() const = 0;
215 };
216 
217 using IBackendSharedPtr = std::shared_ptr<IBackend>;
218 using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
219 
220 /// BackendCapability class
221 enum 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.
226 
227  /// Asynchronous Execution.
229 
230  // add new enum values here
231 };
232 
233 /// Device specific knowledge to be passed to the optimizer.
235 {
236 protected:
238  virtual ~IDeviceSpec() {}
239 public:
240  virtual const BackendIdSet& GetSupportedBackends() const = 0;
241 };
242 
243 /// Type of identifiers for bindable layers (inputs, outputs).
244 using LayerBindingId = int;
245 
247 {
248 public:
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 
254  /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
255  /// when source and target potentially have different memory layouts.
256  ///
257  /// E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels),
258  /// 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(); }
278  /**
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; }
283 
284  bool IsEqual(const PermutationVector& other) const
285  {
286  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;
292  }
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 
304 private:
305  ArrayType m_DimMappings;
306  /// Number of valid entries in @ref m_DimMappings
307  SizeType m_NumDimMappings;
308 };
309 
310 namespace profiling { class ProfilingGuid; }
311 
312 /// Define LayerGuid type.
313 using LayerGuid = profiling::ProfilingGuid;
314 
315 class ITensorHandle;
316 
317 /// 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
321 using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
322 
323 /// Define a timer and associated inference ID for recording execution times
324 using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
325 using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
326 
327 
328 /// 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) \
335  X(BatchToSpaceNd) \
336  X(Comparison) \
337  X(Concat) \
338  X(Constant) \
339  X(ConvertBf16ToFp32) \
340  X(ConvertFp16ToFp32) \
341  X(ConvertFp32ToBf16) \
342  X(ConvertFp32ToFp16) \
343  X(Convolution2d) \
344  X(Debug) \
345  X(DepthToSpace) \
346  X(DepthwiseConvolution2d) \
347  X(Dequantize) \
348  X(DetectionPostProcess) \
349  X(Division) \
350  X(ElementwiseUnary) \
351  X(FakeQuantization) \
352  X(Fill) \
353  X(Floor) \
354  X(FullyConnected) \
355  X(Gather) \
356  X(Input) \
357  X(InstanceNormalization) \
358  X(L2Normalization) \
359  X(LogicalBinary) \
360  X(LogSoftmax) \
361  X(Lstm) \
362  X(QLstm) \
363  X(Map) \
364  X(Maximum) \
365  X(Mean) \
366  X(MemCopy) \
367  X(MemImport) \
368  X(Merge) \
369  X(Minimum) \
370  X(Multiplication) \
371  X(Normalization) \
372  X(Output) \
373  X(Pad) \
374  X(Permute) \
375  X(Pooling2d) \
376  X(PreCompiled) \
377  X(Prelu) \
378  X(Quantize) \
379  X(QuantizedLstm) \
380  X(Reshape) \
381  X(Rank) \
382  X(Resize) \
383  X(Reduce) \
384  X(Slice) \
385  X(Softmax) \
386  X(SpaceToBatchNd) \
387  X(SpaceToDepth) \
388  X(Splitter) \
389  X(Stack) \
390  X(StandIn) \
391  X(StridedSlice) \
392  X(Subtraction) \
393  X(Switch) \
394  X(Transpose) \
395  X(TransposeConvolution2d) \
396  X(Unmap) \
397  X(Cast) \
398  X(Shape) \
399  X(UnidirectionalSequenceLstm) \
400 
401 // New layers should be added at last to minimize instability.
402 
403 /// When adding a new layer, adapt also the LastLayer enum value in the
404 /// enum class LayerType below
405 enum class LayerType
406 {
407 #define X(name) name,
409 #undef X
412 };
413 
414 const char* GetLayerTypeAsCString(LayerType type);
415 
416 } // namespace armnn
unsigned int ValueType
Definition: Types.hpp:249
Dimensionality
Definition: Types.hpp:137
DataLayout
Definition: Types.hpp:53
std::chrono::high_resolution_clock::time_point HighResolutionClock
Define a timer and associated inference ID for recording execution times.
Definition: Types.hpp:324
virtual ~IBackend()
Definition: Types.hpp:211
constexpr unsigned int EXPIRE_RATE
Variable to control expire rate of priority queue.
Definition: Types.hpp:24
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
typename ArrayType::const_iterator ConstIterator
Definition: Types.hpp:252
Each backend should implement an IBackend.
Definition: Types.hpp:207
The padding fields don&#39;t count and are ignored.
#define LIST_OF_LAYER_TYPE
This list uses X macro technique.
Definition: Types.hpp:330
NormalizationAlgorithmChannel
Definition: Types.hpp:161
Copyright (c) 2021 ARM Limited and Contributors.
SizeType GetSize() const
Definition: Types.hpp:275
PoolingAlgorithm
Definition: Types.hpp:116
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:321
LogicalBinaryOperation
Definition: Types.hpp:98
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:244
PaddingMethod
The padding method modifies the output of pooling layers.
Definition: Types.hpp:153
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:217
ComparisonOperation
Definition: Types.hpp:88
ReduceOperation
Definition: Types.hpp:123
DataType
Definition: Types.hpp:35
#define ARMNN_DEPRECATED_ENUM_MSG(message)
Definition: Deprecated.hpp:50
BackendCapability
BackendCapability class.
Definition: Types.hpp:221
std::array< ValueType, MaxNumOfTensorDimensions > ArrayType
Definition: Types.hpp:251
Validate all output shapes.
Status
enumeration
Definition: Types.hpp:29
virtual ~IDeviceSpec()
Definition: Types.hpp:238
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:234
constexpr unsigned int LOWEST_CAPTURE_PERIOD
The lowest performance data capture interval we support is 10 miliseconds.
Definition: Types.hpp:21
std::unique_ptr< IBackend, void(*)(IBackend *backend)> IBackendUniquePtr
Definition: Types.hpp:218
min(a, max(b, input)) ReLu1 & ReLu6.
unsigned int SizeType
Definition: Types.hpp:250
ValueType operator[](SizeType i) const
Definition: Types.hpp:273
OutputShapeRounding
Definition: Types.hpp:175
bool IsEqual(const PermutationVector &other) const
Definition: Types.hpp:284
The padding fields count, but are ignored.
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:198
Jarret 2009: Local Contrast Normalization.
ArgMinMaxFunction
Definition: Types.hpp:82
ConstIterator begin() const
Definition: Types.hpp:277
profiling::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:313
ResizeMethod
Definition: Types.hpp:131
ConstIterator end() const
Definition: Types.hpp:282
UnaryOperation
Definition: Types.hpp:104
Infer missing output shapes and validate all output shapes.
QosExecPriority
Definition: Types.hpp:59
Krichevsky 2012: Local Brightness Normalization.
bool IsInverse(const PermutationVector &other) const
Definition: Types.hpp:294
NormalizationAlgorithmMethod
Definition: Types.hpp:167
ShapeInferenceMethod
The ShapeInferenceMethod modify how the output shapes are treated.
Definition: Types.hpp:189
const char * GetLayerTypeAsCString(LayerType type)
std::pair< HighResolutionClock, HighResolutionClock > InferenceTimingPair
Definition: Types.hpp:325
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:18
ActivationFunction
Definition: Types.hpp:66
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:405