blob: d05da9140bb10088072334a7229e20957a49586d [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Georgios Pinitasf72f9362018-01-12 16:29:45 +00002 * Copyright (c) 2017-2018 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"
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010027#include "Permute.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010028#include "Utils.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010029
Dmitry Savenkod7295b72017-11-20 22:00:08 +070030#include "tests/validation/FixedPoint.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000032#include "tests/validation/reference/Utils.h"
33#include "tests/validation/reference/UtilsQuantizedAsymm.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010034
Dmitry Savenkod7295b72017-11-20 22:00:08 +070035#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
36
Giorgio Arena93a690e2017-08-01 16:09:33 +010037namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43namespace reference
44{
45/** Perform a depthwise convolution
46 *
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 */
Dmitry Savenkod7295b72017-11-20 22:00:08 +070053template <typename T, typename TB>
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010054void depthwise_convolution_nchw(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &biases, SimpleTensor<T> &dst, const PadStrideInfo &conv_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010055{
Giorgio Arena93a690e2017-08-01 16:09:33 +010056 // Compute reference
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010057 const int filter_width = weights.shape().x();
58 const int filter_height = weights.shape().y();
59 const int filter_plane = filter_width * filter_height;
60 const int input_width = src.shape().x();
61 const int input_height = src.shape().y();
62 const int input_depth = src.shape().z();
63 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
Giorgio Arena93a690e2017-08-01 16:09:33 +010064
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010065 const int filter_half_width = filter_width / 2;
66 const int filter_half_height = filter_height / 2;
67
Georgios Pinitas4074c992018-01-30 18:13:46 +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();
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010072
73 const int minimum_x = -pad_left + filter_half_width;
74 const int minimum_y = -pad_top + filter_half_height;
75 const int maximum_x = input_width + pad_left - filter_half_width + pad_right - filter_half_width;
76 const int maximum_y = input_height + pad_top - filter_half_height + pad_bottom - filter_half_height;
Giorgio Arena93a690e2017-08-01 16:09:33 +010077
78 int out_pos = 0;
Giorgio Arena9fe41442017-08-23 16:36:24 +010079 for(int r = 0; r < num_batches; ++r)
Giorgio Arena93a690e2017-08-01 16:09:33 +010080 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010081 for(int z = 0; z < input_depth; ++z)
Giorgio Arena93a690e2017-08-01 16:09:33 +010082 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010083 for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second)
Giorgio Arena93a690e2017-08-01 16:09:33 +010084 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010085 for(int x = minimum_x; x < minimum_x + maximum_x; x += conv_info.stride().first)
Giorgio Arena93a690e2017-08-01 16:09:33 +010086 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010087 Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r));
88 size_t filter_offset = filter_plane * z;
89
Frank Lei8cdfdb82018-01-02 16:49:33 +080090 T val(0);
Giorgio Arena9fe41442017-08-23 16:36:24 +010091 for(int j = y - filter_half_height; j <= static_cast<int>(y + filter_half_height); ++j)
Giorgio Arena93a690e2017-08-01 16:09:33 +010092 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010093 for(int i = x - filter_half_width; i <= static_cast<int>(x + filter_half_width); ++i)
94 {
95 coords.set(0, i);
96 coords.set(1, j);
Frank Lei8cdfdb82018-01-02 16:49:33 +080097 T border_value(0);
98 val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, border_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +010099 ++filter_offset;
100 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100101 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100102 coords.set(0, x);
103 coords.set(1, y);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700104 dst[out_pos++] = saturate_cast<T>(val + *static_cast<const TB *>(biases(Coordinates(z))));
Giorgio Arena93a690e2017-08-01 16:09:33 +0100105 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100106 }
107 }
108 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100109}
110
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000111void depthwise_convolution_nchw(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, SimpleTensor<uint8_t> &dst, const PadStrideInfo &conv_info)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700112{
113 // Create reference
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700114 const int input_offset = -src.quantization_info().offset;
115 const float input_scale = src.quantization_info().scale;
116 const int weights_offset = -weights.quantization_info().offset;
117 const float weights_scale = weights.quantization_info().scale;
118 const int output_offset = dst.quantization_info().offset;
119 const float output_scale = dst.quantization_info().scale;
120
121 int output_multiplier;
122 int output_shift;
123 const float multiplier = input_scale * weights_scale / output_scale;
124 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
125
126 // Compute reference
127 const int filter_width = weights.shape().x();
128 const int filter_height = weights.shape().y();
129 const int filter_plane = filter_width * filter_height;
130 const int input_width = src.shape().x();
131 const int input_height = src.shape().y();
132 const int input_depth = src.shape().z();
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000133 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700134
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000135 const int filter_half_width = filter_width / 2;
136 const int filter_half_height = filter_height / 2;
137
Georgios Pinitas15997872018-02-19 13:58:22 +0000138 const int pad_left = conv_info.pad_left();
139 const int pad_top = conv_info.pad_top();
140 const int pad_right = conv_info.pad_right();
141 const int pad_bottom = conv_info.pad_bottom();
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000142
143 const int minimum_x = -pad_left + filter_half_width;
144 const int minimum_y = -pad_top + filter_half_height;
145 const int maximum_x = input_width + pad_left - filter_half_width + pad_right - filter_half_width;
146 const int maximum_y = input_height + pad_top - filter_half_height + pad_bottom - filter_half_height;
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700147
148 int out_pos = 0;
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000149 for(int r = 0; r < num_batches; ++r)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700150 {
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000151 for(int z = 0; z < input_depth; ++z)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700152 {
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000153 int32_t bias_val = *static_cast<const int32_t *>(biases(Coordinates(z)));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000154 for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700155 {
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000156 for(int x = minimum_x; x < minimum_x + maximum_x; x += conv_info.stride().first)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700157 {
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000158 Coordinates coords(x, y, z, r);
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000159 int filter_offset = filter_plane * z;
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700160
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000161 int32_t val = 0;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000162 for(int j = y - filter_half_height; j <= (y + filter_half_height); ++j)
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000163 {
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000164 for(int i = x - filter_half_width; i <= (x + filter_half_width); ++i)
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000165 {
166 coords.set(0, i);
167 coords.set(1, j);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000168 const auto in_val = tensor_elem_at<uint8_t>(src, coords, BorderMode::CONSTANT, -input_offset);
169 const uint8_t w_val = *(weights.data() + filter_offset);
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000170 val += (in_val + input_offset) * (w_val + weights_offset);
171 ++filter_offset;
172 }
173 }
174 val += bias_val;
175 val = asymm_rounding_divide_by_pow2(asymm_int_mult(val, output_multiplier), output_shift);
176 val += output_offset;
177 val = std::max<int32_t>(val, 0);
178 val = std::min<int32_t>(val, 255);
179
180 // Store the result
181 dst[out_pos++] = val;
182 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700183 }
184 }
185 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000186}
187
188template <>
189SimpleTensor<uint8_t> depthwise_convolution(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &biases, const TensorShape &dst_shape,
190 const PadStrideInfo &conv_info)
191{
192 SimpleTensor<uint8_t> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() };
193
194 if(src.data_layout() == DataLayout::NHWC)
195 {
196 SimpleTensor<uint8_t> src_nchw = reference::permute<uint8_t>(src, PermutationVector(1U, 2U, 0U));
197 SimpleTensor<uint8_t> weights_nchw = reference::permute<uint8_t>(weights, PermutationVector(1U, 2U, 0U));
198 SimpleTensor<uint8_t> dst_nchw = reference::permute<uint8_t>(dst, PermutationVector(1U, 2U, 0U));
199
200 depthwise_convolution_nchw(src_nchw, weights_nchw, biases, dst_nchw, conv_info);
201
202 return reference::permute<uint8_t>(dst_nchw, PermutationVector(2U, 0U, 1U));
203 }
204
205 depthwise_convolution_nchw(src, weights, biases, dst, conv_info);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700206
207 return dst;
208}
209
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100210template <typename T, typename TB>
211SimpleTensor<T> depthwise_convolution(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info)
212{
213 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position() };
214
215 if(src.data_layout() == DataLayout::NHWC && src.data_type() == DataType::F32)
216 {
217 SimpleTensor<T> src_nchw = reference::permute<T>(src, PermutationVector(1U, 2U, 0U));
218 SimpleTensor<T> weights_nchw = reference::permute<T>(weights, PermutationVector(1U, 2U, 0U));
219 SimpleTensor<T> dst_nchw = reference::permute<T>(dst, PermutationVector(1U, 2U, 0U));
220
221 depthwise_convolution_nchw<T, TB>(src_nchw, weights_nchw, biases, dst_nchw, conv_info);
222
223 return reference::permute<T>(dst_nchw, PermutationVector(2U, 0U, 1U));
224 }
225
226 depthwise_convolution_nchw<T, TB>(src, weights, biases, dst, conv_info);
227
228 return dst;
229}
230
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100231template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape,
232 const PadStrideInfo &conv_info);
Frank Lei8cdfdb82018-01-02 16:49:33 +0800233
234template SimpleTensor<half> depthwise_convolution(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &biases, const TensorShape &dst_shape,
235 const PadStrideInfo &conv_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100236} // namespace reference
237} // namespace validation
238} // namespace test
239} // namespace arm_compute