blob: fecdb2bcae07749b6e747d4c1bfe9adc28cef08e [file] [log] [blame]
Sheri Zhang6d9c9822021-09-24 16:02:57 +01001/*
2 * Copyright (c) 2021 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#include "src/cpu/kernels/CpuDirectConv3dKernel.h"
25
26#include "src/core/NEON/kernels/detail/NEDirectConvolutionDetail.h"
27#include "src/core/NEON/wrapper/wrapper.h"
28
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/ITensor.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/utils/misc/ShapeCalculator.h"
37#include "src/core/CPP/Validate.h"
38#include "src/core/helpers/AutoConfiguration.h"
39#include "src/core/helpers/WindowHelpers.h"
40
41#include <algorithm>
42
43using namespace arm_compute::detail;
44
45namespace arm_compute
46{
47namespace cpu
48{
49namespace kernels
50{
51namespace
52{
53Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const Conv3dInfo &conv_info)
54{
55 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
56 ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() != DataLayout::NDHWC);
57 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
58 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
60
61 const DataLayout data_layout = src->data_layout();
62 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
63
64 // Weight layout is D, H, W, Cin, Cout
65 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 5);
66 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) != src->dimension(channel_idx));
67
68 if(biases != nullptr)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(0),
72 "biases size and number of output feature maps should match");
73 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, "biases should be one dimensional");
74 }
75
76 // Checks performed when output is configured
77 if(dst->total_size() != 0)
78 {
79 TensorShape output_shape = misc::shape_calculator::compute_conv3d_shape(src->tensor_shape(), weights->tensor_shape(), conv_info);
80
81 DataType data_type = src->data_type();
82
83 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), output_shape);
84 ARM_COMPUTE_RETURN_ERROR_ON(dst->data_type() != data_type);
85 }
86
87 return Status{};
88}
89
90/** Reduce a vector to be a scalar by accumulating all lanes in the vector
91 *
92 * @param[in] v Vector to be reduced.
93 *
94 * @return the wrapped-around number.
95 */
96auto vreduce(const float32x4_t &v)
97{
98 auto v0 = wrapper::vgethigh(v);
99 auto v1 = wrapper::vgetlow(v);
100 auto v_out = wrapper::vadd(v0, v1);
101
102 float a = wrapper::vgetlane(v_out, 0);
103 float b = wrapper::vgetlane(v_out, 1);
104 return a + b;
105}
106
107#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
108auto vreduce(const float16x8_t &v)
109{
110 auto v0 = wrapper::vgethigh(v);
111 auto v1 = wrapper::vgetlow(v);
112 auto v_out = wrapper::vadd(v0, v1);
113
114 float16_t a = wrapper::vgetlane(v_out, 0);
115 float16_t b = wrapper::vgetlane(v_out, 1);
116 float16_t c = wrapper::vgetlane(v_out, 2);
117 float16_t d = wrapper::vgetlane(v_out, 3);
118 return a + b + c + d;
119}
120#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
121}
122
123template <typename T>
124void CpuDirectConv3dKernel::convolve_ndhwc(const Window &window, const ITensor *src, const ITensor *weights, const ITensor *biases, ITensor *dst)
125{
126 using vtype = wrapper::traits::neon_bitvector<T, wrapper::traits::BitWidth::W128>;
127 using vector_type = typename vtype::type;
128 using tag_type = typename vtype::tag_type;
129 constexpr int num_elems_read_per_iteration = 16 / sizeof(T);
130
131 // Scalar quantities (N D H W Cin)
132 const int element_size = src->info()->element_size();
133 const int input_stride_w = src->info()->strides_in_bytes().y() / element_size;
134 const int input_stride_h = src->info()->strides_in_bytes().z() / element_size;
135 const int input_stride_d = src->info()->strides_in_bytes()[3] / element_size;
136 const int input_stride_n = src->info()->strides_in_bytes()[4] / element_size;
137 const int input_dim_w = src->info()->dimension(1);
138 const int input_dim_h = src->info()->dimension(2);
139 const int input_dim_d = src->info()->dimension(3);
140
141 // Kernel info (D H W Cin Cout)
142 const unsigned int kernel_stride_w = weights->info()->strides_in_bytes()[2] / element_size;
143 const unsigned int kernel_stride_h = weights->info()->strides_in_bytes()[3] / element_size;
144 const unsigned int kernel_stride_d = weights->info()->strides_in_bytes()[4] / element_size;
145 const int kernel_dim_w = weights->info()->dimension(2);
146 const int kernel_dim_h = weights->info()->dimension(3);
147 const int kernel_dim_d = weights->info()->dimension(4);
148
149 // Convolution padding and stride
150 const int conv_pad_top = _conv_info.padding.top;
151 const int conv_pad_left = _conv_info.padding.left;
152 const int conv_pad_front = _conv_info.padding.front;
153 const int conv_stride_w = _conv_info.stride.width;
154 const int conv_stride_h = _conv_info.stride.height;
155 const int conv_stride_d = _conv_info.stride.depth;
156
157 // Setup input window for the output iterator
158 Window window_out = window;
159 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
160
161 // Setup input window for the weights iterator
162 Window window_w = calculate_max_window(*weights->info(), Steps());
163 window_w.set(Window::DimY, Window::Dimension(0, 1, 1));
164 window_w.set(Window::DimZ, Window::Dimension(0, 1, 1));
165 window_w.set(Window::DimW, Window::Dimension(0, 1, 1));
166 window_w.set(4, Window::Dimension(0, 1, 1));
167
168 Iterator out(dst, window_out);
169 Iterator wei(weights, window_w);
170
171 const T *biases_ptr = nullptr;
172 if(biases)
173 {
174 biases_ptr = reinterpret_cast<T *>(biases->buffer() + biases->info()->offset_first_element_in_bytes());
175 }
176 execute_window_loop(window_out, [&](const Coordinates & id)
177 {
178 // We are computing the theoretical input starting points
179 const int in_w_start_t = static_cast<int>(id.y()) * conv_stride_w - conv_pad_left;
180 const int in_h_start_t = static_cast<int>(id.z()) * conv_stride_h - conv_pad_top;
181 const int in_d_start_t = static_cast<int>(id[3]) * conv_stride_d - conv_pad_front;
182 const int in_w_end_t = in_w_start_t + kernel_dim_w;
183 const int in_h_end_t = in_h_start_t + kernel_dim_h;
184 const int in_d_end_t = in_d_start_t + kernel_dim_d;
185
186 // We are computing the valid initial and ending input points by checking the borders
187 const int in_w_start = std::max(in_w_start_t, 0);
188 const int in_h_start = std::max(in_h_start_t, 0);
189 const int in_d_start = std::max(in_d_start_t, 0);
190 const int in_w_end = std::min(in_w_end_t, input_dim_w);
191 const int in_h_end = std::min(in_h_end_t, input_dim_h);
192 const int in_d_end = std::min(in_d_end_t, input_dim_d);
193
194 // We use the input points to select the valid weight points to use
195 const int wei_w_start = in_w_start - in_w_start_t;
196 const int wei_h_start = in_h_start - in_h_start_t;
197 const int wei_d_start = in_d_start - in_d_start_t;
198 const int wei_w_end = kernel_dim_w - (in_w_end_t - in_w_end);
199 const int wei_h_end = kernel_dim_h - (in_h_end_t - in_h_end);
200 const int wei_d_end = kernel_dim_d - (in_d_end_t - in_d_end);
201
202 const int index_c_out_end = weights->info()->dimension(0);
203 const int index_c_in_end = weights->info()->dimension(1);
204 const T *const in_ptr_start = reinterpret_cast<const T *>(src->buffer() + src->info()->offset_first_element_in_bytes()) + id[4] * input_stride_n;
205
206 execute_window_loop(window_w, [&](const Coordinates & id_w)
207 {
208 /*
209 * This is the loop in the weights, and it goes along OFM (output feature map)
210 */
211 const auto weights_ptr_start = reinterpret_cast<const T *>(wei.ptr());
212 T out_temp = static_cast<T>(0);
213 T *out_ptr = reinterpret_cast<T *>(out.ptr());
214 for(int index_wei_d = wei_d_start, index_in_d = in_d_start; index_wei_d < wei_d_end; ++index_wei_d, ++index_in_d)
215 {
216 const auto in_ptr_d = in_ptr_start + index_in_d * input_stride_d;
217 const auto weights_ptr_d = weights_ptr_start + index_wei_d * kernel_stride_d;
218 for(int index_wei_h = wei_h_start, index_in_h = in_h_start; index_wei_h < wei_h_end; ++index_wei_h, ++index_in_h)
219 {
220 const T *const in_ptr_row = in_ptr_d + index_in_h * input_stride_h;
221 const T *const weights_ptr_row = weights_ptr_d + index_wei_h * kernel_stride_h;
222 for(int index_wei_w = wei_w_start, index_in_w = in_w_start; index_wei_w < wei_w_end; ++index_wei_w, ++index_in_w)
223 {
224 const T *in_ptr_mover = in_ptr_row + index_in_w * input_stride_w;
225 const T *weights_ptr_mover = weights_ptr_row + index_wei_w * kernel_stride_w;
226 int index_c_in = 0;
227 vector_type out_temp_vec = wrapper::vdup_n(static_cast<T>(0), tag_type());
228 vector_type w_vec = wrapper::vdup_n(static_cast<T>(0), tag_type());
229 for(; index_c_in <= index_c_in_end - num_elems_read_per_iteration;
230 index_c_in += num_elems_read_per_iteration, in_ptr_mover += num_elems_read_per_iteration)
231 {
232 const auto src_vec = wrapper::vloadq(in_ptr_mover);
233 //Load Cin weights
234 for(unsigned int k = 0; k < num_elems_read_per_iteration; ++k, weights_ptr_mover += index_c_out_end)
235 {
236 w_vec = wrapper::vsetlane(*weights_ptr_mover, w_vec, k);
237 }
238 out_temp_vec = wrapper::vmla(out_temp_vec, w_vec, src_vec);
239 }
240 out_temp += vreduce(out_temp_vec);
241 for(; index_c_in < index_c_in_end; ++index_c_in, ++in_ptr_mover, weights_ptr_mover += index_c_out_end)
242 {
243 const auto src_val = *(in_ptr_mover);
244 const auto w_val = *(weights_ptr_mover);
245 out_temp += src_val * w_val;
246 }
247 }
248 }
249 }
250 *(reinterpret_cast<T *>(out_ptr + id_w[0])) = (biases) ? out_temp + biases_ptr[id_w[0]] : out_temp;
251 },
252 wei);
253 },
254 out);
255}
256
257void CpuDirectConv3dKernel::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const Conv3dInfo &conv_info)
258{
259 ARM_COMPUTE_UNUSED(biases);
260 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
261
262 _conv_info = conv_info;
263
264 // Get convolved dimensions
265 TensorShape output_shape = misc::shape_calculator::compute_conv3d_shape(src->tensor_shape(), weights->tensor_shape(), conv_info);
266
267 DataType data_type = src->data_type();
268
269 // Output auto inizialitation if not yet initialized
270 auto_init_if_empty(*dst, output_shape, 1, data_type);
271
272 // Perform validation step
273 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info));
274
275 // Configure kernel window
276 Window win = calculate_max_window(*dst, Steps());
277 ICpuKernel::configure(win);
278}
279
280Status CpuDirectConv3dKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const Conv3dInfo &conv_info)
281{
282 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info));
283
284 return Status{};
285}
286
287void CpuDirectConv3dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
288{
289 ARM_COMPUTE_UNUSED(info);
290 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
291 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
292
293 auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
294 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
295 auto biases = tensors.get_const_tensor(TensorType::ACL_SRC_2);
296 auto dst = tensors.get_tensor(TensorType::ACL_DST);
297
298 switch(src->info()->data_type())
299 {
300 case DataType::F32:
301 {
302 convolve_ndhwc<float>(window, src, weights, biases, dst);
303 break;
304 }
305#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
306 case DataType::F16:
307 {
308 convolve_ndhwc<float16_t>(window, src, weights, biases, dst);
309 break;
310 }
311#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
312 default:
313 ARM_COMPUTE_ERROR("Data type not supported");
314 break;
315 }
316}
317
318const char *CpuDirectConv3dKernel::name() const
319{
320 return "CpuDirectConv3dKernel";
321}
322} // namespace kernels
323} // namespace cpu
324} // namespace arm_compute