blob: 27bed44098d061a79296667758b29667afd08449 [file] [log] [blame]
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001/*
2 * Copyright (c) 2017-2018 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/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"
Georgios Pinitas78c00902018-01-09 17:33:11 +000030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000031#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
38using namespace arm_compute;
Georgios Pinitas78c00902018-01-09 17:33:11 +000039using namespace arm_compute::misc::shape_calculator;
Isabella Gottardif07d28d2018-02-06 14:52:43 +000040
Georgios Pinitasd8734b52017-12-22 15:27:52 +000041CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights()
42 : _weights_reshape_kernel()
Isabella Gottardif07d28d2018-02-06 14:52:43 +000043{
44}
45
Georgios Pinitas78c00902018-01-09 17:33:11 +000046void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000047{
Georgios Pinitas78c00902018-01-09 17:33:11 +000048 // Perform validation step
Isabella Gottardif07d28d2018-02-06 14:52:43 +000049 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
Georgios Pinitas78c00902018-01-09 17:33:11 +000050 ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayerReshapeWeights::validate(weights->info(),
51 (biases != nullptr) ? biases->info() : nullptr,
52 output->info()));
53
54 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
55 const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr;
56
57 _weights_reshape_kernel.configure(weights, biases_to_use, output);
58
59 output->info()->set_quantization_info(weights->info()->quantization_info());
60}
61
62Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output)
63{
64 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
66 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000067
68 if(biases != nullptr)
69 {
Georgios Pinitas78c00902018-01-09 17:33:11 +000070 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
72 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
73 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000074 }
75
Georgios Pinitas78c00902018-01-09 17:33:11 +000076 if((output != nullptr) && (output->total_size() != 0))
Isabella Gottardif07d28d2018-02-06 14:52:43 +000077 {
Georgios Pinitas78c00902018-01-09 17:33:11 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000080
Georgios Pinitas78c00902018-01-09 17:33:11 +000081 CLWeightsReshapeKernel::validate(weights, biases, output);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000082 }
83
Georgios Pinitas78c00902018-01-09 17:33:11 +000084 return Status{};
Isabella Gottardif07d28d2018-02-06 14:52:43 +000085}
86
87void CLConvolutionLayerReshapeWeights::run()
88{
Isabella Gottardif07d28d2018-02-06 14:52:43 +000089 CLScheduler::get().enqueue(_weights_reshape_kernel);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000090}
91
92CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000093 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(),
Michele Di Giorgiob62280a2018-05-31 17:31:05 +010094 _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false),
95 _retain_internal_weights(false)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000096{
97}
98
Georgios Pinitas78c00902018-01-09 17:33:11 +000099void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000100{
101 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000102 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info()));
103
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000104 if(_is_quantized)
105 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000106 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
107 // Extract and negate input and weights offset
108 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
109 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000110
Georgios Pinitas78c00902018-01-09 17:33:11 +0000111 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
112 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000113
Georgios Pinitas78c00902018-01-09 17:33:11 +0000114 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000115
Georgios Pinitas78c00902018-01-09 17:33:11 +0000116 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
117 input->info()->set_quantization_info(input_quantization_info);
118 weights->info()->set_quantization_info(weights_quantization_info);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000119 }
120 else
121 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000122 // Configure matrix multiply function
123 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000124 }
125}
126
Georgios Pinitas78c00902018-01-09 17:33:11 +0000127Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output)
128{
129 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
130
131 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */);
132 if(is_quantized)
133 {
134 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
135 // Extract and negate input and weights offset
136 const QuantizationInfo input_quantization_info = input->quantization_info();
137 const QuantizationInfo weights_quantization_info = weights->quantization_info();
138
139 std::unique_ptr<ITensorInfo> input_qa = input->clone();
140 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
141 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
142 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
143
144 // Perform validation step on GEMMLowp
145 CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info);
146 }
147 else
148 {
149 // Perform validation step on Matrix multiply function
150 CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
151 }
152 return Status{};
153}
154
Alex Gilday7da29b62018-03-23 14:16:00 +0000155void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000156 const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000157{
158 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000159
160 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
161 weights->info(),
162 biases != nullptr ? biases->info() : nullptr,
163 output->info(),
164 conv_info,
Alex Gilday7da29b62018-03-23 14:16:00 +0000165 weights_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000166 dilation,
167 act_info));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000168
Michele Di Giorgiob62280a2018-05-31 17:31:05 +0100169 _is_prepared = false;
170 _original_weights = weights;
171 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
172 _retain_internal_weights = weights_info.retain_internal_weights();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000173
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000174 const DataType dt = input->info()->data_type();
175
Georgios Pinitas78c00902018-01-09 17:33:11 +0000176 // Set the GPU target for im2col and col2im
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000177 _im2col_kernel.set_target(CLScheduler::get().target());
178 _col2im_kernel.set_target(CLScheduler::get().target());
179
180 const bool append_bias = (biases != nullptr) && (!_is_quantized);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000181
182 const unsigned bias_element = (append_bias) ? 1 : 0;
183 const ICLTensor *biases_to_use = (append_bias) ? biases : nullptr;
184
185 // Get parameters from conv_info
186 unsigned int stride_x = 0;
187 unsigned int stride_y = 0;
188 std::tie(stride_x, stride_y) = conv_info.stride();
189
190 // Get convolved dimensions
191 unsigned int conv_w = 0;
192 unsigned int conv_h = 0;
193
Georgios Pinitas78c00902018-01-09 17:33:11 +0000194 const unsigned int kernel_width = weights->info()->dimension(0);
195 const unsigned int kernel_height = weights->info()->dimension(1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000196 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000197 conv_info, dilation);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000198
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000199 unsigned int mat_weights_cols = weights->info()->dimension(3);
200 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
201
Georgios Pinitas78c00902018-01-09 17:33:11 +0000202 // _weights_reshaped will be auto configured in the kernel.
203 // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM
204 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000205
Georgios Pinitas78c00902018-01-09 17:33:11 +0000206 weights = &_weights_reshaped;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000207
208 // Create tensor to store im2col reshaped inputs
209 const unsigned int mat_input_cols = mat_weights_rows;
210 const unsigned int mat_input_rows = conv_w * conv_h;
211 TensorShape shape_im2col = input->info()->tensor_shape();
212 shape_im2col.set(0, mat_input_cols);
213 shape_im2col.set(1, mat_input_rows);
214 shape_im2col.set(2, 1);
215 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
216 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position());
217 im2col_reshaped_info.set_quantization_info(input->info()->quantization_info());
218 _im2col_output.allocator()->init(im2col_reshaped_info);
219 _memory_group.manage(&_im2col_output);
220
221 // Create GEMM output tensor
222 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
223 shape_gemm.set(0, mat_weights_cols);
224 shape_gemm.set(1, mat_input_rows);
225 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
226 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
227 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
228 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
229 info_gemm.set_quantization_info(output->info()->quantization_info());
230 _gemm_output.allocator()->init(info_gemm);
231 _memory_group.manage(&_gemm_output);
232
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100233 // Configure and tune im2col
Alex Gilday7da29b62018-03-23 14:16:00 +0000234 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation);
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100235 CLScheduler::get().tune_kernel_static(_im2col_kernel);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000236
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100237 // Configure and tune GEMM
Georgios Pinitas78c00902018-01-09 17:33:11 +0000238 configure_mm(&_im2col_output, weights, &_gemm_output);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000239
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000240 _im2col_output.allocator()->allocate();
241
242 // Configure output stage for quantized case
243 if(_is_quantized)
244 {
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000245 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
246
247 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000248 int output_multiplier, output_shift;
249 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
250 _memory_group.manage(&_tmp_output);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000251 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000252 }
253
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100254 // Configure and tune Col2Im
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000255 _col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, std::make_pair(conv_w, conv_h));
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100256 CLScheduler::get().tune_kernel_static(_col2im_kernel);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000257 if(_is_quantized)
258 {
259 _tmp_output.allocator()->allocate();
260 }
261 _gemm_output.allocator()->allocate();
262
263 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");
264
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000265 //Configure Activation Layer
266 _is_activationlayer_enabled = act_info.enabled();
267
268 if(_is_activationlayer_enabled)
269 {
270 _activationlayer_function.configure(output, nullptr, act_info);
271 }
272
Georgios Pinitas78c00902018-01-09 17:33:11 +0000273 ARM_COMPUTE_UNUSED(weights_info);
274}
275
276Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000277 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000278{
279 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
280 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
281 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
282 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
283 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
284 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2));
285 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
286
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000287 if(act_info.enabled())
288 {
289 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
290 }
291
Georgios Pinitas78c00902018-01-09 17:33:11 +0000292 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
293 const bool append_bias = (biases != nullptr) && (!is_quantized);
294 const unsigned bias_element = (append_bias) ? 1 : 0;
295 const DataType dt = input->data_type();
296
297 // Get convolved dimensions
298 unsigned int conv_w = 0;
299 unsigned int conv_h = 0;
300
301 const unsigned int kernel_width = weights->dimension(0);
302 const unsigned int kernel_height = weights->dimension(1);
303
Alex Gilday7da29b62018-03-23 14:16:00 +0000304 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, conv_info, dilation);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000305
306 unsigned int mat_weights_cols = weights->dimension(3);
307 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + bias_element;
308
Georgios Pinitase0437672018-05-02 14:07:55 +0100309 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr));
Georgios Pinitas78c00902018-01-09 17:33:11 +0000310
311 // Create tensor info for im2col reshaped inputs
312 const unsigned int mat_input_cols = mat_weights_rows;
313 const unsigned int mat_input_rows = conv_w * conv_h;
314 TensorShape shape_im2col = input->tensor_shape();
315 shape_im2col.set(0, mat_input_cols);
316 shape_im2col.set(1, mat_input_rows);
317 shape_im2col.set(2, 1);
318 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->fixed_point_position());
319 im2col_reshaped_info.set_quantization_info(input->quantization_info());
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +0100320 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation));
Georgios Pinitas78c00902018-01-09 17:33:11 +0000321
322 // Create GEMM output tensor
323 TensorShape shape_gemm = im2col_reshaped_info.tensor_shape();
324 shape_gemm.set(0, mat_weights_cols);
325 shape_gemm.set(1, mat_input_rows);
326 const DataType gemm_data_type = is_quantized ? DataType::S32 : dt;
327 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
328 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->fixed_point_position());
329 info_gemm.set_quantization_info(output->quantization_info());
330
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +0100331 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(&im2col_reshaped_info, weights, &info_gemm));
332 TensorInfo tmp_info(shape_gemm, 1, DataType::QASYMM8, input->fixed_point_position());
333 tmp_info.set_quantization_info(output->quantization_info());
Georgios Pinitas78c00902018-01-09 17:33:11 +0000334
Georgios Pinitas78c00902018-01-09 17:33:11 +0000335 if(is_quantized)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000336 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000337 float multiplier = input->quantization_info().scale * weights->quantization_info().scale / output->quantization_info().scale;
338 int output_multiplier, output_shift;
339 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
340 // Validate output stage for quantized case
341 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&info_gemm, biases, &tmp_info, output->quantization_info().offset);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000342 }
Georgios Pinitas78c00902018-01-09 17:33:11 +0000343
344 // Validate Col2Im
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +0100345 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(is_quantized ? &tmp_info : &info_gemm, output, std::make_pair(conv_w, conv_h)));
Georgios Pinitas78c00902018-01-09 17:33:11 +0000346
347 if(biases != nullptr)
348 {
349 if(is_quantized)
350 {
351 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
352 }
353 else
354 {
355 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
356 }
357 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
358 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
359 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
360 }
361
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000362 //Validate Activation Layer
363 if(act_info.enabled())
364 {
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +0100365 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000366 }
367
Georgios Pinitas78c00902018-01-09 17:33:11 +0000368 return Status{};
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000369}
370
371void CLGEMMConvolutionLayer::run()
372{
Georgios Pinitase0437672018-05-02 14:07:55 +0100373 prepare();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000374
375 _memory_group.acquire();
376
377 // Run im2col
378 CLScheduler::get().enqueue(_im2col_kernel);
379
Georgios Pinitas78c00902018-01-09 17:33:11 +0000380 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
381 if(_is_quantized)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000382 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000383 // Run gemmlowp
384 _mm_gemmlowp.run();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000385
Georgios Pinitas78c00902018-01-09 17:33:11 +0000386 // Run output stage
387 _gemmlowp_output_stage.run();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000388 }
389 else
390 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000391 // Run gemm
392 _mm_gemm.run();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000393 }
394
395 // Reshape output matrix
396 CLScheduler::get().enqueue(_col2im_kernel, false);
397
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000398 //Run Activation Layer if enabled
399 if(_is_activationlayer_enabled)
400 {
401 _activationlayer_function.run();
402 }
403
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000404 _memory_group.release();
Georgios Pinitase0437672018-05-02 14:07:55 +0100405}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100406
Georgios Pinitase0437672018-05-02 14:07:55 +0100407void CLGEMMConvolutionLayer::prepare()
408{
409 if(!_is_prepared)
410 {
Michele Di Giorgiob62280a2018-05-31 17:31:05 +0100411 if(!_retain_internal_weights)
412 {
413 // Run weights reshaping and mark as unused
414 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
415 _weights_reshaped.allocator()->allocate();
416 _reshape_weights.run();
417 _original_weights->mark_as_unused();
418 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100419
420 // Run GEMM prepare
421 if(!_is_quantized)
422 {
423 _mm_gemm.prepare();
Michele Di Giorgiob62280a2018-05-31 17:31:05 +0100424 if(!_weights_reshaped.is_used() && !_retain_internal_weights)
Georgios Pinitase0437672018-05-02 14:07:55 +0100425 {
426 _weights_reshaped.allocator()->free();
427 }
428 }
429
430 CLScheduler::get().queue().finish();
431 _is_prepared = true;
432 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000433}