blob: 2d0c416d0a8582d45f48b6615879d3d00f4ed4e9 [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()
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010040 : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_x(0), _conv_pad_y(0)
Giorgio Arena93a690e2017-08-01 16:09:33 +010041{
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
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010049void CLDepthwiseConvolution3x3Kernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *weights, const ICLTensor *biases, 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 Pinitas81a26ad2017-10-23 20:29:30 +010056 if(biases != nullptr)
57 {
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
59 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2));
60 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
61 }
62
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010063 std::pair<unsigned int, unsigned int> expected_output = scaled_dimensions(input->info()->tensor_shape().x(), input->info()->tensor_shape().y(),
64 weights->info()->tensor_shape().x(), weights->info()->tensor_shape().y(),
65 conv_info);
66
67 ARM_COMPUTE_UNUSED(expected_output);
68 ARM_COMPUTE_ERROR_ON(expected_output.first != output->info()->tensor_shape().x());
69 ARM_COMPUTE_ERROR_ON(expected_output.second != output->info()->tensor_shape().y());
70
Giorgio Arena93a690e2017-08-01 16:09:33 +010071 _input = input;
72 _output = output;
73 _weights = weights;
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010074 _biases = biases;
Giorgio Arena93a690e2017-08-01 16:09:33 +010075 _conv_stride_x = conv_info.stride().first;
76 _conv_stride_y = conv_info.stride().second;
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010077 _conv_pad_x = conv_info.pad().first;
78 _conv_pad_y = conv_info.pad().second;
79 _border_size = BorderSize(_conv_pad_y, _conv_pad_x);
Giorgio Arena93a690e2017-08-01 16:09:33 +010080
81 // Set build options
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010082 ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3);
83 std::set<std::string> options{ "-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x) };
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010084 if(_biases != nullptr)
85 {
86 options.emplace("-DHAS_BIAS");
87 }
Giorgio Arena93a690e2017-08-01 16:09:33 +010088
89 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("depthwise_convolution_3x3", options));
90
91 // Configure kernel window
92 const unsigned int num_elems_processed_per_iteration = 2;
93 const unsigned int num_elems_written_per_iteration = 2;
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010094 const unsigned int num_elems_read_per_iteration = 3 + _conv_stride_x;
Giorgio Arena93a690e2017-08-01 16:09:33 +010095 const unsigned int num_rows_read_per_iteration = 3;
96
97 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
98
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010099 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 +0100100 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
101 AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1));
102
103 update_window_and_padding(win, input_access, weights_access, output_access);
104
105 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
106
107 ICLKernel::configure(win);
108}
109
Giorgio Arena9fe41442017-08-23 16:36:24 +0100110void CLDepthwiseConvolution3x3Kernel::run(const Window &window, cl::CommandQueue &queue)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100111{
112 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
113 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
114
115 Window slice_in = window.first_slice_window_3D();
116 Window slice_out = window.first_slice_window_3D();
117 Window slice_weights = window.first_slice_window_3D();
118
119 slice_in.adjust(Window::DimX, -_conv_pad_x, true);
120 slice_in.adjust(Window::DimY, -_conv_pad_y, true);
121 slice_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
122 slice_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
123 slice_weights.set_dimension_step(Window::DimX, 0);
124 slice_weights.set_dimension_step(Window::DimY, 0);
125
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100126 // Set biases
127 if(_biases != nullptr)
128 {
129 unsigned int idx = 3 * num_arguments_per_3D_tensor();
130 Window slice_biases;
131 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
132 add_1D_tensor_argument(idx, _biases, slice_biases);
133 }
134
Giorgio Arena93a690e2017-08-01 16:09:33 +0100135 do
136 {
137 unsigned int idx = 0;
138 add_3D_tensor_argument(idx, _input, slice_in);
139 add_3D_tensor_argument(idx, _output, slice_out);
140 add_3D_tensor_argument(idx, _weights, slice_weights);
141
142 enqueue(queue, *this, slice_out);
143 }
144 while(window.slide_window_slice_3D(slice_out));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100145}