Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 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/CLGEMMConvolutionLayer.h" |
| 25 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 27 | #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 Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/MemoryHelpers.h" |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame^] | 35 | #include "src/runtime/gpu/cl/operators/ClGemmConv2d.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 36 | #include "support/Cast.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 37 | |
| 38 | #include <cmath> |
| 39 | #include <memory> |
| 40 | #include <tuple> |
| 41 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 44 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 45 | using namespace arm_compute::utils::cast; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 46 | using namespace arm_compute::experimental; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 47 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 48 | struct CLGEMMConvolutionLayer::Impl |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 49 | { |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame^] | 50 | const ITensor *weights{ nullptr }; |
| 51 | std::unique_ptr<opencl::ClGemmConv2d> op{ nullptr }; |
| 52 | ITensorPack run_pack{}; |
| 53 | ITensorPack prep_pack{}; |
| 54 | MemoryGroup memory_group{}; |
| 55 | IWeightsManager *weights_manager{ nullptr }; |
| 56 | MemoryRequirements aux_mem_req{}; |
| 57 | WorkspaceData<CLTensor> workspace_tensors{}; |
| 58 | bool is_prepared{ false }; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 59 | }; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 60 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 61 | CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 62 | : _impl(std::make_unique<Impl>()) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 63 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 64 | _impl->memory_group = MemoryGroup(memory_manager); |
| 65 | _impl->weights_manager = weights_manager; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 68 | CLGEMMConvolutionLayer::~CLGEMMConvolutionLayer() = default; |
| 69 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 70 | void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 71 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 72 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 73 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, num_groups); |
| 74 | } |
| 75 | |
| 76 | void CLGEMMConvolutionLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 77 | const PadStrideInfo &conv_info, |
| 78 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
| 79 | { |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 80 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 81 | _impl->weights = weights; |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame^] | 82 | _impl->op = std::make_unique<opencl::ClGemmConv2d>(); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 83 | const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, false, num_groups); |
| 84 | _impl->op->configure(compile_context, input->info(), weights->info(), (biases != nullptr ? biases->info() : nullptr), output->info(), conv2d_info, weights_info); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 85 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 86 | _impl->run_pack = |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 87 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 88 | { TensorType::ACL_SRC_0, input }, |
| 89 | { TensorType::ACL_SRC_1, weights }, |
| 90 | { TensorType::ACL_SRC_2, biases }, |
| 91 | { TensorType::ACL_DST, output } |
| 92 | }; |
| 93 | _impl->prep_pack = |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 94 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 95 | { TensorType::ACL_SRC_1, weights }, |
| 96 | { TensorType::ACL_SRC_2, biases }, |
| 97 | }; |
| 98 | _impl->aux_mem_req = _impl->op->workspace(); |
| 99 | _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 103 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 104 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 105 | const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, false, num_groups); |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame^] | 106 | return opencl::ClGemmConv2d::validate(input, weights, biases, output, conv2d_info, weights_info); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void CLGEMMConvolutionLayer::run() |
| 110 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 111 | prepare(); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 112 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
| 113 | _impl->op->run(_impl->run_pack); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 114 | } |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 115 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 116 | void CLGEMMConvolutionLayer::prepare() |
| 117 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 118 | if(!_impl->is_prepared) |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 119 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 120 | _impl->op->prepare(_impl->prep_pack); |
| 121 | auto has_reshape = std::find_if(_impl->aux_mem_req.begin(), |
| 122 | _impl->aux_mem_req.end(), |
| 123 | [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; }); |
| 124 | |
| 125 | if(has_reshape != std::end(_impl->aux_mem_req)) |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 126 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 127 | _impl->weights->mark_as_unused(); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 131 | // Pack the B matrix to be used as the underlying GEMM performs no reshapes |
| 132 | _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->weights); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 133 | } |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 134 | release_temporaries(_impl->aux_mem_req, _impl->workspace_tensors); |
| 135 | _impl->is_prepared = true; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 136 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 137 | } |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 138 | } // namespace arm_compute |