ArmNN
 22.05
ArmComputeTensorUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/Tensor.hpp>
9 
11 
12 #include <arm_compute/core/ITensor.h>
13 #include <arm_compute/core/TensorInfo.h>
14 #include <arm_compute/core/Types.h>
15 
16 #include <Half.hpp>
17 
18 namespace armnn
19 {
20 class ITensorHandle;
21 
22 namespace armcomputetensorutils
23 {
24 
25 /// Utility function to map an armnn::DataType to corresponding arm_compute::DataType.
26 arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType, bool multiScales);
27 
28 /// Utility function to map an arm_compute::DataType to corresponding armnn::DataType.
29 armnn::DataType GetArmNNDataType(arm_compute::DataType datatype);
30 
31 /// Utility function used to set up an arm_compute::Coordinates from a vector of ArmNN Axes for reduction functions
32 arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
33  unsigned int originalInputRank,
34  const std::vector<unsigned int>& armnnAxes);
35 
36 /// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape.
37 arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape);
38 
39 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
40 /// armnn::ITensorInfo.
41 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo);
42 
43 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
44 /// armnn::ITensorInfo.
45 /// armnn::DataLayout.
46 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
47  armnn::DataLayout dataLayout);
48 
49 /// Utility function used to convert armnn::DataLayout to arm_compute::DataLayout
50 /// armnn::DataLayout.
51 arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout);
52 
53 /// Utility function used to setup an arm_compute::PoolingLayerInfo object from given
54 /// armnn::Pooling2dDescriptor
55 /// bool fpMixedPrecision
56 arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
57  bool fpMixedPrecision = false);
58 
59 /// Utility function used to setup an arm_compute::Pooling3dLayerInfo object from given
60 /// armnn::Pooling3dDescriptor
61 /// bool fpMixedPrecision
62 arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(const Pooling3dDescriptor& descriptor,
63  bool fpMixedPrecision = false);
64 
65 /// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor.
66 arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc);
67 
68 /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
69 arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& vector);
70 
71 /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
72 arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& vector);
73 
74 /// Utility function used to setup an arm_compute::Size2D object from width and height values.
75 arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
76 
77 /// Gets the appropriate PixelValue for the TensorInfo DataType
78 arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float pixelValue);
79 
80 /// Computes the depth multiplier parameter for the Depthwise Conv2d ACL workload.
81 unsigned int ComputeDepthwiseConv2dDepthMultiplier(armnn::DataLayout layout,
82  const arm_compute::TensorShape& weightsShape,
83  const arm_compute::TensorShape& inputShape);
84 
85 /// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor.
86 template <typename Descriptor>
87 arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor &descriptor)
88 {
89  return arm_compute::PadStrideInfo(descriptor.m_StrideX,
90  descriptor.m_StrideY,
91  descriptor.m_PadLeft,
92  descriptor.m_PadRight,
93  descriptor.m_PadTop,
94  descriptor.m_PadBottom,
95  arm_compute::DimensionRoundingType::FLOOR);
96 }
97 
98 /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
99 template <typename Tensor>
100 void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
101 {
102  tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
103 }
104 
105 /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
106 template <typename Tensor>
107 void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
108 {
109  tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
110 }
111 
112 template <typename Tensor>
113 void InitialiseArmComputeTensorEmpty(Tensor& tensor)
114 {
115  tensor.allocator()->allocate();
116 }
117 
118 /// Utility function to free unused tensors after a workload is configured and prepared
119 template <typename Tensor>
120 void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
121 {
122  if (tensor && !tensor->is_used())
123  {
124  tensor.reset(nullptr);
125  }
126 }
127 
128 // Helper function to obtain byte offset into tensor data
129 inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info,
130  uint32_t depthIndex,
131  uint32_t batchIndex,
132  uint32_t channelIndex,
133  uint32_t y,
134  uint32_t x)
135 {
137  coords.set(4, static_cast<int>(depthIndex));
138  coords.set(3, static_cast<int>(batchIndex));
139  coords.set(2, static_cast<int>(channelIndex));
140  coords.set(1, static_cast<int>(y));
141  coords.set(0, static_cast<int>(x));
142  return armnn::numeric_cast<size_t>(info.offset_element_in_bytes(coords));
143 }
144 
145 // Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
146 inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info,
147  uint32_t depthIndex,
148  uint32_t batchIndex,
149  uint32_t channelIndex,
150  uint32_t y,
151  uint32_t x)
152 {
153  const arm_compute::TensorShape& shape = info.tensor_shape();
154  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]);
158  return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
159 }
160 
161 template <typename T>
162 void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
163 {
164  // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
165  static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
166  {
167  const arm_compute::ITensorInfo& info = *srcTensor.info();
168  const arm_compute::TensorShape& shape = info.tensor_shape();
169  const uint8_t* const bufferPtr = srcTensor.buffer();
170  uint32_t width = static_cast<uint32_t>(shape[0]);
171  uint32_t height = static_cast<uint32_t>(shape[1]);
172  uint32_t numChannels = static_cast<uint32_t>(shape[2]);
173  uint32_t numBatches = static_cast<uint32_t>(shape[3]);
174  uint32_t depth = static_cast<uint32_t>(shape[4]);
175 
176  for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
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  {
184  // Copies one row from arm_compute tensor buffer to linear memory buffer.
185  // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
186  memcpy(
187  dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
188  bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
189  width * sizeof(T));
190  }
191  }
192  }
193  }
194  }
195 }
196 
197 template <typename T>
198 void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
199 {
200  // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
201  static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
202  {
203  const arm_compute::ITensorInfo& info = *dstTensor.info();
204  const arm_compute::TensorShape& shape = info.tensor_shape();
205  uint8_t* const bufferPtr = dstTensor.buffer();
206  uint32_t width = static_cast<uint32_t>(shape[0]);
207  uint32_t height = static_cast<uint32_t>(shape[1]);
208  uint32_t numChannels = static_cast<uint32_t>(shape[2]);
209  uint32_t numBatches = static_cast<uint32_t>(shape[3]);
210  uint32_t depth = static_cast<uint32_t>(shape[4]);
211 
212  for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
213  {
214  for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
215  {
216  for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
217  {
218  for (unsigned int y = 0; y < height; ++y)
219  {
220  // Copies one row from linear memory buffer to arm_compute tensor buffer.
221  // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
222  memcpy(
223  bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
224  srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
225  width * sizeof(T));
226  }
227  }
228  }
229  }
230  }
231 }
232 
233 /// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions.
234 /// \tparam ArmComputeType Any type that implements the Dimensions interface
235 /// \tparam T Shape value type
236 /// \param shapelike An ArmCompute object that implements the Dimensions interface
237 /// \param initial A default value to initialise the shape with
238 /// \return A TensorShape object filled from the Acl shapelike object.
239 template<typename ArmComputeType, typename T>
240 TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial)
241 {
242  std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial);
243  for (unsigned int i=0; i < shapelike.num_dimensions(); ++i)
244  {
245  s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
246  }
247  return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
248 };
249 
250 /// Get the strides from an ACL strides object
251 inline TensorShape GetStrides(const arm_compute::Strides& strides)
252 {
253  return GetTensorShape(strides, 0U);
254 }
255 
256 /// Get the shape from an ACL shape object
257 inline TensorShape GetShape(const arm_compute::TensorShape& shape)
258 {
259  return GetTensorShape(shape, 1U);
260 }
261 
262 } // namespace armcomputetensorutils
263 } // namespace armnn
DataLayout
Definition: Types.hpp:62
std::array< unsigned int, MaxNumOfTensorDimensions > Coordinates
Copyright (c) 2021 ARM Limited and Contributors.
const armnnSerializer::Pooling2dDescriptor * Pooling2dDescriptor
DataType
Definition: Types.hpp:48
armnn::TensorShape GetTensorShape(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout)
Definition: TensorUtils.cpp:19
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
const armnnSerializer::Pooling3dDescriptor * Pooling3dDescriptor