steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLDirectConvolutionLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/IAccessWindow.h" |
| 33 | #include "arm_compute/core/ITensor.h" |
| 34 | #include "arm_compute/core/Types.h" |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/Utils.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Validate.h" |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 37 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 38 | #include "support/ToolchainSupport.h" |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 42 | namespace |
| 43 | { |
| 44 | /** Calculates expected output shape dimension |
| 45 | * |
| 46 | * @param[in] Input shape |
| 47 | * |
| 48 | * @return Expected output shape |
| 49 | */ |
| 50 | TensorShape get_output_shape(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info) |
| 51 | { |
| 52 | unsigned int output_width = 0; |
| 53 | unsigned int output_height = 0; |
| 54 | std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info); |
| 55 | |
| 56 | TensorShape output_shape = input_shape; |
| 57 | output_shape.set(0, output_width); |
| 58 | output_shape.set(1, output_height); |
| 59 | output_shape.set(2, weights_shape[3]); |
| 60 | |
| 61 | return output_shape; |
| 62 | } |
| 63 | } // namespace |
| 64 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 65 | CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel() |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 66 | : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_stride_x(0), _conv_stride_y(0) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 70 | BorderSize CLDirectConvolutionLayerKernel::border_size() const |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 71 | { |
| 72 | return _border_size; |
| 73 | } |
| 74 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 75 | void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 76 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 78 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 79 | const unsigned int kernel_size = weights->info()->dimension(0); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 80 | const DataType data_type = input->info()->data_type(); |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 81 | |
| 82 | // Get convolved dimensions |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 83 | TensorShape output_shape = get_output_shape(input->info()->tensor_shape(), weights->info()->tensor_shape(), conv_info); |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 84 | |
| 85 | // Output auto inizialitation if not yet initialized |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 86 | auto_init_if_empty(*output->info(), |
| 87 | output_shape, |
| 88 | 1, |
| 89 | input->info()->data_type(), |
| 90 | input->info()->fixed_point_position(), |
| 91 | input->info()->quantization_info()); |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 92 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 93 | // Perform validation step |
| 94 | ARM_COMPUTE_ERROR_THROW_ON(CLDirectConvolutionLayerKernel::validate(input->info(), |
| 95 | weights->info(), |
| 96 | (biases != nullptr) ? biases->info() : nullptr, |
| 97 | output->info(), |
| 98 | conv_info)); |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 99 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 100 | _conv_stride_x = std::get<0>(conv_info.stride()); |
| 101 | _conv_stride_y = std::get<1>(conv_info.stride()); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 102 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 103 | _input = input; |
| 104 | _weights = weights; |
| 105 | _output = output; |
| 106 | _biases = biases; |
| 107 | |
| 108 | int conv_pad_left = std::min(conv_info.pad_left(), kernel_size / 2); |
| 109 | int conv_pad_top = std::min(conv_info.pad_top(), kernel_size / 2); |
| 110 | int conv_pad_right = std::min(conv_info.pad_right(), kernel_size / 2); |
| 111 | int conv_pad_bottom = std::min(conv_info.pad_bottom(), kernel_size / 2); |
| 112 | _border_size = BorderSize(conv_pad_top, conv_pad_right, conv_pad_bottom, conv_pad_left); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 113 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 114 | const GPUTarget gpu_target = get_arch_from_target(get_target()); |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 115 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 116 | std::stringstream kernel_name; |
| 117 | kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 118 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 119 | CLBuildOptions build_options; |
| 120 | build_options.add_option_if(_biases != nullptr, std::string("-DHAS_BIAS")); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 121 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 122 | if((gpu_target == GPUTarget::BIFROST) && (kernel_size <= 5) && (_conv_stride_x == 1) && (_conv_stride_y == 1) && (data_type == DataType::F32)) |
| 123 | { |
| 124 | build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2)))); |
| 125 | |
| 126 | kernel_name << "_f32_bifrost"; |
| 127 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_options.options())); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 128 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 129 | // Configure kernel window |
| 130 | Window win = calculate_max_window(*output->info()); |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 131 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 132 | unsigned int num_elems_read_per_iteration_x = 0; |
| 133 | unsigned int num_elems_read_per_iteration_y = 0; |
| 134 | unsigned int num_elems_written_per_iteration_x = 0; |
| 135 | unsigned int num_elems_written_per_iteration_y = 0; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 136 | |
Anthony Barbier | c8da111 | 2017-11-28 10:28:47 +0000 | [diff] [blame] | 137 | // Through extensive experimentation with over 30 representative tensor |
| 138 | // shapes, we found a small number of local work size configurations |
| 139 | // that result in nearly optimal execution times. Selecting the right |
| 140 | // lws for a given shape, however, required a complex decision tree, |
| 141 | // until we constructed a simple feature as described below. |
| 142 | // |
| 143 | // We started from the number of multiply-accumulate operations for a |
| 144 | // convolution layer, which is equal to the product of the input |
| 145 | // dimensions 0..2 and the weights dimensions 0..2. Unfortunately, |
| 146 | // this resulted in ties between distinct shapes that required distinct |
| 147 | // lws configurations. Replacing the width of the input with the kernel |
| 148 | // size, however, resulted in nearly optimal predictions. We use underscores |
| 149 | // in variable names to indicate when they are intentionally misleading. |
| 150 | const size_t product_of_weights_dimensions = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2); |
| 151 | const size_t product_of_input_dimensions_ = input->info()->dimension(0) * weights->info()->dimension(1) * input->info()->dimension(2); |
| 152 | const float mega_ops_ = 1e-6 * product_of_weights_dimensions * product_of_input_dimensions_; |
| 153 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 154 | switch(kernel_size) |
| 155 | { |
Gian Marco Iodice | 1c8409d | 2017-09-06 17:24:25 +0100 | [diff] [blame] | 156 | case 1: |
| 157 | { |
| 158 | num_elems_read_per_iteration_x = 4; |
| 159 | num_elems_read_per_iteration_y = 4; |
| 160 | num_elems_written_per_iteration_x = 4; |
| 161 | num_elems_written_per_iteration_y = 4; |
Anthony Barbier | c8da111 | 2017-11-28 10:28:47 +0000 | [diff] [blame] | 162 | if(mega_ops_ < 1.f) |
| 163 | { |
| 164 | _lws_hint = cl::NDRange(1, 1, 8); |
| 165 | } |
| 166 | else if(mega_ops_ < 7.f) |
| 167 | { |
| 168 | _lws_hint = cl::NDRange(1, 1, 4); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | _lws_hint = cl::NDRange(1, 1, 2); |
| 173 | } |
Gian Marco Iodice | 1c8409d | 2017-09-06 17:24:25 +0100 | [diff] [blame] | 174 | break; |
| 175 | } |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 176 | case 3: |
| 177 | { |
| 178 | num_elems_read_per_iteration_x = 6; |
| 179 | num_elems_read_per_iteration_y = 5; |
| 180 | num_elems_written_per_iteration_x = 4; |
| 181 | num_elems_written_per_iteration_y = 3; |
Anthony Barbier | c8da111 | 2017-11-28 10:28:47 +0000 | [diff] [blame] | 182 | if(mega_ops_ < 1.f) |
| 183 | { |
| 184 | _lws_hint = cl::NDRange(1, 1, 8); |
| 185 | } |
| 186 | else if(mega_ops_ < 13.f) |
| 187 | { |
| 188 | _lws_hint = cl::NDRange(2, 1, 4); |
| 189 | } |
| 190 | else if(mega_ops_ < 50.f) |
| 191 | { |
| 192 | _lws_hint = cl::NDRange(3, 1, 4); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | _lws_hint = cl::NDRange(2, 1, 6); |
| 197 | } |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | case 5: |
| 201 | { |
| 202 | num_elems_read_per_iteration_x = 8; |
| 203 | num_elems_read_per_iteration_y = 6; |
| 204 | num_elems_written_per_iteration_x = 4; |
| 205 | num_elems_written_per_iteration_y = 2; |
Anthony Barbier | c8da111 | 2017-11-28 10:28:47 +0000 | [diff] [blame] | 206 | if(mega_ops_ < 2.f || mega_ops_ > 80.f) |
| 207 | { |
| 208 | _lws_hint = cl::NDRange(2, 1, 4); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | _lws_hint = cl::NDRange(2, 1, 8); |
| 213 | } |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | default: |
| 217 | { |
| 218 | ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost"); |
| 219 | } |
| 220 | } |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 221 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 222 | // Calculate right and bottom border |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 223 | const int input_width = input->info()->dimension(0) - kernel_size / 2 + conv_pad_right; |
| 224 | const int input_height = input->info()->dimension(1) - kernel_size / 2 + conv_pad_bottom; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 225 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 226 | // Create window and update padding |
| 227 | win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y)); |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 228 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 229 | AccessWindowStatic input_access(input->info(), -conv_pad_left, -conv_pad_top, input_width + num_elems_read_per_iteration_x, input_height + num_elems_read_per_iteration_y); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 230 | AccessWindowStatic weights_access(weights->info(), 0, 0, kernel_size, kernel_size); |
| 231 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 232 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 233 | update_window_and_padding(win, input_access, weights_access, output_access); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 234 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 235 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 236 | |
| 237 | ICLKernel::configure(win); |
| 238 | } |
| 239 | else |
| 240 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 241 | bool is_quantized_fixed_point = is_data_type_fixed_point(data_type); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 242 | bool is_quantized_asymm = is_data_type_quantized_asymmetric(data_type); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 243 | DataType promoted_type = (is_quantized_fixed_point) ? get_promoted_data_type(data_type) : data_type; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 244 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 245 | build_options.add_option_if(is_quantized_asymm, std::string("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size))); |
| 246 | build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); |
| 247 | build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type))); |
| 248 | build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2)))); |
| 249 | build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x))); |
| 250 | build_options.add_option_if(is_quantized_fixed_point, |
| 251 | std::string("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()))); |
| 252 | build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(promoted_type))); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 253 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 254 | // Create kernel |
| 255 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(is_quantized_asymm ? "direct_convolution_1x1_3x3_5x5_quantized" : kernel_name.str(), |
| 256 | build_options.options())); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 257 | |
| 258 | // Configure kernel window |
| 259 | |
| 260 | bool is_stride2 = ((kernel_size != 1) && (_conv_stride_x == 2)); |
| 261 | |
| 262 | const unsigned int num_elems_read_per_iteration_x = 8 + 2 * (kernel_size / 2) + (is_stride2 ? 6 + kernel_size / 2 : 0); |
| 263 | const unsigned int num_elems_read_per_iteration_y = kernel_size; |
| 264 | const unsigned int num_elems_written_per_iteration_x = 8; |
| 265 | const unsigned int num_elems_written_per_iteration_y = 1; |
| 266 | |
| 267 | // Calculate right and bottom border |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 268 | const int input_width = input->info()->dimension(0) - kernel_size / 2 + conv_pad_right; |
| 269 | const int input_height = input->info()->dimension(1) - kernel_size / 2 + conv_pad_bottom; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 270 | |
| 271 | // Create window and update padding |
| 272 | Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y)); |
| 273 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 274 | AccessWindowStatic input_access(input->info(), -conv_pad_left, -conv_pad_top, input_width + num_elems_read_per_iteration_x, input_height + num_elems_read_per_iteration_y); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 275 | AccessWindowStatic weights_access(weights->info(), 0, 0, kernel_size, kernel_size); |
| 276 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); |
| 277 | |
| 278 | update_window_and_padding(win, input_access, weights_access, output_access); |
| 279 | |
| 280 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 281 | |
| 282 | ICLKernel::configure(win); |
| 283 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 284 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 285 | // Set static kernel arguments |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 286 | if(is_data_type_quantized_asymmetric(data_type)) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 287 | { |
| 288 | int output_multiplier = 0; |
| 289 | int output_shift = 0; |
| 290 | |
| 291 | float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale; |
| 292 | ARM_COMPUTE_THROW_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift)); |
| 293 | |
| 294 | unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0) + 1; |
| 295 | _kernel.setArg(idx++, -_input->info()->quantization_info().offset); |
| 296 | _kernel.setArg(idx++, -_weights->info()->quantization_info().offset); |
| 297 | _kernel.setArg(idx++, _output->info()->quantization_info().offset); |
| 298 | _kernel.setArg(idx++, output_multiplier); |
| 299 | _kernel.setArg(idx++, output_shift); |
| 300 | } |
| 301 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 302 | // Set config_id for enabling LWS tuning |
| 303 | _config_id = "direct_convolution_"; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 304 | _config_id += lower_string(string_from_data_type(data_type)); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 305 | _config_id += "_"; |
| 306 | _config_id += support::cpp11::to_string(kernel_size); |
| 307 | _config_id += "_"; |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 308 | _config_id += support::cpp11::to_string(conv_pad_left); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 309 | _config_id += "_"; |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 310 | _config_id += support::cpp11::to_string(conv_pad_top); |
| 311 | _config_id += "_"; |
| 312 | _config_id += support::cpp11::to_string(conv_pad_right); |
| 313 | _config_id += "_"; |
| 314 | _config_id += support::cpp11::to_string(conv_pad_bottom); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 315 | _config_id += "_"; |
| 316 | _config_id += support::cpp11::to_string(_conv_stride_x); |
| 317 | _config_id += "_"; |
| 318 | _config_id += support::cpp11::to_string(_conv_stride_y); |
| 319 | _config_id += "_"; |
| 320 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 321 | _config_id += "_"; |
| 322 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 325 | Error CLDirectConvolutionLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info) |
| 326 | { |
| 327 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 328 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 329 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != weights->dimension(1), |
| 330 | "Weights should have same width as length"); |
| 331 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != 1 && weights->dimension(0) != 3 && weights->dimension(0) != 5, |
| 332 | "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported"); |
| 333 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(2) != input->dimension(2), |
| 334 | "Weights feature map dimension should match the respective input's one"); |
| 335 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != weights->dimension(1), |
| 336 | "Only rectangular weights are supported!"); |
| 337 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, |
| 338 | "Weights can be at most 4 dimensional"); |
| 339 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(0) == 1) && std::get<0>(conv_info.stride()) > 3, |
| 340 | "Strides larger than 3 not supported for 1x1 convolution."); |
| 341 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(0) == 3 || weights->dimension(0) == 5) && std::get<0>(conv_info.stride()) > 2, |
| 342 | "Strides larger than 2 not supported for 3x3 convolution."); |
| 343 | |
| 344 | if(biases != nullptr) |
| 345 | { |
| 346 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 347 | { |
| 348 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 353 | } |
| 354 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3), |
| 355 | "Biases size and number of input feature maps should match"); |
| 356 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, |
| 357 | "Biases should be one dimensional"); |
| 358 | } |
| 359 | |
| 360 | // Checks performed when output is configured |
| 361 | if(output->total_size() != 0) |
| 362 | { |
| 363 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), |
| 364 | get_output_shape(input->tensor_shape(), weights->tensor_shape(), conv_info)); |
| 365 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 366 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 367 | } |
| 368 | |
| 369 | return Error{}; |
| 370 | } |
| 371 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 372 | void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 373 | { |
| 374 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 375 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 376 | |
| 377 | // Get initial windows |
| 378 | Window slice = window.first_slice_window_3D(); |
| 379 | Window win_in = window; |
| 380 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 381 | win_in.adjust(Window::DimX, -_border_size.left, true); |
| 382 | win_in.adjust(Window::DimY, -_border_size.top, true); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 383 | win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); |
| 384 | win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); |
| 385 | |
| 386 | Window slice_in = win_in.first_slice_window_3D(); |
| 387 | |
| 388 | unsigned int idx1 = 2 * num_arguments_per_3D_tensor(); |
| 389 | add_3D_tensor_argument(idx1, _weights, slice); |
| 390 | |
| 391 | if(_biases != nullptr) |
| 392 | { |
| 393 | Window slice_biases; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 394 | slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 395 | add_1D_tensor_argument(idx1, _biases, slice_biases); |
| 396 | } |
| 397 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 398 | _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3])); |
| 399 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 400 | do |
| 401 | { |
| 402 | unsigned int idx = 0; |
| 403 | add_3D_tensor_argument(idx, _input, slice_in); |
| 404 | add_3D_tensor_argument(idx, _output, slice); |
| 405 | |
| 406 | enqueue(queue, *this, slice, _lws_hint); |
| 407 | } |
| 408 | while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in)); |
| 409 | } |