Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | ec5f7d1 | 2021-10-22 17:15:00 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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 <armnn/Tensor.hpp> |
| 8 | #include <armnn/DescriptorsFwd.hpp> |
| 9 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 10 | #include <armnn/utility/NumericCast.hpp> |
| 11 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | #include <arm_compute/core/ITensor.h> |
| 13 | #include <arm_compute/core/TensorInfo.h> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 14 | #include <arm_compute/core/Types.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 16 | #include <Half.hpp> |
| 17 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | class ITensorHandle; |
| 21 | |
| 22 | namespace armcomputetensorutils |
| 23 | { |
| 24 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 25 | /// Utility function to map an armnn::DataType to corresponding arm_compute::DataType. |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 26 | arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType, bool multiScales); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | |
Cathal Corbett | fd5bec4 | 2022-03-03 15:13:23 +0000 | [diff] [blame] | 28 | /// Utility function to map an arm_compute::DataType to corresponding armnn::DataType. |
| 29 | armnn::DataType GetArmNNDataType(arm_compute::DataType datatype); |
| 30 | |
Matthew Bentham | fd89996 | 2018-12-31 15:49:42 +0000 | [diff] [blame] | 31 | /// Utility function used to set up an arm_compute::Coordinates from a vector of ArmNN Axes for reduction functions |
| 32 | arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions, |
| 33 | unsigned int originalInputRank, |
| 34 | const std::vector<unsigned int>& armnnAxes); |
| 35 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | /// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape); |
| 38 | |
| 39 | /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | /// armnn::ITensorInfo. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo); |
| 42 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 43 | /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given |
| 44 | /// armnn::ITensorInfo. |
| 45 | /// armnn::DataLayout. |
| 46 | arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo, |
| 47 | armnn::DataLayout dataLayout); |
| 48 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 49 | /// Utility function used to convert armnn::DataLayout to arm_compute::DataLayout |
| 50 | /// armnn::DataLayout. |
| 51 | arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout); |
| 52 | |
Sadik Armagan | a3600ba | 2019-10-10 10:43:20 +0100 | [diff] [blame] | 53 | /// Utility function used to setup an arm_compute::PoolingLayerInfo object from given |
| 54 | /// armnn::Pooling2dDescriptor |
| 55 | /// bool fpMixedPrecision |
| 56 | arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor, |
| 57 | bool fpMixedPrecision = false); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | /// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc); |
| 61 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& vector); |
| 64 | |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 65 | /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector. |
| 66 | arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& vector); |
| 67 | |
Sadik Armagan | f446432 | 2018-12-20 16:19:12 +0000 | [diff] [blame] | 68 | /// Utility function used to setup an arm_compute::Size2D object from width and height values. |
| 69 | arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height); |
| 70 | |
Matthew Sloyan | 2e5d0b2 | 2021-10-21 14:05:31 +0100 | [diff] [blame] | 71 | /// Gets the appropriate PixelValue for the TensorInfo DataType |
| 72 | arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float pixelValue); |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 73 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | /// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 75 | template <typename Descriptor> |
| 76 | arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor &descriptor) |
| 77 | { |
| 78 | return arm_compute::PadStrideInfo(descriptor.m_StrideX, |
| 79 | descriptor.m_StrideY, |
| 80 | descriptor.m_PadLeft, |
| 81 | descriptor.m_PadRight, |
| 82 | descriptor.m_PadTop, |
| 83 | descriptor.m_PadBottom, |
| 84 | arm_compute::DimensionRoundingType::FLOOR); |
| 85 | } |
| 86 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 87 | /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor. |
| 88 | template <typename Tensor> |
| 89 | void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo) |
| 90 | { |
| 91 | tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo)); |
| 92 | } |
| 93 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 94 | /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor. |
| 95 | template <typename Tensor> |
| 96 | void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout) |
| 97 | { |
| 98 | tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout)); |
| 99 | } |
| 100 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | template <typename Tensor> |
| 102 | void InitialiseArmComputeTensorEmpty(Tensor& tensor) |
| 103 | { |
| 104 | tensor.allocator()->allocate(); |
| 105 | } |
| 106 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 107 | /// Utility function to free unused tensors after a workload is configured and prepared |
| 108 | template <typename Tensor> |
| 109 | void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor) |
| 110 | { |
| 111 | if (tensor && !tensor->is_used()) |
| 112 | { |
| 113 | tensor.reset(nullptr); |
| 114 | } |
| 115 | } |
| 116 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 117 | // Helper function to obtain byte offset into tensor data |
| 118 | inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 119 | uint32_t depthIndex, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | uint32_t batchIndex, |
| 121 | uint32_t channelIndex, |
| 122 | uint32_t y, |
| 123 | uint32_t x) |
| 124 | { |
| 125 | arm_compute::Coordinates coords; |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 126 | coords.set(4, static_cast<int>(depthIndex)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | coords.set(3, static_cast<int>(batchIndex)); |
| 128 | coords.set(2, static_cast<int>(channelIndex)); |
| 129 | coords.set(1, static_cast<int>(y)); |
| 130 | coords.set(0, static_cast<int>(x)); |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 131 | return armnn::numeric_cast<size_t>(info.offset_element_in_bytes(coords)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | } |
| 133 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 134 | // Helper function to obtain element offset into data buffer representing tensor data (assuming no strides). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 136 | uint32_t depthIndex, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 137 | uint32_t batchIndex, |
| 138 | uint32_t channelIndex, |
| 139 | uint32_t y, |
| 140 | uint32_t x) |
| 141 | { |
| 142 | const arm_compute::TensorShape& shape = info.tensor_shape(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | uint32_t width = static_cast<uint32_t>(shape[0]); |
| 144 | uint32_t height = static_cast<uint32_t>(shape[1]); |
| 145 | uint32_t numChannels = static_cast<uint32_t>(shape[2]); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 146 | uint32_t numBatches = static_cast<uint32_t>(shape[3]); |
| 147 | return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | template <typename T> |
| 151 | void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData) |
| 152 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 153 | // If MaxNumOfTensorDimensions is increased, this loop will need fixing. |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 154 | static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | { |
| 156 | const arm_compute::ITensorInfo& info = *srcTensor.info(); |
| 157 | const arm_compute::TensorShape& shape = info.tensor_shape(); |
| 158 | const uint8_t* const bufferPtr = srcTensor.buffer(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 159 | uint32_t width = static_cast<uint32_t>(shape[0]); |
| 160 | uint32_t height = static_cast<uint32_t>(shape[1]); |
| 161 | uint32_t numChannels = static_cast<uint32_t>(shape[2]); |
| 162 | uint32_t numBatches = static_cast<uint32_t>(shape[3]); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 163 | uint32_t depth = static_cast<uint32_t>(shape[4]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 165 | for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 166 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 167 | for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 169 | for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 170 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 171 | for (unsigned int y = 0; y < height; ++y) |
| 172 | { |
| 173 | // Copies one row from arm_compute tensor buffer to linear memory buffer. |
| 174 | // A row is the largest contiguous region we can copy, as the tensor data may be using strides. |
| 175 | memcpy( |
| 176 | dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 177 | bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 178 | width * sizeof(T)); |
| 179 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | template <typename T> |
| 187 | void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor) |
| 188 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 189 | // If MaxNumOfTensorDimensions is increased, this loop will need fixing. |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 190 | static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 191 | { |
| 192 | const arm_compute::ITensorInfo& info = *dstTensor.info(); |
| 193 | const arm_compute::TensorShape& shape = info.tensor_shape(); |
| 194 | uint8_t* const bufferPtr = dstTensor.buffer(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 195 | uint32_t width = static_cast<uint32_t>(shape[0]); |
| 196 | uint32_t height = static_cast<uint32_t>(shape[1]); |
| 197 | uint32_t numChannels = static_cast<uint32_t>(shape[2]); |
| 198 | uint32_t numBatches = static_cast<uint32_t>(shape[3]); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 199 | uint32_t depth = static_cast<uint32_t>(shape[4]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 200 | |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 201 | for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 202 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 203 | for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 205 | for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 206 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 207 | for (unsigned int y = 0; y < height; ++y) |
| 208 | { |
| 209 | // Copies one row from linear memory buffer to arm_compute tensor buffer. |
| 210 | // A row is the largest contiguous region we can copy, as the tensor data may be using strides. |
| 211 | memcpy( |
| 212 | bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 213 | srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 214 | width * sizeof(T)); |
| 215 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 222 | /// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions. |
| 223 | /// \tparam ArmComputeType Any type that implements the Dimensions interface |
| 224 | /// \tparam T Shape value type |
| 225 | /// \param shapelike An ArmCompute object that implements the Dimensions interface |
| 226 | /// \param initial A default value to initialise the shape with |
| 227 | /// \return A TensorShape object filled from the Acl shapelike object. |
| 228 | template<typename ArmComputeType, typename T> |
| 229 | TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial) |
| 230 | { |
| 231 | std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial); |
| 232 | for (unsigned int i=0; i < shapelike.num_dimensions(); ++i) |
| 233 | { |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 234 | s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 235 | } |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 236 | return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | /// Get the strides from an ACL strides object |
| 240 | inline TensorShape GetStrides(const arm_compute::Strides& strides) |
| 241 | { |
| 242 | return GetTensorShape(strides, 0U); |
| 243 | } |
| 244 | |
| 245 | /// Get the shape from an ACL shape object |
| 246 | inline TensorShape GetShape(const arm_compute::TensorShape& shape) |
| 247 | { |
| 248 | return GetTensorShape(shape, 1U); |
| 249 | } |
| 250 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 251 | } // namespace armcomputetensorutils |
| 252 | } // namespace armnn |