blob: 693a0505863650045c667a47ed79d28df4439205 [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,
Nattapat Chaimanowongcd5ac232019-03-19 12:26:36 +000032 Boolean = 4,
33 QuantisedSymm16 = 5
telsoa014fcda012018-03-09 14:13:49 +000034};
35
Derek Lamberti0cff1632018-09-18 16:02:25 +010036enum class DataLayout
37{
38 NCHW = 1,
39 NHWC = 2
40};
41
telsoa014fcda012018-03-09 14:13:49 +000042enum class ActivationFunction
43{
44 Sigmoid = 0,
45 TanH = 1,
46 Linear = 2,
47 ReLu = 3,
telsoa01c577f2c2018-08-31 09:22:23 +010048 BoundedReLu = 4, ///< min(a, max(b, input))
telsoa014fcda012018-03-09 14:13:49 +000049 SoftReLu = 5,
50 LeakyReLu = 6,
51 Abs = 7,
52 Sqrt = 8,
53 Square = 9
54};
55
56enum class PoolingAlgorithm
57{
58 Max = 0,
59 Average = 1,
60 L2 = 2
61};
62
63///
64/// The padding method modifies the output of pooling layers.
65/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +010066/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +000067/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +010068/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +000069/// fields in the divisor of Average and L2 pooling, while
70/// Exclude does not.
71///
72enum class PaddingMethod
73{
telsoa01c577f2c2018-08-31 09:22:23 +010074 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +010075 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +010076 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +010077 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +000078};
79
80enum class NormalizationAlgorithmChannel
81{
82 Across = 0,
83 Within = 1
84};
85
86enum class NormalizationAlgorithmMethod
87{
David Beckdcb751f2018-10-03 11:42:42 +010088 /// Krichevsky 2012: Local Brightness Normalization
89 LocalBrightness = 0,
90 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +010091 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +000092};
93
94enum class OutputShapeRounding
95{
96 Floor = 0,
97 Ceiling = 1
98};
99
David Beck9efb57d2018-11-05 13:40:33 +0000100/// Each backend should implement an IBackend.
101class IBackend
102{
103protected:
104 IBackend() {}
105 virtual ~IBackend() {}
106
107public:
108 virtual const BackendId& GetId() const = 0;
109};
110
111using IBackendSharedPtr = std::shared_ptr<IBackend>;
112using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
113
David Beckdcb751f2018-10-03 11:42:42 +0100114/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100115class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000116{
telsoa01c577f2c2018-08-31 09:22:23 +0100117protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000118 IDeviceSpec() {}
119 virtual ~IDeviceSpec() {}
telsoa014fcda012018-03-09 14:13:49 +0000120};
121
122/// Type of identifiers for bindable layers (inputs, outputs).
123using LayerBindingId = int;
124
125class PermutationVector
126{
127public:
128 using ValueType = unsigned int;
129 using SizeType = unsigned int;
130 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
131 using ConstIterator = typename ArrayType::const_iterator;
132
telsoa01c577f2c2018-08-31 09:22:23 +0100133 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000134 /// when source and target potentially have different memory layouts.
135 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100136 /// 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 +0000137 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
138 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
139 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
140 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
141 /// [ 0, 2, 3, 1 ].
142 ///
143 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
144 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
145 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
146 /// [ 0, 3, 1, 2 ].
147 ///
148 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
149
150 PermutationVector(std::initializer_list<ValueType> dimMappings);
151
152 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
153
154 SizeType GetSize() const { return m_NumDimMappings; }
155
156 ConstIterator begin() const { return m_DimMappings.begin(); }
157 ConstIterator end() const { return m_DimMappings.end(); }
158
159 bool IsEqual(const PermutationVector& other) const
160 {
161 return std::equal(begin(), end(), other.begin(), other.end());
162 }
163
164 bool IsInverse(const PermutationVector& other) const
165 {
166 bool isInverse = (GetSize() == other.GetSize());
167 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
168 {
169 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
170 }
171 return isInverse;
172 }
173
174private:
175 ArrayType m_DimMappings;
176 /// Number of valid entries in @ref m_DimMappings
177 SizeType m_NumDimMappings;
178};
179
telsoa01c577f2c2018-08-31 09:22:23 +0100180/// Define LayerGuid type.
surmeh01bceff2f2018-03-29 16:29:27 +0100181using LayerGuid = unsigned int;
182
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000183class ITensorHandle;
184
185/// Define the type of callback for the debug layer to call
186using DebugCallbackFunction = std::function<void(LayerGuid, unsigned int, ITensorHandle*)>;
187
David Beck9df2d952018-10-10 15:11:44 +0100188} // namespace armnn