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