blob: c9587a799070149c4f38ad67c0bd8d8c48272301 [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
Mike Kelly0a08ec62019-07-25 08:39:31 +010015#include <Half.hpp>
16
telsoa014fcda012018-03-09 14:13:49 +000017#include <boost/cast.hpp>
18
19namespace armnn
20{
21class ITensorHandle;
22
23namespace armcomputetensorutils
24{
25
telsoa01c577f2c2018-08-31 09:22:23 +010026/// Utility function to map an armnn::DataType to corresponding arm_compute::DataType.
telsoa014fcda012018-03-09 14:13:49 +000027arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType);
28
Matthew Benthamfd899962018-12-31 15:49:42 +000029/// Utility function used to set up an arm_compute::Coordinates from a vector of ArmNN Axes for reduction functions
30arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
31 unsigned int originalInputRank,
32 const std::vector<unsigned int>& armnnAxes);
33
telsoa01c577f2c2018-08-31 09:22:23 +010034/// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape.
telsoa014fcda012018-03-09 14:13:49 +000035arm_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
telsoa01c577f2c2018-08-31 09:22:23 +010038/// armnn::ITensorInfo.
telsoa014fcda012018-03-09 14:13:49 +000039arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo);
40
Francis Murtagh351d13d2018-09-24 15:01:18 +010041/// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
42/// armnn::ITensorInfo.
43/// armnn::DataLayout.
44arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
45 armnn::DataLayout dataLayout);
46
Matteo Martincigh747ef822018-12-18 09:26:39 +000047/// Utility function used to convert armnn::DataLayout to arm_compute::DataLayout
48/// armnn::DataLayout.
49arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout);
50
telsoa01c577f2c2018-08-31 09:22:23 +010051/// Utility function used to setup an arm_compute::PoolingLayerInfo object from an armnn::Pooling2dDescriptor.
telsoa014fcda012018-03-09 14:13:49 +000052arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor);
53
telsoa01c577f2c2018-08-31 09:22:23 +010054/// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor.
telsoa014fcda012018-03-09 14:13:49 +000055arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc);
56
telsoa01c577f2c2018-08-31 09:22:23 +010057/// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
telsoa014fcda012018-03-09 14:13:49 +000058arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& vector);
59
Sadik Armaganf4464322018-12-20 16:19:12 +000060/// Utility function used to setup an arm_compute::Size2D object from width and height values.
61arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
62
Mike Kelly0a08ec62019-07-25 08:39:31 +010063/// Gets the appropriate PixelValue for the input DataType
64arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input, float pixelValue);
65
telsoa01c577f2c2018-08-31 09:22:23 +010066/// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor.
surmeh013537c2c2018-05-18 16:31:43 +010067template <typename Descriptor>
68arm_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
telsoa014fcda012018-03-09 14:13:49 +000079/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
80template <typename Tensor>
81void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
82{
83 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
84}
85
Francis Murtagh351d13d2018-09-24 15:01:18 +010086/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
87template <typename Tensor>
88void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
89{
90 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
91}
92
telsoa014fcda012018-03-09 14:13:49 +000093template <typename Tensor>
94void InitialiseArmComputeTensorEmpty(Tensor& tensor)
95{
96 tensor.allocator()->allocate();
97}
98
telsoa01c577f2c2018-08-31 09:22:23 +010099/// Utility function to free unused tensors after a workload is configured and prepared
100template <typename Tensor>
101void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
102{
103 if (tensor && !tensor->is_used())
104 {
105 tensor.reset(nullptr);
106 }
107}
108
telsoa014fcda012018-03-09 14:13:49 +0000109// Helper function to obtain byte offset into tensor data
110inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info,
111 uint32_t batchIndex,
112 uint32_t channelIndex,
113 uint32_t y,
114 uint32_t x)
115{
116 arm_compute::Coordinates coords;
telsoa01c577f2c2018-08-31 09:22:23 +0100117 coords.set(3, static_cast<int>(batchIndex));
118 coords.set(2, static_cast<int>(channelIndex));
119 coords.set(1, static_cast<int>(y));
120 coords.set(0, static_cast<int>(x));
telsoa014fcda012018-03-09 14:13:49 +0000121 return info.offset_element_in_bytes(coords);
122}
123
telsoa01c577f2c2018-08-31 09:22:23 +0100124// Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
telsoa014fcda012018-03-09 14:13:49 +0000125inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info,
126 uint32_t batchIndex,
127 uint32_t channelIndex,
128 uint32_t y,
129 uint32_t x)
130{
131 const arm_compute::TensorShape& shape = info.tensor_shape();
telsoa01c577f2c2018-08-31 09:22:23 +0100132 uint32_t width = static_cast<uint32_t>(shape[0]);
133 uint32_t height = static_cast<uint32_t>(shape[1]);
134 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
telsoa014fcda012018-03-09 14:13:49 +0000135 return ((batchIndex * numChannels + channelIndex) * height + y) * width + x;
136}
137
138template <typename T>
139void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
140{
telsoa01c577f2c2018-08-31 09:22:23 +0100141 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
telsoa014fcda012018-03-09 14:13:49 +0000142 static_assert(MaxNumOfTensorDimensions == 4, "Please update CopyArmComputeITensorData");
143 {
144 const arm_compute::ITensorInfo& info = *srcTensor.info();
145 const arm_compute::TensorShape& shape = info.tensor_shape();
146 const uint8_t* const bufferPtr = srcTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100147 uint32_t width = static_cast<uint32_t>(shape[0]);
148 uint32_t height = static_cast<uint32_t>(shape[1]);
149 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
150 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
telsoa014fcda012018-03-09 14:13:49 +0000151
152 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
153 {
154 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
155 {
156 for (unsigned int y = 0; y < height; ++y)
157 {
telsoa01c577f2c2018-08-31 09:22:23 +0100158 // Copies one row from arm_compute tensor buffer to linear memory buffer.
159 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
telsoa014fcda012018-03-09 14:13:49 +0000160 memcpy(dstData + GetLinearBufferOffset(info, batchIndex, channelIndex, y, 0),
161 bufferPtr + GetTensorOffset(info, batchIndex, channelIndex, y, 0),
162 width * sizeof(T));
163 }
164 }
165 }
166 }
167}
168
169template <typename T>
170void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
171{
telsoa01c577f2c2018-08-31 09:22:23 +0100172 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
telsoa014fcda012018-03-09 14:13:49 +0000173 static_assert(MaxNumOfTensorDimensions == 4, "Please update CopyArmComputeITensorData");
174 {
175 const arm_compute::ITensorInfo& info = *dstTensor.info();
176 const arm_compute::TensorShape& shape = info.tensor_shape();
177 uint8_t* const bufferPtr = dstTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100178 uint32_t width = static_cast<uint32_t>(shape[0]);
179 uint32_t height = static_cast<uint32_t>(shape[1]);
180 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
181 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
telsoa014fcda012018-03-09 14:13:49 +0000182
183 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
184 {
185 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
186 {
187 for (unsigned int y = 0; y < height; ++y)
188 {
telsoa01c577f2c2018-08-31 09:22:23 +0100189 // Copies one row from linear memory buffer to arm_compute tensor buffer.
190 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
telsoa014fcda012018-03-09 14:13:49 +0000191 memcpy(bufferPtr + GetTensorOffset(info, batchIndex, channelIndex, y, 0),
192 srcData + GetLinearBufferOffset(info, batchIndex, channelIndex, y, 0),
193 width * sizeof(T));
194 }
195 }
196 }
197 }
198}
199
telsoa01c577f2c2018-08-31 09:22:23 +0100200/// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions.
201/// \tparam ArmComputeType Any type that implements the Dimensions interface
202/// \tparam T Shape value type
203/// \param shapelike An ArmCompute object that implements the Dimensions interface
204/// \param initial A default value to initialise the shape with
205/// \return A TensorShape object filled from the Acl shapelike object.
206template<typename ArmComputeType, typename T>
207TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial)
208{
209 std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial);
210 for (unsigned int i=0; i < shapelike.num_dimensions(); ++i)
211 {
212 s[(shapelike.num_dimensions()-1)-i] = boost::numeric_cast<unsigned int>(shapelike[i]);
213 }
214 return TensorShape(boost::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
215};
216
217/// Get the strides from an ACL strides object
218inline TensorShape GetStrides(const arm_compute::Strides& strides)
219{
220 return GetTensorShape(strides, 0U);
221}
222
223/// Get the shape from an ACL shape object
224inline TensorShape GetShape(const arm_compute::TensorShape& shape)
225{
226 return GetTensorShape(shape, 1U);
227}
228
telsoa014fcda012018-03-09 14:13:49 +0000229} // namespace armcomputetensorutils
230} // namespace armnn