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