10 #include <arm_compute/core/ITensor.h> 11 #include <arm_compute/core/TensorInfo.h> 12 #include <arm_compute/core/Types.h> 13 #include <arm_compute/core/Size2D.h> 17 #include <boost/cast.hpp> 23 namespace armcomputetensorutils
31 unsigned int originalInputRank,
32 const std::vector<unsigned int>& armnnAxes);
35 arm_compute::TensorShape BuildArmComputeTensorShape(
const armnn::TensorShape& tensorShape);
39 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo);
44 arm_compute::TensorInfo BuildArmComputeTensorInfo(
const armnn::TensorInfo& tensorInfo,
54 arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(
const Pooling2dDescriptor& descriptor,
55 bool fpMixedPrecision =
false);
58 arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(
const NormalizationDescriptor& desc);
64 arm_compute::Size2D BuildArmComputeSize2D(
const unsigned int width,
const unsigned int height);
67 arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input,
float pixelValue);
70 template <
typename Descriptor>
71 arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(
const Descriptor &descriptor)
73 return arm_compute::PadStrideInfo(descriptor.m_StrideX,
76 descriptor.m_PadRight,
78 descriptor.m_PadBottom,
79 arm_compute::DimensionRoundingType::FLOOR);
83 template <
typename Tensor>
86 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
90 template <
typename Tensor>
93 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
96 template <
typename Tensor>
97 void InitialiseArmComputeTensorEmpty(Tensor& tensor)
99 tensor.allocator()->allocate();
103 template <
typename Tensor>
104 void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
106 if (tensor && !tensor->is_used())
108 tensor.reset(
nullptr);
113 inline size_t GetTensorOffset(
const arm_compute::ITensorInfo&
info,
116 uint32_t channelIndex,
121 coords.set(4, static_cast<int>(depthIndex));
122 coords.set(3, static_cast<int>(batchIndex));
123 coords.set(2, static_cast<int>(channelIndex));
124 coords.set(1, static_cast<int>(y));
125 coords.set(0, static_cast<int>(x));
126 return boost::numeric_cast<
size_t>(info.offset_element_in_bytes(coords));
130 inline size_t GetLinearBufferOffset(
const arm_compute::ITensorInfo& info,
133 uint32_t channelIndex,
137 const arm_compute::TensorShape& shape = info.tensor_shape();
138 uint32_t width =
static_cast<uint32_t
>(shape[0]);
139 uint32_t height =
static_cast<uint32_t
>(shape[1]);
140 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
141 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
142 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
145 template <
typename T>
146 void CopyArmComputeITensorData(
const arm_compute::ITensor& srcTensor, T* dstData)
151 const arm_compute::ITensorInfo& info = *srcTensor.info();
152 const arm_compute::TensorShape& shape = info.tensor_shape();
153 const uint8_t*
const bufferPtr = srcTensor.buffer();
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 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
160 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
162 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
164 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
166 for (
unsigned int y = 0; y < height; ++y)
171 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
172 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
181 template <
typename T>
182 void CopyArmComputeITensorData(
const T* srcData, arm_compute::ITensor& dstTensor)
187 const arm_compute::ITensorInfo& info = *dstTensor.info();
188 const arm_compute::TensorShape& shape = info.tensor_shape();
189 uint8_t*
const bufferPtr = dstTensor.buffer();
190 uint32_t width =
static_cast<uint32_t
>(shape[0]);
191 uint32_t height =
static_cast<uint32_t
>(shape[1]);
192 uint32_t numChannels =
static_cast<uint32_t
>(shape[2]);
193 uint32_t numBatches =
static_cast<uint32_t
>(shape[3]);
194 uint32_t depth =
static_cast<uint32_t
>(shape[4]);
196 for (
unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
198 for (
unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
200 for (
unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
202 for (
unsigned int y = 0; y < height; ++y)
207 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
208 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
223 template<
typename ArmComputeType,
typename T>
224 TensorShape
GetTensorShape(
const ArmComputeType& shapelike, T initial)
227 for (
unsigned int i=0; i < shapelike.num_dimensions(); ++i)
229 s[(shapelike.num_dimensions()-1)-i] = boost::numeric_cast<unsigned int>(shapelike[i]);
231 return TensorShape(boost::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
235 inline TensorShape GetStrides(
const arm_compute::Strides& strides)
241 inline TensorShape GetShape(
const arm_compute::TensorShape& shape)
armnn::TensorShape GetTensorShape(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout)
std::array< unsigned int, MaxNumOfTensorDimensions > Coordinates
constexpr unsigned int MaxNumOfTensorDimensions