blob: 11d807cd894296975c6bef3fe2c9ba4069c804a3 [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
Teresa Charlin11f6ace2020-06-23 18:30:57 +0100109enum class Dimensionality
110{
111 NotSpecified = 0,
112 Specified = 1,
113 Scalar = 2
114};
115
telsoa014fcda012018-03-09 14:13:49 +0000116///
117/// The padding method modifies the output of pooling layers.
118/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +0100119/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +0000120/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +0100121/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +0000122/// fields in the divisor of Average and L2 pooling, while
123/// Exclude does not.
124///
125enum class PaddingMethod
126{
telsoa01c577f2c2018-08-31 09:22:23 +0100127 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100128 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +0100129 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +0100130 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +0000131};
132
133enum class NormalizationAlgorithmChannel
134{
135 Across = 0,
136 Within = 1
137};
138
139enum class NormalizationAlgorithmMethod
140{
David Beckdcb751f2018-10-03 11:42:42 +0100141 /// Krichevsky 2012: Local Brightness Normalization
142 LocalBrightness = 0,
143 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +0100144 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +0000145};
146
147enum class OutputShapeRounding
148{
149 Floor = 0,
150 Ceiling = 1
151};
152
Teresa Charlincdc01492020-06-09 18:00:20 +0100153///
154/// The ShapeInferenceMethod modify how the output shapes are treated.
155/// When ValidateOnly is selected, the output shapes are inferred from the input parameters of the layer
156/// and any mismatch is reported.
157/// When InferAndValidate is selected 2 actions must be performed: (1)infer output shape from inputs and (2)validate the
158/// shapes as in ValidateOnly. This option has been added to work with tensors which rank or dimension sizes are not
159/// specified explicitly, however this information can be calculated from the inputs.
160///
161enum class ShapeInferenceMethod
162{
163 /// Validate all output shapes
164 ValidateOnly = 0,
165 /// Infer missing output shapes and validate all output shapes
166 InferAndValidate = 1
167};
168
David Beck9efb57d2018-11-05 13:40:33 +0000169/// Each backend should implement an IBackend.
170class IBackend
171{
172protected:
173 IBackend() {}
174 virtual ~IBackend() {}
175
176public:
177 virtual const BackendId& GetId() const = 0;
178};
179
180using IBackendSharedPtr = std::shared_ptr<IBackend>;
181using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
182
David Beckdcb751f2018-10-03 11:42:42 +0100183/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100184class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000185{
telsoa01c577f2c2018-08-31 09:22:23 +0100186protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000187 IDeviceSpec() {}
188 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100189public:
190 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000191};
192
193/// Type of identifiers for bindable layers (inputs, outputs).
194using LayerBindingId = int;
195
196class PermutationVector
197{
198public:
199 using ValueType = unsigned int;
200 using SizeType = unsigned int;
201 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
202 using ConstIterator = typename ArrayType::const_iterator;
203
telsoa01c577f2c2018-08-31 09:22:23 +0100204 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000205 /// when source and target potentially have different memory layouts.
206 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100207 /// 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 +0000208 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
209 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
210 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
211 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
212 /// [ 0, 2, 3, 1 ].
213 ///
214 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
215 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
216 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
217 /// [ 0, 3, 1, 2 ].
218 ///
219 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
220
221 PermutationVector(std::initializer_list<ValueType> dimMappings);
222
223 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
224
225 SizeType GetSize() const { return m_NumDimMappings; }
226
227 ConstIterator begin() const { return m_DimMappings.begin(); }
228 ConstIterator end() const { return m_DimMappings.end(); }
229
230 bool IsEqual(const PermutationVector& other) const
231 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100232 if (m_NumDimMappings != other.m_NumDimMappings) return false;
233 for (unsigned int i = 0; i < m_NumDimMappings; ++i)
234 {
235 if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
236 }
237 return true;
telsoa014fcda012018-03-09 14:13:49 +0000238 }
239
240 bool IsInverse(const PermutationVector& other) const
241 {
242 bool isInverse = (GetSize() == other.GetSize());
243 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
244 {
245 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
246 }
247 return isInverse;
248 }
249
250private:
251 ArrayType m_DimMappings;
252 /// Number of valid entries in @ref m_DimMappings
253 SizeType m_NumDimMappings;
254};
255
janeil013fec1ea2019-11-07 09:47:20 +0000256namespace profiling { class ProfilingGuid; }
257
telsoa01c577f2c2018-08-31 09:22:23 +0100258/// Define LayerGuid type.
janeil013fec1ea2019-11-07 09:47:20 +0000259using LayerGuid = profiling::ProfilingGuid;
surmeh01bceff2f2018-03-29 16:29:27 +0100260
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000261class ITensorHandle;
262
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000263/// Define the type of callback for the Debug layer to call
264/// @param guid - guid of layer connected to the input of the Debug layer
265/// @param slotIndex - index of the output slot connected to the input of the Debug layer
266/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
267using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000268
janeil01c4946c72019-11-07 09:32:28 +0000269
270namespace profiling
271{
272
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000273static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63;
274
janeil01c4946c72019-11-07 09:32:28 +0000275class ProfilingGuid
276{
277public:
Sadik Armagan3184c902020-03-18 10:57:30 +0000278 ProfilingGuid() : m_Guid(0) {}
279
janeil01c4946c72019-11-07 09:32:28 +0000280 ProfilingGuid(uint64_t guid) : m_Guid(guid) {}
281
282 operator uint64_t() const { return m_Guid; }
283
284 bool operator==(const ProfilingGuid& other) const
285 {
286 return m_Guid == other.m_Guid;
287 }
288
289 bool operator!=(const ProfilingGuid& other) const
290 {
291 return m_Guid != other.m_Guid;
292 }
293
294 bool operator<(const ProfilingGuid& other) const
295 {
296 return m_Guid < other.m_Guid;
297 }
298
299 bool operator<=(const ProfilingGuid& other) const
300 {
301 return m_Guid <= other.m_Guid;
302 }
303
304 bool operator>(const ProfilingGuid& other) const
305 {
306 return m_Guid > other.m_Guid;
307 }
308
309 bool operator>=(const ProfilingGuid& other) const
310 {
311 return m_Guid >= other.m_Guid;
312 }
313
314protected:
315 uint64_t m_Guid;
316};
317
318/// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined.
319struct ProfilingDynamicGuid : public ProfilingGuid
320{
321 using ProfilingGuid::ProfilingGuid;
322};
323
324struct ProfilingStaticGuid : public ProfilingGuid
325{
326 using ProfilingGuid::ProfilingGuid;
327};
328
329} // namespace profiling
330
David Beck9df2d952018-10-10 15:11:44 +0100331} // namespace armnn
janeil01c4946c72019-11-07 09:32:28 +0000332
333
334namespace std
335{
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000336/// make ProfilingGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000337template<>
338struct hash<armnn::profiling::ProfilingGuid>
339{
340 std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept
341 {
342 return hash<uint64_t>()(uint64_t(guid));
343 }
344};
345
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000346/// make ProfilingDynamicGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000347template<>
348struct hash<armnn::profiling::ProfilingDynamicGuid>
349{
350 std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept
351 {
352 return hash<uint64_t>()(uint64_t(guid));
353 }
354};
355
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000356/// make ProfilingStaticGuid hashable
janeil01c4946c72019-11-07 09:32:28 +0000357template<>
358struct hash<armnn::profiling::ProfilingStaticGuid>
359{
360 std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept
361 {
362 return hash<uint64_t>()(uint64_t(guid));
363 }
364};
janeil013fec1ea2019-11-07 09:47:20 +0000365} // namespace std