blob: b1d2b923f729cea655747c68339781f6c1904d26 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins4ba87db2019-05-23 17:11:51 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
24#include "DepthwiseConvolutionLayer.h"
25
26#include "ConvolutionLayer.h"
27#include "Utils.h"
28
Anthony Barbier8140e1e2017-12-14 23:48:46 +000029#include "tests/validation/Helpers.h"
30#include "tests/validation/reference/Utils.h"
31#include "tests/validation/reference/UtilsQuantizedAsymm.h"
32
33#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
34
35namespace arm_compute
36{
37namespace test
38{
39namespace validation
40{
41namespace 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 */
51template <typename T, typename TB>
Jenkinsb3a371b2018-05-23 11:36:53 +010052SimpleTensor<T> depthwise_convolution(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info,
Jenkins975dfe12019-09-02 11:47:54 +010053 unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000054{
Jenkins4ba87db2019-05-23 17:11:51 +010055 ARM_COMPUTE_UNUSED(out_quant_info);
56
Jenkins52ba29e2018-08-29 15:32:11 +000057 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
Anthony Barbier8140e1e2017-12-14 23:48:46 +000058
59 // Compute reference
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);
67
Anthony Barbier06ea0482018-02-22 15:45:35 +000068 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();
Anthony Barbier8140e1e2017-12-14 23:48:46 +000072
Jenkins4ba87db2019-05-23 17:11:51 +010073 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));
75
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);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000086
Jenkinsb3a371b2018-05-23 11:36:53 +010087 const T border_value(0);
88
Anthony Barbier8140e1e2017-12-14 23:48:46 +000089 int out_pos = 0;
90 for(int r = 0; r < num_batches; ++r)
91 {
92 for(int z = 0; z < input_depth; ++z)
93 {
Jenkinsb3a371b2018-05-23 11:36:53 +010094 for(unsigned int m = 0; m < depth_multiplier; ++m)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000095 {
Jenkinsb3a371b2018-05-23 11:36:53 +010096 const int out_z = z * depth_multiplier + m;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000097
Jenkins4ba87db2019-05-23 17:11:51 +010098 for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second)
Jenkinsb3a371b2018-05-23 11:36:53 +010099 {
Jenkins4ba87db2019-05-23 17:11:51 +0100100 for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000101 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100102 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);
Jenkins4ba87db2019-05-23 17:11:51 +0100106 for(int j = y - patch_half_height_floor; j < y + patch_half_height_ceil; j += dilation.y())
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000107 {
Jenkins4ba87db2019-05-23 17:11:51 +0100108 for(int i = x - patch_half_width_floor; i < x + patch_half_width_ceil; i += dilation.x())
Jenkinsb3a371b2018-05-23 11:36:53 +0100109 {
110 coords.set(0, i);
111 coords.set(1, j);
Jenkinsb3a371b2018-05-23 11:36:53 +0100112 val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, border_value);
113 ++filter_offset;
114 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000115 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100116
117 dst[out_pos++] = saturate_cast<T>(val + *static_cast<const TB *>(biases(Coordinates(out_z))));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000118 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000119 }
120 }
121 }
122 }
123
124 return dst;
125}
126
127template <>
128SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape,
Jenkins975dfe12019-09-02 11:47:54 +0100129 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000130{
Jenkins4ba87db2019-05-23 17:11:51 +0100131 // if no explicit quantization has been set you the same as src
Jenkins975dfe12019-09-02 11:47:54 +0100132 const QuantizationInfo &dst_qinfo = out_quant_info.uniform().empty() ? src.quantization_info() : out_quant_info;
133 SimpleTensor<uint8_t> dst{ dst_shape, src.data_type(), 1, dst_qinfo };
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000134
Jenkinsb3a371b2018-05-23 11:36:53 +0100135 // Create reference
Jenkins975dfe12019-09-02 11:47:54 +0100136 const int input_offset = -src.quantization_info().uniform().offset;
137 const float input_scale = src.quantization_info().uniform().scale;
138 const int weights_offset = -weights.quantization_info().uniform().offset;
139 const float weights_scale = weights.quantization_info().uniform().scale;
140 const int output_offset = dst_qinfo.uniform().offset;
141 const float output_scale = dst_qinfo.uniform().scale;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000142
Jenkins975dfe12019-09-02 11:47:54 +0100143 int output_multiplier = 0;
144 int output_shift = 0;
145 const float multiplier = input_scale * weights_scale / output_scale;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000146 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
147
148 // Compute reference
149 const int filter_width = weights.shape().x();
150 const int filter_height = weights.shape().y();
151 const int filter_plane = filter_width * filter_height;
152 const int input_width = src.shape().x();
153 const int input_height = src.shape().y();
154 const int input_depth = src.shape().z();
155 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
156
Anthony Barbier06ea0482018-02-22 15:45:35 +0000157 const int pad_left = conv_info.pad_left();
158 const int pad_top = conv_info.pad_top();
159 const int pad_right = conv_info.pad_right();
160 const int pad_bottom = conv_info.pad_bottom();
161
Jenkins4ba87db2019-05-23 17:11:51 +0100162 const float patch_width = (filter_width + (dilation.x() - 1) * (filter_width - 1));
163 const float patch_height = (filter_height + (dilation.y() - 1) * (filter_height - 1));
164
165 const int patch_half_width_floor = patch_width / 2;
166 const int patch_half_height_floor = patch_height / 2;
167
168 const auto patch_half_width_ceil = static_cast<int>(std::ceil(patch_width / 2));
169 const auto patch_half_height_ceil = static_cast<int>(std::ceil(patch_height / 2));
170
171 const int minimum_x = -pad_left + patch_half_width_floor;
172 const int minimum_y = -pad_top + patch_half_height_floor;
173 const int maximum_x = input_width + pad_left + pad_right - static_cast<int>(patch_width);
174 const int maximum_y = input_height + pad_top + pad_bottom - static_cast<int>(patch_height);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000175
176 int out_pos = 0;
177 for(int r = 0; r < num_batches; ++r)
178 {
179 for(int z = 0; z < input_depth; ++z)
180 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100181 for(unsigned int m = 0; m < depth_multiplier; ++m)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000182 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100183 const int out_z = z * depth_multiplier + m;
184 const int32_t bias_val = *static_cast<const int32_t *>(biases(Coordinates(out_z)));
185
Jenkins4ba87db2019-05-23 17:11:51 +0100186 for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000187 {
Jenkins4ba87db2019-05-23 17:11:51 +0100188 for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000189 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100190 Coordinates coords(x, y, z, r);
191 int filter_offset = filter_plane * out_z;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000192
Jenkinsb3a371b2018-05-23 11:36:53 +0100193 int32_t val = 0;
Jenkins4ba87db2019-05-23 17:11:51 +0100194 for(int j = y - patch_half_height_floor; j < y + patch_half_height_ceil; j += dilation.y())
Jenkinsb3a371b2018-05-23 11:36:53 +0100195 {
Jenkins4ba87db2019-05-23 17:11:51 +0100196 for(int i = x - patch_half_width_floor; i < x + patch_half_width_ceil; i += dilation.x())
Jenkinsb3a371b2018-05-23 11:36:53 +0100197 {
198 coords.set(0, i);
199 coords.set(1, j);
200 const auto in_val = tensor_elem_at<uint8_t>(src, coords, BorderMode::CONSTANT, -input_offset);
201 const uint8_t w_val = *(weights.data() + filter_offset);
202 val += (in_val + input_offset) * (w_val + weights_offset);
203 ++filter_offset;
204 }
205 }
206 val += bias_val;
207 val = asymm_rounding_divide_by_pow2(asymm_int_mult(val, output_multiplier), output_shift);
208 val += output_offset;
209 val = std::max<int32_t>(val, 0);
210 val = std::min<int32_t>(val, 255);
211
212 // Store the result
213 dst[out_pos++] = val;
214 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000215 }
216 }
217 }
218 }
219
220 return dst;
221}
222
223template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape,
Jenkins975dfe12019-09-02 11:47:54 +0100224 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000225
226template SimpleTensor<half> depthwise_convolution(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &biases, const TensorShape &dst_shape,
Jenkins975dfe12019-09-02 11:47:54 +0100227 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000228} // namespace reference
229} // namespace validation
230} // namespace test
231} // namespace arm_compute