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