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