ArmNN
 21.11
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  QSymmS8 = 6,
44  QAsymmS8 = 7,
45  BFloat16 = 8,
46  Signed64 = 9,
47 };
48 
49 enum class DataLayout
50 {
51  NCHW = 1,
52  NHWC = 2,
53  NDHWC = 3,
54  NCDHW = 4
55 };
56 
57 /// Define the behaviour of the internal profiler when outputting network details
59 {
60  Undefined = 0,
62  DetailsOnly = 2
63 };
64 
65 
66 enum class QosExecPriority
67 {
68  Low = 0,
69  Medium = 1,
70  High = 2
71 };
72 
74 {
75  Sigmoid = 0,
76  TanH = 1,
77  Linear = 2,
78  ReLu = 3,
79  BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
80  SoftReLu = 5,
81  LeakyReLu = 6,
82  Abs = 7,
83  Sqrt = 8,
84  Square = 9,
85  Elu = 10,
86  HardSwish = 11
87 };
88 
90 {
91  Min = 0,
92  Max = 1
93 };
94 
96 {
97  Equal = 0,
98  Greater = 1,
99  GreaterOrEqual = 2,
100  Less = 3,
101  LessOrEqual = 4,
102  NotEqual = 5
103 };
104 
106 {
107  LogicalAnd = 0,
108  LogicalOr = 1
109 };
110 
111 enum class UnaryOperation
112 {
113  Abs = 0,
114  Exp = 1,
115  Sqrt = 2,
116  Rsqrt = 3,
117  Neg = 4,
118  LogicalNot = 5,
119  Log = 6,
120  Sin = 7
121 };
122 
124 {
125  Max = 0,
126  Average = 1,
127  L2 = 2
128 };
129 
130 enum class ReduceOperation
131 {
132  Sum = 0,
133  Max = 1,
134  Mean = 2,
135  Min = 3,
136  Prod = 4
137 };
138 
139 enum class ResizeMethod
140 {
141  Bilinear = 0,
142  NearestNeighbor = 1
143 };
144 
145 enum class Dimensionality
146 {
147  NotSpecified = 0,
148  Specified = 1,
149  Scalar = 2
150 };
151 
152 ///
153 /// The padding method modifies the output of pooling layers.
154 /// In both supported methods, the values are ignored (they are
155 /// not even zeroes, which would make a difference for max pooling
156 /// a tensor with negative values). The difference between
157 /// IgnoreValue and Exclude is that the former counts the padding
158 /// fields in the divisor of Average and L2 pooling, while
159 /// Exclude does not.
160 ///
161 enum class PaddingMethod
162 {
163  /// The padding fields count, but are ignored
164  IgnoreValue = 0,
165  /// The padding fields don't count and are ignored
166  Exclude = 1
167 };
168 
169 ///
170 /// The padding mode controls whether the padding should be filled with constant values (Constant), or
171 /// reflect the input, either including the border values (Symmetric) or not (Reflect).
172 ///
173 enum class PaddingMode
174 {
175  Constant = 0,
176  Reflect = 1,
177  Symmetric = 2
178 };
179 
181 {
182  Across = 0,
183  Within = 1
184 };
185 
187 {
188  /// Krichevsky 2012: Local Brightness Normalization
189  LocalBrightness = 0,
190  /// Jarret 2009: Local Contrast Normalization
191  LocalContrast = 1
192 };
193 
195 {
196  Floor = 0,
197  Ceiling = 1
198 };
199 
200 ///
201 /// The ShapeInferenceMethod modify how the output shapes are treated.
202 /// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
203 /// and any mismatch is reported.
204 /// When InferAndValidate is selected 2 actions are performed: (1)infer output shape from inputs and (2)validate the
205 /// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
206 /// specified explicitly, however this information can be calculated from the inputs.
207 ///
209 {
210  /// Validate all output shapes
211  ValidateOnly = 0,
212  /// Infer missing output shapes and validate all output shapes
213  InferAndValidate = 1
214 };
215 
216 /// Define the Memory Source to reduce copies
217 enum class MemorySource : uint32_t
218 {
219  Undefined = 0,
220  Malloc = 1,
221  DmaBuf = 2,
222  DmaBufProtected = 4,
223  Gralloc = 5
224 };
225 
227 {
228  // MemBlocks can be packed on the Y axis only, overlap allowed on X axis.
229  // In other words MemBlocks with overlapping lifetimes cannot use the same MemBin,
230  // equivalent to blob or pooling memory management.
231  SingleAxisPacking = 0,
232 
233  // MemBlocks can be packed on either Y or X axis but cannot overlap on both.
234  // In other words MemBlocks with overlapping lifetimes can use the same MemBin,
235  // equivalent to offset or slab memory management.
236  MultiAxisPacking = 1
237 };
238 
239 /// Each backend should implement an IBackend.
240 class IBackend
241 {
242 protected:
243  IBackend() {}
244  virtual ~IBackend() {}
245 
246 public:
247  virtual const BackendId& GetId() const = 0;
248 };
249 
250 using IBackendSharedPtr = std::shared_ptr<IBackend>;
251 using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
252 
253 /// BackendCapability class
254 enum class BackendCapability : uint32_t
255 {
256  /// Constant weights can be accessed through the descriptors,
257  /// On the other hand, non-const weights can be accessed through inputs.
259 
260  /// Asynchronous Execution.
262 
263  // add new enum values here
264 };
265 
266 /// Device specific knowledge to be passed to the optimizer.
268 {
269 protected:
271  virtual ~IDeviceSpec() {}
272 public:
273  virtual const BackendIdSet& GetSupportedBackends() const = 0;
274 };
275 
276 /// Type of identifiers for bindable layers (inputs, outputs).
277 using LayerBindingId = int;
278 using ImportedInputId = unsigned int;
279 using ImportedOutputId = unsigned int;
280 
281 
283 {
284 public:
285  using ValueType = unsigned int;
286  using SizeType = unsigned int;
287  using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
288  using ConstIterator = typename ArrayType::const_iterator;
289 
290  /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
291  /// when source and target potentially have different memory layouts.
292  ///
293  /// E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels),
294  /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
295  /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
296  /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
297  /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
298  /// [ 0, 2, 3, 1 ].
299  ///
300  /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
301  /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
302  /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
303  /// [ 0, 3, 1, 2 ].
304  ///
305  PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
306 
307  PermutationVector(std::initializer_list<ValueType> dimMappings);
308 
309  ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
310 
311  SizeType GetSize() const { return m_NumDimMappings; }
312 
313  ConstIterator begin() const { return m_DimMappings.begin(); }
314  /**
315  *
316  * @return pointer one past the end of the number of mapping not the length of m_DimMappings.
317  */
318  ConstIterator end() const { return m_DimMappings.begin() + m_NumDimMappings; }
319 
320  bool IsEqual(const PermutationVector& other) const
321  {
322  if (m_NumDimMappings != other.m_NumDimMappings) return false;
323  for (unsigned int i = 0; i < m_NumDimMappings; ++i)
324  {
325  if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
326  }
327  return true;
328  }
329 
330  bool IsInverse(const PermutationVector& other) const
331  {
332  bool isInverse = (GetSize() == other.GetSize());
333  for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
334  {
335  isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
336  }
337  return isInverse;
338  }
339 
340 private:
341  ArrayType m_DimMappings;
342  /// Number of valid entries in @ref m_DimMappings
343  SizeType m_NumDimMappings;
344 };
345 
346 namespace profiling { class ProfilingGuid; }
347 
348 /// Define LayerGuid type.
349 using LayerGuid = profiling::ProfilingGuid;
350 
351 class ITensorHandle;
352 
353 /// Define the type of callback for the Debug layer to call
354 /// @param guid - guid of layer connected to the input of the Debug layer
355 /// @param slotIndex - index of the output slot connected to the input of the Debug layer
356 /// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
357 using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
358 
359 /// Define a timer and associated inference ID for recording execution times
360 using HighResolutionClock = std::chrono::high_resolution_clock::time_point;
361 using InferenceTimingPair = std::pair<HighResolutionClock, HighResolutionClock>;
362 
363 
364 /// This list uses X macro technique.
365 /// See https://en.wikipedia.org/wiki/X_Macro for more info
366 #define LIST_OF_LAYER_TYPE \
367  X(Activation) \
368  X(Addition) \
369  X(ArgMinMax) \
370  X(BatchNormalization) \
371  X(BatchToSpaceNd) \
372  X(Comparison) \
373  X(Concat) \
374  X(Constant) \
375  X(ConvertBf16ToFp32) \
376  X(ConvertFp16ToFp32) \
377  X(ConvertFp32ToBf16) \
378  X(ConvertFp32ToFp16) \
379  X(Convolution2d) \
380  X(Debug) \
381  X(DepthToSpace) \
382  X(DepthwiseConvolution2d) \
383  X(Dequantize) \
384  X(DetectionPostProcess) \
385  X(Division) \
386  X(ElementwiseUnary) \
387  X(FakeQuantization) \
388  X(Fill) \
389  X(Floor) \
390  X(FullyConnected) \
391  X(Gather) \
392  X(Input) \
393  X(InstanceNormalization) \
394  X(L2Normalization) \
395  X(LogicalBinary) \
396  X(LogSoftmax) \
397  X(Lstm) \
398  X(QLstm) \
399  X(Map) \
400  X(Maximum) \
401  X(Mean) \
402  X(MemCopy) \
403  X(MemImport) \
404  X(Merge) \
405  X(Minimum) \
406  X(Multiplication) \
407  X(Normalization) \
408  X(Output) \
409  X(Pad) \
410  X(Permute) \
411  X(Pooling2d) \
412  X(PreCompiled) \
413  X(Prelu) \
414  X(Quantize) \
415  X(QuantizedLstm) \
416  X(Reshape) \
417  X(Rank) \
418  X(Resize) \
419  X(Reduce) \
420  X(Slice) \
421  X(Softmax) \
422  X(SpaceToBatchNd) \
423  X(SpaceToDepth) \
424  X(Splitter) \
425  X(Stack) \
426  X(StandIn) \
427  X(StridedSlice) \
428  X(Subtraction) \
429  X(Switch) \
430  X(Transpose) \
431  X(TransposeConvolution2d) \
432  X(Unmap) \
433  X(Cast) \
434  X(Shape) \
435  X(UnidirectionalSequenceLstm) \
436  X(ChannelShuffle) \
437  X(Convolution3d) \
438 
439 // New layers should be added at last to minimize instability.
440 
441 /// When adding a new layer, adapt also the LastLayer enum value in the
442 /// enum class LayerType below
443 enum class LayerType
444 {
445 #define X(name) name,
447 #undef X
450 };
451 
452 const char* GetLayerTypeAsCString(LayerType type);
453 
454 } // namespace armnn
unsigned int ValueType
Definition: Types.hpp:285
Dimensionality
Definition: Types.hpp:145
DataLayout
Definition: Types.hpp:49
std::chrono::high_resolution_clock::time_point HighResolutionClock
Define a timer and associated inference ID for recording execution times.
Definition: Types.hpp:360
virtual ~IBackend()
Definition: Types.hpp:244
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:193
unsigned int ImportedOutputId
Definition: Types.hpp:279
typename ArrayType::const_iterator ConstIterator
Definition: Types.hpp:288
Each backend should implement an IBackend.
Definition: Types.hpp:240
The padding fields don&#39;t count and are ignored.
#define LIST_OF_LAYER_TYPE
This list uses X macro technique.
Definition: Types.hpp:366
NormalizationAlgorithmChannel
Definition: Types.hpp:180
Copyright (c) 2021 ARM Limited and Contributors.
SizeType GetSize() const
Definition: Types.hpp:311
PoolingAlgorithm
Definition: Types.hpp:123
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:357
LogicalBinaryOperation
Definition: Types.hpp:105
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:277
ProfilingDetailsMethod
Define the behaviour of the internal profiler when outputting network details.
Definition: Types.hpp:58
PaddingMethod
The padding method modifies the output of pooling layers.
Definition: Types.hpp:161
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:250
ComparisonOperation
Definition: Types.hpp:95
ReduceOperation
Definition: Types.hpp:130
DataType
Definition: Types.hpp:35
BackendCapability
BackendCapability class.
Definition: Types.hpp:254
std::array< ValueType, MaxNumOfTensorDimensions > ArrayType
Definition: Types.hpp:287
Validate all output shapes.
Status
enumeration
Definition: Types.hpp:29
virtual ~IDeviceSpec()
Definition: Types.hpp:271
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:267
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:251
PaddingMode
The padding mode controls whether the padding should be filled with constant values (Constant)...
Definition: Types.hpp:173
min(a, max(b, input)) ReLu1 & ReLu6.
unsigned int SizeType
Definition: Types.hpp:286
ValueType operator[](SizeType i) const
Definition: Types.hpp:309
OutputShapeRounding
Definition: Types.hpp:194
unsigned int ImportedInputId
Definition: Types.hpp:278
bool IsEqual(const PermutationVector &other) const
Definition: Types.hpp:320
The padding fields count, but are ignored.
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:217
Jarret 2009: Local Contrast Normalization.
ArgMinMaxFunction
Definition: Types.hpp:89
ConstIterator begin() const
Definition: Types.hpp:313
profiling::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:349
ResizeMethod
Definition: Types.hpp:139
ConstIterator end() const
Definition: Types.hpp:318
UnaryOperation
Definition: Types.hpp:111
Infer missing output shapes and validate all output shapes.
QosExecPriority
Definition: Types.hpp:66
MemBlockStrategyType
Definition: Types.hpp:226
Krichevsky 2012: Local Brightness Normalization.
bool IsInverse(const PermutationVector &other) const
Definition: Types.hpp:330
NormalizationAlgorithmMethod
Definition: Types.hpp:186
ShapeInferenceMethod
The ShapeInferenceMethod modify how the output shapes are treated.
Definition: Types.hpp:208
const char * GetLayerTypeAsCString(LayerType type)
std::pair< HighResolutionClock, HighResolutionClock > InferenceTimingPair
Definition: Types.hpp:361
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:18
ActivationFunction
Definition: Types.hpp:73
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:443