blob: 87315909d87b7469c71458db3aabff47e97d1f5f [file] [log] [blame]
Giorgio Arena44f55722019-07-12 14:49:49 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Giorgio Arena44f55722019-07-12 14:49:49 +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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h"
Giorgio Arena44f55722019-07-12 14:49:49 +010025
Giorgio Arena44f55722019-07-12 14:49:49 +010026#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arenad93e2632019-10-15 11:09:33 +010027#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010028#include "src/core/AccessWindowStatic.h"
29#include "src/core/CPP/Validate.h"
Giorgio Arenad93e2632019-10-15 11:09:33 +010030#include "src/core/NEON/kernels/convolution/depthwise/impl_qa8_qa8.hpp"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010031#include "src/core/NEON/wrapper/traits.h"
32#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/ToolchainSupport.h"
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +010036
Giorgio Arena44f55722019-07-12 14:49:49 +010037namespace arm_compute
38{
39namespace
40{
Sang-Hoon Parke4558b52020-10-01 10:13:07 +010041constexpr auto data_layout = DataLayout::NHWC;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +010042const size_t width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
43const size_t height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
44const size_t channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
45
46constexpr auto dim_manual_loop = Window::Dimension(0, 0, 0);
47constexpr auto dim_single_unit_step = Window::Dimension(0, 1, 1);
48constexpr size_t vector_size = 8;
49
50struct DepthwiseConvolutionRunInfo
Giorgio Arenad93e2632019-10-15 11:09:33 +010051{
Sang-Hoon Parke4558b52020-10-01 10:13:07 +010052 const size_t num_read_elements_per_iteration;
53 const uint32_t x_start;
54 const uint32_t x_end;
55 const uint32_t x_step;
56 const uint32_t x_leftover_start;
57 const size_t input_stride_y;
58 const size_t input_stride_z;
59 const size_t input_max_offset;
60 const size_t weights_width;
61 const size_t weights_height;
62 const size_t weights_stride_y;
63 const size_t weights_stride_z;
64 const size_t conv_stride_x;
65 const size_t conv_stride_y;
66 const size_t conv_pad_left;
67 const size_t conv_pad_top;
68 const size_t input_height;
69 const size_t input_width;
70 const size_t input_depth;
71
72 DepthwiseConvolutionRunInfo(const ITensorInfo &input, const ITensorInfo &weights, const PadStrideInfo &conv_info, const Window &w, uint32_t depth_multiplier = 1)
73 : num_read_elements_per_iteration((depth_multiplier == 1 ? (vector_size / element_size_from_data_type(input.data_type())) : 1)),
74 x_start(w.x().start()),
75 x_end(w.x().end()),
76 x_step(static_cast<uint32_t>(num_read_elements_per_iteration * depth_multiplier)),
77 x_leftover_start(std::max(static_cast<int32_t>(w.x().end()) - static_cast<int32_t>(x_step) + 1, int32_t(0))),
78 input_stride_y(input.strides_in_bytes().y()),
79 input_stride_z(input.strides_in_bytes().z()),
80 input_max_offset(input.strides_in_bytes().z() * input.dimension(height_idx) - (input.padding().bottom + input.padding().top) * input.strides_in_bytes().y()),
81 weights_width(weights.dimension(width_idx)),
82 weights_height(weights.dimension(height_idx)),
83 weights_stride_y(weights.strides_in_bytes().y()),
84 weights_stride_z(weights.strides_in_bytes().z()),
85 conv_stride_x(conv_info.stride().first),
86 conv_stride_y(conv_info.stride().second),
87 conv_pad_left(conv_info.pad_left()),
88 conv_pad_top(conv_info.pad_top()),
89 input_height(input.dimension(height_idx)),
90 input_width(input.dimension(width_idx)),
91 input_depth(input.dimension(channel_idx))
Giorgio Arenad93e2632019-10-15 11:09:33 +010092 {
Giorgio Arenad93e2632019-10-15 11:09:33 +010093 }
Sang-Hoon Parke4558b52020-10-01 10:13:07 +010094};
95
96inline bool is_valid_input_region(int32_t base_w, uint32_t base_h, uint32_t w, uint32_t h, const DepthwiseConvolutionRunInfo &run_info, const Size2D &dilation)
97{
98 const int32_t current_h = base_h + h * dilation.y();
99 const bool is_valid_h = current_h >= 0 && current_h < static_cast<int32_t>(run_info.input_height);
100
101 const int32_t current_w = base_w + w * dilation.x();
102 const bool is_valid_w = current_w >= 0 && current_w < static_cast<int32_t>(run_info.input_width);
103
104 return is_valid_h && is_valid_w;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100105}
106
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100107template <typename T>
Giorgio Arenad93e2632019-10-15 11:09:33 +0100108void depthwise_loop_multiplier1_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Michalis Spyrouf401c742020-05-12 16:18:33 +0100109 const Size2D &dilation, const Window &window, bool has_biases)
Giorgio Arena44f55722019-07-12 14:49:49 +0100110{
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100111 constexpr auto element_per_vector = vector_size / sizeof(T);
112 using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type;
113 using TagType = typename wrapper::traits::neon_vector<T, element_per_vector>::tag_type;
Giorgio Arena44f55722019-07-12 14:49:49 +0100114
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100115 const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window);
116
117 const VectorType zero_vector = wrapper::vdup_n(static_cast<T>(0), TagType{});
118
119 Window execution_window = window;
120 execution_window.set(Window::DimX, dim_single_unit_step);
Giorgio Arena44f55722019-07-12 14:49:49 +0100121
122 Window win_input = window;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100123 win_input.set(Window::DimX, dim_manual_loop);
124 win_input.set(Window::DimY, dim_manual_loop);
125 win_input.set(Window::DimZ, dim_manual_loop);
Giorgio Arena44f55722019-07-12 14:49:49 +0100126
127 Window win_weights = win_input;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100128 win_weights.set(Window::DimW, dim_manual_loop);
129
130 Window win_output = window;
131 win_output.set(Window::DimX, dim_manual_loop);
Giorgio Arena44f55722019-07-12 14:49:49 +0100132
133 Iterator input_it(input, win_input);
134 Iterator weights_it(weights, win_weights);
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100135 Iterator output_it(output, win_output);
Giorgio Arena44f55722019-07-12 14:49:49 +0100136 Iterator biases_it{};
137
138 if(has_biases)
139 {
140 biases_it = Iterator(biases, win_weights);
141 }
142
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100143 execute_window_loop(execution_window, [&](const Coordinates & id)
Giorgio Arena44f55722019-07-12 14:49:49 +0100144 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100145 const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left;
146 const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top;
147 const int64_t base_input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z;
Giorgio Arena44f55722019-07-12 14:49:49 +0100148
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100149 auto const base_weights_ptr = weights_it.ptr();
150 uint32_t x = run_info.x_start;
Giorgio Arena44f55722019-07-12 14:49:49 +0100151
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100152 for(; x < run_info.x_leftover_start; x += run_info.x_step)
Giorgio Arena44f55722019-07-12 14:49:49 +0100153 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100154 VectorType acc = zero_vector;
155 auto weights_ptr = base_weights_ptr;
156 int64_t input_offset = base_input_offset;
Giorgio Arena44f55722019-07-12 14:49:49 +0100157
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100158 for(uint32_t h = 0; h < run_info.weights_height; ++h)
159 {
160 int64_t offs = input_offset + x * sizeof(T);
161 for(uint32_t w = 0; w < run_info.weights_width; ++w)
162 {
163 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
164 const auto input_vals = is_valid_region ?
165 wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) :
166 zero_vector;
167 const auto weights_vals = wrapper::vload(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x);
168 acc = wrapper::vmla(acc, weights_vals, input_vals);
169
170 offs += dilation.x() * run_info.input_stride_y;
171 }
172
173 weights_ptr += run_info.weights_stride_z;
174 input_offset += dilation.y() * run_info.input_stride_z;
Giorgio Arena44f55722019-07-12 14:49:49 +0100175 }
176
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100177 if(has_biases)
178 {
179 const auto biases_vals = wrapper::vload(reinterpret_cast<T *>(biases_it.ptr()) + x);
180 acc = wrapper::vadd(acc, biases_vals);
181 }
182
183 wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, acc);
Giorgio Arena44f55722019-07-12 14:49:49 +0100184 }
185
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100186 for(; x < run_info.x_end; ++x)
Giorgio Arena44f55722019-07-12 14:49:49 +0100187 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100188 auto acc_scalar = T{ 0 };
189 auto weights_ptr = base_weights_ptr;
190 int64_t input_offset = base_input_offset;
Giorgio Arena44f55722019-07-12 14:49:49 +0100191
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100192 for(size_t h = 0; h < run_info.weights_height; ++h)
193 {
194 int64_t offs = input_offset + x * sizeof(T);
195 for(size_t w = 0; w < run_info.weights_width; ++w)
196 {
197 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
198 const auto input_vals = is_valid_region ? *reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset)) : 0;
199 const auto weights_vals = *(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x);
200
201 acc_scalar += (input_vals * weights_vals);
202
203 offs += dilation.x() * run_info.input_stride_y;
204 }
205
206 weights_ptr += run_info.weights_stride_z;
207 input_offset += dilation.y() * run_info.input_stride_z;
208 }
209
210 if(has_biases)
211 {
212 const auto biases_vals = *(reinterpret_cast<T *>(biases_it.ptr()) + x);
213 acc_scalar += biases_vals;
214 }
215 *(reinterpret_cast<T *>(output_it.ptr()) + x) = acc_scalar;
216 }
Giorgio Arena44f55722019-07-12 14:49:49 +0100217 },
218 input_it, weights_it, biases_it, output_it);
219}
220
Michalis Spyrouf401c742020-05-12 16:18:33 +0100221template <typename T>
Giorgio Arenad93e2632019-10-15 11:09:33 +0100222void depthwise_loop_generic_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Michalis Spyrouf401c742020-05-12 16:18:33 +0100223 const Size2D &dilation, unsigned int depth_multiplier, const Window &window, bool has_biases)
Giorgio Arena44f55722019-07-12 14:49:49 +0100224{
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100225 const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window, depth_multiplier);
Giorgio Arena44f55722019-07-12 14:49:49 +0100226
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100227 Window execution_window = window;
228 execution_window.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1));
Giorgio Arena44f55722019-07-12 14:49:49 +0100229
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100230 Window win_input = execution_window;
231 win_input.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1));
232 win_input.set(Window::DimY, dim_manual_loop);
233 win_input.set(Window::DimZ, dim_manual_loop);
Giorgio Arena44f55722019-07-12 14:49:49 +0100234
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100235 Window win_weights = window;
236 win_weights.set_dimension_step(Window::DimX, run_info.x_step);
237 win_weights.set(Window::DimY, dim_manual_loop);
238 win_weights.set(Window::DimZ, dim_manual_loop);
239 win_weights.set(Window::DimW, dim_manual_loop);
240
241 Window win_output = window;
242 win_output.set_dimension_step(Window::DimX, run_info.x_step);
Giorgio Arena44f55722019-07-12 14:49:49 +0100243
244 Iterator input_it(input, win_input);
245 Iterator weights_it(weights, win_weights);
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100246 Iterator output_it(output, win_output);
Giorgio Arena44f55722019-07-12 14:49:49 +0100247 Iterator biases_it{};
248
249 if(has_biases)
250 {
251 biases_it = Iterator(biases, win_weights);
252 }
253
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100254 execute_window_loop(execution_window, [&](const Coordinates & id)
Giorgio Arena44f55722019-07-12 14:49:49 +0100255 {
256 std::vector<T> acc(depth_multiplier, static_cast<T>(0));
257
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100258 const int input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left;
259 const int input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top;
260 int input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z;
Giorgio Arena44f55722019-07-12 14:49:49 +0100261
262 auto weights_ptr = weights_it.ptr();
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100263 for(size_t h = 0; h < run_info.weights_height; ++h)
Giorgio Arena44f55722019-07-12 14:49:49 +0100264 {
265 int offs = input_offset;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100266 for(size_t w = 0; w < run_info.weights_width; ++w)
Giorgio Arena44f55722019-07-12 14:49:49 +0100267 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100268 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
269 const auto input_val = is_valid_region ? *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : T(0);
Giorgio Arena44f55722019-07-12 14:49:49 +0100270
271 for(size_t m = 0; m < depth_multiplier; ++m)
272 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100273 const auto weights_val = *(reinterpret_cast<T *>(weights_ptr + m * sizeof(T) + w * run_info.weights_stride_y));
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100274 acc.at(m) = support::cpp11::fma(weights_val, input_val, acc.at(m));
Giorgio Arena44f55722019-07-12 14:49:49 +0100275 }
276
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100277 offs += dilation.x() * run_info.input_stride_y;
Giorgio Arena44f55722019-07-12 14:49:49 +0100278 }
279
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100280 weights_ptr += run_info.weights_stride_z;
281 input_offset += dilation.y() * run_info.input_stride_z;
Giorgio Arena44f55722019-07-12 14:49:49 +0100282 }
283
284 if(has_biases)
285 {
286 for(size_t m = 0; m < depth_multiplier; ++m)
287 {
288 const auto biases_val = *(reinterpret_cast<T *>(biases_it.ptr() + m * sizeof(T)));
289 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m) + biases_val;
290 }
291 }
292 else
293 {
294 for(size_t m = 0; m < depth_multiplier; ++m)
295 {
296 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m);
297 }
298 }
299 },
300 input_it, weights_it, biases_it, output_it);
301}
302
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100303template <typename T, typename TW>
Giorgio Arenad93e2632019-10-15 11:09:33 +0100304void depthwise_loop_multiplier1_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Michalis Spyrouf401c742020-05-12 16:18:33 +0100305 const Size2D &dilation, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100306{
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100307 constexpr auto element_per_vector = vector_size / sizeof(T);
308 using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type;
309 using TagType = typename wrapper::traits::neon_vector<T, element_per_vector>::tag_type;
310 using AccType = int32_t;
311 using AccArrayType = std::array<AccType, element_per_vector>;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100312
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100313 const auto out_of_bound_value = PixelValue(static_cast<uint64_t>(0), input->info()->data_type(), input->info()->quantization_info()).get<T>();
314 const auto out_of_bound_vector = wrapper::vdup_n(static_cast<T>(out_of_bound_value), TagType{});
315
316 const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100317
318 const int32_t input_qoffset = input->info()->quantization_info().uniform().offset;
319 const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset;
320 const int32_t output_qoffset = output->info()->quantization_info().uniform().offset;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100321 const int32_t k_offset = run_info.weights_width * run_info.weights_height * input_qoffset * weights_qoffset;
322
323 Window execution_window = window;
324 execution_window.set(Window::DimX, dim_single_unit_step);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100325
326 Window win_input = window;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100327 win_input.set(Window::DimX, dim_manual_loop);
328 win_input.set(Window::DimY, dim_manual_loop);
329 win_input.set(Window::DimZ, dim_manual_loop);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100330
331 Window win_weights = win_input;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100332 win_weights.set(Window::DimW, dim_manual_loop);
333
334 Window win_output = window;
335 win_output.set(Window::DimX, dim_manual_loop);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100336
337 Iterator input_it(input, win_input);
338 Iterator weights_it(weights, win_weights);
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100339 Iterator output_it(output, win_output);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100340 Iterator biases_it{};
341
342 if(has_biases)
343 {
344 biases_it = Iterator(biases, win_weights);
345 }
346
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100347 execute_window_loop(execution_window, [&](const Coordinates & id)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100348 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100349 const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left;
350 const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top;
351 const int64_t base_input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z;
352 auto const base_weights_ptr = weights_it.ptr();
353 size_t x = run_info.x_start;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100354
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100355 for(; x < run_info.x_leftover_start; x += run_info.x_step)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100356 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100357 AccArrayType acc{};
358 AccArrayType in_sum{};
359 AccArrayType we_sum{};
Giorgio Arenad93e2632019-10-15 11:09:33 +0100360
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100361 auto weights_ptr = base_weights_ptr;
362 auto input_offset = base_input_offset;
363
364 for(size_t h = 0; h < run_info.weights_height; ++h)
365 {
366 int64_t offs = input_offset + x * sizeof(T);
367 for(size_t w = 0; w < run_info.weights_width; ++w)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100368 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100369 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
370 const auto input_vals = is_valid_region ?
371 wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) :
372 out_of_bound_vector;
373 const auto weights_vals = wrapper::vload(reinterpret_cast<TW *>(weights_ptr + w * run_info.weights_stride_y) + x);
374
Sang-Hoon Park1a0a4bc2020-11-12 17:41:32 +0000375 for(size_t i = 0; i < element_per_vector; ++i)
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100376 {
377 acc.at(i) += input_vals[i] * weights_vals[i];
378 in_sum.at(i) += input_vals[i];
379 we_sum.at(i) += weights_vals[i];
380 }
381
382 offs += dilation.x() * run_info.input_stride_y;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100383 }
384
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100385 weights_ptr += run_info.weights_stride_z;
386 input_offset += dilation.y() * run_info.input_stride_z;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100387 }
388
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100389 VectorType out_vals = wrapper::vdup_n(static_cast<T>(0), TagType{});
Sang-Hoon Park1a0a4bc2020-11-12 17:41:32 +0000390 for(size_t i = 0; i < element_per_vector; ++i)
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100391 {
392 acc.at(i) -= in_sum.at(i) * weights_qoffset;
393 acc.at(i) -= we_sum.at(i) * input_qoffset;
394 acc.at(i) += k_offset;
395
396 if(has_biases)
397 {
398 acc.at(i) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + i * sizeof(int32_t)) + x);
399 }
400
401 const int32_t out_mul = output_multiplier.at(x + i);
402 const int32_t out_shift = output_shift.at(x + i);
403 if(out_shift < 0)
404 {
405 acc.at(i) = saturating_doubling_high_mul(acc.at(i) * (1 << (-out_shift)), out_mul) + output_qoffset;
406 }
407 else
408 {
409 acc.at(i) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(i), out_mul), out_shift) + output_qoffset;
410 }
411 out_vals[i] = static_cast<T>(utility::clamp<AccType, T>(acc.at(i)));
412 }
413
414 wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, out_vals);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100415 }
416
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100417 // left-over
418 for(; x < run_info.x_end; ++x)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100419 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100420 AccType acc = 0;
421 AccType in_sum = 0;
422 AccType we_sum = 0;
423
424 auto weights_ptr = base_weights_ptr;
425 auto input_offset = base_input_offset;
426
427 for(size_t h = 0; h < run_info.weights_height; ++h)
428 {
429 int64_t offs = input_offset + x * sizeof(T);
430 for(size_t w = 0; w < run_info.weights_width; ++w)
431 {
432 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
433 const auto input_val = is_valid_region ?
434 *reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset)) :
435 out_of_bound_value;
436 const auto weights_val = *(reinterpret_cast<TW *>(weights_ptr + w * run_info.weights_stride_y) + x);
437
438 acc += input_val * weights_val;
439 in_sum += input_val;
440 we_sum += weights_val;
441
442 offs += dilation.x() * run_info.input_stride_y;
443 }
444
445 weights_ptr += run_info.weights_stride_z;
446 input_offset += dilation.y() * run_info.input_stride_z;
447 }
448
449 T out_vals{ 0 };
450
451 acc -= in_sum * weights_qoffset;
452 acc -= we_sum * input_qoffset;
453 acc += k_offset;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100454
455 if(has_biases)
456 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100457 acc += *(reinterpret_cast<int32_t *>(biases_it.ptr()) + x);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100458 }
459
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100460 const int32_t out_mul = output_multiplier.at(x);
461 const int32_t out_shift = output_shift.at(x);
462
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000463 if(out_shift < 0)
464 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100465 acc = saturating_doubling_high_mul(acc * (1 << (-out_shift)), out_mul) + output_qoffset;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000466 }
467 else
468 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100469 acc = rounding_divide_by_exp2(saturating_doubling_high_mul(acc, out_mul), out_shift) + output_qoffset;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000470 }
Giorgio Arenad93e2632019-10-15 11:09:33 +0100471
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100472 out_vals = static_cast<T>(utility::clamp<AccType, T>(acc));
473 *(reinterpret_cast<T *>(output_it.ptr()) + x) = out_vals;
474 }
Giorgio Arenad93e2632019-10-15 11:09:33 +0100475 },
476 input_it, weights_it, biases_it, output_it);
477}
478
Michalis Spyrouf401c742020-05-12 16:18:33 +0100479template <typename T, typename TW>
Giorgio Arenad93e2632019-10-15 11:09:33 +0100480void depthwise_loop_generic_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Michalis Spyrouf401c742020-05-12 16:18:33 +0100481 const Size2D &dilation, unsigned int depth_multiplier, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100482{
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100483 using AccType = int32_t;
484
485 const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window, depth_multiplier);
486
487 const auto out_of_bound_value = PixelValue(static_cast<uint64_t>(0), input->info()->data_type(), input->info()->quantization_info()).get<T>();
Giorgio Arenad93e2632019-10-15 11:09:33 +0100488
489 const int32_t input_qoffset = input->info()->quantization_info().uniform().offset;
490 const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset;
491 const int32_t output_qoffset = output->info()->quantization_info().uniform().offset;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100492 const int32_t k_offset = run_info.weights_width * run_info.weights_height * input_qoffset * weights_qoffset;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100493
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100494 Window execution_window = window;
495 execution_window.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1));
Giorgio Arenad93e2632019-10-15 11:09:33 +0100496
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100497 Window win_input = execution_window;
498 win_input.set(Window::DimY, dim_manual_loop);
499 win_input.set(Window::DimZ, dim_manual_loop);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100500
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100501 Window win_weights = window;
502 win_weights.set_dimension_step(Window::DimX, run_info.x_step);
503 win_weights.set(Window::DimY, dim_manual_loop);
504 win_weights.set(Window::DimZ, dim_manual_loop);
505 win_weights.set(Window::DimW, dim_manual_loop);
506
507 Window win_output = window;
508 win_output.set_dimension_step(Window::DimX, run_info.x_step);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100509
510 Iterator input_it(input, win_input);
511 Iterator weights_it(weights, win_weights);
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100512 Iterator output_it(output, win_output);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100513 Iterator biases_it{};
514
515 if(has_biases)
516 {
517 biases_it = Iterator(biases, win_weights);
518 }
519
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100520 execute_window_loop(execution_window, [&](const Coordinates & id)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100521 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100522 std::vector<AccType> acc(depth_multiplier, 0);
523 std::vector<AccType> we_sum(depth_multiplier, 0);
524 AccType in_sum = 0;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100525
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100526 const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left;
527 const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top;
528 int64_t input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100529
530 auto weights_ptr = weights_it.ptr();
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100531 for(size_t h = 0; h < run_info.weights_height; ++h)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100532 {
533 int offs = input_offset;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100534 for(size_t w = 0; w < run_info.weights_width; ++w)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100535 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100536 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
537 const auto input_val = is_valid_region ? *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : out_of_bound_value;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100538
539 for(size_t m = 0; m < depth_multiplier; ++m)
540 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100541 const auto weights_val = *(reinterpret_cast<TW *>(weights_ptr + m * sizeof(T) + w * run_info.weights_stride_y));
Giorgio Arenad93e2632019-10-15 11:09:33 +0100542 acc.at(m) += input_val * weights_val;
543
544 we_sum.at(m) += weights_val;
545 }
546
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100547 offs += dilation.x() * run_info.input_stride_y;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100548 in_sum += input_val;
549 }
550
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100551 weights_ptr += run_info.weights_stride_z;
552 input_offset += dilation.y() * run_info.input_stride_z;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100553 }
554
555 for(size_t m = 0; m < depth_multiplier; ++m)
556 {
557 acc.at(m) -= in_sum * weights_qoffset;
558 acc.at(m) -= we_sum.at(m) * input_qoffset;
559 acc.at(m) += k_offset;
560
561 if(has_biases)
562 {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000563 acc.at(m) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + m * sizeof(int32_t)));
564 }
Giorgio Arenad93e2632019-10-15 11:09:33 +0100565
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100566 const int32_t out_mul = output_multiplier.at(id.x() * depth_multiplier + m);
567 const int32_t out_shift = output_shift.at(id.x() * depth_multiplier + m);
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000568 if(out_shift < 0)
569 {
570 acc.at(m) = saturating_doubling_high_mul(acc.at(m) * (1 << (-out_shift)), out_mul) + output_qoffset;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100571 }
572 else
573 {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000574 acc.at(m) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(m), out_mul), out_shift) + output_qoffset;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100575 }
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100576 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = static_cast<T>(utility::clamp<AccType, T>(acc.at(m)));
Giorgio Arenad93e2632019-10-15 11:09:33 +0100577 }
578 },
579 input_it, weights_it, biases_it, output_it);
580}
581
Giorgio Arena44f55722019-07-12 14:49:49 +0100582Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
583 const Size2D &dilation)
584{
585 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100586 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
587 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +0000588 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Giorgio Arena44f55722019-07-12 14:49:49 +0100589 ARM_COMPUTE_RETURN_ERROR_ON(depth_multiplier == 0);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100590 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) + (weights->dimension(1) - 1) * (dilation.x() - 1) > input->dimension(1) + conv_info.pad_left() + conv_info.pad_right());
591 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) + (weights->dimension(2) - 1) * (dilation.y() - 1) > input->dimension(2) + conv_info.pad_top() + conv_info.pad_bottom());
Giorgio Arena44f55722019-07-12 14:49:49 +0100592 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(0) * depth_multiplier) != weights->dimension(0));
593 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
594 ARM_COMPUTE_RETURN_ERROR_ON((conv_info.stride().first < 1) || (conv_info.stride().second < 1));
595
Giorgio Arenad93e2632019-10-15 11:09:33 +0100596 if(is_data_type_quantized_per_channel(weights->data_type()))
597 {
598 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100599 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(0) != weights->quantization_info().scale().size());
600 }
601 else
602 {
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100603 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100604 }
605
Giorgio Arena44f55722019-07-12 14:49:49 +0100606 if(biases != nullptr)
607 {
608 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
609 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0));
Giorgio Arenad93e2632019-10-15 11:09:33 +0100610
611 if(is_data_type_quantized_asymmetric(input->data_type()))
612 {
613 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
614 }
615 else
616 {
617 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
618 }
Giorgio Arena44f55722019-07-12 14:49:49 +0100619 }
620
621 if(output->total_size() != 0)
622 {
623 const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
624 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100625 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Giorgio Arena44f55722019-07-12 14:49:49 +0100626 }
627
628 return Status{};
629}
Giorgio Arenad93e2632019-10-15 11:09:33 +0100630} // namespace
Giorgio Arena44f55722019-07-12 14:49:49 +0100631
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100632NEDepthwiseConvolutionLayerNativeKernel::NEDepthwiseConvolutionLayerNativeKernel()
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100633 : _func(), _input(), _weights(), _biases(), _output(), _conv_info(), _depth_multiplier(1), _dilation(), _output_multiplier(), _output_shift(), _has_biases()
Giorgio Arena44f55722019-07-12 14:49:49 +0100634{
635}
636
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100637void NEDepthwiseConvolutionLayerNativeKernel::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
638 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
Giorgio Arena44f55722019-07-12 14:49:49 +0100639{
640 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
641 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, dilation));
642
643 _input = input;
644 _weights = weights;
645 _biases = biases;
646 _output = output;
647 _conv_info = conv_info;
648 _depth_multiplier = depth_multiplier;
Giorgio Arena44f55722019-07-12 14:49:49 +0100649 _dilation = dilation;
Michalis Spyrouf401c742020-05-12 16:18:33 +0100650 _has_biases = (biases != nullptr);
Giorgio Arena44f55722019-07-12 14:49:49 +0100651
Giorgio Arenad93e2632019-10-15 11:09:33 +0100652 if(is_data_type_quantized(_input->info()->data_type()))
Giorgio Arena44f55722019-07-12 14:49:49 +0100653 {
Giorgio Arenad93e2632019-10-15 11:09:33 +0100654 const auto input_scale = input->info()->quantization_info().uniform().scale;
655 const auto output_scale = output->info()->quantization_info().uniform().scale;
656
657 auto weights_scale = weights->info()->quantization_info().scale();
658 if(!is_data_type_quantized_per_channel(_weights->info()->data_type()))
659 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100660 for(size_t i = 1; i < _weights->info()->dimension(channel_idx); ++i)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100661 {
662 weights_scale.push_back(weights_scale.front());
663 }
664 }
665
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100666 for(const auto &s : weights_scale)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100667 {
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000668 int32_t out_mult = 0;
669 int32_t out_shift = 0;
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100670 const float multiplier = input_scale * s / output_scale;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000671 arm_compute::quantization::calculate_quantized_multiplier(multiplier, &out_mult, &out_shift);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100672
673 _output_multiplier.push_back(out_mult);
674 _output_shift.push_back(out_shift);
675 }
676 }
677
678 switch(_weights->info()->data_type())
679 {
680 case DataType::QASYMM8:
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100681 _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<uint8_t, uint8_t>;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100682 break;
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +0000683 case DataType::QASYMM8_SIGNED:
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100684 _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<int8_t, int8_t>;
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +0000685 break;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100686 case DataType::QSYMM8_PER_CHANNEL:
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +0000687 if(_input->info()->data_type() == DataType::QASYMM8)
688 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100689 _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<uint8_t, int8_t>;
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +0000690 }
691 else
692 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100693 _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<int8_t, int8_t>;
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +0000694 }
Giorgio Arenad93e2632019-10-15 11:09:33 +0100695 break;
696#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
697 case DataType::F16:
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100698 _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float16_t, float16_t>;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100699 break;
700#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Giorgio Arena44f55722019-07-12 14:49:49 +0100701 case DataType::F32:
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100702 _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float, float>;
Giorgio Arena44f55722019-07-12 14:49:49 +0100703 break;
704 default:
705 ARM_COMPUTE_ERROR("Data type not supported");
706 break;
707 }
708
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100709 const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation);
710 auto_init_if_empty(*output->info(), input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_quantization_info(output->info()->quantization_info()));
711
712 Window win = calculate_max_window(*output->info(), Steps());
713 Coordinates coord;
714 coord.set_num_dimensions(output->info()->num_dimensions());
715 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
716 INEKernel::configure(win);
Giorgio Arena44f55722019-07-12 14:49:49 +0100717}
718
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100719Status NEDepthwiseConvolutionLayerNativeKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
720 unsigned int depth_multiplier,
721 const Size2D &dilation)
Giorgio Arena44f55722019-07-12 14:49:49 +0100722{
723 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, depth_multiplier, dilation));
Giorgio Arena44f55722019-07-12 14:49:49 +0100724 return Status{};
725}
726
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100727void NEDepthwiseConvolutionLayerNativeKernel::run(const Window &window, const ThreadInfo &info)
Giorgio Arena44f55722019-07-12 14:49:49 +0100728{
729 ARM_COMPUTE_UNUSED(info);
730 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
731 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
732
Michalis Spyrouf401c742020-05-12 16:18:33 +0100733 (this->*_func)(window, _has_biases);
Giorgio Arena44f55722019-07-12 14:49:49 +0100734}
735
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100736template <typename T, typename TW, NEDepthwiseConvolutionLayerNativeKernel::FloatEnalber<T>>
Michalis Spyrouf401c742020-05-12 16:18:33 +0100737void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window, bool has_biases)
Giorgio Arena44f55722019-07-12 14:49:49 +0100738{
739 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
740 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
741
742 if(_depth_multiplier == 1)
743 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100744 depthwise_loop_multiplier1_fp<T>(_input, _weights, _biases, _output, _conv_info, _dilation, window, has_biases);
Giorgio Arena44f55722019-07-12 14:49:49 +0100745 }
746 else
747 {
Michalis Spyrouf401c742020-05-12 16:18:33 +0100748 depthwise_loop_generic_fp<T>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, window, has_biases);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100749 }
750}
751
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100752template <typename T, typename TW, NEDepthwiseConvolutionLayerNativeKernel::Quantized8bitEnalber<T>>
Michalis Spyrouf401c742020-05-12 16:18:33 +0100753void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window, bool has_biases)
Giorgio Arenad93e2632019-10-15 11:09:33 +0100754{
755 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
756 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
757
758 if(_depth_multiplier == 1)
759 {
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100760 depthwise_loop_multiplier1_quantized<T, TW>(_input, _weights, _biases, _output, _conv_info, _dilation, _output_multiplier, _output_shift, window, has_biases);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100761 }
762 else
763 {
Michalis Spyrouf401c742020-05-12 16:18:33 +0100764 depthwise_loop_generic_quantized<T, TW>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, _output_multiplier, _output_shift, window, has_biases);
Giorgio Arena44f55722019-07-12 14:49:49 +0100765 }
766}
767} // namespace arm_compute