1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "ConvolutionLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "tests/validation/Helpers.h"
27*c217d954SCole Faust
28*c217d954SCole Faust namespace arm_compute
29*c217d954SCole Faust {
30*c217d954SCole Faust namespace test
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace validation
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace reference
35*c217d954SCole Faust {
36*c217d954SCole Faust template <typename T, typename TW, typename TB>
deconvolution_layer(const SimpleTensor<T> & src,const SimpleTensor<TW> & weights,const SimpleTensor<TB> & bias,const TensorShape & output_shape,const PadStrideInfo & info,QuantizationInfo out_qinfo)37*c217d954SCole Faust SimpleTensor<T> deconvolution_layer(const SimpleTensor<T> &src, const SimpleTensor<TW> &weights, const SimpleTensor<TB> &bias, const TensorShape &output_shape,
38*c217d954SCole Faust const PadStrideInfo &info, QuantizationInfo out_qinfo)
39*c217d954SCole Faust {
40*c217d954SCole Faust // Create reference
41*c217d954SCole Faust const unsigned int pad_left = info.pad_left();
42*c217d954SCole Faust const unsigned int pad_right = info.pad_right();
43*c217d954SCole Faust const unsigned int pad_top = info.pad_top();
44*c217d954SCole Faust const unsigned int pad_bottom = info.pad_bottom();
45*c217d954SCole Faust const int stride_x = info.stride().first;
46*c217d954SCole Faust const int stride_y = info.stride().second;
47*c217d954SCole Faust const int weights_width = weights.shape().x();
48*c217d954SCole Faust const int weights_height = weights.shape().y();
49*c217d954SCole Faust const int weights_upper_dims = weights.shape().total_size() / (weights_width * weights_height);
50*c217d954SCole Faust
51*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(pad_left > (weights.shape().x() - 1));
52*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(pad_right > (weights.shape().x() - 1));
53*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(pad_top > (weights.shape().y() - 1));
54*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(pad_bottom > (weights.shape().y() - 1));
55*c217d954SCole Faust
56*c217d954SCole Faust // Find the upsampled dimensions
57*c217d954SCole Faust unsigned int out_x = (src.shape().x() - 1) * stride_x + 1;
58*c217d954SCole Faust unsigned int out_y = (src.shape().y() - 1) * stride_y + 1;
59*c217d954SCole Faust
60*c217d954SCole Faust // Find the padding needed for the convolution with stride 1 in order to match output shape
61*c217d954SCole Faust unsigned int deconv_pad_x = output_shape.x() - (out_x - weights_width + 1);
62*c217d954SCole Faust unsigned int deconv_pad_y = output_shape.y() - (out_y - weights_height + 1);
63*c217d954SCole Faust out_x += deconv_pad_x;
64*c217d954SCole Faust out_y += deconv_pad_y;
65*c217d954SCole Faust
66*c217d954SCole Faust unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
67*c217d954SCole Faust unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
68*c217d954SCole Faust deconv_pad_x -= deconv_pad_left + deconv_pad_right;
69*c217d954SCole Faust ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
70*c217d954SCole Faust deconv_pad_left += deconv_pad_x / 2;
71*c217d954SCole Faust deconv_pad_right += deconv_pad_x / 2;
72*c217d954SCole Faust
73*c217d954SCole Faust unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
74*c217d954SCole Faust unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
75*c217d954SCole Faust deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
76*c217d954SCole Faust ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
77*c217d954SCole Faust deconv_pad_top += deconv_pad_y / 2;
78*c217d954SCole Faust deconv_pad_bottom += deconv_pad_y / 2;
79*c217d954SCole Faust
80*c217d954SCole Faust TensorShape scaled_shape = src.shape();
81*c217d954SCole Faust scaled_shape.set(0, out_x);
82*c217d954SCole Faust scaled_shape.set(1, out_y);
83*c217d954SCole Faust SimpleTensor<T> scaled{ scaled_shape, src.data_type(), 1, src.quantization_info() };
84*c217d954SCole Faust
85*c217d954SCole Faust const int width_in = src.shape().x();
86*c217d954SCole Faust const int height_in = src.shape().y();
87*c217d954SCole Faust const int width_scaled = scaled.shape().x();
88*c217d954SCole Faust const int height_scaled = scaled.shape().y();
89*c217d954SCole Faust const int num_2d_slices = src.shape().total_size() / (width_in * height_in);
90*c217d954SCole Faust
91*c217d954SCole Faust if(src.data_type() == DataType::QASYMM8 || src.data_type() == DataType::QASYMM8_SIGNED)
92*c217d954SCole Faust {
93*c217d954SCole Faust const auto quantized_zero = static_cast<T>(src.quantization_info().uniform().offset);
94*c217d954SCole Faust std::fill_n(scaled.data(), scaled.num_elements(), quantized_zero);
95*c217d954SCole Faust }
96*c217d954SCole Faust else
97*c217d954SCole Faust {
98*c217d954SCole Faust std::fill_n(scaled.data(), scaled.num_elements(), T(0));
99*c217d954SCole Faust }
100*c217d954SCole Faust
101*c217d954SCole Faust // Flip weights by 180 degrees
102*c217d954SCole Faust SimpleTensor<TW> weights_flipped{ weights.shape(), weights.data_type(), 1, weights.quantization_info(), weights.data_layout() };
103*c217d954SCole Faust #if defined(_OPENMP)
104*c217d954SCole Faust #pragma omp parallel for
105*c217d954SCole Faust #endif /* _OPENMP */
106*c217d954SCole Faust for(int ud = 0; ud < weights_upper_dims; ++ud)
107*c217d954SCole Faust {
108*c217d954SCole Faust const int offset = ud * weights_width * weights_height;
109*c217d954SCole Faust for(int y = 0; y < weights_height; ++y)
110*c217d954SCole Faust {
111*c217d954SCole Faust for(int x = 0; x < weights_width; ++x)
112*c217d954SCole Faust {
113*c217d954SCole Faust weights_flipped[offset + (weights_height - 1 - y) * weights_width + (weights_width - 1 - x)] = weights[offset + y * weights_width + x];
114*c217d954SCole Faust }
115*c217d954SCole Faust }
116*c217d954SCole Faust }
117*c217d954SCole Faust #if defined(_OPENMP)
118*c217d954SCole Faust #pragma omp parallel for
119*c217d954SCole Faust #endif /* _OPENMP */
120*c217d954SCole Faust for(int slice = 0; slice < num_2d_slices; ++slice)
121*c217d954SCole Faust {
122*c217d954SCole Faust const int offset_slice_in = slice * width_in * height_in;
123*c217d954SCole Faust const int offset_slice_out = slice * width_scaled * height_scaled;
124*c217d954SCole Faust const int start_x = deconv_pad_left;
125*c217d954SCole Faust const int start_y = deconv_pad_top;
126*c217d954SCole Faust const int end_x = width_scaled - deconv_pad_right;
127*c217d954SCole Faust const int end_y = height_scaled - deconv_pad_bottom;
128*c217d954SCole Faust
129*c217d954SCole Faust for(int yi = start_y, in_y = 0; yi < end_y; yi += stride_y, in_y++)
130*c217d954SCole Faust {
131*c217d954SCole Faust for(int xi = start_x, in_x = 0; xi < end_x; xi += stride_x, in_x++)
132*c217d954SCole Faust {
133*c217d954SCole Faust const T *in = src.data() + offset_slice_in + in_y * width_in + in_x;
134*c217d954SCole Faust T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled;
135*c217d954SCole Faust *out = *in;
136*c217d954SCole Faust }
137*c217d954SCole Faust }
138*c217d954SCole Faust }
139*c217d954SCole Faust
140*c217d954SCole Faust const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
141*c217d954SCole Faust return convolution_layer(scaled, weights_flipped, bias, output_shape, conv_info, Size2D(1U, 1U), 1, out_qinfo);
142*c217d954SCole Faust }
143*c217d954SCole Faust
144*c217d954SCole Faust template SimpleTensor<uint8_t> deconvolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
145*c217d954SCole Faust const PadStrideInfo &info, QuantizationInfo out_quant_info);
146*c217d954SCole Faust template SimpleTensor<uint8_t> deconvolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
147*c217d954SCole Faust const PadStrideInfo &info, QuantizationInfo out_quant_info);
148*c217d954SCole Faust template SimpleTensor<int8_t> deconvolution_layer(const SimpleTensor<int8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
149*c217d954SCole Faust const PadStrideInfo &info, QuantizationInfo out_quant_info);
150*c217d954SCole Faust template SimpleTensor<float> deconvolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
151*c217d954SCole Faust const PadStrideInfo &info, QuantizationInfo out_quant_info);
152*c217d954SCole Faust template SimpleTensor<half> deconvolution_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &output_shape,
153*c217d954SCole Faust const PadStrideInfo &info, QuantizationInfo out_quant_info);
154*c217d954SCole Faust } // namespace reference
155*c217d954SCole Faust } // namespace validation
156*c217d954SCole Faust } // namespace test
157*c217d954SCole Faust } // namespace arm_compute