Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [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/runtime/CL/functions/CLConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Utils.h" |
| 29 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 32 | |
| 33 | #include <cmath> |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 34 | #include <memory> |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 35 | #include <tuple> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 39 | CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
| 40 | : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 44 | void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW) |
| 45 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
| 48 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 50 | |
| 51 | if(biases != nullptr) |
| 52 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 53 | ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(weights->info()->data_type())); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 55 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3)); |
| 56 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 57 | } |
| 58 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 59 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
| 60 | const unsigned bias_element = (append_biases) ? 1 : 0; |
| 61 | const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 62 | |
| 63 | _transpose1xW = transpose1xW; |
| 64 | |
| 65 | if(transpose1xW) |
| 66 | { |
| 67 | // Create tensor to store the reshaped weights |
| 68 | const unsigned int mat_weights_cols = weights->info()->dimension(3); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 69 | const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 70 | TensorShape shape_wr(mat_weights_cols, mat_weights_rows); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 71 | const DataType dt = weights->info()->data_type(); |
| 72 | const int fixed_point_position = weights->info()->fixed_point_position(); |
| 73 | TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 74 | |
| 75 | _weights_reshaped.allocator()->init(info_wr); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 76 | _memory_group.manage(&_weights_reshaped); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 77 | _weights_reshape_kernel.configure(weights, biases_to_use, &_weights_reshaped); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 78 | _weights_transposed_kernel.configure(&_weights_reshaped, output); |
| 79 | _weights_reshaped.allocator()->allocate(); |
| 80 | } |
| 81 | else |
| 82 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 83 | _weights_reshape_kernel.configure(weights, biases_to_use, output); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | void CLConvolutionLayerReshapeWeights::run() |
| 88 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 89 | _memory_group.acquire(); |
| 90 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 91 | cl::CommandQueue q = CLScheduler::get().queue(); |
| 92 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
| 93 | if(_transpose1xW) |
| 94 | { |
| 95 | CLScheduler::get().enqueue(_weights_transposed_kernel); |
| 96 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 97 | |
| 98 | _memory_group.release(); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 101 | CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 102 | : _memory_group(memory_manager), _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _output_col2im_kernel(), |
| 103 | _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _append_bias(false), _is_fully_connected_convolution(false), |
| 104 | _are_weights_reshaped(false), _is_quantized(false) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 105 | { |
| 106 | } |
| 107 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 108 | void CLConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed) |
| 109 | { |
| 110 | if(_is_quantized) |
| 111 | { |
| 112 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 113 | // Extract and negate input and weights offset |
| 114 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 115 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 116 | |
| 117 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 118 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 119 | |
| 120 | _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
| 121 | |
| 122 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 123 | input->info()->set_quantization_info(input_quantization_info); |
| 124 | weights->info()->set_quantization_info(weights_quantization_info); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | _mm_kernel.configure(input, weights, output, 1.f, is_interleaved_transposed); |
| 129 | } |
| 130 | } |
| 131 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 132 | void CLConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 133 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 134 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 136 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 137 | ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2)); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 138 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 139 | ARM_COMPUTE_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->info()->data_type())); |
| 140 | |
| 141 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 142 | |
| 143 | if(biases != nullptr) |
| 144 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 145 | if(_is_quantized) |
| 146 | { |
| 147 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 152 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 153 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 154 | ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3)); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 156 | } |
| 157 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 158 | const DataType dt = input->info()->data_type(); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 159 | |
| 160 | // Set the GPU target for matrix multiply |
| 161 | _mm_kernel.set_target(CLScheduler::get().target()); |
| 162 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 163 | _append_bias = (biases != nullptr) && (!_is_quantized); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 164 | _are_weights_reshaped = weights_info.are_reshaped(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 165 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 166 | const unsigned bias_element = (_append_bias) ? 1 : 0; |
| 167 | const ICLTensor *biases_to_use = (_append_bias) ? biases : nullptr; |
| 168 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 169 | // Get parameters from conv_info |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 170 | unsigned int stride_x = 0; |
| 171 | unsigned int stride_y = 0; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 172 | std::tie(stride_x, stride_y) = conv_info.stride(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 173 | |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 174 | // Get convolved dimensions |
| 175 | unsigned int conv_w = 0; |
| 176 | unsigned int conv_h = 0; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 177 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 178 | const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0); |
| 179 | const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1); |
| 180 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height, |
| 181 | conv_info); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 182 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 183 | // Check if its a "fully connected" convolution |
| 184 | _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1)); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 185 | const bool run_interleaved = (!_is_fully_connected_convolution && !_is_quantized); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 186 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 187 | unsigned int mat_weights_cols = weights->info()->dimension(3); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 188 | unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 189 | |
| 190 | // Reshape weights if needed |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 191 | if(_are_weights_reshaped) |
| 192 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 193 | if(_is_fully_connected_convolution || _is_quantized) |
| 194 | { |
| 195 | mat_weights_cols = weights->info()->dimension(0); |
| 196 | mat_weights_rows = weights->info()->dimension(1); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | mat_weights_cols = weights_info.num_kernels(); |
| 201 | const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4; |
| 202 | mat_weights_rows = quarter_reshaped_cols + bias_element; |
| 203 | } |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 207 | if(_is_fully_connected_convolution || _is_quantized) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 208 | { |
| 209 | // Create tensor to store the reshaped weights |
| 210 | TensorShape shape_wr(mat_weights_cols, mat_weights_rows); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 211 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_wr)); |
| 212 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, false /* 1xW transpose */); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 213 | } |
| 214 | else |
| 215 | { |
| 216 | // Create tensor to store transposed weights |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 217 | const float transpose_width = 16.0f / input->info()->element_size(); |
| 218 | TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width))); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 219 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_wt)); |
| 220 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, true /* 1xW transpose */); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 221 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 222 | _weights_reshaped.info()->set_quantization_info(weights->info()->quantization_info()); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 223 | weights = &_weights_reshaped; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 224 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 225 | |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 226 | // Create tensor to store im2col reshaped inputs |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 227 | const unsigned int mat_input_cols = mat_weights_rows; |
| 228 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 229 | TensorShape shape_im2col = input->info()->tensor_shape(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 230 | shape_im2col.set(0, mat_input_cols); |
| 231 | shape_im2col.set(1, mat_input_rows); |
| 232 | shape_im2col.set(2, 1); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 233 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position()); |
| 234 | im2col_reshaped_info.set_quantization_info(input->info()->quantization_info()); |
| 235 | _input_im2col_reshaped.allocator()->init(im2col_reshaped_info); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 236 | _memory_group.manage(&_input_im2col_reshaped); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 237 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 238 | // Create tensor (interleave) to prepare input tensor for GEMM |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 239 | if(run_interleaved) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 240 | { |
| 241 | TensorShape shape_interleaved = shape_im2col; |
| 242 | shape_interleaved.set(0, shape_interleaved.x() * 4); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 243 | shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f)); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 244 | TensorInfo interleaved_info(shape_interleaved, 1, dt, input->info()->fixed_point_position()); |
| 245 | interleaved_info.set_quantization_info(input->info()->quantization_info()); |
| 246 | _input_interleaved_reshaped.allocator()->init(interleaved_info); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 247 | _memory_group.manage(&_input_interleaved_reshaped); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 248 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 249 | |
| 250 | // Create GEMM output tensor |
| 251 | TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape(); |
| 252 | shape_gemm.set(0, mat_weights_cols); |
| 253 | shape_gemm.set(1, mat_input_rows); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 254 | const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt; |
| 255 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 256 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position()); |
| 257 | info_gemm.set_quantization_info(output->info()->quantization_info()); |
| 258 | _gemm_output.allocator()->init(info_gemm); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 259 | _memory_group.manage(&_gemm_output); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 260 | |
| 261 | // Configure kernels |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 262 | _input_im2col_kernel.set_target(CLScheduler::get().target()); |
| 263 | _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _append_bias); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 264 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 265 | // Configure matrix multiply |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 266 | if(run_interleaved) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 267 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 268 | _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped); |
| 269 | configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output); |
| 270 | _input_interleaved_reshaped.allocator()->allocate(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 271 | } |
| 272 | else |
| 273 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 274 | configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, false); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 275 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 276 | _input_im2col_reshaped.allocator()->allocate(); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 277 | |
| 278 | // Configure output stage for quantized case |
| 279 | if(_is_quantized) |
| 280 | { |
| 281 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 282 | int output_multiplier, output_shift; |
| 283 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 284 | _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
| 285 | _gemm_output.allocator()->allocate(); |
| 286 | } |
| 287 | |
| 288 | // Configure Col2Im |
| 289 | _output_col2im_kernel.set_target(CLScheduler::get().target()); |
| 290 | _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, std::make_pair(conv_w, conv_h)); |
| 291 | if(_is_quantized) |
| 292 | { |
| 293 | _tmp_output.allocator()->allocate(); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | _gemm_output.allocator()->allocate(); |
| 298 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 299 | |
| 300 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 301 | |
| 302 | // Allocate intermediate tensor |
| 303 | if(!_are_weights_reshaped) |
| 304 | { |
| 305 | _weights_reshaped.allocator()->allocate(); |
| 306 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void CLConvolutionLayer::run() |
| 310 | { |
| 311 | // Run weights reshaping (Runs once for every configure) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 312 | if(!_are_weights_reshaped) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 313 | { |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 314 | _are_weights_reshaped = true; |
| 315 | _reshape_weights.run(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 318 | _memory_group.acquire(); |
| 319 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 320 | // Run im2col |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 321 | CLScheduler::get().enqueue(_input_im2col_kernel); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 322 | |
| 323 | if(!_is_fully_connected_convolution && !_is_quantized) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 324 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 325 | // Run interleave4x4 |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 326 | CLScheduler::get().enqueue(_input_interleave_kernel); |
| 327 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 328 | |
| 329 | // Runs matrix multiply on reshaped matrices |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 330 | if(_is_quantized) |
| 331 | { |
| 332 | _mm_gemmlowp.run(); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | CLScheduler::get().enqueue(_mm_kernel); |
| 337 | } |
| 338 | |
| 339 | // Run output stage for quantized case |
| 340 | if(_is_quantized) |
| 341 | { |
| 342 | _gemmlowp_output_stage.run(); |
| 343 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 344 | |
| 345 | // Reshape output matrix |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 346 | CLScheduler::get().enqueue(_output_col2im_kernel, false); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 347 | |
| 348 | _memory_group.release(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 349 | } |