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" |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 9 | #include "ITensorHandle.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | #include <armnn/Tensor.hpp> |
| 12 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 13 | #include <Half.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 14 | #include <Permute.hpp> |
| 15 | #include <Profiling.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 16 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | #include <boost/cast.hpp> |
| 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(); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 55 | TensorShape dstStrides = dstTensor->GetStrides(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | const TensorShape& dstShape = dstTensor->GetShape(); |
| 57 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 58 | size_t srcDepth = 1; |
| 59 | size_t srcBatches = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 60 | size_t srcHeight = 1; |
| 61 | size_t srcWidth = 1; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 62 | size_t srcChannels = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 63 | AssignValues(srcShape.GetNumDimensions(), |
| 64 | 0, |
| 65 | srcShape, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 66 | srcChannels, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 67 | srcWidth, |
| 68 | srcHeight, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 69 | srcBatches, |
| 70 | srcDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 71 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 72 | size_t srcDepthStride = 0; |
| 73 | size_t srcBatchStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 74 | size_t srcHeightStride = 0; |
| 75 | size_t srcWidthStride = 0; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 76 | size_t srcChannelStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 77 | AssignValues(srcStrides.GetNumDimensions(), |
| 78 | 0, |
| 79 | srcStrides, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 80 | srcChannelStride, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | srcWidthStride, |
| 82 | srcHeightStride, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 83 | srcBatchStride, |
| 84 | srcDepthStride); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 86 | size_t dstDepth = 1; |
| 87 | size_t dstBatches = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 88 | size_t dstHeight = 1; |
| 89 | size_t dstWidth = 1; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 90 | size_t dstChannels = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 91 | AssignValues(dstShape.GetNumDimensions(), |
| 92 | 0, |
| 93 | dstShape, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 94 | dstChannels, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 95 | dstWidth, |
| 96 | dstHeight, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 97 | dstBatches, |
| 98 | dstDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 99 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 100 | size_t dstDepthStride = 0; |
| 101 | size_t dstBatchStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 102 | size_t dstHeightStride = 0; |
| 103 | size_t dstWidthStride = 0; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 104 | size_t dstChannelStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 105 | AssignValues(dstStrides.GetNumDimensions(), |
| 106 | 0, |
| 107 | dstStrides, |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 108 | dstChannelStride, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | dstWidthStride, |
| 110 | dstHeightStride, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 111 | dstBatchStride, |
| 112 | dstDepthStride); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 113 | |
Sadik Armagan | bf86d51 | 2018-12-24 09:01:31 +0000 | [diff] [blame] | 114 | const unsigned char* srcData; |
| 115 | unsigned char* dstData; |
| 116 | { |
| 117 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Synchronize buffers"); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 118 | srcData = static_cast<const uint8_t*>(srcTensor->Map()); |
| 119 | dstData = static_cast<uint8_t*>(dstTensor->Map()); |
Sadik Armagan | bf86d51 | 2018-12-24 09:01:31 +0000 | [diff] [blame] | 120 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 121 | |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 122 | size_t copyLength = std::min(srcChannels*srcChannelStride, dstChannels*dstChannelStride); |
| 123 | size_t copyWidth = std::min(srcWidth, dstWidth); |
| 124 | size_t copyHeight = std::min(srcHeight, dstHeight); |
| 125 | size_t copyBatches = std::min(srcBatches, dstBatches); |
| 126 | size_t copyDepth = std::min(srcDepth, dstDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | |
Matthew Bentham | 019c4b1 | 2019-09-15 00:06:05 +0100 | [diff] [blame] | 128 | // Coalesce inner dimensions where possible |
| 129 | // to reduce overheard calling copy() and to |
| 130 | // allow for memory bandwidth optimisations |
| 131 | if (copyLength == srcWidthStride && |
| 132 | copyLength == dstWidthStride) |
| 133 | { |
| 134 | // There is no special padding between rows, |
| 135 | // and sizes are compatible, so copy whole rows |
| 136 | copyLength *= copyWidth; |
| 137 | copyWidth = 1; |
| 138 | |
| 139 | if (copyLength == srcHeightStride && |
| 140 | copyLength == dstHeightStride) |
| 141 | { |
| 142 | // There is no special padding between batches |
| 143 | // and sizes are compatible so copy whole batches |
| 144 | copyLength *= copyHeight; |
| 145 | copyHeight = 1; |
| 146 | } |
| 147 | } |
| 148 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 149 | for (unsigned int d = 0; d < copyDepth; ++d) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 150 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 151 | auto srcPtrDepth = srcData; |
| 152 | auto dstPtrDepth = dstData; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 153 | for (unsigned int b = 0; b < copyBatches; ++b) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 154 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 155 | auto srcPtrBatch = srcData; |
| 156 | auto dstPtrBatch = dstData; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 157 | for (unsigned int h = 0; h < copyHeight; ++h) |
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 srcPtrChannel = srcData; |
| 160 | auto dstPtrChannel = dstData; |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 161 | for (unsigned int w = 0; w < copyWidth; ++w) |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 162 | { |
| 163 | copy(dstData, srcData, copyLength); |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 164 | dstData += dstWidthStride; |
| 165 | srcData += srcWidthStride; |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 166 | } |
Matthew Bentham | efdbca6 | 2019-09-14 23:35:28 +0100 | [diff] [blame] | 167 | dstData += (static_cast<long>(dstHeightStride) - (dstData - dstPtrChannel)); |
| 168 | srcData += (static_cast<long>(srcHeightStride) - (srcData - srcPtrChannel)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 169 | } |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 170 | dstData += (static_cast<long>(dstBatchStride) - (dstData - dstPtrBatch)); |
| 171 | srcData += (static_cast<long>(srcBatchStride) - (srcData - srcPtrBatch)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 172 | } |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 173 | dstData += (static_cast<long>(dstDepthStride) - (dstData - dstPtrDepth)); |
| 174 | srcData += (static_cast<long>(srcDepthStride) - (srcData - srcPtrDepth)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | srcTensor->Unmap(); |
| 178 | dstTensor->Unmap(); |
| 179 | } |
| 180 | |
| 181 | template <typename SrcTensorHandleType, typename DstTensorHandleType, typename DescriptorType> |
| 182 | void GatherTensorHandlePairs(const DescriptorType& descriptor, |
| 183 | std::vector<std::pair<SrcTensorHandleType*, DstTensorHandleType*>>& tensorHandlePairs) |
| 184 | { |
| 185 | const unsigned int numInputs = static_cast<unsigned int>(descriptor.m_Inputs.size()); |
| 186 | tensorHandlePairs.reserve(numInputs); |
| 187 | |
| 188 | for (unsigned int i = 0; i < numInputs; ++i) |
| 189 | { |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 190 | SrcTensorHandleType* const srcTensorHandle = |
| 191 | boost::polymorphic_downcast<SrcTensorHandleType*>(descriptor.m_Inputs[i]); |
| 192 | DstTensorHandleType* const dstTensorHandle = |
| 193 | boost::polymorphic_downcast<DstTensorHandleType*>(descriptor.m_Outputs[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 194 | |
| 195 | tensorHandlePairs.emplace_back(srcTensorHandle, dstTensorHandle); |
| 196 | } |
| 197 | } |
| 198 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 199 | armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle* tensor, |
| 200 | const PermutationVector& permutationVector, |
| 201 | void* permuteBuffer); |
| 202 | |
| 203 | void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout); |
| 204 | |
| 205 | TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout); |
| 206 | |
| 207 | armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle* weightTensor, |
| 208 | DataLayout dataLayout, |
| 209 | void* permuteBuffer); |
| 210 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 211 | } //namespace armnn |