Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 29 | #include "tests/validation/FixedPoint.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 30 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 31 | #include "tests/validation/reference/Utils.h" |
| 32 | #include "tests/validation/reference/UtilsQuantizedAsymm.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 33 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 34 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 35 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | namespace reference |
| 43 | { |
| 44 | /** Perform a depthwise convolution |
| 45 | * |
| 46 | * - Three dimensions tensors |
| 47 | * - Third dimention is number of channels |
| 48 | * - Depths of input tensor and filter are equals |
| 49 | * - Padding, stride and output shape "match" |
| 50 | * |
| 51 | */ |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 52 | template <typename T, typename TB> |
| 53 | SimpleTensor<T> depthwise_convolution(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 54 | { |
| 55 | // Create reference |
| 56 | SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position() }; |
| 57 | |
| 58 | // Compute reference |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 59 | const int filter_width = weights.shape().x(); |
| 60 | const int filter_height = weights.shape().y(); |
| 61 | const int filter_plane = filter_width * filter_height; |
| 62 | const int input_width = src.shape().x(); |
| 63 | const int input_height = src.shape().y(); |
| 64 | const int input_depth = src.shape().z(); |
| 65 | 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] | 66 | |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 67 | const int filter_half_width = filter_width / 2; |
| 68 | const int filter_half_height = filter_height / 2; |
| 69 | |
| 70 | const int pad_left = std::min(static_cast<int>(conv_info.pad_left()), filter_half_width); |
| 71 | const int pad_top = std::min(static_cast<int>(conv_info.pad_top()), filter_half_height); |
| 72 | const int pad_right = std::min(static_cast<int>(conv_info.pad_right()), filter_half_width); |
| 73 | const int pad_bottom = std::min(static_cast<int>(conv_info.pad_bottom()), filter_half_height); |
| 74 | |
| 75 | const int minimum_x = -pad_left + filter_half_width; |
| 76 | const int minimum_y = -pad_top + filter_half_height; |
| 77 | const int maximum_x = input_width + pad_left - filter_half_width + pad_right - filter_half_width; |
| 78 | const int maximum_y = input_height + pad_top - filter_half_height + pad_bottom - filter_half_height; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 79 | |
| 80 | int out_pos = 0; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 81 | for(int r = 0; r < num_batches; ++r) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 82 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 83 | for(int z = 0; z < input_depth; ++z) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 84 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 85 | for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 86 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 87 | 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] | 88 | { |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 89 | Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r)); |
| 90 | size_t filter_offset = filter_plane * z; |
| 91 | |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 92 | T val(0); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 93 | for(int j = y - filter_half_height; j <= static_cast<int>(y + filter_half_height); ++j) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 94 | { |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 95 | for(int i = x - filter_half_width; i <= static_cast<int>(x + filter_half_width); ++i) |
| 96 | { |
| 97 | coords.set(0, i); |
| 98 | coords.set(1, j); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 99 | T border_value(0); |
| 100 | val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, border_value); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 101 | ++filter_offset; |
| 102 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 103 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 104 | coords.set(0, x); |
| 105 | coords.set(1, y); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 106 | dst[out_pos++] = saturate_cast<T>(val + *static_cast<const TB *>(biases(Coordinates(z)))); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 107 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return dst; |
| 113 | } |
| 114 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 115 | template <> |
| 116 | SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape, |
| 117 | const PadStrideInfo &conv_info) |
| 118 | { |
| 119 | // Create reference |
| 120 | SimpleTensor<uint8_t> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() }; |
| 121 | |
| 122 | const int input_offset = -src.quantization_info().offset; |
| 123 | const float input_scale = src.quantization_info().scale; |
| 124 | const int weights_offset = -weights.quantization_info().offset; |
| 125 | const float weights_scale = weights.quantization_info().scale; |
| 126 | const int output_offset = dst.quantization_info().offset; |
| 127 | const float output_scale = dst.quantization_info().scale; |
| 128 | |
| 129 | int output_multiplier; |
| 130 | int output_shift; |
| 131 | const float multiplier = input_scale * weights_scale / output_scale; |
| 132 | arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 133 | |
| 134 | // Compute reference |
| 135 | const int filter_width = weights.shape().x(); |
| 136 | const int filter_height = weights.shape().y(); |
| 137 | const int filter_plane = filter_width * filter_height; |
| 138 | const int input_width = src.shape().x(); |
| 139 | const int input_height = src.shape().y(); |
| 140 | const int input_depth = src.shape().z(); |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 141 | 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] | 142 | |
| 143 | const int filter_half_size = filter_width / 2; |
| 144 | const int pad_x = std::min(filter_half_size, static_cast<int>(conv_info.pad().first)); |
| 145 | const int pad_y = std::min(filter_half_size, static_cast<int>(conv_info.pad().second)); |
| 146 | const int minimum_x = -pad_x + filter_half_size; |
| 147 | const int minimum_y = -pad_y + filter_half_size; |
| 148 | |
| 149 | int out_pos = 0; |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 150 | for(int r = 0; r < num_batches; ++r) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 151 | { |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 152 | for(int z = 0; z < input_depth; ++z) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 153 | { |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 154 | int32_t bias_val = *static_cast<const int32_t *>(biases(Coordinates(z))); |
| 155 | for(int y = minimum_y; y < input_height + pad_y - filter_half_size; y += conv_info.stride().second) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 156 | { |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 157 | for(int x = minimum_x; x < input_width + pad_x - filter_half_size; x += conv_info.stride().first) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 158 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 159 | Coordinates coords(x, y, z, r); |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 160 | int filter_offset = filter_plane * z; |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 161 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 162 | int32_t val = 0; |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 163 | for(int j = y - filter_half_size; j <= (y + filter_half_size); ++j) |
| 164 | { |
| 165 | for(int i = x - filter_half_size; i <= (x + filter_half_size); ++i) |
| 166 | { |
| 167 | coords.set(0, i); |
| 168 | coords.set(1, j); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 169 | auto in_val = tensor_elem_at<uint8_t>(src, coords, BorderMode::CONSTANT, -input_offset); |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 170 | uint8_t w_val = *(weights.data() + filter_offset); |
| 171 | val += (in_val + input_offset) * (w_val + weights_offset); |
| 172 | ++filter_offset; |
| 173 | } |
| 174 | } |
| 175 | val += bias_val; |
| 176 | val = asymm_rounding_divide_by_pow2(asymm_int_mult(val, output_multiplier), output_shift); |
| 177 | val += output_offset; |
| 178 | val = std::max<int32_t>(val, 0); |
| 179 | val = std::min<int32_t>(val, 255); |
| 180 | |
| 181 | // Store the result |
| 182 | dst[out_pos++] = val; |
| 183 | } |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return dst; |
| 189 | } |
| 190 | |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 191 | template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape, |
| 192 | const PadStrideInfo &conv_info); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 193 | |
| 194 | template SimpleTensor<half> depthwise_convolution(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &biases, const TensorShape &dst_shape, |
| 195 | const PadStrideInfo &conv_info); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 196 | } // namespace reference |
| 197 | } // namespace validation |
| 198 | } // namespace test |
| 199 | } // namespace arm_compute |