16a148c9c27e6afb544133a5c95f325746c06dc4
[platform/upstream/armnn.git] / include / armnn / Types.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include <array>
8 #include <functional>
9 #include <memory>
10 #include "BackendId.hpp"
11 #include "Exceptions.hpp"
12
13 namespace armnn
14 {
15
16 constexpr unsigned int MaxNumOfTensorDimensions = 5U;
17
18 /// @enum Status enumeration
19 /// @var Status::Successful
20 /// @var Status::Failure
21 enum class Status
22 {
23     Success = 0,
24     Failure = 1
25 };
26
27 enum class DataType
28 {
29     Float16 = 0,
30     Float32 = 1,
31     QuantisedAsymm8 = 2,
32     Signed32 = 3,
33     Boolean = 4,
34     QuantisedSymm16 = 5
35 };
36
37 enum class DataLayout
38 {
39     NCHW = 1,
40     NHWC = 2
41 };
42
43 enum class ActivationFunction
44 {
45     Sigmoid     = 0,
46     TanH        = 1,
47     Linear      = 2,
48     ReLu        = 3,
49     BoundedReLu = 4, ///< min(a, max(b, input))
50     SoftReLu    = 5,
51     LeakyReLu   = 6,
52     Abs         = 7,
53     Sqrt        = 8,
54     Square      = 9
55 };
56
57 enum class ArgMinMaxFunction
58 {
59     Min = 0,
60     Max = 1
61 };
62
63 enum class ComparisonOperation
64 {
65     Equal          = 0,
66     Greater        = 1,
67     GreaterOrEqual = 2,
68     Less           = 3,
69     LessOrEqual    = 4,
70     NotEqual       = 5
71 };
72
73 enum class PoolingAlgorithm
74 {
75     Max     = 0,
76     Average = 1,
77     L2      = 2
78 };
79
80 enum class ResizeMethod
81 {
82     Bilinear        = 0,
83     NearestNeighbor = 1
84 };
85
86 ///
87 /// The padding method modifies the output of pooling layers.
88 /// In both supported methods, the values are ignored (they are
89 /// not even zeroes, which would make a difference for max pooling
90 /// a tensor with negative values). The difference between
91 /// IgnoreValue and Exclude is that the former counts the padding
92 /// fields in the divisor of Average and L2 pooling, while
93 /// Exclude does not.
94 ///
95 enum class PaddingMethod
96 {
97     /// The padding fields count, but are ignored
98     IgnoreValue = 0,
99     /// The padding fields don't count and are ignored
100     Exclude     = 1
101 };
102
103 enum class NormalizationAlgorithmChannel
104 {
105     Across = 0,
106     Within = 1
107 };
108
109 enum class NormalizationAlgorithmMethod
110 {
111     /// Krichevsky 2012: Local Brightness Normalization
112     LocalBrightness = 0,
113     /// Jarret 2009: Local Contrast Normalization
114     LocalContrast = 1
115 };
116
117 enum class OutputShapeRounding
118 {
119     Floor       = 0,
120     Ceiling     = 1
121 };
122
123 /// Each backend should implement an IBackend.
124 class IBackend
125 {
126 protected:
127     IBackend() {}
128     virtual ~IBackend() {}
129
130 public:
131     virtual const BackendId& GetId() const = 0;
132 };
133
134 using IBackendSharedPtr = std::shared_ptr<IBackend>;
135 using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
136
137 /// Device specific knowledge to be passed to the optimizer.
138 class IDeviceSpec
139 {
140 protected:
141     IDeviceSpec() {}
142     virtual ~IDeviceSpec() {}
143 public:
144     virtual const BackendIdSet& GetSupportedBackends() const = 0;
145 };
146
147 /// Type of identifiers for bindable layers (inputs, outputs).
148 using LayerBindingId = int;
149
150 class PermutationVector
151 {
152 public:
153     using ValueType = unsigned int;
154     using SizeType = unsigned int;
155     using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
156     using ConstIterator = typename ArrayType::const_iterator;
157
158     /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
159     /// when source and target potentially have different memory layouts.
160     ///
161     /// E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels),
162     /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
163     /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
164     /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
165     /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
166     /// [ 0, 2, 3, 1 ].
167     ///
168     /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
169     /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
170     /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
171     /// [ 0, 3, 1, 2 ].
172     ///
173     PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
174
175     PermutationVector(std::initializer_list<ValueType> dimMappings);
176
177     ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
178
179     SizeType GetSize() const { return m_NumDimMappings; }
180
181     ConstIterator begin() const { return m_DimMappings.begin(); }
182     ConstIterator end() const { return m_DimMappings.end(); }
183
184     bool IsEqual(const PermutationVector& other) const
185     {
186         if (m_NumDimMappings != other.m_NumDimMappings) return false;
187         for (unsigned int i = 0; i < m_NumDimMappings; ++i)
188         {
189             if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
190         }
191         return true;
192     }
193
194     bool IsInverse(const PermutationVector& other) const
195     {
196         bool isInverse = (GetSize() == other.GetSize());
197         for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
198         {
199             isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
200         }
201         return isInverse;
202     }
203
204 private:
205     ArrayType m_DimMappings;
206     /// Number of valid entries in @ref m_DimMappings
207     SizeType m_NumDimMappings;
208 };
209
210 /// Define LayerGuid type.
211 using LayerGuid = unsigned int;
212
213 class ITensorHandle;
214
215 /// Define the type of callback for the Debug layer to call
216 /// @param guid - guid of layer connected to the input of the Debug layer
217 /// @param slotIndex - index of the output slot connected to the input of the Debug layer
218 /// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
219 using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
220
221 } // namespace armnn