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 | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3Kernel.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" |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 36 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
| 42 | /** Calculates expected output shape dimension |
| 43 | * |
| 44 | * @param[in] Input shape |
| 45 | * |
| 46 | * @return Expected output shape |
| 47 | */ |
| 48 | TensorShape get_output_shape(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info) |
| 49 | { |
| 50 | unsigned int output_width = 0; |
| 51 | unsigned int output_height = 0; |
| 52 | |
| 53 | std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info); |
| 54 | |
| 55 | TensorShape output_shape = input_shape; |
| 56 | output_shape.set(0, output_width); |
| 57 | output_shape.set(1, output_height); |
| 58 | |
| 59 | return output_shape; |
| 60 | } |
| 61 | } // namespace |
| 62 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 63 | CLDepthwiseConvolutionLayer3x3Kernel::CLDepthwiseConvolutionLayer3x3Kernel() |
Jaroslaw Rzepecki | 16cdf89 | 2017-10-27 13:15:03 +0100 | [diff] [blame] | 64 | : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 68 | BorderSize CLDepthwiseConvolutionLayer3x3Kernel::border_size() const |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 69 | { |
| 70 | return _border_size; |
| 71 | } |
| 72 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 73 | void CLDepthwiseConvolutionLayer3x3Kernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 74 | { |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 75 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); |
| 78 | |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 79 | if(biases != nullptr) |
| 80 | { |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 81 | if(is_data_type_quantized_asymmetric(weights->info()->data_type())) |
| 82 | { |
| 83 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 88 | } |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2)); |
| 90 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 91 | } |
| 92 | |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 93 | // Get convolved dimensions |
| 94 | TensorShape output_shape = get_output_shape(input->info()->tensor_shape(), weights->info()->tensor_shape(), conv_info); |
Georgios Pinitas | c26ecf8 | 2017-09-22 13:44:05 +0100 | [diff] [blame] | 95 | |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 96 | // Output auto inizialitation if not yet initialized |
| 97 | auto_init_if_empty(*output->info(), |
| 98 | output_shape, |
| 99 | 1, |
| 100 | input->info()->data_type(), |
| 101 | input->info()->fixed_point_position(), |
| 102 | input->info()->quantization_info()); |
| 103 | |
| 104 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Georgios Pinitas | c26ecf8 | 2017-09-22 13:44:05 +0100 | [diff] [blame] | 105 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 106 | _input = input; |
| 107 | _output = output; |
| 108 | _weights = weights; |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 109 | _biases = biases; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 110 | _conv_stride_x = conv_info.stride().first; |
| 111 | _conv_stride_y = conv_info.stride().second; |
Jaroslaw Rzepecki | 16cdf89 | 2017-10-27 13:15:03 +0100 | [diff] [blame] | 112 | _conv_pad_left = conv_info.pad_left(); |
| 113 | _conv_pad_top = conv_info.pad_top(); |
| 114 | _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 115 | |
| 116 | // Set build options |
Georgios Pinitas | c26ecf8 | 2017-09-22 13:44:05 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 118 | CLBuildOptions build_opts; |
| 119 | build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x)); |
| 120 | build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS"); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 121 | |
Anthony Barbier | a2ea753 | 2017-11-28 10:33:22 +0000 | [diff] [blame] | 122 | // Create kernel |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 123 | std::string kernel_name = is_data_type_quantized_asymmetric(_input->info()->data_type()) ? "depthwise_convolution_3x3_quantized" : "depthwise_convolution_3x3"; |
| 124 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 125 | |
Anthony Barbier | a2ea753 | 2017-11-28 10:33:22 +0000 | [diff] [blame] | 126 | // Set static arguments |
| 127 | if(is_data_type_quantized_asymmetric(_input->info()->data_type())) |
| 128 | { |
| 129 | float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale; |
| 130 | int output_multiplier = 0; |
| 131 | int output_shift = 0; |
| 132 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 133 | |
| 134 | unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0); |
| 135 | |
| 136 | _kernel.setArg(idx++, -_input->info()->quantization_info().offset); |
| 137 | _kernel.setArg(idx++, -_weights->info()->quantization_info().offset); |
| 138 | _kernel.setArg(idx++, _output->info()->quantization_info().offset); |
| 139 | _kernel.setArg(idx++, output_multiplier); |
| 140 | _kernel.setArg(idx++, output_shift); |
| 141 | } |
| 142 | |
| 143 | // Configure the local work size for Bifrost with a value obtained |
| 144 | // via exhaustive autotuning for the MobileNets tensor shapes. |
| 145 | const GPUTarget gpu_target = get_arch_from_target(get_target()); |
| 146 | if(gpu_target == GPUTarget::BIFROST) |
| 147 | { |
| 148 | const size_t width = input->info()->dimension(0); |
| 149 | if(width >= 56) // 56 or 112 |
| 150 | { |
| 151 | _lws_hint = cl::NDRange(8, 5, 2); |
| 152 | } |
| 153 | else if(width >= 14) // 14 or 28 |
| 154 | { |
| 155 | _lws_hint = cl::NDRange(1, 5, 2); |
| 156 | } |
| 157 | else // 7 |
| 158 | { |
| 159 | _lws_hint = cl::NDRange(1, 1, 2); |
| 160 | } |
| 161 | } |
| 162 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 163 | // Configure kernel window |
| 164 | const unsigned int num_elems_processed_per_iteration = 2; |
| 165 | const unsigned int num_elems_written_per_iteration = 2; |
Georgios Pinitas | c26ecf8 | 2017-09-22 13:44:05 +0100 | [diff] [blame] | 166 | const unsigned int num_elems_read_per_iteration = 3 + _conv_stride_x; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 167 | const unsigned int num_rows_read_per_iteration = 3; |
| 168 | |
| 169 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 170 | |
Georgios Pinitas | c26ecf8 | 2017-09-22 13:44:05 +0100 | [diff] [blame] | 171 | 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 Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 172 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 173 | AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1)); |
| 174 | |
| 175 | update_window_and_padding(win, input_access, weights_access, output_access); |
| 176 | |
| 177 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 178 | |
| 179 | ICLKernel::configure(win); |
| 180 | } |
| 181 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 182 | void CLDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, cl::CommandQueue &queue) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 183 | { |
| 184 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 185 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 186 | |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 187 | // Create input window and adjust |
| 188 | Window win_in = window; |
| 189 | win_in.adjust(Window::DimX, -_conv_pad_left, true); |
| 190 | win_in.adjust(Window::DimY, -_conv_pad_top, true); |
| 191 | win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); |
| 192 | win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); |
| 193 | |
| 194 | Window slice_in = win_in.first_slice_window_3D(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 195 | Window slice_out = window.first_slice_window_3D(); |
| 196 | Window slice_weights = window.first_slice_window_3D(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 197 | slice_weights.set_dimension_step(Window::DimX, 0); |
| 198 | slice_weights.set_dimension_step(Window::DimY, 0); |
| 199 | |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 200 | // Set biases |
| 201 | if(_biases != nullptr) |
| 202 | { |
| 203 | unsigned int idx = 3 * num_arguments_per_3D_tensor(); |
| 204 | Window slice_biases; |
| 205 | slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); |
| 206 | add_1D_tensor_argument(idx, _biases, slice_biases); |
| 207 | } |
| 208 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 209 | do |
| 210 | { |
| 211 | unsigned int idx = 0; |
| 212 | add_3D_tensor_argument(idx, _input, slice_in); |
| 213 | add_3D_tensor_argument(idx, _output, slice_out); |
| 214 | add_3D_tensor_argument(idx, _weights, slice_weights); |
| 215 | |
Anthony Barbier | a2ea753 | 2017-11-28 10:33:22 +0000 | [diff] [blame] | 216 | enqueue(queue, *this, slice_out, _lws_hint); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 217 | } |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 218 | while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in)); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 219 | } |