Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 9197c96 | 2019-02-27 09:29:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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 | */ |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLKernel.h" |
| 31 | #include "arm_compute/core/CL/ICLTensor.h" |
| 32 | #include "arm_compute/core/Error.h" |
| 33 | #include "arm_compute/core/Helpers.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 38 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 39 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 42 | using namespace arm_compute::misc::shape_calculator; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 43 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 47 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D dilation, |
| 48 | const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 49 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((act_info.enabled()) && (input->data_type() == DataType::QASYMM8) && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 53 | && (act_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU) |
| 54 | && (act_info.activation() != ActivationLayerInfo::ActivationFunction::RELU) |
| 55 | && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC), |
Michele Di Giorgio | d304e80 | 2018-07-06 10:17:33 +0100 | [diff] [blame] | 56 | "For QASYMM8 only logistic, relu, lower bounded relu and lower-upper bounded relu are supported"); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(0) != 3 || weights->dimension(1) != 3); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON(conv_info.stride().first < 1 || conv_info.stride().first > 3); |
| 59 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); |
| 61 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 62 | const bool is_qasymm = is_data_type_quantized_asymmetric(input->data_type()); |
| 63 | |
| 64 | if(biases != nullptr) |
| 65 | { |
| 66 | if(is_qasymm) |
| 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 73 | } |
Michele Di Giorgio | d4b2c9f | 2018-05-10 10:49:32 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON((biases->dimension(0) != weights->dimension(2)) && (weights->dimension(2) != 1 || biases->dimension(0) != weights->dimension(3))); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 76 | } |
| 77 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 78 | if(is_qasymm) |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output_multipliers, output_shifts); |
| 81 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_multipliers, 1, DataType::S32); |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_shifts, 1, DataType::S32); |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1); |
| 85 | |
| 86 | if(is_data_type_quantized_per_channel(weights->data_type())) |
| 87 | { |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL); |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != output_multipliers->dimension(0)); |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != output_shifts->dimension(0)); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(1 != output_multipliers->dimension(0)); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(1 != output_shifts->dimension(0)); |
| 97 | } |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 102 | } |
| 103 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 104 | if(output->total_size() != 0) |
| 105 | { |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 106 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 108 | } |
| 109 | |
| 110 | return Status{}; |
| 111 | } |
| 112 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 113 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info, |
| 114 | unsigned int depth_multiplier, GPUTarget gpu_target, std::string &kernel_name, const Size2D dilation) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 115 | { |
| 116 | // Output auto inizialitation if not yet initialized |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 117 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 118 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_quantization_info(output->quantization_info())); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 119 | |
| 120 | const unsigned int conv_stride_x = conv_info.stride().first; |
| 121 | const unsigned int conv_stride_y = conv_info.stride().second; |
| 122 | const bool is_qasymm = is_data_type_quantized_asymmetric(input->data_type()); |
| 123 | const bool is_bifrost = get_arch_from_target(gpu_target) == GPUTarget::BIFROST; |
| 124 | |
| 125 | // Configure kernel window |
| 126 | unsigned int num_elems_read_per_iteration_x = 0; |
| 127 | unsigned int num_elems_read_per_iteration_y = 0; |
| 128 | unsigned int num_elems_written_per_iteration_x = 0; |
| 129 | unsigned int num_elems_written_per_iteration_y = 0; |
| 130 | |
| 131 | if(input->data_type() == DataType::F16) |
| 132 | { |
| 133 | kernel_name = "depthwise_convolution_3x3_f16"; |
| 134 | num_elems_written_per_iteration_x = 8 / data_size_from_type(input->data_type()); |
| 135 | num_elems_written_per_iteration_y = 1; |
| 136 | num_elems_read_per_iteration_y = 3; |
| 137 | switch(conv_stride_x) |
| 138 | { |
| 139 | case 1: |
| 140 | num_elems_read_per_iteration_x = 8; |
| 141 | break; |
| 142 | case 2: |
| 143 | num_elems_read_per_iteration_x = 9; |
| 144 | break; |
| 145 | case 3: |
| 146 | num_elems_read_per_iteration_x = 16; |
| 147 | break; |
| 148 | default: |
| 149 | num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * conv_stride_x; |
| 150 | break; |
| 151 | } |
| 152 | if(is_bifrost) |
| 153 | { |
| 154 | if(conv_stride_x == 1 && conv_stride_y == 1) |
| 155 | { |
| 156 | kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f16"; |
| 157 | num_elems_read_per_iteration_x = 8; |
| 158 | num_elems_written_per_iteration_x = 4; |
| 159 | num_elems_read_per_iteration_y = 6; |
| 160 | num_elems_written_per_iteration_y = 4; |
| 161 | } |
| 162 | else if(conv_stride_x == 2 && conv_stride_y == 2) |
| 163 | { |
| 164 | kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f16"; |
| 165 | num_elems_read_per_iteration_x = 10; |
| 166 | num_elems_written_per_iteration_x = 4; |
| 167 | num_elems_read_per_iteration_y = 5; |
| 168 | num_elems_written_per_iteration_y = 2; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | else if(input->data_type() == DataType::F32 && is_bifrost) |
| 173 | { |
| 174 | if(conv_stride_x == 1 && conv_stride_y == 1) |
| 175 | { |
| 176 | kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f32"; |
| 177 | num_elems_read_per_iteration_x = 4; |
| 178 | num_elems_read_per_iteration_y = 6; |
| 179 | num_elems_written_per_iteration_x = 2; |
| 180 | num_elems_written_per_iteration_y = 4; |
| 181 | } |
| 182 | else if(conv_stride_x == 2 && conv_stride_y == 2) |
| 183 | { |
| 184 | kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f32"; |
| 185 | num_elems_read_per_iteration_x = 6; |
| 186 | num_elems_read_per_iteration_y = 5; |
| 187 | num_elems_written_per_iteration_x = 2; |
| 188 | num_elems_written_per_iteration_y = 2; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | kernel_name = "depthwise_convolution_3x3"; |
| 193 | num_elems_written_per_iteration_x = 8 / data_size_from_type(input->data_type()); |
| 194 | num_elems_written_per_iteration_y = 1; |
| 195 | num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * conv_stride_x; |
| 196 | num_elems_read_per_iteration_y = 3; |
| 197 | } |
| 198 | } |
| 199 | else |
| 200 | { |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 201 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()) && !is_data_type_quantized_per_channel(weights->data_type()); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 202 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 203 | kernel_name = is_qasymm ? "dwc_3x3_native_quantized8" : "depthwise_convolution_3x3"; |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 204 | kernel_name += (is_qasymm && is_dot8_supported ? "_dot8" : ""); |
Pablo Tello | da0e513 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 205 | kernel_name += (is_qasymm ? "_nchw" : ""); |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 206 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 207 | num_elems_written_per_iteration_x = 8 / data_size_from_type(input->data_type()); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 208 | num_elems_written_per_iteration_y = (is_qasymm && conv_stride_y == 1 && dilation.y() == 1) ? 2 : 1; |
Gian Marco Iodice | 3ae323f | 2019-08-13 14:56:50 +0100 | [diff] [blame] | 209 | num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * conv_stride_x + (conv_stride_x > 1 ? 1 : 0); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 210 | num_elems_read_per_iteration_y = num_elems_written_per_iteration_y + 2; |
| 211 | } |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 212 | num_elems_read_per_iteration_x += (num_elems_read_per_iteration_x - 1) * (dilation.x() - 1); |
| 213 | num_elems_read_per_iteration_y += (num_elems_read_per_iteration_y - 1) * (dilation.y() - 1); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 214 | |
| 215 | // Create window and update padding |
| 216 | Window win = calculate_max_window(*output, Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y)); |
| 217 | |
| 218 | AccessWindowRectangle input_access(input, -conv_info.pad_left(), -conv_info.pad_top(), |
| 219 | num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, |
| 220 | conv_stride_x, conv_stride_y); |
| 221 | AccessWindowStatic weights_access(weights, 0, 0, 3, 3); |
| 222 | AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); |
| 223 | |
| 224 | bool window_changed = update_window_and_padding(win, input_access, weights_access, output_access); |
| 225 | |
| 226 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 227 | |
| 228 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 229 | return std::make_pair(err, win); |
| 230 | } |
| 231 | } // namespace |
| 232 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 233 | CLDepthwiseConvolutionLayer3x3NCHWKernel::CLDepthwiseConvolutionLayer3x3NCHWKernel() |
Giorgio Arena | fa23f11 | 2018-06-19 11:27:38 +0100 | [diff] [blame] | 234 | : _conv_stride_x(0), _conv_pad_top(0), _conv_pad_left(0) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 235 | { |
| 236 | } |
| 237 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 238 | BorderSize CLDepthwiseConvolutionLayer3x3NCHWKernel::border_size() const |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 239 | { |
| 240 | return _border_size; |
| 241 | } |
| 242 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 243 | void CLDepthwiseConvolutionLayer3x3NCHWKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 244 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation, |
| 245 | const ICLTensor *output_multipliers, const ICLTensor *output_shifts) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 246 | { |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 247 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 248 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), |
| 249 | conv_info, depth_multiplier, act_info, dilation, |
| 250 | (output_multipliers != nullptr) ? output_multipliers->info() : nullptr, |
| 251 | (output_shifts != nullptr) ? output_shifts->info() : nullptr)); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 252 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 253 | _input = input; |
| 254 | _output = output; |
| 255 | _weights = weights; |
| 256 | _biases = biases; |
| 257 | _conv_stride_x = conv_info.stride().first; |
| 258 | _conv_stride_y = conv_info.stride().second; |
| 259 | _conv_pad_left = conv_info.pad_left(); |
| 260 | _conv_pad_top = conv_info.pad_top(); |
| 261 | _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left); |
| 262 | _output_multipliers = output_multipliers; |
| 263 | _output_shifts = output_shifts; |
| 264 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 265 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 266 | // Configure kernel window |
| 267 | std::string kernel_name; |
| 268 | const GPUTarget gpu_target = get_target(); |
| 269 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 270 | auto win_config = validate_and_configure_window(input->info(), weights->info(), output->info(), conv_info, depth_multiplier, gpu_target, kernel_name, dilation); |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 271 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 272 | ICLKernel::configure_internal(win_config.second); |
| 273 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 274 | // Set build options |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 275 | CLBuildOptions build_opts; |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 276 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation()))); |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 277 | build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(_output->info()->tensor_shape().z())); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 278 | build_opts.add_option("-DDEPTH_MULTIPLIER=" + support::cpp11::to_string(depth_multiplier)); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 279 | build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x)); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 280 | build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x())); |
| 281 | build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y())); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 282 | build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS"); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 283 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 284 | if(_is_quantized) |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 285 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 286 | const UniformQuantizationInfo iq_info = _input->info()->quantization_info().uniform(); |
| 287 | const UniformQuantizationInfo wq_info = _weights->info()->quantization_info().uniform(); |
| 288 | const UniformQuantizationInfo oq_info = _output->info()->quantization_info().uniform(); |
| 289 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 290 | const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type()); |
| 291 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()) && !is_quantized_per_channel; |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 292 | build_opts.add_option("-DCONV_STRIDE_Y=" + support::cpp11::to_string(_conv_stride_y)); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 293 | build_opts.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-iq_info.offset)); |
| 294 | build_opts.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-wq_info.offset)); |
| 295 | build_opts.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(oq_info.offset)); |
| 296 | build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(9 * iq_info.offset * wq_info.offset)); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 297 | build_opts.add_option_if(is_quantized_per_channel, "-DPER_CHANNEL_QUANTIZATION"); |
| 298 | build_opts.add_option_if(is_dot8_supported, "-DIS_DOT8"); |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 299 | |
| 300 | if(act_info.enabled()) |
| 301 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 302 | const int a_val = quantize_qasymm8(act_info.a(), oq_info); |
| 303 | const int b_val = quantize_qasymm8(act_info.b(), oq_info); |
| 304 | const int o1 = oq_info.offset; |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 305 | |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 306 | build_opts.add_option("-DA_VAL=" + support::cpp11::to_string(a_val)); |
| 307 | build_opts.add_option("-DB_VAL=" + support::cpp11::to_string(b_val)); |
| 308 | build_opts.add_option("-DCONST_0=" + support::cpp11::to_string(o1)); |
| 309 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 310 | const float s1 = iq_info.scale; |
Isabella Gottardi | 9197c96 | 2019-02-27 09:29:21 +0000 | [diff] [blame] | 311 | build_opts.add_option("-DS1_VAL=" + float_to_string_with_full_precision(s1)); |
| 312 | build_opts.add_option("-DO1_VAL=" + support::cpp11::to_string(o1)); |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 313 | } |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 314 | |
| 315 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 316 | build_opts.add_option("-DWEIGHTS_TYPE=" + get_cl_type_from_data_type(weights->info()->data_type())); |
| 317 | build_opts.add_option("-DWEIGHTS_PROMOTED_TYPE=" + get_cl_promoted_type_from_data_type(weights->info()->data_type())); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 318 | } |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 319 | else |
| 320 | { |
| 321 | build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 322 | build_opts.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 323 | build_opts.add_option_if(act_info.enabled(), "-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 324 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(win_config.second.x().step())); |
| 325 | } |
| 326 | |
| 327 | build_opts.add_option_if(input->info()->data_type() == DataType::F16, "-DIS_F16"); |
| 328 | build_opts.add_option_if(input->info()->data_type() == DataType::F32, "-DIS_F32"); |
| 329 | |
Gian Marco | c799ed8 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 330 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 331 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 332 | // Set config_id for enabling LWS tuning |
Gian Marco | c799ed8 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 333 | _config_id = kernel_name; |
| 334 | _config_id += "_"; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 335 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 336 | _config_id += "_"; |
| 337 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 338 | _config_id += "_"; |
| 339 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 340 | _config_id += "_"; |
| 341 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 342 | _config_id += "_"; |
| 343 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 344 | _config_id += "_"; |
| 345 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 348 | Status CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 349 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, |
| 350 | const Size2D &dilation, const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 351 | { |
| 352 | std::string kernel_name; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 353 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation, output_multipliers, output_shifts)); |
| 354 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), |
| 355 | conv_info, depth_multiplier, gpu_target, kernel_name, dilation) |
| 356 | .first); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 357 | |
| 358 | return Status{}; |
| 359 | } |
| 360 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 361 | void CLDepthwiseConvolutionLayer3x3NCHWKernel::run(const Window &window, cl::CommandQueue &queue) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 362 | { |
| 363 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 364 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 365 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 366 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
Georgios Pinitas | b6f182d3 | 2017-11-29 10:17:56 +0000 | [diff] [blame] | 367 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 368 | // Create input window and adjust |
| 369 | Window collapsed_in = collapsed; |
| 370 | collapsed_in.adjust(Window::DimX, -_conv_pad_left, true); |
| 371 | collapsed_in.adjust(Window::DimY, -_conv_pad_top, true); |
| 372 | collapsed_in.set_dimension_step(Window::DimX, collapsed_in.x().step() * _conv_stride_x); |
| 373 | collapsed_in.set_dimension_step(Window::DimY, collapsed_in.y().step() * _conv_stride_y); |
| 374 | |
| 375 | Window slice_in = collapsed_in.first_slice_window_3D(); |
| 376 | Window slice_out = collapsed.first_slice_window_3D(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 377 | Window slice_weights = window.first_slice_window_3D(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 378 | slice_weights.set_dimension_step(Window::DimX, 0); |
| 379 | slice_weights.set_dimension_step(Window::DimY, 0); |
| 380 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 381 | unsigned int idx = 3 * num_arguments_per_3D_tensor(); |
| 382 | |
| 383 | // Set output multipliers in case of quantized data type |
| 384 | if(_is_quantized) |
| 385 | { |
| 386 | Window slice; |
| 387 | slice.use_tensor_dimensions(_output_multipliers->info()->tensor_shape()); |
| 388 | add_1D_tensor_argument(idx, _output_multipliers, slice); |
| 389 | add_1D_tensor_argument(idx, _output_shifts, slice); |
| 390 | } |
| 391 | |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 392 | // Set biases |
| 393 | if(_biases != nullptr) |
| 394 | { |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 395 | Window slice_biases; |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 396 | slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); |
| 397 | add_1D_tensor_argument(idx, _biases, slice_biases); |
| 398 | } |
| 399 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 400 | do |
| 401 | { |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 402 | idx = 0; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 403 | add_3D_tensor_argument(idx, _input, slice_in); |
| 404 | add_3D_tensor_argument(idx, _output, slice_out); |
| 405 | add_3D_tensor_argument(idx, _weights, slice_weights); |
| 406 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 407 | enqueue(queue, *this, slice_out, lws_hint()); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 408 | } |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 409 | while(collapsed.slide_window_slice_3D(slice_out) && collapsed_in.slide_window_slice_3D(slice_in)); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 410 | } |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 411 | } // namespace arm_compute |