blob: e788ded512ce57f105ffa1d7d5bd208c927cc6df [file] [log] [blame]
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2019-2021 Arm Limited.
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01003 *
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"
25
ramelg016d891572021-09-29 10:05:09 +010026#include "src/common/utils/Log.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027#include "src/core/CL/kernels/CLPadLayerKernel.h"
ramelg016d891572021-09-29 10:05:09 +010028
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010029namespace arm_compute
30{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031CLPadLayer::CLPadLayer() : _pad_kernel(std::make_unique<CLPadLayerKernel>()), _copy(), _perform_pad(false)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010032{
33}
Manuel Bottini60f39112019-03-18 15:25:15 +000034
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035CLPadLayer::~CLPadLayer() = default;
36
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037void CLPadLayer::configure(
38 ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010039{
Manuel Bottini2b84be52020-04-08 10:15:51 +010040 configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
41}
42
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043void CLPadLayer::configure(const CLCompileContext &compile_context,
44 ICLTensor *input,
45 ICLTensor *output,
46 const PaddingList &padding,
47 PixelValue constant_value,
48 PaddingMode mode)
Manuel Bottini2b84be52020-04-08 10:15:51 +010049{
Giorgio Arena205eed82019-08-14 10:13:50 +010050 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
ramelg016d891572021-09-29 10:05:09 +010051 ARM_COMPUTE_LOG_PARAMS(input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000052
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 _perform_pad =
54 std::any_of(padding.begin(), padding.end(), [](PaddingInfo info) { return info.first > 0 || info.second > 0; });
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010055
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 if (_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000057 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010058 _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000059 }
60 else
61 {
62 // Copy the input to the whole output if no padding is applied
Sheri Zhang7e20e292021-02-02 11:49:34 +000063 _copy.configure(compile_context, input, output);
Manuel Bottini60f39112019-03-18 15:25:15 +000064 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010065}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066Status CLPadLayer::validate(const ITensorInfo *input,
67 const ITensorInfo *output,
68 const PaddingList &padding,
69 PixelValue constant_value,
70 PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010071{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 bool perform_pad =
73 std::any_of(padding.begin(), padding.end(), [](PaddingInfo info) { return info.first > 0 || info.second > 0; });
Manuel Bottini60f39112019-03-18 15:25:15 +000074
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 if (perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000076 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010077 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000078 }
79 else
80 {
Sheri Zhang7e20e292021-02-02 11:49:34 +000081 ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(input, output));
Manuel Bottini60f39112019-03-18 15:25:15 +000082 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010083 return Status{};
84}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010085void CLPadLayer::run()
86{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 if (_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000088 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010089 CLScheduler::get().enqueue(*_pad_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000090 }
91 else
92 {
Sheri Zhang7e20e292021-02-02 11:49:34 +000093 _copy.run();
Manuel Bottini60f39112019-03-18 15:25:15 +000094 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010095}
ramelg016d891572021-09-29 10:05:09 +010096} // namespace arm_compute