blob: b00a8bf986adaf64982d50198398dd5d15737489 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// 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
Colm Donelan02705242019-11-14 14:19:07 +000020// The lowest performance data capture interval we support is 10 miliseconds.
21constexpr 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,
Derek Lambertif90c56d2020-01-10 17:14:08 +000043
Derek Lamberti41e92b02020-01-21 13:43:21 +000044 QuantisedAsymm8 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QAsymmU8 instead.") = QAsymmU8,
45 QuantisedSymm16 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QSymmS16 instead.") = QSymmS16
telsoa014fcda012018-03-09 14:13:49 +000046};
47
Derek Lamberti0cff1632018-09-18 16:02:25 +010048enum class DataLayout
49{
50 NCHW = 1,
51 NHWC = 2
52};
53
telsoa014fcda012018-03-09 14:13:49 +000054enum class ActivationFunction
55{
56 Sigmoid = 0,
57 TanH = 1,
58 Linear = 2,
59 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010060 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000061 SoftReLu = 5,
62 LeakyReLu = 6,
63 Abs = 7,
64 Sqrt = 8,
David Monahan3b3c3812020-02-25 09:03:29 +000065 Square = 9,
66 Elu = 10
telsoa014fcda012018-03-09 14:13:49 +000067};
68
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010069enum class ArgMinMaxFunction
70{
71 Min = 0,
72 Max = 1
73};
74
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010075enum class ComparisonOperation
76{
77 Equal = 0,
78 Greater = 1,
79 GreaterOrEqual = 2,
80 Less = 3,
81 LessOrEqual = 4,
82 NotEqual = 5
83};
84
josh minor4a3c6102020-01-06 16:40:46 -060085enum class UnaryOperation
86{
87 Abs = 0,
88 Exp = 1,
89 Sqrt = 2,
90 Rsqrt = 3,
91 Neg = 4
92};
93
telsoa014fcda012018-03-09 14:13:49 +000094enum class PoolingAlgorithm
95{
96 Max = 0,
97 Average = 1,
98 L2 = 2
99};
100
Teresa Charlina9075df2019-06-27 15:41:57 +0100101enum class ResizeMethod
102{
103 Bilinear = 0,
104 NearestNeighbor = 1
105};
106
telsoa014fcda012018-03-09 14:13:49 +0000107///
108/// The padding method modifies the output of pooling layers.
109/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100110/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000111/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100112/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000113/// fields in the divisor of Average and L2 pooling, while
114/// Exclude does not.
115///
116enum class PaddingMethod
117{
telsoa01c577f2c2018-08-31 09:22:23 +0100118 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100119 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100120 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100121 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000122};
123
124enum class NormalizationAlgorithmChannel
125{
126 Across = 0,
127 Within = 1
128};
129
130enum class NormalizationAlgorithmMethod
131{
David Beckdcb751f2018-10-03 11:42:42 +0100132 /// Krichevsky 2012: Local Brightness Normalization
133 LocalBrightness = 0,
134 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100135 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000136};
137
138enum class OutputShapeRounding
139{
140 Floor = 0,
141 Ceiling = 1
142};
143
David Beck9efb57d2018-11-05 13:40:33 +0000144/// Each backend should implement an IBackend.
145class IBackend
146{
147protected:
148 IBackend() {}
149 virtual ~IBackend() {}
150
151public:
152 virtual const BackendId& GetId() const = 0;
153};
154
155using IBackendSharedPtr = std::shared_ptr<IBackend>;
156using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
157
David Beckdcb751f2018-10-03 11:42:42 +0100158/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100159class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000160{
telsoa01c577f2c2018-08-31 09:22:23 +0100161protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000162 IDeviceSpec() {}
163 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100164public:
165 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000166};
167
168/// Type of identifiers for bindable layers (inputs, outputs).
169using LayerBindingId = int;
170
171class PermutationVector
172{
173public:
174 using ValueType = unsigned int;
175 using SizeType = unsigned int;
176 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
177 using ConstIterator = typename ArrayType::const_iterator;
178
telsoa01c577f2c2018-08-31 09:22:23 +0100179 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000180 /// when source and target potentially have different memory layouts.
181 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100182 /// 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 +0000183 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
184 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
185 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
186 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
187 /// [ 0, 2, 3, 1 ].
188 ///
189 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
190 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
191 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
192 /// [ 0, 3, 1, 2 ].
193 ///
194 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
195
196 PermutationVector(std::initializer_list<ValueType> dimMappings);
197
198 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
199
200 SizeType GetSize() const { return m_NumDimMappings; }
201
202 ConstIterator begin() const { return m_DimMappings.begin(); }
203 ConstIterator end() const { return m_DimMappings.end(); }
204
205 bool IsEqual(const PermutationVector& other) const
206 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100207 if (m_NumDimMappings != other.m_NumDimMappings) return false;
208 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
209 {
210 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
211 }
212 return true;
telsoa014fcda012018-03-09 14:13:49 +0000213 }
214
215 bool IsInverse(const PermutationVector& other) const
216 {
217 bool isInverse = (GetSize() == other.GetSize());
218 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
219 {
220 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
221 }
222 return isInverse;
223 }
224
225private:
226 ArrayType m_DimMappings;
227 /// Number of valid entries in @ref m_DimMappings
228 SizeType m_NumDimMappings;
229};
230
janeil013fec1ea2019-11-07 09:47:20 +0000231namespace profiling { class ProfilingGuid; }
232
telsoa01c577f2c2018-08-31 09:22:23 +0100233/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000234using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100235
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000236class ITensorHandle;
237
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000238/// Define the type of callback for the Debug layer to call
239/// @param guid - guid of layer connected to the input of the Debug layer
240/// @param slotIndex - index of the output slot connected to the input of the Debug layer
241/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
242using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000243
janeil01c4946c72019-11-07 09:32:28 +0000244
245namespace profiling
246{
247
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000248static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63;
249
janeil01c4946c72019-11-07 09:32:28 +0000250class ProfilingGuid
251{
252public:
253 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
254
255 operator uint64_t() const { return m_Guid; }
256
257 bool operator==(const ProfilingGuid& other) const
258 {
259 return m_Guid == other.m_Guid;
260 }
261
262 bool operator!=(const ProfilingGuid& other) const
263 {
264 return m_Guid != other.m_Guid;
265 }
266
267 bool operator<(const ProfilingGuid& other) const
268 {
269 return m_Guid < other.m_Guid;
270 }
271
272 bool operator<=(const ProfilingGuid& other) const
273 {
274 return m_Guid <= other.m_Guid;
275 }
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
287protected:
288 uint64_t m_Guid;
289};
290
291/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
292struct ProfilingDynamicGuid : public ProfilingGuid
293{
294 using ProfilingGuid::ProfilingGuid;
295};
296
297struct ProfilingStaticGuid : public ProfilingGuid
298{
299 using ProfilingGuid::ProfilingGuid;
300};
301
302} // namespace profiling
303
David Beck9df2d952018-10-10 15:11:44 +0100304} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000305
306
307namespace std
308{
309// make ProfilingGuid hashable
310template<>
311struct hash<armnn::profiling::ProfilingGuid>
312{
313 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
314 {
315 return hash<uint64_t>()(uint64_t(guid));
316 }
317};
318
319// make ProfilingDynamicGuid hashable
320template<>
321struct hash<armnn::profiling::ProfilingDynamicGuid>
322{
323 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
324 {
325 return hash<uint64_t>()(uint64_t(guid));
326 }
327};
328
329// make ProfilingStaticGuid hashable
330template<>
331struct hash<armnn::profiling::ProfilingStaticGuid>
332{
333 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
334 {
335 return hash<uint64_t>()(uint64_t(guid));
336 }
337};
janeil013fec1ea2019-11-07 09:47:20 +0000338} // namespace std