1 /*
2 * Copyright (c) 2017-2020 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/CLDeconvolutionLayer.h"
25
26 #include "arm_compute/core/Utils.h"
27 #include "arm_compute/core/Validate.h"
28 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
29 #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30 #include "arm_compute/runtime/CL/CLScheduler.h"
31 #include "support/MemorySupport.h"
32
33 #include <cmath>
34 #include <memory>
35 #include <tuple>
36
37 using namespace arm_compute;
38 using namespace arm_compute::misc::shape_calculator;
39
CLDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)40 CLDeconvolutionLayer::CLDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
41 : _memory_manager(std::move(memory_manager)), _function()
42 {
43 }
44
configure(ICLTensor * input,ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & deconv_info,const WeightsInfo & weights_info)45 void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info,
46 const WeightsInfo &weights_info)
47 {
48 configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, deconv_info, weights_info);
49 }
50
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & deconv_info,const WeightsInfo & weights_info)51 void CLDeconvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info,
52 const WeightsInfo &weights_info)
53 {
54 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
55
56 switch(CLDeconvolutionLayer::get_deconvolution_method(input->info(), weights->info(), nullptr, output->info(), deconv_info, weights_info))
57 {
58 case DeconvolutionMethod::DIRECT:
59 {
60 auto f = arm_compute::support::cpp14::make_unique<CLDirectDeconvolutionLayer>();
61 f->configure(compile_context, input, weights, bias, output, deconv_info, weights_info);
62 _function = std::move(f);
63 break;
64 }
65 case DeconvolutionMethod::GEMM:
66 {
67 auto f = arm_compute::support::cpp14::make_unique<CLGEMMDeconvolutionLayer>(_memory_manager);
68 f->configure(compile_context, input, weights, bias, output, deconv_info);
69 _function = std::move(f);
70 break;
71 }
72 default:
73 ARM_COMPUTE_ERROR("Not supported.");
74 break;
75 }
76 }
77
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * bias,ITensorInfo * output,const PadStrideInfo & deconv_info,const WeightsInfo & weights_info)78 Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &deconv_info,
79 const WeightsInfo &weights_info)
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
82 switch(CLDeconvolutionLayer::get_deconvolution_method(input, weights, bias, output, deconv_info, weights_info))
83 {
84 case DeconvolutionMethod::DIRECT:
85 {
86 // Validate direct convolution layer
87 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectDeconvolutionLayer::validate(input, weights, bias, output, deconv_info, weights_info));
88 break;
89 }
90 case DeconvolutionMethod::GEMM:
91 {
92 // Validate gemm-based convolution layer
93 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMDeconvolutionLayer::validate(input, weights, bias, output, deconv_info));
94 break;
95 }
96 default:
97 ARM_COMPUTE_ERROR("Not supported.");
98 break;
99 }
100
101 return Status{};
102 }
103
get_deconvolution_method(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * bias,ITensorInfo * output,const PadStrideInfo & deconv_info,const WeightsInfo & weights_info)104 DeconvolutionMethod CLDeconvolutionLayer::get_deconvolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &deconv_info,
105 const WeightsInfo &weights_info)
106 {
107 ARM_COMPUTE_UNUSED(output, bias, weights_info);
108
109 const DataLayout data_layout = input->data_layout();
110
111 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
112 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
113
114 if(weights->dimension(idx_w) != deconv_info.stride().first || weights->dimension(idx_h) != deconv_info.stride().second)
115 {
116 return DeconvolutionMethod::DIRECT;
117 }
118
119 return DeconvolutionMethod::GEMM;
120 }
121
run()122 void CLDeconvolutionLayer::run()
123 {
124 prepare();
125 _function->run();
126 }
127
prepare()128 void CLDeconvolutionLayer::prepare()
129 {
130 _function->prepare();
131 }
132