blob: bf7de1b04c297834c0965b2148dd701df85758d6 [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
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010012#include <armnn/utility/Assert.hpp>
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000013
Matteo Martincigh21350152018-11-28 16:22:22 +000014using namespace armnnUtils;
15
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000016namespace armnn
17{
18
19inline unsigned int Offset(const TensorShape& shape, unsigned int batch, unsigned int height, unsigned int width,
20 unsigned int channels, const DataLayoutIndexed& dataLayout)
21{
22 if (dataLayout.GetDataLayout() == DataLayout::NHWC)
23 {
24 return ((batch * shape[dataLayout.GetHeightIndex()] + height) * shape[dataLayout.GetWidthIndex()] + width) *
25 shape[dataLayout.GetChannelsIndex()] + channels;
26 }
27 else
28 {
29 return ((batch * shape[dataLayout.GetChannelsIndex()] + channels) *
30 shape[dataLayout.GetHeightIndex()] + height) *
31 shape[dataLayout.GetWidthIndex()] + width;
32 }
33}
34
35void BatchToSpaceNd(const DataLayoutIndexed& dataLayout,
36 const TensorInfo& inputTensorInfo,
37 const TensorInfo& outputTensorInfo,
38 const std::vector<unsigned int>& blockShape,
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000039 const std::vector<std::pair<unsigned int, unsigned int>>& cropsData,
Francis Murtagh47ea3c02019-06-20 12:07:19 +010040 Decoder<float>& inputDecoder,
41 Encoder<float>& outputEncoder)
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000042{
43 TensorShape inputShape = inputTensorInfo.GetShape();
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000044
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010045 ARMNN_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Expected Input with 4 Dimensions");
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000046
47 TensorShape outputShape = outputTensorInfo.GetShape();
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000048
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010049 ARMNN_ASSERT_MSG(outputShape.GetNumDimensions() == 4, "Expected Output with 4 Dimensions");
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000050
51 const unsigned int inputBatchSize = inputShape[0];
52 const unsigned int channels = inputShape[dataLayout.GetChannelsIndex()];
53
54 const unsigned int outputBatchSize = outputShape[0];
55 const unsigned int outputHeight = outputShape[dataLayout.GetHeightIndex()];
56 const unsigned int outputWidth = outputShape[dataLayout.GetWidthIndex()];
57
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010058 ARMNN_ASSERT_MSG(blockShape.size() > 0, "BlockShape must contain 1 or more entries");
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000059
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000060 const unsigned int blockShapeHeight = blockShape[0];
61 const unsigned int blockShapeWidth = blockShape[1];
62
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010063 ARMNN_ASSERT_MSG(cropsData.size() > 0, "Crops must contain 1 or more entries");
Éanna Ó Catháin95807ce2018-11-12 17:14:43 +000064
65 const unsigned int cropsTop = cropsData[0].first;
66 const unsigned int cropsLeft = cropsData[1].first;
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000067
68 for (unsigned int inBatch = 0; inBatch < inputBatchSize; ++inBatch)
69 {
70 const unsigned int outBatch = inBatch % outputBatchSize;
71 const unsigned int spatialOffset = inBatch / outputBatchSize;
72
73 for (unsigned int inH = 0; inH < inputTensorInfo.GetShape()[dataLayout.GetHeightIndex()]; ++inH) {
74 const unsigned int outH = inH * blockShapeHeight + spatialOffset / blockShapeWidth - cropsTop;
75
76 if (outH >= outputHeight)
77 {
78 continue;
79 }
80
81 for (unsigned int inW = 0; inW < inputTensorInfo.GetShape()[dataLayout.GetWidthIndex()]; ++inW) {
82 const unsigned int outW = inW * blockShapeWidth + spatialOffset % blockShapeWidth - cropsLeft;
83
84 if (outW >= outputWidth)
85 {
86 continue;
87 }
88
89 for (unsigned int c = 0; c < channels; c++)
90 {
91 unsigned int outOffset = Offset(outputShape, outBatch, outH, outW, c, dataLayout);
92 unsigned int inOffset = Offset(inputShape, inBatch, inH, inW, c, dataLayout);
Francis Murtagh47ea3c02019-06-20 12:07:19 +010093
94 outputEncoder[outOffset];
95 inputDecoder[inOffset];
96 outputEncoder.Set(inputDecoder.Get());
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000097 }
98 }
99 }
100 }
101}
102
103} //namespace armnn