blob: 20d7292d38427cc249b2259b029e7ec9e5d6c3d9 [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"
SiCongLid5694c92021-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:
Isabella Gottardif07d28d2018-02-06 14:52:43 +000090 case ConvolutionMethod::GEMM:
Gian Marco20d78482018-01-11 15:10:58 +000091 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +010092 auto f = std::make_unique<opencl::ClConv2d>();
93 f->configure(compile_context, input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv2d_info, weights_info);
94 _impl->op = std::move(f);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000095 break;
Gian Marco20d78482018-01-11 15:10:58 +000096 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010097 case ConvolutionMethod::FFT:
98 {
SiCongLi579ca842021-10-18 09:38:33 +010099 ARM_COMPUTE_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops");
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100100 auto f = std::make_unique<CLFFTConvolutionLayer>(_impl->memory_manager);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000101 f->configure(compile_context, input, weights, biases, output, conv_info, act_info, enable_fast_math);
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100102 _impl->func = std::move(f);
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100103 break;
104 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000105 default:
106 ARM_COMPUTE_ERROR("Not supported.");
107 break;
Chunosov5124be52017-11-22 20:42:13 +0700108 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100109
110 if(_impl->op)
111 {
SiCongLi579ca842021-10-18 09:38:33 +0100112 _impl->memory_group = MemoryGroup(std::move(_impl->memory_manager));
113 _impl->aux_mem_req = _impl->op->workspace();
114 _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
115 size_t post_op_tensor_index = 0;
116 for(const auto &op : post_ops.get_list())
117 {
118 for(auto &tensor : op->arguments())
119 {
120 _impl->run_pack.add_const_tensor(experimental::get_post_op_arg_type(post_op_tensor_index++), *tensor);
121 }
122 }
123 _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } };
124 _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 +0100125 }
Chunosov5124be52017-11-22 20:42:13 +0700126}
127
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000128Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
SiCongLi579ca842021-10-18 09:38:33 +0100129 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 +0100130{
Georgios Pinitas78c00902018-01-09 17:33:11 +0000131 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100132 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 +0700133
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100134 const GPUTarget gpu_target = CLScheduler::get().target();
SiCongLi579ca842021-10-18 09:38:33 +0100135 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100137 switch(opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100139 case ConvolutionMethod::WINOGRAD:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000140 case ConvolutionMethod::DIRECT:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000141 case ConvolutionMethod::GEMM:
Chunosov5124be52017-11-22 20:42:13 +0700142 {
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100143 ARM_COMPUTE_RETURN_ON_ERROR(opencl::ClConv2d::validate(input, weights, biases, output, conv2d_info, weights_info));
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000144 break;
Chunosov5124be52017-11-22 20:42:13 +0700145 }
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100146 case ConvolutionMethod::FFT:
147 {
148 // Validate FFT-based convolution layer
SiCongLi579ca842021-10-18 09:38:33 +0100149 ARM_COMPUTE_RETURN_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops");
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000150 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 +0100151 break;
152 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000153 default:
154 ARM_COMPUTE_ERROR("Not supported.");
155 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 }
157
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000158 return Status{};
159}
Gian Marco Iodice368da832017-07-03 12:33:49 +0100160
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100161ConvolutionMethod 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 +0100162 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 +0000163{
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100164 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, 1);
165 return opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166}
167
168void CLConvolutionLayer::run()
169{
Georgios Pinitase0437672018-05-02 14:07:55 +0100170 prepare();
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100171
172 MemoryGroupResourceScope scope_mg(_impl->memory_group);
173
174 if(_impl->func)
175 {
176 _impl->func->run();
177 }
178 else
179 {
180 _impl->op->run(_impl->run_pack);
181 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182}
Georgios Pinitase0437672018-05-02 14:07:55 +0100183
184void CLConvolutionLayer::prepare()
185{
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100186 if(_impl->func)
187 {
188 _impl->func->prepare();
189 }
190 else
191 {
192 _impl->op->prepare(_impl->prep_pack);
193
194 // Release temporary tensors that are only used in prepare stage
195 release_temporaries(_impl->aux_mem_req, _impl->workspace);
196 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100197}
ramelg016d891572021-09-29 10:05:09 +0100198} // namespace arm_compute