Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 1 | /* |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [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 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/CLGEMMConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
| 27 | #include "arm_compute/core/Size2D.h" |
| 28 | #include "arm_compute/core/Utils.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 31 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | |
| 34 | #include <cmath> |
| 35 | #include <memory> |
| 36 | #include <tuple> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | using namespace arm_compute::misc::shape_calculator; |
| 40 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 41 | CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights() |
| 42 | : _weights_reshape_kernel() |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 46 | void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 47 | { |
| 48 | // Perform validation step |
| 49 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
| 50 | ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayerReshapeWeights::validate(weights->info(), |
| 51 | (biases != nullptr) ? biases->info() : nullptr, |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 52 | output->info(), |
| 53 | num_groups)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 54 | |
| 55 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
| 56 | const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 57 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 58 | _weights_reshape_kernel.configure(weights, biases_to_use, output, num_groups); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 59 | |
| 60 | output->info()->set_quantization_info(weights->info()->quantization_info()); |
| 61 | } |
| 62 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 63 | Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 67 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 68 | |
| 69 | if(biases != nullptr) |
| 70 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 71 | const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type())); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 76 | } |
| 77 | |
| 78 | if((output != nullptr) && (output->total_size() != 0)) |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 81 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 82 | CLWeightsReshapeKernel::validate(weights, biases, output, num_groups); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return Status{}; |
| 86 | } |
| 87 | |
| 88 | void CLConvolutionLayerReshapeWeights::run() |
| 89 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 90 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 94 | : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _col2im_kernel(), _activationlayer_function(), |
| 95 | _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _skip_im2col(false), _skip_col2im(false), _is_quantized(false), _fuse_activation(true), _is_prepared(false) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 99 | void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage, |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 100 | int gemm_3d_depth, const ActivationLayerInfo &act_info) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 101 | { |
| 102 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 103 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), gemmlowp_output_stage, gemm_3d_depth, _skip_im2col, act_info)); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 104 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 105 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 106 | false, // is_b_reshaped |
| 107 | true, // reshape_b_only_on_first_run |
| 108 | gemm_3d_depth, // depth_output_gemm3d |
| 109 | _skip_im2col, // reinterpret_input_as_3d |
| 110 | false, // retain_internal_weights |
| 111 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 112 | false, // fp_mixed_precision |
| 113 | true, // broadcast_bias |
| 114 | act_info); // activation_info |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 115 | |
| 116 | if(_is_quantized) |
| 117 | { |
| 118 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 119 | // Extract and negate input and weights offset |
| 120 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 121 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 122 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 123 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 124 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 125 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 126 | _mm_gemmlowp.configure(input, weights, biases, output, gemm_info); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 127 | |
| 128 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 129 | input->info()->set_quantization_info(input_quantization_info); |
| 130 | weights->info()->set_quantization_info(weights_quantization_info); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | // Configure matrix multiply function |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 135 | _mm_gemm.configure(input, weights, biases, output, 1.0f, 1.0f, gemm_info); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 139 | Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 140 | const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 141 | { |
| 142 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 143 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 144 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 145 | false, // is_b_reshaped |
| 146 | true, // reshape_b_only_on_first_run |
| 147 | gemm_3d_depth, // depth_output_gemm3d |
| 148 | skip_im2col, // reinterpret_input_as_3d |
| 149 | false, // retain_internal_weights |
| 150 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 151 | false, // fp_mixed_precision |
| 152 | true, // broadcast_bias |
| 153 | act_info); // activation_info |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 154 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 155 | if(is_quantized) |
| 156 | { |
| 157 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 158 | // Extract and negate input and weights offset |
| 159 | const QuantizationInfo input_quantization_info = input->quantization_info(); |
| 160 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 161 | |
| 162 | std::unique_ptr<ITensorInfo> input_qa = input->clone(); |
| 163 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 164 | input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 165 | weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 166 | |
| 167 | // Perform validation step on GEMMLowp |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 168 | return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, gemm_info); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 169 | } |
| 170 | else |
| 171 | { |
| 172 | // Perform validation step on Matrix multiply function |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 173 | return CLGEMM::validate(input, weights, biases, output, 1.0f, 1.0f, gemm_info); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 174 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 177 | void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 178 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 179 | { |
| 180 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 181 | |
| 182 | ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(), |
| 183 | weights->info(), |
| 184 | biases != nullptr ? biases->info() : nullptr, |
| 185 | output->info(), |
| 186 | conv_info, |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 187 | weights_info, |
| 188 | dilation, |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 189 | act_info, |
| 190 | num_groups)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 191 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 192 | const DataType data_type = input->info()->data_type(); |
| 193 | const DataLayout data_layout = input->info()->data_layout(); |
| 194 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 195 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 196 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 197 | |
| 198 | const unsigned int kernel_width = weights->info()->dimension(idx_width); |
| 199 | const unsigned int kernel_height = weights->info()->dimension(idx_height); |
| 200 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 201 | const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); |
| 202 | const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform(); |
| 203 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 204 | |
| 205 | _is_prepared = weights_info.retain_internal_weights(); |
| 206 | _original_weights = weights; |
| 207 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 208 | _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1); |
| 209 | _skip_col2im = data_layout == DataLayout::NHWC; |
| 210 | |
| 211 | // Only for quantize there are few cases where we cannot fuse the activation function in GEMM |
| 212 | _fuse_activation = true; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 213 | |
| 214 | // Set the GPU target for im2col and col2im |
| 215 | _im2col_kernel.set_target(CLScheduler::get().target()); |
| 216 | _col2im_kernel.set_target(CLScheduler::get().target()); |
| 217 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 218 | const ICLTensor *gemm_input_to_use = input; |
| 219 | ICLTensor *gemm_output_to_use = output; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 220 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 221 | // Get parameters from conv_info |
| 222 | unsigned int stride_x = 0; |
| 223 | unsigned int stride_y = 0; |
| 224 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 225 | |
| 226 | // Get convolved dimensions |
| 227 | unsigned int conv_w = 0; |
| 228 | unsigned int conv_h = 0; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 229 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width), |
| 230 | input->info()->dimension(idx_height), |
| 231 | kernel_width, |
| 232 | kernel_height, |
| 233 | conv_info, |
| 234 | dilation); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 235 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 236 | unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels) / num_groups; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 237 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 238 | const ICLTensor *biases_to_use = biases; |
| 239 | bool append_bias = false; |
| 240 | |
| 241 | if(num_groups != 1 && biases != nullptr) |
| 242 | { |
| 243 | // num_groups != 1 can only be for NCHW |
| 244 | // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor |
| 245 | biases_to_use = nullptr; |
| 246 | append_bias = true; |
| 247 | |
| 248 | _reshape_weights.configure(weights, biases, &_weights_reshaped, num_groups); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | _reshape_weights.configure(weights, nullptr, &_weights_reshaped, num_groups); |
| 253 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 254 | |
| 255 | // Create tensor to store im2col reshaped inputs |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 256 | if(!_skip_im2col) |
| 257 | { |
| 258 | _memory_group.manage(&_im2col_output); |
| 259 | |
| 260 | // Configure and tune im2col. im2col output shape is auto-initialized |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 261 | _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation, num_groups); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 262 | |
| 263 | // Set quantization info |
| 264 | _im2col_output.info()->set_quantization_info(input->info()->quantization_info()); |
| 265 | CLScheduler::get().tune_kernel_static(_im2col_kernel); |
| 266 | |
| 267 | // Update GEMM input |
| 268 | gemm_input_to_use = &_im2col_output; |
| 269 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 270 | |
| 271 | // Create GEMM output tensor |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 272 | if(!_skip_col2im) |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 273 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 274 | TensorShape shape_gemm; |
| 275 | |
| 276 | // If we cannot skip col2im it means we run im2col as well |
| 277 | shape_gemm = _im2col_output.info()->tensor_shape(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 278 | shape_gemm.set(0, mat_weights_cols); |
| 279 | shape_gemm.set(1, conv_w * conv_h); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 280 | |
Jenkins | 4ba87db | 2019-05-23 17:11:51 +0100 | [diff] [blame] | 281 | // TODO(COMPMID-2078): input->clone() doesn't work with subtensors for grouped convolutions. |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 282 | TensorInfo info_gemm(shape_gemm, 1, data_type); |
| 283 | info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout()); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 284 | _gemm_output.allocator()->init(info_gemm); |
| 285 | _memory_group.manage(&_gemm_output); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 286 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 287 | // Update GEMM output |
| 288 | gemm_output_to_use = &_gemm_output; |
| 289 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 290 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 291 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
| 292 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 293 | gemmlowp_output_stage.gemmlowp_offset = 0; |
| 294 | gemmlowp_output_stage.gemmlowp_multiplier = 0; |
| 295 | gemmlowp_output_stage.gemmlowp_shift = 0; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 296 | |
| 297 | // Configure output stage for quantized case |
| 298 | if(_is_quantized) |
| 299 | { |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 300 | const auto output_quant_info = (output->info()->total_size() == 0) ? iq_info : oq_info; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 301 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 302 | const float multiplier = (iq_info.scale * wq_info.scale) / output_quant_info.scale; |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame] | 303 | int output_multiplier = 0; |
| 304 | int output_shift = 0; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 305 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 306 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 307 | int min_activation = 0; |
| 308 | int max_activation = 0; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 309 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 310 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 311 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 312 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 313 | }; |
| 314 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 315 | if(act_info.enabled()) |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 316 | { |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 317 | if(supported_acts.count(act_info.activation()) != 0) |
| 318 | { |
| 319 | const int a_const_int = quantize_qasymm8(act_info.a(), output_quant_info); |
| 320 | const int b_const_int = quantize_qasymm8(act_info.b(), output_quant_info); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 321 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 322 | min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int; |
| 323 | max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | _fuse_activation = false; |
| 328 | } |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Set the GEMMLowp output stage info |
| 332 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 333 | gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier; |
| 334 | gemmlowp_output_stage.gemmlowp_shift = output_shift; |
| 335 | gemmlowp_output_stage.gemmlowp_min_bound = min_activation; |
| 336 | gemmlowp_output_stage.gemmlowp_max_bound = max_activation; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 339 | // Configure and tune GEMM |
| 340 | // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix |
| 341 | const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0; |
| 342 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 343 | configure_mm(gemm_input_to_use, &_weights_reshaped, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, act_info); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 344 | |
| 345 | if(!_skip_im2col) |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 346 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 347 | _im2col_output.allocator()->allocate(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 350 | if(!_skip_col2im) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 351 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 352 | // Configure and tune Col2Im |
| 353 | _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups); |
| 354 | CLScheduler::get().tune_kernel_static(_col2im_kernel); |
| 355 | } |
| 356 | |
| 357 | if(!_skip_col2im) |
| 358 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 359 | _gemm_output.allocator()->allocate(); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 360 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 361 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 362 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h), |
| 363 | "Output shape does not match the expected one"); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 364 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 365 | if(!_fuse_activation) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 366 | { |
| 367 | _activationlayer_function.configure(output, nullptr, act_info); |
| 368 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 369 | |
| 370 | ARM_COMPUTE_UNUSED(weights_info); |
| 371 | } |
| 372 | |
| 373 | Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 374 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 375 | { |
| 376 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 377 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 378 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 379 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 380 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
| 381 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_layout() != DataLayout::NCHW), "Grouping (num_groups != 1) with NHWC data layout is not supported"); |
| 382 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8"); |
| 383 | ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW)); |
| 384 | |
| 385 | const DataLayout data_layout = input->data_layout(); |
| 386 | const DataType data_type = input->data_type(); |
| 387 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 388 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 389 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 390 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 391 | |
| 392 | const unsigned int kernel_width = weights->dimension(idx_width); |
| 393 | const unsigned int kernel_height = weights->dimension(idx_height); |
| 394 | |
Jenkins | 4ba87db | 2019-05-23 17:11:51 +0100 | [diff] [blame] | 395 | TensorInfo im2col_reshaped_info{}; |
| 396 | TensorInfo info_gemm{}; |
| 397 | TensorInfo weights_reshaped_info{}; |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 398 | const ITensorInfo *gemm_input_to_use = input; |
| 399 | const ITensorInfo *gemm_output_to_use = output; |
| 400 | const ITensorInfo *weights_to_use = weights; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 401 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 402 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
| 403 | const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1); |
| 404 | const bool skip_col2im = data_layout == DataLayout::NHWC; |
| 405 | bool fuse_activation = true; |
| 406 | |
| 407 | const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); |
| 408 | const UniformQuantizationInfo wq_info = weights->quantization_info().uniform(); |
| 409 | const UniformQuantizationInfo oq_info = output->quantization_info().uniform(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 410 | |
| 411 | ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 412 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 413 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 414 | // Validate biases |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 415 | if(biases != nullptr) |
| 416 | { |
| 417 | if(is_quantized) |
| 418 | { |
| 419 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 424 | } |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 425 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 426 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 427 | } |
| 428 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 429 | if(act_info.enabled()) |
| 430 | { |
| 431 | ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a()); |
| 432 | } |
| 433 | |
| 434 | // Get convolved dimensions |
| 435 | unsigned int conv_w = 0; |
| 436 | unsigned int conv_h = 0; |
| 437 | |
| 438 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), |
| 439 | input->dimension(idx_height), |
| 440 | kernel_width, |
| 441 | kernel_height, |
| 442 | conv_info, |
| 443 | dilation); |
| 444 | |
| 445 | unsigned int mat_weights_cols = weights->dimension(idx_kernels) / num_groups; |
| 446 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 447 | const ITensorInfo *biases_to_use = biases; |
| 448 | bool append_bias = false; |
| 449 | |
| 450 | if(num_groups != 1 && biases != nullptr) |
| 451 | { |
| 452 | // num_groups != 1 can only be for NCHW |
| 453 | // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor |
| 454 | biases_to_use = nullptr; |
| 455 | append_bias = true; |
| 456 | |
| 457 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, biases, nullptr, num_groups)); |
| 458 | weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, true, num_groups), 1, data_type); |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, nullptr, nullptr, num_groups)); |
| 463 | weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, false, num_groups), 1, data_type); |
| 464 | } |
| 465 | |
| 466 | weights_to_use = &weights_reshaped_info; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 467 | |
| 468 | if(!skip_im2col) |
| 469 | { |
| 470 | const Size2D kernel_dims(kernel_width, kernel_height); |
| 471 | |
| 472 | // Output tensor auto initialization if not yet initialized |
| 473 | TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups); |
| 474 | |
| 475 | auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape)); |
| 476 | |
| 477 | ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups)); |
| 478 | gemm_input_to_use = &im2col_reshaped_info; |
| 479 | } |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 480 | |
| 481 | // Create GEMM output tensor |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 482 | if(!skip_col2im) |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 483 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 484 | TensorShape shape_gemm; |
| 485 | |
| 486 | shape_gemm = gemm_input_to_use->tensor_shape(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 487 | shape_gemm.set(0, mat_weights_cols); |
| 488 | shape_gemm.set(1, conv_w * conv_h); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 489 | |
| 490 | info_gemm = TensorInfo(shape_gemm, 1, data_type); |
| 491 | info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout()); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 492 | gemm_output_to_use = &info_gemm; |
| 493 | } |
| 494 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 495 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
| 496 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 497 | gemmlowp_output_stage.gemmlowp_offset = 0; |
| 498 | gemmlowp_output_stage.gemmlowp_multiplier = 0; |
| 499 | gemmlowp_output_stage.gemmlowp_shift = 0; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 500 | |
| 501 | if(is_quantized) |
| 502 | { |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 503 | const auto output_quant_info = (output->total_size() == 0) ? iq_info : oq_info; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 504 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 505 | const float multiplier = (iq_info.scale * wq_info.scale) / output_quant_info.scale; |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame] | 506 | int output_multiplier = 0; |
| 507 | int output_shift = 0; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 508 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 509 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift)); |
| 510 | |
| 511 | int min_activation = 0; |
| 512 | int max_activation = 0; |
| 513 | |
| 514 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 515 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 516 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 517 | }; |
| 518 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 519 | if(act_info.enabled()) |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 520 | { |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 521 | if(supported_acts.count(act_info.activation()) != 0) |
| 522 | { |
| 523 | const int a_const_int = quantize_qasymm8(act_info.a(), output_quant_info); |
| 524 | const int b_const_int = quantize_qasymm8(act_info.b(), output_quant_info); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 525 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 526 | min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int; |
| 527 | max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int; |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | fuse_activation = false; |
| 532 | } |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Set the GEMMLowp output stage info |
| 536 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 537 | gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier; |
| 538 | gemmlowp_output_stage.gemmlowp_shift = output_shift; |
| 539 | gemmlowp_output_stage.gemmlowp_min_bound = min_activation; |
| 540 | gemmlowp_output_stage.gemmlowp_max_bound = max_activation; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 543 | // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix |
| 544 | const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0; |
| 545 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 546 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, skip_im2col, act_info)); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 547 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 548 | // Validate Col2Im |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 549 | if(!skip_col2im) |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 550 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 551 | ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups)); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 554 | //Validate Activation Layer |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 555 | if(!fuse_activation) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 556 | { |
| 557 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
| 558 | } |
| 559 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 560 | return Status{}; |
| 561 | } |
| 562 | |
| 563 | void CLGEMMConvolutionLayer::run() |
| 564 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 565 | prepare(); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 566 | |
Jenkins | 4ba87db | 2019-05-23 17:11:51 +0100 | [diff] [blame] | 567 | MemoryGroupResourceScope scope_mg(_memory_group); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 568 | |
| 569 | // Run im2col |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 570 | if(!_skip_im2col) |
| 571 | { |
| 572 | CLScheduler::get().enqueue(_im2col_kernel); |
| 573 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 574 | |
| 575 | // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions |
| 576 | if(_is_quantized) |
| 577 | { |
| 578 | // Run gemmlowp |
| 579 | _mm_gemmlowp.run(); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 580 | } |
| 581 | else |
| 582 | { |
| 583 | // Run gemm |
| 584 | _mm_gemm.run(); |
| 585 | } |
| 586 | |
| 587 | // Reshape output matrix |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 588 | if(!_skip_col2im) |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 589 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 590 | CLScheduler::get().enqueue(_col2im_kernel, false); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 591 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 592 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame^] | 593 | //Run Activation Layer if we cannot fuse in GEMM |
| 594 | if(!_fuse_activation) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 595 | { |
| 596 | _activationlayer_function.run(); |
| 597 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 598 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 599 | |
| 600 | void CLGEMMConvolutionLayer::prepare() |
| 601 | { |
| 602 | if(!_is_prepared) |
| 603 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 604 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 605 | |
| 606 | // Run weights reshaping and mark original weights tensor as unused |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 607 | _weights_reshaped.allocator()->allocate(); |
| 608 | _reshape_weights.run(); |
| 609 | _original_weights->mark_as_unused(); |
| 610 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 611 | // Prepare GEMM |
| 612 | _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare(); |
| 613 | if(!_weights_reshaped.is_used()) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 614 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 615 | _weights_reshaped.allocator()->free(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | CLScheduler::get().queue().finish(); |
| 619 | _is_prepared = true; |
| 620 | } |
| 621 | } |