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> |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 8 | #include <memory> |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 9 | #include "BackendId.hpp" |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 10 | #include "Exceptions.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 11 | |
| 12 | namespace armnn |
| 13 | { |
| 14 | |
| 15 | constexpr unsigned int MaxNumOfTensorDimensions = 4U; |
| 16 | |
| 17 | /// @enum Status enumeration |
| 18 | /// @var Status::Successful |
| 19 | /// @var Status::Failure |
| 20 | enum class Status |
| 21 | { |
| 22 | Success = 0, |
| 23 | Failure = 1 |
| 24 | }; |
| 25 | |
| 26 | enum class DataType |
| 27 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 28 | Float16 = 0, |
| 29 | Float32 = 1, |
| 30 | QuantisedAsymm8 = 2, |
| 31 | Signed32 = 3 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 34 | // Begin: DataLayout |
| 35 | |
Derek Lamberti | 0cff163 | 2018-09-18 16:02:25 +0100 | [diff] [blame] | 36 | enum class DataLayout |
| 37 | { |
| 38 | NCHW = 1, |
| 39 | NHWC = 2 |
| 40 | }; |
| 41 | |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 42 | // Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout |
| 43 | class DataLayoutIndexed |
| 44 | { |
| 45 | public: |
| 46 | DataLayoutIndexed(DataLayout dataLayout) : m_DataLayout(dataLayout) |
| 47 | { |
| 48 | switch (dataLayout) |
| 49 | { |
| 50 | case DataLayout::NHWC: |
| 51 | m_ChannelsIndex = 3; |
| 52 | m_HeightIndex = 1; |
| 53 | m_WidthIndex = 2; |
| 54 | break; |
| 55 | case DataLayout::NCHW: |
| 56 | m_ChannelsIndex = 1; |
| 57 | m_HeightIndex = 2; |
| 58 | m_WidthIndex = 3; |
| 59 | break; |
| 60 | default: |
| 61 | throw InvalidArgumentException("Unknown DataLayout value: " + |
| 62 | std::to_string(static_cast<int>(dataLayout))); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | DataLayout GetDataLayout() const { return m_DataLayout; } |
| 67 | unsigned int GetChannelsIndex() const { return m_ChannelsIndex; } |
| 68 | unsigned int GetHeightIndex() const { return m_HeightIndex; } |
| 69 | unsigned int GetWidthIndex() const { return m_WidthIndex; } |
| 70 | |
| 71 | private: |
| 72 | DataLayout m_DataLayout; |
| 73 | unsigned int m_ChannelsIndex; |
| 74 | unsigned int m_HeightIndex; |
| 75 | unsigned int m_WidthIndex; |
| 76 | }; |
| 77 | |
| 78 | // Conversion methods - implementations in src/armnn/InternalTypes.cpp |
| 79 | bool operator==(const DataLayout& dataLayout, const DataLayoutIndexed& indexed); |
| 80 | bool operator==(const DataLayoutIndexed& indexed, const DataLayout& dataLayout); |
| 81 | |
| 82 | // End: DataLayout |
| 83 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 84 | enum class ActivationFunction |
| 85 | { |
| 86 | Sigmoid = 0, |
| 87 | TanH = 1, |
| 88 | Linear = 2, |
| 89 | ReLu = 3, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 90 | BoundedReLu = 4, ///< min(a, max(b, input)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | SoftReLu = 5, |
| 92 | LeakyReLu = 6, |
| 93 | Abs = 7, |
| 94 | Sqrt = 8, |
| 95 | Square = 9 |
| 96 | }; |
| 97 | |
| 98 | enum class PoolingAlgorithm |
| 99 | { |
| 100 | Max = 0, |
| 101 | Average = 1, |
| 102 | L2 = 2 |
| 103 | }; |
| 104 | |
| 105 | /// |
| 106 | /// The padding method modifies the output of pooling layers. |
| 107 | /// In both supported methods, the values are ignored (they are |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | /// not even zeroes, which would make a difference for max pooling |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | /// a tensor with negative values). The difference between |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | /// IgnoreValue and Exclude is that the former counts the padding |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | /// fields in the divisor of Average and L2 pooling, while |
| 112 | /// Exclude does not. |
| 113 | /// |
| 114 | enum class PaddingMethod |
| 115 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 116 | /// The padding fields count, but are ignored |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 117 | IgnoreValue = 0, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 118 | /// The padding fields don't count and are ignored |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 119 | Exclude = 1 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | enum class NormalizationAlgorithmChannel |
| 123 | { |
| 124 | Across = 0, |
| 125 | Within = 1 |
| 126 | }; |
| 127 | |
| 128 | enum class NormalizationAlgorithmMethod |
| 129 | { |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 130 | /// Krichevsky 2012: Local Brightness Normalization |
| 131 | LocalBrightness = 0, |
| 132 | /// Jarret 2009: Local Contrast Normalization |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | LocalContrast = 1 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | enum class OutputShapeRounding |
| 137 | { |
| 138 | Floor = 0, |
| 139 | Ceiling = 1 |
| 140 | }; |
| 141 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame^] | 142 | /// Each backend should implement an IBackend. |
| 143 | class IBackend |
| 144 | { |
| 145 | protected: |
| 146 | IBackend() {} |
| 147 | virtual ~IBackend() {} |
| 148 | |
| 149 | public: |
| 150 | virtual const BackendId& GetId() const = 0; |
| 151 | }; |
| 152 | |
| 153 | using IBackendSharedPtr = std::shared_ptr<IBackend>; |
| 154 | using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>; |
| 155 | |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 156 | /// Device specific knowledge to be passed to the optimizer. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 157 | class IDeviceSpec |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 158 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 159 | protected: |
| 160 | IDeviceSpec() {}; |
| 161 | virtual ~IDeviceSpec() {}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /// Type of identifiers for bindable layers (inputs, outputs). |
| 165 | using LayerBindingId = int; |
| 166 | |
| 167 | class PermutationVector |
| 168 | { |
| 169 | public: |
| 170 | using ValueType = unsigned int; |
| 171 | using SizeType = unsigned int; |
| 172 | using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>; |
| 173 | using ConstIterator = typename ArrayType::const_iterator; |
| 174 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 175 | /// @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] | 176 | /// when source and target potentially have different memory layouts. |
| 177 | /// |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 178 | /// 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] | 179 | /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding |
| 180 | /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped |
| 181 | /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and |
| 182 | /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array: |
| 183 | /// [ 0, 2, 3, 1 ]. |
| 184 | /// |
| 185 | /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element, |
| 186 | /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have |
| 187 | /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents: |
| 188 | /// [ 0, 3, 1, 2 ]. |
| 189 | /// |
| 190 | PermutationVector(const ValueType *dimMappings, SizeType numDimMappings); |
| 191 | |
| 192 | PermutationVector(std::initializer_list<ValueType> dimMappings); |
| 193 | |
| 194 | ValueType operator[](SizeType i) const { return m_DimMappings.at(i); } |
| 195 | |
| 196 | SizeType GetSize() const { return m_NumDimMappings; } |
| 197 | |
| 198 | ConstIterator begin() const { return m_DimMappings.begin(); } |
| 199 | ConstIterator end() const { return m_DimMappings.end(); } |
| 200 | |
| 201 | bool IsEqual(const PermutationVector& other) const |
| 202 | { |
| 203 | return std::equal(begin(), end(), other.begin(), other.end()); |
| 204 | } |
| 205 | |
| 206 | bool IsInverse(const PermutationVector& other) const |
| 207 | { |
| 208 | bool isInverse = (GetSize() == other.GetSize()); |
| 209 | for (SizeType i = 0; isInverse && (i < GetSize()); ++i) |
| 210 | { |
| 211 | isInverse = (m_DimMappings[other.m_DimMappings[i]] == i); |
| 212 | } |
| 213 | return isInverse; |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | ArrayType m_DimMappings; |
| 218 | /// Number of valid entries in @ref m_DimMappings |
| 219 | SizeType m_NumDimMappings; |
| 220 | }; |
| 221 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 222 | /// Define LayerGuid type. |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 223 | using LayerGuid = unsigned int; |
| 224 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 225 | } // namespace armnn |