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