Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
| 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 Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 24 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolution3x3Kernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 25 | |
| 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 | |
| 37 | using namespace arm_compute; |
| 38 | |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 39 | CLDepthwiseConvolution3x3Kernel::CLDepthwiseConvolution3x3Kernel() |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 40 | : _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 Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 44 | BorderSize CLDepthwiseConvolution3x3Kernel::border_size() const |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 45 | { |
| 46 | return _border_size; |
| 47 | } |
| 48 | |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 49 | void CLDepthwiseConvolution3x3Kernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *weights, const PadStrideInfo &conv_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 50 | { |
| 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 | |
| 56 | _input = input; |
| 57 | _output = output; |
| 58 | _weights = weights; |
| 59 | _conv_stride_x = conv_info.stride().first; |
| 60 | _conv_stride_y = conv_info.stride().second; |
| 61 | _border_size = BorderSize(weights->info()->dimension(1) / 2, weights->info()->dimension(0) / 2); |
| 62 | _conv_pad_x = std::min(border_size().right, conv_info.pad().first); |
| 63 | _conv_pad_y = std::min(border_size().bottom, conv_info.pad().second); |
| 64 | |
| 65 | // Set build options |
| 66 | std::set<std::string> options; |
| 67 | |
| 68 | options.emplace("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x)); |
| 69 | |
| 70 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("depthwise_convolution_3x3", options)); |
| 71 | |
| 72 | // Configure kernel window |
| 73 | const unsigned int num_elems_processed_per_iteration = 2; |
| 74 | const unsigned int num_elems_written_per_iteration = 2; |
| 75 | const unsigned int num_elems_read_per_iteration = (_conv_stride_x == 1) ? 4 : (_conv_stride_x == 2) ? 5 : 6; |
| 76 | const unsigned int num_rows_read_per_iteration = 3; |
| 77 | |
| 78 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 79 | |
| 80 | const int access_right = border_size().left + ceil_to_multiple(border_size().left + input->info()->dimension(0), num_elems_read_per_iteration); |
| 81 | const int access_bottom = border_size().bottom + ceil_to_multiple(border_size().bottom + input->info()->dimension(1), num_rows_read_per_iteration); |
| 82 | |
| 83 | AccessWindowStatic input_access(input->info(), -border_size().left, -border_size().bottom, access_right, access_bottom); |
| 84 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 85 | AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1)); |
| 86 | |
| 87 | update_window_and_padding(win, input_access, weights_access, output_access); |
| 88 | |
| 89 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 90 | |
| 91 | ICLKernel::configure(win); |
| 92 | } |
| 93 | |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 94 | void CLDepthwiseConvolution3x3Kernel::run(const Window &window, cl::CommandQueue &queue) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 95 | { |
| 96 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 97 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 98 | |
| 99 | Window slice_in = window.first_slice_window_3D(); |
| 100 | Window slice_out = window.first_slice_window_3D(); |
| 101 | Window slice_weights = window.first_slice_window_3D(); |
| 102 | |
| 103 | slice_in.adjust(Window::DimX, -_conv_pad_x, true); |
| 104 | slice_in.adjust(Window::DimY, -_conv_pad_y, true); |
| 105 | slice_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); |
| 106 | slice_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); |
| 107 | slice_weights.set_dimension_step(Window::DimX, 0); |
| 108 | slice_weights.set_dimension_step(Window::DimY, 0); |
| 109 | |
| 110 | do |
| 111 | { |
| 112 | unsigned int idx = 0; |
| 113 | add_3D_tensor_argument(idx, _input, slice_in); |
| 114 | add_3D_tensor_argument(idx, _output, slice_out); |
| 115 | add_3D_tensor_argument(idx, _weights, slice_weights); |
| 116 | |
| 117 | enqueue(queue, *this, slice_out); |
| 118 | } |
| 119 | while(window.slide_window_slice_3D(slice_out)); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 120 | } |