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