Compute Library
 21.02
CLPadLayer.cpp
Go to the documentation of this file.
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  */
26 
27 namespace arm_compute
28 {
30  : _pad_kernel(std::make_unique<CLPadLayerKernel>()),
31  _copy(),
32  _perform_pad(false)
33 {
34 }
35 
36 CLPadLayer::~CLPadLayer() = default;
37 
38 void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
39 {
40  configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
41 }
42 
43 void CLPadLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
44 {
45  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
46 
47  _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
48  {
49  return info.first > 0 || info.second > 0;
50  });
51 
52  if(_perform_pad)
53  {
54  _pad_kernel->configure(compile_context, input, output, padding, constant_value, mode);
55  }
56  else
57  {
58  // Copy the input to the whole output if no padding is applied
59  _copy.configure(compile_context, input, output);
60  }
61 }
62 Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
63 {
64  bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info)
65  {
66  return info.first > 0 || info.second > 0;
67  });
68 
69  if(perform_pad)
70  {
71  ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode));
72  }
73  else
74  {
76  }
77  return Status{};
78 }
80 {
81  if(_perform_pad)
82  {
83  CLScheduler::get().enqueue(*_pad_kernel);
84  }
85  else
86  {
87  _copy.run();
88  }
89 }
90 } // namespace arm_compute
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of CLPadLayerKernel.
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:481
static CLScheduler & get()
Access the scheduler singleton.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
void run() override
Run the kernels contained in the function.
Definition: CLCopy.cpp:73
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of CLPadLayer.
Definition: CLPadLayer.cpp:62
Copyright (c) 2017-2021 Arm Limited.
void configure(ICLTensor *input, ICLTensor *output, Window *dst_window=nullptr)
Initialise the function&#39;s source and destination.
Definition: CLCopy.cpp:52
PaddingMode
Padding mode to use for PadLayer.
Definition: Types.h:169
void configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Initialize the function.
Definition: CLPadLayer.cpp:38
Interface for the PadLayer function.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
CLPadLayer()
Default constructor.
Definition: CLPadLayer.cpp:29
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
CLCompileContext class.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, Window *dst_window=nullptr)
Static function to check if given info will lead to a valid configuration of CLCopy.
Definition: CLCopy.cpp:68
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
std::pair< uint32_t, uint32_t > PaddingInfo
Padding information as a pair of unsigned int start/end.
Definition: Types.h:478
void run() override
Run the kernels contained in the function.
Definition: CLPadLayer.cpp:79
~CLPadLayer()
Default destructor.