blob: 47a8d5fa8f6756a690aff3a0933221ad5281cac2 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +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/CLConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000031#include "arm_compute/runtime/CL/CLScheduler.h"
32
33#include <cmath>
Kaizen8938bd32017-09-28 14:38:23 +010034#include <memory>
Anthony Barbier871448e2017-03-24 14:54:29 +000035#include <tuple>
36
37using namespace arm_compute;
Anthony Barbier06ea0482018-02-22 15:45:35 +000038using namespace arm_compute::misc::shape_calculator;
Anthony Barbierdbdab852017-06-23 15:42:00 +010039
Kaizen8938bd32017-09-28 14:38:23 +010040CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier06ea0482018-02-22 15:45:35 +000041 : _memory_manager(std::move(memory_manager)), _function()
Anthony Barbierdbdab852017-06-23 15:42:00 +010042{
43}
44
Jenkinsb3a371b2018-05-23 11:36:53 +010045void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
46 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000047{
Anthony Barbier06ea0482018-02-22 15:45:35 +000048 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Jenkinsb3a371b2018-05-23 11:36:53 +010049 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,
50 enable_fast_math));
Anthony Barbier06ea0482018-02-22 15:45:35 +000051
Jenkinsb3a371b2018-05-23 11:36:53 +010052 switch(CLConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info,
53 weights_info, act_info, CLScheduler::get().target(), dilation, enable_fast_math))
Anthony Barbier8140e1e2017-12-14 23:48:46 +000054 {
Jenkinsb3a371b2018-05-23 11:36:53 +010055 case ConvolutionMethod::WINOGRAD:
56 {
57 auto f = arm_compute::support::cpp14::make_unique<CLWinogradConvolutionLayer>(_memory_manager);
58 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
59 _function = std::move(f);
60 break;
61 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000062 case ConvolutionMethod::DIRECT:
Anthony Barbierf45d5a92018-01-24 16:23:15 +000063 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000064 auto f = arm_compute::support::cpp14::make_unique<CLDirectConvolutionLayer>();
Jenkinsb3a371b2018-05-23 11:36:53 +010065 f->configure(input, weights, biases, output, conv_info, act_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +000066 _function = std::move(f);
67 break;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000068 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000069 case ConvolutionMethod::GEMM:
Anthony Barbierf45d5a92018-01-24 16:23:15 +000070 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000071 auto f = arm_compute::support::cpp14::make_unique<CLGEMMConvolutionLayer>(_memory_manager);
Jenkinsb3a371b2018-05-23 11:36:53 +010072 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +000073 _function = std::move(f);
74 break;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000075 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000076 default:
77 ARM_COMPUTE_ERROR("Not supported.");
78 break;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000079 }
80}
81
Anthony Barbier06ea0482018-02-22 15:45:35 +000082Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +010083 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Anthony Barbier871448e2017-03-24 14:54:29 +000084{
Anthony Barbier06ea0482018-02-22 15:45:35 +000085 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000086
Anthony Barbier06ea0482018-02-22 15:45:35 +000087 const GPUTarget gpu_target = CLScheduler::get().target();
Anthony Barbier871448e2017-03-24 14:54:29 +000088
Jenkinsb3a371b2018-05-23 11:36:53 +010089 switch(CLConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, act_info, gpu_target, dilation, enable_fast_math))
Anthony Barbier871448e2017-03-24 14:54:29 +000090 {
Jenkinsb3a371b2018-05-23 11:36:53 +010091 case ConvolutionMethod::WINOGRAD:
92 {
93 //Validate Winograd
94 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math));
95 break;
96 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000097 case ConvolutionMethod::DIRECT:
Anthony Barbier8140e1e2017-12-14 23:48:46 +000098 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000099 // Validate direct convolution layer
Jenkinsb3a371b2018-05-23 11:36:53 +0100100 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000101 break;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000102 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103 case ConvolutionMethod::GEMM:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000105 // Validate gemm-based convolution layer
Jenkinsb3a371b2018-05-23 11:36:53 +0100106 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000107 break;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000108 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000109 default:
110 ARM_COMPUTE_ERROR("Not supported.");
111 break;
Anthony Barbier871448e2017-03-24 14:54:29 +0000112 }
113
Anthony Barbier06ea0482018-02-22 15:45:35 +0000114 return Status{};
115}
Kaizen8938bd32017-09-28 14:38:23 +0100116
Jenkinsb3a371b2018-05-23 11:36:53 +0100117ConvolutionMethod CLConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info,
118 const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation, bool enable_fast_math)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000119{
Jenkinsb3a371b2018-05-23 11:36:53 +0100120 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
121 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
122 ARM_COMPUTE_ERROR_ON_NULLPTR(weights);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000123 ARM_COMPUTE_UNUSED(weights_info);
124 ARM_COMPUTE_UNUSED(gpu_target);
Kaizen8938bd32017-09-28 14:38:23 +0100125
Jenkinsb3a371b2018-05-23 11:36:53 +0100126 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
127
128 if(dilation != Size2D(1U, 1U) || (input->dimension(idx_c) < 16))
129 {
130 return ConvolutionMethod::GEMM;
131 }
132 else
133 {
134 return bool(CLWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
135 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000136}
137
138void CLConvolutionLayer::run()
139{
Jenkinsb3a371b2018-05-23 11:36:53 +0100140 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000141 _function->run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000142}
Jenkinsb3a371b2018-05-23 11:36:53 +0100143
144void CLConvolutionLayer::prepare()
145{
146 _function->prepare();
147}