blob: c348bfcd0cb1766baf34feaba3f895685f7dd83d [file] [log] [blame]
Michalis Spyrou780db4e2017-11-23 09:49:51 +00001/*
Freddie Liardete92b0452021-04-22 14:55:17 +01002 * Copyright (c) 2017-2021 Arm Limited.
Michalis Spyrou780db4e2017-11-23 09:49:51 +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/CLDeconvolutionLayer.h"
25
Michalis Spyrou780db4e2017-11-23 09:49:51 +000026#include "arm_compute/core/Utils.h"
27#include "arm_compute/core/Validate.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
giuros014a8ec802019-03-18 13:25:05 +000029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010030#include "arm_compute/runtime/CL/CLScheduler.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010031#include "src/core/CL/ICLKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032
ramelg016d891572021-09-29 10:05:09 +010033#include "src/common/utils/Log.h"
34
giuros014a8ec802019-03-18 13:25:05 +000035#include <cmath>
Michalis Spyrou780db4e2017-11-23 09:49:51 +000036#include <memory>
37#include <tuple>
38
39using namespace arm_compute;
40using namespace arm_compute::misc::shape_calculator;
41
giuros014a8ec802019-03-18 13:25:05 +000042CLDeconvolutionLayer::CLDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
43 : _memory_manager(std::move(memory_manager)), _function()
Michalis Spyrou780db4e2017-11-23 09:49:51 +000044{
45}
46
giuros014a8ec802019-03-18 13:25:05 +000047void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info,
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010048 const WeightsInfo &weights_info)
giuros014a8ec802019-03-18 13:25:05 +000049{
Manuel Bottini2b84be52020-04-08 10:15:51 +010050 configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, deconv_info, weights_info);
51}
52
53void CLDeconvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info,
54 const WeightsInfo &weights_info)
55{
giuros014a8ec802019-03-18 13:25:05 +000056 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
ramelg016d891572021-09-29 10:05:09 +010057 ARM_COMPUTE_LOG_PARAMS(input, weights, bias, output, deconv_info, weights_info);
giuros0146a49a02019-04-01 13:50:22 +010058
59 switch(CLDeconvolutionLayer::get_deconvolution_method(input->info(), weights->info(), nullptr, output->info(), deconv_info, weights_info))
60 {
61 case DeconvolutionMethod::DIRECT:
62 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000063 auto f = std::make_unique<CLDirectDeconvolutionLayer>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010064 f->configure(compile_context, input, weights, bias, output, deconv_info, weights_info);
giuros0146a49a02019-04-01 13:50:22 +010065 _function = std::move(f);
66 break;
67 }
68 case DeconvolutionMethod::GEMM:
69 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000070 auto f = std::make_unique<CLGEMMDeconvolutionLayer>(_memory_manager);
Manuel Bottini2b84be52020-04-08 10:15:51 +010071 f->configure(compile_context, input, weights, bias, output, deconv_info);
giuros0146a49a02019-04-01 13:50:22 +010072 _function = std::move(f);
73 break;
74 }
75 default:
76 ARM_COMPUTE_ERROR("Not supported.");
77 break;
78 }
giuros014a8ec802019-03-18 13:25:05 +000079}
80
81Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &deconv_info,
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010082 const WeightsInfo &weights_info)
Michalis Spyrou780db4e2017-11-23 09:49:51 +000083{
84 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
giuros0146a49a02019-04-01 13:50:22 +010085 switch(CLDeconvolutionLayer::get_deconvolution_method(input, weights, bias, output, deconv_info, weights_info))
86 {
87 case DeconvolutionMethod::DIRECT:
88 {
89 // Validate direct convolution layer
90 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectDeconvolutionLayer::validate(input, weights, bias, output, deconv_info, weights_info));
91 break;
92 }
93 case DeconvolutionMethod::GEMM:
94 {
95 // Validate gemm-based convolution layer
96 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMDeconvolutionLayer::validate(input, weights, bias, output, deconv_info));
97 break;
98 }
99 default:
100 ARM_COMPUTE_ERROR("Not supported.");
101 break;
102 }
103
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000104 return Status{};
105}
106
giuros0146a49a02019-04-01 13:50:22 +0100107DeconvolutionMethod CLDeconvolutionLayer::get_deconvolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &deconv_info,
108 const WeightsInfo &weights_info)
109{
110 ARM_COMPUTE_UNUSED(output, bias, weights_info);
111
Freddie Liardete92b0452021-04-22 14:55:17 +0100112 if(is_data_type_quantized_per_channel(weights->data_type()))
113 {
114 return DeconvolutionMethod::DIRECT;
115 }
116
giuros0146a49a02019-04-01 13:50:22 +0100117 const DataLayout data_layout = input->data_layout();
118
119 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
120 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
121
122 if(weights->dimension(idx_w) != deconv_info.stride().first || weights->dimension(idx_h) != deconv_info.stride().second)
123 {
124 return DeconvolutionMethod::DIRECT;
125 }
126
127 return DeconvolutionMethod::GEMM;
128}
129
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000130void CLDeconvolutionLayer::run()
131{
Georgios Pinitas72219332018-06-05 14:56:06 +0100132 prepare();
giuros014a8ec802019-03-18 13:25:05 +0000133 _function->run();
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000134}
Georgios Pinitas72219332018-06-05 14:56:06 +0100135
136void CLDeconvolutionLayer::prepare()
137{
giuros014a8ec802019-03-18 13:25:05 +0000138 _function->prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100139}