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