telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <array> |
Matthew Bentham | 47bfac4 | 2019-03-25 12:30:56 +0000 | [diff] [blame] | 8 | #include <functional> |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 9 | #include <memory> |
janeil01 | c4946c7 | 2019-11-07 09:32:28 +0000 | [diff] [blame^] | 10 | #include <stdint.h> |
Jim Flynn | 44db7c3 | 2019-03-22 15:58:39 +0000 | [diff] [blame] | 11 | #include "BackendId.hpp" |
| 12 | #include "Exceptions.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 17 | constexpr unsigned int MaxNumOfTensorDimensions = 5U; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | |
| 19 | /// @enum Status enumeration |
| 20 | /// @var Status::Successful |
| 21 | /// @var Status::Failure |
| 22 | enum class Status |
| 23 | { |
| 24 | Success = 0, |
| 25 | Failure = 1 |
| 26 | }; |
| 27 | |
| 28 | enum class DataType |
| 29 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | Float16 = 0, |
ruoyan01 | 20e984f | 2018-12-12 18:11:25 +0000 | [diff] [blame] | 31 | Float32 = 1, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | QuantisedAsymm8 = 2, |
ruoyan01 | 20e984f | 2018-12-12 18:11:25 +0000 | [diff] [blame] | 33 | Signed32 = 3, |
Nattapat Chaimanowong | cd5ac23 | 2019-03-19 12:26:36 +0000 | [diff] [blame] | 34 | Boolean = 4, |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 35 | QuantisedSymm16 = 5, |
| 36 | QuantizedSymm8PerAxis = 6 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Derek Lamberti | 0cff163 | 2018-09-18 16:02:25 +0100 | [diff] [blame] | 39 | enum class DataLayout |
| 40 | { |
| 41 | NCHW = 1, |
| 42 | NHWC = 2 |
| 43 | }; |
| 44 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 45 | enum class ActivationFunction |
| 46 | { |
| 47 | Sigmoid = 0, |
| 48 | TanH = 1, |
| 49 | Linear = 2, |
| 50 | ReLu = 3, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | BoundedReLu = 4, ///< min(a, max(b, input)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | SoftReLu = 5, |
| 53 | LeakyReLu = 6, |
| 54 | Abs = 7, |
| 55 | Sqrt = 8, |
| 56 | Square = 9 |
| 57 | }; |
| 58 | |
Narumol Prangnawarat | 8d001d4 | 2019-09-09 15:01:18 +0100 | [diff] [blame] | 59 | enum class ArgMinMaxFunction |
| 60 | { |
| 61 | Min = 0, |
| 62 | Max = 1 |
| 63 | }; |
| 64 | |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 65 | enum class ComparisonOperation |
| 66 | { |
| 67 | Equal = 0, |
| 68 | Greater = 1, |
| 69 | GreaterOrEqual = 2, |
| 70 | Less = 3, |
| 71 | LessOrEqual = 4, |
| 72 | NotEqual = 5 |
| 73 | }; |
| 74 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 75 | enum class PoolingAlgorithm |
| 76 | { |
| 77 | Max = 0, |
| 78 | Average = 1, |
| 79 | L2 = 2 |
| 80 | }; |
| 81 | |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 82 | enum class ResizeMethod |
| 83 | { |
| 84 | Bilinear = 0, |
| 85 | NearestNeighbor = 1 |
| 86 | }; |
| 87 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 88 | /// |
| 89 | /// The padding method modifies the output of pooling layers. |
| 90 | /// In both supported methods, the values are ignored (they are |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | /// not even zeroes, which would make a difference for max pooling |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | /// a tensor with negative values). The difference between |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | /// IgnoreValue and Exclude is that the former counts the padding |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | /// fields in the divisor of Average and L2 pooling, while |
| 95 | /// Exclude does not. |
| 96 | /// |
| 97 | enum class PaddingMethod |
| 98 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 99 | /// The padding fields count, but are ignored |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 100 | IgnoreValue = 0, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | /// The padding fields don't count and are ignored |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 102 | Exclude = 1 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | enum class NormalizationAlgorithmChannel |
| 106 | { |
| 107 | Across = 0, |
| 108 | Within = 1 |
| 109 | }; |
| 110 | |
| 111 | enum class NormalizationAlgorithmMethod |
| 112 | { |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 113 | /// Krichevsky 2012: Local Brightness Normalization |
| 114 | LocalBrightness = 0, |
| 115 | /// Jarret 2009: Local Contrast Normalization |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 116 | LocalContrast = 1 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | enum class OutputShapeRounding |
| 120 | { |
| 121 | Floor = 0, |
| 122 | Ceiling = 1 |
| 123 | }; |
| 124 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 125 | /// Each backend should implement an IBackend. |
| 126 | class IBackend |
| 127 | { |
| 128 | protected: |
| 129 | IBackend() {} |
| 130 | virtual ~IBackend() {} |
| 131 | |
| 132 | public: |
| 133 | virtual const BackendId& GetId() const = 0; |
| 134 | }; |
| 135 | |
| 136 | using IBackendSharedPtr = std::shared_ptr<IBackend>; |
| 137 | using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>; |
| 138 | |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 139 | /// Device specific knowledge to be passed to the optimizer. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | class IDeviceSpec |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | protected: |
Matteo Martincigh | 9c5d33a | 2019-02-07 17:52:41 +0000 | [diff] [blame] | 143 | IDeviceSpec() {} |
| 144 | virtual ~IDeviceSpec() {} |
Narumol Prangnawarat | 8710676 | 2019-05-03 15:54:39 +0100 | [diff] [blame] | 145 | public: |
| 146 | virtual const BackendIdSet& GetSupportedBackends() const = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /// Type of identifiers for bindable layers (inputs, outputs). |
| 150 | using LayerBindingId = int; |
| 151 | |
| 152 | class PermutationVector |
| 153 | { |
| 154 | public: |
| 155 | using ValueType = unsigned int; |
| 156 | using SizeType = unsigned int; |
| 157 | using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>; |
| 158 | using ConstIterator = typename ArrayType::const_iterator; |
| 159 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 160 | /// @param dimMappings - Indicates how to translate tensor elements from a given source into the target destination, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | /// when source and target potentially have different memory layouts. |
| 162 | /// |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 163 | /// E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels), |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding |
| 165 | /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped |
| 166 | /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and |
| 167 | /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array: |
| 168 | /// [ 0, 2, 3, 1 ]. |
| 169 | /// |
| 170 | /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element, |
| 171 | /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have |
| 172 | /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents: |
| 173 | /// [ 0, 3, 1, 2 ]. |
| 174 | /// |
| 175 | PermutationVector(const ValueType *dimMappings, SizeType numDimMappings); |
| 176 | |
| 177 | PermutationVector(std::initializer_list<ValueType> dimMappings); |
| 178 | |
| 179 | ValueType operator[](SizeType i) const { return m_DimMappings.at(i); } |
| 180 | |
| 181 | SizeType GetSize() const { return m_NumDimMappings; } |
| 182 | |
| 183 | ConstIterator begin() const { return m_DimMappings.begin(); } |
| 184 | ConstIterator end() const { return m_DimMappings.end(); } |
| 185 | |
| 186 | bool IsEqual(const PermutationVector& other) const |
| 187 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 188 | if (m_NumDimMappings != other.m_NumDimMappings) return false; |
| 189 | for (unsigned int i = 0; i < m_NumDimMappings; ++i) |
| 190 | { |
| 191 | if (m_DimMappings[i] != other.m_DimMappings[i]) return false; |
| 192 | } |
| 193 | return true; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool IsInverse(const PermutationVector& other) const |
| 197 | { |
| 198 | bool isInverse = (GetSize() == other.GetSize()); |
| 199 | for (SizeType i = 0; isInverse && (i < GetSize()); ++i) |
| 200 | { |
| 201 | isInverse = (m_DimMappings[other.m_DimMappings[i]] == i); |
| 202 | } |
| 203 | return isInverse; |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | ArrayType m_DimMappings; |
| 208 | /// Number of valid entries in @ref m_DimMappings |
| 209 | SizeType m_NumDimMappings; |
| 210 | }; |
| 211 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 212 | /// Define LayerGuid type. |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 213 | using LayerGuid = unsigned int; |
| 214 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 215 | class ITensorHandle; |
| 216 | |
Nattapat Chaimanowong | 317cae5 | 2019-03-28 10:29:12 +0000 | [diff] [blame] | 217 | /// Define the type of callback for the Debug layer to call |
| 218 | /// @param guid - guid of layer connected to the input of the Debug layer |
| 219 | /// @param slotIndex - index of the output slot connected to the input of the Debug layer |
| 220 | /// @param tensorHandle - TensorHandle for the input tensor to the Debug layer |
| 221 | using DebugCallbackFunction = std::function<void(LayerGuid guid, unsigned int slotIndex, ITensorHandle* tensorHandle)>; |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 222 | |
janeil01 | c4946c7 | 2019-11-07 09:32:28 +0000 | [diff] [blame^] | 223 | |
| 224 | namespace profiling |
| 225 | { |
| 226 | |
| 227 | class ProfilingGuid |
| 228 | { |
| 229 | public: |
| 230 | ProfilingGuid(uint64_t guid) : m_Guid(guid) {} |
| 231 | |
| 232 | operator uint64_t() const { return m_Guid; } |
| 233 | |
| 234 | bool operator==(const ProfilingGuid& other) const |
| 235 | { |
| 236 | return m_Guid == other.m_Guid; |
| 237 | } |
| 238 | |
| 239 | bool operator!=(const ProfilingGuid& other) const |
| 240 | { |
| 241 | return m_Guid != other.m_Guid; |
| 242 | } |
| 243 | |
| 244 | bool operator<(const ProfilingGuid& other) const |
| 245 | { |
| 246 | return m_Guid < other.m_Guid; |
| 247 | } |
| 248 | |
| 249 | bool operator<=(const ProfilingGuid& other) const |
| 250 | { |
| 251 | return m_Guid <= other.m_Guid; |
| 252 | } |
| 253 | |
| 254 | bool operator>(const ProfilingGuid& other) const |
| 255 | { |
| 256 | return m_Guid > other.m_Guid; |
| 257 | } |
| 258 | |
| 259 | bool operator>=(const ProfilingGuid& other) const |
| 260 | { |
| 261 | return m_Guid >= other.m_Guid; |
| 262 | } |
| 263 | |
| 264 | protected: |
| 265 | uint64_t m_Guid; |
| 266 | }; |
| 267 | |
| 268 | /// Strongly typed guids to distinguish between those generated at runtime, and those that are statically defined. |
| 269 | struct ProfilingDynamicGuid : public ProfilingGuid |
| 270 | { |
| 271 | using ProfilingGuid::ProfilingGuid; |
| 272 | }; |
| 273 | |
| 274 | struct ProfilingStaticGuid : public ProfilingGuid |
| 275 | { |
| 276 | using ProfilingGuid::ProfilingGuid; |
| 277 | }; |
| 278 | |
| 279 | } // namespace profiling |
| 280 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 281 | } // namespace armnn |
janeil01 | c4946c7 | 2019-11-07 09:32:28 +0000 | [diff] [blame^] | 282 | |
| 283 | |
| 284 | namespace std |
| 285 | { |
| 286 | // make ProfilingGuid hashable |
| 287 | template<> |
| 288 | struct hash<armnn::profiling::ProfilingGuid> |
| 289 | { |
| 290 | std::size_t operator()(armnn::profiling::ProfilingGuid const& guid) const noexcept |
| 291 | { |
| 292 | return hash<uint64_t>()(uint64_t(guid)); |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | // make ProfilingDynamicGuid hashable |
| 297 | template<> |
| 298 | struct hash<armnn::profiling::ProfilingDynamicGuid> |
| 299 | { |
| 300 | std::size_t operator()(armnn::profiling::ProfilingDynamicGuid const& guid) const noexcept |
| 301 | { |
| 302 | return hash<uint64_t>()(uint64_t(guid)); |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | // make ProfilingStaticGuid hashable |
| 307 | template<> |
| 308 | struct hash<armnn::profiling::ProfilingStaticGuid> |
| 309 | { |
| 310 | std::size_t operator()(armnn::profiling::ProfilingStaticGuid const& guid) const noexcept |
| 311 | { |
| 312 | return hash<uint64_t>()(uint64_t(guid)); |
| 313 | } |
| 314 | }; |
| 315 | } |