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