Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 1 | // |
| 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 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | inline 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 | |
| 33 | void BatchToSpaceNd(const DataLayoutIndexed& dataLayout, |
| 34 | const TensorInfo& inputTensorInfo, |
| 35 | const TensorInfo& outputTensorInfo, |
| 36 | const std::vector<unsigned int>& blockShape, |
Éanna Ó Catháin | 95807ce | 2018-11-12 17:14:43 +0000 | [diff] [blame] | 37 | const std::vector<std::pair<unsigned int, unsigned int>>& cropsData, |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 38 | const float* inputData, |
| 39 | float* outputData) |
| 40 | { |
| 41 | TensorShape inputShape = inputTensorInfo.GetShape(); |
Éanna Ó Catháin | 95807ce | 2018-11-12 17:14:43 +0000 | [diff] [blame] | 42 | |
| 43 | BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Expected Input with 4 Dimensions"); |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 44 | |
| 45 | TensorShape outputShape = outputTensorInfo.GetShape(); |
Éanna Ó Catháin | 95807ce | 2018-11-12 17:14:43 +0000 | [diff] [blame] | 46 | |
| 47 | BOOST_ASSERT_MSG(outputShape.GetNumDimensions() == 4, "Expected Output with 4 Dimensions"); |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 48 | |
| 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áin | 95807ce | 2018-11-12 17:14:43 +0000 | [diff] [blame] | 56 | BOOST_ASSERT_MSG(blockShape.size() > 0, "BlockShape must contain 1 or more entries"); |
| 57 | |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 58 | const unsigned int blockShapeHeight = blockShape[0]; |
| 59 | const unsigned int blockShapeWidth = blockShape[1]; |
| 60 | |
Éanna Ó Catháin | 95807ce | 2018-11-12 17:14:43 +0000 | [diff] [blame] | 61 | 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áin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 65 | |
| 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 |