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