blob: cd6e17be37b8ac4c06616ac826c260c57abd7a0d [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,
29 Float32 = 1,
30 QuantisedAsymm8 = 2,
31 Signed32 = 3
telsoa014fcda012018-03-09 14:13:49 +000032};
33
James Conroy59540822018-10-11 12:39:05 +010034// Begin: DataLayout
35
Derek Lamberti0cff1632018-09-18 16:02:25 +010036enum class DataLayout
37{
38 NCHW = 1,
39 NHWC = 2
40};
41
James Conroy59540822018-10-11 12:39:05 +010042// Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout
43class DataLayoutIndexed
44{
45public:
46 DataLayoutIndexed(DataLayout dataLayout) : m_DataLayout(dataLayout)
47 {
48 switch (dataLayout)
49 {
50 case DataLayout::NHWC:
51 m_ChannelsIndex = 3;
52 m_HeightIndex = 1;
53 m_WidthIndex = 2;
54 break;
55 case DataLayout::NCHW:
56 m_ChannelsIndex = 1;
57 m_HeightIndex = 2;
58 m_WidthIndex = 3;
59 break;
60 default:
61 throw InvalidArgumentException("Unknown DataLayout value: " +
62 std::to_string(static_cast<int>(dataLayout)));
63 }
64 }
65
66 DataLayout GetDataLayout() const { return m_DataLayout; }
67 unsigned int GetChannelsIndex() const { return m_ChannelsIndex; }
68 unsigned int GetHeightIndex() const { return m_HeightIndex; }
69 unsigned int GetWidthIndex() const { return m_WidthIndex; }
70
71private:
72 DataLayout m_DataLayout;
73 unsigned int m_ChannelsIndex;
74 unsigned int m_HeightIndex;
75 unsigned int m_WidthIndex;
76};
77
78// Conversion methods - implementations in src/armnn/InternalTypes.cpp
79bool operator==(const DataLayout& dataLayout, const DataLayoutIndexed& indexed);
80bool operator==(const DataLayoutIndexed& indexed, const DataLayout& dataLayout);
81
82// End: DataLayout
83
telsoa014fcda012018-03-09 14:13:49 +000084enum class ActivationFunction
85{
86 Sigmoid = 0,
87 TanH = 1,
88 Linear = 2,
89 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010090 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000091 SoftReLu = 5,
92 LeakyReLu = 6,
93 Abs = 7,
94 Sqrt = 8,
95 Square = 9
96};
97
98enum class PoolingAlgorithm
99{
100 Max = 0,
101 Average = 1,
102 L2 = 2
103};
104
105///
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:
160 IDeviceSpec() {};
161 virtual ~IDeviceSpec() {};
telsoa014fcda012018-03-09 14:13:49 +0000162};
163
164/// Type of identifiers for bindable layers (inputs, outputs).
165using LayerBindingId = int;
166
167class PermutationVector
168{
169public:
170 using ValueType = unsigned int;
171 using SizeType = unsigned int;
172 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
173 using ConstIterator = typename ArrayType::const_iterator;
174
telsoa01c577f2c2018-08-31 09:22:23 +0100175 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000176 /// when source and target potentially have different memory layouts.
177 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100178 /// 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 +0000179 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
180 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
181 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
182 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
183 /// [ 0, 2, 3, 1 ].
184 ///
185 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
186 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
187 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
188 /// [ 0, 3, 1, 2 ].
189 ///
190 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
191
192 PermutationVector(std::initializer_list<ValueType> dimMappings);
193
194 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
195
196 SizeType GetSize() const { return m_NumDimMappings; }
197
198 ConstIterator begin() const { return m_DimMappings.begin(); }
199 ConstIterator end() const { return m_DimMappings.end(); }
200
201 bool IsEqual(const PermutationVector& other) const
202 {
203 return std::equal(begin(), end(), other.begin(), other.end());
204 }
205
206 bool IsInverse(const PermutationVector& other) const
207 {
208 bool isInverse = (GetSize() == other.GetSize());
209 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
210 {
211 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
212 }
213 return isInverse;
214 }
215
216private:
217 ArrayType m_DimMappings;
218 /// Number of valid entries in @ref m_DimMappings
219 SizeType m_NumDimMappings;
220};
221
telsoa01c577f2c2018-08-31 09:22:23 +0100222/// Define LayerGuid type.
surmeh01bceff2f2018-03-29 16:29:27 +0100223using LayerGuid = unsigned int;
224
David Beck9df2d952018-10-10 15:11:44 +0100225} // namespace armnn