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 | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 9 | #include "ILayerSupport.hpp" |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame^] | 10 | #include "BackendId.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 | |
Derek Lamberti | 0cff163 | 2018-09-18 16:02:25 +0100 | [diff] [blame] | 34 | enum class DataLayout |
| 35 | { |
| 36 | NCHW = 1, |
| 37 | NHWC = 2 |
| 38 | }; |
| 39 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | enum class ActivationFunction |
| 41 | { |
| 42 | Sigmoid = 0, |
| 43 | TanH = 1, |
| 44 | Linear = 2, |
| 45 | ReLu = 3, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 46 | BoundedReLu = 4, ///< min(a, max(b, input)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | SoftReLu = 5, |
| 48 | LeakyReLu = 6, |
| 49 | Abs = 7, |
| 50 | Sqrt = 8, |
| 51 | Square = 9 |
| 52 | }; |
| 53 | |
| 54 | enum class PoolingAlgorithm |
| 55 | { |
| 56 | Max = 0, |
| 57 | Average = 1, |
| 58 | L2 = 2 |
| 59 | }; |
| 60 | |
| 61 | /// |
| 62 | /// The padding method modifies the output of pooling layers. |
| 63 | /// In both supported methods, the values are ignored (they are |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | /// not even zeroes, which would make a difference for max pooling |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | /// a tensor with negative values). The difference between |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 66 | /// IgnoreValue and Exclude is that the former counts the padding |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | /// fields in the divisor of Average and L2 pooling, while |
| 68 | /// Exclude does not. |
| 69 | /// |
| 70 | enum class PaddingMethod |
| 71 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | /// The padding fields count, but are ignored |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 73 | IgnoreValue = 0, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | /// The padding fields don't count and are ignored |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 75 | Exclude = 1 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | enum class NormalizationAlgorithmChannel |
| 79 | { |
| 80 | Across = 0, |
| 81 | Within = 1 |
| 82 | }; |
| 83 | |
| 84 | enum class NormalizationAlgorithmMethod |
| 85 | { |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 86 | /// Krichevsky 2012: Local Brightness Normalization |
| 87 | LocalBrightness = 0, |
| 88 | /// Jarret 2009: Local Contrast Normalization |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 89 | LocalContrast = 1 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | enum class OutputShapeRounding |
| 93 | { |
| 94 | Floor = 0, |
| 95 | Ceiling = 1 |
| 96 | }; |
| 97 | |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 98 | /// Each backend should implement an IBackend. |
| 99 | class IBackend |
| 100 | { |
| 101 | protected: |
| 102 | IBackend() {} |
| 103 | virtual ~IBackend() {} |
| 104 | |
| 105 | public: |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame^] | 106 | virtual const BackendId& GetId() const = 0; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 107 | virtual const ILayerSupport& GetLayerSupport() const = 0; |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 108 | }; |
| 109 | |
David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 110 | using IBackendSharedPtr = std::shared_ptr<IBackend>; |
| 111 | using IBackendUniquePtr = std::unique_ptr<IBackend, void(*)(IBackend* backend)>; |
David Beck | dcb751f | 2018-10-03 11:42:42 +0100 | [diff] [blame] | 112 | |
| 113 | /// Device specific knowledge to be passed to the optimizer. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 114 | class IDeviceSpec |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 116 | protected: |
| 117 | IDeviceSpec() {}; |
| 118 | virtual ~IDeviceSpec() {}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /// Type of identifiers for bindable layers (inputs, outputs). |
| 122 | using LayerBindingId = int; |
| 123 | |
| 124 | class PermutationVector |
| 125 | { |
| 126 | public: |
| 127 | using ValueType = unsigned int; |
| 128 | using SizeType = unsigned int; |
| 129 | using ArrayType = std::array<ValueType, MaxNumOfTensorDimensions>; |
| 130 | using ConstIterator = typename ArrayType::const_iterator; |
| 131 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | /// @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] | 133 | /// when source and target potentially have different memory layouts. |
| 134 | /// |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | /// 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] | 136 | /// which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding |
| 137 | /// ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped |
| 138 | /// to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and |
| 139 | /// Channels (2 -> 3 and 3 -> 1). This will lead to @ref m_DimMappings pointing to the following array: |
| 140 | /// [ 0, 2, 3, 1 ]. |
| 141 | /// |
| 142 | /// Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element, |
| 143 | /// Channels, Height, Width) being written to a destination with the format mentioned above. We now have |
| 144 | /// 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following @ref m_DimMappings contents: |
| 145 | /// [ 0, 3, 1, 2 ]. |
| 146 | /// |
| 147 | PermutationVector(const ValueType *dimMappings, SizeType numDimMappings); |
| 148 | |
| 149 | PermutationVector(std::initializer_list<ValueType> dimMappings); |
| 150 | |
| 151 | ValueType operator[](SizeType i) const { return m_DimMappings.at(i); } |
| 152 | |
| 153 | SizeType GetSize() const { return m_NumDimMappings; } |
| 154 | |
| 155 | ConstIterator begin() const { return m_DimMappings.begin(); } |
| 156 | ConstIterator end() const { return m_DimMappings.end(); } |
| 157 | |
| 158 | bool IsEqual(const PermutationVector& other) const |
| 159 | { |
| 160 | return std::equal(begin(), end(), other.begin(), other.end()); |
| 161 | } |
| 162 | |
| 163 | bool IsInverse(const PermutationVector& other) const |
| 164 | { |
| 165 | bool isInverse = (GetSize() == other.GetSize()); |
| 166 | for (SizeType i = 0; isInverse && (i < GetSize()); ++i) |
| 167 | { |
| 168 | isInverse = (m_DimMappings[other.m_DimMappings[i]] == i); |
| 169 | } |
| 170 | return isInverse; |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | ArrayType m_DimMappings; |
| 175 | /// Number of valid entries in @ref m_DimMappings |
| 176 | SizeType m_NumDimMappings; |
| 177 | }; |
| 178 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 179 | /// Define LayerGuid type. |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 180 | using LayerGuid = unsigned int; |
| 181 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame^] | 182 | } // namespace armnn |