blob: 3fc6818b0d4150600dea91b9babb2636d884f46d [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
Sadik Armagana3600ba2019-10-10 10:43:20 +010051/// Utility function used to setup an arm_compute::PoolingLayerInfo object from given
52/// armnn::Pooling2dDescriptor
53/// bool fpMixedPrecision
54arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
55 bool fpMixedPrecision = false);
telsoa014fcda012018-03-09 14:13:49 +000056
telsoa01c577f2c2018-08-31 09:22:23 +010057/// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor.
telsoa014fcda012018-03-09 14:13:49 +000058arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc);
59
telsoa01c577f2c2018-08-31 09:22:23 +010060/// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
telsoa014fcda012018-03-09 14:13:49 +000061arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& vector);
62
Sadik Armaganf4464322018-12-20 16:19:12 +000063/// Utility function used to setup an arm_compute::Size2D object from width and height values.
64arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
65
Mike Kelly0a08ec62019-07-25 08:39:31 +010066/// Gets the appropriate PixelValue for the input DataType
67arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input, float pixelValue);
68
telsoa01c577f2c2018-08-31 09:22:23 +010069/// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor.
surmeh013537c2c2018-05-18 16:31:43 +010070template <typename Descriptor>
71arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor &descriptor)
72{
73 return arm_compute::PadStrideInfo(descriptor.m_StrideX,
74 descriptor.m_StrideY,
75 descriptor.m_PadLeft,
76 descriptor.m_PadRight,
77 descriptor.m_PadTop,
78 descriptor.m_PadBottom,
79 arm_compute::DimensionRoundingType::FLOOR);
80}
81
telsoa014fcda012018-03-09 14:13:49 +000082/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
83template <typename Tensor>
84void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
85{
86 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
87}
88
Francis Murtagh351d13d2018-09-24 15:01:18 +010089/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
90template <typename Tensor>
91void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
92{
93 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
94}
95
telsoa014fcda012018-03-09 14:13:49 +000096template <typename Tensor>
97void InitialiseArmComputeTensorEmpty(Tensor& tensor)
98{
99 tensor.allocator()->allocate();
100}
101
telsoa01c577f2c2018-08-31 09:22:23 +0100102/// Utility function to free unused tensors after a workload is configured and prepared
103template <typename Tensor>
104void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
105{
106 if (tensor && !tensor->is_used())
107 {
108 tensor.reset(nullptr);
109 }
110}
111
telsoa014fcda012018-03-09 14:13:49 +0000112// Helper function to obtain byte offset into tensor data
113inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100114 uint32_t depthIndex,
telsoa014fcda012018-03-09 14:13:49 +0000115 uint32_t batchIndex,
116 uint32_t channelIndex,
117 uint32_t y,
118 uint32_t x)
119{
120 arm_compute::Coordinates coords;
Matthew Jacksondba634f2019-08-15 15:14:18 +0100121 coords.set(4, static_cast<int>(depthIndex));
telsoa01c577f2c2018-08-31 09:22:23 +0100122 coords.set(3, static_cast<int>(batchIndex));
123 coords.set(2, static_cast<int>(channelIndex));
124 coords.set(1, static_cast<int>(y));
125 coords.set(0, static_cast<int>(x));
Jammy Zhoud80cc0c2019-10-21 16:44:40 +0800126 return boost::numeric_cast<size_t>(info.offset_element_in_bytes(coords));
telsoa014fcda012018-03-09 14:13:49 +0000127}
128
telsoa01c577f2c2018-08-31 09:22:23 +0100129// Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
telsoa014fcda012018-03-09 14:13:49 +0000130inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100131 uint32_t depthIndex,
telsoa014fcda012018-03-09 14:13:49 +0000132 uint32_t batchIndex,
133 uint32_t channelIndex,
134 uint32_t y,
135 uint32_t x)
136{
137 const arm_compute::TensorShape& shape = info.tensor_shape();
telsoa01c577f2c2018-08-31 09:22:23 +0100138 uint32_t width = static_cast<uint32_t>(shape[0]);
139 uint32_t height = static_cast<uint32_t>(shape[1]);
140 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100141 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
142 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
telsoa014fcda012018-03-09 14:13:49 +0000143}
144
145template <typename T>
146void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
147{
telsoa01c577f2c2018-08-31 09:22:23 +0100148 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
Matthew Jacksondba634f2019-08-15 15:14:18 +0100149 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
telsoa014fcda012018-03-09 14:13:49 +0000150 {
151 const arm_compute::ITensorInfo& info = *srcTensor.info();
152 const arm_compute::TensorShape& shape = info.tensor_shape();
153 const uint8_t* const bufferPtr = srcTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100154 uint32_t width = static_cast<uint32_t>(shape[0]);
155 uint32_t height = static_cast<uint32_t>(shape[1]);
156 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
157 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100158 uint32_t depth = static_cast<uint32_t>(shape[4]);
telsoa014fcda012018-03-09 14:13:49 +0000159
Matthew Jacksondba634f2019-08-15 15:14:18 +0100160 for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
telsoa014fcda012018-03-09 14:13:49 +0000161 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100162 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
telsoa014fcda012018-03-09 14:13:49 +0000163 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100164 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
telsoa014fcda012018-03-09 14:13:49 +0000165 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100166 for (unsigned int y = 0; y < height; ++y)
167 {
168 // Copies one row from arm_compute tensor buffer to linear memory buffer.
169 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
170 memcpy(
171 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
172 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
173 width * sizeof(T));
174 }
telsoa014fcda012018-03-09 14:13:49 +0000175 }
176 }
177 }
178 }
179}
180
181template <typename T>
182void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
183{
telsoa01c577f2c2018-08-31 09:22:23 +0100184 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
Matthew Jacksondba634f2019-08-15 15:14:18 +0100185 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
telsoa014fcda012018-03-09 14:13:49 +0000186 {
187 const arm_compute::ITensorInfo& info = *dstTensor.info();
188 const arm_compute::TensorShape& shape = info.tensor_shape();
189 uint8_t* const bufferPtr = dstTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100190 uint32_t width = static_cast<uint32_t>(shape[0]);
191 uint32_t height = static_cast<uint32_t>(shape[1]);
192 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
193 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100194 uint32_t depth = static_cast<uint32_t>(shape[4]);
telsoa014fcda012018-03-09 14:13:49 +0000195
Matthew Jacksondba634f2019-08-15 15:14:18 +0100196 for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
telsoa014fcda012018-03-09 14:13:49 +0000197 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100198 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
telsoa014fcda012018-03-09 14:13:49 +0000199 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100200 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
telsoa014fcda012018-03-09 14:13:49 +0000201 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100202 for (unsigned int y = 0; y < height; ++y)
203 {
204 // Copies one row from linear memory buffer to arm_compute tensor buffer.
205 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
206 memcpy(
207 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
208 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
209 width * sizeof(T));
210 }
telsoa014fcda012018-03-09 14:13:49 +0000211 }
212 }
213 }
214 }
215}
216
telsoa01c577f2c2018-08-31 09:22:23 +0100217/// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions.
218/// \tparam ArmComputeType Any type that implements the Dimensions interface
219/// \tparam T Shape value type
220/// \param shapelike An ArmCompute object that implements the Dimensions interface
221/// \param initial A default value to initialise the shape with
222/// \return A TensorShape object filled from the Acl shapelike object.
223template<typename ArmComputeType, typename T>
224TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial)
225{
226 std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial);
227 for (unsigned int i=0; i < shapelike.num_dimensions(); ++i)
228 {
229 s[(shapelike.num_dimensions()-1)-i] = boost::numeric_cast<unsigned int>(shapelike[i]);
230 }
231 return TensorShape(boost::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
232};
233
234/// Get the strides from an ACL strides object
235inline TensorShape GetStrides(const arm_compute::Strides& strides)
236{
237 return GetTensorShape(strides, 0U);
238}
239
240/// Get the shape from an ACL shape object
241inline TensorShape GetShape(const arm_compute::TensorShape& shape)
242{
243 return GetTensorShape(shape, 1U);
244}
245
Aron Virginas-Tar710f6642019-11-27 14:48:32 +0000246bool IsQuantMultiplierSupported(const TensorInfo& input,
247 const TensorInfo& output,
248 const TensorInfo& weights);
249
telsoa014fcda012018-03-09 14:13:49 +0000250} // namespace armcomputetensorutils
251} // namespace armnn