Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Usama | 3e92459 | 2019-04-01 11:58:18 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [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: |
| 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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "DepthwiseConvolutionLayer.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 25 | |
| 26 | #include "ConvolutionLayer.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 27 | #include "Utils.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 28 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 29 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 30 | #include "tests/validation/reference/Utils.h" |
| 31 | #include "tests/validation/reference/UtilsQuantizedAsymm.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 32 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 33 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 34 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace test |
| 38 | { |
| 39 | namespace validation |
| 40 | { |
| 41 | namespace reference |
| 42 | { |
| 43 | /** Perform a depthwise convolution |
| 44 | * |
| 45 | * - Three dimensions tensors |
| 46 | * - Third dimention is number of channels |
| 47 | * - Depths of input tensor and filter are equals |
| 48 | * - Padding, stride and output shape "match" |
| 49 | * |
| 50 | */ |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 51 | template <typename T, typename TB> |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 52 | SimpleTensor<T> depthwise_convolution(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info, |
John Kesapides | 8d94269 | 2019-02-26 14:52:12 +0000 | [diff] [blame] | 53 | unsigned int depth_multiplier, const Size2D &dilation, QuantizationInfo out_quant_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 54 | { |
John Kesapides | 8d94269 | 2019-02-26 14:52:12 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_UNUSED(out_quant_info); |
| 56 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 57 | SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 }; |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 58 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 59 | // Compute reference |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 60 | const int filter_width = weights.shape().x(); |
| 61 | const int filter_height = weights.shape().y(); |
| 62 | const int filter_plane = filter_width * filter_height; |
| 63 | const int input_width = src.shape().x(); |
| 64 | const int input_height = src.shape().y(); |
| 65 | const int input_depth = src.shape().z(); |
| 66 | const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 67 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 68 | const int pad_left = conv_info.pad_left(); |
| 69 | const int pad_top = conv_info.pad_top(); |
| 70 | const int pad_right = conv_info.pad_right(); |
| 71 | const int pad_bottom = conv_info.pad_bottom(); |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 72 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 73 | const float patch_width = (filter_width + (dilation.x() - 1) * (filter_width - 1)); |
| 74 | const float patch_height = (filter_height + (dilation.y() - 1) * (filter_height - 1)); |
Usama | 3e92459 | 2019-04-01 11:58:18 +0100 | [diff] [blame] | 75 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 76 | const int patch_half_width_floor = patch_width / 2; |
| 77 | const int patch_half_height_floor = patch_height / 2; |
| 78 | |
| 79 | const auto patch_half_width_ceil = static_cast<int>(std::ceil(patch_width / 2)); |
| 80 | const auto patch_half_height_ceil = static_cast<int>(std::ceil(patch_height / 2)); |
| 81 | |
| 82 | const int minimum_x = -pad_left + patch_half_width_floor; |
| 83 | const int minimum_y = -pad_top + patch_half_height_floor; |
| 84 | const int maximum_x = input_width + pad_left + pad_right - static_cast<int>(patch_width); |
| 85 | const int maximum_y = input_height + pad_top + pad_bottom - static_cast<int>(patch_height); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 86 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 87 | const T border_value(0); |
| 88 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 89 | int out_pos = 0; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 90 | for(int r = 0; r < num_batches; ++r) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 91 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 92 | for(int z = 0; z < input_depth; ++z) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 93 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 94 | for(unsigned int m = 0; m < depth_multiplier; ++m) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 95 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 96 | const int out_z = z * depth_multiplier + m; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 97 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 98 | for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second) |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 99 | { |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 100 | for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 101 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 102 | Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r)); |
| 103 | size_t filter_offset = filter_plane * out_z; |
| 104 | |
| 105 | T val(0); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 106 | for(int j = y - patch_half_height_floor; j < y + patch_half_height_ceil; j += dilation.y()) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 107 | { |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 108 | for(int i = x - patch_half_width_floor; i < x + patch_half_width_ceil; i += dilation.x()) |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 109 | { |
| 110 | coords.set(0, i); |
| 111 | coords.set(1, j); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 112 | val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, border_value); |
| 113 | ++filter_offset; |
| 114 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 115 | } |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 116 | |
| 117 | dst[out_pos++] = saturate_cast<T>(val + *static_cast<const TB *>(biases(Coordinates(out_z)))); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 123 | |
| 124 | return dst; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 127 | template <> |
| 128 | SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape, |
John Kesapides | 8d94269 | 2019-02-26 14:52:12 +0000 | [diff] [blame] | 129 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, QuantizationInfo out_quant_info) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 130 | { |
John Kesapides | 8d94269 | 2019-02-26 14:52:12 +0000 | [diff] [blame] | 131 | // if no explicit quantization has been set you the same as src |
| 132 | if(out_quant_info == QuantizationInfo(0.0f, 0)) |
| 133 | { |
| 134 | out_quant_info = src.quantization_info(); |
| 135 | } |
| 136 | SimpleTensor<uint8_t> dst{ dst_shape, src.data_type(), 1, out_quant_info }; |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 137 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 138 | // Create reference |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 139 | const int input_offset = -src.quantization_info().offset; |
| 140 | const float input_scale = src.quantization_info().scale; |
| 141 | const int weights_offset = -weights.quantization_info().offset; |
| 142 | const float weights_scale = weights.quantization_info().scale; |
| 143 | const int output_offset = dst.quantization_info().offset; |
| 144 | const float output_scale = dst.quantization_info().scale; |
| 145 | |
| 146 | int output_multiplier; |
| 147 | int output_shift; |
| 148 | const float multiplier = input_scale * weights_scale / output_scale; |
| 149 | arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 150 | |
| 151 | // Compute reference |
| 152 | const int filter_width = weights.shape().x(); |
| 153 | const int filter_height = weights.shape().y(); |
| 154 | const int filter_plane = filter_width * filter_height; |
| 155 | const int input_width = src.shape().x(); |
| 156 | const int input_height = src.shape().y(); |
| 157 | const int input_depth = src.shape().z(); |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 158 | const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 159 | |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 160 | const int pad_left = conv_info.pad_left(); |
| 161 | const int pad_top = conv_info.pad_top(); |
| 162 | const int pad_right = conv_info.pad_right(); |
| 163 | const int pad_bottom = conv_info.pad_bottom(); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 164 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 165 | const float patch_width = (filter_width + (dilation.x() - 1) * (filter_width - 1)); |
| 166 | const float patch_height = (filter_height + (dilation.y() - 1) * (filter_height - 1)); |
Usama | 3e92459 | 2019-04-01 11:58:18 +0100 | [diff] [blame] | 167 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 168 | const int patch_half_width_floor = patch_width / 2; |
| 169 | const int patch_half_height_floor = patch_height / 2; |
| 170 | |
| 171 | const auto patch_half_width_ceil = static_cast<int>(std::ceil(patch_width / 2)); |
| 172 | const auto patch_half_height_ceil = static_cast<int>(std::ceil(patch_height / 2)); |
| 173 | |
| 174 | const int minimum_x = -pad_left + patch_half_width_floor; |
| 175 | const int minimum_y = -pad_top + patch_half_height_floor; |
| 176 | const int maximum_x = input_width + pad_left + pad_right - static_cast<int>(patch_width); |
| 177 | const int maximum_y = input_height + pad_top + pad_bottom - static_cast<int>(patch_height); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 178 | |
| 179 | int out_pos = 0; |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 180 | for(int r = 0; r < num_batches; ++r) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 181 | { |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 182 | for(int z = 0; z < input_depth; ++z) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 183 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 184 | for(unsigned int m = 0; m < depth_multiplier; ++m) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 185 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 186 | const int out_z = z * depth_multiplier + m; |
| 187 | const int32_t bias_val = *static_cast<const int32_t *>(biases(Coordinates(out_z))); |
| 188 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 189 | for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 190 | { |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 191 | for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first) |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 192 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 193 | Coordinates coords(x, y, z, r); |
| 194 | int filter_offset = filter_plane * out_z; |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 195 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 196 | int32_t val = 0; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 197 | for(int j = y - patch_half_height_floor; j < y + patch_half_height_ceil; j += dilation.y()) |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 198 | { |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 199 | for(int i = x - patch_half_width_floor; i < x + patch_half_width_ceil; i += dilation.x()) |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 200 | { |
| 201 | coords.set(0, i); |
| 202 | coords.set(1, j); |
| 203 | const auto in_val = tensor_elem_at<uint8_t>(src, coords, BorderMode::CONSTANT, -input_offset); |
| 204 | const uint8_t w_val = *(weights.data() + filter_offset); |
| 205 | val += (in_val + input_offset) * (w_val + weights_offset); |
| 206 | ++filter_offset; |
| 207 | } |
| 208 | } |
| 209 | val += bias_val; |
| 210 | val = asymm_rounding_divide_by_pow2(asymm_int_mult(val, output_multiplier), output_shift); |
| 211 | val += output_offset; |
| 212 | val = std::max<int32_t>(val, 0); |
| 213 | val = std::min<int32_t>(val, 255); |
| 214 | |
| 215 | // Store the result |
| 216 | dst[out_pos++] = val; |
| 217 | } |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 218 | } |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 222 | |
| 223 | return dst; |
| 224 | } |
| 225 | |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 226 | template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape, |
John Kesapides | 8d94269 | 2019-02-26 14:52:12 +0000 | [diff] [blame] | 227 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, QuantizationInfo out_quant_info); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 228 | |
| 229 | template SimpleTensor<half> depthwise_convolution(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &biases, const TensorShape &dst_shape, |
John Kesapides | 8d94269 | 2019-02-26 14:52:12 +0000 | [diff] [blame] | 230 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, QuantizationInfo out_quant_info); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 231 | } // namespace reference |
| 232 | } // namespace validation |
| 233 | } // namespace test |
| 234 | } // namespace arm_compute |