blob: afbf063e6350c6390c486ad62305715bda1e0264 [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Freddie Liardet9d061b02021-04-06 15:59:28 +01002 * Copyright (c) 2017-2021 Arm Limited.
Pablo Tellof5f34bb2017-08-22 13:34:13 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "ConvolutionLayer.h"
25
Pablo Tellof5f34bb2017-08-22 13:34:13 +010026#include "tests/validation/Helpers.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010027namespace arm_compute
28{
29namespace test
30{
31namespace validation
32{
33namespace reference
34{
Freddie Liardet9d061b02021-04-06 15:59:28 +010035template <typename T, typename TW, typename TB>
36SimpleTensor<T> deconvolution_layer(const SimpleTensor<T> &src, const SimpleTensor<TW> &weights, const SimpleTensor<TB> &bias, const TensorShape &output_shape,
Manuel Bottini279814b2019-10-25 10:28:28 +010037 const PadStrideInfo &info, QuantizationInfo out_qinfo)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010038{
39 // Create reference
Sheri Zhanga14817a2020-02-26 10:30:15 +000040 const unsigned int pad_left = info.pad_left();
41 const unsigned int pad_right = info.pad_right();
42 const unsigned int pad_top = info.pad_top();
43 const unsigned int pad_bottom = info.pad_bottom();
44 const int stride_x = info.stride().first;
45 const int stride_y = info.stride().second;
46 const int weights_width = weights.shape().x();
47 const int weights_height = weights.shape().y();
48 const int weights_upper_dims = weights.shape().total_size() / (weights_width * weights_height);
Matthew Jacksonb9070a42019-08-22 16:13:27 +010049
Sheri Zhanga14817a2020-02-26 10:30:15 +000050 ARM_COMPUTE_ERROR_ON(pad_left > (weights.shape().x() - 1));
51 ARM_COMPUTE_ERROR_ON(pad_right > (weights.shape().x() - 1));
52 ARM_COMPUTE_ERROR_ON(pad_top > (weights.shape().y() - 1));
Matthew Jacksonb9070a42019-08-22 16:13:27 +010053 ARM_COMPUTE_ERROR_ON(pad_bottom > (weights.shape().y() - 1));
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010054
55 // Find the upsampled dimensions
giuros01a69a88b2019-01-31 16:29:19 +000056 unsigned int out_x = (src.shape().x() - 1) * stride_x + 1;
57 unsigned int out_y = (src.shape().y() - 1) * stride_y + 1;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010058
59 // Find the padding needed for the convolution with stride 1 in order to match output shape
Matthew Jacksonb9070a42019-08-22 16:13:27 +010060 unsigned int deconv_pad_x = output_shape.x() - (out_x - weights_width + 1);
61 unsigned int deconv_pad_y = output_shape.y() - (out_y - weights_height + 1);
62 out_x += deconv_pad_x;
63 out_y += deconv_pad_y;
64
65 unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
66 unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
67 deconv_pad_x -= deconv_pad_left + deconv_pad_right;
68 ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
Sheri Zhanga14817a2020-02-26 10:30:15 +000069 deconv_pad_left += deconv_pad_x / 2;
Matthew Jacksonb9070a42019-08-22 16:13:27 +010070 deconv_pad_right += deconv_pad_x / 2;
71
72 unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
73 unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
74 deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
75 ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
Sheri Zhanga14817a2020-02-26 10:30:15 +000076 deconv_pad_top += deconv_pad_y / 2;
Matthew Jacksonb9070a42019-08-22 16:13:27 +010077 deconv_pad_bottom += deconv_pad_y / 2;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010078
Pablo Tellof5f34bb2017-08-22 13:34:13 +010079 TensorShape scaled_shape = src.shape();
Michalis Spyrou780db4e2017-11-23 09:49:51 +000080 scaled_shape.set(0, out_x);
81 scaled_shape.set(1, out_y);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010082 SimpleTensor<T> scaled{ scaled_shape, src.data_type(), 1, src.quantization_info() };
Pablo Tellof5f34bb2017-08-22 13:34:13 +010083
Michalis Spyrou780db4e2017-11-23 09:49:51 +000084 const int width_in = src.shape().x();
85 const int height_in = src.shape().y();
86 const int width_scaled = scaled.shape().x();
87 const int height_scaled = scaled.shape().y();
88 const int num_2d_slices = src.shape().total_size() / (width_in * height_in);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000089
Sheri Zhanga14817a2020-02-26 10:30:15 +000090 if(src.data_type() == DataType::QASYMM8 || src.data_type() == DataType::QASYMM8_SIGNED)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010091 {
Sheri Zhanga14817a2020-02-26 10:30:15 +000092 const auto quantized_zero = static_cast<T>(src.quantization_info().uniform().offset);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010093 std::fill_n(scaled.data(), scaled.num_elements(), quantized_zero);
94 }
95 else
96 {
97 std::fill_n(scaled.data(), scaled.num_elements(), T(0));
Pablo Tellof5f34bb2017-08-22 13:34:13 +010098 }
99
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100100 // Flip weights by 180 degrees
Freddie Liardet9d061b02021-04-06 15:59:28 +0100101 SimpleTensor<TW> weights_flipped{ weights.shape(), weights.data_type(), 1, weights.quantization_info(), weights.data_layout() };
Michalis Spyroud1d77222020-04-08 14:10:15 +0100102#if defined(_OPENMP)
103 #pragma omp parallel for
104#endif /* _OPENMP */
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100105 for(int ud = 0; ud < weights_upper_dims; ++ud)
106 {
107 const int offset = ud * weights_width * weights_height;
108 for(int y = 0; y < weights_height; ++y)
109 {
110 for(int x = 0; x < weights_width; ++x)
111 {
112 weights_flipped[offset + (weights_height - 1 - y) * weights_width + (weights_width - 1 - x)] = weights[offset + y * weights_width + x];
113 }
114 }
115 }
Michalis Spyroud1d77222020-04-08 14:10:15 +0100116#if defined(_OPENMP)
117 #pragma omp parallel for
118#endif /* _OPENMP */
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100119 for(int slice = 0; slice < num_2d_slices; ++slice)
120 {
121 const int offset_slice_in = slice * width_in * height_in;
122 const int offset_slice_out = slice * width_scaled * height_scaled;
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100123 const int start_x = deconv_pad_left;
124 const int start_y = deconv_pad_top;
125 const int end_x = width_scaled - deconv_pad_right;
126 const int end_y = height_scaled - deconv_pad_bottom;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000127
128 for(int yi = start_y, in_y = 0; yi < end_y; yi += stride_y, in_y++)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100129 {
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000130 for(int xi = start_x, in_x = 0; xi < end_x; xi += stride_x, in_x++)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100131 {
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000132 const T *in = src.data() + offset_slice_in + in_y * width_in + in_x;
133 T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled;
134 *out = *in;
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100135 }
136 }
137 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000138
139 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
Manuel Bottini279814b2019-10-25 10:28:28 +0100140 return convolution_layer(scaled, weights_flipped, bias, output_shape, conv_info, Size2D(1U, 1U), 1, out_qinfo);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100141}
142
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100143template SimpleTensor<uint8_t> deconvolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
Manuel Bottini279814b2019-10-25 10:28:28 +0100144 const PadStrideInfo &info, QuantizationInfo out_quant_info);
Freddie Liardet9d061b02021-04-06 15:59:28 +0100145template SimpleTensor<uint8_t> deconvolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
146 const PadStrideInfo &info, QuantizationInfo out_quant_info);
Sheri Zhanga14817a2020-02-26 10:30:15 +0000147template SimpleTensor<int8_t> deconvolution_layer(const SimpleTensor<int8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
148 const PadStrideInfo &info, QuantizationInfo out_quant_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100149template SimpleTensor<float> deconvolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
Manuel Bottini279814b2019-10-25 10:28:28 +0100150 const PadStrideInfo &info, QuantizationInfo out_quant_info);
Georgios Pinitas793f87d2018-05-18 20:08:58 +0100151template SimpleTensor<half> deconvolution_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &output_shape,
Manuel Bottini279814b2019-10-25 10:28:28 +0100152 const PadStrideInfo &info, QuantizationInfo out_quant_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100153} // namespace reference
154} // namespace validation
155} // namespace test
Manuel Bottini6e10aa32020-04-30 13:28:23 +0100156} // namespace arm_compute