steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
steniu01 | 27b386c | 2017-07-18 17:37:43 +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/CLDirectConvolutionLayer.h" |
| 25 | |
Georgios Pinitas | 6fdfaa8 | 2017-11-29 14:27:24 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/ICLTensor.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/PixelValue.h" |
| 28 | #include "arm_compute/core/Utils.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 31 | #include "src/core/CL/kernels/CLDirectConvolutionLayerKernel.h" |
| 32 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 33 | |
| 34 | using namespace arm_compute; |
| 35 | |
| 36 | CLDirectConvolutionLayer::CLDirectConvolutionLayer() |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 37 | : _direct_conv_kernel(std::make_unique<CLDirectConvolutionLayerKernel>()), _input_border_handler(std::make_unique<CLFillBorderKernel>()), _activationlayer_function(), |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 38 | _is_activationlayer_enabled(false) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 42 | CLDirectConvolutionLayer::~CLDirectConvolutionLayer() = default; |
| 43 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 44 | void CLDirectConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 45 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 46 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, act_info); |
| 47 | } |
| 48 | |
| 49 | void CLDirectConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 50 | const PadStrideInfo &conv_info, |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 51 | const ActivationLayerInfo &act_info) |
| 52 | { |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 53 | // Set GPU target |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 54 | _direct_conv_kernel->set_target(CLScheduler::get().target()); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 55 | |
| 56 | // Configure direct convolution |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 57 | _direct_conv_kernel->configure(compile_context, input, weights, biases, output, conv_info); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 58 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 59 | // Configure border handler |
Georgios Pinitas | 6fdfaa8 | 2017-11-29 14:27:24 +0000 | [diff] [blame] | 60 | PixelValue &&zero_value(0.f); |
| 61 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 62 | { |
Giorgio Arena | 3c4bf0c | 2020-03-02 09:49:29 +0000 | [diff] [blame] | 63 | zero_value = PixelValue(0, input->info()->data_type(), input->info()->quantization_info()); |
Georgios Pinitas | 6fdfaa8 | 2017-11-29 14:27:24 +0000 | [diff] [blame] | 64 | } |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 65 | _input_border_handler->configure(compile_context, input, _direct_conv_kernel->border_size(), BorderMode::CONSTANT, zero_value); |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 66 | |
| 67 | // Tune kernels |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 68 | CLScheduler::get().tune_kernel_static(*_direct_conv_kernel); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 69 | |
| 70 | _is_activationlayer_enabled = act_info.enabled(); |
| 71 | |
| 72 | //Configure Activation Layer |
| 73 | if(_is_activationlayer_enabled) |
| 74 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 75 | _activationlayer_function.configure(compile_context, output, nullptr, act_info); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 76 | } |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 79 | Status CLDirectConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 80 | const ActivationLayerInfo &act_info) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 81 | { |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerKernel::validate(input, weights, biases, output, conv_info, CLScheduler::get().target())); |
| 83 | if(act_info.enabled()) |
| 84 | { |
| 85 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
| 86 | } |
| 87 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 88 | } |
| 89 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 90 | void CLDirectConvolutionLayer::run() |
| 91 | { |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 92 | // Run border handler |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 93 | CLScheduler::get().enqueue(*_input_border_handler, false); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 94 | |
| 95 | // Run direct convolution |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 96 | CLScheduler::get().enqueue(*_direct_conv_kernel); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 97 | |
| 98 | //Run Activation Layer |
| 99 | if(_is_activationlayer_enabled) |
| 100 | { |
| 101 | _activationlayer_function.run(); |
| 102 | } |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 103 | } |