blob: fb6078cc79a4f4c29de8ab257210f8a068deef44 [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"
25
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010026namespace arm_compute
27{
28CLPadLayer::CLPadLayer()
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010029 : _pad_kernel(), _copy_kernel(), _perform_pad(false)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010030{
31}
Manuel Bottini60f39112019-03-18 15:25:15 +000032
Usama Arif8cf8c112019-03-14 15:36:54 +000033void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010034{
Manuel Bottini2b84be52020-04-08 10:15:51 +010035 configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
36}
37
38void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
39{
Giorgio Arena205eed82019-08-14 10:13:50 +010040 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000041
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010042 _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Manuel Bottini60f39112019-03-18 15:25:15 +000043 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010044 return info.first > 0 || info.second > 0;
45 });
46
47 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000048 {
Manuel Bottini2b84be52020-04-08 10:15:51 +010049 _pad_kernel.configure(compile_context, input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000050 }
51 else
52 {
53 // Copy the input to the whole output if no padding is applied
Manuel Bottini2b84be52020-04-08 10:15:51 +010054 _copy_kernel.configure(compile_context, input, output);
Manuel Bottini60f39112019-03-18 15:25:15 +000055 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010056}
Usama Arif8cf8c112019-03-14 15:36:54 +000057Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010058{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010059 bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
60 {
61 return info.first > 0 || info.second > 0;
62 });
Manuel Bottini60f39112019-03-18 15:25:15 +000063
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010064 if(perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000065 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010066 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000067 }
68 else
69 {
SiCong Li3580c752020-10-14 17:00:56 +010070 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output));
Manuel Bottini60f39112019-03-18 15:25:15 +000071 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010072 return Status{};
73}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010074void CLPadLayer::run()
75{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010076 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000077 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010078 CLScheduler::get().enqueue(_pad_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000079 }
80 else
81 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010082 CLScheduler::get().enqueue(_copy_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000083 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010084}
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010085} // namespace arm_compute