blob: 7f950bcfb3c8b4ae5dc9f581b881bdb59d4d0d51 [file] [log] [blame]
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2018-2021 Arm Limited.
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLPADLAYER_H
25#define ARM_COMPUTE_CLPADLAYER_H
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010026
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010027#include "arm_compute/core/Error.h"
Manuel Bottini60f39112019-03-18 15:25:15 +000028#include "arm_compute/runtime/CL/CLTensor.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000029#include "arm_compute/runtime/CL/functions/CLCopy.h"
30#include "arm_compute/runtime/CL/functions/CLPermute.h"
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010031#include "arm_compute/runtime/IFunction.h"
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010032
33namespace arm_compute
34{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035class CLCompileContext;
36class CLPadLayerKernel;
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010037class ICLTensor;
38
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010039/** Basic function to pad a tensor. This function calls the following OpenCL functions/kernels:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010040 *
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010041 * -# @ref CLPadLayerKernel if there is padding to be added
Sheri Zhang7e20e292021-02-02 11:49:34 +000042 * -# @ref CLCopy otherwise
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010043 */
44class CLPadLayer : public IFunction
45{
46public:
Giorgio Arena205eed82019-08-14 10:13:50 +010047 /** Default constructor */
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010048 CLPadLayer();
Giorgio Arena205eed82019-08-14 10:13:50 +010049 /** 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;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010057 /** Default destructor */
58 ~CLPadLayer();
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010059
60 /** Initialize the function
61 *
Teresa Charlin62687422021-04-28 10:58:49 +010062 * Valid data layouts:
63 * - NHWC
64 * - NCHW
65 *
66 * Valid data type configurations:
67 * |src |dst |
68 * |:--------|:---------|
69 * |All |All |
70 *
Manuel Bottini8481d832019-12-10 15:28:40 +000071 * @param[in] input Source tensor. Data types supported: All.
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000072 * @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.
Usama Arif8cf8c112019-03-14 15:36:54 +000075 * @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,
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010077 * or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010078 */
Usama Arif8cf8c112019-03-14 15:36:54 +000079 void configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value = PixelValue(), PaddingMode mode = PaddingMode::CONSTANT);
Manuel Bottini2b84be52020-04-08 10:15:51 +010080 /** 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);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010093
94 /** Static function to check if given info will lead to a valid configuration of @ref CLPadLayer.
95 *
Manuel Bottini8481d832019-12-10 15:28:40 +000096 * @param[in] input Source tensor info. Data types supported: All.
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000097 * @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
Usama Arif8cf8c112019-03-14 15:36:54 +0000101 * @param[in] mode (Optional) Controls whether the padding should be filled with @p constant_value using CONSTANT,
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100102 * or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100103 */
Usama Arif8cf8c112019-03-14 15:36:54 +0000104 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value = PixelValue(), PaddingMode mode = PaddingMode::CONSTANT);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100105
106 // Inherited methods overridden:
107 void run() override;
108
109private:
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100110 void configure_reflect_mode(ICLTensor *input, ICLTensor *output);
Manuel Bottini60f39112019-03-18 15:25:15 +0000111
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100112 std::unique_ptr<CLPadLayerKernel> _pad_kernel;
Sheri Zhang7e20e292021-02-02 11:49:34 +0000113 CLCopy _copy;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100114 bool _perform_pad;
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100115};
116} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000117#endif /*ARM_COMPUTE_PADLAYER_H */