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