xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/functions/CLStridedSlice.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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_CL_STRIDED_SLICE_H
25 #define ARM_COMPUTE_CL_STRIDED_SLICE_H
26 
27 #include "arm_compute/runtime/CL/CLRuntimeContext.h"
28 #include "arm_compute/runtime/CL/ICLOperator.h"
29 #include "arm_compute/runtime/IFunction.h"
30 
31 namespace arm_compute
32 {
33 // Forward Declarations
34 class ICLTensor;
35 
36 /** Basic function to run @ref CLStridedSliceKernel */
37 class CLStridedSlice : public IFunction
38 {
39 public:
40     /** Constructor
41      *
42      * @param[in] ctx Runtime context to be used by the function
43      */
44     CLStridedSlice(CLRuntimeContext *ctx = nullptr);
45     /** Destructor */
46     ~CLStridedSlice();
47     /** Prevent instances of this class from being copied (As this class contains pointers) */
48     CLStridedSlice(const CLStridedSlice &) = delete;
49     /** Default move constructor */
50     CLStridedSlice(CLStridedSlice &&);
51     /** Prevent instances of this class from being copied (As this class contains pointers) */
52     CLStridedSlice &operator=(const CLStridedSlice &) = delete;
53     /** Default move assignment operator */
54     CLStridedSlice &operator=(CLStridedSlice &&);
55     /** Configure kernel
56      *
57      * Valid data layouts:
58      * - All
59      *
60      * Valid data type configurations:
61      * |src            |dst            |
62      * |:--------------|:--------------|
63      * |All            |All            |
64      *
65      * @note Supported tensor rank: up to 4
66      *
67      * @param[in]  input            Source tensor. Data type supported: All.
68      * @param[out] output           Destination tensor. Data type supported: Same as @p input
69      * @param[in]  starts           The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
70      * @param[in]  ends             The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
71      * @param[in]  strides          The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
72      * @param[in]  begin_mask       (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
73      * @param[in]  end_mask         (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
74      * @param[in]  shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
75      *                              A slice of size 1 starting from starts[i] in the dimension must be preserved.
76      */
77     void configure(const ICLTensor *input, ICLTensor *output,
78                    const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
79                    int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
80     /** Configure kernel
81      *
82      * @note Supported tensor rank: up to 4
83      *
84      * @param[in]  compile_context  The compile context to be used.
85      * @param[in]  input            Source tensor. Data type supported: All.
86      * @param[out] output           Destination tensor. Data type supported: Same as @p input
87      * @param[in]  starts           The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
88      * @param[in]  ends             The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
89      * @param[in]  strides          The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
90      * @param[in]  begin_mask       (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
91      * @param[in]  end_mask         (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
92      * @param[in]  shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
93      *                              A slice of size 1 starting from starts[i] in the dimension must be preserved.
94      */
95     void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output,
96                    const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
97                    int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
98 
99     /** Static function to check if given info will lead to a valid configuration of @ref CLStridedSlice
100      *
101      * @note Supported tensor rank: up to 4
102      *
103      * @param[in] input            Source tensor. Data type supported: All.
104      * @param[in] output           Destination tensor. Data type supported: Same as @p input
105      * @param[in] starts           The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
106      * @param[in] ends             The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
107      * @param[in] strides          The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
108      * @param[in] begin_mask       (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
109      * @param[in] end_mask         (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
110      * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
111      *                             A slice of size 1 starting from starts[i] in the dimension must be preserved.
112      */
113     static Status validate(const ITensorInfo *input, const ITensorInfo *output,
114                            const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
115                            int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
116 
117     // Inherited methods overridden:
118     void run() override;
119 
120 private:
121     struct Impl;
122     std::unique_ptr<Impl> _impl;
123 };
124 
125 namespace experimental
126 {
127 /** Basic function to run @ref CLStridedSliceKernel */
128 class CLStridedSlice : public ICLOperator
129 {
130 public:
131     /** Configure kernel
132      *
133      * @note Supported tensor rank: up to 4
134      *
135      * @param[in]  compile_context  The compile context to be used.
136      * @param[in]  input            Source tensor info. Data type supported: All.
137      * @param[out] output           Destination tensor info. Data type supported: Same as @p input
138      * @param[in]  starts           The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
139      * @param[in]  ends             The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
140      * @param[in]  strides          The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
141      * @param[in]  begin_mask       (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
142      * @param[in]  end_mask         (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
143      * @param[in]  shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
144      *                              A slice of size 1 starting from starts[i] in the dimension must be preserved.
145      */
146     void configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output,
147                    const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
148                    int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
149 
150     /** Static function to check if given info will lead to a valid configuration of @ref CLStridedSlice
151      *
152      * @note Supported tensor rank: up to 4
153      *
154      * @param[in] input            Source tensor info. Data type supported: All.
155      * @param[in] output           Destination tensor info. Data type supported: Same as @p input
156      * @param[in] starts           The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
157      * @param[in] ends             The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
158      * @param[in] strides          The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
159      * @param[in] begin_mask       (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
160      * @param[in] end_mask         (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
161      * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
162      *                             A slice of size 1 starting from starts[i] in the dimension must be preserved.
163      */
164     static Status validate(const ITensorInfo *input, const ITensorInfo *output,
165                            const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
166                            int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
167 };
168 } // namespace experimental
169 } // namespace arm_compute
170 #endif /* ARM_COMPUTE_CL_STRIDED_SLICE_H */
171