blob: 6e56835115c87d21319552e0ef7d67cb85dfc104 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
2 * Copyright (c) 2017 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 */
Giorgio Arena9fe41442017-08-23 16:36:24 +010024#include "arm_compute/core/CL/kernels/CLDepthwiseConvolution3x3Kernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLKernel.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
36
37using namespace arm_compute;
38
Giorgio Arena9fe41442017-08-23 16:36:24 +010039CLDepthwiseConvolution3x3Kernel::CLDepthwiseConvolution3x3Kernel()
Giorgio Arena93a690e2017-08-01 16:09:33 +010040 : _border_size(0), _input(), _output(), _weights(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_x(0), _conv_pad_y(0)
41{
42}
43
Giorgio Arena9fe41442017-08-23 16:36:24 +010044BorderSize CLDepthwiseConvolution3x3Kernel::border_size() const
Giorgio Arena93a690e2017-08-01 16:09:33 +010045{
46 return _border_size;
47}
48
Giorgio Arena9fe41442017-08-23 16:36:24 +010049void CLDepthwiseConvolution3x3Kernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *weights, const PadStrideInfo &conv_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010050{
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32);
54 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3);
55
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010056 std::pair<unsigned int, unsigned int> expected_output = scaled_dimensions(input->info()->tensor_shape().x(), input->info()->tensor_shape().y(),
57 weights->info()->tensor_shape().x(), weights->info()->tensor_shape().y(),
58 conv_info);
59
60 ARM_COMPUTE_UNUSED(expected_output);
61 ARM_COMPUTE_ERROR_ON(expected_output.first != output->info()->tensor_shape().x());
62 ARM_COMPUTE_ERROR_ON(expected_output.second != output->info()->tensor_shape().y());
63
Giorgio Arena93a690e2017-08-01 16:09:33 +010064 _input = input;
65 _output = output;
66 _weights = weights;
67 _conv_stride_x = conv_info.stride().first;
68 _conv_stride_y = conv_info.stride().second;
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010069 _conv_pad_x = conv_info.pad().first;
70 _conv_pad_y = conv_info.pad().second;
71 _border_size = BorderSize(_conv_pad_y, _conv_pad_x);
Giorgio Arena93a690e2017-08-01 16:09:33 +010072
73 // Set build options
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010074 ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3);
75 std::set<std::string> options{ "-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x) };
Giorgio Arena93a690e2017-08-01 16:09:33 +010076
77 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("depthwise_convolution_3x3", options));
78
79 // Configure kernel window
80 const unsigned int num_elems_processed_per_iteration = 2;
81 const unsigned int num_elems_written_per_iteration = 2;
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010082 const unsigned int num_elems_read_per_iteration = 3 + _conv_stride_x;
Giorgio Arena93a690e2017-08-01 16:09:33 +010083 const unsigned int num_rows_read_per_iteration = 3;
84
85 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
86
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010087 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration, _conv_stride_x, _conv_stride_y);
Giorgio Arena93a690e2017-08-01 16:09:33 +010088 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
89 AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1));
90
91 update_window_and_padding(win, input_access, weights_access, output_access);
92
93 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
94
95 ICLKernel::configure(win);
96}
97
Giorgio Arena9fe41442017-08-23 16:36:24 +010098void CLDepthwiseConvolution3x3Kernel::run(const Window &window, cl::CommandQueue &queue)
Giorgio Arena93a690e2017-08-01 16:09:33 +010099{
100 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
101 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
102
103 Window slice_in = window.first_slice_window_3D();
104 Window slice_out = window.first_slice_window_3D();
105 Window slice_weights = window.first_slice_window_3D();
106
107 slice_in.adjust(Window::DimX, -_conv_pad_x, true);
108 slice_in.adjust(Window::DimY, -_conv_pad_y, true);
109 slice_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
110 slice_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
111 slice_weights.set_dimension_step(Window::DimX, 0);
112 slice_weights.set_dimension_step(Window::DimY, 0);
113
114 do
115 {
116 unsigned int idx = 0;
117 add_3D_tensor_argument(idx, _input, slice_in);
118 add_3D_tensor_argument(idx, _output, slice_out);
119 add_3D_tensor_argument(idx, _weights, slice_weights);
120
121 enqueue(queue, *this, slice_out);
122 }
123 while(window.slide_window_slice_3D(slice_out));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100124}