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 | |
| 8 | #include <DataLayoutIndexed.hpp> |
| 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 | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 52 | // Set the initial output values to be logically 0 otherwise the algorithm doesn't work. |
| 53 | for (unsigned int i = 0u; i < outputShape.GetNumElements(); ++i) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 54 | { |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 55 | outputEncoder.Set(0.f); |
| 56 | ++outputEncoder; |
| 57 | } |
| 58 | |
| 59 | for (unsigned int batch = 0u; batch < numBatches; ++batch) |
| 60 | { |
| 61 | for (unsigned int yInput = 0u; yInput < inputHeight; ++yInput) |
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 xInput = 0u; xInput < inputWidth; ++xInput) |
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 | unsigned int xOutputOrigin = xInput * strideX - paddingLeft; |
| 66 | unsigned int yOutputOrigin = yInput * strideY - paddingTop; |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 67 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 68 | for (unsigned int dOutput = 0u; dOutput < outputDepth; ++dOutput) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 69 | { |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 70 | for (unsigned int yWeights = 0u; yWeights < weightsHeight; ++yWeights) |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 71 | { |
Aron Virginas-Tar | 0558ca4 | 2019-08-14 11:39:50 +0100 | [diff] [blame] | 72 | for (unsigned int xWeights = 0u; xWeights < weightsWidth; ++xWeights) |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 73 | { |
| 74 | unsigned int yOutput = yOutputOrigin + yWeights; |
| 75 | unsigned int xOutput = xOutputOrigin + xWeights; |
| 76 | |
| 77 | if (yOutput < outputHeight && xOutput< outputWidth) |
| 78 | { |
| 79 | for (unsigned int dInput = 0u; dInput < inputDepth; dInput++) |
| 80 | { |
| 81 | const unsigned int inputIndex = |
| 82 | dataLayoutIndexed.GetIndex(inputShape, batch, dInput, yInput, xInput); |
| 83 | inputDecoder[inputIndex]; |
| 84 | |
| 85 | const unsigned int weightsIndex = |
Aron Virginas-Tar | aec942c | 2019-08-14 14:37:42 +0100 | [diff] [blame^] | 86 | dataLayoutIndexed.GetIndex(weightsShape, dOutput, dInput, yWeights, xWeights); |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 87 | weightsDecoder[weightsIndex]; |
| 88 | |
| 89 | const unsigned int outputIndex = |
| 90 | dataLayoutIndexed.GetIndex(outputShape, batch, dOutput, yOutput, xOutput); |
| 91 | outputEncoder[outputIndex]; |
| 92 | |
| 93 | float output = outputEncoder.Get(); |
| 94 | output += inputDecoder.Get() * weightsDecoder.Get(); |
| 95 | |
| 96 | outputEncoder.Set(output); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Apply bias (if enabled) |
| 107 | if (descriptor.m_BiasEnabled) |
| 108 | { |
| 109 | outputEncoder[0]; |
| 110 | Decoder<float>& rBiasesDecoder = *biasesDecoder; |
| 111 | |
| 112 | for (unsigned int batch = 0u; batch < numBatches; ++batch) |
| 113 | { |
| 114 | for (unsigned int dOutput = 0u; dOutput < outputDepth; ++dOutput) |
| 115 | { |
| 116 | rBiasesDecoder[dOutput]; |
| 117 | for (unsigned int yOutput = 0u; yOutput < outputHeight; ++yOutput) |
| 118 | { |
| 119 | for (unsigned int xOutput = 0u; xOutput < outputWidth; ++xOutput) |
| 120 | { |
| 121 | const unsigned int outputIndex = |
| 122 | dataLayoutIndexed.GetIndex(outputShape, batch, dOutput, yOutput, xOutput); |
| 123 | |
| 124 | outputEncoder[outputIndex]; |
| 125 | outputEncoder.Set(outputEncoder.Get() + rBiasesDecoder.Get()); |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Mike Kelly | a24d9c7 | 2019-08-13 10:06:25 +0100 | [diff] [blame] | 133 | } // namespace armnn |