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 #ifndef ARM_COMPUTE_TEST_VALIDATION_CONVOLUTION_H
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_VALIDATION_CONVOLUTION_H
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
28*c217d954SCole Faust #include "support/Requires.h"
29*c217d954SCole Faust #include "tests/validation/Helpers.h"
30*c217d954SCole Faust #include "tests/validation/reference/UtilsQuantizedAsymm.h"
31*c217d954SCole Faust
32*c217d954SCole Faust namespace arm_compute
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace test
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace convolution_3d
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace detail
39*c217d954SCole Faust {
is_valid_pixel(int i,int min,int max)40*c217d954SCole Faust inline bool is_valid_pixel(int i, int min, int max)
41*c217d954SCole Faust {
42*c217d954SCole Faust return (i >= min && i < max);
43*c217d954SCole Faust }
44*c217d954SCole Faust
45*c217d954SCole Faust // 3D convolution for floating point type
46*c217d954SCole Faust template < typename T, typename TW, typename TB, typename std::enable_if < validation::is_floating_point<T>::value &&validation::is_floating_point<TW>::value
47*c217d954SCole Faust &&validation::is_floating_point<TB>::value,
48*c217d954SCole Faust int >::type = 0 >
49*c217d954SCole Faust inline void convolution3d(const SimpleTensor<T> &in, const SimpleTensor<TW> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &out,
50*c217d954SCole Faust int i_offset, int w_offset, int b_offset, int o_offset,
51*c217d954SCole Faust int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights, int dilation_x = 1, int dilation_y = 1, int filter_id = 0)
52*c217d954SCole Faust {
53*c217d954SCole Faust ARM_COMPUTE_UNUSED(filter_id);
54*c217d954SCole Faust const T *in_ptr = in.data() + i_offset;
55*c217d954SCole Faust const TW *w_ptr = weights.data() + w_offset;
56*c217d954SCole Faust const TB *b_ptr = bias.data() + b_offset;
57*c217d954SCole Faust T *out_ptr = out.data() + o_offset;
58*c217d954SCole Faust
59*c217d954SCole Faust const int half_width_weights_start = width_weights / 2;
60*c217d954SCole Faust const int half_width_weights_end = ((width_weights % 2) == 0) ? (half_width_weights_start - 1) : half_width_weights_start;
61*c217d954SCole Faust const int half_height_weights_start = height_weights / 2;
62*c217d954SCole Faust const int half_height_weights_end = ((height_weights % 2) == 0) ? (half_height_weights_start - 1) : half_height_weights_start;
63*c217d954SCole Faust
64*c217d954SCole Faust // Reset accumulator
65*c217d954SCole Faust T acc(0);
66*c217d954SCole Faust
67*c217d954SCole Faust // Compute a 2D convolution for each IFM and accumulate the result
68*c217d954SCole Faust for(int ifm = 0; ifm < depth_in; ++ifm)
69*c217d954SCole Faust {
70*c217d954SCole Faust // Compute the offset for the input slice
71*c217d954SCole Faust const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
72*c217d954SCole Faust
73*c217d954SCole Faust // Compute 2D convolution
74*c217d954SCole Faust for(int yk = -half_height_weights_start; yk <= half_height_weights_end; ++yk)
75*c217d954SCole Faust {
76*c217d954SCole Faust for(int xk = -half_width_weights_start; xk <= half_width_weights_end; ++xk)
77*c217d954SCole Faust {
78*c217d954SCole Faust // Check if the pixel is out-of-bound
79*c217d954SCole Faust if(is_valid_pixel(xi + xk * dilation_x, 0, width_in) && is_valid_pixel(yi + yk * dilation_y, 0, height_in))
80*c217d954SCole Faust {
81*c217d954SCole Faust const int idx = xk + half_width_weights_start;
82*c217d954SCole Faust const int idy = yk + half_height_weights_start;
83*c217d954SCole Faust
84*c217d954SCole Faust const T i_value = in_ptr[offset_slice_in + xk * dilation_x + yk * dilation_y * width_in];
85*c217d954SCole Faust const TW w_value = w_ptr[idx + idy * width_weights + ifm * width_weights * height_weights];
86*c217d954SCole Faust
87*c217d954SCole Faust acc += i_value * w_value;
88*c217d954SCole Faust }
89*c217d954SCole Faust }
90*c217d954SCole Faust }
91*c217d954SCole Faust }
92*c217d954SCole Faust
93*c217d954SCole Faust // Accumulate the bias and store the result
94*c217d954SCole Faust *out_ptr = acc + (*b_ptr);
95*c217d954SCole Faust }
96*c217d954SCole Faust
97*c217d954SCole Faust // 3D convolution for QASYMM8 type
98*c217d954SCole Faust template < typename T, typename TW, typename TB, ARM_COMPUTE_REQUIRES_TA((std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value) &&(std::is_same<TW, uint8_t>::value
99*c217d954SCole Faust || std::is_same<TW, int8_t>::value)) >
100*c217d954SCole Faust inline void convolution3d(const SimpleTensor<T> &in, const SimpleTensor<TW> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &out,
101*c217d954SCole Faust int i_offset, int w_offset, int b_offset, int o_offset,
102*c217d954SCole Faust int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights, int dilation_x = 1, int dilation_y = 1, int filter_id = 0)
103*c217d954SCole Faust {
104*c217d954SCole Faust const T *in_ptr = in.data() + i_offset;
105*c217d954SCole Faust const TW *w_ptr = weights.data() + w_offset;
106*c217d954SCole Faust const TB *b_ptr = bias.data() + b_offset;
107*c217d954SCole Faust T *out_ptr = out.data() + o_offset;
108*c217d954SCole Faust
109*c217d954SCole Faust const UniformQuantizationInfo iq_info = in.quantization_info().uniform();
110*c217d954SCole Faust const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
111*c217d954SCole Faust const UniformQuantizationInfo oq_info = out.quantization_info().uniform();
112*c217d954SCole Faust
113*c217d954SCole Faust const int input_offset = -iq_info.offset;
114*c217d954SCole Faust const float input_scale = iq_info.scale;
115*c217d954SCole Faust int weights_offset = -wq_info.offset;
116*c217d954SCole Faust float weights_scale = wq_info.scale;
117*c217d954SCole Faust if(is_data_type_quantized_per_channel(weights.data_type()))
118*c217d954SCole Faust {
119*c217d954SCole Faust if(is_data_type_quantized_asymmetric(weights.data_type()))
120*c217d954SCole Faust {
121*c217d954SCole Faust weights_offset = weights.quantization_info().offset()[filter_id];
122*c217d954SCole Faust }
123*c217d954SCole Faust else
124*c217d954SCole Faust {
125*c217d954SCole Faust weights_offset = 0;
126*c217d954SCole Faust }
127*c217d954SCole Faust weights_scale = weights.quantization_info().scale()[filter_id];
128*c217d954SCole Faust }
129*c217d954SCole Faust const int output_offset = oq_info.offset;
130*c217d954SCole Faust const float output_scale = oq_info.scale;
131*c217d954SCole Faust
132*c217d954SCole Faust int output_multiplier = 0;
133*c217d954SCole Faust int output_shift = 0;
134*c217d954SCole Faust const float multiplier = input_scale * weights_scale / output_scale;
135*c217d954SCole Faust arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
136*c217d954SCole Faust
137*c217d954SCole Faust const int half_width_weights_start = width_weights / 2;
138*c217d954SCole Faust const int half_width_weights_end = ((width_weights % 2) == 0) ? (half_width_weights_start - 1) : half_width_weights_start;
139*c217d954SCole Faust const int half_height_weights_start = height_weights / 2;
140*c217d954SCole Faust const int half_height_weights_end = ((height_weights % 2) == 0) ? (half_height_weights_start - 1) : half_height_weights_start;
141*c217d954SCole Faust
142*c217d954SCole Faust // Reset accumulator
143*c217d954SCole Faust int32_t acc(0);
144*c217d954SCole Faust
145*c217d954SCole Faust // Compute a 2D convolution for each IFM and accumulate the result
146*c217d954SCole Faust for(int ifm = 0; ifm < depth_in; ++ifm)
147*c217d954SCole Faust {
148*c217d954SCole Faust // Compute the offset for the input slice
149*c217d954SCole Faust const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
150*c217d954SCole Faust
151*c217d954SCole Faust // Compute 2D convolution
152*c217d954SCole Faust for(int yk = -half_height_weights_start; yk <= half_height_weights_end; ++yk)
153*c217d954SCole Faust {
154*c217d954SCole Faust for(int xk = -half_width_weights_start; xk <= half_width_weights_end; ++xk)
155*c217d954SCole Faust {
156*c217d954SCole Faust // Check if the pixel is out-of-bound
157*c217d954SCole Faust if(is_valid_pixel(xi + xk * dilation_x, 0, width_in) && is_valid_pixel(yi + yk * dilation_y, 0, height_in))
158*c217d954SCole Faust {
159*c217d954SCole Faust const int idx = xk + half_width_weights_start;
160*c217d954SCole Faust const int idy = yk + half_height_weights_start;
161*c217d954SCole Faust
162*c217d954SCole Faust const int32_t i_value = in_ptr[offset_slice_in + xk * dilation_x + yk * dilation_y * width_in];
163*c217d954SCole Faust const int32_t w_value = w_ptr[idx + idy * width_weights + ifm * width_weights * height_weights];
164*c217d954SCole Faust acc += (i_value + input_offset) * (w_value + weights_offset);
165*c217d954SCole Faust }
166*c217d954SCole Faust }
167*c217d954SCole Faust }
168*c217d954SCole Faust }
169*c217d954SCole Faust
170*c217d954SCole Faust // Accumulate the bias
171*c217d954SCole Faust acc += (*b_ptr);
172*c217d954SCole Faust
173*c217d954SCole Faust // Quantize down
174*c217d954SCole Faust acc = validation::quantize_down_scale_by_fixedpoint(acc, output_multiplier, output_shift, output_offset,
175*c217d954SCole Faust std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
176*c217d954SCole Faust
177*c217d954SCole Faust // Store the result
178*c217d954SCole Faust *out_ptr = acc;
179*c217d954SCole Faust }
180*c217d954SCole Faust } // namespace detail
181*c217d954SCole Faust } // namespace convolution_3d
182*c217d954SCole Faust } // namespace test
183*c217d954SCole Faust } // namespace arm_compute
184*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_VALIDATION_CONVOLUTION_H */
185