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 Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 49 | static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyTensorContents"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 51 | TensorShape srcStrides = srcTensor->GetStrides(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | const TensorShape& srcShape = srcTensor->GetShape(); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 53 | TensorShape dstStrides = dstTensor->GetStrides(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | const TensorShape& dstShape = dstTensor->GetShape(); |
| 55 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 56 | size_t srcDepth = 1; |
| 57 | size_t srcBatches = 1; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | size_t srcChannels = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 59 | size_t srcHeight = 1; |
| 60 | size_t srcWidth = 1; |
| 61 | AssignValues(srcShape.GetNumDimensions(), |
| 62 | 0, |
| 63 | srcShape, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | srcWidth, |
| 65 | srcHeight, |
| 66 | srcChannels, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 67 | srcBatches, |
| 68 | srcDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 70 | size_t srcDepthStride = 0; |
| 71 | size_t srcBatchStride = 0; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | size_t srcChannelStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 73 | size_t srcHeightStride = 0; |
| 74 | size_t srcWidthStride = 0; |
| 75 | AssignValues(srcStrides.GetNumDimensions(), |
| 76 | 0, |
| 77 | srcStrides, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | srcWidthStride, |
| 79 | srcHeightStride, |
| 80 | srcChannelStride, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 81 | srcBatchStride, |
| 82 | srcDepthStride); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 83 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 84 | size_t dstDepth = 1; |
| 85 | size_t dstBatches = 1; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 86 | size_t dstChannels = 1; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 87 | size_t dstHeight = 1; |
| 88 | size_t dstWidth = 1; |
| 89 | AssignValues(dstShape.GetNumDimensions(), |
| 90 | 0, |
| 91 | dstShape, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | dstWidth, |
| 93 | dstHeight, |
| 94 | dstChannels, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 95 | dstBatches, |
| 96 | dstDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 97 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 98 | size_t dstDepthStride = 0; |
| 99 | size_t dstBatchStride = 0; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | size_t dstChannelStride = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 101 | size_t dstHeightStride = 0; |
| 102 | size_t dstWidthStride = 0; |
| 103 | AssignValues(dstStrides.GetNumDimensions(), |
| 104 | 0, |
| 105 | dstStrides, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 106 | dstWidthStride, |
| 107 | dstHeightStride, |
| 108 | dstChannelStride, |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 109 | dstBatchStride, |
| 110 | dstDepthStride); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | |
Sadik Armagan | bf86d51 | 2018-12-24 09:01:31 +0000 | [diff] [blame] | 112 | const unsigned char* srcData; |
| 113 | unsigned char* dstData; |
| 114 | { |
| 115 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Synchronize buffers"); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 116 | srcData = static_cast<const uint8_t*>(srcTensor->Map()); |
| 117 | dstData = static_cast<uint8_t*>(dstTensor->Map()); |
Sadik Armagan | bf86d51 | 2018-12-24 09:01:31 +0000 | [diff] [blame] | 118 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 120 | size_t copyLength = std::min(srcWidth * srcWidthStride, dstWidth * dstWidthStride); |
| 121 | size_t copyHeight = std::min(srcHeight, dstHeight); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | size_t copyChannels = std::min(srcChannels, dstChannels); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 123 | size_t copyBatches = std::min(srcBatches, dstBatches); |
| 124 | size_t copyDepth = std::min(srcDepth, dstDepth); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 126 | for (unsigned int d = 0; d < copyDepth; ++d) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 128 | auto srcPtrDepth = srcData; |
| 129 | auto dstPtrDepth = dstData; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 130 | for (unsigned int b = 0; b < copyBatches; ++b) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 131 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 132 | auto srcPtrBatch = srcData; |
| 133 | auto dstPtrBatch = dstData; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 134 | for (unsigned int c = 0; c < copyChannels; ++c) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | { |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 136 | auto srcPtrChannel = srcData; |
| 137 | auto dstPtrChannel = dstData; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 138 | for (unsigned int h = 0; h < copyHeight; ++h) |
Matthew Jackson | dba634f | 2019-08-15 15:14:18 +0100 | [diff] [blame] | 139 | { |
| 140 | copy(dstData, srcData, copyLength); |
| 141 | dstData += dstHeightStride; |
| 142 | srcData += srcHeightStride; |
| 143 | } |
| 144 | dstData += (static_cast<long>(dstChannelStride) - (dstData - dstPtrChannel)); |
| 145 | srcData += (static_cast<long>(srcChannelStride) - (srcData - srcPtrChannel)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 146 | } |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 147 | dstData += (static_cast<long>(dstBatchStride) - (dstData - dstPtrBatch)); |
| 148 | srcData += (static_cast<long>(srcBatchStride) - (srcData - srcPtrBatch)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | } |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 150 | dstData += (static_cast<long>(dstDepthStride) - (dstData - dstPtrDepth)); |
| 151 | srcData += (static_cast<long>(srcDepthStride) - (srcData - srcPtrDepth)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | srcTensor->Unmap(); |
| 155 | dstTensor->Unmap(); |
| 156 | } |
| 157 | |
| 158 | template <typename SrcTensorHandleType, typename DstTensorHandleType, typename DescriptorType> |
| 159 | void GatherTensorHandlePairs(const DescriptorType& descriptor, |
| 160 | std::vector<std::pair<SrcTensorHandleType*, DstTensorHandleType*>>& tensorHandlePairs) |
| 161 | { |
| 162 | const unsigned int numInputs = static_cast<unsigned int>(descriptor.m_Inputs.size()); |
| 163 | tensorHandlePairs.reserve(numInputs); |
| 164 | |
| 165 | for (unsigned int i = 0; i < numInputs; ++i) |
| 166 | { |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 167 | SrcTensorHandleType* const srcTensorHandle = |
| 168 | boost::polymorphic_downcast<SrcTensorHandleType*>(descriptor.m_Inputs[i]); |
| 169 | DstTensorHandleType* const dstTensorHandle = |
| 170 | boost::polymorphic_downcast<DstTensorHandleType*>(descriptor.m_Outputs[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 171 | |
| 172 | tensorHandlePairs.emplace_back(srcTensorHandle, dstTensorHandle); |
| 173 | } |
| 174 | } |
| 175 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 176 | armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle* tensor, |
| 177 | const PermutationVector& permutationVector, |
| 178 | void* permuteBuffer); |
| 179 | |
| 180 | void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout); |
| 181 | |
| 182 | TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout); |
| 183 | |
| 184 | armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle* weightTensor, |
| 185 | DataLayout dataLayout, |
| 186 | void* permuteBuffer); |
| 187 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 188 | } //namespace armnn |