blob: aafdb2e8a47950de1a14ed3ad7208a4c820261be [file] [log] [blame]
Giorgio Arena44f55722019-07-12 14:49:49 +01001/*
2 * Copyright (c) 2019 ARM Limited.
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 */
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010024#include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h"
Giorgio Arena44f55722019-07-12 14:49:49 +010025
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/NEON/wrapper/traits.h"
28#include "arm_compute/core/NEON/wrapper/wrapper.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30
31namespace arm_compute
32{
33namespace
34{
35template <typename T, int S, bool has_biases>
36void depthwise_loop_multiplier1(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
37 const Size2D &dilation, const Window &window)
38{
39 using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
40 using TagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
41
42 const size_t input_stride_y = input->info()->strides_in_bytes().y();
43 const size_t input_stride_z = input->info()->strides_in_bytes().z();
44 const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) *
45 input->info()->strides_in_bytes().y();
46 const size_t weights_width = weights->info()->dimension(1);
47 const size_t weights_height = weights->info()->dimension(2);
48 const size_t weights_stride_y = weights->info()->strides_in_bytes().y();
49 const size_t weights_stride_z = weights->info()->strides_in_bytes().z();
50 const size_t conv_stride_x = conv_info.stride().first;
51 const size_t conv_stride_y = conv_info.stride().second;
52 const size_t conv_pad_left = conv_info.pad_left();
53 const size_t conv_pad_top = conv_info.pad_top();
54
55 Window win_input = window;
56 win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
57 win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));
58
59 Window win_weights = win_input;
60 win_weights.set(3, Window::Dimension(0, 0, 0));
61
62 Iterator input_it(input, win_input);
63 Iterator weights_it(weights, win_weights);
64 Iterator output_it(output, window);
65 Iterator biases_it{};
66
67 if(has_biases)
68 {
69 biases_it = Iterator(biases, win_weights);
70 }
71
72 execute_window_loop(window, [&](const Coordinates & id)
73 {
74 VectorType acc = wrapper::vdup_n(static_cast<T>(0), TagType{});
75
76 const int input_y = id.y() * conv_stride_x - conv_pad_left;
77 const int input_z = id.z() * conv_stride_y - conv_pad_top;
78 int input_offset = input_y * input_stride_y + input_z * input_stride_z;
79
80 auto weights_ptr = weights_it.ptr();
81 for(size_t h = 0; h < weights_height; ++h)
82 {
83 int offs = input_offset;
84 for(size_t w = 0; w < weights_width; ++w)
85 {
86 const auto input_vals = wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), input_max_offset)));
87 const auto weights_vals = wrapper::vload(reinterpret_cast<T *>(weights_ptr + w * weights_stride_y));
88
89 acc = wrapper::vmla(acc, weights_vals, input_vals);
90 offs += dilation.x() * input_stride_y;
91 }
92
93 weights_ptr += weights_stride_z;
94 input_offset += dilation.y() * input_stride_z;
95 }
96
97 if(has_biases)
98 {
99 const auto biases_vals = wrapper::vload(reinterpret_cast<T *>(biases_it.ptr()));
100 acc = wrapper::vadd(acc, biases_vals);
101 }
102
103 wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()), acc);
104 },
105 input_it, weights_it, biases_it, output_it);
106}
107
108template <typename T, bool has_biases>
109void depthwise_loop_generic(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
110 const Size2D &dilation, unsigned int depth_multiplier, const Window &window)
111{
112 const size_t input_stride_y = input->info()->strides_in_bytes().y();
113 const size_t input_stride_z = input->info()->strides_in_bytes().z();
114 const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) *
115 input->info()->strides_in_bytes().y();
116 const size_t weights_width = weights->info()->dimension(1);
117 const size_t weights_height = weights->info()->dimension(2);
118 const size_t weights_stride_y = weights->info()->strides_in_bytes().y();
119 const size_t weights_stride_z = weights->info()->strides_in_bytes().z();
120 const size_t conv_stride_x = conv_info.stride().first;
121 const size_t conv_stride_y = conv_info.stride().second;
122 const size_t conv_pad_left = conv_info.pad_left();
123 const size_t conv_pad_top = conv_info.pad_top();
124
125 Window win_input = window;
126 win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
127 win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));
128
129 Window win_weights = win_input;
130 win_weights.set(3, Window::Dimension(0, 0, 0));
131
132 win_input.set_dimension_step(Window::DimX, 1);
133
134 Iterator input_it(input, win_input);
135 Iterator weights_it(weights, win_weights);
136 Iterator output_it(output, window);
137 Iterator biases_it{};
138
139 if(has_biases)
140 {
141 biases_it = Iterator(biases, win_weights);
142 }
143
144 execute_window_loop(window, [&](const Coordinates & id)
145 {
146 std::vector<T> acc(depth_multiplier, static_cast<T>(0));
147
148 const int input_y = id.y() * conv_stride_x - conv_pad_left;
149 const int input_z = id.z() * conv_stride_y - conv_pad_top;
150 int input_offset = input_y * input_stride_y + input_z * input_stride_z;
151
152 auto weights_ptr = weights_it.ptr();
153 for(size_t h = 0; h < weights_height; ++h)
154 {
155 int offs = input_offset;
156 for(size_t w = 0; w < weights_width; ++w)
157 {
158 const auto input_val = *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), input_max_offset)));
159
160 for(size_t m = 0; m < depth_multiplier; ++m)
161 {
162 const auto weights_val = *(reinterpret_cast<T *>(weights_ptr + m * sizeof(T) + w * weights_stride_y));
163 acc.at(m) = std::fma(weights_val, input_val, acc.at(m));
164 }
165
166 offs += dilation.x() * input_stride_y;
167 }
168
169 weights_ptr += weights_stride_z;
170 input_offset += dilation.y() * input_stride_z;
171 }
172
173 if(has_biases)
174 {
175 for(size_t m = 0; m < depth_multiplier; ++m)
176 {
177 const auto biases_val = *(reinterpret_cast<T *>(biases_it.ptr() + m * sizeof(T)));
178 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m) + biases_val;
179 }
180 }
181 else
182 {
183 for(size_t m = 0; m < depth_multiplier; ++m)
184 {
185 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m);
186 }
187 }
188 },
189 input_it, weights_it, biases_it, output_it);
190}
191
192Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
193 const Size2D &dilation)
194{
195 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
196 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
197 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
198 ARM_COMPUTE_RETURN_ERROR_ON(depth_multiplier == 0);
199 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(0) * depth_multiplier) != weights->dimension(0));
200 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
201 ARM_COMPUTE_RETURN_ERROR_ON((conv_info.stride().first < 1) || (conv_info.stride().second < 1));
202
203 if(biases != nullptr)
204 {
205 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
206 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0));
207 }
208
209 if(output->total_size() != 0)
210 {
211 const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
212 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
213 }
214
215 return Status{};
216}
217} // namespace
218
219std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases,
220 ITensorInfo *output, const PadStrideInfo &conv_info,
221 unsigned int depth_multiplier, const Size2D &dilation)
222{
223 // Get convolved dimensions
224 const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
225
226 // Output auto inizialitation if not yet initialized
227 auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
228
229 // Configure kernel window (generic)
230 const unsigned int num_elems_read_per_iteration = (depth_multiplier == 1) ? 8 / element_size_from_data_type(input->data_type()) : 1;
231 const unsigned int num_elems_written_per_iteration = num_elems_read_per_iteration * depth_multiplier;
232
233 // Configure kernel window
234 Window win = calculate_max_window(*output, Steps(num_elems_written_per_iteration));
235
236 AccessWindowStatic input_access(input, 0, -conv_info.pad_left(), ceil_to_multiple(num_elems_read_per_iteration, input->dimension(0)),
237 input->dimension(1) + std::max(std::max(conv_info.pad_right(), conv_info.pad_bottom()), conv_info.pad_top()));
238 AccessWindowHorizontal weights_access(weights, 0, num_elems_written_per_iteration);
239 AccessWindowHorizontal output_access(output, 0, num_elems_written_per_iteration);
240
241 bool window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
242
243 if(biases != nullptr)
244 {
245 AccessWindowHorizontal biases_access(biases, 0, num_elems_written_per_iteration);
246 window_changed |= update_window_and_padding(win, biases_access);
247 }
248
249 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
250
251 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
252 return std::make_pair(err, win);
253}
254
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100255NEDepthwiseConvolutionLayerNativeKernel::NEDepthwiseConvolutionLayerNativeKernel()
Giorgio Arena44f55722019-07-12 14:49:49 +0100256 : _func(), _border_size(0), _input(), _weights(), _biases(), _output(), _conv_info(), _depth_multiplier(1), _dilation()
257{
258}
259
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100260BorderSize NEDepthwiseConvolutionLayerNativeKernel::border_size() const
Giorgio Arena44f55722019-07-12 14:49:49 +0100261{
262 return _border_size;
263}
264
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100265void NEDepthwiseConvolutionLayerNativeKernel::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
266 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
Giorgio Arena44f55722019-07-12 14:49:49 +0100267{
268 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
269 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, dilation));
270
271 _input = input;
272 _weights = weights;
273 _biases = biases;
274 _output = output;
275 _conv_info = conv_info;
276 _depth_multiplier = depth_multiplier;
277 _border_size = BorderSize(_conv_info.pad_left(), 0, std::max(std::max(conv_info.pad_right(), conv_info.pad_bottom()), conv_info.pad_top()), 0);
278 _dilation = dilation;
279
280 switch(_input->info()->data_type())
281 {
282 case DataType::F32:
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100283 _func = (biases != nullptr) ? &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float, 2, true> : &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float, 2, false>;
Giorgio Arena44f55722019-07-12 14:49:49 +0100284 break;
285 default:
286 ARM_COMPUTE_ERROR("Data type not supported");
287 break;
288 }
289
290 auto win_config = validate_and_configure_window(_input->info(), _weights->info(), (biases != nullptr) ? biases->info() : nullptr, _output->info(), _conv_info, _depth_multiplier, dilation);
291 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
292 INEKernel::configure(win_config.second);
293}
294
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100295Status NEDepthwiseConvolutionLayerNativeKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
296 unsigned int depth_multiplier,
297 const Size2D &dilation)
Giorgio Arena44f55722019-07-12 14:49:49 +0100298{
299 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, depth_multiplier, dilation));
300 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), (biases != nullptr) ? biases->clone().get() : nullptr, output->clone().get(), conv_info,
301 depth_multiplier, dilation)
302 .first);
303 return Status{};
304}
305
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100306void NEDepthwiseConvolutionLayerNativeKernel::run(const Window &window, const ThreadInfo &info)
Giorgio Arena44f55722019-07-12 14:49:49 +0100307{
308 ARM_COMPUTE_UNUSED(info);
309 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
310 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
311
312 (this->*_func)(window);
313}
314
315template <typename T, int S, bool has_biases>
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100316void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window)
Giorgio Arena44f55722019-07-12 14:49:49 +0100317{
318 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
319 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
320
321 if(_depth_multiplier == 1)
322 {
323 depthwise_loop_multiplier1<T, S, has_biases>(_input, _weights, _biases, _output, _conv_info, _dilation, window);
324 }
325 else
326 {
327 depthwise_loop_generic<T, has_biases>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, window);
328 }
329}
330} // namespace arm_compute