blob: 643e24d63857500d733fb771b905cc2449f03e25 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco20d78482018-01-11 15:10:58 +00002 * Copyright (c) 2017-2018 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
37using namespace arm_compute;
Georgios Pinitas78c00902018-01-09 17:33:11 +000038using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010040CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000041 : _memory_manager(std::move(memory_manager)), _function()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042{
43}
44
Alex Gilday7da29b62018-03-23 14:16:00 +000045void CLConvolutionLayer::configure(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 +000046 const Size2D &dilation, const ActivationLayerInfo &act_info)
Chunosov5124be52017-11-22 20:42:13 +070047{
Isabella Gottardif07d28d2018-02-06 14:52:43 +000048 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000049 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));
Isabella Gottardif07d28d2018-02-06 14:52:43 +000050
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010051 switch(CLConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000052 weights_info, act_info, CLScheduler::get().target(), dilation))
Chunosov5124be52017-11-22 20:42:13 +070053 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010054 case ConvolutionMethod::WINOGRAD:
55 {
56 auto f = arm_compute::support::cpp14::make_unique<CLWinogradConvolutionLayer>();
57 f->configure(input, weights, biases, output, conv_info);
58 _function = std::move(f);
59 break;
60 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000061 case ConvolutionMethod::DIRECT:
Gian Marco20d78482018-01-11 15:10:58 +000062 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +000063 auto f = arm_compute::support::cpp14::make_unique<CLDirectConvolutionLayer>();
64 f->configure(input, weights, biases, output, conv_info);
65 _function = std::move(f);
66 break;
Gian Marco20d78482018-01-11 15:10:58 +000067 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000068 case ConvolutionMethod::GEMM:
Gian Marco20d78482018-01-11 15:10:58 +000069 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +000070 auto f = arm_compute::support::cpp14::make_unique<CLGEMMConvolutionLayer>(_memory_manager);
Alex Gilday7da29b62018-03-23 14:16:00 +000071 f->configure(input, weights, biases, output, conv_info, weights_info, dilation);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000072 _function = std::move(f);
73 break;
Gian Marco20d78482018-01-11 15:10:58 +000074 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000075 default:
76 ARM_COMPUTE_ERROR("Not supported.");
77 break;
Chunosov5124be52017-11-22 20:42:13 +070078 }
79}
80
Isabella Gottardif07d28d2018-02-06 14:52:43 +000081Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000082 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083{
Georgios Pinitas78c00902018-01-09 17:33:11 +000084 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Chunosov5124be52017-11-22 20:42:13 +070085
Isabella Gottardif07d28d2018-02-06 14:52:43 +000086 //Configure if the parameters match the direct convolution or the gemm-based
87 const GPUTarget gpu_target = CLScheduler::get().target();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010089 switch(CLConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, act_info, gpu_target, dilation))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010091 case ConvolutionMethod::WINOGRAD:
92 {
93 //Validate Winograd
94 CLWinogradConvolutionLayer::validate(input, weights, biases, output, conv_info);
95 break;
96 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000097 case ConvolutionMethod::DIRECT:
Chunosov5124be52017-11-22 20:42:13 +070098 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +000099 // Validate direct convolution layer
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000100 CLDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000101 break;
Chunosov5124be52017-11-22 20:42:13 +0700102 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000103 case ConvolutionMethod::GEMM:
Chunosov5124be52017-11-22 20:42:13 +0700104 {
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000105 // Validate gemm-based convolution layer
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000106 CLGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000107 break;
Chunosov5124be52017-11-22 20:42:13 +0700108 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000109 default:
110 ARM_COMPUTE_ERROR("Not supported.");
111 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 }
113
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000114 return Status{};
115}
Gian Marco Iodice368da832017-07-03 12:33:49 +0100116
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100117ConvolutionMethod CLConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000118 const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000119{
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100120 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
121 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
122 ARM_COMPUTE_ERROR_ON_NULLPTR(weights);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000123 ARM_COMPUTE_UNUSED(output);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000124 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardicac4a2e2018-02-19 16:42:22 +0000125 ARM_COMPUTE_UNUSED(gpu_target);
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100126
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100127 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
128 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
129 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
130
131 if((input->data_type() == DataType::F32) && (input->data_layout() == DataLayout::NCHW) && (input->dimension(idx_c) > 3) && (weights->dimension(idx_w) == 3) && (weights->dimension(idx_h) == 3)
132 && (weights->num_dimensions() <= 4) && (conv_info.stride().first == 1) && (conv_info.stride().second == 1) && (dilation == Size2D(1U, 1U)) && (!act_info.enabled()))
133 {
134 return ConvolutionMethod::WINOGRAD;
135 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000136 return ConvolutionMethod::GEMM;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137}
138
139void CLConvolutionLayer::run()
140{
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000141 _function->run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142}