12 #include <arm_compute/core/ITensor.h>
13 #include <arm_compute/core/TensorInfo.h>
14 #include <arm_compute/core/Types.h>
22 namespace armcomputetensorutils
33 unsigned int originalInputRank,
34 const std::vector<unsigned int>& armnnAxes);
37 arm_compute::TensorShape BuildArmComputeTensorShape(
const armnn::TensorShape& tensorShape);
41 arm_compute::TensorShape BuildArmComputeTensorShape(
const armnn::TensorShape& tensorShape,
unsigned int dimensions);
45 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo);
50 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
unsigned int dimensions);
55 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
57 unsigned int dimensions);
62 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
68 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
78 arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(
const Pooling2dDescriptor& descriptor,
79 bool fpMixedPrecision =
false);
84 arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(
const Pooling3dDescriptor& descriptor,
85 bool fpMixedPrecision =
false);
88 arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(
const NormalizationDescriptor& desc);
101 arm_compute::Size2D BuildArmComputeSize2D(
const unsigned int width,
const unsigned int height);
104 arm_compute::PixelValue GetPixelValue(
const arm_compute::ITensorInfo* tensorInfo,
float value);
108 const arm_compute::TensorShape& weightsShape,
109 const arm_compute::TensorShape& inputShape);
112 template <
typename Descriptor>
113 arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(
const Descriptor &descriptor)
115 return arm_compute::PadStrideInfo(descriptor.m_StrideX,
116 descriptor.m_StrideY,
117 descriptor.m_PadLeft,
118 descriptor.m_PadRight,
120 descriptor.m_PadBottom,
121 arm_compute::DimensionRoundingType::FLOOR);
125 template <
typename Descriptor>
126 arm_compute::CropInfo BuildArmComputeCropInfo(
const Descriptor& descriptor)
128 return arm_compute::CropInfo(descriptor.m_Crops[1].first, descriptor.m_Crops[1].second,
129 descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
133 template <
typename Tensor>
136 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
140 template <
typename Tensor>
143 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
146 template <
typename Tensor>
147 void InitialiseArmComputeTensorEmpty(Tensor& tensor)
149 tensor.allocator()->allocate();
153 template <
typename Tensor>
154 void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
156 if (tensor && !tensor->is_used())
158 tensor.reset(
nullptr);
163 inline size_t GetTensorOffset(
const arm_compute::ITensorInfo& info,
166 uint32_t channelIndex,
171 coords.set(4,
static_cast<int>(depthIndex));
172 coords.set(3,
static_cast<int>(batchIndex));
173 coords.set(2,
static_cast<int>(channelIndex));
174 coords.set(1,
static_cast<int>(y));
175 coords.set(0,
static_cast<int>(x));
176 return armnn::numeric_cast<size_t>(
info.offset_element_in_bytes(coords));
180 inline size_t GetLinearBufferOffset(
const arm_compute::ITensorInfo& info,
183 uint32_t channelIndex,
187 const arm_compute::TensorShape& shape =
info.tensor_shape();
188 uint32_t width =
static_cast<uint32_t
>(shape[0]);
189 uint32_t height =
static_cast<uint32_t
>(shape[1]);
190 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
191 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
192 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
195 template <
typename T>
196 void CopyArmComputeITensorData(
const arm_compute::ITensor& srcTensor, T* dstData)
201 const arm_compute::ITensorInfo&
info = *srcTensor.info();
202 const arm_compute::TensorShape& shape =
info.tensor_shape();
203 const uint8_t*
const bufferPtr = srcTensor.buffer();
204 uint32_t width =
static_cast<uint32_t
>(shape[0]);
205 uint32_t height =
static_cast<uint32_t
>(shape[1]);
206 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
207 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
208 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
210 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
212 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
214 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
216 for (
unsigned int y = 0; y < height; ++y)
221 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
222 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
231 template <
typename T>
232 void CopyArmComputeITensorData(
const T* srcData, arm_compute::ITensor& dstTensor)
237 const arm_compute::ITensorInfo&
info = *dstTensor.info();
238 const arm_compute::TensorShape& shape =
info.tensor_shape();
239 uint8_t*
const bufferPtr = dstTensor.buffer();
240 uint32_t width =
static_cast<uint32_t
>(shape[0]);
241 uint32_t height =
static_cast<uint32_t
>(shape[1]);
242 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
243 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
244 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
246 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
248 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
250 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
252 for (
unsigned int y = 0; y < height; ++y)
257 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
258 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
273 template<
typename ArmComputeType,
typename T>
274 TensorShape
GetTensorShape(
const ArmComputeType& shapelike, T initial)
277 for (
unsigned int i=0; i < shapelike.num_dimensions(); ++i)
279 s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
281 return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
285 inline TensorShape GetStrides(
const arm_compute::Strides& strides)
291 inline TensorShape GetShape(
const arm_compute::TensorShape& shape)