blob: 39429e2449f19be2245f56a7a672bb478ac739ca [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 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,
53 unsigned int depth_multiplier)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000054{
Jenkins52ba29e2018-08-29 15:32:11 +000055 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
Anthony Barbier8140e1e2017-12-14 23:48:46 +000056
57 // Compute reference
58 const int filter_width = weights.shape().x();
59 const int filter_height = weights.shape().y();
60 const int filter_plane = filter_width * filter_height;
61 const int input_width = src.shape().x();
62 const int input_height = src.shape().y();
63 const int input_depth = src.shape().z();
64 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
65
66 const int filter_half_width = filter_width / 2;
67 const int filter_half_height = filter_height / 2;
68
Anthony Barbier06ea0482018-02-22 15:45:35 +000069 const int pad_left = conv_info.pad_left();
70 const int pad_top = conv_info.pad_top();
71 const int pad_right = conv_info.pad_right();
72 const int pad_bottom = conv_info.pad_bottom();
Anthony Barbier8140e1e2017-12-14 23:48:46 +000073
74 const int minimum_x = -pad_left + filter_half_width;
75 const int minimum_y = -pad_top + filter_half_height;
76 const int maximum_x = input_width + pad_left - filter_half_width + pad_right - filter_half_width;
77 const int maximum_y = input_height + pad_top - filter_half_height + pad_bottom - filter_half_height;
78
Jenkinsb3a371b2018-05-23 11:36:53 +010079 const T border_value(0);
80
Anthony Barbier8140e1e2017-12-14 23:48:46 +000081 int out_pos = 0;
82 for(int r = 0; r < num_batches; ++r)
83 {
84 for(int z = 0; z < input_depth; ++z)
85 {
Jenkinsb3a371b2018-05-23 11:36:53 +010086 for(unsigned int m = 0; m < depth_multiplier; ++m)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000087 {
Jenkinsb3a371b2018-05-23 11:36:53 +010088 const int out_z = z * depth_multiplier + m;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000089
Jenkinsb3a371b2018-05-23 11:36:53 +010090 for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second)
91 {
92 for(int x = minimum_x; x < minimum_x + maximum_x; x += conv_info.stride().first)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000093 {
Jenkinsb3a371b2018-05-23 11:36:53 +010094 Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r));
95 size_t filter_offset = filter_plane * out_z;
96
97 T val(0);
98 for(int j = y - filter_half_height; j <= static_cast<int>(y + filter_half_height); ++j)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000099 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100100 for(int i = x - filter_half_width; i <= static_cast<int>(x + filter_half_width); ++i)
101 {
102 coords.set(0, i);
103 coords.set(1, j);
104
105 val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, border_value);
106 ++filter_offset;
107 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000108 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100109
110 dst[out_pos++] = saturate_cast<T>(val + *static_cast<const TB *>(biases(Coordinates(out_z))));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000111 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000112 }
113 }
114 }
115 }
116
117 return dst;
118}
119
120template <>
121SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape,
Jenkinsb3a371b2018-05-23 11:36:53 +0100122 const PadStrideInfo &conv_info, unsigned int depth_multiplier)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000123{
Jenkins52ba29e2018-08-29 15:32:11 +0000124 SimpleTensor<uint8_t> dst{ dst_shape, src.data_type(), 1, src.quantization_info() };
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000125
Jenkinsb3a371b2018-05-23 11:36:53 +0100126 // Create reference
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000127 const int input_offset = -src.quantization_info().offset;
128 const float input_scale = src.quantization_info().scale;
129 const int weights_offset = -weights.quantization_info().offset;
130 const float weights_scale = weights.quantization_info().scale;
131 const int output_offset = dst.quantization_info().offset;
132 const float output_scale = dst.quantization_info().scale;
133
134 int output_multiplier;
135 int output_shift;
136 const float multiplier = input_scale * weights_scale / output_scale;
137 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
138
139 // Compute reference
140 const int filter_width = weights.shape().x();
141 const int filter_height = weights.shape().y();
142 const int filter_plane = filter_width * filter_height;
143 const int input_width = src.shape().x();
144 const int input_height = src.shape().y();
145 const int input_depth = src.shape().z();
146 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
147
Anthony Barbier06ea0482018-02-22 15:45:35 +0000148 const int filter_half_width = filter_width / 2;
149 const int filter_half_height = filter_height / 2;
150
151 const int pad_left = conv_info.pad_left();
152 const int pad_top = conv_info.pad_top();
153 const int pad_right = conv_info.pad_right();
154 const int pad_bottom = conv_info.pad_bottom();
155
156 const int minimum_x = -pad_left + filter_half_width;
157 const int minimum_y = -pad_top + filter_half_height;
158 const int maximum_x = input_width + pad_left - filter_half_width + pad_right - filter_half_width;
159 const int maximum_y = input_height + pad_top - filter_half_height + pad_bottom - filter_half_height;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000160
161 int out_pos = 0;
162 for(int r = 0; r < num_batches; ++r)
163 {
164 for(int z = 0; z < input_depth; ++z)
165 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100166 for(unsigned int m = 0; m < depth_multiplier; ++m)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000167 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100168 const int out_z = z * depth_multiplier + m;
169 const int32_t bias_val = *static_cast<const int32_t *>(biases(Coordinates(out_z)));
170
171 for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000172 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100173 for(int x = minimum_x; x < minimum_x + maximum_x; x += conv_info.stride().first)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000174 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100175 Coordinates coords(x, y, z, r);
176 int filter_offset = filter_plane * out_z;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000177
Jenkinsb3a371b2018-05-23 11:36:53 +0100178 int32_t val = 0;
179 for(int j = y - filter_half_height; j <= (y + filter_half_height); ++j)
180 {
181 for(int i = x - filter_half_width; i <= (x + filter_half_width); ++i)
182 {
183 coords.set(0, i);
184 coords.set(1, j);
185 const auto in_val = tensor_elem_at<uint8_t>(src, coords, BorderMode::CONSTANT, -input_offset);
186 const uint8_t w_val = *(weights.data() + filter_offset);
187 val += (in_val + input_offset) * (w_val + weights_offset);
188 ++filter_offset;
189 }
190 }
191 val += bias_val;
192 val = asymm_rounding_divide_by_pow2(asymm_int_mult(val, output_multiplier), output_shift);
193 val += output_offset;
194 val = std::max<int32_t>(val, 0);
195 val = std::min<int32_t>(val, 255);
196
197 // Store the result
198 dst[out_pos++] = val;
199 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000200 }
201 }
202 }
203 }
204
205 return dst;
206}
207
208template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape,
Jenkinsb3a371b2018-05-23 11:36:53 +0100209 const PadStrideInfo &conv_info, unsigned int depth_multiplier);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000210
211template SimpleTensor<half> depthwise_convolution(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &biases, const TensorShape &dst_shape,
Jenkinsb3a371b2018-05-23 11:36:53 +0100212 const PadStrideInfo &conv_info, unsigned int depth_multiplier);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000213} // namespace reference
214} // namespace validation
215} // namespace test
216} // namespace arm_compute