blob: f235167e28222ac19052f81eaf027c5076e5a561 [file] [log] [blame]
alerah01c9e519d2022-01-31 19:04:10 +02001/*
2 * Copyright (c) 2018-2022 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 */
24
25#include "src/cpu/kernels/directconv2d/nhwc/neon/impl.h"
26
alerah01c9e519d2022-01-31 19:04:10 +020027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Utils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
alerah01c9e519d2022-01-31 19:04:10 +020034#include "src/core/helpers/WindowHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035#include "src/core/NEON/kernels/detail/NEDirectConvolutionDetail.h"
36#include "src/core/NEON/wrapper/wrapper.h"
alerah01c9e519d2022-01-31 19:04:10 +020037
38#include <algorithm>
39
40using namespace arm_compute::detail;
41
42namespace arm_compute
43{
44namespace cpu
45{
46namespace kernels
47{
48namespace
49{
50bool have_zero_x_internal_padding(ITensorInfo *src, const ITensorInfo *weights)
51{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 return (src->padding().left == 0 && weights->padding().left == 0 && src->padding().right == 0 &&
53 weights->padding().right == 0);
alerah01c9e519d2022-01-31 19:04:10 +020054}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055} // namespace
alerah01c9e519d2022-01-31 19:04:10 +020056
57template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058void convolve_nhwc(
59 const Window &window, const ITensor *src, const ITensor *weights, ITensor *dst, const PadStrideInfo &conv_info)
alerah01c9e519d2022-01-31 19:04:10 +020060{
61 // Declare useful types
62 using vtype = wrapper::traits::neon_bitvector<T, wrapper::traits::BitWidth::W128>;
63 using vector_type = typename vtype::type;
64 using tag_type = typename vtype::tag_type;
65
66 // Scalar quantities
67 const int element_size = src->info()->element_size();
68 const int input_stride_w = src->info()->strides_in_bytes().y() / element_size;
69 const int input_stride_h = src->info()->strides_in_bytes().z() / element_size;
70 const int input_stride_n = src->info()->strides_in_bytes()[3] / element_size;
71 const int input_dim_w = src->info()->dimension(1);
72 const int input_dim_h = src->info()->dimension(2);
73
74 const int output_stride_c = dst->info()->strides_in_bytes().x();
75
76 const unsigned int kernel_stride_w = weights->info()->strides_in_bytes().y() / element_size;
77 const unsigned int kernel_stride_h = weights->info()->strides_in_bytes().z() / element_size;
78 const int kernel_dim_w = weights->info()->dimension(1);
79 const int kernel_dim_h = weights->info()->dimension(2);
80
81 const int conv_pad_top = conv_info.pad_top();
82 const int conv_pad_left = conv_info.pad_left();
83 const int conv_stride_w = std::get<0>(conv_info.stride());
84 const int conv_stride_h = std::get<1>(conv_info.stride());
85
86 // Setup input window for the output iterator
87 Window window_out = window;
88 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
89
90 // Setup input window for the weights iterator
91 Window window_w = calculate_max_window(*weights->info(), Steps());
92 window_w.set(Window::DimX, Window::Dimension(0, 1, 1));
93 window_w.set(Window::DimY, Window::Dimension(0, 1, 1));
94 window_w.set(Window::DimZ, Window::Dimension(0, 1, 1));
95
96 Iterator out(dst, window_out);
97 Iterator wei(weights, window_w);
98
99 constexpr int num_elems_read_per_iteration = 16 / sizeof(T);
100
101 // nhwc optimized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 if (have_zero_x_internal_padding(src->info(), weights->info()))
alerah01c9e519d2022-01-31 19:04:10 +0200103 {
104 // This function assumes that input and weights have not padding in channel
105
106 /*
107 * This implementation parallelize the full WC plane of input and weights by
108 * treating them as series of elements. So for example, a 3x3 weights and
109 * floating point vector operations of 4 elements per time, the first 3
110 * channel elements of the first row would be taken and additionally the first
111 * element of the second row. The 9 elements in each single WC weight plane
112 * would require 2 4-element vector operations and a last single element operation.
113 *
114 * This works since when we create the input vector to multiply with the weights,
115 * the exact required elements are loaded in the same order. Therefore the
116 * multiplication works on the correct input/weight elements.
117 */
118 execute_window_loop(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 window_out,
120 [&](const Coordinates &id)
121 {
122 /*
alerah01c9e519d2022-01-31 19:04:10 +0200123 * In here we create theoretical indexes which then we validate for both
124 * inputs and weights.
125 * As a reminder, this loop take each output point in NHW, C is treated
126 * in the weights loop.
127 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 // We are computing the theoretical starting input starting points
129 const int in_w_start_t = static_cast<int>(id.y()) * conv_stride_w - conv_pad_left;
130 const int in_h_start_t = static_cast<int>(id.z()) * conv_stride_h - conv_pad_top;
131 const int in_w_end_t = in_w_start_t + kernel_dim_w;
132 const int in_h_end_t = in_h_start_t + kernel_dim_h;
alerah01c9e519d2022-01-31 19:04:10 +0200133
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 // We are computing the valid initial and ending input points by checking the borders
135 const int in_w_start = std::max(in_w_start_t, 0);
136 const int in_h_start = std::max(in_h_start_t, 0);
137 const int in_w_end = std::min(in_w_end_t, input_dim_w);
138 const int in_h_end = std::min(in_h_end_t, input_dim_h);
alerah01c9e519d2022-01-31 19:04:10 +0200139
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 // We use the input points to select the valid weight points to use
141 const int index_wc_start = (in_w_start - in_w_start_t) * kernel_stride_w;
142 const int index_h_start = in_h_start - in_h_start_t;
143 const int index_wc_end = (kernel_dim_w - (in_w_end_t - in_w_end)) * kernel_stride_w;
144 const int index_h_end = kernel_dim_h - (in_h_end_t - in_h_end);
alerah01c9e519d2022-01-31 19:04:10 +0200145
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100146 execute_window_loop(
147 window_w,
148 [&](const Coordinates &id_w)
149 {
150 /*
alerah01c9e519d2022-01-31 19:04:10 +0200151 * This is the loop in the weights, and it goes along N (the batches)
152 * As a reminder, the batches of the weights are translated into the
153 * channels of the output
154 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 const T *in_ptr_row =
156 reinterpret_cast<const T *>(src->buffer() + src->info()->offset_first_element_in_bytes()) +
157 id[3] * input_stride_n + in_w_start * input_stride_w + in_h_start * input_stride_h;
158 const T *weights_ptr_row =
159 reinterpret_cast<const T *>(wei.ptr()) + index_h_start * kernel_stride_h;
160 uint8_t *out_ptr = out.ptr() + id_w[3] * output_stride_c;
alerah01c9e519d2022-01-31 19:04:10 +0200161
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 T out_temp = static_cast<T>(0);
163 for (int index_h = index_h_start; index_h < index_h_end;
164 ++index_h, in_ptr_row += input_stride_h, weights_ptr_row += kernel_stride_h)
165 {
166 const T *in_ptr_mover = in_ptr_row;
167 int index_wc = index_wc_start;
168 vector_type out_temp_vec = wrapper::vdup_n(static_cast<T>(0), tag_type());
169 for (; index_wc <= index_wc_end - num_elems_read_per_iteration;
170 index_wc += num_elems_read_per_iteration, in_ptr_mover += num_elems_read_per_iteration)
171 {
172 const auto src_vec = wrapper::vloadq(in_ptr_mover);
173 const auto w_vec = wrapper::vloadq(weights_ptr_row + index_wc);
174 out_temp_vec = wrapper::vmla(out_temp_vec, w_vec, src_vec);
175 }
176 out_temp += vreduce(out_temp_vec);
177 for (; index_wc < index_wc_end; ++index_wc, ++in_ptr_mover)
178 {
179 const auto src_val = *(in_ptr_mover);
180 const auto w_val = *(weights_ptr_row + index_wc);
181 out_temp += src_val * w_val;
182 }
183 }
184 *(reinterpret_cast<T *>(out_ptr)) = out_temp;
185 },
186 wei);
alerah01c9e519d2022-01-31 19:04:10 +0200187 },
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 out);
alerah01c9e519d2022-01-31 19:04:10 +0200189 }
190 else // nhwc non optimized
191 {
192 execute_window_loop(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193 window_out,
194 [&](const Coordinates &id)
alerah01c9e519d2022-01-31 19:04:10 +0200195 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 // We are computing the theoretical starting input starting points
197 const int in_w_start_t = static_cast<int>(id.y()) * conv_stride_w - conv_pad_left;
198 const int in_h_start_t = static_cast<int>(id.z()) * conv_stride_h - conv_pad_top;
199 const int in_w_end_t = in_w_start_t + kernel_dim_w;
200 const int in_h_end_t = in_h_start_t + kernel_dim_h;
alerah01c9e519d2022-01-31 19:04:10 +0200201
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 // We are computing the valid initial and ending input points by checking the borders
203 const int in_w_start = std::max(in_w_start_t, 0);
204 const int in_h_start = std::max(in_h_start_t, 0);
205 const int in_w_end = std::min(in_w_end_t, input_dim_w);
206 const int in_h_end = std::min(in_h_end_t, input_dim_h);
207
208 // We use the input points to select the valid weight points to use
209 const int wei_w_start = in_w_start - in_w_start_t;
210 const int wei_h_start = in_h_start - in_h_start_t;
211 const int wei_w_end = kernel_dim_w - (in_w_end_t - in_w_end);
212 const int wei_h_end = kernel_dim_h - (in_h_end_t - in_h_end);
213
214 const int index_c_end = weights->info()->dimension(0);
215 const T *const in_ptr_start =
216 reinterpret_cast<const T *>(src->buffer() + src->info()->offset_first_element_in_bytes()) +
217 id[3] * input_stride_n;
218
219 execute_window_loop(
220 window_w,
221 [&](const Coordinates &id_w)
alerah01c9e519d2022-01-31 19:04:10 +0200222 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 const T *const weights_ptr_start = reinterpret_cast<const T *>(wei.ptr());
224 uint8_t *out_ptr = out.ptr() + id_w[3] * output_stride_c;
225
226 T out_temp = static_cast<T>(0);
227 for (int index_wei_h = wei_h_start, index_in_h = in_h_start; index_wei_h < wei_h_end;
228 ++index_wei_h, ++index_in_h)
alerah01c9e519d2022-01-31 19:04:10 +0200229 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100230 const T *const in_ptr_row = in_ptr_start + index_in_h * input_stride_h;
231 const T *const weights_ptr_row = weights_ptr_start + index_wei_h * kernel_stride_h;
232 for (int index_wei_w = wei_w_start, index_in_w = in_w_start; index_wei_w < wei_w_end;
233 ++index_wei_w, ++index_in_w)
234 {
235 const T *in_ptr_mover = in_ptr_row + index_in_w * input_stride_w;
236 const T *weights_ptr_mover = weights_ptr_row + index_wei_w * kernel_stride_w;
237 int index_c = 0;
238 vector_type out_temp_vec = wrapper::vdup_n(static_cast<T>(0), tag_type());
239 for (; index_c <= index_c_end - num_elems_read_per_iteration;
240 index_c += num_elems_read_per_iteration,
241 in_ptr_mover += num_elems_read_per_iteration,
242 weights_ptr_mover += num_elems_read_per_iteration)
243 {
244 const auto src_vec = wrapper::vloadq(in_ptr_mover);
245 const auto w_vec = wrapper::vloadq(weights_ptr_mover);
246 out_temp_vec = wrapper::vmla(out_temp_vec, w_vec, src_vec);
247 }
248 out_temp += vreduce(out_temp_vec);
249 for (; index_c < index_c_end; ++index_c, ++in_ptr_mover, ++weights_ptr_mover)
250 {
251 const auto src_val = *(in_ptr_mover);
252 const auto w_val = *(weights_ptr_mover);
253 out_temp += src_val * w_val;
254 }
255 }
alerah01c9e519d2022-01-31 19:04:10 +0200256 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100257 *(reinterpret_cast<T *>(out_ptr)) = out_temp;
258 },
259 wei);
alerah01c9e519d2022-01-31 19:04:10 +0200260 },
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100261 out);
alerah01c9e519d2022-01-31 19:04:10 +0200262 }
263}
264
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265template void convolve_nhwc<float>(
266 const Window &window, const ITensor *src, const ITensor *weights, ITensor *dst, const PadStrideInfo &conv_info);
alerah01c9e519d2022-01-31 19:04:10 +0200267
268} // namespace kernels
269} // namespace cpu
270} // namespace arm_compute