blob: 94f45302c714fc05b4067d9a2887f0e4b4defebf [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"
telsoa014fcda012018-03-09 14:13:49 +000013
14namespace armnn
15{
16
Matthew Jacksondba634f2019-08-15 15:14:18 +010017constexpr unsigned int MaxNumOfTensorDimensions = 5U;
telsoa014fcda012018-03-09 14:13:49 +000018
Colm Donelan02705242019-11-14 14:19:07 +000019// The lowest performance data capture interval we support is 10 miliseconds.
20constexpr unsigned int LOWEST_CAPTURE_PERIOD = 10000u;
21
telsoa014fcda012018-03-09 14:13:49 +000022/// @enum Status enumeration
23/// @var Status::Successful
24/// @var Status::Failure
25enum class Status
26{
27 Success = 0,
28 Failure = 1
29};
30
31enum class DataType
32{
telsoa01c577f2c2018-08-31 09:22:23 +010033 Float16 = 0,
ruoyan0120e984f2018-12-12 18:11:25 +000034 Float32 = 1,
telsoa01c577f2c2018-08-31 09:22:23 +010035 QuantisedAsymm8 = 2,
ruoyan0120e984f2018-12-12 18:11:25 +000036 Signed32 = 3,
Nattapat Chaimanowongcd5ac232019-03-19 12:26:36 +000037 Boolean = 4,
Keith Davis5236e1d2019-11-04 08:58:33 +000038 QuantisedSymm16 = 5,
39 QuantizedSymm8PerAxis = 6
telsoa014fcda012018-03-09 14:13:49 +000040};
41
Derek Lamberti0cff1632018-09-18 16:02:25 +010042enum class DataLayout
43{
44 NCHW = 1,
45 NHWC = 2
46};
47
telsoa014fcda012018-03-09 14:13:49 +000048enum class ActivationFunction
49{
50 Sigmoid = 0,
51 TanH = 1,
52 Linear = 2,
53 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010054 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000055 SoftReLu = 5,
56 LeakyReLu = 6,
57 Abs = 7,
58 Sqrt = 8,
59 Square = 9
60};
61
Narumol Prangnawarat8d001d42019-09-09 15:01:18 +010062enum class ArgMinMaxFunction
63{
64 Min = 0,
65 Max = 1
66};
67
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010068enum class ComparisonOperation
69{
70 Equal = 0,
71 Greater = 1,
72 GreaterOrEqual = 2,
73 Less = 3,
74 LessOrEqual = 4,
75 NotEqual = 5
76};
77
telsoa014fcda012018-03-09 14:13:49 +000078enum class PoolingAlgorithm
79{
80 Max = 0,
81 Average = 1,
82 L2 = 2
83};
84
Teresa Charlina9075df2019-06-27 15:41:57 +010085enum class ResizeMethod
86{
87 Bilinear = 0,
88 NearestNeighbor = 1
89};
90
telsoa014fcda012018-03-09 14:13:49 +000091///
92/// The padding method modifies the output of pooling layers.
93/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +010094/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +000095/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +010096/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +000097/// fields in the divisor of Average and L2 pooling, while
98/// Exclude does not.
99///
100enum class PaddingMethod
101{
telsoa01c577f2c2018-08-31 09:22:23 +0100102 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100103 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100104 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100105 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000106};
107
108enum class NormalizationAlgorithmChannel
109{
110 Across = 0,
111 Within = 1
112};
113
114enum class NormalizationAlgorithmMethod
115{
David Beckdcb751f2018-10-03 11:42:42 +0100116 /// Krichevsky 2012: Local Brightness Normalization
117 LocalBrightness = 0,
118 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100119 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000120};
121
122enum class OutputShapeRounding
123{
124 Floor = 0,
125 Ceiling = 1
126};
127
David Beck9efb57d2018-11-05 13:40:33 +0000128/// Each backend should implement an IBackend.
129class IBackend
130{
131protected:
132 IBackend() {}
133 virtual ~IBackend() {}
134
135public:
136 virtual const BackendId& GetId() const = 0;
137};
138
139using IBackendSharedPtr = std::shared_ptr<IBackend>;
140using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
141
David Beckdcb751f2018-10-03 11:42:42 +0100142/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100143class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000144{
telsoa01c577f2c2018-08-31 09:22:23 +0100145protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000146 IDeviceSpec() {}
147 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100148public:
149 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000150};
151
152/// Type of identifiers for bindable layers (inputs, outputs).
153using LayerBindingId = int;
154
155class PermutationVector
156{
157public:
158 using ValueType = unsigned int;
159 using SizeType = unsigned int;
160 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
161 using ConstIterator = typename ArrayType::const_iterator;
162
telsoa01c577f2c2018-08-31 09:22:23 +0100163 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000164 /// when source and target potentially have different memory layouts.
165 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100166 /// 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 +0000167 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
168 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
169 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
170 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
171 /// [ 0, 2, 3, 1 ].
172 ///
173 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
174 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
175 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
176 /// [ 0, 3, 1, 2 ].
177 ///
178 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
179
180 PermutationVector(std::initializer_list<ValueType> dimMappings);
181
182 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
183
184 SizeType GetSize() const { return m_NumDimMappings; }
185
186 ConstIterator begin() const { return m_DimMappings.begin(); }
187 ConstIterator end() const { return m_DimMappings.end(); }
188
189 bool IsEqual(const PermutationVector& other) const
190 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100191 if (m_NumDimMappings != other.m_NumDimMappings) return false;
192 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
193 {
194 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
195 }
196 return true;
telsoa014fcda012018-03-09 14:13:49 +0000197 }
198
199 bool IsInverse(const PermutationVector& other) const
200 {
201 bool isInverse = (GetSize() == other.GetSize());
202 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
203 {
204 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
205 }
206 return isInverse;
207 }
208
209private:
210 ArrayType m_DimMappings;
211 /// Number of valid entries in @ref m_DimMappings
212 SizeType m_NumDimMappings;
213};
214
janeil013fec1ea2019-11-07 09:47:20 +0000215namespace profiling { class ProfilingGuid; }
216
telsoa01c577f2c2018-08-31 09:22:23 +0100217/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000218using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100219
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000220class ITensorHandle;
221
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000222/// Define the type of callback for the Debug layer to call
223/// @param guid - guid of layer connected to the input of the Debug layer
224/// @param slotIndex - index of the output slot connected to the input of the Debug layer
225/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
226using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000227
janeil01c4946c72019-11-07 09:32:28 +0000228
229namespace profiling
230{
231
232class ProfilingGuid
233{
234public:
235 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
236
237 operator uint64_t() const { return m_Guid; }
238
239 bool operator==(const ProfilingGuid& other) const
240 {
241 return m_Guid == other.m_Guid;
242 }
243
244 bool operator!=(const ProfilingGuid& other) const
245 {
246 return m_Guid != other.m_Guid;
247 }
248
249 bool operator<(const ProfilingGuid& other) const
250 {
251 return m_Guid < other.m_Guid;
252 }
253
254 bool operator<=(const ProfilingGuid& other) const
255 {
256 return m_Guid <= other.m_Guid;
257 }
258
259 bool operator>(const ProfilingGuid& other) const
260 {
261 return m_Guid > other.m_Guid;
262 }
263
264 bool operator>=(const ProfilingGuid& other) const
265 {
266 return m_Guid >= other.m_Guid;
267 }
268
269protected:
270 uint64_t m_Guid;
271};
272
273/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
274struct ProfilingDynamicGuid : public ProfilingGuid
275{
276 using ProfilingGuid::ProfilingGuid;
277};
278
279struct ProfilingStaticGuid : public ProfilingGuid
280{
281 using ProfilingGuid::ProfilingGuid;
282};
283
284} // namespace profiling
285
David Beck9df2d952018-10-10 15:11:44 +0100286} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000287
288
289namespace std
290{
291// make ProfilingGuid hashable
292template<>
293struct hash<armnn::profiling::ProfilingGuid>
294{
295 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
296 {
297 return hash<uint64_t>()(uint64_t(guid));
298 }
299};
300
301// make ProfilingDynamicGuid hashable
302template<>
303struct hash<armnn::profiling::ProfilingDynamicGuid>
304{
305 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
306 {
307 return hash<uint64_t>()(uint64_t(guid));
308 }
309};
310
311// make ProfilingStaticGuid hashable
312template<>
313struct hash<armnn::profiling::ProfilingStaticGuid>
314{
315 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
316 {
317 return hash<uint64_t>()(uint64_t(guid));
318 }
319};
janeil013fec1ea2019-11-07 09:47:20 +0000320} // namespace std