blob: 4313085ba55c5ef97f651fd9bc045442909e7ec6 [file] [log] [blame]
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "BatchToSpaceNd.hpp"
7
8#include "RefWorkloadUtils.hpp"
9
10#include <armnn/Types.hpp>
11
12#include <boost/assert.hpp>
13
14namespace armnn
15{
16
17inline unsigned int Offset(const TensorShape& shape, unsigned int batch, unsigned int height, unsigned int width,
18 unsigned int channels, const DataLayoutIndexed& dataLayout)
19{
20 if (dataLayout.GetDataLayout() == DataLayout::NHWC)
21 {
22 return ((batch * shape[dataLayout.GetHeightIndex()] + height) * shape[dataLayout.GetWidthIndex()] + width) *
23 shape[dataLayout.GetChannelsIndex()] + channels;
24 }
25 else
26 {
27 return ((batch * shape[dataLayout.GetChannelsIndex()] + channels) *
28 shape[dataLayout.GetHeightIndex()] + height) *
29 shape[dataLayout.GetWidthIndex()] + width;
30 }
31}
32
33void BatchToSpaceNd(const DataLayoutIndexed& dataLayout,
34 const TensorInfo& inputTensorInfo,
35 const TensorInfo& outputTensorInfo,
36 const std::vector<unsigned int>& blockShape,
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000037 const std::vector<std::pair<unsigned int, unsigned int>>& cropsData,
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000038 const float* inputData,
39 float* outputData)
40{
41 TensorShape inputShape = inputTensorInfo.GetShape();
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000042
43 BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Expected Input with 4 Dimensions");
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000044
45 TensorShape outputShape = outputTensorInfo.GetShape();
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000046
47 BOOST_ASSERT_MSG(outputShape.GetNumDimensions() == 4, "Expected Output with 4 Dimensions");
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000048
49 const unsigned int inputBatchSize = inputShape[0];
50 const unsigned int channels = inputShape[dataLayout.GetChannelsIndex()];
51
52 const unsigned int outputBatchSize = outputShape[0];
53 const unsigned int outputHeight = outputShape[dataLayout.GetHeightIndex()];
54 const unsigned int outputWidth = outputShape[dataLayout.GetWidthIndex()];
55
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000056 BOOST_ASSERT_MSG(blockShape.size() > 0, "BlockShape must contain 1 or more entries");
57
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000058 const unsigned int blockShapeHeight = blockShape[0];
59 const unsigned int blockShapeWidth = blockShape[1];
60
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000061 BOOST_ASSERT_MSG(cropsData.size() > 0, "Crops must contain 1 or more entries");
62
63 const unsigned int cropsTop = cropsData[0].first;
64 const unsigned int cropsLeft = cropsData[1].first;
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000065
66 for (unsigned int inBatch = 0; inBatch < inputBatchSize; ++inBatch)
67 {
68 const unsigned int outBatch = inBatch % outputBatchSize;
69 const unsigned int spatialOffset = inBatch / outputBatchSize;
70
71 for (unsigned int inH = 0; inH < inputTensorInfo.GetShape()[dataLayout.GetHeightIndex()]; ++inH) {
72 const unsigned int outH = inH * blockShapeHeight + spatialOffset / blockShapeWidth - cropsTop;
73
74 if (outH >= outputHeight)
75 {
76 continue;
77 }
78
79 for (unsigned int inW = 0; inW < inputTensorInfo.GetShape()[dataLayout.GetWidthIndex()]; ++inW) {
80 const unsigned int outW = inW * blockShapeWidth + spatialOffset % blockShapeWidth - cropsLeft;
81
82 if (outW >= outputWidth)
83 {
84 continue;
85 }
86
87 for (unsigned int c = 0; c < channels; c++)
88 {
89 unsigned int outOffset = Offset(outputShape, outBatch, outH, outW, c, dataLayout);
90 unsigned int inOffset = Offset(inputShape, inBatch, inH, inW, c, dataLayout);
91 outputData[outOffset] = inputData[inOffset];
92 }
93 }
94 }
95 }
96}
97
98} //namespace armnn