blob: 4aa6725496234eabe67037a08dff5747b0614cb1 [file] [log] [blame]
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +00001/*
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +00002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEPADLAYER_H
25#define ARM_COMPUTE_NEPADLAYER_H
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000026
27#include "arm_compute/runtime/IFunction.h"
Usama Arif8cf8c112019-03-14 15:36:54 +000028#include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h"
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000029#include "arm_compute/runtime/NEON/functions/NECopy.h"
Usama Arif8cf8c112019-03-14 15:36:54 +000030#include "arm_compute/runtime/NEON/functions/NEStridedSlice.h"
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000031#include "arm_compute/runtime/SubTensor.h"
32
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000033#include "arm_compute/core/Types.h"
Usama Arif8cf8c112019-03-14 15:36:54 +000034#include "arm_compute/runtime/Tensor.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010035#include <memory>
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000036
37namespace arm_compute
38{
Michalis Spyrouebcebf12020-10-21 00:04:14 +010039class NEPadLayerKernel;
40
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000041/** Basic function to pad a tensor. This function calls the following functions/kernels:
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000042 *
Manuel Bottini9032ee32019-08-07 17:04:11 +010043 * - For padding mode = PaddingMode::CONSTANT:
44 * -# @ref NEPadLayerKernel
45 * - Otherwise:
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000046 * -# @ref NECopy
Manuel Bottini9032ee32019-08-07 17:04:11 +010047 * -# @ref NEStridedSlice
48 * -# @ref NEConcatenateLayer
49 *
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000050 */
51class NEPadLayer : public IFunction
52{
53public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +010054 /** Default Constructor */
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000055 NEPadLayer();
Michalis Spyrouebcebf12020-10-21 00:04:14 +010056 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 NEPadLayer(const NEPadLayer &) = delete;
58 /** Prevent instances of this class from being copied (As this class contains pointers) */
59 NEPadLayer &operator=(const NEPadLayer &) = delete;
60 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
61 NEPadLayer(NEPadLayer &&) = delete;
62 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
63 NEPadLayer &operator=(NEPadLayer &&) = delete;
64 /** Default destructor */
65 ~NEPadLayer();
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000066 /** Initialize the function
67 *
Teresa Charlin62687422021-04-28 10:58:49 +010068 * Valid data layouts:
69 * - NHWC
70 * - NCHW
71 *
72 * Valid data type configurations:
73 * |src |dst |
74 * |:--------|:---------|
75 * |All |All |
76 *
Georgios Pinitas33843562019-12-10 13:33:18 +000077 * @param[in] input Source tensor. Data types supported: All.
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000078 * @param[out] output Output tensor. Data type supported: same as @p input
79 * @param[in] padding The padding for each spatial dimension of the input tensor. The pair padding[i]
80 * specifies the front and the end padding in the i-th dimension.
81 * @param[in] constant_value (Optional) Constant value to be used for the padding
Usama Arif8cf8c112019-03-14 15:36:54 +000082 * @param[in] mode (Optional) Controls whether the padding should be filled with @p constant_value using CONSTANT,
83 * or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000084 */
Usama Arif8cf8c112019-03-14 15:36:54 +000085 void configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value = PixelValue(), const PaddingMode mode = PaddingMode::CONSTANT);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000086 /** Static function to check if given info will lead to a valid configuration of @ref NEPadLayer.
87 *
Georgios Pinitas33843562019-12-10 13:33:18 +000088 * @param[in] input Source tensor info. Data types supported: All.
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000089 * @param[in] output Output tensor info. Data type supported: same as @p input
90 * @param[in] padding The padding for each spatial dimension of the input tensor. The pair padding[i]
91 * specifies the front and the end padding in the i-th dimension.
92 * @param[in] constant_value (Optional) Constant value to be used for the padding
Usama Arif8cf8c112019-03-14 15:36:54 +000093 * @param[in] mode (Optional) Controls whether the padding should be filled with @p constant_value using CONSTANT,
94 * or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000095 *
96 * @return a status
97 */
Usama Arif8cf8c112019-03-14 15:36:54 +000098 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value = PixelValue(), const PaddingMode mode = PaddingMode::CONSTANT);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000099
100 // Inherited methods overridden:
101 void run() override;
102
103private:
Usama Arif8cf8c112019-03-14 15:36:54 +0000104 /** Configure kernels for when constant padding is used.
105 *
Georgios Pinitas33843562019-12-10 13:33:18 +0000106 * @param[in] input Source tensor. Data types supported: All.
Usama Arif8cf8c112019-03-14 15:36:54 +0000107 * @param[out] output Output tensor. Data type supported: same as @p input
108 * @param[in] padding The padding for each spatial dimension of the input tensor. The pair padding[i]
109 * specifies the front and the end padding in the i-th dimension.
110 * @param[in] constant_value Constant value to be used for the padding
111 */
112 void configure_constant_mode(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value);
113 /** Configure functions for when reflect or symmetric padding is used.
114 *
Georgios Pinitas33843562019-12-10 13:33:18 +0000115 * @param[in] input Source tensor. Data types supported: All.
Usama Arif8cf8c112019-03-14 15:36:54 +0000116 * @param[out] output Output tensor. Data type supported: same as @p input
117 */
118 void configure_reflect_symmetric_mode(ITensor *input, ITensor *output);
119
120private:
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000121 NECopy _copy_function;
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100122 std::unique_ptr<NEPadLayerKernel> _pad_kernel;
123 PaddingMode _mode;
124 PaddingList _padding;
125 uint32_t _num_dimensions;
126 std::vector<NEStridedSlice> _slice_functions;
127 std::vector<NEConcatenateLayer> _concat_functions;
128 std::vector<Tensor> _slice_results;
129 std::vector<Tensor> _concat_results;
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000130};
131} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000132#endif /*ARM_COMPUTE_NEPADLAYER_H */