blob: 7c8a533e60bae7558bdb153d3ef430f1f30d6185 [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
David Beck9efb57d2018-11-05 13:40:33 +0000146/// Each backend should implement an IBackend.
147class IBackend
148{
149protected:
150 IBackend() {}
151 virtual ~IBackend() {}
152
153public:
154 virtual const BackendId& GetId() const = 0;
155};
156
157using IBackendSharedPtr = std::shared_ptr<IBackend>;
158using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
159
David Beckdcb751f2018-10-03 11:42:42 +0100160/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100161class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000162{
telsoa01c577f2c2018-08-31 09:22:23 +0100163protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000164 IDeviceSpec() {}
165 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100166public:
167 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000168};
169
170/// Type of identifiers for bindable layers (inputs, outputs).
171using LayerBindingId = int;
172
173class PermutationVector
174{
175public:
176 using ValueType = unsigned int;
177 using SizeType = unsigned int;
178 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
179 using ConstIterator = typename ArrayType::const_iterator;
180
telsoa01c577f2c2018-08-31 09:22:23 +0100181 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000182 /// when source and target potentially have different memory layouts.
183 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100184 /// 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 +0000185 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
186 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
187 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
188 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
189 /// [ 0, 2, 3, 1 ].
190 ///
191 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
192 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
193 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
194 /// [ 0, 3, 1, 2 ].
195 ///
196 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
197
198 PermutationVector(std::initializer_list<ValueType> dimMappings);
199
200 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
201
202 SizeType GetSize() const { return m_NumDimMappings; }
203
204 ConstIterator begin() const { return m_DimMappings.begin(); }
205 ConstIterator end() const { return m_DimMappings.end(); }
206
207 bool IsEqual(const PermutationVector& other) const
208 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100209 if (m_NumDimMappings != other.m_NumDimMappings) return false;
210 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
211 {
212 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
213 }
214 return true;
telsoa014fcda012018-03-09 14:13:49 +0000215 }
216
217 bool IsInverse(const PermutationVector& other) const
218 {
219 bool isInverse = (GetSize() == other.GetSize());
220 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
221 {
222 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
223 }
224 return isInverse;
225 }
226
227private:
228 ArrayType m_DimMappings;
229 /// Number of valid entries in @ref m_DimMappings
230 SizeType m_NumDimMappings;
231};
232
janeil013fec1ea2019-11-07 09:47:20 +0000233namespace profiling { class ProfilingGuid; }
234
telsoa01c577f2c2018-08-31 09:22:23 +0100235/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000236using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100237
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000238class ITensorHandle;
239
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000240/// Define the type of callback for the Debug layer to call
241/// @param guid - guid of layer connected to the input of the Debug layer
242/// @param slotIndex - index of the output slot connected to the input of the Debug layer
243/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
244using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000245
janeil01c4946c72019-11-07 09:32:28 +0000246
247namespace profiling
248{
249
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000250static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63;
251
janeil01c4946c72019-11-07 09:32:28 +0000252class ProfilingGuid
253{
254public:
Sadik Armagan3184c902020-03-18 10:57:30 +0000255 ProfilingGuid() : m_Guid(0) {}
256
janeil01c4946c72019-11-07 09:32:28 +0000257 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
258
259 operator uint64_t() const { return m_Guid; }
260
261 bool operator==(const ProfilingGuid& other) const
262 {
263 return m_Guid == other.m_Guid;
264 }
265
266 bool operator!=(const ProfilingGuid& other) const
267 {
268 return m_Guid != other.m_Guid;
269 }
270
271 bool operator<(const ProfilingGuid& other) const
272 {
273 return m_Guid < other.m_Guid;
274 }
275
276 bool operator<=(const ProfilingGuid& other) const
277 {
278 return m_Guid <= other.m_Guid;
279 }
280
281 bool operator>(const ProfilingGuid& other) const
282 {
283 return m_Guid > other.m_Guid;
284 }
285
286 bool operator>=(const ProfilingGuid& other) const
287 {
288 return m_Guid >= other.m_Guid;
289 }
290
291protected:
292 uint64_t m_Guid;
293};
294
295/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
296struct ProfilingDynamicGuid : public ProfilingGuid
297{
298 using ProfilingGuid::ProfilingGuid;
299};
300
301struct ProfilingStaticGuid : public ProfilingGuid
302{
303 using ProfilingGuid::ProfilingGuid;
304};
305
306} // namespace profiling
307
David Beck9df2d952018-10-10 15:11:44 +0100308} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000309
310
311namespace std
312{
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000313/// make ProfilingGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000314template<>
315struct hash<armnn::profiling::ProfilingGuid>
316{
317 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
318 {
319 return hash<uint64_t>()(uint64_t(guid));
320 }
321};
322
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000323/// make ProfilingDynamicGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000324template<>
325struct hash<armnn::profiling::ProfilingDynamicGuid>
326{
327 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
328 {
329 return hash<uint64_t>()(uint64_t(guid));
330 }
331};
332
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000333/// make ProfilingStaticGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000334template<>
335struct hash<armnn::profiling::ProfilingStaticGuid>
336{
337 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
338 {
339 return hash<uint64_t>()(uint64_t(guid));
340 }
341};
janeil013fec1ea2019-11-07 09:47:20 +0000342} // namespace std