12#include <arm_compute/core/ITensor.h>
13#include <arm_compute/core/TensorInfo.h>
14#include <arm_compute/core/Types.h>
15#include <arm_compute/function_info/ScatterInfo.h>
23namespace armcomputetensorutils
27arm_compute::DataType GetArmComputeDataType(
armnn::DataType dataType,
bool multiScales);
33arm_compute::Coordinates BuildArmComputeReductionCoordinates(
size_t inputDimensions,
34 unsigned int originalInputRank,
35 const std::vector<unsigned int>& armnnAxes);
38arm_compute::TensorShape BuildArmComputeTensorShape(
const armnn::TensorShape& tensorShape);
42arm_compute::TensorShape BuildArmComputeTensorShape(
const armnn::TensorShape& tensorShape,
unsigned int dimensions);
46arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo);
51arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
unsigned int dimensions);
56arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
58 unsigned int dimensions);
63arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
69arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
79arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(
const Pooling2dDescriptor& descriptor,
80 bool fpMixedPrecision =
false);
85arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(
const Pooling3dDescriptor& descriptor,
86 bool fpMixedPrecision =
false);
89arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(
const NormalizationDescriptor& desc);
94arm_compute::PermutationVector BuildArmComputePermutationVector(
const armnn::PermutationVector& perm);
99arm_compute::PermutationVector BuildArmComputeTransposeVector(
const armnn::PermutationVector& perm);
102arm_compute::Size2D BuildArmComputeSize2D(
const unsigned int width,
const unsigned int height);
105arm_compute::PixelValue GetPixelValue(
const arm_compute::ITensorInfo* tensorInfo,
float value);
109 const arm_compute::TensorShape& weightsShape,
110 const arm_compute::TensorShape& inputShape);
113arm_compute::ScatterInfo BuildArmComputeScatterInfo(
const ScatterNdDescriptor& descriptor);
116template <
typename Descriptor>
117arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(
const Descriptor& descriptor)
119 return arm_compute::PadStrideInfo(descriptor.m_StrideX,
120 descriptor.m_StrideY,
121 descriptor.m_PadLeft,
122 descriptor.m_PadRight,
124 descriptor.m_PadBottom,
125 arm_compute::DimensionRoundingType::FLOOR);
129template <
typename Descriptor>
130arm_compute::Padding2D BuildArmComputePaddingInfo(
const Descriptor &descriptor)
132 return arm_compute::Padding2D(descriptor.m_PadLeft,
133 descriptor.m_PadRight,
135 descriptor.m_PadBottom);
139template <
typename Descriptor>
140arm_compute::CropInfo BuildArmComputeCropInfo(
const Descriptor& descriptor,
const unsigned int rank = 4)
144 return arm_compute::CropInfo(0, 0,
145 descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
149 return arm_compute::CropInfo(descriptor.m_Crops[1].first, descriptor.m_Crops[1].second,
150 descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
154 throw InvalidArgumentException(
"Tensor rank must be either 3 or 4",
CHECK_LOCATION());
159template <
typename Tensor>
160void BuildArmComputeTensor(Tensor& tensor,
const armnn::TensorInfo& tensorInfo)
162 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
166template <
typename Tensor>
167void BuildArmComputeTensor(Tensor& tensor,
const armnn::TensorInfo& tensorInfo,
DataLayout dataLayout)
169 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
172template <
typename Tensor>
173void InitialiseArmComputeTensorEmpty(Tensor& tensor)
175 tensor.allocator()->allocate();
179template <
typename Tensor>
180void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
182 if (tensor && !tensor->is_used())
184 tensor.reset(
nullptr);
189inline size_t GetTensorOffset(
const arm_compute::ITensorInfo&
info,
192 uint32_t channelIndex,
196 arm_compute::Coordinates coords;
197 coords.set(4,
static_cast<int>(depthIndex));
198 coords.set(3,
static_cast<int>(batchIndex));
199 coords.set(2,
static_cast<int>(channelIndex));
200 coords.set(1,
static_cast<int>(y));
201 coords.set(0,
static_cast<int>(x));
206inline size_t GetLinearBufferOffset(
const arm_compute::ITensorInfo&
info,
209 uint32_t channelIndex,
213 const arm_compute::TensorShape& shape =
info.tensor_shape();
214 uint32_t width =
static_cast<uint32_t
>(shape[0]);
215 uint32_t height =
static_cast<uint32_t
>(shape[1]);
216 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
217 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
218 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
222void CopyArmComputeITensorData(
const arm_compute::ITensor& srcTensor, T* dstData)
227 const arm_compute::ITensorInfo&
info = *srcTensor.info();
228 const arm_compute::TensorShape& shape =
info.tensor_shape();
229 const uint8_t*
const bufferPtr = srcTensor.buffer();
230 uint32_t width =
static_cast<uint32_t
>(shape[0]);
231 uint32_t height =
static_cast<uint32_t
>(shape[1]);
232 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
233 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
234 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
236 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
238 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
240 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
242 for (
unsigned int y = 0; y < height; ++y)
247 dstData + GetLinearBufferOffset(
info, depthIndex, batchIndex, channelIndex, y, 0),
248 bufferPtr + GetTensorOffset(
info, depthIndex, batchIndex, channelIndex, y, 0),
258void CopyArmComputeITensorData(
const T* srcData, arm_compute::ITensor& dstTensor)
263 const arm_compute::ITensorInfo&
info = *dstTensor.info();
264 const arm_compute::TensorShape& shape =
info.tensor_shape();
265 uint8_t*
const bufferPtr = dstTensor.buffer();
266 uint32_t width =
static_cast<uint32_t
>(shape[0]);
267 uint32_t height =
static_cast<uint32_t
>(shape[1]);
268 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
269 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
270 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
272 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
274 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
276 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
278 for (
unsigned int y = 0; y < height; ++y)
283 bufferPtr + GetTensorOffset(
info, depthIndex, batchIndex, channelIndex, y, 0),
284 srcData + GetLinearBufferOffset(
info, depthIndex, batchIndex, channelIndex, y, 0),
299template<
typename ArmComputeType,
typename T>
300TensorShape GetTensorShape(
const ArmComputeType& shapelike, T initial)
303 for (
unsigned int i=0; i < shapelike.num_dimensions(); ++i)
311inline TensorShape GetStrides(
const arm_compute::Strides& strides)
313 return GetTensorShape(strides, 0U);
317inline TensorShape GetShape(
const arm_compute::TensorShape& shape)
319 return GetTensorShape(shape, 1U);
Copyright (c) 2021 ARM Limited and Contributors.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
constexpr unsigned int MaxNumOfTensorDimensions