Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 1 | /* |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 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 "arm_compute/core/GLES_COMPUTE/kernels/GCDepthwiseConvolutionLayer3x3Kernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/IGCKernel.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.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" |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 37 | |
| 38 | using namespace arm_compute; |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 39 | using namespace arm_compute::misc::shape_calculator; |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 40 | |
| 41 | GCDepthwiseConvolutionLayer3x3Kernel::GCDepthwiseConvolutionLayer3x3Kernel() |
| 42 | : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0), _lws(gles::NDRange(1U, 1U, 1U)) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | BorderSize GCDepthwiseConvolutionLayer3x3Kernel::border_size() const |
| 47 | { |
| 48 | return _border_size; |
| 49 | } |
| 50 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 51 | void GCDepthwiseConvolutionLayer3x3Kernel::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, |
| 52 | unsigned int depth_multiplier) |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16); |
| 55 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 56 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); |
| 57 | |
| 58 | if(biases != nullptr) |
| 59 | { |
| 60 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 61 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2)); |
| 62 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 63 | } |
| 64 | |
| 65 | // Get convolved dimensions |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 66 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 67 | |
| 68 | // Output auto inizialitation if not yet initialized |
| 69 | auto_init_if_empty(*output->info(), |
| 70 | output_shape, |
| 71 | 1, |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 72 | input->info()->data_type()); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 73 | |
| 74 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_ERROR_ON(output->info()->dimension(2) != weights->info()->dimension(2)); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 76 | |
| 77 | _input = input; |
| 78 | _output = output; |
| 79 | _weights = weights; |
| 80 | _biases = biases; |
| 81 | _conv_stride_x = conv_info.stride().first; |
| 82 | _conv_stride_y = conv_info.stride().second; |
| 83 | _conv_pad_left = conv_info.pad_left(); |
| 84 | _conv_pad_top = conv_info.pad_top(); |
| 85 | _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left); |
| 86 | |
| 87 | // Set build options |
| 88 | ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3); |
| 89 | std::set<std::string> options; |
| 90 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 91 | options.emplace("#define DEPTH_MULTIPLIER " + support::cpp11::to_string(depth_multiplier)); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 92 | options.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0])); |
| 93 | options.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1])); |
| 94 | options.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2])); |
| 95 | options.emplace("#define STRIDE_X " + support::cpp11::to_string(_conv_stride_x)); |
| 96 | options.emplace("#define STRIDE_Y " + support::cpp11::to_string(_conv_stride_y)); |
| 97 | |
| 98 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 99 | options.emplace(("#define " + dt_name)); |
| 100 | |
| 101 | unsigned int num_elems_read_per_iteration_x = 8; |
| 102 | unsigned int num_elems_read_per_iteration_y = 1; |
| 103 | unsigned int num_elems_written_per_iteration_x = 4; |
| 104 | unsigned int num_elems_written_per_iteration_y = 1; |
| 105 | unsigned int num_elems_written_per_iteration_z = 1; |
| 106 | |
| 107 | if((_conv_stride_x == 1) && (_conv_stride_y == 1)) |
| 108 | { |
| 109 | switch(input->info()->data_type()) |
| 110 | { |
| 111 | #define PROCESS_4X_3Y_1Z |
| 112 | |
| 113 | case DataType::F16: |
| 114 | #if defined(PROCESS_4X_3Y_1Z) |
| 115 | options.emplace("#define PROCESS_4X_3Y_1Z"); |
| 116 | num_elems_read_per_iteration_y = 5; |
| 117 | num_elems_written_per_iteration_y = 3; |
| 118 | #endif /* PROCESS_4X_3Y_1Z */ |
| 119 | #undef PROCESS_4X_3Y_1Z |
| 120 | break; |
| 121 | |
| 122 | default: |
| 123 | ARM_COMPUTE_ERROR("Current data type is not supported"); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | switch(input->info()->data_type()) |
| 130 | { |
| 131 | case DataType::F16: |
| 132 | options.emplace("#define PROCESS_4X_1Y_1Z"); |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | ARM_COMPUTE_ERROR("Current data type is not supported"); |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if(_biases != nullptr) |
| 142 | { |
| 143 | options.emplace("#define BIAS"); |
| 144 | } |
| 145 | |
| 146 | // Create kernel |
| 147 | std::string kernel_name = "depthwise_convolution_3x3"; |
| 148 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, options)); |
| 149 | |
| 150 | // Calculate output right and bottom border |
| 151 | const int output_width = output->info()->dimension(0); |
| 152 | const int output_height = output->info()->dimension(1); |
| 153 | const int output_padding_right = ceil_to_multiple(output_width, num_elems_written_per_iteration_x * _lws[0]) - output_width; |
| 154 | const int output_padding_bottom = ceil_to_multiple(output_height, num_elems_written_per_iteration_y * _lws[1]) - output_height; |
| 155 | |
| 156 | // Calculate input right and bottom border |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 157 | const int input_width = input->info()->dimension(0); |
| 158 | const int input_height = input->info()->dimension(1); |
| 159 | |
| 160 | const int input_total_width = std::max(int(input->info()->padding().left), int(_conv_pad_left)) + input_width + std::max(int(input->info()->padding().right), int(_conv_pad_left)); |
| 161 | const int input_total_height = std::max(int(input->info()->padding().top), int(_conv_pad_top)) + input_height + std::max(int(input->info()->padding().bottom), int(_conv_pad_top)); |
| 162 | |
| 163 | const int input_padding_right = ceil_to_multiple(input_total_width, num_elems_read_per_iteration_x * _lws[0]) - input_width - _conv_pad_left; |
| 164 | const int input_padding_bottom = ceil_to_multiple(input_total_height, num_elems_read_per_iteration_y * _lws[1]) - input_height - _conv_pad_top; |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 165 | |
| 166 | BorderSize border = BorderSize(0, output_padding_right, output_padding_bottom, 0); |
| 167 | |
| 168 | Window win = calculate_max_enlarged_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y, num_elems_written_per_iteration_z), border); |
| 169 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 170 | AccessWindowStatic input_access(input->info(), -_conv_pad_left, -_conv_pad_top, input_width + input_padding_right, input_height + input_padding_bottom); |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 171 | AccessWindowStatic weights_access = AccessWindowStatic(nullptr, 0, 0, 0, 0); |
| 172 | AccessWindowStatic bias_access = AccessWindowStatic(nullptr, 0, 0, 0, 1); |
| 173 | |
| 174 | switch(weights->info()->data_type()) |
| 175 | { |
| 176 | case DataType::F16: |
| 177 | weights_access = AccessWindowStatic(weights->info(), 0, 0, 4, 3); |
| 178 | if(_biases != nullptr) |
| 179 | { |
| 180 | bias_access = AccessWindowStatic(_biases->info(), 0, 0, _biases->info()->dimension(0) + 1, 1); |
| 181 | } |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | ARM_COMPUTE_ERROR("Current data type is not supported"); |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height + output_padding_bottom); |
| 190 | |
| 191 | if(_biases != nullptr) |
| 192 | { |
| 193 | update_window_and_padding(win, input_access, weights_access, bias_access, output_access); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | update_window_and_padding(win, input_access, weights_access, output_access); |
| 198 | } |
| 199 | |
| 200 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 201 | |
| 202 | IGCKernel::configure(win); |
| 203 | } |
| 204 | |
| 205 | void GCDepthwiseConvolutionLayer3x3Kernel::run(const Window &window) |
| 206 | { |
| 207 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 208 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 209 | |
| 210 | _kernel.use(); |
| 211 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 212 | _output->set_needs_shifting(true); |
| 213 | |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 214 | // Create input window and adjust |
| 215 | Window win_in = window; |
| 216 | win_in.adjust(Window::DimX, -_conv_pad_left, true); |
| 217 | win_in.adjust(Window::DimY, -_conv_pad_top, true); |
| 218 | win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); |
| 219 | win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); |
| 220 | |
| 221 | Window slice_in = win_in.first_slice_window_3D(); |
| 222 | Window slice_out = window.first_slice_window_3D(); |
| 223 | Window slice_weights = window.first_slice_window_3D(); |
| 224 | slice_weights.set_dimension_step(Window::DimX, 0); |
| 225 | slice_weights.set_dimension_step(Window::DimY, 0); |
| 226 | |
| 227 | // Set biases |
| 228 | if(_biases != nullptr) |
| 229 | { |
| 230 | unsigned int idx = 3 * num_arguments_per_3D_tensor(); |
| 231 | Window slice_biases; |
| 232 | slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); |
| 233 | add_1D_tensor_argument(idx, _biases, 4, slice_biases); |
| 234 | } |
| 235 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 236 | slice_out.shift(Window::DimX, -(_output->info()->padding()).left); |
| 237 | |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 238 | do |
| 239 | { |
| 240 | unsigned int idx = 0; |
| 241 | add_3D_tensor_argument(idx, _input, 1, slice_in); |
| 242 | add_3D_tensor_argument(idx, _output, 2, slice_out); |
| 243 | add_3D_tensor_argument(idx, _weights, 3, slice_weights); |
| 244 | |
| 245 | _kernel.update_shader_params(); |
| 246 | enqueue(*this, slice_out, _lws); |
| 247 | } |
| 248 | while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in)); |
| 249 | } |