blob: 8c5d529117490a634343be9e427da9d2843ee0e8 [file] [log] [blame]
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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/CLCopyKernel.h"
26#include "src/core/CL/kernels/CLPadLayerKernel.h"
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010027
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010028namespace arm_compute
29{
30CLPadLayer::CLPadLayer()
Georgios Pinitas40f51a62020-11-21 03:04:18 +000031 : _pad_kernel(std::make_unique<CLPadLayerKernel>()),
32 _copy_kernel(std::make_unique<CLCopyKernel>()),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010033 _perform_pad(false)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010034{
35}
Manuel Bottini60f39112019-03-18 15:25:15 +000036
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010037CLPadLayer::~CLPadLayer() = default;
38
Usama Arif8cf8c112019-03-14 15:36:54 +000039void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010040{
Manuel Bottini2b84be52020-04-08 10:15:51 +010041 configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
42}
43
44void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
45{
Giorgio Arena205eed82019-08-14 10:13:50 +010046 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000047
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010048 _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Manuel Bottini60f39112019-03-18 15:25:15 +000049 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010050 return info.first > 0 || info.second > 0;
51 });
52
53 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000054 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010055 _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000056 }
57 else
58 {
59 // Copy the input to the whole output if no padding is applied
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010060 _copy_kernel->configure(compile_context, input, output);
Manuel Bottini60f39112019-03-18 15:25:15 +000061 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010062}
Usama Arif8cf8c112019-03-14 15:36:54 +000063Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010064{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010065 bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
66 {
67 return info.first > 0 || info.second > 0;
68 });
Manuel Bottini60f39112019-03-18 15:25:15 +000069
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010070 if(perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000071 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010072 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000073 }
74 else
75 {
SiCong Li3580c752020-10-14 17:00:56 +010076 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output));
Manuel Bottini60f39112019-03-18 15:25:15 +000077 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010078 return Status{};
79}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010080void CLPadLayer::run()
81{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010082 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000083 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010084 CLScheduler::get().enqueue(*_pad_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000085 }
86 else
87 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010088 CLScheduler::get().enqueue(*_copy_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000089 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010090}
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010091} // namespace arm_compute