blob: d105c0597cca089ba39140bb7317ca600fbba881 [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
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010027namespace arm_compute
28{
29CLPadLayer::CLPadLayer()
Georgios Pinitas40f51a62020-11-21 03:04:18 +000030 : _pad_kernel(std::make_unique<CLPadLayerKernel>()),
Sheri Zhang7e20e292021-02-02 11:49:34 +000031 _copy(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032 _perform_pad(false)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010033{
34}
Manuel Bottini60f39112019-03-18 15:25:15 +000035
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036CLPadLayer::~CLPadLayer() = default;
37
Usama Arif8cf8c112019-03-14 15:36:54 +000038void CLPadLayer::configure(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
43void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
44{
Giorgio Arena205eed82019-08-14 10:13:50 +010045 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000046
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010047 _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Manuel Bottini60f39112019-03-18 15:25:15 +000048 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010049 return info.first > 0 || info.second > 0;
50 });
51
52 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000053 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010054 _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode);
Manuel Bottini60f39112019-03-18 15:25:15 +000055 }
56 else
57 {
58 // Copy the input to the whole output if no padding is applied
Sheri Zhang7e20e292021-02-02 11:49:34 +000059 _copy.configure(compile_context, input, output);
Manuel Bottini60f39112019-03-18 15:25:15 +000060 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010061}
Usama Arif8cf8c112019-03-14 15:36:54 +000062Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010063{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010064 bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
65 {
66 return info.first > 0 || info.second > 0;
67 });
Manuel Bottini60f39112019-03-18 15:25:15 +000068
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010069 if(perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000070 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010071 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Manuel Bottini60f39112019-03-18 15:25:15 +000072 }
73 else
74 {
Sheri Zhang7e20e292021-02-02 11:49:34 +000075 ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(input, output));
Manuel Bottini60f39112019-03-18 15:25:15 +000076 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010077 return Status{};
78}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010079void CLPadLayer::run()
80{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010081 if(_perform_pad)
Manuel Bottini60f39112019-03-18 15:25:15 +000082 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010083 CLScheduler::get().enqueue(*_pad_kernel);
Manuel Bottini60f39112019-03-18 15:25:15 +000084 }
85 else
86 {
Sheri Zhang7e20e292021-02-02 11:49:34 +000087 _copy.run();
Manuel Bottini60f39112019-03-18 15:25:15 +000088 }
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010089}
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010090} // namespace arm_compute