blob: 682812b1c88eb072b0928dddee40e9fb87d4ccc8 [file] [log] [blame]
Anthony Barbier06ea0482018-02-22 15:45:35 +00001/*
Jenkins36ccc902020-02-21 11:10:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier06ea0482018-02-22 15:45:35 +00003 *
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"
Jenkins0e205f72019-11-28 16:53:35 +000030#include "arm_compute/core/utils/misc/Cast.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
33#include "arm_compute/runtime/CL/CLScheduler.h"
34
35#include <cmath>
36#include <memory>
37#include <tuple>
38
Jenkins0e205f72019-11-28 16:53:35 +000039namespace arm_compute
40{
Anthony Barbier06ea0482018-02-22 15:45:35 +000041using namespace arm_compute::misc::shape_calculator;
Jenkins0e205f72019-11-28 16:53:35 +000042using namespace arm_compute::utils::cast;
Anthony Barbier06ea0482018-02-22 15:45:35 +000043
Jenkinsb3a371b2018-05-23 11:36:53 +010044CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights()
45 : _weights_reshape_kernel()
Anthony Barbier06ea0482018-02-22 15:45:35 +000046{
47}
48
Jenkins52ba29e2018-08-29 15:32:11 +000049void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +000050{
51 // Perform validation step
52 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
53 ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayerReshapeWeights::validate(weights->info(),
54 (biases != nullptr) ? biases->info() : nullptr,
Jenkins52ba29e2018-08-29 15:32:11 +000055 output->info(),
56 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +000057
58 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
59 const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr;
60
Jenkins52ba29e2018-08-29 15:32:11 +000061 _weights_reshape_kernel.configure(weights, biases_to_use, output, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +000062
63 output->info()->set_quantization_info(weights->info()->quantization_info());
64}
65
Jenkins52ba29e2018-08-29 15:32:11 +000066Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +000067{
68 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
Jenkins36ccc902020-02-21 11:10:48 +000069 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::F16, DataType::F32);
Anthony Barbier06ea0482018-02-22 15:45:35 +000070 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
71
72 if(biases != nullptr)
73 {
Jenkins52ba29e2018-08-29 15:32:11 +000074 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Jenkins0e205f72019-11-28 16:53:35 +000075 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(weights->data_type()));
76
Anthony Barbier06ea0482018-02-22 15:45:35 +000077 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Jenkins52ba29e2018-08-29 15:32:11 +000078 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +000079 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
80 }
81
82 if((output != nullptr) && (output->total_size() != 0))
83 {
84 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
Jenkins52ba29e2018-08-29 15:32:11 +000085 CLWeightsReshapeKernel::validate(weights, biases, output, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +000086 }
87
88 return Status{};
89}
90
91void CLConvolutionLayerReshapeWeights::run()
92{
Anthony Barbier06ea0482018-02-22 15:45:35 +000093 CLScheduler::get().enqueue(_weights_reshape_kernel);
Anthony Barbier06ea0482018-02-22 15:45:35 +000094}
95
Jenkins0e205f72019-11-28 16:53:35 +000096CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
97 : _memory_group(memory_manager), _weights_manager(weights_manager), _reshape_weights(), _reshape_weights_managed(), _im2col_kernel(), _mm_gemm(memory_manager, weights_manager),
98 _mm_gemmlowp(memory_manager), _col2im_kernel(), _activationlayer_function(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _skip_im2col(false),
99 _skip_col2im(false), _is_quantized(false), _fuse_activation(true), _is_prepared(false)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000100{
101}
102
Jenkinsb9abeae2018-11-22 11:58:08 +0000103void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
Jenkins975dfe12019-09-02 11:47:54 +0100104 int gemm_3d_depth, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000105{
106 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Jenkins975dfe12019-09-02 11:47:54 +0100107 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));
Jenkinsb9abeae2018-11-22 11:58:08 +0000108
Jenkins975dfe12019-09-02 11:47:54 +0100109 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
110 false, // is_b_reshaped
111 true, // reshape_b_only_on_first_run
112 gemm_3d_depth, // depth_output_gemm3d
113 _skip_im2col, // reinterpret_input_as_3d
114 false, // retain_internal_weights
115 gemmlowp_output_stage, // gemmlowp_output_stage
116 false, // fp_mixed_precision
117 true, // broadcast_bias
118 act_info); // activation_info
Anthony Barbier06ea0482018-02-22 15:45:35 +0000119
120 if(_is_quantized)
121 {
122 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
123 // Extract and negate input and weights offset
124 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
125 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
126
Jenkins975dfe12019-09-02 11:47:54 +0100127 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
128 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000129
Jenkinsb9abeae2018-11-22 11:58:08 +0000130 _mm_gemmlowp.configure(input, weights, biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000131
132 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
133 input->info()->set_quantization_info(input_quantization_info);
134 weights->info()->set_quantization_info(weights_quantization_info);
135 }
136 else
137 {
138 // Configure matrix multiply function
Jenkins975dfe12019-09-02 11:47:54 +0100139 _mm_gemm.configure(input, weights, biases, output, 1.0f, 1.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000140 }
141}
142
Jenkinsb9abeae2018-11-22 11:58:08 +0000143Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Jenkins975dfe12019-09-02 11:47:54 +0100144 const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000145{
146 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
147
Jenkins975dfe12019-09-02 11:47:54 +0100148 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
149 false, // is_b_reshaped
150 true, // reshape_b_only_on_first_run
151 gemm_3d_depth, // depth_output_gemm3d
152 skip_im2col, // reinterpret_input_as_3d
153 false, // retain_internal_weights
154 gemmlowp_output_stage, // gemmlowp_output_stage
155 false, // fp_mixed_precision
156 true, // broadcast_bias
157 act_info); // activation_info
Jenkinsb9abeae2018-11-22 11:58:08 +0000158
Anthony Barbier06ea0482018-02-22 15:45:35 +0000159 if(is_quantized)
160 {
161 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
162 // Extract and negate input and weights offset
163 const QuantizationInfo input_quantization_info = input->quantization_info();
164 const QuantizationInfo weights_quantization_info = weights->quantization_info();
165
166 std::unique_ptr<ITensorInfo> input_qa = input->clone();
167 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
Jenkins975dfe12019-09-02 11:47:54 +0100168 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
169 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000170
171 // Perform validation step on GEMMLowp
Jenkinsb9abeae2018-11-22 11:58:08 +0000172 return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000173 }
174 else
175 {
176 // Perform validation step on Matrix multiply function
Jenkins975dfe12019-09-02 11:47:54 +0100177 return CLGEMM::validate(input, weights, biases, output, 1.0f, 1.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000178 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000179}
180
Jenkinsb3a371b2018-05-23 11:36:53 +0100181void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000182 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000183{
184 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
185
186 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
187 weights->info(),
188 biases != nullptr ? biases->info() : nullptr,
189 output->info(),
190 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100191 weights_info,
192 dilation,
Jenkins52ba29e2018-08-29 15:32:11 +0000193 act_info,
194 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000195
Jenkins52ba29e2018-08-29 15:32:11 +0000196 const DataType data_type = input->info()->data_type();
197 const DataLayout data_layout = input->info()->data_layout();
198 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
199 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
200 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
201
202 const unsigned int kernel_width = weights->info()->dimension(idx_width);
203 const unsigned int kernel_height = weights->info()->dimension(idx_height);
Jenkins0e205f72019-11-28 16:53:35 +0000204 const unsigned int num_kernels = weights->info()->dimension(idx_kernels);
Jenkins52ba29e2018-08-29 15:32:11 +0000205
Jenkins975dfe12019-09-02 11:47:54 +0100206 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
Jenkins975dfe12019-09-02 11:47:54 +0100207 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
208
209 _is_prepared = weights_info.retain_internal_weights();
210 _original_weights = weights;
211 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
212 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
213 _skip_col2im = data_layout == DataLayout::NHWC;
214
215 // Only for quantize there are few cases where we cannot fuse the activation function in GEMM
216 _fuse_activation = true;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000217
218 // Set the GPU target for im2col and col2im
219 _im2col_kernel.set_target(CLScheduler::get().target());
220 _col2im_kernel.set_target(CLScheduler::get().target());
221
Jenkinsb9abeae2018-11-22 11:58:08 +0000222 const ICLTensor *gemm_input_to_use = input;
223 ICLTensor *gemm_output_to_use = output;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000224
Anthony Barbier06ea0482018-02-22 15:45:35 +0000225 // Get parameters from conv_info
226 unsigned int stride_x = 0;
227 unsigned int stride_y = 0;
228 std::tie(stride_x, stride_y) = conv_info.stride();
229
230 // Get convolved dimensions
231 unsigned int conv_w = 0;
232 unsigned int conv_h = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000233 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
234 input->info()->dimension(idx_height),
235 kernel_width,
236 kernel_height,
237 conv_info,
238 dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000239
Jenkins0e205f72019-11-28 16:53:35 +0000240 unsigned int mat_weights_cols = num_kernels / num_groups;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000241
Jenkins975dfe12019-09-02 11:47:54 +0100242 const ICLTensor *biases_to_use = biases;
243 bool append_bias = false;
244
Jenkins0e205f72019-11-28 16:53:35 +0000245 ICLTensor *weights_to_use = &_weights_reshaped;
Jenkins975dfe12019-09-02 11:47:54 +0100246 if(num_groups != 1 && biases != nullptr)
247 {
248 // num_groups != 1 can only be for NCHW
249 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
250 biases_to_use = nullptr;
251 append_bias = true;
252
Jenkins0e205f72019-11-28 16:53:35 +0000253 if(_weights_manager && _weights_manager->are_weights_managed(weights))
254 {
255 _reshape_weights_managed.configure(weights, biases, num_groups);
256 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed));
257 }
258 else
259 {
260 _reshape_weights.configure(weights, biases, &_weights_reshaped, num_groups);
261 }
Jenkins975dfe12019-09-02 11:47:54 +0100262 }
263 else
264 {
Jenkins0e205f72019-11-28 16:53:35 +0000265 if(_weights_manager && _weights_manager->are_weights_managed(weights))
266 {
267 _reshape_weights_managed.configure(weights, nullptr, num_groups);
268 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed));
269 }
270 else
271 {
272 _reshape_weights.configure(weights, nullptr, &_weights_reshaped, num_groups);
273 }
Jenkins975dfe12019-09-02 11:47:54 +0100274 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000275
276 // Create tensor to store im2col reshaped inputs
Jenkins52ba29e2018-08-29 15:32:11 +0000277 if(!_skip_im2col)
278 {
279 _memory_group.manage(&_im2col_output);
280
281 // Configure and tune im2col. im2col output shape is auto-initialized
Jenkins975dfe12019-09-02 11:47:54 +0100282 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation, num_groups);
Jenkins52ba29e2018-08-29 15:32:11 +0000283
284 // Set quantization info
285 _im2col_output.info()->set_quantization_info(input->info()->quantization_info());
286 CLScheduler::get().tune_kernel_static(_im2col_kernel);
287
288 // Update GEMM input
289 gemm_input_to_use = &_im2col_output;
290 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000291
292 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000293 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000294 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000295 TensorShape shape_gemm;
296
297 // If we cannot skip col2im it means we run im2col as well
298 shape_gemm = _im2col_output.info()->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000299 shape_gemm.set(0, mat_weights_cols);
300 shape_gemm.set(1, conv_w * conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000301
Jenkins4ba87db2019-05-23 17:11:51 +0100302 // TODO(COMPMID-2078): input->clone() doesn't work with subtensors for grouped convolutions.
Jenkinsb9abeae2018-11-22 11:58:08 +0000303 TensorInfo info_gemm(shape_gemm, 1, data_type);
304 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000305 _gemm_output.allocator()->init(info_gemm);
306 _memory_group.manage(&_gemm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000307
Jenkins52ba29e2018-08-29 15:32:11 +0000308 // Update GEMM output
309 gemm_output_to_use = &_gemm_output;
310 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000311
Jenkinsb9abeae2018-11-22 11:58:08 +0000312 GEMMLowpOutputStageInfo gemmlowp_output_stage;
Jenkins0e205f72019-11-28 16:53:35 +0000313 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
314 gemmlowp_output_stage.gemmlowp_offset = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000315
316 // Configure output stage for quantized case
317 if(_is_quantized)
318 {
Jenkins0e205f72019-11-28 16:53:35 +0000319 const auto output_quant_info = (output->info()->total_size() == 0) ? iq_info : oq_info;
320 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type());
321 const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000322
Jenkins0e205f72019-11-28 16:53:35 +0000323 gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel;
324
325 gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters);
326 gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters);
327 quantization::compute_quantized_multipliers_and_shifts(input->info(),
328 weights->info(),
329 output->info(),
330 idx_kernels,
331 gemmlowp_output_stage.gemmlowp_multipliers.data(),
332 gemmlowp_output_stage.gemmlowp_shifts.data());
333 gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0];
334 gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0];
Jenkins52ba29e2018-08-29 15:32:11 +0000335
Jenkinsb9abeae2018-11-22 11:58:08 +0000336 int min_activation = 0;
337 int max_activation = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000338
Jenkinsb9abeae2018-11-22 11:58:08 +0000339 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
340 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
341 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
342 };
343
Jenkins975dfe12019-09-02 11:47:54 +0100344 if(act_info.enabled())
Jenkinsb9abeae2018-11-22 11:58:08 +0000345 {
Jenkins975dfe12019-09-02 11:47:54 +0100346 if(supported_acts.count(act_info.activation()) != 0)
347 {
Jenkins36ccc902020-02-21 11:10:48 +0000348 std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, output_quant_info);
Jenkins975dfe12019-09-02 11:47:54 +0100349 }
350 else
351 {
352 _fuse_activation = false;
353 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000354 }
355
356 // Set the GEMMLowp output stage info
Jenkins0e205f72019-11-28 16:53:35 +0000357 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
358 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
359 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000360 }
361
Jenkinsb9abeae2018-11-22 11:58:08 +0000362 // Configure and tune GEMM
363 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
364 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
365
Jenkins0e205f72019-11-28 16:53:35 +0000366 configure_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, act_info);
Jenkinsb9abeae2018-11-22 11:58:08 +0000367
368 if(!_skip_im2col)
Jenkins52ba29e2018-08-29 15:32:11 +0000369 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000370 _im2col_output.allocator()->allocate();
Jenkins52ba29e2018-08-29 15:32:11 +0000371 }
372
Jenkinsb9abeae2018-11-22 11:58:08 +0000373 if(!_skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000374 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000375 // Configure and tune Col2Im
376 _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups);
377 CLScheduler::get().tune_kernel_static(_col2im_kernel);
378 }
379
380 if(!_skip_col2im)
381 {
Jenkins52ba29e2018-08-29 15:32:11 +0000382 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000383 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000384
Jenkins52ba29e2018-08-29 15:32:11 +0000385 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
386 "Output shape does not match the expected one");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000387
Jenkins975dfe12019-09-02 11:47:54 +0100388 if(!_fuse_activation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100389 {
390 _activationlayer_function.configure(output, nullptr, act_info);
391 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000392
393 ARM_COMPUTE_UNUSED(weights_info);
394}
395
396Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000397 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000398{
399 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
400 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Jenkins36ccc902020-02-21 11:10:48 +0000401 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Jenkins0e205f72019-11-28 16:53:35 +0000402 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type());
403
404 if(is_quantized_per_channel)
405 {
406 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() != DataType::QASYMM8, "Input data type not compatible with Weights");
407 }
408 else
409 {
410 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
411 }
Jenkins52ba29e2018-08-29 15:32:11 +0000412 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
413 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");
414 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8");
415 ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW));
416
417 const DataLayout data_layout = input->data_layout();
418 const DataType data_type = input->data_type();
419 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
420 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
421 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
422 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
423
424 const unsigned int kernel_width = weights->dimension(idx_width);
425 const unsigned int kernel_height = weights->dimension(idx_height);
Jenkins0e205f72019-11-28 16:53:35 +0000426 const unsigned int num_kernels = weights->dimension(idx_kernels);
Jenkins52ba29e2018-08-29 15:32:11 +0000427
Jenkins4ba87db2019-05-23 17:11:51 +0100428 TensorInfo im2col_reshaped_info{};
429 TensorInfo info_gemm{};
430 TensorInfo weights_reshaped_info{};
Jenkinsb9abeae2018-11-22 11:58:08 +0000431 const ITensorInfo *gemm_input_to_use = input;
432 const ITensorInfo *gemm_output_to_use = output;
433 const ITensorInfo *weights_to_use = weights;
Jenkins0e205f72019-11-28 16:53:35 +0000434 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
435 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
436 const bool skip_col2im = data_layout == DataLayout::NHWC;
437 bool fuse_activation = true;
Jenkins52ba29e2018-08-29 15:32:11 +0000438
439 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000440 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
441
Jenkins52ba29e2018-08-29 15:32:11 +0000442 // Validate biases
Anthony Barbier06ea0482018-02-22 15:45:35 +0000443 if(biases != nullptr)
444 {
445 if(is_quantized)
446 {
447 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
448 }
449 else
450 {
451 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
452 }
Jenkins52ba29e2018-08-29 15:32:11 +0000453 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000454 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
455 }
456
Jenkins52ba29e2018-08-29 15:32:11 +0000457 if(act_info.enabled())
458 {
459 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
460 }
461
462 // Get convolved dimensions
463 unsigned int conv_w = 0;
464 unsigned int conv_h = 0;
465
466 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
467 input->dimension(idx_height),
468 kernel_width,
469 kernel_height,
470 conv_info,
471 dilation);
472
Jenkins0e205f72019-11-28 16:53:35 +0000473 unsigned int mat_weights_cols = num_kernels / num_groups;
Jenkins52ba29e2018-08-29 15:32:11 +0000474
Jenkins975dfe12019-09-02 11:47:54 +0100475 const ITensorInfo *biases_to_use = biases;
476 bool append_bias = false;
477
478 if(num_groups != 1 && biases != nullptr)
479 {
480 // num_groups != 1 can only be for NCHW
481 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
482 biases_to_use = nullptr;
483 append_bias = true;
484
485 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, biases, nullptr, num_groups));
486 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, true, num_groups), 1, data_type);
487 }
488 else
489 {
490 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, nullptr, nullptr, num_groups));
491 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, false, num_groups), 1, data_type);
492 }
493
494 weights_to_use = &weights_reshaped_info;
Jenkins52ba29e2018-08-29 15:32:11 +0000495
496 if(!skip_im2col)
497 {
498 const Size2D kernel_dims(kernel_width, kernel_height);
499
500 // Output tensor auto initialization if not yet initialized
501 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups);
502
503 auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape));
504
505 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups));
506 gemm_input_to_use = &im2col_reshaped_info;
507 }
Jenkins52ba29e2018-08-29 15:32:11 +0000508
509 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000510 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000511 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000512 TensorShape shape_gemm;
513
514 shape_gemm = gemm_input_to_use->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000515 shape_gemm.set(0, mat_weights_cols);
516 shape_gemm.set(1, conv_w * conv_h);
Jenkinsb9abeae2018-11-22 11:58:08 +0000517
518 info_gemm = TensorInfo(shape_gemm, 1, data_type);
519 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000520 gemm_output_to_use = &info_gemm;
521 }
522
Jenkinsb9abeae2018-11-22 11:58:08 +0000523 GEMMLowpOutputStageInfo gemmlowp_output_stage;
Jenkins0e205f72019-11-28 16:53:35 +0000524 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
525 gemmlowp_output_stage.gemmlowp_offset = 0;
526 gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel;
Jenkins52ba29e2018-08-29 15:32:11 +0000527
528 if(is_quantized)
529 {
Jenkins0e205f72019-11-28 16:53:35 +0000530 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
531 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
532 const auto output_quant_info = (output->total_size() == 0) ? iq_info : oq_info;
533 const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1;
Jenkins52ba29e2018-08-29 15:32:11 +0000534
Jenkins0e205f72019-11-28 16:53:35 +0000535 gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters);
536 gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters);
537 quantization::compute_quantized_multipliers_and_shifts(input,
538 weights,
539 output,
540 idx_kernels,
541 gemmlowp_output_stage.gemmlowp_multipliers.data(),
542 gemmlowp_output_stage.gemmlowp_shifts.data());
543 gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0];
544 gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0];
Jenkinsb9abeae2018-11-22 11:58:08 +0000545
546 int min_activation = 0;
547 int max_activation = 0;
548
549 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
550 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
551 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
552 };
553
Jenkins975dfe12019-09-02 11:47:54 +0100554 if(act_info.enabled())
Jenkinsb9abeae2018-11-22 11:58:08 +0000555 {
Jenkins975dfe12019-09-02 11:47:54 +0100556 if(supported_acts.count(act_info.activation()) != 0)
557 {
Jenkins36ccc902020-02-21 11:10:48 +0000558 std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, output_quant_info);
Jenkins975dfe12019-09-02 11:47:54 +0100559 }
560 else
561 {
562 fuse_activation = false;
563 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000564 }
565
566 // Set the GEMMLowp output stage info
Jenkins0e205f72019-11-28 16:53:35 +0000567 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
568 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
569 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Jenkins52ba29e2018-08-29 15:32:11 +0000570 }
571
Jenkinsb9abeae2018-11-22 11:58:08 +0000572 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
573 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
574
Jenkins975dfe12019-09-02 11:47:54 +0100575 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));
Jenkinsb9abeae2018-11-22 11:58:08 +0000576
Jenkins52ba29e2018-08-29 15:32:11 +0000577 // Validate Col2Im
Jenkinsb9abeae2018-11-22 11:58:08 +0000578 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000579 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000580 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups));
Jenkins52ba29e2018-08-29 15:32:11 +0000581 }
582
Jenkinsb3a371b2018-05-23 11:36:53 +0100583 //Validate Activation Layer
Jenkins975dfe12019-09-02 11:47:54 +0100584 if(!fuse_activation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100585 {
586 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
587 }
588
Anthony Barbier06ea0482018-02-22 15:45:35 +0000589 return Status{};
590}
591
592void CLGEMMConvolutionLayer::run()
593{
Jenkinsb3a371b2018-05-23 11:36:53 +0100594 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000595
Jenkins4ba87db2019-05-23 17:11:51 +0100596 MemoryGroupResourceScope scope_mg(_memory_group);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000597
598 // Run im2col
Jenkins52ba29e2018-08-29 15:32:11 +0000599 if(!_skip_im2col)
600 {
601 CLScheduler::get().enqueue(_im2col_kernel);
602 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000603
604 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
605 if(_is_quantized)
606 {
607 // Run gemmlowp
608 _mm_gemmlowp.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000609 }
610 else
611 {
612 // Run gemm
613 _mm_gemm.run();
614 }
615
616 // Reshape output matrix
Jenkinsb9abeae2018-11-22 11:58:08 +0000617 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000618 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000619 CLScheduler::get().enqueue(_col2im_kernel, false);
Jenkins52ba29e2018-08-29 15:32:11 +0000620 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000621
Jenkins975dfe12019-09-02 11:47:54 +0100622 //Run Activation Layer if we cannot fuse in GEMM
623 if(!_fuse_activation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100624 {
625 _activationlayer_function.run();
626 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000627}
Jenkinsb3a371b2018-05-23 11:36:53 +0100628
629void CLGEMMConvolutionLayer::prepare()
630{
631 if(!_is_prepared)
632 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100633 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Jenkins0e205f72019-11-28 16:53:35 +0000634 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
635 {
636 _weights_manager->run(_original_weights, &_reshape_weights_managed);
637 }
638 else
639 {
640 // Run weights reshaping and mark original weights tensor as unused
641 _weights_reshaped.allocator()->allocate();
642 _reshape_weights.run();
643 _original_weights->mark_as_unused();
644 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100645
Jenkins52ba29e2018-08-29 15:32:11 +0000646 // Prepare GEMM
647 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
648 if(!_weights_reshaped.is_used())
Jenkinsb3a371b2018-05-23 11:36:53 +0100649 {
Jenkins52ba29e2018-08-29 15:32:11 +0000650 _weights_reshaped.allocator()->free();
Jenkinsb3a371b2018-05-23 11:36:53 +0100651 }
652
653 CLScheduler::get().queue().finish();
654 _is_prepared = true;
655 }
656}
Jenkins0e205f72019-11-28 16:53:35 +0000657} // namespace arm_compute