blob: b295a274bd7aed9bfcb23b30f319ef93a3466c60 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +00002 * Copyright (c) 2017-2021 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
Sheri Zhang06d1efd2021-07-28 11:20:04 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/KernelDescriptors.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010030#include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h"
31#include "src/core/CL/ICLKernel.h"
32#include "src/core/helpers/MemoryHelpers.h"
33#include "src/runtime/gpu/cl/operators/ClConv2d.h"
34#include "support/Cast.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010036namespace arm_compute
37{
Georgios Pinitas78c00902018-01-09 17:33:11 +000038using namespace arm_compute::misc::shape_calculator;
Sheri Zhang06d1efd2021-07-28 11:20:04 +010039using namespace arm_compute::experimental;
40struct CLConvolutionLayer::Impl
41{
42 MemoryGroup memory_group{};
43 std::shared_ptr<IMemoryManager> memory_manager{};
44 std::unique_ptr<opencl::IClOperator> op{ nullptr };
45 ITensorPack run_pack{};
46 ITensorPack prep_pack{};
47 WorkspaceData<CLTensor> workspace{};
48 experimental::MemoryRequirements aux_mem_req{};
49 std::unique_ptr<IFunction> func{ nullptr };
50};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010052CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Sheri Zhang06d1efd2021-07-28 11:20:04 +010053 : _impl(std::make_unique<Impl>())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054{
Sheri Zhang06d1efd2021-07-28 11:20:04 +010055 _impl->memory_manager = std::move(memory_manager);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056}
57
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010058CLConvolutionLayer::~CLConvolutionLayer() = default;
59
Alex Gilday7da29b62018-03-23 14:16:00 +000060void 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 +010061 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Chunosov5124be52017-11-22 20:42:13 +070062{
Manuel Bottini2b84be52020-04-08 10:15:51 +010063 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups);
64}
65
66void CLConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
67 const WeightsInfo &weights_info,
68 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
69{
Isabella Gottardif07d28d2018-02-06 14:52:43 +000070 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010071 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 +010072 enable_fast_math, num_groups));
Isabella Gottardif07d28d2018-02-06 14:52:43 +000073
Sheri Zhang06d1efd2021-07-28 11:20:04 +010074 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups);
75
76 switch(opencl::ClConv2d::get_convolution_method(input->info(), weights->info(), output->info(), conv2d_info,
77 weights_info, CLScheduler::get().target()))
Chunosov5124be52017-11-22 20:42:13 +070078 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010079 case ConvolutionMethod::WINOGRAD:
Isabella Gottardif07d28d2018-02-06 14:52:43 +000080 case ConvolutionMethod::DIRECT:
Isabella Gottardif07d28d2018-02-06 14:52:43 +000081 case ConvolutionMethod::GEMM:
Gian Marco20d78482018-01-11 15:10:58 +000082 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +010083 auto f = std::make_unique<opencl::ClConv2d>();
84 f->configure(compile_context, input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv2d_info, weights_info);
85 _impl->op = std::move(f);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000086 break;
Gian Marco20d78482018-01-11 15:10:58 +000087 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010088 case ConvolutionMethod::FFT:
89 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +010090 auto f = std::make_unique<CLFFTConvolutionLayer>(_impl->memory_manager);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000091 f->configure(compile_context, input, weights, biases, output, conv_info, act_info, enable_fast_math);
Sheri Zhang06d1efd2021-07-28 11:20:04 +010092 _impl->func = std::move(f);
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010093 break;
94 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +000095 default:
96 ARM_COMPUTE_ERROR("Not supported.");
97 break;
Chunosov5124be52017-11-22 20:42:13 +070098 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +010099
100 if(_impl->op)
101 {
102 _impl->memory_group = MemoryGroup(std::move(_impl->memory_manager));
103 _impl->aux_mem_req = _impl->op->workspace();
104 _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
105 _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } };
106 _impl->workspace = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
107 }
Chunosov5124be52017-11-22 20:42:13 +0700108}
109
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000110Status 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 +0100111 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 +0100112{
Georgios Pinitas78c00902018-01-09 17:33:11 +0000113 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100114 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 +0700115
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100116 const GPUTarget gpu_target = CLScheduler::get().target();
117 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100119 switch(opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100121 case ConvolutionMethod::WINOGRAD:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000122 case ConvolutionMethod::DIRECT:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000123 case ConvolutionMethod::GEMM:
Chunosov5124be52017-11-22 20:42:13 +0700124 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100125 ARM_COMPUTE_RETURN_ON_ERROR(opencl::ClConv2d::validate(input, weights, biases, output, conv2d_info, weights_info));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000126 break;
Chunosov5124be52017-11-22 20:42:13 +0700127 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100128 case ConvolutionMethod::FFT:
129 {
130 // Validate FFT-based convolution layer
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000131 ARM_COMPUTE_RETURN_ON_ERROR(CLFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math));
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100132 break;
133 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000134 default:
135 ARM_COMPUTE_ERROR("Not supported.");
136 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 }
138
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000139 return Status{};
140}
Gian Marco Iodice368da832017-07-03 12:33:49 +0100141
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100142ConvolutionMethod 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 +0100143 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 +0000144{
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100145 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, 1);
146 return opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147}
148
149void CLConvolutionLayer::run()
150{
Georgios Pinitase0437672018-05-02 14:07:55 +0100151 prepare();
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100152
153 MemoryGroupResourceScope scope_mg(_impl->memory_group);
154
155 if(_impl->func)
156 {
157 _impl->func->run();
158 }
159 else
160 {
161 _impl->op->run(_impl->run_pack);
162 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163}
Georgios Pinitase0437672018-05-02 14:07:55 +0100164
165void CLConvolutionLayer::prepare()
166{
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100167 if(_impl->func)
168 {
169 _impl->func->prepare();
170 }
171 else
172 {
173 _impl->op->prepare(_impl->prep_pack);
174
175 // Release temporary tensors that are only used in prepare stage
176 release_temporaries(_impl->aux_mem_req, _impl->workspace);
177 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100178}
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100179} // namespace arm_compute