blob: fa455b746bc408d68c38362e55d6f7993662481c [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
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>
surmeh013537c2c2018-05-18 16:31:43 +010012#include <arm_compute/core/Types.h>
Sadik Armaganf4464322018-12-20 16:19:12 +000013#include <arm_compute/core/Size2D.h>
telsoa014fcda012018-03-09 14:13:49 +000014
15#include <boost/cast.hpp>
16
17namespace armnn
18{
19class ITensorHandle;
20
21namespace armcomputetensorutils
22{
23
telsoa01c577f2c2018-08-31 09:22:23 +010024/// Utility function to map an armnn::DataType to corresponding arm_compute::DataType.
telsoa014fcda012018-03-09 14:13:49 +000025arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType);
26
Matthew Benthamfd899962018-12-31 15:49:42 +000027/// Utility function used to set up an arm_compute::Coordinates from a vector of ArmNN Axes for reduction functions
28arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
29 unsigned int originalInputRank,
30 const std::vector<unsigned int>& armnnAxes);
31
telsoa01c577f2c2018-08-31 09:22:23 +010032/// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape.
telsoa014fcda012018-03-09 14:13:49 +000033arm_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
telsoa01c577f2c2018-08-31 09:22:23 +010036/// armnn::ITensorInfo.
telsoa014fcda012018-03-09 14:13:49 +000037arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo);
38
Francis Murtagh351d13d2018-09-24 15:01:18 +010039/// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
40/// armnn::ITensorInfo.
41/// armnn::DataLayout.
42arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
43 armnn::DataLayout dataLayout);
44
Matteo Martincigh747ef822018-12-18 09:26:39 +000045/// Utility function used to convert armnn::DataLayout to arm_compute::DataLayout
46/// armnn::DataLayout.
47arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout);
48
telsoa01c577f2c2018-08-31 09:22:23 +010049/// Utility function used to setup an arm_compute::PoolingLayerInfo object from an armnn::Pooling2dDescriptor.
telsoa014fcda012018-03-09 14:13:49 +000050arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor);
51
telsoa01c577f2c2018-08-31 09:22:23 +010052/// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor.
telsoa014fcda012018-03-09 14:13:49 +000053arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc);
54
telsoa01c577f2c2018-08-31 09:22:23 +010055/// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
telsoa014fcda012018-03-09 14:13:49 +000056arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& vector);
57
Sadik Armaganf4464322018-12-20 16:19:12 +000058/// Utility function used to setup an arm_compute::Size2D object from width and height values.
59arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
60
telsoa01c577f2c2018-08-31 09:22:23 +010061/// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor.
surmeh013537c2c2018-05-18 16:31:43 +010062template <typename Descriptor>
63arm_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
telsoa014fcda012018-03-09 14:13:49 +000074/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
75template <typename Tensor>
76void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
77{
78 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
79}
80
Francis Murtagh351d13d2018-09-24 15:01:18 +010081/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
82template <typename Tensor>
83void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
84{
85 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
86}
87
telsoa014fcda012018-03-09 14:13:49 +000088template <typename Tensor>
89void InitialiseArmComputeTensorEmpty(Tensor& tensor)
90{
91 tensor.allocator()->allocate();
92}
93
telsoa01c577f2c2018-08-31 09:22:23 +010094/// Utility function to free unused tensors after a workload is configured and prepared
95template <typename Tensor>
96void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
97{
98 if (tensor && !tensor->is_used())
99 {
100 tensor.reset(nullptr);
101 }
102}
103
telsoa014fcda012018-03-09 14:13:49 +0000104// Helper function to obtain byte offset into tensor data
105inline 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;
telsoa01c577f2c2018-08-31 09:22:23 +0100112 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));
telsoa014fcda012018-03-09 14:13:49 +0000116 return info.offset_element_in_bytes(coords);
117}
118
telsoa01c577f2c2018-08-31 09:22:23 +0100119// Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
telsoa014fcda012018-03-09 14:13:49 +0000120inline 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();
telsoa01c577f2c2018-08-31 09:22:23 +0100127 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]);
telsoa014fcda012018-03-09 14:13:49 +0000130 return ((batchIndex * numChannels + channelIndex) * height + y) * width + x;
131}
132
133template <typename T>
134void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
135{
telsoa01c577f2c2018-08-31 09:22:23 +0100136 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
telsoa014fcda012018-03-09 14:13:49 +0000137 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();
telsoa01c577f2c2018-08-31 09:22:23 +0100142 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]);
telsoa014fcda012018-03-09 14:13:49 +0000146
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 {
telsoa01c577f2c2018-08-31 09:22:23 +0100153 // 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.
telsoa014fcda012018-03-09 14:13:49 +0000155 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
164template <typename T>
165void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
166{
telsoa01c577f2c2018-08-31 09:22:23 +0100167 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
telsoa014fcda012018-03-09 14:13:49 +0000168 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();
telsoa01c577f2c2018-08-31 09:22:23 +0100173 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]);
telsoa014fcda012018-03-09 14:13:49 +0000177
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 {
telsoa01c577f2c2018-08-31 09:22:23 +0100184 // 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.
telsoa014fcda012018-03-09 14:13:49 +0000186 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
telsoa01c577f2c2018-08-31 09:22:23 +0100195/// 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.
201template<typename ArmComputeType, typename T>
202TensorShape 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
213inline TensorShape GetStrides(const arm_compute::Strides& strides)
214{
215 return GetTensorShape(strides, 0U);
216}
217
218/// Get the shape from an ACL shape object
219inline TensorShape GetShape(const arm_compute::TensorShape& shape)
220{
221 return GetTensorShape(shape, 1U);
222}
223
telsoa014fcda012018-03-09 14:13:49 +0000224} // namespace armcomputetensorutils
225} // namespace armnn