aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/BatchToSpaceNd.cpp
blob: 5f64213b39f47c8a03fa9318240f8ae4260460fa (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "BatchToSpaceNd.hpp"

#include "RefWorkloadUtils.hpp"

#include <armnn/Types.hpp>

#include <boost/assert.hpp>

using namespace armnnUtils;

namespace armnn
{

inline unsigned int Offset(const TensorShape& shape, unsigned int batch, unsigned int height, unsigned int width,
        unsigned int channels, const DataLayoutIndexed& dataLayout)
{
    if (dataLayout.GetDataLayout() == DataLayout::NHWC)
    {
        return ((batch * shape[dataLayout.GetHeightIndex()] + height) * shape[dataLayout.GetWidthIndex()] + width) *
               shape[dataLayout.GetChannelsIndex()] + channels;
    }
    else
    {
        return ((batch * shape[dataLayout.GetChannelsIndex()] + channels) *
               shape[dataLayout.GetHeightIndex()] + height) *
               shape[dataLayout.GetWidthIndex()] + width;
    }
}

void BatchToSpaceNd(const DataLayoutIndexed& dataLayout,
                    const TensorInfo& inputTensorInfo,
                    const TensorInfo& outputTensorInfo,
                    const std::vector<unsigned int>& blockShape,
                    const std::vector<std::pair<unsigned int, unsigned int>>& cropsData,
                    const float* inputData,
                    float* outputData)
{
    TensorShape inputShape = inputTensorInfo.GetShape();

    BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Expected Input with 4 Dimensions");

    TensorShape outputShape = outputTensorInfo.GetShape();

    BOOST_ASSERT_MSG(outputShape.GetNumDimensions() == 4, "Expected Output with 4 Dimensions");

    const unsigned int inputBatchSize = inputShape[0];
    const unsigned int channels = inputShape[dataLayout.GetChannelsIndex()];

    const unsigned int outputBatchSize = outputShape[0];
    const unsigned int outputHeight = outputShape[dataLayout.GetHeightIndex()];
    const unsigned int outputWidth = outputShape[dataLayout.GetWidthIndex()];

    BOOST_ASSERT_MSG(blockShape.size() > 0, "BlockShape must contain 1 or more entries");

    const unsigned int blockShapeHeight = blockShape[0];
    const unsigned int blockShapeWidth = blockShape[1];

    BOOST_ASSERT_MSG(cropsData.size() > 0, "Crops must contain 1 or more entries");

    const unsigned int cropsTop = cropsData[0].first;
    const unsigned int cropsLeft = cropsData[1].first;

    for (unsigned int inBatch = 0; inBatch < inputBatchSize; ++inBatch)
    {
        const unsigned int outBatch = inBatch % outputBatchSize;
        const unsigned int spatialOffset = inBatch / outputBatchSize;

        for (unsigned int inH = 0; inH < inputTensorInfo.GetShape()[dataLayout.GetHeightIndex()]; ++inH) {
            const unsigned int outH = inH * blockShapeHeight + spatialOffset / blockShapeWidth - cropsTop;

            if (outH >= outputHeight)
            {
                continue;
            }

            for (unsigned int inW = 0; inW < inputTensorInfo.GetShape()[dataLayout.GetWidthIndex()]; ++inW) {
                const unsigned int outW = inW * blockShapeWidth + spatialOffset % blockShapeWidth - cropsLeft;

                if (outW >= outputWidth)
                {
                    continue;
                }

                for (unsigned int c = 0; c < channels; c++)
                {
                    unsigned int outOffset = Offset(outputShape, outBatch, outH, outW, c, dataLayout);
                    unsigned int inOffset = Offset(inputShape, inBatch, inH, inW, c, dataLayout);
                    outputData[outOffset] = inputData[inOffset];
                }
            }
        }
    }
}

} //namespace armnn