blob: 1ab5660109863766602d49468c80c68db491bd52 [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,
Mike Kelly49d0f122019-11-19 09:12:19 +000040 QuantizedSymm8PerAxis = 6,
Derek Lambertif90c56d2020-01-10 17:14:08 +000041 QSymmS8 = 7,
42
Derek Lamberti41e92b02020-01-21 13:43:21 +000043 QuantisedAsymm8 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QAsymmU8 instead.") = QAsymmU8,
44 QuantisedSymm16 ARMNN_DEPRECATED_ENUM_MSG("Use DataType::QSymmS16 instead.") = QSymmS16
telsoa014fcda012018-03-09 14:13:49 +000045};
46
Derek Lamberti0cff1632018-09-18 16:02:25 +010047enum class DataLayout
48{
49 NCHW = 1,
50 NHWC = 2
51};
52
telsoa014fcda012018-03-09 14:13:49 +000053enum class ActivationFunction
54{
55 Sigmoid = 0,
56 TanH = 1,
57 Linear = 2,
58 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010059 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000060 SoftReLu = 5,
61 LeakyReLu = 6,
62 Abs = 7,
63 Sqrt = 8,
64 Square = 9
65};
66
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010067enum class ArgMinMaxFunction
68{
69 Min = 0,
70 Max = 1
71};
72
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010073enum class ComparisonOperation
74{
75 Equal = 0,
76 Greater = 1,
77 GreaterOrEqual = 2,
78 Less = 3,
79 LessOrEqual = 4,
80 NotEqual = 5
81};
82
josh minor4a3c6102020-01-06 16:40:46 -060083enum class UnaryOperation
84{
85 Abs = 0,
86 Exp = 1,
87 Sqrt = 2,
88 Rsqrt = 3,
89 Neg = 4
90};
91
telsoa014fcda012018-03-09 14:13:49 +000092enum class PoolingAlgorithm
93{
94 Max = 0,
95 Average = 1,
96 L2 = 2
97};
98
Teresa Charlina9075df2019-06-27 15:41:57 +010099enum class ResizeMethod
100{
101 Bilinear = 0,
102 NearestNeighbor = 1
103};
104
telsoa014fcda012018-03-09 14:13:49 +0000105///
106/// The padding method modifies the output of pooling layers.
107/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100108/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000109/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100110/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000111/// fields in the divisor of Average and L2 pooling, while
112/// Exclude does not.
113///
114enum class PaddingMethod
115{
telsoa01c577f2c2018-08-31 09:22:23 +0100116 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100117 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100118 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100119 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000120};
121
122enum class NormalizationAlgorithmChannel
123{
124 Across = 0,
125 Within = 1
126};
127
128enum class NormalizationAlgorithmMethod
129{
David Beckdcb751f2018-10-03 11:42:42 +0100130 /// Krichevsky 2012: Local Brightness Normalization
131 LocalBrightness = 0,
132 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100133 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000134};
135
136enum class OutputShapeRounding
137{
138 Floor = 0,
139 Ceiling = 1
140};
141
David Beck9efb57d2018-11-05 13:40:33 +0000142/// Each backend should implement an IBackend.
143class IBackend
144{
145protected:
146 IBackend() {}
147 virtual ~IBackend() {}
148
149public:
150 virtual const BackendId& GetId() const = 0;
151};
152
153using IBackendSharedPtr = std::shared_ptr<IBackend>;
154using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
155
David Beckdcb751f2018-10-03 11:42:42 +0100156/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100157class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000158{
telsoa01c577f2c2018-08-31 09:22:23 +0100159protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000160 IDeviceSpec() {}
161 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100162public:
163 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000164};
165
166/// Type of identifiers for bindable layers (inputs, outputs).
167using LayerBindingId = int;
168
169class PermutationVector
170{
171public:
172 using ValueType = unsigned int;
173 using SizeType = unsigned int;
174 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
175 using ConstIterator = typename ArrayType::const_iterator;
176
telsoa01c577f2c2018-08-31 09:22:23 +0100177 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000178 /// when source and target potentially have different memory layouts.
179 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100180 /// 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 +0000181 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
182 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
183 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
184 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
185 /// [ 0, 2, 3, 1 ].
186 ///
187 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
188 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
189 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
190 /// [ 0, 3, 1, 2 ].
191 ///
192 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
193
194 PermutationVector(std::initializer_list<ValueType> dimMappings);
195
196 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
197
198 SizeType GetSize() const { return m_NumDimMappings; }
199
200 ConstIterator begin() const { return m_DimMappings.begin(); }
201 ConstIterator end() const { return m_DimMappings.end(); }
202
203 bool IsEqual(const PermutationVector& other) const
204 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100205 if (m_NumDimMappings != other.m_NumDimMappings) return false;
206 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
207 {
208 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
209 }
210 return true;
telsoa014fcda012018-03-09 14:13:49 +0000211 }
212
213 bool IsInverse(const PermutationVector& other) const
214 {
215 bool isInverse = (GetSize() == other.GetSize());
216 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
217 {
218 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
219 }
220 return isInverse;
221 }
222
223private:
224 ArrayType m_DimMappings;
225 /// Number of valid entries in @ref m_DimMappings
226 SizeType m_NumDimMappings;
227};
228
janeil013fec1ea2019-11-07 09:47:20 +0000229namespace profiling { class ProfilingGuid; }
230
telsoa01c577f2c2018-08-31 09:22:23 +0100231/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000232using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100233
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000234class ITensorHandle;
235
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000236/// Define the type of callback for the Debug layer to call
237/// @param guid - guid of layer connected to the input of the Debug layer
238/// @param slotIndex - index of the output slot connected to the input of the Debug layer
239/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
240using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000241
janeil01c4946c72019-11-07 09:32:28 +0000242
243namespace profiling
244{
245
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000246static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63;
247
janeil01c4946c72019-11-07 09:32:28 +0000248class ProfilingGuid
249{
250public:
251 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
252
253 operator uint64_t() const { return m_Guid; }
254
255 bool operator==(const ProfilingGuid& other) const
256 {
257 return m_Guid == other.m_Guid;
258 }
259
260 bool operator!=(const ProfilingGuid& other) const
261 {
262 return m_Guid != other.m_Guid;
263 }
264
265 bool operator<(const ProfilingGuid& other) const
266 {
267 return m_Guid < other.m_Guid;
268 }
269
270 bool operator<=(const ProfilingGuid& other) const
271 {
272 return m_Guid <= other.m_Guid;
273 }
274
275 bool operator>(const ProfilingGuid& other) const
276 {
277 return m_Guid > other.m_Guid;
278 }
279
280 bool operator>=(const ProfilingGuid& other) const
281 {
282 return m_Guid >= other.m_Guid;
283 }
284
285protected:
286 uint64_t m_Guid;
287};
288
289/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
290struct ProfilingDynamicGuid : public ProfilingGuid
291{
292 using ProfilingGuid::ProfilingGuid;
293};
294
295struct ProfilingStaticGuid : public ProfilingGuid
296{
297 using ProfilingGuid::ProfilingGuid;
298};
299
300} // namespace profiling
301
David Beck9df2d952018-10-10 15:11:44 +0100302} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000303
304
305namespace std
306{
307// make ProfilingGuid hashable
308template<>
309struct hash<armnn::profiling::ProfilingGuid>
310{
311 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
312 {
313 return hash<uint64_t>()(uint64_t(guid));
314 }
315};
316
317// make ProfilingDynamicGuid hashable
318template<>
319struct hash<armnn::profiling::ProfilingDynamicGuid>
320{
321 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
322 {
323 return hash<uint64_t>()(uint64_t(guid));
324 }
325};
326
327// make ProfilingStaticGuid hashable
328template<>
329struct hash<armnn::profiling::ProfilingStaticGuid>
330{
331 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
332 {
333 return hash<uint64_t>()(uint64_t(guid));
334 }
335};
janeil013fec1ea2019-11-07 09:47:20 +0000336} // namespace std