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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 8 | #include "ITensorHandle.hpp" |
| 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <armnn/Tensor.hpp> |
| 11 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | #include <boost/cast.hpp> |
| 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | namespace |
| 17 | { |
| 18 | template<typename ArrayType, typename Arg> |
| 19 | void AssignValues(unsigned int num, unsigned int& idx, const ArrayType& array, Arg& arg) |
| 20 | { |
| 21 | if (idx >= num) |
| 22 | { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | arg = array[(num - 1) - idx]; |
| 27 | idx++; |
| 28 | }; |
| 29 | |
| 30 | template<typename T, typename ArrayType, typename ...Args> |
| 31 | void AssignValues(unsigned int num, unsigned int idx, const ArrayType& array, T& assignee, Args& ... args) |
| 32 | { |
| 33 | AssignValues(num, idx, array, assignee); |
| 34 | |
| 35 | AssignValues(num, idx, array, args...); |
| 36 | } |
| 37 | } // namespace |
| 38 | |
| 39 | template<typename CopyFunc> |
| 40 | void CopyTensorContentsGeneric(const ITensorHandle* srcTensor, ITensorHandle* dstTensor, CopyFunc copy) |
| 41 | { |
| 42 | static_assert(MaxNumOfTensorDimensions == 4, "Please update CopyTensorContents"); |
| 43 | |
| 44 | TensorShape srcStrides = srcTensor->GetStrides(); |
| 45 | const TensorShape& srcShape = srcTensor->GetShape(); |
| 46 | TensorShape dstStrides = dstTensor->GetStrides(); |
| 47 | const TensorShape& dstShape = dstTensor->GetShape(); |
| 48 | |
| 49 | size_t srcBatches = 1; |
| 50 | size_t srcChannels = 1; |
| 51 | size_t srcHeight = 1; |
| 52 | size_t srcWidth = 1; |
| 53 | AssignValues(srcShape.GetNumDimensions(),0, srcShape, |
| 54 | srcWidth, |
| 55 | srcHeight, |
| 56 | srcChannels, |
| 57 | srcBatches); |
| 58 | |
| 59 | size_t srcBatchStride = 0; |
| 60 | size_t srcChannelStride = 0; |
| 61 | size_t srcHeightStride = 0; |
| 62 | size_t srcWidthStride = 0; |
| 63 | AssignValues(srcStrides.GetNumDimensions(),0, srcStrides, |
| 64 | srcWidthStride, |
| 65 | srcHeightStride, |
| 66 | srcChannelStride, |
| 67 | srcBatchStride); |
| 68 | |
| 69 | size_t dstBatches = 1; |
| 70 | size_t dstChannels = 1; |
| 71 | size_t dstHeight = 1; |
| 72 | size_t dstWidth = 1; |
| 73 | AssignValues(dstShape.GetNumDimensions(),0, dstShape, |
| 74 | dstWidth, |
| 75 | dstHeight, |
| 76 | dstChannels, |
| 77 | dstBatches); |
| 78 | |
| 79 | size_t dstBatchStride = 0; |
| 80 | size_t dstChannelStride = 0; |
| 81 | size_t dstHeightStride = 0; |
| 82 | size_t dstWidthStride = 0; |
| 83 | AssignValues(dstStrides.GetNumDimensions(),0, dstStrides, |
| 84 | dstWidthStride, |
| 85 | dstHeightStride, |
| 86 | dstChannelStride, |
| 87 | dstBatchStride); |
| 88 | |
| 89 | auto srcData = static_cast<const uint8_t*>(srcTensor->Map()); |
| 90 | auto dstData = static_cast<uint8_t*>(dstTensor->Map()); |
| 91 | |
| 92 | size_t copyLength = std::min(srcWidth*srcWidthStride, dstWidth*dstWidthStride); |
| 93 | size_t copyHeight = std::min(srcHeight, dstHeight); |
| 94 | size_t copyChannels = std::min(srcChannels, dstChannels); |
| 95 | size_t copyBatches = std::min(srcBatches, dstBatches); |
| 96 | |
| 97 | for(unsigned int b=0; b < copyBatches; ++b) |
| 98 | { |
| 99 | auto srcPtrBatch = srcData; |
| 100 | auto dstPtrBatch = dstData; |
| 101 | for (unsigned int c=0; c< copyChannels; ++c) |
| 102 | { |
| 103 | auto srcPtrChannel = srcData; |
| 104 | auto dstPtrChannel = dstData; |
| 105 | for (unsigned int h=0; h < copyHeight; ++h) |
| 106 | { |
| 107 | copy(dstData, srcData, copyLength); |
| 108 | dstData += dstHeightStride; |
| 109 | srcData += srcHeightStride; |
| 110 | } |
| 111 | dstData += (static_cast<long>(dstChannelStride) - (dstData - dstPtrChannel)); |
| 112 | srcData += (static_cast<long>(srcChannelStride) - (srcData - srcPtrChannel)); |
| 113 | } |
| 114 | dstData += (static_cast<long>(dstBatchStride)-(dstData - dstPtrBatch)); |
| 115 | srcData += (static_cast<long>(srcBatchStride)-(srcData - srcPtrBatch)); |
| 116 | } |
| 117 | |
| 118 | srcTensor->Unmap(); |
| 119 | dstTensor->Unmap(); |
| 120 | } |
| 121 | |
| 122 | template <typename SrcTensorHandleType, typename DstTensorHandleType, typename DescriptorType> |
| 123 | void GatherTensorHandlePairs(const DescriptorType& descriptor, |
| 124 | std::vector<std::pair<SrcTensorHandleType*, DstTensorHandleType*>>& tensorHandlePairs) |
| 125 | { |
| 126 | const unsigned int numInputs = static_cast<unsigned int>(descriptor.m_Inputs.size()); |
| 127 | tensorHandlePairs.reserve(numInputs); |
| 128 | |
| 129 | for (unsigned int i = 0; i < numInputs; ++i) |
| 130 | { |
| 131 | SrcTensorHandleType* const srcTensorHandle = boost::polymorphic_downcast<SrcTensorHandleType*>( |
| 132 | descriptor.m_Inputs[i]); |
| 133 | DstTensorHandleType* const dstTensorHandle = boost::polymorphic_downcast<DstTensorHandleType*>( |
| 134 | descriptor.m_Outputs[i]); |
| 135 | |
| 136 | tensorHandlePairs.emplace_back(srcTensorHandle, dstTensorHandle); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } //namespace armnn |