• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
3  *
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 #include "src/core/CL/kernels/CLPadLayerKernel.h"
26 
27 #include "src/common/utils/Log.h"
28 
29 namespace arm_compute
30 {
CLPadLayer()31 CLPadLayer::CLPadLayer()
32     : _pad_kernel(std::make_unique<CLPadLayerKernel>()),
33       _copy(),
34       _perform_pad(false)
35 {
36 }
37 
38 CLPadLayer::~CLPadLayer() = default;
39 
configure(ICLTensor * input,ICLTensor * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)40 void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
41 {
42     configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
43 }
44 
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)45 void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
46 {
47     ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
48     ARM_COMPUTE_LOG_PARAMS(input, output, padding, constant_value, mode);
49 
50     _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
51     {
52         return info.first > 0 || info.second > 0;
53     });
54 
55     if(_perform_pad)
56     {
57         _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode);
58     }
59     else
60     {
61         // Copy the input to the whole output if no padding is applied
62         _copy.configure(compile_context, input, output);
63     }
64 }
validate(const ITensorInfo * input,const ITensorInfo * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)65 Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
66 {
67     bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
68     {
69         return info.first > 0 || info.second > 0;
70     });
71 
72     if(perform_pad)
73     {
74         ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
75     }
76     else
77     {
78         ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(input, output));
79     }
80     return Status{};
81 }
run()82 void CLPadLayer::run()
83 {
84     if(_perform_pad)
85     {
86         CLScheduler::get().enqueue(*_pad_kernel);
87     }
88     else
89     {
90         _copy.run();
91     }
92 }
93 } // namespace arm_compute
94