Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +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/CLPadLayer.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 25 | #include "src/core/CL/kernels/CLPadLayerKernel.h" |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 26 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 27 | namespace arm_compute |
| 28 | { |
| 29 | CLPadLayer::CLPadLayer() |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 30 | : _pad_kernel(std::make_unique<CLPadLayerKernel>()), |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 31 | _copy(), |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 32 | _perform_pad(false) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 33 | { |
| 34 | } |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 35 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 36 | CLPadLayer::~CLPadLayer() = default; |
| 37 | |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 38 | void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 39 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 40 | configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode); |
| 41 | } |
| 42 | |
| 43 | void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) |
| 44 | { |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 45 | ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode)); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 46 | |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 47 | _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info) |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 48 | { |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 49 | return info.first > 0 || info.second > 0; |
| 50 | }); |
| 51 | |
| 52 | if(_perform_pad) |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 53 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 54 | _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 55 | } |
| 56 | else |
| 57 | { |
| 58 | // Copy the input to the whole output if no padding is applied |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 59 | _copy.configure(compile_context, input, output); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 60 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 61 | } |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 62 | Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 63 | { |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 64 | bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info) |
| 65 | { |
| 66 | return info.first > 0 || info.second > 0; |
| 67 | }); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 68 | |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 69 | if(perform_pad) |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 70 | { |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode)); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 72 | } |
| 73 | else |
| 74 | { |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(input, output)); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 76 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 77 | return Status{}; |
| 78 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 79 | void CLPadLayer::run() |
| 80 | { |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 81 | if(_perform_pad) |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 82 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 83 | CLScheduler::get().enqueue(*_pad_kernel); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 87 | _copy.run(); |
Manuel Bottini | 60f3911 | 2019-03-18 15:25:15 +0000 | [diff] [blame] | 88 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 89 | } |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 90 | } // namespace arm_compute |