Matthew Jackson | 81e601c | 2019-07-11 12:07:09 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Stack.hpp" |
| 7 | #include "RefWorkloadUtils.hpp" |
| 8 | |
| 9 | namespace armnn |
| 10 | { |
| 11 | |
| 12 | void Stack(const StackQueueDescriptor& data, |
| 13 | std::vector<std::unique_ptr<Decoder<float>>>& inputs, |
mathad01 | f52e9fd | 2021-05-05 12:51:56 +0100 | [diff] [blame] | 14 | Encoder<float>& output, |
| 15 | const TensorInfo& inputInfo, |
| 16 | const TensorInfo& outputInfo) |
Matthew Jackson | 81e601c | 2019-07-11 12:07:09 +0100 | [diff] [blame] | 17 | { |
Matthew Jackson | 81e601c | 2019-07-11 12:07:09 +0100 | [diff] [blame] | 18 | unsigned int outputNumDims = outputInfo.GetNumDimensions(); |
| 19 | unsigned int inputNumDims = inputInfo.GetNumDimensions(); |
| 20 | |
| 21 | const armnn::TensorShape& outputDims = outputInfo.GetShape(); |
| 22 | const armnn::TensorShape& inputDims = inputInfo.GetShape(); |
| 23 | |
| 24 | unsigned int axis = data.m_Parameters.m_Axis; |
| 25 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 26 | // Can perform a simple concatenation when axis == 0 |
| 27 | if (!axis) |
| 28 | { |
| 29 | unsigned int numInputs = data.m_Parameters.m_NumInputs; |
| 30 | unsigned int inputLength = inputInfo.GetNumElements(); |
| 31 | |
| 32 | for (unsigned int inputIdx=0; inputIdx<numInputs; ++inputIdx) |
| 33 | { |
| 34 | for (unsigned int elmt=0; elmt<inputLength; ++elmt) |
| 35 | { |
| 36 | (*inputs[inputIdx])[elmt]; |
| 37 | output[(inputIdx * inputLength) + elmt]; |
| 38 | output.Set(inputs[inputIdx]->Get()); |
| 39 | } |
| 40 | } |
| 41 | return; |
| 42 | } |
| 43 | |
Matthew Jackson | 81e601c | 2019-07-11 12:07:09 +0100 | [diff] [blame] | 44 | // Initialise output data |
| 45 | unsigned int numOutputElements = 1; |
| 46 | for (unsigned int i=0; i<outputNumDims; ++i) |
| 47 | { |
| 48 | numOutputElements *= outputDims[i]; |
| 49 | } |
| 50 | |
| 51 | const unsigned int iNumTensors = static_cast<unsigned int>(data.m_Inputs.size()); |
| 52 | const unsigned int iBatchSize = inputDims[0]; |
| 53 | const unsigned int iChannels = (inputNumDims > 1) ? inputDims[1] : 1; |
| 54 | const unsigned int iHeight = (inputNumDims > 2) ? inputDims[2] : 1; |
| 55 | const unsigned int iWidth = (inputNumDims > 3) ? inputDims[3] : 1; |
| 56 | |
| 57 | const unsigned int oBatchSize = outputDims[1]; |
| 58 | const unsigned int oChannels = (outputNumDims > 2) ? outputDims[2] : 1; |
| 59 | const unsigned int oHeight = (outputNumDims > 3) ? outputDims[3] : 1; |
| 60 | const unsigned int oWidth = (outputNumDims > 4) ? outputDims[4] : 1; |
| 61 | |
| 62 | // Array to store the input coordinates |
| 63 | // iCoordinates[0] = i, iCoordinates[1] = bi, iCoordinates[2] = ci |
| 64 | // iCoordinates[3] = hi, iCoordinates[4] = wi, iCoordinates[5] = 0 |
| 65 | // iCoordinates[5] will be always zero and used for not incrementing |
| 66 | // the output when the input has less than 4 dimensions |
| 67 | std::array<unsigned int, 6> iCoordinates{ 0 }; |
| 68 | |
| 69 | // Array of pointers used to map the output coordinates to the input ones, in accordance with the axis |
| 70 | // This array is initialized with &iCoordinates[5] since this will be always zero |
| 71 | std::array<unsigned int *, 5> oCoordinates = { &iCoordinates[5], |
| 72 | &iCoordinates[5], |
| 73 | &iCoordinates[5], |
| 74 | &iCoordinates[5], |
| 75 | &iCoordinates[5] }; |
| 76 | |
| 77 | // Set the axis coordinate |
| 78 | oCoordinates[axis] = &iCoordinates[0]; |
| 79 | |
| 80 | // Map the output coordinates, accounting for the axis |
| 81 | unsigned int dim_shift = 0; |
| 82 | for(unsigned int dim = 0; dim < inputNumDims; ++dim) |
| 83 | { |
| 84 | if(dim == axis) |
| 85 | { |
| 86 | dim_shift++; |
| 87 | } |
| 88 | oCoordinates[dim + dim_shift] = &iCoordinates[dim + 1]; |
| 89 | } |
| 90 | |
| 91 | // Alias for the input coordinates |
| 92 | unsigned int &i = iCoordinates[0]; |
| 93 | unsigned int &bi = iCoordinates[1]; |
| 94 | unsigned int &ci = iCoordinates[2]; |
| 95 | unsigned int &hi = iCoordinates[3]; |
| 96 | unsigned int &wi = iCoordinates[4]; |
| 97 | |
| 98 | // Alias for the output coordinates |
| 99 | unsigned int &o = *(oCoordinates[0]); |
| 100 | unsigned int &bo = *(oCoordinates[1]); |
| 101 | unsigned int &co = *(oCoordinates[2]); |
| 102 | unsigned int &ho = *(oCoordinates[3]); |
| 103 | unsigned int &wo = *(oCoordinates[4]); |
| 104 | |
| 105 | // Stack tensors |
| 106 | for(; i < iNumTensors; ++(i)) |
| 107 | { |
| 108 | for(bi = 0; bi < iBatchSize; ++(bi)) |
| 109 | { |
| 110 | for(ci = 0; ci < iChannels; ++(ci)) |
| 111 | { |
| 112 | for(hi = 0; hi < iHeight; ++(hi)) |
| 113 | { |
| 114 | for(wi = 0; wi < iWidth; ++(wi)) |
| 115 | { |
| 116 | output[o * oWidth * oHeight * oChannels * oBatchSize + |
| 117 | bo * oWidth * oHeight * oChannels + |
| 118 | co * oWidth * oHeight + |
| 119 | ho * oWidth + |
| 120 | wo]; |
| 121 | |
| 122 | output.Set(inputs[i]->Get()); |
| 123 | |
| 124 | ++(*(inputs[i])); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace armnn |