aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/Stack.cpp
blob: 386c8992ebe00dae7523505639c468cdfe13fe52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "Stack.hpp"
#include "RefWorkloadUtils.hpp"

namespace armnn
{

void Stack(const StackQueueDescriptor& data,
           std::vector<std::unique_ptr<Decoder<float>>>& inputs,
           Encoder<float>& output)
{
    const TensorInfo& outputInfo = GetTensorInfo(data.m_Outputs[0]);
    const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[0]);

    unsigned int outputNumDims = outputInfo.GetNumDimensions();
    unsigned int inputNumDims = inputInfo.GetNumDimensions();

    const armnn::TensorShape& outputDims = outputInfo.GetShape();
    const armnn::TensorShape& inputDims = inputInfo.GetShape();

    unsigned int axis = data.m_Parameters.m_Axis;

    // Initialise output data
    unsigned int numOutputElements = 1;
    for (unsigned int i=0; i<outputNumDims; ++i)
    {
        numOutputElements *= outputDims[i];
    }

    const unsigned int iNumTensors = static_cast<unsigned int>(data.m_Inputs.size());
    const unsigned int iBatchSize  = inputDims[0];
    const unsigned int iChannels   = (inputNumDims > 1) ? inputDims[1] : 1;
    const unsigned int iHeight     = (inputNumDims > 2) ? inputDims[2] : 1;
    const unsigned int iWidth      = (inputNumDims > 3) ? inputDims[3] : 1;

    const unsigned int oBatchSize  = outputDims[1];
    const unsigned int oChannels   = (outputNumDims > 2) ? outputDims[2] : 1;
    const unsigned int oHeight     = (outputNumDims > 3) ? outputDims[3] : 1;
    const unsigned int oWidth      = (outputNumDims > 4) ? outputDims[4] : 1;

    // Array to store the input coordinates
    // iCoordinates[0] = i, iCoordinates[1] = bi, iCoordinates[2] = ci
    // iCoordinates[3] = hi, iCoordinates[4] = wi, iCoordinates[5] = 0
    // iCoordinates[5] will be always zero and used for not incrementing
    // the output when the input has less than 4 dimensions
    std::array<unsigned int, 6> iCoordinates{ 0 };

    // Array of pointers used to map the output coordinates to the input ones, in accordance with the axis
    // This array is initialized with &iCoordinates[5] since this will be always zero
    std::array<unsigned int *, 5> oCoordinates = { &iCoordinates[5],
                                                   &iCoordinates[5],
                                                   &iCoordinates[5],
                                                   &iCoordinates[5],
                                                   &iCoordinates[5] };

    // Set the axis coordinate
    oCoordinates[axis] = &iCoordinates[0];

    // Map the output coordinates, accounting for the axis
    unsigned int dim_shift = 0;
    for(unsigned int dim = 0; dim < inputNumDims; ++dim)
    {
        if(dim == axis)
        {
            dim_shift++;
        }
        oCoordinates[dim + dim_shift] = &iCoordinates[dim + 1];
    }

    // Alias for the input coordinates
    unsigned int &i  = iCoordinates[0];
    unsigned int &bi = iCoordinates[1];
    unsigned int &ci = iCoordinates[2];
    unsigned int &hi = iCoordinates[3];
    unsigned int &wi = iCoordinates[4];

    // Alias for the output coordinates
    unsigned int &o  = *(oCoordinates[0]);
    unsigned int &bo = *(oCoordinates[1]);
    unsigned int &co = *(oCoordinates[2]);
    unsigned int &ho = *(oCoordinates[3]);
    unsigned int &wo = *(oCoordinates[4]);

    // Stack tensors
    for(; i < iNumTensors; ++(i))
    {
        for(bi = 0; bi < iBatchSize; ++(bi))
        {
            for(ci = 0; ci < iChannels; ++(ci))
            {
                for(hi = 0; hi < iHeight; ++(hi))
                {
                    for(wi = 0; wi < iWidth; ++(wi))
                    {
                        output[o  * oWidth * oHeight * oChannels * oBatchSize +
                               bo * oWidth * oHeight * oChannels +
                               co * oWidth * oHeight +
                               ho * oWidth +
                               wo];

                        output.Set(inputs[i]->Get());

                        ++(*(inputs[i]));
                    }
                }
            }
        }
    }
}

} // namespace armnn