steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 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 | |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame^] | 26 | #include "arm_compute/core/utils/ActivationFunctionUtils.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 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 | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/KernelDescriptors.h" |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/PixelValue.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame^] | 33 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 35 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame^] | 36 | #include "arm_compute/core/utils/StringUtils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 37 | #include "src/core/AccessWindowStatic.h" |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 38 | #include "src/core/CL/CLUtils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 39 | #include "src/core/CL/CLValidate.h" |
| 40 | #include "src/core/helpers/AutoConfiguration.h" |
| 41 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 42 | #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h" |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 43 | #include "support/Cast.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 44 | #include "support/StringSupport.h" |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 45 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 46 | namespace arm_compute |
| 47 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 48 | namespace opencl |
| 49 | { |
| 50 | namespace kernels |
| 51 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 52 | namespace |
| 53 | { |
Georgios Pinitas | 9fc3be6 | 2021-05-29 04:01:51 +0100 | [diff] [blame] | 54 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 55 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 56 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 60 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 61 | const DataLayout data_layout = src->data_layout(); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 62 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 63 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 64 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 65 | |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 66 | 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] | 67 | 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] | 68 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.export_input_to_cl_image == true, "Export to CLImage is not supported for the input tensor"); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.export_output_to_cl_image == true, "Export to CLImage is not supported for the output tensor"); |
| 71 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 72 | if(data_layout == DataLayout::NCHW) |
| 73 | { |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx), "Weights should have same width and height"); |
| 75 | 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."); |
| 76 | 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, |
| 77 | "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution."); |
SiCong Li | 1b2f868 | 2023-01-04 10:04:26 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(act_info.enabled(), "Fused activation is not supported for NCHW layout"); |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 79 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 80 | if(is_data_type_quantized(src->data_type())) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 81 | { |
| 82 | 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] | 83 | "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] | 84 | } |
| 85 | else |
| 86 | { |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5, |
| 88 | "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types"); |
| 89 | } |
| 90 | } |
| 91 | |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 92 | if(data_layout == DataLayout::NHWC) |
| 93 | { |
SiCong Li | 1b2f868 | 2023-01-04 10:04:26 +0000 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(act_info.enabled() && !is_data_type_float(src->data_type()), "Fused activation in NHWC is only supported for floating point."); |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 95 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.m0 <= 0 || desc.m0 > 8, "M0 can only be greater than 0 and less than or equal to 8"); |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 96 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 && desc.n0 != 16, |
| 97 | "N0 can only be: 1, 2, 3, 4, 8, and 16"); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16, |
| 99 | "K0 can only be: 1, 2, 3, 4, 8, and 16"); |
| 100 | if(desc.export_weights_to_cl_image) |
| 101 | { |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16, |
| 103 | "K0 can only be: 4, 8, and 16"); |
Gian Marco Iodice | ad9a7ed | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 104 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(weights), |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 105 | "Export to CLImage is not supported for this weight configuration"); |
| 106 | } |
| 107 | } |
| 108 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 109 | if(biases != nullptr) |
| 110 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 111 | if(is_data_type_quantized_asymmetric(src->data_type())) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 112 | { |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 118 | } |
| 119 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3), |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 120 | "Biases size and number of dst feature maps should match"); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 121 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, |
| 122 | "Biases should be one dimensional"); |
| 123 | } |
| 124 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 125 | // Checks performed when dst is configured |
| 126 | if(dst->total_size() != 0) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 127 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), |
| 129 | misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info)); |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 133 | const auto data_type = src->data_type(); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 134 | if(is_data_type_quantized(data_type)) |
| 135 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 136 | const UniformQuantizationInfo iqinfo = src->quantization_info().uniform(); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 137 | const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform(); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 138 | const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform(); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 139 | |
| 140 | float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale; |
| 141 | int output_multiplier = 0; |
| 142 | int output_shift = 0; |
| 143 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift)); |
| 144 | } |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 145 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 146 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 147 | } // namespace |
| 148 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 149 | ClDirectConv2dKernel::ClDirectConv2dKernel() |
| 150 | { |
| 151 | _type = CLKernelType::DIRECT; |
| 152 | } |
| 153 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 154 | void ClDirectConv2dKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 155 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc) |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 156 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 157 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 158 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 159 | // Perform validation |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 160 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info, act_info, desc)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 161 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 162 | const int conv_stride_x = std::get<0>(conv_info.stride()); |
| 163 | const int conv_stride_y = std::get<1>(conv_info.stride()); |
| 164 | |
| 165 | _data_layout = src->data_layout(); |
| 166 | _conv_info = conv_info; |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 167 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 168 | const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 169 | const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 170 | 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] | 171 | const unsigned int kernel_size = weights->dimension(width_idx); |
| 172 | const DataType data_type = src->data_type(); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 173 | |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 174 | const GPUTarget gpu_target = get_target(); |
| 175 | unsigned int _num_elems_processed_per_iteration = 0; |
| 176 | |
| 177 | // Get dst shape |
| 178 | TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info); |
| 179 | |
| 180 | // Output auto inizialitation if not yet initialized |
| 181 | auto_init_if_empty(*dst, output_shape, |
| 182 | 1, |
| 183 | src->data_type(), |
| 184 | src->quantization_info()); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 185 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 186 | // Configure kernel window |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 187 | Window win; |
| 188 | if(_data_layout == DataLayout::NHWC) |
| 189 | { |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 190 | output_shape.collapse(2U, 1U); |
| 191 | const unsigned int n0 = adjust_vec_size(desc.n0, output_shape[0]); |
| 192 | const unsigned int m0 = adjust_vec_size(desc.m0, output_shape[1]); |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 193 | |
| 194 | // Create window and update padding |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 195 | win = calculate_max_window(output_shape, Steps(n0, m0)); |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 196 | } |
| 197 | else if(_data_layout == DataLayout::NCHW) |
| 198 | { |
| 199 | _num_elems_processed_per_iteration = 1u; |
| 200 | win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration)); |
| 201 | } |
| 202 | |
| 203 | ICLKernel::configure_internal(win); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 204 | |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 205 | std::stringstream kernel_name; |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 206 | CLBuildOptions build_options; |
| 207 | |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 208 | if(_data_layout == DataLayout::NHWC) |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 209 | { |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 210 | kernel_name << "direct_convolution_nhwc"; |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 211 | |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 212 | const unsigned int n0 = win.x().step(); |
| 213 | const unsigned int m0 = win.y().step(); |
| 214 | const unsigned int k0 = adjust_vec_size(desc.k0, src->dimension(channel_idx)); |
| 215 | const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0; |
| 216 | const unsigned int pad_left = conv_info.pad_left(); |
| 217 | const unsigned int pad_top = conv_info.pad_top(); |
| 218 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 219 | _export_weights_to_cl_image = desc.export_weights_to_cl_image; |
| 220 | _export_input_to_cl_image = desc.export_input_to_cl_image; |
| 221 | _export_output_to_cl_image = desc.export_output_to_cl_image; |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 222 | |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 223 | // Update the padding for the weights tensor if we can export to cl_image |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 224 | if(_export_weights_to_cl_image) |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 225 | { |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 226 | gemm::update_padding_for_cl_image(weights); |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 227 | } |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 228 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 229 | if(_export_output_to_cl_image) |
| 230 | { |
| 231 | gemm::update_padding_for_cl_image(dst); |
| 232 | } |
| 233 | |
| 234 | if(_export_input_to_cl_image) |
| 235 | { |
| 236 | gemm::update_padding_for_cl_image(src); |
| 237 | } |
| 238 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 239 | if(biases != nullptr) |
Teresa Charlin | 8d8a1c5 | 2021-02-03 17:01:23 +0000 | [diff] [blame] | 240 | { |
| 241 | build_options.add_option(std::string("-DHAS_BIAS")); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 242 | 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] | 243 | } |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 244 | |
Gunes Bayir | fdb5342 | 2022-05-25 10:09:39 +0100 | [diff] [blame] | 245 | // Conditions of -cl-fast-relaxed-math causing accuracy issues can be traced from COMPMID-5324 |
| 246 | const auto act_function = act_info.activation(); |
| 247 | const auto dst_data_type = dst->data_type(); |
| 248 | |
| 249 | if((gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) |
| 250 | && (act_function == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU || act_function == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 251 | && (dst_data_type == DataType::F32 || dst_data_type == DataType::F16)) |
| 252 | { |
| 253 | // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations |
| 254 | // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations |
| 255 | build_options.add_option("-cl-unsafe-math-optimizations"); |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | build_options.add_option("-cl-fast-relaxed-math"); |
| 260 | } |
| 261 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 262 | build_options.add_option_if_else(_export_input_to_cl_image, "-DSRC_TENSOR_TYPE=IMAGE", "-DSRC_TENSOR_TYPE=BUFFER"); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 263 | build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type())); |
Gian Marco Iodice | 3394f3e | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 264 | build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(0))); |
| 265 | build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(1))); |
| 266 | build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(2))); |
| 267 | build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(0))); |
| 268 | build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst->dimension(1))); |
| 269 | build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(2))); |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 270 | build_options.add_option_if_else(_export_output_to_cl_image, "-DDST_TENSOR_TYPE=IMAGE", "-DDST_TENSOR_TYPE=BUFFER"); |
Gunes Bayir | fdb5342 | 2022-05-25 10:09:39 +0100 | [diff] [blame] | 271 | build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst_data_type)); |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 272 | build_options.add_option_if_else(_export_weights_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE", "-DWEI_TENSOR_TYPE=BUFFER"); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 273 | build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx))); |
| 274 | build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx))); |
| 275 | build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type())); |
| 276 | build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x)); |
| 277 | 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] | 278 | build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left)); |
| 279 | build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top)); |
| 280 | build_options.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 281 | build_options.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 282 | build_options.add_option("-DK0=" + support::cpp11::to_string(k0)); |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 283 | build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 284 | build_options.add_option_if((src->dimension(channel_idx) % k0) != 0, "-DLEFTOVER_LOOP"); |
Gunes Bayir | fdb5342 | 2022-05-25 10:09:39 +0100 | [diff] [blame] | 285 | build_options.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_function))); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 286 | |
| 287 | if(is_data_type_quantized(data_type)) |
| 288 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 289 | const UniformQuantizationInfo iqinfo = src->quantization_info().uniform(); |
| 290 | const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform(); |
| 291 | const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform(); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 292 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 293 | PixelValue zero_value = PixelValue(0, src->data_type(), src->quantization_info()); |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 294 | int zero_value_s32; |
| 295 | zero_value.get(zero_value_s32); |
| 296 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 297 | float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale; |
| 298 | int output_multiplier = 0; |
| 299 | int output_shift = 0; |
| 300 | quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 301 | build_options.add_option("-DIS_QUANTIZED"); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 302 | build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier)); |
| 303 | build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift)); |
| 304 | build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset)); |
| 305 | build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset)); |
| 306 | 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] | 307 | 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] | 308 | 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] | 309 | } |
| 310 | else |
| 311 | { |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 312 | 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] | 313 | build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0)); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 314 | build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0)); |
| 315 | build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0)); |
| 316 | build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0)); |
Georgios Pinitas | 9fc3be6 | 2021-05-29 04:01:51 +0100 | [diff] [blame] | 317 | build_options.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 318 | 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] | 319 | } |
Viet-Hoa Do | b5368fb | 2022-09-21 11:31:46 +0100 | [diff] [blame] | 320 | |
| 321 | if(compile_context.get_ddk_version() >= 30) |
| 322 | { |
| 323 | build_options.add_option("-fregister-allocation=64"); |
| 324 | } |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 328 | _export_weights_to_cl_image = false; |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 329 | |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 330 | kernel_name << "direct_convolution_nchw"; |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 331 | build_options.add_option_if(biases != nullptr, std::string("-DHAS_BIAS")); |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 332 | build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(width_idx))); |
| 333 | build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(height_idx))); |
| 334 | build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx))); |
| 335 | build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())); |
| 336 | build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); |
| 337 | build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x)); |
| 338 | build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y)); |
| 339 | build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx))); |
| 340 | build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx))); |
| 341 | build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); |
| 342 | build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type))); |
| 343 | build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(weights->dimension(channel_idx)))); |
| 344 | build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x))); |
| 345 | build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type))); |
| 346 | build_options.add_option(std::string("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration))); |
| 347 | build_options.add_option(std::string("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration))); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 348 | |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 349 | if(is_data_type_quantized(data_type)) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 350 | { |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 351 | const UniformQuantizationInfo iqinfo = src->quantization_info().uniform(); |
| 352 | const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform(); |
| 353 | const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform(); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 354 | |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 355 | float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale; |
| 356 | int output_multiplier = 0; |
| 357 | int output_shift = 0; |
| 358 | quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
| 359 | build_options.add_option("-DIS_QUANTIZED"); |
| 360 | build_options.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier)); |
| 361 | build_options.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift)); |
| 362 | build_options.add_option("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size)); |
| 363 | build_options.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-iqinfo.offset)); |
| 364 | build_options.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-wqinfo.offset)); |
| 365 | build_options.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(oqinfo.offset)); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 366 | } |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 369 | _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options()); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 370 | |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 371 | // Set config_id for enabling LWS tuning |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 372 | // config_id should include the variables used to parameterize the kernel |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 373 | _config_id = kernel_name.str(); |
| 374 | _config_id += "_"; |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 375 | _config_id += lower_string(string_from_data_type(data_type)); |
| 376 | _config_id += "_"; |
| 377 | _config_id += support::cpp11::to_string(kernel_size); |
| 378 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 379 | _config_id += support::cpp11::to_string(border_size().left); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 380 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 381 | _config_id += support::cpp11::to_string(border_size().top); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 382 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 383 | _config_id += support::cpp11::to_string(border_size().right); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 384 | _config_id += "_"; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 385 | _config_id += support::cpp11::to_string(border_size().bottom); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 386 | _config_id += "_"; |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 387 | _config_id += support::cpp11::to_string(conv_stride_x); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 388 | _config_id += "_"; |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 389 | _config_id += support::cpp11::to_string(conv_stride_y); |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 390 | // SRC_CHANNELS, SRC_WIDTH, SRC_HEIGHT |
| 391 | _config_id += "_"; |
| 392 | _config_id += support::cpp11::to_string(src->dimension(channel_idx)); |
| 393 | _config_id += "_"; |
| 394 | _config_id += support::cpp11::to_string(src->dimension(width_idx)); |
| 395 | _config_id += "_"; |
| 396 | _config_id += support::cpp11::to_string(src->dimension(height_idx)); |
| 397 | _config_id += "_"; |
| 398 | // DST_CHANNELS, DST_WIDTH, DST_HEIGHT |
| 399 | _config_id += support::cpp11::to_string(dst->dimension(channel_idx)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 400 | _config_id += "_"; |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 401 | _config_id += support::cpp11::to_string(dst->dimension(width_idx)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 402 | _config_id += "_"; |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 403 | _config_id += support::cpp11::to_string(dst->dimension(height_idx)); |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 404 | _config_id += "_"; |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 405 | _config_id += lower_string(string_from_data_layout(_data_layout)); |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Georgios Pinitas | 9fc3be6 | 2021-05-29 04:01:51 +0100 | [diff] [blame] | 408 | Status ClDirectConv2dKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 409 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc) |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 410 | { |
Gian Marco Iodice | 2cc50b3 | 2022-05-30 14:41:49 +0100 | [diff] [blame] | 411 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info, act_info, desc)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 412 | return Status{}; |
Giorgio Arena | 5948634 | 2017-12-01 10:42:47 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 415 | void ClDirectConv2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 416 | { |
| 417 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 418 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 419 | |
| 420 | // Get initial windows |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 421 | Window slice = window.first_slice_window_3D(); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 422 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 423 | const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 424 | const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 425 | const auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2)); |
| 426 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 427 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 428 | if(_data_layout == DataLayout::NHWC) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 429 | { |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 430 | cl::Image2D weights_cl_image; |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 431 | cl::Image2D output_cl_image; |
| 432 | cl::Image2D input_cl_image; |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 433 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 434 | if(_export_weights_to_cl_image) |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 435 | { |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 436 | // Export tensor to cl_image |
| 437 | weights_cl_image = create_image2d_from_tensor(weights, CLImage2DType::ReadOnly); |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if(_export_output_to_cl_image) |
| 441 | { |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 442 | // Export tensor to cl_image |
| 443 | output_cl_image = create_image2d_from_tensor(dst, CLImage2DType::WriteOnly); |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if(_export_input_to_cl_image) |
| 447 | { |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 448 | // Export tensor to cl_image |
| 449 | input_cl_image = create_image2d_from_tensor(src, CLImage2DType::ReadOnly); |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 450 | } |
| 451 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 452 | unsigned int idx = 0; |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 453 | if(_export_input_to_cl_image) |
| 454 | { |
| 455 | _kernel.setArg(idx++, input_cl_image); |
| 456 | } |
Gian Marco Iodice | 78baa48 | 2021-12-01 09:26:14 +0000 | [diff] [blame] | 457 | add_4d_tensor_nhwc_argument(idx, src); |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 458 | if(_export_output_to_cl_image) |
| 459 | { |
| 460 | _kernel.setArg(idx++, output_cl_image); |
| 461 | } |
Gian Marco Iodice | 78baa48 | 2021-12-01 09:26:14 +0000 | [diff] [blame] | 462 | add_4d_tensor_nhwc_argument(idx, dst); |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 463 | if(_export_weights_to_cl_image) |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 464 | { |
| 465 | _kernel.setArg(idx++, weights_cl_image); |
| 466 | } |
Gian Marco Iodice | 78baa48 | 2021-12-01 09:26:14 +0000 | [diff] [blame] | 467 | add_4d_tensor_nhwc_argument(idx, weights); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 468 | if(biases != nullptr) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 469 | { |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 470 | add_1D_tensor_argument(idx, biases, slice); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 471 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 472 | enqueue(queue, *this, slice, lws_hint()); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 473 | } |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 474 | else |
| 475 | { |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 476 | unsigned int idx1 = 2 * num_arguments_per_3D_tensor(); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 477 | add_3D_tensor_argument(idx1, weights, slice); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 478 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 479 | if(biases != nullptr) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 480 | { |
| 481 | Window slice_biases; |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 482 | slice_biases.use_tensor_dimensions(biases->info()->tensor_shape()); |
| 483 | add_1D_tensor_argument(idx1, biases, slice_biases); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 486 | _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] | 487 | |
| 488 | do |
| 489 | { |
| 490 | unsigned int idx = 0; |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 491 | add_3D_tensor_argument(idx, src, slice); |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 492 | add_3D_tensor_argument(idx, dst, slice); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 493 | enqueue(queue, *this, slice, lws_hint()); |
| 494 | } |
Adnan AlSinan | 3012435 | 2021-12-02 19:12:20 +0000 | [diff] [blame] | 495 | while(window.slide_window_slice_3D(slice)); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 496 | } |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 497 | } |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 498 | } // namespace kernels |
| 499 | } // namespace opencl |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 500 | } // namespace arm_compute |