blob: 438b6be7413a87c45cc4fd4fc8a270c4469b59c9 [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
64///
65/// The padding method modifies the output of pooling layers.
66/// In both supported methods, the values are ignored (they are
telsoa01c577f2c2018-08-31 09:22:23 +010067/// not even zeroes, which would make a difference for max pooling
telsoa014fcda012018-03-09 14:13:49 +000068/// a tensor with negative values). The difference between
telsoa01c577f2c2018-08-31 09:22:23 +010069/// IgnoreValue and Exclude is that the former counts the padding
telsoa014fcda012018-03-09 14:13:49 +000070/// fields in the divisor of Average and L2 pooling, while
71/// Exclude does not.
72///
73enum class PaddingMethod
74{
telsoa01c577f2c2018-08-31 09:22:23 +010075 /// The padding fields count, but are ignored
David Beckdcb751f2018-10-03 11:42:42 +010076 IgnoreValue = 0,
telsoa01c577f2c2018-08-31 09:22:23 +010077 /// The padding fields don't count and are ignored
David Beckdcb751f2018-10-03 11:42:42 +010078 Exclude = 1
telsoa014fcda012018-03-09 14:13:49 +000079};
80
81enum class NormalizationAlgorithmChannel
82{
83 Across = 0,
84 Within = 1
85};
86
87enum class NormalizationAlgorithmMethod
88{
David Beckdcb751f2018-10-03 11:42:42 +010089 /// Krichevsky 2012: Local Brightness Normalization
90 LocalBrightness = 0,
91 /// Jarret 2009: Local Contrast Normalization
telsoa01c577f2c2018-08-31 09:22:23 +010092 LocalContrast = 1
telsoa014fcda012018-03-09 14:13:49 +000093};
94
95enum class OutputShapeRounding
96{
97 Floor = 0,
98 Ceiling = 1
99};
100
David Beck9efb57d2018-11-05 13:40:33 +0000101/// Each backend should implement an IBackend.
102class IBackend
103{
104protected:
105 IBackend() {}
106 virtual ~IBackend() {}
107
108public:
109 virtual const BackendId& GetId() const = 0;
110};
111
112using IBackendSharedPtr = std::shared_ptr<IBackend>;
113using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>;
114
David Beckdcb751f2018-10-03 11:42:42 +0100115/// Device specific knowledge to be passed to the optimizer.
telsoa01c577f2c2018-08-31 09:22:23 +0100116class IDeviceSpec
telsoa014fcda012018-03-09 14:13:49 +0000117{
telsoa01c577f2c2018-08-31 09:22:23 +0100118protected:
Matteo Martincigh9c5d33a2019-02-07 17:52:41 +0000119 IDeviceSpec() {}
120 virtual ~IDeviceSpec() {}
telsoa014fcda012018-03-09 14:13:49 +0000121};
122
123/// Type of identifiers for bindable layers (inputs, outputs).
124using LayerBindingId = int;
125
126class PermutationVector
127{
128public:
129 using ValueType = unsigned int;
130 using SizeType = unsigned int;
131 using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>;
132 using ConstIterator = typename ArrayType::const_iterator;
133
telsoa01c577f2c2018-08-31 09:22:23 +0100134 /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination,
telsoa014fcda012018-03-09 14:13:49 +0000135 /// when source and target potentially have different memory layouts.
136 ///
telsoa01c577f2c2018-08-31 09:22:23 +0100137 /// 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 +0000138 /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding
139 /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped
140 /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and
141 /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array:
142 /// [ 0, 2, 3, 1 ].
143 ///
144 /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element,
145 /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have
146 /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents:
147 /// [ 0, 3, 1, 2 ].
148 ///
149 PermutationVector(const ValueType *dimMappings, SizeType numDimMappings);
150
151 PermutationVector(std::initializer_list<ValueType> dimMappings);
152
153 ValueType operator[](SizeType i) const { return m_DimMappings.at(i); }
154
155 SizeType GetSize() const { return m_NumDimMappings; }
156
157 ConstIterator begin() const { return m_DimMappings.begin(); }
158 ConstIterator end() const { return m_DimMappings.end(); }
159
160 bool IsEqual(const PermutationVector& other) const
161 {
162 return std::equal(begin(), end(), other.begin(), other.end());
163 }
164
165 bool IsInverse(const PermutationVector& other) const
166 {
167 bool isInverse = (GetSize() == other.GetSize());
168 for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
169 {
170 isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
171 }
172 return isInverse;
173 }
174
175private:
176 ArrayType m_DimMappings;
177 /// Number of valid entries in @ref m_DimMappings
178 SizeType m_NumDimMappings;
179};
180
telsoa01c577f2c2018-08-31 09:22:23 +0100181/// Define LayerGuid type.
surmeh01bceff2f2018-03-29 16:29:27 +0100182using LayerGuid = unsigned int;
183
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000184class ITensorHandle;
185
Nattapat Chaimanowong317cae52019-03-28 10:29:12 +0000186/// Define the type of callback for the Debug layer to call
187/// @param guid - guid of layer connected to the input of the Debug layer
188/// @param slotIndex - index of the output slot connected to the input of the Debug layer
189/// @param tensorHandle - TensorHandle for the input tensor to the Debug layer
190using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>;
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000191
David Beck9df2d952018-10-10 15:11:44 +0100192} // namespace armnn