steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [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/CL/kernels/CLDirectConvolutionLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 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/IAccessWindow.h" |
| 34 | #include "arm_compute/core/ITensor.h" |
| 35 | #include "arm_compute/core/Types.h" |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Utils.h" |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 38 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 39 | #include "support/ToolchainSupport.h" |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 46 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 50 | |
| 51 | const DataLayout data_layout = input->data_layout(); |
| 52 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 53 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 54 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 55 | |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx), "Weights should have same width and height"); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5, |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 58 | "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported"); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != input->dimension(channel_idx), |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 60 | "Weights feature map dimension should match the respective input's one"); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional"); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 1) && std::get<0>(conv_info.stride()) > 3, "Strides larger than 3 not supported for 1x1 convolution."); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 3 || weights->dimension(width_idx) == 5) && std::get<0>(conv_info.stride()) > 2, |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 64 | "Strides larger than 2 not supported for 3x3 convolution."); |
| 65 | |
| 66 | if(biases != nullptr) |
| 67 | { |
| 68 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 69 | { |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 75 | } |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3), |
| 77 | "Biases size and number of input feature maps should match"); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, |
| 79 | "Biases should be one dimensional"); |
| 80 | } |
| 81 | |
| 82 | // Checks performed when output is configured |
| 83 | if(output->total_size() != 0) |
| 84 | { |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 86 | misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info)); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 90 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 93 | inline bool can_run_optimized_kernel_for_bifrost(GPUTarget gpu_target, unsigned int conv_stride_x, unsigned int conv_stride_y, unsigned int kernel_size, |
| 94 | DataType data_type, DataLayout data_layout) |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 95 | { |
Georgios Pinitas | a34286e | 2018-09-04 12:18:50 +0100 | [diff] [blame] | 96 | return gpu_target_is_in(gpu_target, |
| 97 | GPUTarget::G71, GPUTarget::G72, GPUTarget::G76, |
| 98 | GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, |
| 99 | GPUTarget::G52, GPUTarget::G52LIT) |
| 100 | && (kernel_size <= 5) |
| 101 | && (conv_stride_x == 1) && (conv_stride_y == 1) |
| 102 | && (data_type == DataType::F32) |
| 103 | && (data_layout == DataLayout::NCHW); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 104 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 105 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 106 | inline void setup_num_elems(unsigned int &num_elems_read_per_iteration_x, unsigned int &num_elems_read_per_iteration_y, |
| 107 | unsigned int &num_elems_written_per_iteration_x, unsigned int &num_elems_written_per_iteration_y, |
| 108 | unsigned int kernel_size, const PadStrideInfo &conv_info, const GPUTarget target, ITensorInfo *input) |
| 109 | { |
| 110 | const DataType data_type = input->data_type(); |
| 111 | const DataLayout data_layout = input->data_layout(); |
| 112 | unsigned int conv_stride_x = std::get<0>(conv_info.stride()); |
| 113 | unsigned int conv_stride_y = std::get<1>(conv_info.stride()); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 114 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 115 | const bool run_optimized_bifrost = can_run_optimized_kernel_for_bifrost(target, conv_stride_x, conv_stride_y, kernel_size, data_type, data_layout); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 116 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 117 | if(run_optimized_bifrost) |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 118 | { |
| 119 | // Configure kernel window |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 120 | switch(kernel_size) |
| 121 | { |
| 122 | case 1: |
| 123 | { |
| 124 | num_elems_read_per_iteration_x = 4; |
| 125 | num_elems_read_per_iteration_y = 4; |
| 126 | num_elems_written_per_iteration_x = 4; |
| 127 | num_elems_written_per_iteration_y = 4; |
| 128 | break; |
| 129 | } |
| 130 | case 3: |
| 131 | { |
| 132 | num_elems_read_per_iteration_x = 6; |
| 133 | num_elems_read_per_iteration_y = 5; |
| 134 | num_elems_written_per_iteration_x = 4; |
| 135 | num_elems_written_per_iteration_y = 3; |
| 136 | break; |
| 137 | } |
| 138 | case 5: |
| 139 | { |
| 140 | num_elems_read_per_iteration_x = 8; |
| 141 | num_elems_read_per_iteration_y = 6; |
| 142 | num_elems_written_per_iteration_x = 4; |
| 143 | num_elems_written_per_iteration_y = 2; |
| 144 | break; |
| 145 | } |
| 146 | default: |
| 147 | { |
| 148 | ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost"); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 154 | num_elems_read_per_iteration_y = kernel_size; |
| 155 | num_elems_written_per_iteration_x = 8; |
| 156 | num_elems_written_per_iteration_y = 1; |
Anthony Barbier | cc9fed5 | 2017-12-13 10:46:00 +0000 | [diff] [blame] | 157 | switch(kernel_size) |
| 158 | { |
| 159 | case 1: |
| 160 | switch(conv_stride_x) |
| 161 | { |
| 162 | case 1: |
| 163 | num_elems_read_per_iteration_x = 8; |
| 164 | break; |
| 165 | case 2: |
| 166 | num_elems_read_per_iteration_x = 16; |
| 167 | break; |
| 168 | case 3: |
| 169 | switch(input->element_size()) |
| 170 | { |
| 171 | case 1: |
| 172 | num_elems_read_per_iteration_x = 28; |
| 173 | break; |
| 174 | case 2: |
| 175 | num_elems_read_per_iteration_x = 24; |
| 176 | break; |
| 177 | case 4: |
| 178 | num_elems_read_per_iteration_x = 22; |
| 179 | break; |
| 180 | default: |
| 181 | ARM_COMPUTE_ERROR("Invalid data size"); |
| 182 | } |
| 183 | break; |
| 184 | default: |
| 185 | ARM_COMPUTE_ERROR("Invalid convolution stride X"); |
| 186 | } |
| 187 | break; |
| 188 | case 3: |
| 189 | switch(conv_stride_x) |
| 190 | { |
| 191 | case 1: |
| 192 | num_elems_read_per_iteration_x = 10; |
| 193 | break; |
| 194 | case 2: |
| 195 | num_elems_read_per_iteration_x = 17; |
| 196 | break; |
| 197 | default: |
| 198 | ARM_COMPUTE_ERROR("Invalid convolution stride X"); |
| 199 | } |
| 200 | break; |
| 201 | case 5: |
| 202 | switch(conv_stride_x) |
| 203 | { |
| 204 | case 1: |
| 205 | num_elems_read_per_iteration_x = 12; |
| 206 | break; |
| 207 | case 2: |
| 208 | num_elems_read_per_iteration_x = 20; |
| 209 | break; |
| 210 | default: |
| 211 | ARM_COMPUTE_ERROR("Invalid convolution stride X"); |
| 212 | } |
| 213 | break; |
| 214 | default: |
| 215 | ARM_COMPUTE_ERROR("Invalid direct convolution size"); |
| 216 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 219 | if(data_layout == DataLayout::NHWC) |
| 220 | { |
| 221 | num_elems_written_per_iteration_x = 1; |
| 222 | num_elems_read_per_iteration_x = 1; |
| 223 | switch(kernel_size) |
| 224 | { |
| 225 | case 1: |
| 226 | switch(conv_stride_x) |
| 227 | { |
| 228 | case 1: |
| 229 | num_elems_read_per_iteration_y = 8; |
| 230 | num_elems_written_per_iteration_y = 8; |
| 231 | break; |
| 232 | case 2: |
| 233 | num_elems_read_per_iteration_y = 16; |
| 234 | num_elems_written_per_iteration_y = 8; |
| 235 | break; |
| 236 | default: |
| 237 | ARM_COMPUTE_ERROR("Invalid convolution stride X"); |
| 238 | } |
| 239 | break; |
| 240 | case 3: |
| 241 | switch(conv_stride_x) |
| 242 | { |
| 243 | case 1: |
| 244 | num_elems_read_per_iteration_y = 10; |
| 245 | num_elems_written_per_iteration_y = 8; |
| 246 | break; |
| 247 | case 2: |
| 248 | num_elems_read_per_iteration_y = 17; |
| 249 | num_elems_written_per_iteration_y = 8; |
| 250 | break; |
| 251 | default: |
| 252 | ARM_COMPUTE_ERROR("Invalid convolution stride X"); |
| 253 | } |
| 254 | break; |
| 255 | case 5: |
| 256 | switch(conv_stride_x) |
| 257 | { |
| 258 | case 1: |
| 259 | num_elems_read_per_iteration_y = 12; |
| 260 | num_elems_written_per_iteration_y = 8; |
| 261 | break; |
| 262 | case 2: |
| 263 | num_elems_read_per_iteration_y = 20; |
| 264 | num_elems_written_per_iteration_y = 8; |
| 265 | break; |
| 266 | default: |
| 267 | ARM_COMPUTE_ERROR("Invalid convolution stride X"); |
| 268 | } |
| 269 | break; |
| 270 | default: |
| 271 | ARM_COMPUTE_ERROR("Not implemented."); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info, const GPUTarget target) |
| 278 | { |
| 279 | const DataLayout data_layout = input->data_layout(); |
| 280 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 281 | const unsigned int kernel_size = weights->dimension(width_idx); |
| 282 | |
| 283 | // Get convolved dimensions |
| 284 | TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info); |
| 285 | |
| 286 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 287 | // TODO(COMPMID-2078): input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet). |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 288 | auto_init_if_empty(*output, output_shape, |
| 289 | 1, |
| 290 | input->data_type(), |
| 291 | input->quantization_info()); |
| 292 | |
| 293 | unsigned int num_elems_read_per_iteration_x = 0; |
| 294 | unsigned int num_elems_read_per_iteration_y = 0; |
| 295 | unsigned int num_elems_written_per_iteration_x = 0; |
| 296 | unsigned int num_elems_written_per_iteration_y = 0; |
| 297 | |
| 298 | unsigned int conv_pad_left = conv_info.pad_left(); |
| 299 | unsigned int conv_pad_top = conv_info.pad_top(); |
| 300 | unsigned int conv_stride_x = std::get<0>(conv_info.stride()); |
| 301 | unsigned int conv_stride_y = std::get<1>(conv_info.stride()); |
| 302 | |
| 303 | setup_num_elems(num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, |
| 304 | num_elems_written_per_iteration_x, num_elems_written_per_iteration_y, |
| 305 | kernel_size, conv_info, target, input); |
| 306 | |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 307 | // Create window and update padding |
Anthony Barbier | cc9fed5 | 2017-12-13 10:46:00 +0000 | [diff] [blame] | 308 | bool window_changed = false; |
| 309 | Window win = calculate_max_window(*output, Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 310 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 311 | if(data_layout == DataLayout::NHWC) |
| 312 | { |
| 313 | AccessWindowStatic input_access(input, 0, -conv_pad_left, |
| 314 | num_elems_read_per_iteration_x, |
| 315 | ceil_to_multiple(input->dimension(1) + conv_info.pad_right(), num_elems_read_per_iteration_y)); |
| 316 | AccessWindowStatic weights_access(weights, 0, 0, weights->dimension(0), weights->dimension(1)); |
| 317 | AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); |
| 318 | window_changed = update_window_and_padding(win, input_access, weights_access, output_access); |
| 319 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 320 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 321 | return std::make_pair(err, win); |
| 322 | } |
| 323 | else if(data_layout == DataLayout::NCHW) |
| 324 | { |
| 325 | AccessWindowRectangle input_access(input, -conv_pad_left, -conv_pad_top, num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, conv_stride_x, conv_stride_y); |
| 326 | AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size); |
| 327 | AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); |
| 328 | window_changed = update_window_and_padding(win, input_access, weights_access, output_access); |
| 329 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 330 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 331 | return std::make_pair(err, win); |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | ARM_COMPUTE_ERROR("Not supported"); |
| 336 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 337 | } |
| 338 | } // namespace |
| 339 | |
| 340 | CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel() |
| 341 | : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_stride_x(0), _conv_stride_y(0) |
| 342 | { |
| 343 | } |
| 344 | |
| 345 | BorderSize CLDirectConvolutionLayerKernel::border_size() const |
| 346 | { |
| 347 | return _border_size; |
| 348 | } |
| 349 | |
| 350 | void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info) |
| 351 | { |
| 352 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 353 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 354 | const DataLayout data_layout = input->info()->data_layout(); |
| 355 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 356 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 357 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 358 | |
| 359 | const unsigned int kernel_size = weights->info()->dimension(width_idx); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 360 | const DataType data_type = input->info()->data_type(); |
| 361 | |
| 362 | // Get convolved dimensions |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 363 | TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input->info(), *weights->info(), conv_info); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 364 | |
| 365 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 366 | // TODO(COMPMID-2078): input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet). |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 367 | auto_init_if_empty(*output->info(), |
| 368 | output_shape, |
| 369 | 1, |
| 370 | input->info()->data_type(), |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 371 | input->info()->quantization_info()); |
| 372 | |
| 373 | // Perform validation step |
| 374 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), |
| 375 | weights->info(), |
| 376 | (biases != nullptr) ? biases->info() : nullptr, |
| 377 | output->info(), |
| 378 | conv_info)); |
| 379 | |
| 380 | _conv_stride_x = std::get<0>(conv_info.stride()); |
| 381 | _conv_stride_y = std::get<1>(conv_info.stride()); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 382 | |
| 383 | if(data_layout == DataLayout::NHWC) |
| 384 | { |
| 385 | _border_size = BorderSize(conv_info.pad_left(), 0, conv_info.pad_right(), 0); |
| 386 | } |
| 387 | else if(data_layout == DataLayout::NCHW) |
| 388 | { |
| 389 | _border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left()); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | ARM_COMPUTE_ERROR("Not supported"); |
| 394 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 395 | |
| 396 | _input = input; |
| 397 | _weights = weights; |
| 398 | _output = output; |
| 399 | _biases = biases; |
| 400 | |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 401 | const GPUTarget gpu_target = get_target(); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 402 | |
| 403 | std::stringstream kernel_name; |
| 404 | kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size; |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 405 | if(data_layout == DataLayout::NHWC) |
| 406 | { |
| 407 | kernel_name << "_" << lower_string(string_from_data_layout(data_layout)); |
| 408 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 409 | |
| 410 | CLBuildOptions build_options; |
| 411 | build_options.add_option_if(_biases != nullptr, std::string("-DHAS_BIAS")); |
| 412 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 413 | const bool run_optimized_for_bifrost = can_run_optimized_kernel_for_bifrost(gpu_target, _conv_stride_x, _conv_stride_y, kernel_size, data_type, data_layout); |
| 414 | |
| 415 | if(run_optimized_for_bifrost) |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 416 | { |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 417 | build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(channel_idx)))); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 418 | |
| 419 | kernel_name << "_f32_bifrost"; |
| 420 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_options.options())); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 421 | } |
| 422 | else |
| 423 | { |
Pablo Tello | d041a83 | 2018-10-03 17:11:09 +0100 | [diff] [blame] | 424 | const bool is_quantized_asymm = is_data_type_quantized_asymmetric(data_type); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 425 | build_options.add_option_if(is_quantized_asymm, std::string("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size))); |
| 426 | build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); |
| 427 | build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type))); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 428 | build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(channel_idx)))); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 429 | build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x))); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 430 | if(data_layout == DataLayout::NHWC) |
| 431 | { |
| 432 | build_options.add_option(std::string("-DDATA_LAYOUT_NHWC=1")); |
| 433 | build_options.add_option(std::string("-DDST_HEIGHT=" + support::cpp11::to_string(_output->info()->dimension(height_idx)))); |
| 434 | build_options.add_option(std::string("-DDST_WIDTH=" + support::cpp11::to_string(_output->info()->dimension(width_idx)))); |
| 435 | build_options.add_option(std::string("-DSRC_HEIGHT=" + support::cpp11::to_string(_input->info()->dimension(height_idx)))); |
| 436 | build_options.add_option(std::string("-DSRC_WIDTH=" + support::cpp11::to_string(_input->info()->dimension(width_idx)))); |
| 437 | build_options.add_option(std::string("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()))); |
| 438 | build_options.add_option(std::string("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()))); |
| 439 | build_options.add_option(std::string("-DSTRIDE_Y=" + support::cpp11::to_string(_conv_stride_y))); |
| 440 | } |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 441 | build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type))); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 442 | // Create kernel |
| 443 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(is_quantized_asymm ? "direct_convolution_1x1_3x3_5x5_quantized" : kernel_name.str(), |
| 444 | build_options.options())); |
| 445 | } |
| 446 | |
| 447 | // Configure kernel window |
| 448 | auto win_config = validate_and_configure_window(input->info(), weights->info(), output->info(), conv_info, gpu_target); |
| 449 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 450 | ICLKernel::configure_internal(win_config.second); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 451 | |
| 452 | // Set static kernel arguments |
| 453 | if(is_data_type_quantized_asymmetric(data_type)) |
| 454 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 455 | const UniformQuantizationInfo iqinfo = _input->info()->quantization_info().uniform(); |
| 456 | const UniformQuantizationInfo wqinfo = _weights->info()->quantization_info().uniform(); |
| 457 | const UniformQuantizationInfo oqinfo = _output->info()->quantization_info().uniform(); |
| 458 | |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 459 | int output_multiplier = 0; |
| 460 | int output_shift = 0; |
| 461 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 462 | float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale; |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 463 | ARM_COMPUTE_THROW_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift)); |
| 464 | |
| 465 | unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0) + 1; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 466 | _kernel.setArg(idx++, -iqinfo.offset); |
| 467 | _kernel.setArg(idx++, -wqinfo.offset); |
| 468 | _kernel.setArg(idx++, oqinfo.offset); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 469 | _kernel.setArg(idx++, output_multiplier); |
| 470 | _kernel.setArg(idx++, output_shift); |
| 471 | } |
| 472 | |
| 473 | // Set config_id for enabling LWS tuning |
| 474 | _config_id = "direct_convolution_"; |
| 475 | _config_id += lower_string(string_from_data_type(data_type)); |
| 476 | _config_id += "_"; |
| 477 | _config_id += support::cpp11::to_string(kernel_size); |
| 478 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 479 | _config_id += support::cpp11::to_string(border_size().left); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 480 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 481 | _config_id += support::cpp11::to_string(border_size().top); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 482 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 483 | _config_id += support::cpp11::to_string(border_size().right); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 484 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 485 | _config_id += support::cpp11::to_string(border_size().bottom); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 486 | _config_id += "_"; |
| 487 | _config_id += support::cpp11::to_string(_conv_stride_x); |
| 488 | _config_id += "_"; |
| 489 | _config_id += support::cpp11::to_string(_conv_stride_y); |
| 490 | _config_id += "_"; |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 491 | _config_id += support::cpp11::to_string(output->info()->dimension(width_idx)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 492 | _config_id += "_"; |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 493 | _config_id += support::cpp11::to_string(output->info()->dimension(height_idx)); |
| 494 | _config_id += "_"; |
| 495 | _config_id += lower_string(string_from_data_layout(data_layout)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 498 | Status CLDirectConvolutionLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 499 | const GPUTarget target) |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 500 | { |
| 501 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info)); |
| 502 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), conv_info, target).first); |
| 503 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 504 | return Status{}; |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 505 | } |
| 506 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 507 | void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 508 | { |
| 509 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 510 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 511 | |
| 512 | // Get initial windows |
| 513 | Window slice = window.first_slice_window_3D(); |
| 514 | Window win_in = window; |
| 515 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 516 | win_in.adjust(Window::DimX, -_border_size.left, true); |
| 517 | win_in.adjust(Window::DimY, -_border_size.top, true); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 518 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 519 | const DataLayout data_layout = _input->info()->data_layout(); |
| 520 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 521 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 522 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 523 | win_in.set_dimension_step(width_idx, window[width_idx].step() * _conv_stride_x); |
| 524 | win_in.set_dimension_step(height_idx, window[height_idx].step() * _conv_stride_y); |
| 525 | |
| 526 | Window slice_in = win_in.first_slice_window_3D(); |
| 527 | unsigned int idx1 = 2 * num_arguments_per_3D_tensor(); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 528 | add_3D_tensor_argument(idx1, _weights, slice); |
| 529 | |
| 530 | if(_biases != nullptr) |
| 531 | { |
| 532 | Window slice_biases; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 533 | slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 534 | add_1D_tensor_argument(idx1, _biases, slice_biases); |
| 535 | } |
| 536 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 537 | _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3])); |
| 538 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 539 | do |
| 540 | { |
| 541 | unsigned int idx = 0; |
| 542 | add_3D_tensor_argument(idx, _input, slice_in); |
| 543 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 544 | enqueue(queue, *this, slice, lws_hint()); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 545 | } |
| 546 | while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in)); |
| 547 | } |