• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-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 #ifndef ARM_COMPUTE_CLPADLAYER_H
25 #define ARM_COMPUTE_CLPADLAYER_H
26 
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/runtime/CL/CLTensor.h"
29 #include "arm_compute/runtime/CL/functions/CLCopy.h"
30 #include "arm_compute/runtime/CL/functions/CLPermute.h"
31 #include "arm_compute/runtime/IFunction.h"
32 
33 namespace arm_compute
34 {
35 class CLCompileContext;
36 class CLPadLayerKernel;
37 class ICLTensor;
38 
39 /** Basic function to pad a tensor. This function calls the following OpenCL functions/kernels:
40  *
41  *  -# @ref CLPadLayerKernel if there is padding to be added
42  *  -# @ref CLCopy otherwise
43  */
44 class CLPadLayer : public IFunction
45 {
46 public:
47     /** Default constructor */
48     CLPadLayer();
49     /** Prevent instances of this class from being copied (As this class contains pointers) */
50     CLPadLayer(const CLPadLayer &) = delete;
51     /** Default move constructor */
52     CLPadLayer(CLPadLayer &&) = default;
53     /** Prevent instances of this class from being copied (As this class contains pointers) */
54     CLPadLayer &operator=(const CLPadLayer &) = delete;
55     /** Default move assignment operator */
56     CLPadLayer &operator=(CLPadLayer &&) = default;
57     /** Default destructor */
58     ~CLPadLayer();
59 
60     /** Initialize the function
61      *
62      * Valid data layouts:
63      * - NHWC
64      * - NCHW
65      *
66      * Valid data type configurations:
67      * |src      |dst       |
68      * |:--------|:---------|
69      * |All      |All       |
70      *
71      * @param[in]  input          Source tensor. Data types supported: All.
72      * @param[out] output         Output tensor. Data type supported: same as @p input
73      * @param[in]  padding        The padding for each spatial dimension of the input tensor. The pair padding[i]
74      *                            specifies the front and the end padding in the i-th dimension.
75      * @param[in]  constant_value (Optional) Constant value to be used for the padding.
76      * @param[in]  mode           (Optional) Controls whether the padding should be filled with @p constant_value using CONSTANT,
77      *                            or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
78      */
79     void configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value = PixelValue(), PaddingMode mode = PaddingMode::CONSTANT);
80     /** Initialize the function
81      *
82      * @param[in]  compile_context The compile context to be used.
83      * @param[in]  input           Source tensor. Data types supported: All.
84      * @param[out] output          Output tensor. Data type supported: same as @p input
85      * @param[in]  padding         The padding for each spatial dimension of the input tensor. The pair padding[i]
86      *                             specifies the front and the end padding in the i-th dimension.
87      * @param[in]  constant_value  (Optional) Constant value to be used for the padding.
88      * @param[in]  mode            (Optional) Controls whether the padding should be filled with @p constant_value using CONSTANT,
89      *                            or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
90      */
91     void configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value = PixelValue(),
92                    PaddingMode mode = PaddingMode::CONSTANT);
93 
94     /**  Static function to check if given info will lead to a valid configuration of @ref CLPadLayer.
95      *
96      * @param[in] input          Source tensor info. Data types supported: All.
97      * @param[in] output         Output tensor info. Data type supported: same as @p input
98      * @param[in] padding        The padding for each spatial dimension of the input tensor. The pair padding[i]
99      *                           specifies the front and the end padding in the i-th dimension.
100      * @param[in] constant_value (Optional) Constant value to be used for the padding
101      * @param[in] mode           (Optional) Controls whether the padding should be filled with @p constant_value using CONSTANT,
102      *                            or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
103      */
104     static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value = PixelValue(), PaddingMode mode = PaddingMode::CONSTANT);
105 
106     // Inherited methods overridden:
107     void run() override;
108 
109 private:
110     void configure_reflect_mode(ICLTensor *input, ICLTensor *output);
111 
112     std::unique_ptr<CLPadLayerKernel> _pad_kernel;
113     CLCopy                            _copy;
114     bool                              _perform_pad;
115 };
116 } // namespace arm_compute
117 #endif /*ARM_COMPUTE_PADLAYER_H */
118