Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "OutputShapeUtils.hpp" |
| 7 | |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame] | 8 | #include <DataLayoutIndexed.hpp> |
| 9 | |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 10 | #include <algorithm> |
Sadik Armagan | 310d8ff | 2019-07-11 10:53:38 +0100 | [diff] [blame] | 11 | #include <vector> |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 12 | |
Sadik Armagan | 5e9521c | 2019-07-12 13:55:57 +0100 | [diff] [blame] | 13 | namespace |
| 14 | { |
| 15 | |
| 16 | using namespace armnn; |
| 17 | |
| 18 | TensorShape CalculateMaxShape(const TensorShape& inShape0, const TensorShape& inShape1) |
| 19 | { |
| 20 | // NOTE: The inferred output size will be the maximum size along each dimension |
| 21 | // of inShape0 and inShape1, starting with the trailing dimensions, and working its way forward. |
| 22 | // |
| 23 | // Example: inShape0={4, 1, 2}, inShape1={5, 4, 3, 1} => outputShape={5, 4, 3, 2} |
| 24 | |
| 25 | const unsigned int numInput0Dims = inShape0.GetNumDimensions(); |
| 26 | const unsigned int numInput1Dims = inShape1.GetNumDimensions(); |
| 27 | |
| 28 | const unsigned int maxNumDims = std::max(numInput0Dims, numInput1Dims); |
| 29 | |
| 30 | TensorShape outputShape = TensorShape(maxNumDims); |
| 31 | for (unsigned int reverseIdx = 1u; reverseIdx <= maxNumDims; ++reverseIdx) |
| 32 | { |
| 33 | const int input0Idx = numInput0Dims - reverseIdx; |
| 34 | const int input1Idx = numInput1Dims - reverseIdx; |
| 35 | |
| 36 | const unsigned int input0DimSize = input0Idx >= 0 ? inShape0[input0Idx] : 0u; |
| 37 | const unsigned int input1DimSize = input1Idx >= 0 ? inShape1[input1Idx] : 0u; |
| 38 | |
| 39 | const unsigned int outputIdx = maxNumDims - reverseIdx; |
| 40 | outputShape[outputIdx] = std::max(input0DimSize, input1DimSize); |
| 41 | } |
| 42 | |
| 43 | return outputShape; |
| 44 | } |
| 45 | |
Aron Virginas-Tar | 9fd3739 | 2019-07-15 18:04:32 +0100 | [diff] [blame] | 46 | template<typename ConvolutionDescriptor> |
| 47 | TensorShape InferConvolution2dOutputShapeImpl(const TensorShape& inputShape, |
| 48 | const TensorShape& kernelShape, |
| 49 | const ConvolutionDescriptor& descriptor, |
| 50 | bool isDepthwiseConvolution) |
| 51 | { |
| 52 | if (inputShape.GetNumDimensions() != 4) |
| 53 | { |
| 54 | throw InvalidArgumentException("Input shape must be 4D"); |
| 55 | } |
Sadik Armagan | 5e9521c | 2019-07-12 13:55:57 +0100 | [diff] [blame] | 56 | |
Aron Virginas-Tar | 9fd3739 | 2019-07-15 18:04:32 +0100 | [diff] [blame] | 57 | armnnUtils::DataLayoutIndexed dataLayoutIndex(descriptor.m_DataLayout); |
| 58 | |
| 59 | const unsigned int cIndex = dataLayoutIndex.GetChannelsIndex(); |
| 60 | const unsigned int wIndex = dataLayoutIndex.GetWidthIndex(); |
| 61 | const unsigned int hIndex = dataLayoutIndex.GetHeightIndex(); |
| 62 | |
| 63 | const unsigned int wInput = inputShape[wIndex]; |
| 64 | const unsigned int hInput = inputShape[hIndex]; |
| 65 | |
| 66 | const unsigned int wKernel = isDepthwiseConvolution ? kernelShape[2] : kernelShape[wIndex]; |
| 67 | const unsigned int wDilated = wKernel + (descriptor.m_DilationX - 1) * (wKernel - 1); |
| 68 | |
| 69 | const unsigned int wRead = (wInput + descriptor.m_PadLeft + descriptor.m_PadRight) - wDilated; |
| 70 | const unsigned int wOutput = 1 + (wRead / descriptor.m_StrideX); |
| 71 | |
| 72 | const unsigned int hKernel = isDepthwiseConvolution ? kernelShape[3] : kernelShape[hIndex]; |
| 73 | const unsigned int hDilated = hKernel + (descriptor.m_DilationY - 1) * (hKernel - 1); |
| 74 | |
| 75 | const unsigned int hRead = (hInput + descriptor.m_PadTop + descriptor.m_PadBottom) - hDilated; |
| 76 | const unsigned int hOutput = 1 + (hRead / descriptor.m_StrideY); |
| 77 | |
| 78 | TensorShape outputShape(4); |
| 79 | outputShape[0] = inputShape[0]; |
| 80 | outputShape[cIndex] = kernelShape[0]; |
| 81 | outputShape[wIndex] = wOutput; |
| 82 | outputShape[hIndex] = hOutput; |
| 83 | |
| 84 | if (isDepthwiseConvolution) |
| 85 | { |
| 86 | outputShape[cIndex] *= inputShape[cIndex]; |
| 87 | } |
| 88 | |
| 89 | return outputShape; |
| 90 | } |
| 91 | |
| 92 | } // anonymous namespace |
Sadik Armagan | 5e9521c | 2019-07-12 13:55:57 +0100 | [diff] [blame] | 93 | |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 94 | namespace armnn_driver |
| 95 | { |
| 96 | |
| 97 | using namespace armnn; |
| 98 | |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 99 | bool IsDynamicOutput(const TensorInfo& outputInfo) |
| 100 | { |
| 101 | return outputInfo.GetNumElements() == 0u; |
| 102 | } |
| 103 | |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame] | 104 | TensorShape InferConvolution2dOutputShape(const TensorShape& inputShape, |
| 105 | const TensorShape& kernelShape, |
| 106 | const Convolution2dDescriptor& descriptor) |
| 107 | { |
Aron Virginas-Tar | 9fd3739 | 2019-07-15 18:04:32 +0100 | [diff] [blame] | 108 | return InferConvolution2dOutputShapeImpl(inputShape, kernelShape, descriptor, false); |
| 109 | } |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame] | 110 | |
Aron Virginas-Tar | 9fd3739 | 2019-07-15 18:04:32 +0100 | [diff] [blame] | 111 | TensorShape InferDepthwiseConvolution2dOutputShape(const TensorShape& inputShape, |
| 112 | const TensorShape& kernelShape, |
| 113 | const DepthwiseConvolution2dDescriptor& descriptor) |
| 114 | { |
| 115 | return InferConvolution2dOutputShapeImpl(inputShape, kernelShape, descriptor, true); |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 118 | TensorShape InferMaximumOutputShape(const armnn::TensorShape& input0Shape, |
| 119 | const armnn::TensorShape& input1Shape) |
| 120 | { |
| 121 | return CalculateMaxShape(input0Shape, input1Shape); |
| 122 | } |
| 123 | |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 124 | TensorShape InferMinimumOutputShape(const armnn::TensorShape& input0Shape, |
| 125 | const armnn::TensorShape& input1Shape) |
| 126 | { |
| 127 | return CalculateMaxShape(input0Shape, input1Shape); |
| 128 | } |
| 129 | |
Sadik Armagan | 310d8ff | 2019-07-11 10:53:38 +0100 | [diff] [blame] | 130 | TensorShape InferPadOutputShape(const TensorShape& inputShape, |
| 131 | const std::vector<std::pair<unsigned int, unsigned int>>& padList) |
| 132 | { |
| 133 | const unsigned int numDims = inputShape.GetNumDimensions(); |
| 134 | |
| 135 | std::vector<unsigned int> outputDims; |
| 136 | TensorShape outputShape = TensorShape(numDims); |
| 137 | for (unsigned int dim = 0; dim < numDims; ++dim) |
| 138 | { |
| 139 | unsigned int dimSize = inputShape[dim]; |
| 140 | const std::pair<unsigned int, unsigned int>& dimPadding = padList[dim]; |
| 141 | dimSize += dimPadding.first; |
| 142 | dimSize += dimPadding.second; |
| 143 | outputShape[dim] = dimSize; |
| 144 | } |
| 145 | return outputShape; |
| 146 | } |
| 147 | |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 148 | TensorShape InferPreluOutputShape(const TensorShape& inputShape, const TensorShape& alphaShape) |
| 149 | { |
Sadik Armagan | 5e9521c | 2019-07-12 13:55:57 +0100 | [diff] [blame] | 150 | return CalculateMaxShape(inputShape, alphaShape); |
| 151 | } |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 152 | |
Aron Virginas-Tar | be5d356 | 2019-07-16 11:32:29 +0100 | [diff] [blame] | 153 | TensorShape InferResizeOutputShape(const TensorShape& inputShape, const ResizeDescriptor& descriptor) |
| 154 | { |
| 155 | if (inputShape.GetNumDimensions() != 4) |
| 156 | { |
| 157 | throw InvalidArgumentException("Input shape for Resize must be 4D"); |
| 158 | } |
| 159 | |
| 160 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout); |
| 161 | |
| 162 | const unsigned int cIndex = dataLayoutIndexed.GetChannelsIndex(); |
| 163 | const unsigned int wIndex = dataLayoutIndexed.GetWidthIndex(); |
| 164 | const unsigned int hIndex = dataLayoutIndexed.GetHeightIndex(); |
| 165 | |
| 166 | TensorShape outputShape(4); |
| 167 | outputShape[0] = inputShape[0]; |
| 168 | outputShape[cIndex] = inputShape[cIndex]; |
| 169 | outputShape[wIndex] = descriptor.m_TargetWidth; |
| 170 | outputShape[hIndex] = descriptor.m_TargetHeight; |
| 171 | |
| 172 | return outputShape; |
| 173 | } |
| 174 | |
Sadik Armagan | 5e9521c | 2019-07-12 13:55:57 +0100 | [diff] [blame] | 175 | TensorShape InferSubOutputShape(const TensorShape& input0Shape, const TensorShape& input1Shape) |
| 176 | { |
| 177 | return CalculateMaxShape(input0Shape, input1Shape); |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } // namespace armnn_driver |