Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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" |
SiCongLi | d5694c9 | 2021-11-12 17:33:45 +0000 | [diff] [blame] | 32 | #include "src/core/experimental/PostOpUtils.h" |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/MemoryHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 34 | #include "src/gpu/cl/operators/ClConv2d.h" |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 35 | |
| 36 | #include "src/common/utils/Log.h" |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 37 | #include "support/Cast.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 41 | using namespace arm_compute::misc::shape_calculator; |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 42 | using namespace arm_compute::experimental; |
| 43 | struct 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 55 | CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 56 | : _impl(std::make_unique<Impl>()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 58 | _impl->memory_manager = std::move(memory_manager); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 61 | CLConvolutionLayer::~CLConvolutionLayer() = default; |
| 62 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 63 | void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 64 | const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 65 | { |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 66 | 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 Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void 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, |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 71 | const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 72 | { |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 74 | 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] | 75 | enable_fast_math, num_groups)); |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups, post_ops); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 77 | |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 78 | // 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 Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 84 | |
| 85 | switch(opencl::ClConv2d::get_convolution_method(input->info(), weights->info(), output->info(), conv2d_info, |
| 86 | weights_info, CLScheduler::get().target())) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 87 | { |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 88 | case ConvolutionMethod::WINOGRAD: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 89 | case ConvolutionMethod::DIRECT: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 90 | case ConvolutionMethod::GEMM: |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 91 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 92 | 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 Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 95 | break; |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 96 | } |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 97 | case ConvolutionMethod::FFT: |
| 98 | { |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops"); |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 100 | auto f = std::make_unique<CLFFTConvolutionLayer>(_impl->memory_manager); |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 101 | 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] | 102 | _impl->func = std::move(f); |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 103 | break; |
| 104 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 105 | default: |
| 106 | ARM_COMPUTE_ERROR("Not supported."); |
| 107 | break; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 108 | } |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 109 | |
| 110 | if(_impl->op) |
| 111 | { |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 112 | _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 Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 125 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 126 | } |
| 127 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 128 | Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 129 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 131 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 132 | 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] | 133 | |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 134 | const GPUTarget gpu_target = CLScheduler::get().target(); |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 135 | const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups, post_ops); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 137 | 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] | 138 | { |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 139 | case ConvolutionMethod::WINOGRAD: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 140 | case ConvolutionMethod::DIRECT: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 141 | case ConvolutionMethod::GEMM: |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 142 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 143 | 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] | 144 | break; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 145 | } |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 146 | case ConvolutionMethod::FFT: |
| 147 | { |
| 148 | // Validate FFT-based convolution layer |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 149 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops"); |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 150 | 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] | 151 | break; |
| 152 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 153 | default: |
| 154 | ARM_COMPUTE_ERROR("Not supported."); |
| 155 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 158 | return Status{}; |
| 159 | } |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 160 | |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 161 | 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] | 162 | 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] | 163 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 164 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void CLConvolutionLayer::run() |
| 169 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 170 | prepare(); |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 171 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 183 | |
| 184 | void CLConvolutionLayer::prepare() |
| 185 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 186 | 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 Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 197 | } |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 198 | } // namespace arm_compute |