blob: 3fa5c58c3cb29755e5767da32c4e542265f309ec [file] [log] [blame]
Dana Zlotnikebbae942022-02-03 12:52:15 +02001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2022-2023 Arm Limited.
Dana Zlotnikebbae942022-02-03 12:52:15 +02003 *
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#ifndef SRC_CORE_KERNELS_DEPTWISECONV2DNATIVE_IMPL_H
25#define SRC_CORE_KERNELS_DEPTWISECONV2DNATIVE_IMPL_H
26#include "arm_compute/core/Helpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +010028#include "src/core/NEON/wrapper/wrapper.h"
29
Dana Zlotnikebbae942022-02-03 12:52:15 +020030namespace arm_compute
31{
Matthew Benthamf1aeab92023-05-30 13:35:34 +000032struct ConvolutionInfo;
33
Dana Zlotnikebbae942022-02-03 12:52:15 +020034namespace cpu
35{
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +010036constexpr auto data_layout = DataLayout::NHWC;
37const size_t width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
38const size_t height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
39const size_t channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
40
41constexpr auto dim_manual_loop = Window::Dimension(0, 0, 0);
42constexpr auto dim_single_unit_step = Window::Dimension(0, 1, 1);
43constexpr size_t vector_size = 8;
44
45struct DepthwiseConvolutionRunInfo
46{
47 const size_t num_read_elements_per_iteration;
48 const uint32_t x_start;
49 const uint32_t x_end;
50 const uint32_t x_step;
51 const uint32_t x_leftover_start;
52 const size_t input_stride_y;
53 const size_t input_stride_z;
54 const size_t input_max_offset;
55 const size_t weights_width;
56 const size_t weights_height;
57 const size_t weights_stride_y;
58 const size_t weights_stride_z;
59 const size_t conv_stride_x;
60 const size_t conv_stride_y;
61 const size_t conv_pad_left;
62 const size_t conv_pad_top;
63 const size_t input_height;
64 const size_t input_width;
65 const size_t input_depth;
66
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 DepthwiseConvolutionRunInfo(const ITensorInfo &input,
68 const ITensorInfo &weights,
69 const PadStrideInfo &conv_info,
70 const Window &w,
71 uint32_t depth_multiplier = 1) // NOLINT
72 : num_read_elements_per_iteration(
73 (depth_multiplier == 1 ? (vector_size / element_size_from_data_type(input.data_type())) : 1)),
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +010074 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() + 1) - static_cast<int32_t>(x_step), int32_t(0))),
78 input_stride_y(input.strides_in_bytes().y()),
79 input_stride_z(input.strides_in_bytes().z()),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 input_max_offset(input.strides_in_bytes().z() * input.dimension(height_idx) -
81 (input.padding().bottom + input.padding().top) * input.strides_in_bytes().y()),
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +010082 weights_width(weights.dimension(width_idx)),
83 weights_height(weights.dimension(height_idx)),
84 weights_stride_y(weights.strides_in_bytes().y()),
85 weights_stride_z(weights.strides_in_bytes().z()),
86 conv_stride_x(conv_info.stride().first),
87 conv_stride_y(conv_info.stride().second),
88 conv_pad_left(conv_info.pad_left()),
89 conv_pad_top(conv_info.pad_top()),
90 input_height(input.dimension(height_idx)),
91 input_width(input.dimension(width_idx)),
92 input_depth(input.dimension(channel_idx))
93 {
94 }
95};
96
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097inline bool is_valid_input_region(int32_t base_w,
98 uint32_t base_h,
99 uint32_t w,
100 uint32_t h,
101 const DepthwiseConvolutionRunInfo &run_info,
102 const Size2D &dilation)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100103{
104 const int32_t current_h = base_h + h * dilation.y();
105 const bool is_valid_h = current_h >= 0 && current_h < static_cast<int32_t>(run_info.input_height);
106
107 const int32_t current_w = base_w + w * dilation.x();
108 const bool is_valid_w = current_w >= 0 && current_w < static_cast<int32_t>(run_info.input_width);
109
110 return is_valid_h && is_valid_w;
111}
112
113template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114void depthwise_loop_multiplier1_fp(const ITensor *src,
115 const ITensor *weights,
116 const ITensor *biases,
117 ITensor *dst,
118 const PadStrideInfo &conv_info,
119 const Size2D &dilation,
120 const Window &window,
121 bool has_biases)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100122{
123 constexpr auto element_per_vector = vector_size / sizeof(T);
124 using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type;
125 using TagType = typename wrapper::traits::neon_vector<T, element_per_vector>::tag_type;
126
127 const auto run_info = DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window);
128
129 const VectorType zero_vector = wrapper::vdup_n(static_cast<T>(0), TagType{});
130
131 Window execution_window = window;
132 execution_window.set(Window::DimX, dim_single_unit_step);
133
134 Window win_input = window;
135 win_input.set(Window::DimX, dim_manual_loop);
136 win_input.set(Window::DimY, dim_manual_loop);
137 win_input.set(Window::DimZ, dim_manual_loop);
138
139 Window win_weights = win_input;
140 win_weights.set(Window::DimW, dim_manual_loop);
141
142 Window win_output = window;
143 win_output.set(Window::DimX, dim_manual_loop);
144
145 Iterator input_it(src, win_input);
146 Iterator weights_it(weights, win_weights);
147 Iterator output_it(dst, win_output);
148 Iterator biases_it{};
149
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 if (has_biases)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100151 {
152 biases_it = Iterator(biases, win_weights);
153 }
154
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 execute_window_loop(
156 execution_window,
157 [&](const Coordinates &id)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100158 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left;
160 const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top;
161 const int64_t base_input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z;
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100162
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163 auto const base_weights_ptr = weights_it.ptr();
164 uint32_t x = run_info.x_start;
165
166 for (; x < run_info.x_leftover_start; x += run_info.x_step)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100167 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 VectorType acc = zero_vector;
169 auto weights_ptr = base_weights_ptr;
170 int64_t input_offset = base_input_offset;
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100171
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 for (uint32_t h = 0; h < run_info.weights_height; ++h)
173 {
174 int64_t offs = input_offset + x * sizeof(T);
175 for (uint32_t w = 0; w < run_info.weights_width; ++w)
176 {
177 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
178 const auto input_vals =
179 is_valid_region
180 ? wrapper::vload(reinterpret_cast<T *>(
181 input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset)))
182 : zero_vector;
183 const auto weights_vals =
184 wrapper::vload(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x);
185 acc = wrapper::vmla(acc, weights_vals, input_vals);
186
187 offs += dilation.x() * run_info.input_stride_y;
188 }
189
190 weights_ptr += run_info.weights_stride_z;
191 input_offset += dilation.y() * run_info.input_stride_z;
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100192 }
193
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 if (has_biases)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100195 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 const auto biases_vals = wrapper::vload(reinterpret_cast<T *>(biases_it.ptr()) + x);
197 acc = wrapper::vadd(acc, biases_vals);
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100198 }
199
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100200 wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, acc);
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100201 }
202
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 for (; x < run_info.x_end; ++x)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100204 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 auto acc_scalar = T{0};
206 auto weights_ptr = base_weights_ptr;
207 int64_t input_offset = base_input_offset;
208
209 for (size_t h = 0; h < run_info.weights_height; ++h)
210 {
211 int64_t offs = input_offset + x * sizeof(T);
212 for (size_t w = 0; w < run_info.weights_width; ++w)
213 {
214 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
215 const auto input_vals =
216 is_valid_region
217 ? *reinterpret_cast<T *>(input_it.ptr() +
218 std::min(static_cast<size_t>(offs), run_info.input_max_offset))
219 : 0;
220 const auto weights_vals =
221 *(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x);
222
223 acc_scalar += (input_vals * weights_vals);
224
225 offs += dilation.x() * run_info.input_stride_y;
226 }
227
228 weights_ptr += run_info.weights_stride_z;
229 input_offset += dilation.y() * run_info.input_stride_z;
230 }
231
232 if (has_biases)
233 {
234 const auto biases_vals = *(reinterpret_cast<T *>(biases_it.ptr()) + x);
235 acc_scalar += biases_vals;
236 }
237 *(reinterpret_cast<T *>(output_it.ptr()) + x) = acc_scalar;
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100238 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100239 },
240 input_it, weights_it, biases_it, output_it);
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100241}
242
243template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100244void depthwise_loop_generic_fp(const ITensor *src,
245 const ITensor *weights,
246 const ITensor *biases,
247 ITensor *dst,
248 const PadStrideInfo &conv_info,
249 const Size2D &dilation,
250 unsigned int depth_multiplier,
251 const Window &window,
252 bool has_biases)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100253{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 const auto run_info =
255 DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window, depth_multiplier);
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100256
257 Window execution_window = window;
258 execution_window.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1));
259
260 Window win_input = execution_window;
261 win_input.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1));
262 win_input.set(Window::DimY, dim_manual_loop);
263 win_input.set(Window::DimZ, dim_manual_loop);
264
265 Window win_weights = window;
266 win_weights.set_dimension_step(Window::DimX, run_info.x_step);
267 win_weights.set(Window::DimY, dim_manual_loop);
268 win_weights.set(Window::DimZ, dim_manual_loop);
269 win_weights.set(Window::DimW, dim_manual_loop);
270
271 Window win_output = window;
272 win_output.set_dimension_step(Window::DimX, run_info.x_step);
273
274 Iterator input_it(src, win_input);
275 Iterator weights_it(weights, win_weights);
276 Iterator output_it(dst, win_output);
277 Iterator biases_it{};
278
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100279 if (has_biases)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100280 {
281 biases_it = Iterator(biases, win_weights);
282 }
283
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100284 execute_window_loop(
285 execution_window,
286 [&](const Coordinates &id)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100287 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100288 std::vector<T> acc(depth_multiplier, static_cast<T>(0));
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100289
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100290 const int input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left;
291 const int input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top;
292 int input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z;
293
294 auto weights_ptr = weights_it.ptr();
295 for (size_t h = 0; h < run_info.weights_height; ++h)
296 {
297 int offs = input_offset;
298 for (size_t w = 0; w < run_info.weights_width; ++w)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100299 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100300 const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation);
301 const auto input_val =
302 is_valid_region ? *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs),
303 run_info.input_max_offset)))
304 : T(0);
305
306 for (size_t m = 0; m < depth_multiplier; ++m)
307 {
308 const auto weights_val =
309 *(reinterpret_cast<T *>(weights_ptr + m * sizeof(T) + w * run_info.weights_stride_y));
310 acc.at(m) = support::cpp11::fma(weights_val, input_val, acc.at(m));
311 }
312
313 offs += dilation.x() * run_info.input_stride_y;
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100314 }
315
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100316 weights_ptr += run_info.weights_stride_z;
317 input_offset += dilation.y() * run_info.input_stride_z;
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100318 }
319
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100320 if (has_biases)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100321 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100322 for (size_t m = 0; m < depth_multiplier; ++m)
323 {
324 const auto biases_val = *(reinterpret_cast<T *>(biases_it.ptr() + m * sizeof(T)));
325 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m) + biases_val;
326 }
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100327 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100328 else
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100329 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 for (size_t m = 0; m < depth_multiplier; ++m)
331 {
332 *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m);
333 }
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100334 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100335 },
336 input_it, weights_it, biases_it, output_it);
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100337}
338
Dana Zlotnikebbae942022-02-03 12:52:15 +0200339template <typename T, typename TW>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100340void run_depthwise_float(const ITensor *src,
341 const ITensor *weights,
342 const ITensor *biases,
343 ITensor *dst,
344 const Window &window,
345 bool has_biases,
346 const ConvolutionInfo &info)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100347{
348 PadStrideInfo conv_info = info.pad_stride_info;
349 unsigned int depth_multiplier = info.depth_multiplier;
350 Size2D dilation = info.dilation;
351
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100352 if (depth_multiplier == 1)
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100353 {
354 depthwise_loop_multiplier1_fp<T>(src, weights, biases, dst, conv_info, dilation, window, has_biases);
355 }
356 else
357 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100358 depthwise_loop_generic_fp<T>(src, weights, biases, dst, conv_info, dilation, depth_multiplier, window,
359 has_biases);
Pablo Marquez Tello7ff03b62023-08-30 13:41:41 +0100360 }
361}
Dana Zlotnikebbae942022-02-03 12:52:15 +0200362
363template <typename T, typename TW>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100364void run_depthwise_quanitized8bit(const ITensor *src,
365 const ITensor *weights,
366 const ITensor *biases,
367 ITensor *dst,
368 const Window &window,
369 bool has_biases,
370 const ConvolutionInfo &info);
Dana Zlotnikebbae942022-02-03 12:52:15 +0200371
372} // namespace cpu
373} // namespace arm_compute
374#endif //define SRC_CORE_KERNELS_DEPTWISECONV2DNATIVE_IMPL_H