Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame] | 2 | // Copyright © 2017,2022 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 | |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 59 | /// Utility function used to setup an arm_compute::Pooling3dLayerInfo object from given |
| 60 | /// armnn::Pooling3dDescriptor |
| 61 | /// bool fpMixedPrecision |
| 62 | arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(const Pooling3dDescriptor& descriptor, |
| 63 | bool fpMixedPrecision = false); |
| 64 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | /// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 66 | arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc); |
| 67 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector. |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame] | 69 | /// \param perm PermutationVector used in Arm NN Permute layer |
| 70 | /// \return PermutationVector used in ACL Transpose layer |
| 71 | arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 72 | |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 73 | /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector. |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame] | 74 | /// \param perm PermutationVector used in Arm NN Transpose layer |
| 75 | /// \return PermutationVector used in ACL Transpose layer |
| 76 | arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& perm); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 77 | |
Sadik Armagan | f446432 | 2018-12-20 16:19:12 +0000 | [diff] [blame] | 78 | /// Utility function used to setup an arm_compute::Size2D object from width and height values. |
| 79 | arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height); |
| 80 | |
Matthew Sloyan | 2e5d0b2 | 2021-10-21 14:05:31 +0100 | [diff] [blame] | 81 | /// Gets the appropriate PixelValue for the TensorInfo DataType |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 82 | arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float value); |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 83 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 84 | /// Computes the depth multiplier parameter for the Depthwise Conv2d ACL workload. |
| 85 | unsigned int ComputeDepthwiseConv2dDepthMultiplier(armnn::DataLayout layout, |
| 86 | const arm_compute::TensorShape& weightsShape, |
| 87 | const arm_compute::TensorShape& inputShape); |
| 88 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 89 | /// 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] | 90 | template <typename Descriptor> |
| 91 | arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor &descriptor) |
| 92 | { |
| 93 | return arm_compute::PadStrideInfo(descriptor.m_StrideX, |
| 94 | descriptor.m_StrideY, |
| 95 | descriptor.m_PadLeft, |
| 96 | descriptor.m_PadRight, |
| 97 | descriptor.m_PadTop, |
| 98 | descriptor.m_PadBottom, |
| 99 | arm_compute::DimensionRoundingType::FLOOR); |
| 100 | } |
| 101 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor. |
| 103 | template <typename Tensor> |
| 104 | void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo) |
| 105 | { |
| 106 | tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo)); |
| 107 | } |
| 108 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 109 | /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor. |
| 110 | template <typename Tensor> |
| 111 | void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout) |
| 112 | { |
| 113 | tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout)); |
| 114 | } |
| 115 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 116 | template <typename Tensor> |
| 117 | void InitialiseArmComputeTensorEmpty(Tensor& tensor) |
| 118 | { |
| 119 | tensor.allocator()->allocate(); |
| 120 | } |
| 121 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | /// Utility function to free unused tensors after a workload is configured and prepared |
| 123 | template <typename Tensor> |
| 124 | void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor) |
| 125 | { |
| 126 | if (tensor && !tensor->is_used()) |
| 127 | { |
| 128 | tensor.reset(nullptr); |
| 129 | } |
| 130 | } |
| 131 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | // Helper function to obtain byte offset into tensor data |
| 133 | inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 134 | uint32_t depthIndex, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | uint32_t batchIndex, |
| 136 | uint32_t channelIndex, |
| 137 | uint32_t y, |
| 138 | uint32_t x) |
| 139 | { |
| 140 | arm_compute::Coordinates coords; |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 141 | coords.set(4, static_cast<int>(depthIndex)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | coords.set(3, static_cast<int>(batchIndex)); |
| 143 | coords.set(2, static_cast<int>(channelIndex)); |
| 144 | coords.set(1, static_cast<int>(y)); |
| 145 | coords.set(0, static_cast<int>(x)); |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 146 | return armnn::numeric_cast<size_t>(info.offset_element_in_bytes(coords)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | // 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] | 150 | inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 151 | uint32_t depthIndex, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 152 | uint32_t batchIndex, |
| 153 | uint32_t channelIndex, |
| 154 | uint32_t y, |
| 155 | uint32_t x) |
| 156 | { |
| 157 | const arm_compute::TensorShape& shape = info.tensor_shape(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 158 | uint32_t width = static_cast<uint32_t>(shape[0]); |
| 159 | uint32_t height = static_cast<uint32_t>(shape[1]); |
| 160 | uint32_t numChannels = static_cast<uint32_t>(shape[2]); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 161 | uint32_t numBatches = static_cast<uint32_t>(shape[3]); |
| 162 | return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | template <typename T> |
| 166 | void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData) |
| 167 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 168 | // If MaxNumOfTensorDimensions is increased, this loop will need fixing. |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 169 | static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 170 | { |
| 171 | const arm_compute::ITensorInfo& info = *srcTensor.info(); |
| 172 | const arm_compute::TensorShape& shape = info.tensor_shape(); |
| 173 | const uint8_t* const bufferPtr = srcTensor.buffer(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 174 | uint32_t width = static_cast<uint32_t>(shape[0]); |
| 175 | uint32_t height = static_cast<uint32_t>(shape[1]); |
| 176 | uint32_t numChannels = static_cast<uint32_t>(shape[2]); |
| 177 | uint32_t numBatches = static_cast<uint32_t>(shape[3]); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 178 | uint32_t depth = static_cast<uint32_t>(shape[4]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 179 | |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 180 | for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 181 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 182 | for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 183 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 184 | for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 185 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 186 | for (unsigned int y = 0; y < height; ++y) |
| 187 | { |
| 188 | // Copies one row from arm_compute tensor buffer to linear memory buffer. |
| 189 | // A row is the largest contiguous region we can copy, as the tensor data may be using strides. |
| 190 | memcpy( |
| 191 | dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 192 | bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 193 | width * sizeof(T)); |
| 194 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | template <typename T> |
| 202 | void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor) |
| 203 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 204 | // If MaxNumOfTensorDimensions is increased, this loop will need fixing. |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 205 | static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 206 | { |
| 207 | const arm_compute::ITensorInfo& info = *dstTensor.info(); |
| 208 | const arm_compute::TensorShape& shape = info.tensor_shape(); |
| 209 | uint8_t* const bufferPtr = dstTensor.buffer(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 210 | uint32_t width = static_cast<uint32_t>(shape[0]); |
| 211 | uint32_t height = static_cast<uint32_t>(shape[1]); |
| 212 | uint32_t numChannels = static_cast<uint32_t>(shape[2]); |
| 213 | uint32_t numBatches = static_cast<uint32_t>(shape[3]); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 214 | uint32_t depth = static_cast<uint32_t>(shape[4]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 215 | |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 216 | for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 217 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 218 | for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 219 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 220 | for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 221 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 222 | for (unsigned int y = 0; y < height; ++y) |
| 223 | { |
| 224 | // Copies one row from linear memory buffer to arm_compute tensor buffer. |
| 225 | // A row is the largest contiguous region we can copy, as the tensor data may be using strides. |
| 226 | memcpy( |
| 227 | bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 228 | srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0), |
| 229 | width * sizeof(T)); |
| 230 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 237 | /// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions. |
| 238 | /// \tparam ArmComputeType Any type that implements the Dimensions interface |
| 239 | /// \tparam T Shape value type |
| 240 | /// \param shapelike An ArmCompute object that implements the Dimensions interface |
| 241 | /// \param initial A default value to initialise the shape with |
| 242 | /// \return A TensorShape object filled from the Acl shapelike object. |
| 243 | template<typename ArmComputeType, typename T> |
| 244 | TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial) |
| 245 | { |
| 246 | std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial); |
| 247 | for (unsigned int i=0; i < shapelike.num_dimensions(); ++i) |
| 248 | { |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 249 | s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 250 | } |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 251 | return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | /// Get the strides from an ACL strides object |
| 255 | inline TensorShape GetStrides(const arm_compute::Strides& strides) |
| 256 | { |
| 257 | return GetTensorShape(strides, 0U); |
| 258 | } |
| 259 | |
| 260 | /// Get the shape from an ACL shape object |
| 261 | inline TensorShape GetShape(const arm_compute::TensorShape& shape) |
| 262 | { |
| 263 | return GetTensorShape(shape, 1U); |
| 264 | } |
| 265 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 266 | } // namespace armcomputetensorutils |
| 267 | } // namespace armnn |