blob: c3f022c6a6db36378330dc5b4e92349c27d1983b [file] [log] [blame]
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +00001//
Teresa Charlinf77cab52023-06-01 16:15:13 +01002// Copyright © 2017-2019,2023 Arm Ltd and Contributors. All rights reserved.
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +00003// SPDX-License-Identifier: MIT
4//
5
6#include "SpaceToBatchNd.hpp"
7
Matteo Martincighe011d202019-11-28 11:35:47 +00008#include <armnnUtils/DataLayoutIndexed.hpp>
Matteo Martincigh21350152018-11-28 16:22:22 +00009
10using namespace armnnUtils;
Matthew Bentham8800c002018-11-19 13:19:28 +000011
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000012namespace armnn
13{
14
15unsigned int GetOffset(const TensorShape& shape,
16 unsigned int b,
17 unsigned int h,
18 unsigned int w,
19 unsigned int c,
20 const DataLayoutIndexed& dataLayout)
21{
Teresa Charlinf77cab52023-06-01 16:15:13 +010022 // 3D Tensors
23 unsigned int channelDimension3D = dataLayout.GetDataLayout() == DataLayout::NCHW ? 1 : 2;
24 if (shape.GetNumDimensions() == 3)
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000025 {
Teresa Charlinf77cab52023-06-01 16:15:13 +010026 return (b * shape[dataLayout.GetHeightIndex()] + h) * shape[channelDimension3D] + c;
27 }
28 // 4D Tensors
29 else if (shape.GetNumDimensions() == 4)
30 {
31 if (dataLayout.GetDataLayout() == DataLayout::NHWC)
32 {
33 return ((b * shape[dataLayout.GetHeightIndex()] + h) * shape[dataLayout.GetWidthIndex()] + w) *
34 shape[dataLayout.GetChannelsIndex()] + c;
35 }
36 else
37 {
38 return ((b * shape[dataLayout.GetChannelsIndex()] + c) * shape[dataLayout.GetHeightIndex()] + h) *
39 shape[dataLayout.GetWidthIndex()] + w;
40 }
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000041 }
42 else
43 {
Teresa Charlinf77cab52023-06-01 16:15:13 +010044 throw InvalidArgumentException("Tensor rank must be either 3 or 4", CHECK_LOCATION());
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000045 }
46}
47
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000048void SpaceToBatchNd(const TensorInfo& inputInfo,
49 const TensorInfo& outputInfo,
50 const SpaceToBatchNdDescriptor& params,
nikraj0122f0f2b2019-05-30 17:29:32 +010051 Decoder<float>& inputData,
52 Encoder<float>& outputData)
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000053{
Teresa Charlinf77cab52023-06-01 16:15:13 +010054 unsigned int rank = inputInfo.GetNumDimensions();
55 if (rank != 3 && rank != 4 )
56 {
57 throw InvalidArgumentException("Tensor rank must be either 3 or 4, but it is " + std::to_string(rank),
58 CHECK_LOCATION());
59 }
60
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000061 DataLayoutIndexed dataLayout = params.m_DataLayout;
Teresa Charlinf77cab52023-06-01 16:15:13 +010062 unsigned int channelDimension3D = params.m_DataLayout == DataLayout::NCHW ? 1 : 2;
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000063
64 const TensorShape& inputShape = inputInfo.GetShape();
65 const TensorShape& outputShape = outputInfo.GetShape();
66
Teresa Charlinf77cab52023-06-01 16:15:13 +010067 const unsigned int inputBatchSize = inputShape[0];
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000068 const unsigned int outputBatchSize = outputShape[0];
Teresa Charlinf77cab52023-06-01 16:15:13 +010069
70 const unsigned int channels = (rank == 3) ? inputShape[channelDimension3D]
71 : inputShape[dataLayout.GetChannelsIndex()];
72
73 const unsigned int inputHeight = inputShape[dataLayout.GetHeightIndex()];
74 const unsigned int inputWidth = (rank == 3) ? 1 : inputShape[dataLayout.GetWidthIndex()];
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000075 const unsigned int outputHeight = outputShape[dataLayout.GetHeightIndex()];
Teresa Charlinf77cab52023-06-01 16:15:13 +010076 const unsigned int outputWidth = (rank == 3) ? 1 : outputShape[dataLayout.GetWidthIndex()];
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000077
78 const unsigned int blockHeight = params.m_BlockShape[0];
Teresa Charlinf77cab52023-06-01 16:15:13 +010079 const unsigned int blockWidth = (rank == 3) ? 1 : params.m_BlockShape[1];
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000080
Teresa Charlinf77cab52023-06-01 16:15:13 +010081 const unsigned int paddingTop = params.m_PadList[0].first;
82 const unsigned int paddingLeft = (rank == 3) ? 0 : params.m_PadList[1].first;
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000083
Teresa Charlinf77cab52023-06-01 16:15:13 +010084 for (unsigned int outB = 0; outB < outputBatchSize; ++outB)
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000085 {
86 unsigned int inB = outB % inputBatchSize;
87
88 unsigned int shiftW = (outB / inputBatchSize) % blockWidth;
89 unsigned int shiftH = (outB / inputBatchSize) / blockWidth;
90
Teresa Charlinf77cab52023-06-01 16:15:13 +010091 for (unsigned int outH = 0; outH < outputHeight; ++outH)
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000092 {
Teresa Charlinf77cab52023-06-01 16:15:13 +010093 for (unsigned int outW = 0; outW < outputWidth; ++outW)
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +000094 {
95 if (outH * blockHeight + shiftH < paddingTop ||
96 outH * blockHeight + shiftH >= paddingTop + inputHeight ||
97 outW * blockWidth + shiftW < paddingLeft ||
98 outW * blockWidth + shiftW >= paddingLeft + inputWidth)
99 {
100 for (unsigned int c = 0; c < channels; c++)
101 {
102 unsigned int outOffset = GetOffset(outputShape,
103 outB,
104 outH,
105 outW,
106 c,
107 dataLayout);
nikraj0122f0f2b2019-05-30 17:29:32 +0100108 outputData += outOffset;
109 outputData.Set(0);
110 outputData -= outOffset;
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +0000111 }
112 }
113 else
114 {
115 for (unsigned int c = 0; c < channels; c++)
116 {
117 unsigned int inOffset = GetOffset(inputShape,
118 inB,
119 (outH * blockHeight + shiftH) - paddingTop,
120 (outW * blockWidth + shiftW) - paddingLeft,
121 c,
122 dataLayout);
123
124 unsigned int outOffset = GetOffset(outputShape,
125 outB,
126 outH,
127 outW,
128 c,
129 dataLayout);
130
nikraj0122f0f2b2019-05-30 17:29:32 +0100131 outputData += outOffset;
132 inputData += inOffset;
133 outputData.Set(inputData.Get());
134 inputData -= inOffset;
135 outputData -= outOffset;
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +0000136 }
137 }
138 }
139 }
140 }
141}
142
Nattapat Chaimanowong3ea76d52018-11-09 14:10:38 +0000143} //namespace armnn