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