aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/TransposeConvolution2d.cpp
blob: 7408e92982e05a77cf2ec97ebe188926eb7db1a2 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "TransposeConvolution2d.hpp"

#include <armnnUtils/DataLayoutIndexed.hpp>

namespace armnn
{

using namespace armnnUtils;

void TransposeConvolution2dImpl(const TransposeConvolution2dDescriptor& descriptor,
                                const TensorShape& inputShape,
                                Decoder<float>& inputDecoder,
                                const TensorShape& outputShape,
                                Encoder<float>& outputEncoder,
                                const TensorShape& weightsShape,
                                Decoder<float>& weightsDecoder,
                                Decoder<float>* biasesDecoder)
{
    if (descriptor.m_BiasEnabled && !biasesDecoder)
    {
        throw InvalidArgumentException("Biases enabled but no bias data provided");
    }
    const DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout);
    const unsigned int channelsIndex = dataLayoutIndexed.GetChannelsIndex();
    const unsigned int heightIndex   = dataLayoutIndexed.GetHeightIndex();
    const unsigned int widthIndex    = dataLayoutIndexed.GetWidthIndex();

    const unsigned int numBatches = inputShape[0];

    const unsigned int inputWidth  = inputShape[widthIndex];
    const unsigned int inputHeight = inputShape[heightIndex];
    const unsigned int inputDepth  = inputShape[channelsIndex];

    const unsigned int weightsHeight = weightsShape[heightIndex];
    const unsigned int weightsWidth  = weightsShape[widthIndex];
    const unsigned int weightsDepth  = weightsShape[channelsIndex];

    const unsigned int outputHeight = outputShape[heightIndex];
    const unsigned int outputWidth  = outputShape[widthIndex];
    const unsigned int outputDepth  = outputShape[channelsIndex];

    const unsigned int paddingLeft = descriptor.m_PadLeft;
    const unsigned int paddingTop  = descriptor.m_PadTop;

    const unsigned int strideX = descriptor.m_StrideX;
    const unsigned int strideY = descriptor.m_StrideY;

    std::vector<float> outputBuffer(outputShape.GetNumElements(), 0);

    const std::vector<float> inputVec = inputDecoder.DecodeTensor(inputShape);
    const std::vector<float> filterVec = weightsDecoder.DecodeTensor(weightsShape);

    for (unsigned int batch = 0u; batch < numBatches; ++batch)
    {
        for (unsigned int yInput = 0u; yInput < inputHeight; ++yInput)
        {
            for (unsigned int xInput = 0u; xInput < inputWidth; ++xInput)
            {
                unsigned int xOutputOrigin = xInput * strideX - paddingLeft;
                unsigned int yOutputOrigin = yInput * strideY - paddingTop;

                for (unsigned int dOutput = 0u; dOutput < outputDepth; ++dOutput)
                {
                    for (unsigned int yWeights = 0u; yWeights < weightsHeight; ++yWeights)
                    {
                        for (unsigned int xWeights = 0u; xWeights < weightsWidth; ++xWeights)
                        {
                            unsigned int yOutput = yOutputOrigin + yWeights;
                            unsigned int xOutput = xOutputOrigin + xWeights;

                            if (yOutput < outputHeight && xOutput< outputWidth)
                            {
                                for (unsigned int dInput = 0u; dInput < inputDepth; dInput++)
                                {
                                    unsigned int inputIndex;
                                    unsigned int outputIndex;
                                    unsigned int weightsIndex;

                                    if(descriptor.m_DataLayout == armnn::DataLayout::NHWC)
                                    {
                                        inputIndex   = batch  * inputHeight * inputWidth * inputDepth +
                                                       yInput * inputWidth * inputDepth +
                                                       xInput * inputDepth +
                                                       dInput;

                                        weightsIndex = dOutput  * weightsHeight * weightsWidth * weightsDepth +
                                                       yWeights * weightsWidth * weightsDepth +
                                                       xWeights * weightsDepth +
                                                       dInput;

                                        outputIndex  = batch   * outputHeight * outputWidth * outputDepth +
                                                       yOutput * outputWidth * outputDepth +
                                                       xOutput * outputDepth +
                                                       dOutput;
                                    }
                                    else
                                    {
                                        inputIndex   = batch  * inputDepth * inputHeight * inputWidth +
                                                       dInput * inputHeight * inputWidth +
                                                       yInput * inputWidth +
                                                       xInput;

                                        weightsIndex = dOutput  * weightsDepth * weightsHeight * weightsWidth +
                                                       dInput   * weightsHeight * weightsWidth +
                                                       yWeights * weightsWidth +
                                                       xWeights;

                                        outputIndex  = batch   * outputDepth * outputHeight * outputWidth +
                                                       dOutput * outputHeight * outputWidth +
                                                       yOutput * outputWidth +
                                                       xOutput;
                                    }

                                    outputBuffer[outputIndex] += inputVec[inputIndex] * filterVec[weightsIndex];
                                }
                            }
                        }
                    }

                }
            }
        }
    }

    // Apply bias (if enabled)
    if (descriptor.m_BiasEnabled)
    {
        outputEncoder[0];
        Decoder<float>& rBiasesDecoder = *biasesDecoder;

        for (unsigned int batch = 0u; batch < numBatches; ++batch)
        {
            for (unsigned int dOutput = 0u; dOutput < outputDepth; ++dOutput)
            {
                rBiasesDecoder.SetIndex(dOutput, dOutput);
                for (unsigned int yOutput = 0u; yOutput < outputHeight; ++yOutput)
                {
                    for (unsigned int xOutput = 0u; xOutput < outputWidth; ++xOutput)
                    {
                        const unsigned int outputIndex =
                            dataLayoutIndexed.GetIndex(outputShape, batch, dOutput, yOutput, xOutput);
                        outputBuffer[outputIndex] += rBiasesDecoder.Get();
                    }
                }
            }
        }
    }
    outputEncoder[0];
    for (float output : outputBuffer)
    {
        outputEncoder.Set(output);
        ++outputEncoder;
    }
}

} // namespace armnn