blob: 0e5487336f77144a2e237e7d5de152bc7f11c8ee [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
6#pragma once
7
Matteo Martincighe5b8eb92019-11-28 15:45:42 +00008#include <armnn/backends/ITensorHandle.hpp>
Colm Donelan0c479742021-12-10 12:43:54 +00009#include <armnn/backends/TensorHandle.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000010#include <armnn/Tensor.hpp>
Jan Eilersbb446e52020-04-02 13:56:54 +010011#include <armnn/utility/PolymorphicDowncast.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000012#include <armnnUtils/Permute.hpp>
13
Kevin May665a964a2019-08-21 16:53:50 +010014#include <Half.hpp>
Matteo Martincigh747ef822018-12-18 09:26:39 +000015#include <Profiling.hpp>
Matteo Martincigh747ef822018-12-18 09:26:39 +000016
telsoa01c577f2c2018-08-31 09:22:23 +010017
18namespace armnn
19{
20namespace
21{
Matteo Martincigh747ef822018-12-18 09:26:39 +000022
Kevin May665a964a2019-08-21 16:53:50 +010023template <typename ArrayType, typename Arg>
telsoa01c577f2c2018-08-31 09:22:23 +010024void AssignValues(unsigned int num, unsigned int& idx, const ArrayType& array, Arg& arg)
25{
Matteo Martincigh747ef822018-12-18 09:26:39 +000026 if (idx >= num)
27 {
28 return;
29 }
telsoa01c577f2c2018-08-31 09:22:23 +010030
Matteo Martincigh747ef822018-12-18 09:26:39 +000031 arg = array[(num - 1) - idx];
32 idx++;
33}
telsoa01c577f2c2018-08-31 09:22:23 +010034
Kevin May665a964a2019-08-21 16:53:50 +010035template <typename T, typename ArrayType, typename... Args>
36void AssignValues(unsigned int num, unsigned int idx, const ArrayType& array, T& assignee, Args&... args)
telsoa01c577f2c2018-08-31 09:22:23 +010037{
Matteo Martincigh747ef822018-12-18 09:26:39 +000038 AssignValues(num, idx, array, assignee);
telsoa01c577f2c2018-08-31 09:22:23 +010039
Matteo Martincigh747ef822018-12-18 09:26:39 +000040 AssignValues(num, idx, array, args...);
telsoa01c577f2c2018-08-31 09:22:23 +010041}
Matteo Martincigh747ef822018-12-18 09:26:39 +000042
Kevin May665a964a2019-08-21 16:53:50 +010043} // anonymous namespace
telsoa01c577f2c2018-08-31 09:22:23 +010044
Kevin May665a964a2019-08-21 16:53:50 +010045template <typename CopyFunc>
telsoa01c577f2c2018-08-31 09:22:23 +010046void CopyTensorContentsGeneric(const ITensorHandle* srcTensor, ITensorHandle* dstTensor, CopyFunc copy)
47{
Matthew Benthamefdbca62019-09-14 23:35:28 +010048 // For ease of understanding, names are assigned to the dimensions
49 // of the tensor as if NHWC, however this routine works with any 5D tensor
Matthew Jacksondba634f2019-08-15 15:14:18 +010050 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyTensorContents");
telsoa01c577f2c2018-08-31 09:22:23 +010051
Kevin May665a964a2019-08-21 16:53:50 +010052 TensorShape srcStrides = srcTensor->GetStrides();
telsoa01c577f2c2018-08-31 09:22:23 +010053 const TensorShape& srcShape = srcTensor->GetShape();
Rob Hughes9934e4e2019-11-27 14:27:11 +000054 const auto srcSize = srcTensor->GetStrides()[0] * srcShape[0];
Jan Eilers8eb25602020-03-09 12:13:48 +000055 IgnoreUnused(srcSize); // Only used for asserts
Kevin May665a964a2019-08-21 16:53:50 +010056 TensorShape dstStrides = dstTensor->GetStrides();
telsoa01c577f2c2018-08-31 09:22:23 +010057 const TensorShape& dstShape = dstTensor->GetShape();
Rob Hughes9934e4e2019-11-27 14:27:11 +000058 const auto dstSize = dstTensor->GetStrides()[0] * dstShape[0];
Jan Eilers8eb25602020-03-09 12:13:48 +000059 IgnoreUnused(dstSize); // Only used for asserts
telsoa01c577f2c2018-08-31 09:22:23 +010060
Kevin May665a964a2019-08-21 16:53:50 +010061 size_t srcDepth = 1;
62 size_t srcBatches = 1;
Kevin May665a964a2019-08-21 16:53:50 +010063 size_t srcHeight = 1;
64 size_t srcWidth = 1;
Matthew Benthamefdbca62019-09-14 23:35:28 +010065 size_t srcChannels = 1;
Kevin May665a964a2019-08-21 16:53:50 +010066 AssignValues(srcShape.GetNumDimensions(),
67 0,
68 srcShape,
Matthew Benthamefdbca62019-09-14 23:35:28 +010069 srcChannels,
telsoa01c577f2c2018-08-31 09:22:23 +010070 srcWidth,
71 srcHeight,
Matthew Jacksondba634f2019-08-15 15:14:18 +010072 srcBatches,
73 srcDepth);
telsoa01c577f2c2018-08-31 09:22:23 +010074
Kevin May665a964a2019-08-21 16:53:50 +010075 size_t srcDepthStride = 0;
76 size_t srcBatchStride = 0;
Kevin May665a964a2019-08-21 16:53:50 +010077 size_t srcHeightStride = 0;
78 size_t srcWidthStride = 0;
Matthew Benthamefdbca62019-09-14 23:35:28 +010079 size_t srcChannelStride = 0;
Kevin May665a964a2019-08-21 16:53:50 +010080 AssignValues(srcStrides.GetNumDimensions(),
81 0,
82 srcStrides,
Matthew Benthamefdbca62019-09-14 23:35:28 +010083 srcChannelStride,
telsoa01c577f2c2018-08-31 09:22:23 +010084 srcWidthStride,
85 srcHeightStride,
Matthew Jacksondba634f2019-08-15 15:14:18 +010086 srcBatchStride,
87 srcDepthStride);
telsoa01c577f2c2018-08-31 09:22:23 +010088
Kevin May665a964a2019-08-21 16:53:50 +010089 size_t dstDepth = 1;
90 size_t dstBatches = 1;
Kevin May665a964a2019-08-21 16:53:50 +010091 size_t dstHeight = 1;
92 size_t dstWidth = 1;
Matthew Benthamefdbca62019-09-14 23:35:28 +010093 size_t dstChannels = 1;
Kevin May665a964a2019-08-21 16:53:50 +010094 AssignValues(dstShape.GetNumDimensions(),
95 0,
96 dstShape,
Matthew Benthamefdbca62019-09-14 23:35:28 +010097 dstChannels,
telsoa01c577f2c2018-08-31 09:22:23 +010098 dstWidth,
99 dstHeight,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100100 dstBatches,
101 dstDepth);
telsoa01c577f2c2018-08-31 09:22:23 +0100102
Kevin May665a964a2019-08-21 16:53:50 +0100103 size_t dstDepthStride = 0;
104 size_t dstBatchStride = 0;
Kevin May665a964a2019-08-21 16:53:50 +0100105 size_t dstHeightStride = 0;
106 size_t dstWidthStride = 0;
Matthew Benthamefdbca62019-09-14 23:35:28 +0100107 size_t dstChannelStride = 0;
Kevin May665a964a2019-08-21 16:53:50 +0100108 AssignValues(dstStrides.GetNumDimensions(),
109 0,
110 dstStrides,
Matthew Benthamefdbca62019-09-14 23:35:28 +0100111 dstChannelStride,
telsoa01c577f2c2018-08-31 09:22:23 +0100112 dstWidthStride,
113 dstHeightStride,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100114 dstBatchStride,
115 dstDepthStride);
telsoa01c577f2c2018-08-31 09:22:23 +0100116
Rob Hughes9934e4e2019-11-27 14:27:11 +0000117 const unsigned char* srcDataStart;
118 unsigned char* dstDataStart;
Sadik Armaganbf86d512018-12-24 09:01:31 +0000119 {
120 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Synchronize buffers");
Rob Hughes9934e4e2019-11-27 14:27:11 +0000121 srcDataStart = static_cast<const uint8_t*>(srcTensor->Map());
122 dstDataStart = static_cast<uint8_t*>(dstTensor->Map());
Sadik Armaganbf86d512018-12-24 09:01:31 +0000123 }
telsoa01c577f2c2018-08-31 09:22:23 +0100124
Rob Hughes9934e4e2019-11-27 14:27:11 +0000125 size_t copyLength = std::min(srcChannels * srcChannelStride, dstChannels * dstChannelStride);
Matthew Benthamefdbca62019-09-14 23:35:28 +0100126 size_t copyWidth = std::min(srcWidth, dstWidth);
127 size_t copyHeight = std::min(srcHeight, dstHeight);
128 size_t copyBatches = std::min(srcBatches, dstBatches);
129 size_t copyDepth = std::min(srcDepth, dstDepth);
telsoa01c577f2c2018-08-31 09:22:23 +0100130
Matthew Bentham019c4b12019-09-15 00:06:05 +0100131 // Coalesce inner dimensions where possible
132 // to reduce overheard calling copy() and to
133 // allow for memory bandwidth optimisations
134 if (copyLength == srcWidthStride &&
135 copyLength == dstWidthStride)
136 {
137 // There is no special padding between rows,
138 // and sizes are compatible, so copy whole rows
139 copyLength *= copyWidth;
140 copyWidth = 1;
141
142 if (copyLength == srcHeightStride &&
143 copyLength == dstHeightStride)
144 {
145 // There is no special padding between batches
146 // and sizes are compatible so copy whole batches
147 copyLength *= copyHeight;
148 copyHeight = 1;
149 }
150 }
151
Rob Hughes9934e4e2019-11-27 14:27:11 +0000152 const unsigned char* srcData = srcDataStart;
153 unsigned char* dstData = dstDataStart;
Kevin May665a964a2019-08-21 16:53:50 +0100154 for (unsigned int d = 0; d < copyDepth; ++d)
telsoa01c577f2c2018-08-31 09:22:23 +0100155 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100156 auto srcPtrDepth = srcData;
157 auto dstPtrDepth = dstData;
Kevin May665a964a2019-08-21 16:53:50 +0100158 for (unsigned int b = 0; b < copyBatches; ++b)
telsoa01c577f2c2018-08-31 09:22:23 +0100159 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100160 auto srcPtrBatch = srcData;
161 auto dstPtrBatch = dstData;
Matthew Benthamefdbca62019-09-14 23:35:28 +0100162 for (unsigned int h = 0; h < copyHeight; ++h)
telsoa01c577f2c2018-08-31 09:22:23 +0100163 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100164 auto srcPtrChannel = srcData;
165 auto dstPtrChannel = dstData;
Matthew Benthamefdbca62019-09-14 23:35:28 +0100166 for (unsigned int w = 0; w < copyWidth; ++w)
Matthew Jacksondba634f2019-08-15 15:14:18 +0100167 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100168 ARMNN_ASSERT(srcData >= srcDataStart && srcData + copyLength <= srcDataStart + srcSize);
169 ARMNN_ASSERT(dstData >= dstDataStart && dstData + copyLength <= dstDataStart + dstSize);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100170 copy(dstData, srcData, copyLength);
Matthew Benthamefdbca62019-09-14 23:35:28 +0100171 dstData += dstWidthStride;
172 srcData += srcWidthStride;
Matthew Jacksondba634f2019-08-15 15:14:18 +0100173 }
Matthew Benthamefdbca62019-09-14 23:35:28 +0100174 dstData += (static_cast<long>(dstHeightStride) - (dstData - dstPtrChannel));
175 srcData += (static_cast<long>(srcHeightStride) - (srcData - srcPtrChannel));
telsoa01c577f2c2018-08-31 09:22:23 +0100176 }
Kevin May665a964a2019-08-21 16:53:50 +0100177 dstData += (static_cast<long>(dstBatchStride) - (dstData - dstPtrBatch));
178 srcData += (static_cast<long>(srcBatchStride) - (srcData - srcPtrBatch));
telsoa01c577f2c2018-08-31 09:22:23 +0100179 }
Kevin May665a964a2019-08-21 16:53:50 +0100180 dstData += (static_cast<long>(dstDepthStride) - (dstData - dstPtrDepth));
181 srcData += (static_cast<long>(srcDepthStride) - (srcData - srcPtrDepth));
telsoa01c577f2c2018-08-31 09:22:23 +0100182 }
183
184 srcTensor->Unmap();
185 dstTensor->Unmap();
186}
187
188template <typename SrcTensorHandleType, typename DstTensorHandleType, typename DescriptorType>
189void GatherTensorHandlePairs(const DescriptorType& descriptor,
190 std::vector<std::pair<SrcTensorHandleType*, DstTensorHandleType*>>& tensorHandlePairs)
191{
192 const unsigned int numInputs = static_cast<unsigned int>(descriptor.m_Inputs.size());
193 tensorHandlePairs.reserve(numInputs);
194
195 for (unsigned int i = 0; i < numInputs; ++i)
196 {
Kevin May665a964a2019-08-21 16:53:50 +0100197 SrcTensorHandleType* const srcTensorHandle =
Jan Eilersbb446e52020-04-02 13:56:54 +0100198 PolymorphicDowncast<SrcTensorHandleType*>(descriptor.m_Inputs[i]);
Kevin May665a964a2019-08-21 16:53:50 +0100199 DstTensorHandleType* const dstTensorHandle =
Jan Eilersbb446e52020-04-02 13:56:54 +0100200 PolymorphicDowncast<DstTensorHandleType*>(descriptor.m_Outputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +0100201
202 tensorHandlePairs.emplace_back(srcTensorHandle, dstTensorHandle);
203 }
204}
205
Francis Murtaghec33a912019-11-05 14:26:23 +0000206int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim);
207
James Conroy1f58f032021-04-27 17:13:27 +0100208armnn::ConstTensor PermuteTensor(const ConstTensorHandle* tensor,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000209 const PermutationVector& permutationVector,
210 void* permuteBuffer);
211
212void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout);
213
214TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout);
215
Jan Eilers53ef7952021-06-02 12:01:25 +0100216/// Weights for depthwise have a datalayout of [1,H,W,O] = [1,H,W,I*M]
217/// This function coverts a TensorInfo from [1,H,W,I*M] to [1,I*M,H,W] (if NCHW) or keeps it at [1,H,W,I*M] (if NHWC)
218/// as required by the compute library
219/// Returns a tuple of converted weights tensor info and depth multiplier
220std::tuple<TensorInfo, unsigned int> Convert1HWOTensorInfoToAcl(const TensorInfo& weightInfo,
221 const TensorInfo& inputInfo,
222 const DataLayout dataLayout);
223
James Conroy1f58f032021-04-27 17:13:27 +0100224armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstTensorHandle* weightTensor,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000225 DataLayout dataLayout,
226 void* permuteBuffer);
227
Jan Eilers53ef7952021-06-02 12:01:25 +0100228/// Weights for depthwise have a datalayout of [1,H,W,O] = [1,H,W,I*M]
229/// This function coverts a ConstCpuTensorHandle from [1,H,W,I*M] to [1,I*M,H,W] (if NCHW) or
230/// keeps it at [1,H,W,I*M] (if NHWC) as required by the compute library
231///
232/// \param weightTensor - ConstTensorHandle of weights tensor
233/// \param inputInfo - TensorInfo of input tensor
234/// \param dataLayout - DataLayout of the input tensor
235/// \param permuteBuffer - Pointer to memory with the size of tensor. Used for the permutation
236/// \return tuple of transformed weights-ConstTensor and depthwise multiplier
237std::tuple<ConstTensor, unsigned int> Convert1HWOTensorToAcl(const ConstTensorHandle* weightTensor,
238 const TensorInfo& inputInfo,
239 const DataLayout dataLayout,
240 void* permuteBuffer);
241
242/// Converts a (weights) tensor from [1, H, W, I*M] = [1, H, W, O] to [M, I, H, W]
243///
244/// \param weightTensor - ConstTensorHandle of the weight tensor that should be converted
245/// \param inputInfo - TensorInfo of the corresponding input tensor
246/// \param dataLayout - DataLayout of the input tensor e.g. NHWC or NCHW
247/// \param permuteBuffer - Memory location with the same size as the weight tensor to write converted data to
248/// \return - A tuple of ConstTensor and unsigned int which is the converted weightTensor and the depthMultiplier
249std::tuple<ConstTensor, unsigned int> Convert1HWOtoMIHW(const ConstTensorHandle* weightTensor,
250 const TensorInfo& inputInfo,
251 const DataLayout& dataLayout,
252 void* permuteBuffer);
253
Teresa Charlinb2d3ec52022-04-12 22:07:09 +0100254/// Calculates the key index values needed for GatherNd: N, ND, K, W, C (N is always 1)
255///
256/// \param inputInfo0 - TensorInfo of the corresponding input tensor: params
257/// \param inputInfo1 - TensorInfo of the corresponding input tensor: indices
258/// \return - A map with names and values for N, ND, K, W, C
259std::map<std::string, unsigned int> CalculateGatherNdKeyIndices(TensorInfo inputInfo0, TensorInfo inputInfo1);
260
Kevin May665a964a2019-08-21 16:53:50 +0100261} //namespace armnn