Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "TransposeConvolution2d.hpp" |
| 7 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 8 | #include <armnnUtils/DataLayoutIndexed.hpp> |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 9 | |
| 10 | namespace armnn |
| 11 | { |
| 12 | |
| 13 | using namespace armnnUtils; |
| 14 | |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 15 | void TransposeConvolution2dImpl(const TransposeConvolution2dDescriptor& descriptor, |
| 16 | const TensorShape& inputShape, |
| 17 | Decoder<float>& inputDecoder, |
| 18 | const TensorShape& outputShape, |
| 19 | Encoder<float>& outputEncoder, |
| 20 | const TensorShape& weightsShape, |
| 21 | Decoder<float>& weightsDecoder, |
| 22 | Decoder<float>* biasesDecoder) |
| 23 | { |
| 24 | if (descriptor.m_BiasEnabled && !biasesDecoder) |
| 25 | { |
| 26 | throw InvalidArgumentException("Biases enabled but no bias data provided"); |
| 27 | } |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 28 | const DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout); |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 29 | const unsigned int channelsIndex = dataLayoutIndexed.GetChannelsIndex(); |
| 30 | const unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex(); |
| 31 | const unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex(); |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 32 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 33 | unsigned int numBatches = inputShape[0]; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 34 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 35 | unsigned int inputWidth = inputShape[widthIndex]; |
| 36 | unsigned int inputHeight = inputShape[heightIndex]; |
| 37 | unsigned int inputDepth = inputShape[channelsIndex]; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 38 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 39 | unsigned int weightsHeight = weightsShape[heightIndex]; |
| 40 | unsigned int weightsWidth = weightsShape[widthIndex]; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 41 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 42 | unsigned int outputHeight = outputShape[heightIndex]; |
| 43 | unsigned int outputWidth = outputShape[widthIndex]; |
| 44 | unsigned int outputDepth = outputShape[channelsIndex]; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 45 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 46 | unsigned int paddingLeft = descriptor.m_PadLeft; |
| 47 | unsigned int paddingTop = descriptor.m_PadTop; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 48 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 49 | unsigned int strideX = descriptor.m_StrideX; |
| 50 | unsigned int strideY = descriptor.m_StrideY; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 51 | |
Mike Kelly | 675fa4f | 2019-08-21 09:36:58 +0100 | [diff] [blame] | 52 | std::vector<float> outputBuffer(outputShape.GetNumElements(), 0); |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 53 | |
| 54 | for (unsigned int batch = 0u; batch < numBatches; ++batch) |
| 55 | { |
| 56 | for (unsigned int yInput = 0u; yInput < inputHeight; ++yInput) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 57 | { |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 58 | for (unsigned int xInput = 0u; xInput < inputWidth; ++xInput) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 59 | { |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 60 | unsigned int xOutputOrigin = xInput * strideX - paddingLeft; |
| 61 | unsigned int yOutputOrigin = yInput * strideY - paddingTop; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 62 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 63 | for (unsigned int dOutput = 0u; dOutput < outputDepth; ++dOutput) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 64 | { |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 65 | for (unsigned int yWeights = 0u; yWeights < weightsHeight; ++yWeights) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 66 | { |
Aron Virginas-Tar | 0558ca4 | 2019-08-14 11:39:50 +0100 | [diff] [blame] | 67 | for (unsigned int xWeights = 0u; xWeights < weightsWidth; ++xWeights) |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 68 | { |
| 69 | unsigned int yOutput = yOutputOrigin + yWeights; |
| 70 | unsigned int xOutput = xOutputOrigin + xWeights; |
| 71 | |
| 72 | if (yOutput < outputHeight && xOutput< outputWidth) |
| 73 | { |
| 74 | for (unsigned int dInput = 0u; dInput < inputDepth; dInput++) |
| 75 | { |
| 76 | const unsigned int inputIndex = |
| 77 | dataLayoutIndexed.GetIndex(inputShape, batch, dInput, yInput, xInput); |
| 78 | inputDecoder[inputIndex]; |
| 79 | |
| 80 | const unsigned int weightsIndex = |
Aron Virginas-Tar | aec942c | 2019-08-14 14:37:42 +0100 | [diff] [blame] | 81 | dataLayoutIndexed.GetIndex(weightsShape, dOutput, dInput, yWeights, xWeights); |
Aron Virginas-Tar | 94d3b93 | 2019-11-11 12:54:47 +0000 | [diff] [blame] | 82 | weightsDecoder.SetIndex(weightsIndex, dOutput); |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 83 | |
| 84 | const unsigned int outputIndex = |
| 85 | dataLayoutIndexed.GetIndex(outputShape, batch, dOutput, yOutput, xOutput); |
| 86 | outputEncoder[outputIndex]; |
| 87 | |
Mike Kelly | 675fa4f | 2019-08-21 09:36:58 +0100 | [diff] [blame] | 88 | float output = outputBuffer[outputIndex]; |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 89 | output += inputDecoder.Get() * weightsDecoder.Get(); |
Mike Kelly | 675fa4f | 2019-08-21 09:36:58 +0100 | [diff] [blame] | 90 | outputBuffer[outputIndex] = output; |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Apply bias (if enabled) |
| 101 | if (descriptor.m_BiasEnabled) |
| 102 | { |
| 103 | outputEncoder[0]; |
| 104 | Decoder<float>& rBiasesDecoder = *biasesDecoder; |
| 105 | |
| 106 | for (unsigned int batch = 0u; batch < numBatches; ++batch) |
| 107 | { |
| 108 | for (unsigned int dOutput = 0u; dOutput < outputDepth; ++dOutput) |
| 109 | { |
Aron Virginas-Tar | 94d3b93 | 2019-11-11 12:54:47 +0000 | [diff] [blame] | 110 | rBiasesDecoder.SetIndex(dOutput, dOutput); |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 111 | for (unsigned int yOutput = 0u; yOutput < outputHeight; ++yOutput) |
| 112 | { |
| 113 | for (unsigned int xOutput = 0u; xOutput < outputWidth; ++xOutput) |
| 114 | { |
| 115 | const unsigned int outputIndex = |
| 116 | dataLayoutIndexed.GetIndex(outputShape, batch, dOutput, yOutput, xOutput); |
Mike Kelly | 675fa4f | 2019-08-21 09:36:58 +0100 | [diff] [blame] | 117 | outputBuffer[outputIndex] += rBiasesDecoder.Get(); |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
Mike Kelly | 675fa4f | 2019-08-21 09:36:58 +0100 | [diff] [blame] | 123 | outputEncoder[0]; |
| 124 | for (float output : outputBuffer) |
| 125 | { |
| 126 | outputEncoder.Set(output); |
| 127 | ++outputEncoder; |
| 128 | } |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 131 | } // namespace armnn |