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