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