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::Padding2D BuildArmComputePaddingInfo(
const Descriptor &descriptor)
128 return arm_compute::Padding2D(descriptor.m_PadLeft,
129 descriptor.m_PadRight,
131 descriptor.m_PadBottom);
135 template <
typename Descriptor>
136 arm_compute::CropInfo BuildArmComputeCropInfo(
const Descriptor& descriptor,
const unsigned int rank = 4)
140 return arm_compute::CropInfo(0, 0,
141 descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
145 return arm_compute::CropInfo(descriptor.m_Crops[1].first, descriptor.m_Crops[1].second,
146 descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
150 throw InvalidArgumentException(
"Tensor rank must be either 3 or 4",
CHECK_LOCATION());
155 template <
typename Tensor>
158 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
162 template <
typename Tensor>
165 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
168 template <
typename Tensor>
169 void InitialiseArmComputeTensorEmpty(Tensor& tensor)
171 tensor.allocator()->allocate();
175 template <
typename Tensor>
176 void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
178 if (tensor && !tensor->is_used())
180 tensor.reset(
nullptr);
185 inline size_t GetTensorOffset(
const arm_compute::ITensorInfo& info,
188 uint32_t channelIndex,
193 coords.set(4,
static_cast<int>(depthIndex));
194 coords.set(3,
static_cast<int>(batchIndex));
195 coords.set(2,
static_cast<int>(channelIndex));
196 coords.set(1,
static_cast<int>(y));
197 coords.set(0,
static_cast<int>(x));
198 return armnn::numeric_cast<size_t>(
info.offset_element_in_bytes(coords));
202 inline size_t GetLinearBufferOffset(
const arm_compute::ITensorInfo& info,
205 uint32_t channelIndex,
209 const arm_compute::TensorShape& shape =
info.tensor_shape();
210 uint32_t width =
static_cast<uint32_t
>(shape[0]);
211 uint32_t height =
static_cast<uint32_t
>(shape[1]);
212 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
213 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
214 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
217 template <
typename T>
218 void CopyArmComputeITensorData(
const arm_compute::ITensor& srcTensor, T* dstData)
223 const arm_compute::ITensorInfo&
info = *srcTensor.info();
224 const arm_compute::TensorShape& shape =
info.tensor_shape();
225 const uint8_t*
const bufferPtr = srcTensor.buffer();
226 uint32_t width =
static_cast<uint32_t
>(shape[0]);
227 uint32_t height =
static_cast<uint32_t
>(shape[1]);
228 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
229 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
230 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
232 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
234 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
236 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
238 for (
unsigned int y = 0; y < height; ++y)
243 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
244 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
253 template <
typename T>
254 void CopyArmComputeITensorData(
const T* srcData, arm_compute::ITensor& dstTensor)
259 const arm_compute::ITensorInfo&
info = *dstTensor.info();
260 const arm_compute::TensorShape& shape =
info.tensor_shape();
261 uint8_t*
const bufferPtr = dstTensor.buffer();
262 uint32_t width =
static_cast<uint32_t
>(shape[0]);
263 uint32_t height =
static_cast<uint32_t
>(shape[1]);
264 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
265 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
266 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
268 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
270 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
272 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
274 for (
unsigned int y = 0; y < height; ++y)
279 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
280 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
295 template<
typename ArmComputeType,
typename T>
296 TensorShape
GetTensorShape(
const ArmComputeType& shapelike, T initial)
299 for (
unsigned int i=0; i < shapelike.num_dimensions(); ++i)
301 s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
303 return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
307 inline TensorShape GetStrides(
const arm_compute::Strides& strides)
313 inline TensorShape GetShape(
const arm_compute::TensorShape& shape)