blob: 49549a0ad0e8053c1a823a85b38531e448d565bb [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);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010065 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas78c00902018-01-09 17:33:11 +000066 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000067
68 if(biases != nullptr)
69 {
Georgios Pinitas19ea4192018-06-19 13:09:53 +010070 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Georgios Pinitas78c00902018-01-09 17:33:11 +000071 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Georgios Pinitas19ea4192018-06-19 13:09:53 +010073 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Georgios Pinitas78c00902018-01-09 17:33:11 +000074 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000075 }
76
Georgios Pinitas78c00902018-01-09 17:33:11 +000077 if((output != nullptr) && (output->total_size() != 0))
Isabella Gottardif07d28d2018-02-06 14:52:43 +000078 {
Georgios Pinitas78c00902018-01-09 17:33:11 +000079 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(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(),
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010094 _add_bias_kernel(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _append_bias(false), _skip_im2col(false),
95 _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000096{
97}
98
Georgios Pinitas19ea4192018-06-19 13:09:53 +010099void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, int gemm_3d_depth)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000100{
101 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Georgios Pinitas883f4892018-08-03 13:41:33 +0100102 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info(), gemm_3d_depth, _skip_im2col));
Georgios Pinitas78c00902018-01-09 17:33:11 +0000103
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
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100123 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/, gemm_3d_depth,
124 _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000125 }
126}
127
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100128Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000129{
130 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
131
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100132 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000133 if(is_quantized)
134 {
135 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
136 // Extract and negate input and weights offset
137 const QuantizationInfo input_quantization_info = input->quantization_info();
138 const QuantizationInfo weights_quantization_info = weights->quantization_info();
139
140 std::unique_ptr<ITensorInfo> input_qa = input->clone();
141 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
142 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
143 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
144
145 // Perform validation step on GEMMLowp
146 CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info);
147 }
148 else
149 {
150 // Perform validation step on Matrix multiply function
151 CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
152 }
153 return Status{};
154}
155
Alex Gilday7da29b62018-03-23 14:16:00 +0000156void 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 +0000157 const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000158{
159 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000160
161 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
162 weights->info(),
163 biases != nullptr ? biases->info() : nullptr,
164 output->info(),
165 conv_info,
Alex Gilday7da29b62018-03-23 14:16:00 +0000166 weights_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000167 dilation,
168 act_info));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000169
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100170 const DataType data_type = input->info()->data_type();
171 const DataLayout data_layout = input->info()->data_layout();
172 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
173 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100174 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
175
176 const unsigned int kernel_width = weights->info()->dimension(idx_width);
177 const unsigned int kernel_height = weights->info()->dimension(idx_height);
178
Georgios Pinitas72219332018-06-05 14:56:06 +0100179 _is_prepared = weights_info.retain_internal_weights();
180 _original_weights = weights;
181 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100182 _data_layout = data_layout;
Gian Marco Iodiceebbb7f22018-08-02 16:43:24 +0100183 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1) && !_is_quantized;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100184 _append_bias = (biases != nullptr) && (!_is_quantized);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000185
Georgios Pinitas78c00902018-01-09 17:33:11 +0000186 // Set the GPU target for im2col and col2im
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000187 _im2col_kernel.set_target(CLScheduler::get().target());
188 _col2im_kernel.set_target(CLScheduler::get().target());
189
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100190 bool is_nhwc = _data_layout == DataLayout::NHWC;
191 const ICLTensor *gemm_input_to_use = input;
192 ICLTensor *gemm_output_to_use = output;
193 ICLTensor *gemm_output_staged_to_use = output;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000194
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100195 const ICLTensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000196
197 // Get parameters from conv_info
198 unsigned int stride_x = 0;
199 unsigned int stride_y = 0;
200 std::tie(stride_x, stride_y) = conv_info.stride();
201
202 // Get convolved dimensions
203 unsigned int conv_w = 0;
204 unsigned int conv_h = 0;
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100205 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
206 input->info()->dimension(idx_height),
207 kernel_width,
208 kernel_height,
209 conv_info,
210 dilation);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000211
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100212 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000213
Georgios Pinitas78c00902018-01-09 17:33:11 +0000214 // _weights_reshaped will be auto configured in the kernel.
215 // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM
216 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000217
Georgios Pinitas78c00902018-01-09 17:33:11 +0000218 weights = &_weights_reshaped;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000219
220 // Create tensor to store im2col reshaped inputs
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100221 if(!_skip_im2col)
222 {
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100223 _memory_group.manage(&_im2col_output);
224
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100225 // Configure and tune im2col. im2col output shape is auto-initialized
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100226 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100227
228 // Set quantization info
229 _im2col_output.info()->set_quantization_info(input->info()->quantization_info());
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100230 CLScheduler::get().tune_kernel_static(_im2col_kernel);
231
232 // Update GEMM input
233 gemm_input_to_use = &_im2col_output;
234 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100235 else if(_append_bias)
236 {
237 // Configure add bias kernel
238 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
239 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000240
241 // Create GEMM output tensor
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100242 if(!is_nhwc || _is_quantized)
243 {
244 // Calculate GEMM output shape
245 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
246 shape_gemm.set(0, mat_weights_cols);
247 shape_gemm.set(1, conv_w * conv_h);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000248
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100249 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
250 const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type;
251 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100252 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100253 info_gemm.set_quantization_info(output->info()->quantization_info());
254 _gemm_output.allocator()->init(info_gemm);
255 _memory_group.manage(&_gemm_output);
256
257 // Update GEMM output
258 gemm_output_to_use = &_gemm_output;
259 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000260
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100261 // Configure and tune GEMM
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100262 configure_mm(gemm_input_to_use, weights, gemm_output_to_use, (data_layout == DataLayout::NHWC) ? conv_h : 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000263
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100264 if(!_skip_im2col)
265 {
266 _im2col_output.allocator()->allocate();
267 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000268
269 // Configure output stage for quantized case
270 if(_is_quantized)
271 {
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000272 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
273
274 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000275 int output_multiplier, output_shift;
276 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100277
278 _memory_group.manage(&_tmp_output);
279 gemm_output_staged_to_use = &_tmp_output;
280
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100281 _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000282 }
283
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100284 if(!is_nhwc || _is_quantized)
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100285 {
286 // Configure and tune Col2Im
287 _col2im_kernel.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output, std::make_pair(conv_w, conv_h));
288 CLScheduler::get().tune_kernel_static(_col2im_kernel);
289 }
290
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100291 if(!is_nhwc || _is_quantized)
292 {
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100293 _tmp_output.allocator()->allocate();
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100294 _gemm_output.allocator()->allocate();
295 }
296
297 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
298 "Output shape does not match the expected one");
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000299
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000300 //Configure Activation Layer
301 _is_activationlayer_enabled = act_info.enabled();
302
303 if(_is_activationlayer_enabled)
304 {
305 _activationlayer_function.configure(output, nullptr, act_info);
306 }
307
Georgios Pinitas78c00902018-01-09 17:33:11 +0000308 ARM_COMPUTE_UNUSED(weights_info);
309}
310
311Status 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 +0000312 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000313{
314 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
315 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100316 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000317 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100318 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100319 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QASYMM8 && input->data_layout() == DataLayout::NHWC,
320 "NHWC is unsupported for QASYMM8!");
Georgios Pinitas78c00902018-01-09 17:33:11 +0000321
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100322 const DataLayout data_layout = input->data_layout();
323 const DataType data_type = input->data_type();
324 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
325 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
326 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
327 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000328
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100329 const unsigned int kernel_width = weights->dimension(idx_width);
330 const unsigned int kernel_height = weights->dimension(idx_height);
331
332 TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info;
333 const ITensorInfo *gemm_input_to_use = input;
334 const ITensorInfo *gemm_output_to_use = output;
335 const ITensorInfo *gemm_output_staged_to_use = output;
336 const ITensorInfo *weights_to_use = weights;
337
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100338 const bool is_nhwc = data_layout == DataLayout::NHWC;
339 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
340 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1) && !is_quantized;
341 const bool append_bias = (biases != nullptr) && (!is_quantized);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000342
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100343 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel));
344 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000345
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100346 // Validate biases
Georgios Pinitas78c00902018-01-09 17:33:11 +0000347 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 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100357 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Georgios Pinitas78c00902018-01-09 17:33:11 +0000358 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
359 }
360
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100361 if(act_info.enabled())
362 {
363 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
364 }
365
366 // Get convolved dimensions
367 unsigned int conv_w = 0;
368 unsigned int conv_h = 0;
369
370 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
371 input->dimension(idx_height),
372 kernel_width,
373 kernel_height,
374 conv_info,
375 dilation);
376
377 unsigned int mat_weights_cols = weights->dimension(idx_kernels);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100378
379 // Output tensor auto inizialitation if not yet initialized
380 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr));
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100381 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, append_bias), 1, data_type);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100382 weights_to_use = &weights_reshaped_info;
383
384 if(!skip_im2col)
385 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100386 const Size2D kernel_dims(kernel_width, kernel_height);
387
388 // Output tensor auto initialization if not yet initialized
389 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, true);
390
391 auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape));
392
393 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation));
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100394 gemm_input_to_use = &im2col_reshaped_info;
395 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100396 else if(append_bias)
397 {
398 // Validate add bias kernel
399 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
400 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100401
402 // Create GEMM output tensor
403 if(!is_nhwc || is_quantized)
404 {
405 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
406 shape_gemm.set(0, mat_weights_cols);
407 shape_gemm.set(1, conv_w * conv_h);
408 const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type;
409 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100410 info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100411 info_gemm.set_quantization_info(output->quantization_info());
412 gemm_output_to_use = &info_gemm;
413 }
414
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100415 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, gemm_output_to_use, (data_layout == DataLayout::NHWC) ? conv_h : 1, skip_im2col));
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100416
417 if(is_quantized)
418 {
419 float multiplier = input->quantization_info().scale * weights_to_use->quantization_info().scale / output->quantization_info().scale;
420 int output_multiplier, output_shift;
421 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100422
423 tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8);
424 tmp_info.set_quantization_info(output->quantization_info());
425 gemm_output_staged_to_use = &tmp_info;
426
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100427 // Validate output stage for quantized case
428 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, output->quantization_info().offset);
429 }
430
431 // Validate Col2Im
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100432 if(!is_nhwc || is_quantized)
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100433 {
434 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use,
435 output,
436 std::make_pair(conv_w, conv_h)));
437 }
438
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000439 //Validate Activation Layer
440 if(act_info.enabled())
441 {
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +0100442 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000443 }
444
Georgios Pinitas78c00902018-01-09 17:33:11 +0000445 return Status{};
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000446}
447
448void CLGEMMConvolutionLayer::run()
449{
Georgios Pinitase0437672018-05-02 14:07:55 +0100450 prepare();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000451
452 _memory_group.acquire();
453
454 // Run im2col
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100455 if(!_skip_im2col)
456 {
457 CLScheduler::get().enqueue(_im2col_kernel);
458 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000459
Georgios Pinitas78c00902018-01-09 17:33:11 +0000460 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
461 if(_is_quantized)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000462 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000463 // Run gemmlowp
464 _mm_gemmlowp.run();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000465
Georgios Pinitas78c00902018-01-09 17:33:11 +0000466 // Run output stage
467 _gemmlowp_output_stage.run();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000468 }
469 else
470 {
Georgios Pinitas78c00902018-01-09 17:33:11 +0000471 // Run gemm
472 _mm_gemm.run();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000473 }
474
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100475 if(_skip_im2col && _append_bias)
476 {
477 CLScheduler::get().enqueue(_add_bias_kernel);
478 }
479
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000480 // Reshape output matrix
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100481 if(_data_layout == DataLayout::NCHW || _is_quantized)
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100482 {
483 CLScheduler::get().enqueue(_col2im_kernel, false);
484 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000485
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000486 //Run Activation Layer if enabled
487 if(_is_activationlayer_enabled)
488 {
489 _activationlayer_function.run();
490 }
491
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000492 _memory_group.release();
Georgios Pinitase0437672018-05-02 14:07:55 +0100493}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100494
Georgios Pinitase0437672018-05-02 14:07:55 +0100495void CLGEMMConvolutionLayer::prepare()
496{
497 if(!_is_prepared)
498 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100499 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitase0437672018-05-02 14:07:55 +0100500
Georgios Pinitas72219332018-06-05 14:56:06 +0100501 // Run weights reshaping and mark original weights tensor as unused
502 _weights_reshaped.allocator()->allocate();
503 _reshape_weights.run();
504 _original_weights->mark_as_unused();
505
506 // Prepare GEMM
507 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
508 if(!_weights_reshaped.is_used())
Georgios Pinitase0437672018-05-02 14:07:55 +0100509 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100510 _weights_reshaped.allocator()->free();
Georgios Pinitase0437672018-05-02 14:07:55 +0100511 }
512
513 CLScheduler::get().queue().finish();
514 _is_prepared = true;
515 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000516}