blob: 078bdbc51fb61447abb39c3f35a847c7ba90b0e5 [file] [log] [blame]
Jenkinsb9abeae2018-11-22 11:58:08 +00001/*
Jenkins6a7771e2020-05-28 11:28:36 +01002 * Copyright (c) 2019-2020 ARM Limited.
Jenkinsb9abeae2018-11-22 11:58:08 +00003 *
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
Jenkinsb9abeae2018-11-22 11:58:08 +000026namespace arm_compute
27{
28CLPadLayer::CLPadLayer()
Jenkins0e205f72019-11-28 16:53:35 +000029 : _pad_kernel(), _copy_kernel(), _perform_pad(false)
Jenkinsb9abeae2018-11-22 11:58:08 +000030{
31}
32
Jenkins4ba87db2019-05-23 17:11:51 +010033void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
34{
Jenkins6a7771e2020-05-28 11:28:36 +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{
Jenkins4ba87db2019-05-23 17:11:51 +010040 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
41
Jenkins0e205f72019-11-28 16:53:35 +000042 _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Jenkins4ba87db2019-05-23 17:11:51 +010043 {
Jenkins0e205f72019-11-28 16:53:35 +000044 return info.first > 0 || info.second > 0;
45 });
46
47 if(_perform_pad)
Jenkins4ba87db2019-05-23 17:11:51 +010048 {
Jenkins6a7771e2020-05-28 11:28:36 +010049 _pad_kernel.configure(compile_context, input, output, padding, constant_value, mode);
Jenkins4ba87db2019-05-23 17:11:51 +010050 }
51 else
52 {
53 // Copy the input to the whole output if no padding is applied
Jenkins6a7771e2020-05-28 11:28:36 +010054 _copy_kernel.configure(compile_context, input, output);
Jenkins4ba87db2019-05-23 17:11:51 +010055 }
56}
Jenkins4ba87db2019-05-23 17:11:51 +010057Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
58{
Jenkins0e205f72019-11-28 16:53:35 +000059 bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Jenkins4ba87db2019-05-23 17:11:51 +010060 {
Jenkins0e205f72019-11-28 16:53:35 +000061 return info.first > 0 || info.second > 0;
62 });
Jenkins4ba87db2019-05-23 17:11:51 +010063
Jenkins0e205f72019-11-28 16:53:35 +000064 if(perform_pad)
Jenkins4ba87db2019-05-23 17:11:51 +010065 {
Jenkins0e205f72019-11-28 16:53:35 +000066 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Jenkins4ba87db2019-05-23 17:11:51 +010067 }
68 else
69 {
Jenkins0e205f72019-11-28 16:53:35 +000070 Window copy_window = Window();
71 copy_window.use_tensor_dimensions(output->tensor_shape());
72 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output, PaddingList(), &copy_window));
Jenkins4ba87db2019-05-23 17:11:51 +010073 }
Jenkinsb9abeae2018-11-22 11:58:08 +000074 return Status{};
75}
Jenkinsb9abeae2018-11-22 11:58:08 +000076void CLPadLayer::run()
77{
Jenkins0e205f72019-11-28 16:53:35 +000078 if(_perform_pad)
Jenkins4ba87db2019-05-23 17:11:51 +010079 {
Jenkins0e205f72019-11-28 16:53:35 +000080 CLScheduler::get().enqueue(_pad_kernel);
Jenkins4ba87db2019-05-23 17:11:51 +010081 }
82 else
83 {
Jenkins0e205f72019-11-28 16:53:35 +000084 CLScheduler::get().enqueue(_copy_kernel);
Jenkins4ba87db2019-05-23 17:11:51 +010085 }
Jenkinsb9abeae2018-11-22 11:58:08 +000086}
Jenkins0e205f72019-11-28 16:53:35 +000087} // namespace arm_compute