blob: ad5bfd8dd21487dedeca57601482e5b1e8d64568 [file] [log] [blame]
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001/*
Georgios Pinitas856f66e2021-04-22 21:13:21 +01002 * Copyright (c) 2017-2021 Arm Limited.
Isabella Gottardif07d28d2018-02-06 14:52:43 +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/CLGEMMConvolutionLayer.h"
25
Manuel Bottinid87aded2021-07-16 10:23:31 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000027#include "arm_compute/core/PixelValue.h"
28#include "arm_compute/core/Size2D.h"
29#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000032#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
33#include "arm_compute/runtime/CL/CLScheduler.h"
SiCongLid5694c92021-11-12 17:33:45 +000034#include "src/core/experimental/PostOpUtils.h"
Manuel Bottinid87aded2021-07-16 10:23:31 +010035#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010036#include "src/gpu/cl/operators/ClGemmConv2d.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "support/Cast.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000038
39#include <cmath>
40#include <memory>
41#include <tuple>
42
Michalis Spyroub27e13a2019-09-27 11:04:27 +010043namespace arm_compute
44{
Georgios Pinitas78c00902018-01-09 17:33:11 +000045using namespace arm_compute::misc::shape_calculator;
Michalis Spyroub27e13a2019-09-27 11:04:27 +010046using namespace arm_compute::utils::cast;
Manuel Bottinid87aded2021-07-16 10:23:31 +010047using namespace arm_compute::experimental;
Isabella Gottardif07d28d2018-02-06 14:52:43 +000048
Manuel Bottinid87aded2021-07-16 10:23:31 +010049struct CLGEMMConvolutionLayer::Impl
Isabella Gottardif07d28d2018-02-06 14:52:43 +000050{
Georgios Pinitas19884632021-08-16 12:38:54 +010051 const ITensor *weights{ nullptr };
52 std::unique_ptr<opencl::ClGemmConv2d> op{ nullptr };
53 ITensorPack run_pack{};
54 ITensorPack prep_pack{};
55 MemoryGroup memory_group{};
56 IWeightsManager *weights_manager{ nullptr };
57 MemoryRequirements aux_mem_req{};
58 WorkspaceData<CLTensor> workspace_tensors{};
59 bool is_prepared{ false };
Manuel Bottinid87aded2021-07-16 10:23:31 +010060};
Isabella Gottardif07d28d2018-02-06 14:52:43 +000061
Michalis Spyroub27e13a2019-09-27 11:04:27 +010062CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Manuel Bottinid87aded2021-07-16 10:23:31 +010063 : _impl(std::make_unique<Impl>())
Isabella Gottardif07d28d2018-02-06 14:52:43 +000064{
Manuel Bottinid87aded2021-07-16 10:23:31 +010065 _impl->memory_group = MemoryGroup(memory_manager);
66 _impl->weights_manager = weights_manager;
Isabella Gottardif07d28d2018-02-06 14:52:43 +000067}
68
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010069CLGEMMConvolutionLayer::~CLGEMMConvolutionLayer() = default;
70
Alex Gilday7da29b62018-03-23 14:16:00 +000071void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
SiCongLi579ca842021-10-18 09:38:33 +010072 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000073{
SiCongLi579ca842021-10-18 09:38:33 +010074 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, num_groups, post_ops);
Manuel Bottini2b84be52020-04-08 10:15:51 +010075}
76
77void CLGEMMConvolutionLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
78 const PadStrideInfo &conv_info,
SiCongLi579ca842021-10-18 09:38:33 +010079 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
Manuel Bottini2b84be52020-04-08 10:15:51 +010080{
Isabella Gottardif07d28d2018-02-06 14:52:43 +000081 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
SiCongLi579ca842021-10-18 09:38:33 +010082 _impl->weights = weights;
83 _impl->op = std::make_unique<opencl::ClGemmConv2d>();
84 // Convert post op arguments to ITensorInfo
85 auto transformed_post_ops = experimental::transform_post_op_list_arguments<ICLTensor *, ITensorInfo *>(post_ops, [](auto tensor)
86 {
87 return tensor->info();
88 });
89 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, false, num_groups, transformed_post_ops);
Manuel Bottinid87aded2021-07-16 10:23:31 +010090 _impl->op->configure(compile_context, input->info(), weights->info(), (biases != nullptr ? biases->info() : nullptr), output->info(), conv2d_info, weights_info);
Georgios Pinitas78c00902018-01-09 17:33:11 +000091
Manuel Bottinid87aded2021-07-16 10:23:31 +010092 _impl->run_pack =
Gian Marco Iodicef3622be2019-07-29 14:27:16 +010093 {
Manuel Bottinid87aded2021-07-16 10:23:31 +010094 { TensorType::ACL_SRC_0, input },
95 { TensorType::ACL_SRC_1, weights },
96 { TensorType::ACL_SRC_2, biases },
97 { TensorType::ACL_DST, output }
98 };
SiCongLi579ca842021-10-18 09:38:33 +010099 // Add post op tensors
100 size_t post_op_tensor_index = 0;
101 for(const auto &op : post_ops.get_list())
102 {
103 for(auto &tensor : op->arguments())
104 {
105 _impl->run_pack.add_const_tensor(experimental::get_post_op_arg_type(post_op_tensor_index++), *tensor);
106 }
107 }
Manuel Bottinid87aded2021-07-16 10:23:31 +0100108 _impl->prep_pack =
Gian Marco Iodicef3622be2019-07-29 14:27:16 +0100109 {
Manuel Bottinid87aded2021-07-16 10:23:31 +0100110 { TensorType::ACL_SRC_1, weights },
111 { TensorType::ACL_SRC_2, biases },
112 };
113 _impl->aux_mem_req = _impl->op->workspace();
114 _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000115}
116
117Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
SiCongLi579ca842021-10-18 09:38:33 +0100118 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups, const experimental::PostOpList<ITensorInfo *> &post_ops)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000119{
SiCongLi579ca842021-10-18 09:38:33 +0100120 const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, false, num_groups, post_ops);
Georgios Pinitas19884632021-08-16 12:38:54 +0100121 return opencl::ClGemmConv2d::validate(input, weights, biases, output, conv2d_info, weights_info);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000122}
123
124void CLGEMMConvolutionLayer::run()
125{
Georgios Pinitase0437672018-05-02 14:07:55 +0100126 prepare();
Manuel Bottinid87aded2021-07-16 10:23:31 +0100127 MemoryGroupResourceScope scope_mg(_impl->memory_group);
128 _impl->op->run(_impl->run_pack);
Georgios Pinitase0437672018-05-02 14:07:55 +0100129}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100130
Georgios Pinitase0437672018-05-02 14:07:55 +0100131void CLGEMMConvolutionLayer::prepare()
132{
Manuel Bottinid87aded2021-07-16 10:23:31 +0100133 if(!_impl->is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100134 {
Manuel Bottinid87aded2021-07-16 10:23:31 +0100135 _impl->op->prepare(_impl->prep_pack);
136 auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
137 _impl->aux_mem_req.end(),
138 [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
139
140 if(has_reshape != std::end(_impl->aux_mem_req))
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100141 {
Manuel Bottinid87aded2021-07-16 10:23:31 +0100142 _impl->weights->mark_as_unused();
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100143 }
144 else
145 {
Manuel Bottinid87aded2021-07-16 10:23:31 +0100146 // Pack the B matrix to be used as the underlying GEMM performs no reshapes
147 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->weights);
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100148 }
Manuel Bottinid87aded2021-07-16 10:23:31 +0100149 release_temporaries(_impl->aux_mem_req, _impl->workspace_tensors);
150 _impl->is_prepared = true;
Georgios Pinitase0437672018-05-02 14:07:55 +0100151 }
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000152}
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100153} // namespace arm_compute