blob: fb6f13476695bda033caaf9e9267803b9a9a5b98 [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,
Derek Lambertif90c56d2020-01-10 17:14:08 +000044
Derek Lamberti41e92b02020-01-21 13:43:21 +000045 QuantisedAsymm8 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QAsymmU8 instead.") = QAsymmU8,
46 QuantisedSymm16 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QSymmS16 instead.") = QSymmS16
telsoa014fcda012018-03-09 14:13:49 +000047};
48
Derek Lamberti0cff1632018-09-18 16:02:25 +010049enum class DataLayout
50{
51 NCHW = 1,
52 NHWC = 2
53};
54
telsoa014fcda012018-03-09 14:13:49 +000055enum class ActivationFunction
56{
57 Sigmoid = 0,
58 TanH = 1,
59 Linear = 2,
60 ReLu = 3,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000061 BoundedReLu = 4, ///< min(a, max(b, input)) ReLu1 & ReLu6.
telsoa014fcda012018-03-09 14:13:49 +000062 SoftReLu = 5,
63 LeakyReLu = 6,
64 Abs = 7,
65 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000066 Square = 9,
Colm Donelan03fbeaf2020-02-26 15:39:23 +000067 Elu = 10,
68 HardSwish = 11
telsoa014fcda012018-03-09 14:13:49 +000069};
70
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010071enum class ArgMinMaxFunction
72{
73 Min = 0,
74 Max = 1
75};
76
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010077enum class ComparisonOperation
78{
79 Equal = 0,
80 Greater = 1,
81 GreaterOrEqual = 2,
82 Less = 3,
83 LessOrEqual = 4,
84 NotEqual = 5
85};
86
josh minor4a3c6102020-01-06 16:40:46 -060087enum class UnaryOperation
88{
89 Abs = 0,
90 Exp = 1,
91 Sqrt = 2,
92 Rsqrt = 3,
93 Neg = 4
94};
95
telsoa014fcda012018-03-09 14:13:49 +000096enum class PoolingAlgorithm
97{
98 Max = 0,
99 Average = 1,
100 L2 = 2
101};
102
Teresa Charlina9075df2019-06-27 15:41:57 +0100103enum class ResizeMethod
104{
105 Bilinear = 0,
106 NearestNeighbor = 1
107};
108
telsoa014fcda012018-03-09 14:13:49 +0000109///
110/// The padding method modifies the output of pooling layers.
111/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100112/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000113/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100114/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000115/// fields in the divisor of Average and L2 pooling, while
116/// Exclude does not.
117///
118enum class PaddingMethod
119{
telsoa01c577f2c2018-08-31 09:22:23 +0100120 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100121 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100122 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100123 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000124};
125
126enum class NormalizationAlgorithmChannel
127{
128 Across = 0,
129 Within = 1
130};
131
132enum class NormalizationAlgorithmMethod
133{
David Beckdcb751f2018-10-03 11:42:42 +0100134 /// Krichevsky 2012: Local Brightness Normalization
135 LocalBrightness = 0,
136 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100137 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000138};
139
140enum class OutputShapeRounding
141{
142 Floor = 0,
143 Ceiling = 1
144};
145
Teresa Charlincdc01492020-06-09 18:00:20 +0100146///
147/// The ShapeInferenceMethod modify how the output shapes are treated.
148/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
149/// and any mismatch is reported.
150/// When InferAndValidate is selected 2 actions must be performed: (1)infer output shape from inputs and (2)validate the
151/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
152/// specified explicitly, however this information can be calculated from the inputs.
153///
154enum class ShapeInferenceMethod
155{
156 /// Validate all output shapes
157 ValidateOnly = 0,
158 /// Infer missing output shapes and validate all output shapes
159 InferAndValidate = 1
160};
161
David Beck9efb57d2018-11-05 13:40:33 +0000162/// Each backend should implement an IBackend.
163class IBackend
164{
165protected:
166 IBackend() {}
167 virtual ~IBackend() {}
168
169public:
170 virtual const BackendId& GetId() const = 0;
171};
172
173using IBackendSharedPtr = std::shared_ptr<IBackend>;
174using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
175
David Beckdcb751f2018-10-03 11:42:42 +0100176/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100177class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000178{
telsoa01c577f2c2018-08-31 09:22:23 +0100179protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000180 IDeviceSpec() {}
181 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100182public:
183 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000184};
185
186/// Type of identifiers for bindable layers (inputs, outputs).
187using LayerBindingId = int;
188
189class PermutationVector
190{
191public:
192 using ValueType = unsigned int;
193 using SizeType = unsigned int;
194 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
195 using ConstIterator = typename ArrayType::const_iterator;
196
telsoa01c577f2c2018-08-31 09:22:23 +0100197 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000198 /// when source and target potentially have different memory layouts.
199 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100200 /// 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 +0000201 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
202 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
203 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
204 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
205 /// [ 0, 2, 3, 1 ].
206 ///
207 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
208 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
209 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
210 /// [ 0, 3, 1, 2 ].
211 ///
212 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
213
214 PermutationVector(std::initializer_list<ValueType> dimMappings);
215
216 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
217
218 SizeType GetSize() const { return m_NumDimMappings; }
219
220 ConstIterator begin() const { return m_DimMappings.begin(); }
221 ConstIterator end() const { return m_DimMappings.end(); }
222
223 bool IsEqual(const PermutationVector& other) const
224 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100225 if (m_NumDimMappings != other.m_NumDimMappings) return false;
226 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
227 {
228 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
229 }
230 return true;
telsoa014fcda012018-03-09 14:13:49 +0000231 }
232
233 bool IsInverse(const PermutationVector& other) const
234 {
235 bool isInverse = (GetSize() == other.GetSize());
236 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
237 {
238 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
239 }
240 return isInverse;
241 }
242
243private:
244 ArrayType m_DimMappings;
245 /// Number of valid entries in @ref m_DimMappings
246 SizeType m_NumDimMappings;
247};
248
janeil013fec1ea2019-11-07 09:47:20 +0000249namespace profiling { class ProfilingGuid; }
250
telsoa01c577f2c2018-08-31 09:22:23 +0100251/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000252using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100253
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000254class ITensorHandle;
255
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000256/// Define the type of callback for the Debug layer to call
257/// @param guid - guid of layer connected to the input of the Debug layer
258/// @param slotIndex - index of the output slot connected to the input of the Debug layer
259/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
260using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000261
janeil01c4946c72019-11-07 09:32:28 +0000262
263namespace profiling
264{
265
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000266static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63;
267
janeil01c4946c72019-11-07 09:32:28 +0000268class ProfilingGuid
269{
270public:
Sadik Armagan3184c902020-03-18 10:57:30 +0000271 ProfilingGuid() : m_Guid(0) {}
272
janeil01c4946c72019-11-07 09:32:28 +0000273 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
274
275 operator uint64_t() const { return m_Guid; }
276
277 bool operator==(const ProfilingGuid& other) const
278 {
279 return m_Guid == other.m_Guid;
280 }
281
282 bool operator!=(const ProfilingGuid& other) const
283 {
284 return m_Guid != other.m_Guid;
285 }
286
287 bool operator<(const ProfilingGuid& other) const
288 {
289 return m_Guid < other.m_Guid;
290 }
291
292 bool operator<=(const ProfilingGuid& other) const
293 {
294 return m_Guid <= other.m_Guid;
295 }
296
297 bool operator>(const ProfilingGuid& other) const
298 {
299 return m_Guid > other.m_Guid;
300 }
301
302 bool operator>=(const ProfilingGuid& other) const
303 {
304 return m_Guid >= other.m_Guid;
305 }
306
307protected:
308 uint64_t m_Guid;
309};
310
311/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
312struct ProfilingDynamicGuid : public ProfilingGuid
313{
314 using ProfilingGuid::ProfilingGuid;
315};
316
317struct ProfilingStaticGuid : public ProfilingGuid
318{
319 using ProfilingGuid::ProfilingGuid;
320};
321
322} // namespace profiling
323
David Beck9df2d952018-10-10 15:11:44 +0100324} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000325
326
327namespace std
328{
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000329/// make ProfilingGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000330template<>
331struct hash<armnn::profiling::ProfilingGuid>
332{
333 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
334 {
335 return hash<uint64_t>()(uint64_t(guid));
336 }
337};
338
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000339/// make ProfilingDynamicGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000340template<>
341struct hash<armnn::profiling::ProfilingDynamicGuid>
342{
343 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
344 {
345 return hash<uint64_t>()(uint64_t(guid));
346 }
347};
348
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000349/// make ProfilingStaticGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000350template<>
351struct hash<armnn::profiling::ProfilingStaticGuid>
352{
353 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
354 {
355 return hash<uint64_t>()(uint64_t(guid));
356 }
357};
janeil013fec1ea2019-11-07 09:47:20 +0000358} // namespace std