blob: 8f36a69866613d3bde8ee99c4077b9be8394d7f8 [file] [log] [blame]
Jenkinsb9abeae2018-11-22 11:58:08 +00001/*
Jenkins0e205f72019-11-28 16:53:35 +00002 * Copyright (c) 2019 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{
35 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
36
Jenkins0e205f72019-11-28 16:53:35 +000037 _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Jenkins4ba87db2019-05-23 17:11:51 +010038 {
Jenkins0e205f72019-11-28 16:53:35 +000039 return info.first > 0 || info.second > 0;
40 });
41
42 if(_perform_pad)
Jenkins4ba87db2019-05-23 17:11:51 +010043 {
Jenkins0e205f72019-11-28 16:53:35 +000044 _pad_kernel.configure(input, output, padding, constant_value, mode);
Jenkins4ba87db2019-05-23 17:11:51 +010045 }
46 else
47 {
48 // Copy the input to the whole output if no padding is applied
49 _copy_kernel.configure(input, output);
50 }
51}
Jenkins4ba87db2019-05-23 17:11:51 +010052Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
53{
Jenkins0e205f72019-11-28 16:53:35 +000054 bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
Jenkins4ba87db2019-05-23 17:11:51 +010055 {
Jenkins0e205f72019-11-28 16:53:35 +000056 return info.first > 0 || info.second > 0;
57 });
Jenkins4ba87db2019-05-23 17:11:51 +010058
Jenkins0e205f72019-11-28 16:53:35 +000059 if(perform_pad)
Jenkins4ba87db2019-05-23 17:11:51 +010060 {
Jenkins0e205f72019-11-28 16:53:35 +000061 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
Jenkins4ba87db2019-05-23 17:11:51 +010062 }
63 else
64 {
Jenkins0e205f72019-11-28 16:53:35 +000065 Window copy_window = Window();
66 copy_window.use_tensor_dimensions(output->tensor_shape());
67 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output, PaddingList(), &copy_window));
Jenkins4ba87db2019-05-23 17:11:51 +010068 }
Jenkinsb9abeae2018-11-22 11:58:08 +000069 return Status{};
70}
Jenkinsb9abeae2018-11-22 11:58:08 +000071void CLPadLayer::run()
72{
Jenkins0e205f72019-11-28 16:53:35 +000073 if(_perform_pad)
Jenkins4ba87db2019-05-23 17:11:51 +010074 {
Jenkins0e205f72019-11-28 16:53:35 +000075 CLScheduler::get().enqueue(_pad_kernel);
Jenkins4ba87db2019-05-23 17:11:51 +010076 }
77 else
78 {
Jenkins0e205f72019-11-28 16:53:35 +000079 CLScheduler::get().enqueue(_copy_kernel);
Jenkins4ba87db2019-05-23 17:11:51 +010080 }
Jenkinsb9abeae2018-11-22 11:58:08 +000081}
Jenkins0e205f72019-11-28 16:53:35 +000082} // namespace arm_compute