blob: c271f502e90bfdd5a68b5ece9523ac976b7bb4d7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/CL/CLScheduler.h"
32
33#include <cmath>
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010034#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include <tuple>
36
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010037namespace arm_compute
38{
Georgios Pinitas78c00902018-01-09 17:33:11 +000039using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010041CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000042 : _memory_manager(std::move(memory_manager)), _function()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043{
44}
45
Alex Gilday7da29b62018-03-23 14:16:00 +000046void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010047 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Chunosov5124be52017-11-22 20:42:13 +070048{
Isabella Gottardif07d28d2018-02-06 14:52:43 +000049 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010050 ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010051 enable_fast_math, num_groups));
Isabella Gottardif07d28d2018-02-06 14:52:43 +000052
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010053 switch(CLConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010054 weights_info, act_info, CLScheduler::get().target(), dilation, enable_fast_math))
Chunosov5124be52017-11-22 20:42:13 +070055 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010056 case ConvolutionMethod::WINOGRAD:
57 {
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010058 ARM_COMPUTE_ERROR_ON(num_groups != 1);
Georgios Pinitas82b51482018-04-24 15:14:12 +010059 auto f = arm_compute::support::cpp14::make_unique<CLWinogradConvolutionLayer>(_memory_manager);
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010060 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010061 _function = std::move(f);
62 break;
63 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000064 case ConvolutionMethod::DIRECT:
Gian Marco20d78482018-01-11 15:10:58 +000065 {
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010066 ARM_COMPUTE_ERROR_ON(num_groups != 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000067 auto f = arm_compute::support::cpp14::make_unique<CLDirectConvolutionLayer>();
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010068 f->configure(input, weights, biases, output, conv_info, act_info);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000069 _function = std::move(f);
70 break;
Gian Marco20d78482018-01-11 15:10:58 +000071 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000072 case ConvolutionMethod::GEMM:
Gian Marco20d78482018-01-11 15:10:58 +000073 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +000074 auto f = arm_compute::support::cpp14::make_unique<CLGEMMConvolutionLayer>(_memory_manager);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010075 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info, num_groups);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000076 _function = std::move(f);
77 break;
Gian Marco20d78482018-01-11 15:10:58 +000078 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010079 case ConvolutionMethod::FFT:
80 {
81 auto f = arm_compute::support::cpp14::make_unique<CLFFTConvolutionLayer>(_memory_manager);
82 f->configure(input, weights, biases, output, conv_info, act_info);
83 _function = std::move(f);
84 break;
85 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000086 default:
87 ARM_COMPUTE_ERROR("Not supported.");
88 break;
Chunosov5124be52017-11-22 20:42:13 +070089 }
90}
91
Isabella Gottardif07d28d2018-02-06 14:52:43 +000092Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010093 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
Georgios Pinitas78c00902018-01-09 17:33:11 +000095 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010096 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");
Chunosov5124be52017-11-22 20:42:13 +070097
Isabella Gottardif07d28d2018-02-06 14:52:43 +000098 const GPUTarget gpu_target = CLScheduler::get().target();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100100 switch(CLConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, act_info, gpu_target, dilation, enable_fast_math))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100102 case ConvolutionMethod::WINOGRAD:
103 {
104 //Validate Winograd
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100105 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups != 1, "Grouping (num_groups != 1) with CLWinogradConvolutionLayer is not supported");
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100106 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math));
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100107 break;
108 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000109 case ConvolutionMethod::DIRECT:
Chunosov5124be52017-11-22 20:42:13 +0700110 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000111 // Validate direct convolution layer
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100112 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups != 1, "Grouping (num_groups != 1) with CLDirectConvolutionLayer is not supported");
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100113 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000114 break;
Chunosov5124be52017-11-22 20:42:13 +0700115 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000116 case ConvolutionMethod::GEMM:
Chunosov5124be52017-11-22 20:42:13 +0700117 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000118 // Validate gemm-based convolution layer
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100119 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info, num_groups));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000120 break;
Chunosov5124be52017-11-22 20:42:13 +0700121 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100122 case ConvolutionMethod::FFT:
123 {
124 // Validate FFT-based convolution layer
125 ARM_COMPUTE_RETURN_ON_ERROR(CLFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info));
126 break;
127 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000128 default:
129 ARM_COMPUTE_ERROR("Not supported.");
130 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 }
132
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000133 return Status{};
134}
Gian Marco Iodice368da832017-07-03 12:33:49 +0100135
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100136ConvolutionMethod CLConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100137 const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation, bool enable_fast_math)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000138{
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100139 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
140 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
141 ARM_COMPUTE_ERROR_ON_NULLPTR(weights);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000142 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardicac4a2e2018-02-19 16:42:22 +0000143 ARM_COMPUTE_UNUSED(gpu_target);
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100144
Giorgio Arenae0837712018-06-12 11:30:50 +0100145 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
146 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Gian Marco Iodicea8aef292018-05-14 14:21:39 +0100147 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
148
Giorgio Arenae0837712018-06-12 11:30:50 +0100149 /* Input spatial dims, kernel size, IFM/OFM, conv info*/
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100150 using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo, DataLayout>;
Giorgio Arenae0837712018-06-12 11:30:50 +0100151 using ConfigurationMethod = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
152
153 const std::vector<ConfigurationMethod> known_configs =
154 {
155 // Alexnet
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100156 ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U), DataLayout::NCHW), ConvolutionMethod::DIRECT),
Giorgio Arenae0837712018-06-12 11:30:50 +0100157 // VGG16 / VGG19
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100158 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U), DataLayout::NCHW), ConvolutionMethod::DIRECT),
Giorgio Arenae0837712018-06-12 11:30:50 +0100159 // Mobilenet 224
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100160 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NCHW), ConvolutionMethod::GEMM),
Giorgio Arenae0837712018-06-12 11:30:50 +0100161 // Mobilenet 160
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100162 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NCHW), ConvolutionMethod::GEMM),
163 // Mobilenet 224
164 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NHWC), ConvolutionMethod::GEMM),
165 // Mobilenet 160
166 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NHWC), ConvolutionMethod::GEMM),
Giorgio Arenae0837712018-06-12 11:30:50 +0100167 };
168
169 const auto find_config = [&](ConfigurationMethod c)
170 {
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100171 const ConvolutionConfiguration config = c.first;
172 const PadStrideInfo info = std::get<3>(config);
173 const DataLayout data_layout = std::get<4>(config);
Giorgio Arenae0837712018-06-12 11:30:50 +0100174
175 return std::get<0>(config) == Size2D(input->dimension(idx_w), input->dimension(idx_h)) && std::get<1>(config) == Size2D(weights->dimension(idx_w), weights->dimension(idx_h))
176 && std::get<2>(config) == Size2D(weights->dimension(idx_c), weights->dimension(3)) && info.pad_top() == conv_info.pad_top() && info.pad_right() == conv_info.pad_right()
Gian Marco Iodiceff4bbce2018-07-31 12:04:57 +0100177 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride() && (data_layout == input->data_layout());
Giorgio Arenae0837712018-06-12 11:30:50 +0100178 };
179
180 std::vector<ConfigurationMethod>::const_iterator found;
181 if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
182 {
183 return (*found).second;
184 }
185
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100186 if(dilation != Size2D(1U, 1U))
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100187 {
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100188 return ConvolutionMethod::GEMM;
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100189 }
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100190 else
191 {
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100192 // SRGAN
193 if((input->dimension(idx_h) > 720U) && (output->dimension(idx_h) > 720U) && (weights->dimension(idx_h) == 9) && (conv_info.pad_top() < 3)
194 && (CLDirectConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
195 {
196 return ConvolutionMethod::DIRECT;
197 }
198 if((weights->dimension(idx_h) > 7) && (input->dimension(idx_c) > output->dimension(idx_c)) && (CLFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100199 {
200 return ConvolutionMethod::FFT;
201 }
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100202 if(input->dimension(idx_c) < 16)
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100203 {
204 return ConvolutionMethod::GEMM;
205 }
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100206 return bool(CLWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
207 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208}
209
210void CLConvolutionLayer::run()
211{
Georgios Pinitase0437672018-05-02 14:07:55 +0100212 prepare();
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000213 _function->run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214}
Georgios Pinitase0437672018-05-02 14:07:55 +0100215
216void CLConvolutionLayer::prepare()
217{
218 _function->prepare();
219}
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100220} // namespace arm_compute