blob: 0ed8f03d64518795c67ff31737595869c4c191fd [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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010025#include "src/core/CL/kernels/CLPadLayerKernel.h"
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010026
ramelg016d891572021-09-29 10:05:09 +010027#include "src/common/utils/Log.h"
28
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010029namespace arm_compute
30{
31CLPadLayer::CLPadLayer()
Georgios Pinitas40f51a62020-11-21 03:04:18 +000032 : _pad_kernel(std::make_unique<CLPadLayerKernel>()),
Sheri Zhang7e20e292021-02-02 11:49:34 +000033 _copy(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010034 _perform_pad(false)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010035{
36}
Manuel Bottini60f39112019-03-18 15:25:15 +000037
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010038CLPadLayer::~CLPadLayer() = default;
39
Usama Arif8cf8c112019-03-14 15:36:54 +000040void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010041{
Manuel Bottini2b84be52020-04-08 10:15:51 +010042 configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
43}
44
45void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
46{
Giorgio Arena205eed82019-08-14 10:13:50 +010047 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
ramelg016d891572021-09-29 10:05:09 +010048 ARM_COMPUTE_LOG_PARAMS(input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000049
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010050 _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Manuel Bottini60f39112019-03-18 15:25:15 +000051 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010052 return info.first > 0 || info.second > 0;
53 });
54
55 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000056 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010057 _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000058 }
59 else
60 {
61 // Copy the input to the whole output if no padding is applied
Sheri Zhang7e20e292021-02-02 11:49:34 +000062 _copy.configure(compile_context, input, output);
Manuel Bottini60f39112019-03-18 15:25:15 +000063 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010064}
Usama Arif8cf8c112019-03-14 15:36:54 +000065Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010066{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010067 bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
68 {
69 return info.first > 0 || info.second > 0;
70 });
Manuel Bottini60f39112019-03-18 15:25:15 +000071
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010072 if(perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000073 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010074 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000075 }
76 else
77 {
Sheri Zhang7e20e292021-02-02 11:49:34 +000078 ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(input, output));
Manuel Bottini60f39112019-03-18 15:25:15 +000079 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010080 return Status{};
81}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010082void CLPadLayer::run()
83{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010084 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000085 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010086 CLScheduler::get().enqueue(*_pad_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000087 }
88 else
89 {
Sheri Zhang7e20e292021-02-02 11:49:34 +000090 _copy.run();
Manuel Bottini60f39112019-03-18 15:25:15 +000091 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010092}
ramelg016d891572021-09-29 10:05:09 +010093} // namespace arm_compute