blob: 476bf274231c6e3e87915e1379dadcb49f51de9e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Doedafe7f2023-05-04 17:39:30 +01002 * Copyright (c) 2017-2023 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"
SiCongLi31778612021-11-12 17:33:45 +000032#include "src/core/experimental/PostOpUtils.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010033#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/gpu/cl/operators/ClConv2d.h"
ramelg016d891572021-09-29 10:05:09 +010035
36#include "src/common/utils/Log.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010037#include "support/Cast.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010039namespace arm_compute
40{
Georgios Pinitas78c00902018-01-09 17:33:11 +000041using namespace arm_compute::misc::shape_calculator;
Sheri Zhang06d1efd2021-07-28 11:20:04 +010042using namespace arm_compute::experimental;
43struct CLConvolutionLayer::Impl
44{
45 MemoryGroup memory_group{};
46 std::shared_ptr<IMemoryManager> memory_manager{};
47 std::unique_ptr<opencl::IClOperator> op{ nullptr };
48 ITensorPack run_pack{};
49 ITensorPack prep_pack{};
50 WorkspaceData<CLTensor> workspace{};
51 experimental::MemoryRequirements aux_mem_req{};
52 std::unique_ptr<IFunction> func{ nullptr };
53};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010055CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Sheri Zhang06d1efd2021-07-28 11:20:04 +010056 : _impl(std::make_unique<Impl>())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
Sheri Zhang06d1efd2021-07-28 11:20:04 +010058 _impl->memory_manager = std::move(memory_manager);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059}
60
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010061CLConvolutionLayer::~CLConvolutionLayer() = default;
62
Alex Gilday7da29b62018-03-23 14:16:00 +000063void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
SiCongLi579ca842021-10-18 09:38:33 +010064 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
Chunosov5124be52017-11-22 20:42:13 +070065{
SiCongLi579ca842021-10-18 09:38:33 +010066 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
Manuel Bottini2b84be52020-04-08 10:15:51 +010067}
68
69void CLConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
70 const WeightsInfo &weights_info,
SiCongLi579ca842021-10-18 09:38:33 +010071 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
Manuel Bottini2b84be52020-04-08 10:15:51 +010072{
Isabella Gottardif07d28d2018-02-06 14:52:43 +000073 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010074 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 +010075 enable_fast_math, num_groups));
SiCongLi579ca842021-10-18 09:38:33 +010076 ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000077
SiCongLi579ca842021-10-18 09:38:33 +010078 // Convert post op arguments to ITensorInfo
79 auto transformed_post_ops = experimental::transform_post_op_list_arguments<ICLTensor *, ITensorInfo *>(post_ops, [](auto tensor)
80 {
81 return tensor->info();
82 });
83 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups, transformed_post_ops);
Sheri Zhang06d1efd2021-07-28 11:20:04 +010084
85 switch(opencl::ClConv2d::get_convolution_method(input->info(), weights->info(), output->info(), conv2d_info,
86 weights_info, CLScheduler::get().target()))
Chunosov5124be52017-11-22 20:42:13 +070087 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010088 case ConvolutionMethod::WINOGRAD:
Isabella Gottardif07d28d2018-02-06 14:52:43 +000089 case ConvolutionMethod::DIRECT:
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +000090 case ConvolutionMethod::INDIRECT:
Isabella Gottardif07d28d2018-02-06 14:52:43 +000091 case ConvolutionMethod::GEMM:
Gian Marco20d78482018-01-11 15:10:58 +000092 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +010093 auto f = std::make_unique<opencl::ClConv2d>();
94 f->configure(compile_context, input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv2d_info, weights_info);
95 _impl->op = std::move(f);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000096 break;
Gian Marco20d78482018-01-11 15:10:58 +000097 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010098 case ConvolutionMethod::FFT:
99 {
SiCongLi579ca842021-10-18 09:38:33 +0100100 ARM_COMPUTE_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops");
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100101 auto f = std::make_unique<CLFFTConvolutionLayer>(_impl->memory_manager);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000102 f->configure(compile_context, input, weights, biases, output, conv_info, act_info, enable_fast_math);
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100103 _impl->func = std::move(f);
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100104 break;
105 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000106 default:
107 ARM_COMPUTE_ERROR("Not supported.");
108 break;
Chunosov5124be52017-11-22 20:42:13 +0700109 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100110
111 if(_impl->op)
112 {
SiCongLi579ca842021-10-18 09:38:33 +0100113 _impl->memory_group = MemoryGroup(std::move(_impl->memory_manager));
114 _impl->aux_mem_req = _impl->op->workspace();
115 _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
116 size_t post_op_tensor_index = 0;
117 for(const auto &op : post_ops.get_list())
118 {
119 for(auto &tensor : op->arguments())
120 {
121 _impl->run_pack.add_const_tensor(experimental::get_post_op_arg_type(post_op_tensor_index++), *tensor);
122 }
123 }
124 _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } };
125 _impl->workspace = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100126 }
Chunosov5124be52017-11-22 20:42:13 +0700127}
128
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000129Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
SiCongLi579ca842021-10-18 09:38:33 +0100130 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ITensorInfo *> &post_ops)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131{
Georgios Pinitas78c00902018-01-09 17:33:11 +0000132 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Viet-Hoa Doedafe7f2023-05-04 17:39:30 +0100133 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!weights->are_values_constant(), "Dynamic weights are not supported");
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100134 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 +0700135
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100136 const GPUTarget gpu_target = CLScheduler::get().target();
SiCongLi579ca842021-10-18 09:38:33 +0100137 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100139 switch(opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100141 case ConvolutionMethod::WINOGRAD:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000142 case ConvolutionMethod::DIRECT:
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000143 case ConvolutionMethod::INDIRECT:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000144 case ConvolutionMethod::GEMM:
Chunosov5124be52017-11-22 20:42:13 +0700145 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100146 ARM_COMPUTE_RETURN_ON_ERROR(opencl::ClConv2d::validate(input, weights, biases, output, conv2d_info, weights_info));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000147 break;
Chunosov5124be52017-11-22 20:42:13 +0700148 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100149 case ConvolutionMethod::FFT:
150 {
151 // Validate FFT-based convolution layer
SiCongLi579ca842021-10-18 09:38:33 +0100152 ARM_COMPUTE_RETURN_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops");
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000153 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 +0100154 break;
155 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000156 default:
157 ARM_COMPUTE_ERROR("Not supported.");
158 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 }
160
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000161 return Status{};
162}
Gian Marco Iodice368da832017-07-03 12:33:49 +0100163
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100164ConvolutionMethod 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 +0100165 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 +0000166{
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100167 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, 1);
168 return opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169}
170
171void CLConvolutionLayer::run()
172{
Georgios Pinitase0437672018-05-02 14:07:55 +0100173 prepare();
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100174
175 MemoryGroupResourceScope scope_mg(_impl->memory_group);
176
177 if(_impl->func)
178 {
179 _impl->func->run();
180 }
181 else
182 {
183 _impl->op->run(_impl->run_pack);
184 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185}
Georgios Pinitase0437672018-05-02 14:07:55 +0100186
187void CLConvolutionLayer::prepare()
188{
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100189 if(_impl->func)
190 {
191 _impl->func->prepare();
192 }
193 else
194 {
195 _impl->op->prepare(_impl->prep_pack);
196
197 // Release temporary tensors that are only used in prepare stage
198 release_temporaries(_impl->aux_mem_req, _impl->workspace);
199 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100200}
ramelg016d891572021-09-29 10:05:09 +0100201} // namespace arm_compute