blob: 598eaaf6be0a3a167a03097a440a6ed0e81937a0 [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>
David Beckdcb751f2018-10-03 11:42:42 +01008#include <memory>
David Beck9df2d952018-10-10 15:11:44 +01009#include "BackendId.hpp"
David Beck3e9e1152018-10-17 14:17:50 +010010#include "Exceptions.hpp"
telsoa014fcda012018-03-09 14:13:49 +000011
12namespace armnn
13{
14
15constexpr unsigned int MaxNumOfTensorDimensions = 4U;
16
17/// @enum Status enumeration
18/// @var Status::Successful
19/// @var Status::Failure
20enum class Status
21{
22 Success = 0,
23 Failure = 1
24};
25
26enum class DataType
27{
telsoa01c577f2c2018-08-31 09:22:23 +010028 Float16 = 0,
ruoyan0120e984f2018-12-12 18:11:25 +000029 Float32 = 1,
telsoa01c577f2c2018-08-31 09:22:23 +010030 QuantisedAsymm8 = 2,
ruoyan0120e984f2018-12-12 18:11:25 +000031 Signed32 = 3,
32 Boolean = 4
telsoa014fcda012018-03-09 14:13:49 +000033};
34
Derek Lamberti0cff1632018-09-18 16:02:25 +010035enum class DataLayout
36{
37 NCHW = 1,
38 NHWC = 2
39};
40
telsoa014fcda012018-03-09 14:13:49 +000041enum class ActivationFunction
42{
43 Sigmoid = 0,
44 TanH = 1,
45 Linear = 2,
46 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010047 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000048 SoftReLu = 5,
49 LeakyReLu = 6,
50 Abs = 7,
51 Sqrt = 8,
52 Square = 9
53};
54
55enum class PoolingAlgorithm
56{
57 Max = 0,
58 Average = 1,
59 L2 = 2
60};
61
62///
63/// The padding method modifies the output of pooling layers.
64/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +010065/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +000066/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +010067/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +000068/// fields in the divisor of Average and L2 pooling, while
69/// Exclude does not.
70///
71enum class PaddingMethod
72{
telsoa01c577f2c2018-08-31 09:22:23 +010073 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +010074 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +010075 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +010076 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +000077};
78
79enum class NormalizationAlgorithmChannel
80{
81 Across = 0,
82 Within = 1
83};
84
85enum class NormalizationAlgorithmMethod
86{
David Beckdcb751f2018-10-03 11:42:42 +010087 /// Krichevsky 2012: Local Brightness Normalization
88 LocalBrightness = 0,
89 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +010090 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +000091};
92
93enum class OutputShapeRounding
94{
95 Floor = 0,
96 Ceiling = 1
97};
98
David Beck9efb57d2018-11-05 13:40:33 +000099/// Each backend should implement an IBackend.
100class IBackend
101{
102protected:
103 IBackend() {}
104 virtual ~IBackend() {}
105
106public:
107 virtual const BackendId& GetId() const = 0;
108};
109
110using IBackendSharedPtr = std::shared_ptr<IBackend>;
111using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
112
David Beckdcb751f2018-10-03 11:42:42 +0100113/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100114class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000115{
telsoa01c577f2c2018-08-31 09:22:23 +0100116protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000117 IDeviceSpec() {}
118 virtual ~IDeviceSpec() {}
telsoa014fcda012018-03-09 14:13:49 +0000119};
120
121/// Type of identifiers for bindable layers (inputs, outputs).
122using LayerBindingId = int;
123
124class PermutationVector
125{
126public:
127 using ValueType = unsigned int;
128 using SizeType = unsigned int;
129 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
130 using ConstIterator = typename ArrayType::const_iterator;
131
telsoa01c577f2c2018-08-31 09:22:23 +0100132 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000133 /// when source and target potentially have different memory layouts.
134 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100135 /// 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 +0000136 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
137 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
138 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
139 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
140 /// [ 0, 2, 3, 1 ].
141 ///
142 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
143 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
144 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
145 /// [ 0, 3, 1, 2 ].
146 ///
147 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
148
149 PermutationVector(std::initializer_list<ValueType> dimMappings);
150
151 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
152
153 SizeType GetSize() const { return m_NumDimMappings; }
154
155 ConstIterator begin() const { return m_DimMappings.begin(); }
156 ConstIterator end() const { return m_DimMappings.end(); }
157
158 bool IsEqual(const PermutationVector& other) const
159 {
160 return std::equal(begin(), end(), other.begin(), other.end());
161 }
162
163 bool IsInverse(const PermutationVector& other) const
164 {
165 bool isInverse = (GetSize() == other.GetSize());
166 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
167 {
168 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
169 }
170 return isInverse;
171 }
172
173private:
174 ArrayType m_DimMappings;
175 /// Number of valid entries in @ref m_DimMappings
176 SizeType m_NumDimMappings;
177};
178
telsoa01c577f2c2018-08-31 09:22:23 +0100179/// Define LayerGuid type.
surmeh01bceff2f2018-03-29 16:29:27 +0100180using LayerGuid = unsigned int;
181
David Beck9df2d952018-10-10 15:11:44 +0100182} // namespace armnn