telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 8 | #include "CpuTensorHandle.hpp" |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 9 | |
| 10 | #include <armnn/backends/ITensorHandle.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <armnn/Tensor.hpp> |
| 13 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 14 | #include <armnnUtils/Permute.hpp> |
| 15 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 16 | #include <Half.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 17 | #include <Profiling.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 18 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | #include <boost/cast.hpp> |
| 20 | |
| 21 | namespace armnn |
| 22 | { |
| 23 | namespace |
| 24 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 25 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 26 | template <typename ArrayType, typename Arg> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 27 | void AssignValues(unsigned int num, unsigned int& idx, const ArrayType& array, Arg& arg) |
| 28 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 29 | if (idx >= num) |
| 30 | { |
| 31 | return; |
| 32 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 33 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 34 | arg = array[(num - 1) - idx]; |
| 35 | idx++; |
| 36 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 38 | template <typename T, typename ArrayType, typename... Args> |
| 39 | void AssignValues(unsigned int num, unsigned int idx, const ArrayType& array, T& assignee, Args&... args) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 41 | AssignValues(num, idx, array, assignee); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 43 | AssignValues(num, idx, array, args...); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | } |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 45 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 46 | } // anonymous namespace |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 47 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 48 | template <typename CopyFunc> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | void CopyTensorContentsGeneric(const ITensorHandle* srcTensor, ITensorHandle* dstTensor, CopyFunc copy) |
| 50 | { |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 51 | // For ease of understanding, names are assigned to the dimensions |
| 52 | // of the tensor as if NHWC, however this routine works with any 5D tensor |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 53 | static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyTensorContents"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 55 | TensorShape srcStrides = srcTensor->GetStrides(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | const TensorShape& srcShape = srcTensor->GetShape(); |
Rob Hughes | 9934e4e | 2019-11-27 14:27:11 +0000 | [diff] [blame] | 57 | const auto srcSize = srcTensor->GetStrides()[0] * srcShape[0]; |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 58 | IgnoreUnused(srcSize); // Only used for asserts |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 59 | TensorShape dstStrides = dstTensor->GetStrides(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | const TensorShape& dstShape = dstTensor->GetShape(); |
Rob Hughes | 9934e4e | 2019-11-27 14:27:11 +0000 | [diff] [blame] | 61 | const auto dstSize = dstTensor->GetStrides()[0] * dstShape[0]; |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 62 | IgnoreUnused(dstSize); // Only used for asserts |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 64 | size_t srcDepth = 1; |
| 65 | size_t srcBatches = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 66 | size_t srcHeight = 1; |
| 67 | size_t srcWidth = 1; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 68 | size_t srcChannels = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 69 | AssignValues(srcShape.GetNumDimensions(), |
| 70 | 0, |
| 71 | srcShape, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 72 | srcChannels, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | srcWidth, |
| 74 | srcHeight, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 75 | srcBatches, |
| 76 | srcDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 78 | size_t srcDepthStride = 0; |
| 79 | size_t srcBatchStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 80 | size_t srcHeightStride = 0; |
| 81 | size_t srcWidthStride = 0; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 82 | size_t srcChannelStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 83 | AssignValues(srcStrides.GetNumDimensions(), |
| 84 | 0, |
| 85 | srcStrides, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 86 | srcChannelStride, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 87 | srcWidthStride, |
| 88 | srcHeightStride, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 89 | srcBatchStride, |
| 90 | srcDepthStride); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 92 | size_t dstDepth = 1; |
| 93 | size_t dstBatches = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 94 | size_t dstHeight = 1; |
| 95 | size_t dstWidth = 1; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 96 | size_t dstChannels = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 97 | AssignValues(dstShape.GetNumDimensions(), |
| 98 | 0, |
| 99 | dstShape, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 100 | dstChannels, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | dstWidth, |
| 102 | dstHeight, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 103 | dstBatches, |
| 104 | dstDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 105 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 106 | size_t dstDepthStride = 0; |
| 107 | size_t dstBatchStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 108 | size_t dstHeightStride = 0; |
| 109 | size_t dstWidthStride = 0; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 110 | size_t dstChannelStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 111 | AssignValues(dstStrides.GetNumDimensions(), |
| 112 | 0, |
| 113 | dstStrides, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 114 | dstChannelStride, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | dstWidthStride, |
| 116 | dstHeightStride, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 117 | dstBatchStride, |
| 118 | dstDepthStride); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | |
Rob Hughes | 9934e4e | 2019-11-27 14:27:11 +0000 | [diff] [blame] | 120 | const unsigned char* srcDataStart; |
| 121 | unsigned char* dstDataStart; |
Sadik Armagan | bf86d51 | 2018-12-24 09:01:31 +0000 | [diff] [blame] | 122 | { |
| 123 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Synchronize buffers"); |
Rob Hughes | 9934e4e | 2019-11-27 14:27:11 +0000 | [diff] [blame] | 124 | srcDataStart = static_cast<const uint8_t*>(srcTensor->Map()); |
| 125 | dstDataStart = static_cast<uint8_t*>(dstTensor->Map()); |
Sadik Armagan | bf86d51 | 2018-12-24 09:01:31 +0000 | [diff] [blame] | 126 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | |
Rob Hughes | 9934e4e | 2019-11-27 14:27:11 +0000 | [diff] [blame] | 128 | size_t copyLength = std::min(srcChannels * srcChannelStride, dstChannels * dstChannelStride); |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 129 | size_t copyWidth = std::min(srcWidth, dstWidth); |
| 130 | size_t copyHeight = std::min(srcHeight, dstHeight); |
| 131 | size_t copyBatches = std::min(srcBatches, dstBatches); |
| 132 | size_t copyDepth = std::min(srcDepth, dstDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | |
Matthew Bentham | 019c4b1 | 2019-09-15 00:06:05 +0100 | [diff] [blame] | 134 | // Coalesce inner dimensions where possible |
| 135 | // to reduce overheard calling copy() and to |
| 136 | // allow for memory bandwidth optimisations |
| 137 | if (copyLength == srcWidthStride && |
| 138 | copyLength == dstWidthStride) |
| 139 | { |
| 140 | // There is no special padding between rows, |
| 141 | // and sizes are compatible, so copy whole rows |
| 142 | copyLength *= copyWidth; |
| 143 | copyWidth = 1; |
| 144 | |
| 145 | if (copyLength == srcHeightStride && |
| 146 | copyLength == dstHeightStride) |
| 147 | { |
| 148 | // There is no special padding between batches |
| 149 | // and sizes are compatible so copy whole batches |
| 150 | copyLength *= copyHeight; |
| 151 | copyHeight = 1; |
| 152 | } |
| 153 | } |
| 154 | |
Rob Hughes | 9934e4e | 2019-11-27 14:27:11 +0000 | [diff] [blame] | 155 | const unsigned char* srcData = srcDataStart; |
| 156 | unsigned char* dstData = dstDataStart; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 157 | for (unsigned int d = 0; d < copyDepth; ++d) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 158 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 159 | auto srcPtrDepth = srcData; |
| 160 | auto dstPtrDepth = dstData; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 161 | for (unsigned int b = 0; b < copyBatches; ++b) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 163 | auto srcPtrBatch = srcData; |
| 164 | auto dstPtrBatch = dstData; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 165 | for (unsigned int h = 0; h < copyHeight; ++h) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 166 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 167 | auto srcPtrChannel = srcData; |
| 168 | auto dstPtrChannel = dstData; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 169 | for (unsigned int w = 0; w < copyWidth; ++w) |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 170 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame^] | 171 | ARMNN_ASSERT(srcData >= srcDataStart && srcData + copyLength <= srcDataStart + srcSize); |
| 172 | ARMNN_ASSERT(dstData >= dstDataStart && dstData + copyLength <= dstDataStart + dstSize); |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 173 | copy(dstData, srcData, copyLength); |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 174 | dstData += dstWidthStride; |
| 175 | srcData += srcWidthStride; |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 176 | } |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 177 | dstData += (static_cast<long>(dstHeightStride) - (dstData - dstPtrChannel)); |
| 178 | srcData += (static_cast<long>(srcHeightStride) - (srcData - srcPtrChannel)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 179 | } |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 180 | dstData += (static_cast<long>(dstBatchStride) - (dstData - dstPtrBatch)); |
| 181 | srcData += (static_cast<long>(srcBatchStride) - (srcData - srcPtrBatch)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 182 | } |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 183 | dstData += (static_cast<long>(dstDepthStride) - (dstData - dstPtrDepth)); |
| 184 | srcData += (static_cast<long>(srcDepthStride) - (srcData - srcPtrDepth)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | srcTensor->Unmap(); |
| 188 | dstTensor->Unmap(); |
| 189 | } |
| 190 | |
| 191 | template <typename SrcTensorHandleType, typename DstTensorHandleType, typename DescriptorType> |
| 192 | void GatherTensorHandlePairs(const DescriptorType& descriptor, |
| 193 | std::vector<std::pair<SrcTensorHandleType*, DstTensorHandleType*>>& tensorHandlePairs) |
| 194 | { |
| 195 | const unsigned int numInputs = static_cast<unsigned int>(descriptor.m_Inputs.size()); |
| 196 | tensorHandlePairs.reserve(numInputs); |
| 197 | |
| 198 | for (unsigned int i = 0; i < numInputs; ++i) |
| 199 | { |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 200 | SrcTensorHandleType* const srcTensorHandle = |
| 201 | boost::polymorphic_downcast<SrcTensorHandleType*>(descriptor.m_Inputs[i]); |
| 202 | DstTensorHandleType* const dstTensorHandle = |
| 203 | boost::polymorphic_downcast<DstTensorHandleType*>(descriptor.m_Outputs[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 204 | |
| 205 | tensorHandlePairs.emplace_back(srcTensorHandle, dstTensorHandle); |
| 206 | } |
| 207 | } |
| 208 | |
Francis Murtagh | ec33a91 | 2019-11-05 14:26:23 +0000 | [diff] [blame] | 209 | int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim); |
| 210 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 211 | armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle* tensor, |
| 212 | const PermutationVector& permutationVector, |
| 213 | void* permuteBuffer); |
| 214 | |
| 215 | void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout); |
| 216 | |
| 217 | TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout); |
| 218 | |
| 219 | armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle* weightTensor, |
| 220 | DataLayout dataLayout, |
| 221 | void* permuteBuffer); |
| 222 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 223 | } //namespace armnn |