blob: 4afc50b1b1135bb57794774bc2371cab9774e470 [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 Becke97c6e02018-10-03 13:09:28 +01009#include "ILayerSupport.hpp"
David Beck9df2d952018-10-10 15:11:44 +010010#include "BackendId.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,
29 Float32 = 1,
30 QuantisedAsymm8 = 2,
31 Signed32 = 3
telsoa014fcda012018-03-09 14:13:49 +000032};
33
Derek Lamberti0cff1632018-09-18 16:02:25 +010034enum class DataLayout
35{
36 NCHW = 1,
37 NHWC = 2
38};
39
telsoa014fcda012018-03-09 14:13:49 +000040enum class ActivationFunction
41{
42 Sigmoid = 0,
43 TanH = 1,
44 Linear = 2,
45 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010046 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000047 SoftReLu = 5,
48 LeakyReLu = 6,
49 Abs = 7,
50 Sqrt = 8,
51 Square = 9
52};
53
54enum class PoolingAlgorithm
55{
56 Max = 0,
57 Average = 1,
58 L2 = 2
59};
60
61///
62/// The padding method modifies the output of pooling layers.
63/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +010064/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +000065/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +010066/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +000067/// fields in the divisor of Average and L2 pooling, while
68/// Exclude does not.
69///
70enum class PaddingMethod
71{
telsoa01c577f2c2018-08-31 09:22:23 +010072 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +010073 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +010074 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +010075 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +000076};
77
78enum class NormalizationAlgorithmChannel
79{
80 Across = 0,
81 Within = 1
82};
83
84enum class NormalizationAlgorithmMethod
85{
David Beckdcb751f2018-10-03 11:42:42 +010086 /// Krichevsky 2012: Local Brightness Normalization
87 LocalBrightness = 0,
88 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +010089 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +000090};
91
92enum class OutputShapeRounding
93{
94 Floor = 0,
95 Ceiling = 1
96};
97
David Beckdcb751f2018-10-03 11:42:42 +010098/// Each backend should implement an IBackend.
99class IBackend
100{
101protected:
102 IBackend() {}
103 virtual ~IBackend() {}
104
105public:
David Beck9df2d952018-10-10 15:11:44 +0100106 virtual const BackendId& GetId() const = 0;
David Becke97c6e02018-10-03 13:09:28 +0100107 virtual const ILayerSupport& GetLayerSupport() const = 0;
David Beckdcb751f2018-10-03 11:42:42 +0100108};
109
David Beck32cbb0c2018-10-09 15:46:08 +0100110using IBackendSharedPtr = std::shared_ptr<IBackend>;
111using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
David Beckdcb751f2018-10-03 11:42:42 +0100112
113/// 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:
117 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