blob: 608093d38135e0dd6e8ad8eb98fa688917d1efa0 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Usama3e924592019-04-01 11:58:18 +01002 * Copyright (c) 2017-2019 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +01003 *
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 Arena04a8f8c2017-11-23 11:45:24 +000024#include "DepthwiseConvolutionLayer.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
26#include "ConvolutionLayer.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010027#include "Utils.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010028
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010029#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000030#include "tests/validation/reference/Utils.h"
31#include "tests/validation/reference/UtilsQuantizedAsymm.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010032
Dmitry Savenkod7295b72017-11-20 22:00:08 +070033#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
34
Giorgio Arena93a690e2017-08-01 16:09:33 +010035namespace arm_compute
36{
37namespace test
38{
39namespace validation
40{
41namespace reference
42{
Michele Di Giorgio633d30b2019-10-08 17:17:18 +010043namespace
44{
45/** Perform a depthwise convolution for floating-point types
Giorgio Arena93a690e2017-08-01 16:09:33 +010046 *
47 * - Three dimensions tensors
48 * - Third dimention is number of channels
49 * - Depths of input tensor and filter are equals
50 * - Padding, stride and output shape "match"
51 *
52 */
Michele Di Giorgio633d30b2019-10-08 17:17:18 +010053template <typename T>
54SimpleTensor<T> depthwise_convolution_fp(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info,
55 unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010056{
John Kesapides8d942692019-02-26 14:52:12 +000057 ARM_COMPUTE_UNUSED(out_quant_info);
58
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010059 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
Giorgio Arena563494c2018-04-30 17:29:41 +010060
Giorgio Arena93a690e2017-08-01 16:09:33 +010061 // Compute reference
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010062 const int filter_width = weights.shape().x();
63 const int filter_height = weights.shape().y();
64 const int filter_plane = filter_width * filter_height;
65 const int input_width = src.shape().x();
66 const int input_height = src.shape().y();
67 const int input_depth = src.shape().z();
68 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
Giorgio Arena93a690e2017-08-01 16:09:33 +010069
Georgios Pinitas4074c992018-01-30 18:13:46 +000070 const int pad_left = conv_info.pad_left();
71 const int pad_top = conv_info.pad_top();
72 const int pad_right = conv_info.pad_right();
73 const int pad_bottom = conv_info.pad_bottom();
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010074
Usama Arife73686a2019-04-08 17:30:48 +010075 const float patch_width = (filter_width + (dilation.x() - 1) * (filter_width - 1));
76 const float patch_height = (filter_height + (dilation.y() - 1) * (filter_height - 1));
Usama3e924592019-04-01 11:58:18 +010077
Usama Arife73686a2019-04-08 17:30:48 +010078 const int patch_half_width_floor = patch_width / 2;
79 const int patch_half_height_floor = patch_height / 2;
80
81 const auto patch_half_width_ceil = static_cast<int>(std::ceil(patch_width / 2));
82 const auto patch_half_height_ceil = static_cast<int>(std::ceil(patch_height / 2));
83
84 const int minimum_x = -pad_left + patch_half_width_floor;
85 const int minimum_y = -pad_top + patch_half_height_floor;
86 const int maximum_x = input_width + pad_left + pad_right - static_cast<int>(patch_width);
87 const int maximum_y = input_height + pad_top + pad_bottom - static_cast<int>(patch_height);
Giorgio Arena93a690e2017-08-01 16:09:33 +010088
Giorgio Arena76572242018-04-04 17:44:26 +010089 const T border_value(0);
90
Giorgio Arena93a690e2017-08-01 16:09:33 +010091 int out_pos = 0;
Giorgio Arena9fe41442017-08-23 16:36:24 +010092 for(int r = 0; r < num_batches; ++r)
Giorgio Arena93a690e2017-08-01 16:09:33 +010093 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010094 for(int z = 0; z < input_depth; ++z)
Giorgio Arena93a690e2017-08-01 16:09:33 +010095 {
Giorgio Arena76572242018-04-04 17:44:26 +010096 for(unsigned int m = 0; m < depth_multiplier; ++m)
Giorgio Arena93a690e2017-08-01 16:09:33 +010097 {
Giorgio Arena76572242018-04-04 17:44:26 +010098 const int out_z = z * depth_multiplier + m;
Giorgio Arena9fe41442017-08-23 16:36:24 +010099
Usama Arife73686a2019-04-08 17:30:48 +0100100 for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second)
Giorgio Arena76572242018-04-04 17:44:26 +0100101 {
Usama Arife73686a2019-04-08 17:30:48 +0100102 for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100103 {
Giorgio Arena76572242018-04-04 17:44:26 +0100104 Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r));
105 size_t filter_offset = filter_plane * out_z;
106
107 T val(0);
Usama Arife73686a2019-04-08 17:30:48 +0100108 for(int j = y - patch_half_height_floor; j < y + patch_half_height_ceil; j += dilation.y())
Giorgio Arena9fe41442017-08-23 16:36:24 +0100109 {
Usama Arife73686a2019-04-08 17:30:48 +0100110 for(int i = x - patch_half_width_floor; i < x + patch_half_width_ceil; i += dilation.x())
Giorgio Arena76572242018-04-04 17:44:26 +0100111 {
112 coords.set(0, i);
113 coords.set(1, j);
Giorgio Arena76572242018-04-04 17:44:26 +0100114 val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, border_value);
115 ++filter_offset;
116 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100117 }
Giorgio Arena76572242018-04-04 17:44:26 +0100118
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100119 dst[out_pos++] = saturate_cast<T>(val + *static_cast<const T *>(biases(Coordinates(out_z))));
Giorgio Arena93a690e2017-08-01 16:09:33 +0100120 }
121 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100122 }
123 }
124 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100125
126 return dst;
Giorgio Arena93a690e2017-08-01 16:09:33 +0100127}
128
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100129/** Perform a quantized depthwise convolution
130 *
131 * - Three dimensions tensors
132 * - Third dimention is number of channels
133 * - Depths of input tensor and filter are equals
134 * - Padding, stride and output shape "match"
135 * - QASYMM8 input, output
136 * - QASYMM8 or QSYMM8_PER_CHANNEL filter
137 *
138 */
139template <typename T, typename TW, typename TB>
140SimpleTensor<T> depthwise_convolution_quantized(const SimpleTensor<T> &src, const SimpleTensor<TW> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape,
141 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700142{
John Kesapides8d942692019-02-26 14:52:12 +0000143 // if no explicit quantization has been set you the same as src
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100144 const QuantizationInfo &dst_qinfo = out_quant_info.uniform().empty() ? src.quantization_info() : out_quant_info;
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100145 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, dst_qinfo };
Giorgio Arena563494c2018-04-30 17:29:41 +0100146
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700147 // Create reference
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100148 const int input_offset = -src.quantization_info().uniform().offset;
149 const float input_scale = src.quantization_info().uniform().scale;
150 const int weights_offset = -weights.quantization_info().uniform().offset;
Georgios Pinitasddec4d62019-07-10 19:23:02 +0100151 const int output_offset = dst_qinfo.uniform().offset;
152 const float output_scale = dst_qinfo.uniform().scale;
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700153
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100154 const std::vector<float> weights_scale_vec = weights.quantization_info().scale();
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700155
156 // Compute reference
157 const int filter_width = weights.shape().x();
158 const int filter_height = weights.shape().y();
159 const int filter_plane = filter_width * filter_height;
160 const int input_width = src.shape().x();
161 const int input_height = src.shape().y();
162 const int input_depth = src.shape().z();
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000163 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700164
Georgios Pinitas15997872018-02-19 13:58:22 +0000165 const int pad_left = conv_info.pad_left();
166 const int pad_top = conv_info.pad_top();
167 const int pad_right = conv_info.pad_right();
168 const int pad_bottom = conv_info.pad_bottom();
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000169
Usama Arife73686a2019-04-08 17:30:48 +0100170 const float patch_width = (filter_width + (dilation.x() - 1) * (filter_width - 1));
171 const float patch_height = (filter_height + (dilation.y() - 1) * (filter_height - 1));
Usama3e924592019-04-01 11:58:18 +0100172
Usama Arife73686a2019-04-08 17:30:48 +0100173 const int patch_half_width_floor = patch_width / 2;
174 const int patch_half_height_floor = patch_height / 2;
175
176 const auto patch_half_width_ceil = static_cast<int>(std::ceil(patch_width / 2));
177 const auto patch_half_height_ceil = static_cast<int>(std::ceil(patch_height / 2));
178
179 const int minimum_x = -pad_left + patch_half_width_floor;
180 const int minimum_y = -pad_top + patch_half_height_floor;
181 const int maximum_x = input_width + pad_left + pad_right - static_cast<int>(patch_width);
182 const int maximum_y = input_height + pad_top + pad_bottom - static_cast<int>(patch_height);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700183
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100184 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights.data_type());
185
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700186 int out_pos = 0;
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000187 for(int r = 0; r < num_batches; ++r)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700188 {
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000189 for(int z = 0; z < input_depth; ++z)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700190 {
Giorgio Arena76572242018-04-04 17:44:26 +0100191 for(unsigned int m = 0; m < depth_multiplier; ++m)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700192 {
Giorgio Arena76572242018-04-04 17:44:26 +0100193 const int out_z = z * depth_multiplier + m;
194 const int32_t bias_val = *static_cast<const int32_t *>(biases(Coordinates(out_z)));
195
Giorgio Arenad93e2632019-10-15 11:09:33 +0100196 int output_multiplier = 0;
197 int output_shift = 0;
198 const float weights_scale = (is_quantized_per_channel) ? weights_scale_vec[out_z] : weights_scale_vec[0];
199 const float multiplier = input_scale * weights_scale / output_scale;
200 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
201
Usama Arife73686a2019-04-08 17:30:48 +0100202 for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700203 {
Usama Arife73686a2019-04-08 17:30:48 +0100204 for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first)
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000205 {
Giorgio Arena76572242018-04-04 17:44:26 +0100206 Coordinates coords(x, y, z, r);
207 int filter_offset = filter_plane * out_z;
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000208
Giorgio Arena76572242018-04-04 17:44:26 +0100209 int32_t val = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100210 for(int j = y - patch_half_height_floor; j < y + patch_half_height_ceil; j += dilation.y())
Giorgio Arena76572242018-04-04 17:44:26 +0100211 {
Usama Arife73686a2019-04-08 17:30:48 +0100212 for(int i = x - patch_half_width_floor; i < x + patch_half_width_ceil; i += dilation.x())
Giorgio Arena76572242018-04-04 17:44:26 +0100213 {
214 coords.set(0, i);
215 coords.set(1, j);
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100216 const auto in_val = tensor_elem_at<T>(src, coords, BorderMode::CONSTANT, -input_offset);
217 const TW w_val = *(weights.data() + filter_offset);
Giorgio Arena76572242018-04-04 17:44:26 +0100218 val += (in_val + input_offset) * (w_val + weights_offset);
219 ++filter_offset;
220 }
221 }
222 val += bias_val;
223 val = asymm_rounding_divide_by_pow2(asymm_int_mult(val, output_multiplier), output_shift);
224 val += output_offset;
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100225 val = utility::clamp<int32_t>(val, 0, 255);
Giorgio Arena76572242018-04-04 17:44:26 +0100226
227 // Store the result
228 dst[out_pos++] = val;
229 }
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000230 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700231 }
232 }
233 }
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100234
235 return dst;
236}
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100237} // namespace
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100238
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100239template <>
240SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape,
241 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
242{
243 return depthwise_convolution_fp(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation, out_quant_info);
244}
Frank Lei8cdfdb82018-01-02 16:49:33 +0800245
Michele Di Giorgio633d30b2019-10-08 17:17:18 +0100246template <>
247SimpleTensor<half> depthwise_convolution(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &biases, const TensorShape &dst_shape,
248 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
249{
250 return depthwise_convolution_fp(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation, out_quant_info);
251}
252
253template <>
254SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape,
255 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
256{
257 return depthwise_convolution_quantized<uint8_t, uint8_t, int32_t>(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation, out_quant_info);
258}
259
260template <>
261SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape,
262 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation, const QuantizationInfo &out_quant_info)
263{
264 return depthwise_convolution_quantized<uint8_t, int8_t, int32_t>(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation, out_quant_info);
265}
Giorgio Arena93a690e2017-08-01 16:09:33 +0100266} // namespace reference
267} // namespace validation
268} // namespace test
269} // namespace arm_compute