Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | edafe7f | 2023-05-04 17:39:30 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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/CLConvolutionLayer.h" |
| 25 | |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/KernelDescriptors.h" |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h" |
| 31 | #include "src/core/CL/ICLKernel.h" |
| 32 | #include "src/core/helpers/MemoryHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 33 | #include "src/gpu/cl/operators/ClConv2d.h" |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 34 | |
| 35 | #include "src/common/utils/Log.h" |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 36 | #include "support/Cast.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 40 | using namespace arm_compute::misc::shape_calculator; |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 41 | using namespace arm_compute::experimental; |
| 42 | struct CLConvolutionLayer::Impl |
| 43 | { |
| 44 | MemoryGroup memory_group{}; |
| 45 | std::shared_ptr<IMemoryManager> memory_manager{}; |
| 46 | std::unique_ptr<opencl::IClOperator> op{ nullptr }; |
| 47 | ITensorPack run_pack{}; |
| 48 | ITensorPack prep_pack{}; |
| 49 | WorkspaceData<CLTensor> workspace{}; |
| 50 | experimental::MemoryRequirements aux_mem_req{}; |
| 51 | std::unique_ptr<IFunction> func{ nullptr }; |
| 52 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 54 | CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 55 | : _impl(std::make_unique<Impl>()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 57 | _impl->memory_manager = std::move(memory_manager); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 60 | CLConvolutionLayer::~CLConvolutionLayer() = default; |
| 61 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 62 | void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 63 | const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 64 | { |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 65 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void CLConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
| 69 | const WeightsInfo &weights_info, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 70 | const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 71 | { |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 72 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 73 | 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 Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 74 | enable_fast_math, num_groups)); |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 75 | ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 76 | |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 77 | const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups); |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 78 | |
| 79 | switch(opencl::ClConv2d::get_convolution_method(input->info(), weights->info(), output->info(), conv2d_info, |
| 80 | weights_info, CLScheduler::get().target())) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 81 | { |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 82 | case ConvolutionMethod::WINOGRAD: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 83 | case ConvolutionMethod::DIRECT: |
Gian Marco Iodice | a5cb79f | 2022-12-28 13:53:51 +0000 | [diff] [blame] | 84 | case ConvolutionMethod::INDIRECT: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 85 | case ConvolutionMethod::GEMM: |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 86 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 87 | auto f = std::make_unique<opencl::ClConv2d>(); |
| 88 | f->configure(compile_context, input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv2d_info, weights_info); |
| 89 | _impl->op = std::move(f); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 90 | break; |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 91 | } |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 92 | case ConvolutionMethod::FFT: |
| 93 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 94 | auto f = std::make_unique<CLFFTConvolutionLayer>(_impl->memory_manager); |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 95 | f->configure(compile_context, input, weights, biases, output, conv_info, act_info, enable_fast_math); |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 96 | _impl->func = std::move(f); |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 97 | break; |
| 98 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 99 | default: |
| 100 | ARM_COMPUTE_ERROR("Not supported."); |
| 101 | break; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 102 | } |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 103 | |
| 104 | if(_impl->op) |
| 105 | { |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 106 | _impl->memory_group = MemoryGroup(std::move(_impl->memory_manager)); |
| 107 | _impl->aux_mem_req = _impl->op->workspace(); |
| 108 | _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } }; |
| 109 | _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } }; |
| 110 | _impl->workspace = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack); |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 111 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 112 | } |
| 113 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 114 | Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 115 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 117 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Viet-Hoa Do | edafe7f | 2023-05-04 17:39:30 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!weights->are_values_constant(), "Dynamic weights are not supported"); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 119 | 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"); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 120 | |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 121 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame^] | 122 | const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 124 | switch(opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | { |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 126 | case ConvolutionMethod::WINOGRAD: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 127 | case ConvolutionMethod::DIRECT: |
Gian Marco Iodice | a5cb79f | 2022-12-28 13:53:51 +0000 | [diff] [blame] | 128 | case ConvolutionMethod::INDIRECT: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 129 | case ConvolutionMethod::GEMM: |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 130 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 131 | ARM_COMPUTE_RETURN_ON_ERROR(opencl::ClConv2d::validate(input, weights, biases, output, conv2d_info, weights_info)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 132 | break; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 133 | } |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 134 | case ConvolutionMethod::FFT: |
| 135 | { |
| 136 | // Validate FFT-based convolution layer |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 137 | ARM_COMPUTE_RETURN_ON_ERROR(CLFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)); |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 138 | break; |
| 139 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 140 | default: |
| 141 | ARM_COMPUTE_ERROR("Not supported."); |
| 142 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 145 | return Status{}; |
| 146 | } |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 147 | |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 148 | ConvolutionMethod CLConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 149 | const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation, bool enable_fast_math) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 150 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 151 | const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, 1); |
| 152 | return opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void CLConvolutionLayer::run() |
| 156 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 157 | prepare(); |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 158 | |
| 159 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
| 160 | |
| 161 | if(_impl->func) |
| 162 | { |
| 163 | _impl->func->run(); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | _impl->op->run(_impl->run_pack); |
| 168 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 170 | |
| 171 | void CLConvolutionLayer::prepare() |
| 172 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 173 | if(_impl->func) |
| 174 | { |
| 175 | _impl->func->prepare(); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | _impl->op->prepare(_impl->prep_pack); |
| 180 | |
| 181 | // Release temporary tensors that are only used in prepare stage |
| 182 | release_temporaries(_impl->aux_mem_req, _impl->workspace); |
| 183 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 184 | } |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 185 | } // namespace arm_compute |