blob: 12897e2af7345c1e9e38a1eac16d82a4aae1c86c [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>
Matthew Bentham47bfac42019-03-25 12:30:56 +00008#include <functional>
David Beckdcb751f2018-10-03 11:42:42 +01009#include <memory>
Jim Flynn44db7c32019-03-22 15:58:39 +000010#include "BackendId.hpp"
11#include "Exceptions.hpp"
telsoa014fcda012018-03-09 14:13:49 +000012
13namespace armnn
14{
15
16constexpr unsigned int MaxNumOfTensorDimensions = 4U;
17
18/// @enum Status enumeration
19/// @var Status::Successful
20/// @var Status::Failure
21enum class Status
22{
23 Success = 0,
24 Failure = 1
25};
26
27enum class DataType
28{
telsoa01c577f2c2018-08-31 09:22:23 +010029 Float16 = 0,
ruoyan0120e984f2018-12-12 18:11:25 +000030 Float32 = 1,
telsoa01c577f2c2018-08-31 09:22:23 +010031 QuantisedAsymm8 = 2,
ruoyan0120e984f2018-12-12 18:11:25 +000032 Signed32 = 3,
Nattapat Chaimanowongcd5ac232019-03-19 12:26:36 +000033 Boolean = 4,
34 QuantisedSymm16 = 5
telsoa014fcda012018-03-09 14:13:49 +000035};
36
Derek Lamberti0cff1632018-09-18 16:02:25 +010037enum class DataLayout
38{
39 NCHW = 1,
40 NHWC = 2
41};
42
telsoa014fcda012018-03-09 14:13:49 +000043enum class ActivationFunction
44{
45 Sigmoid = 0,
46 TanH = 1,
47 Linear = 2,
48 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010049 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000050 SoftReLu = 5,
51 LeakyReLu = 6,
52 Abs = 7,
53 Sqrt = 8,
54 Square = 9
55};
56
57enum class PoolingAlgorithm
58{
59 Max = 0,
60 Average = 1,
61 L2 = 2
62};
63
Teresa Charlina9075df2019-06-27 15:41:57 +010064enum class ResizeMethod
65{
66 Bilinear = 0,
67 NearestNeighbor = 1
68};
69
telsoa014fcda012018-03-09 14:13:49 +000070///
71/// The padding method modifies the output of pooling layers.
72/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +010073/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +000074/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +010075/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +000076/// fields in the divisor of Average and L2 pooling, while
77/// Exclude does not.
78///
79enum class PaddingMethod
80{
telsoa01c577f2c2018-08-31 09:22:23 +010081 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +010082 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +010083 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +010084 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +000085};
86
87enum class NormalizationAlgorithmChannel
88{
89 Across = 0,
90 Within = 1
91};
92
93enum class NormalizationAlgorithmMethod
94{
David Beckdcb751f2018-10-03 11:42:42 +010095 /// Krichevsky 2012: Local Brightness Normalization
96 LocalBrightness = 0,
97 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +010098 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +000099};
100
101enum class OutputShapeRounding
102{
103 Floor = 0,
104 Ceiling = 1
105};
106
David Beck9efb57d2018-11-05 13:40:33 +0000107/// Each backend should implement an IBackend.
108class IBackend
109{
110protected:
111 IBackend() {}
112 virtual ~IBackend() {}
113
114public:
115 virtual const BackendId& GetId() const = 0;
116};
117
118using IBackendSharedPtr = std::shared_ptr<IBackend>;
119using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
120
David Beckdcb751f2018-10-03 11:42:42 +0100121/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100122class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000123{
telsoa01c577f2c2018-08-31 09:22:23 +0100124protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000125 IDeviceSpec() {}
126 virtual ~IDeviceSpec() {}
Narumol Prangnawarat87106762019-05-03 15:54:39 +0100127public:
128 virtual const BackendIdSet& GetSupportedBackends() const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000129};
130
131/// Type of identifiers for bindable layers (inputs, outputs).
132using LayerBindingId = int;
133
134class PermutationVector
135{
136public:
137 using ValueType = unsigned int;
138 using SizeType = unsigned int;
139 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
140 using ConstIterator = typename ArrayType::const_iterator;
141
telsoa01c577f2c2018-08-31 09:22:23 +0100142 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000143 /// when source and target potentially have different memory layouts.
144 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100145 /// 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 +0000146 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
147 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
148 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
149 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
150 /// [ 0, 2, 3, 1 ].
151 ///
152 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
153 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
154 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
155 /// [ 0, 3, 1, 2 ].
156 ///
157 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
158
159 PermutationVector(std::initializer_list<ValueType> dimMappings);
160
161 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
162
163 SizeType GetSize() const { return m_NumDimMappings; }
164
165 ConstIterator begin() const { return m_DimMappings.begin(); }
166 ConstIterator end() const { return m_DimMappings.end(); }
167
168 bool IsEqual(const PermutationVector& other) const
169 {
170 return std::equal(begin(), end(), other.begin(), other.end());
171 }
172
173 bool IsInverse(const PermutationVector& other) const
174 {
175 bool isInverse = (GetSize() == other.GetSize());
176 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
177 {
178 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
179 }
180 return isInverse;
181 }
182
183private:
184 ArrayType m_DimMappings;
185 /// Number of valid entries in @ref m_DimMappings
186 SizeType m_NumDimMappings;
187};
188
telsoa01c577f2c2018-08-31 09:22:23 +0100189/// Define LayerGuid type.
surmeh01bceff2f2018-03-29 16:29:27 +0100190using LayerGuid = unsigned int;
191
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000192class ITensorHandle;
193
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000194/// Define the type of callback for the Debug layer to call
195/// @param guid - guid of layer connected to the input of the Debug layer
196/// @param slotIndex - index of the output slot connected to the input of the Debug layer
197/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
198using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000199
David Beck9df2d952018-10-10 15:11:44 +0100200} // namespace armnn