• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Stack(const StackQueueDescriptor & data,std::vector<std::unique_ptr<Decoder<float>>> & inputs,Encoder<float> & output)12 void Stack(const StackQueueDescriptor& data,
13            std::vector<std::unique_ptr<Decoder<float>>>& inputs,
14            Encoder<float>& output)
15 {
16     const TensorInfo& outputInfo = GetTensorInfo(data.m_Outputs[0]);
17     const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[0]);
18 
19     unsigned int outputNumDims = outputInfo.GetNumDimensions();
20     unsigned int inputNumDims = inputInfo.GetNumDimensions();
21 
22     const armnn::TensorShape& outputDims = outputInfo.GetShape();
23     const armnn::TensorShape& inputDims = inputInfo.GetShape();
24 
25     unsigned int axis = data.m_Parameters.m_Axis;
26 
27     // Initialise output data
28     unsigned int numOutputElements = 1;
29     for (unsigned int i=0; i<outputNumDims; ++i)
30     {
31         numOutputElements *= outputDims[i];
32     }
33 
34     const unsigned int iNumTensors = static_cast<unsigned int>(data.m_Inputs.size());
35     const unsigned int iBatchSize  = inputDims[0];
36     const unsigned int iChannels   = (inputNumDims > 1) ? inputDims[1] : 1;
37     const unsigned int iHeight     = (inputNumDims > 2) ? inputDims[2] : 1;
38     const unsigned int iWidth      = (inputNumDims > 3) ? inputDims[3] : 1;
39 
40     const unsigned int oBatchSize  = outputDims[1];
41     const unsigned int oChannels   = (outputNumDims > 2) ? outputDims[2] : 1;
42     const unsigned int oHeight     = (outputNumDims > 3) ? outputDims[3] : 1;
43     const unsigned int oWidth      = (outputNumDims > 4) ? outputDims[4] : 1;
44 
45     // Array to store the input coordinates
46     // iCoordinates[0] = i, iCoordinates[1] = bi, iCoordinates[2] = ci
47     // iCoordinates[3] = hi, iCoordinates[4] = wi, iCoordinates[5] = 0
48     // iCoordinates[5] will be always zero and used for not incrementing
49     // the output when the input has less than 4 dimensions
50     std::array<unsigned int, 6> iCoordinates{ 0 };
51 
52     // Array of pointers used to map the output coordinates to the input ones, in accordance with the axis
53     // This array is initialized with &iCoordinates[5] since this will be always zero
54     std::array<unsigned int *, 5> oCoordinates = { &iCoordinates[5],
55                                                    &iCoordinates[5],
56                                                    &iCoordinates[5],
57                                                    &iCoordinates[5],
58                                                    &iCoordinates[5] };
59 
60     // Set the axis coordinate
61     oCoordinates[axis] = &iCoordinates[0];
62 
63     // Map the output coordinates, accounting for the axis
64     unsigned int dim_shift = 0;
65     for(unsigned int dim = 0; dim < inputNumDims; ++dim)
66     {
67         if(dim == axis)
68         {
69             dim_shift++;
70         }
71         oCoordinates[dim + dim_shift] = &iCoordinates[dim + 1];
72     }
73 
74     // Alias for the input coordinates
75     unsigned int &i  = iCoordinates[0];
76     unsigned int &bi = iCoordinates[1];
77     unsigned int &ci = iCoordinates[2];
78     unsigned int &hi = iCoordinates[3];
79     unsigned int &wi = iCoordinates[4];
80 
81     // Alias for the output coordinates
82     unsigned int &o  = *(oCoordinates[0]);
83     unsigned int &bo = *(oCoordinates[1]);
84     unsigned int &co = *(oCoordinates[2]);
85     unsigned int &ho = *(oCoordinates[3]);
86     unsigned int &wo = *(oCoordinates[4]);
87 
88     // Stack tensors
89     for(; i < iNumTensors; ++(i))
90     {
91         for(bi = 0; bi < iBatchSize; ++(bi))
92         {
93             for(ci = 0; ci < iChannels; ++(ci))
94             {
95                 for(hi = 0; hi < iHeight; ++(hi))
96                 {
97                     for(wi = 0; wi < iWidth; ++(wi))
98                     {
99                         output[o  * oWidth * oHeight * oChannels * oBatchSize +
100                                bo * oWidth * oHeight * oChannels +
101                                co * oWidth * oHeight +
102                                ho * oWidth +
103                                wo];
104 
105                         output.Set(inputs[i]->Get());
106 
107                         ++(*(inputs[i]));
108                     }
109                 }
110             }
111         }
112     }
113 }
114 
115 } // namespace armnn
116