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