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 Tensor>
128 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
132 template <
typename Tensor>
135 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
138 template <
typename Tensor>
139 void InitialiseArmComputeTensorEmpty(Tensor& tensor)
141 tensor.allocator()->allocate();
145 template <
typename Tensor>
146 void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
148 if (tensor && !tensor->is_used())
150 tensor.reset(
nullptr);
155 inline size_t GetTensorOffset(
const arm_compute::ITensorInfo& info,
158 uint32_t channelIndex,
163 coords.set(4,
static_cast<int>(depthIndex));
164 coords.set(3,
static_cast<int>(batchIndex));
165 coords.set(2,
static_cast<int>(channelIndex));
166 coords.set(1,
static_cast<int>(y));
167 coords.set(0,
static_cast<int>(x));
168 return armnn::numeric_cast<size_t>(
info.offset_element_in_bytes(coords));
172 inline size_t GetLinearBufferOffset(
const arm_compute::ITensorInfo& info,
175 uint32_t channelIndex,
179 const arm_compute::TensorShape& shape =
info.tensor_shape();
180 uint32_t width =
static_cast<uint32_t
>(shape[0]);
181 uint32_t height =
static_cast<uint32_t
>(shape[1]);
182 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
183 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
184 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
187 template <
typename T>
188 void CopyArmComputeITensorData(
const arm_compute::ITensor& srcTensor, T* dstData)
193 const arm_compute::ITensorInfo&
info = *srcTensor.info();
194 const arm_compute::TensorShape& shape =
info.tensor_shape();
195 const uint8_t*
const bufferPtr = srcTensor.buffer();
196 uint32_t width =
static_cast<uint32_t
>(shape[0]);
197 uint32_t height =
static_cast<uint32_t
>(shape[1]);
198 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
199 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
200 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
202 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
204 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
206 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
208 for (
unsigned int y = 0; y < height; ++y)
213 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
214 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
223 template <
typename T>
224 void CopyArmComputeITensorData(
const T* srcData, arm_compute::ITensor& dstTensor)
229 const arm_compute::ITensorInfo&
info = *dstTensor.info();
230 const arm_compute::TensorShape& shape =
info.tensor_shape();
231 uint8_t*
const bufferPtr = dstTensor.buffer();
232 uint32_t width =
static_cast<uint32_t
>(shape[0]);
233 uint32_t height =
static_cast<uint32_t
>(shape[1]);
234 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
235 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
236 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
238 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
240 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
242 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
244 for (
unsigned int y = 0; y < height; ++y)
249 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
250 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
265 template<
typename ArmComputeType,
typename T>
266 TensorShape
GetTensorShape(
const ArmComputeType& shapelike, T initial)
269 for (
unsigned int i=0; i < shapelike.num_dimensions(); ++i)
271 s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
273 return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
277 inline TensorShape GetStrides(
const arm_compute::Strides& strides)
283 inline TensorShape GetShape(
const arm_compute::TensorShape& shape)