blob: 7a61fa192a0dc05553a9c650368c35bb6abef5b7 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
Georgios Pinitas77589b52018-08-21 14:41:35 +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_UTILS_HELPERS_TENSOR_TRANSFORM_H
25#define ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H
Georgios Pinitas77589b52018-08-21 14:41:35 +010026
27#include "arm_compute/core/Types.h"
28
29namespace arm_compute
30{
31namespace helpers
32{
33namespace tensor_transform
34{
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000035/** Computes stride of a given index
Georgios Pinitasc1a72452018-08-24 11:25:32 +010036 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000037 * @param[in] index Index of tensor to calculate absolute start position
38 * @param[in] strides Slice strides
Georgios Pinitasc1a72452018-08-24 11:25:32 +010039 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000040 * @return Stride at a given index
Georgios Pinitasc1a72452018-08-24 11:25:32 +010041 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000042int calculate_stride_on_index(int index, Coordinates strides);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010043
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000044/** Computes absolute start position of a given index for a strided slice operation
Georgios Pinitasc1a72452018-08-24 11:25:32 +010045 *
46 * @param[in] input_shape Input tensor shape
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000047 * @param[in] index Index of tensor to calculate absolute start position
Georgios Pinitas77589b52018-08-21 14:41:35 +010048 * @param[in] starts Start coordinates
49 * @param[in] strides Slice strides
Georgios Pinitasc1a72452018-08-24 11:25:32 +010050 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and
Georgios Pinitas77589b52018-08-21 14:41:35 +010051 * the fullest possible range in that dimension is used instead.
52 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000053 * @return Absolute start position of a given index
Georgios Pinitas77589b52018-08-21 14:41:35 +010054 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055int calculate_start_on_index(
56 TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +010057
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000058/** Returns the absolute end position of a given index for a strided slice operation
Georgios Pinitasc1a72452018-08-24 11:25:32 +010059 *
Georgios Pinitas77589b52018-08-21 14:41:35 +010060 * @param[in] input_shape Input tensor shape
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000061 * @param[in] index Index of tensor to calculate absolute start position
62 * @param[in] start_on_index Absolute start coordinate for given index
Georgios Pinitas77589b52018-08-21 14:41:35 +010063 * @param[in] ends End coordinates
64 * @param[in] strides Slice strides
65 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and
66 * the fullest possible range in that dimension is used instead.
67 * @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.
Georgios Pinitasc1a72452018-08-24 11:25:32 +010068 * A slice of size 1 starting from starts[i] in the dimension must be preserved.
Georgios Pinitas77589b52018-08-21 14:41:35 +010069 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000070 * @return Absolute end position of a given index
Georgios Pinitas77589b52018-08-21 14:41:35 +010071 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072int calculate_end_on_index(TensorShape input_shape,
73 int index,
74 int start_on_index,
75 Coordinates ends,
76 Coordinates strides,
77 int32_t end_mask = 0,
78 int32_t shrink_axis_mask = 0);
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000079
80/** Calculate start, end and stride coordinates for a strided slice
Georgios Pinitas77589b52018-08-21 14:41:35 +010081 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000082 * @param[in] input_shape Input tensor shape
83 * @param[in] starts Start coordinates
84 * @param[in] ends End coordinates
85 * @param[in] strides Slice strides
86 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and
87 * the fullest possible range in that dimension is used instead.
88 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and
89 * the fullest possible range in that dimension is used instead.
90 * @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.
91 * A slice of size 1 starting from starts[i] in the dimension must be preserved.
Georgios Pinitas77589b52018-08-21 14:41:35 +010092 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000093 * @return A tuple with <Start,End,Strides>
Georgios Pinitas77589b52018-08-21 14:41:35 +010094 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000095std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 Coordinates starts,
97 Coordinates ends,
98 Coordinates strides,
99 int32_t begin_mask = 0,
100 int32_t end_mask = 0,
101 int32_t shrink_axis_mask = 0);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100102
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100103/** Computes output shape of strided slice
104 *
105 * @warning Starts and ends must be non-negative
106 * @warning Starts, ends and final strides should have the same dimensions as the input shape
Georgios Pinitas77589b52018-08-21 14:41:35 +0100107 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000108 * @param[in] input_shape Input tensor shape
109 * @param[in] starts Absolute start coordinates
110 * @param[in] ends Absolute end coordinates
111 * @param[in] strides Slice strides
112 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and
113 * the fullest possible range in that dimension is used instead.
114 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and
115 * the fullest possible range in that dimension is used instead.
116 * @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.
117 * A slice of size 1 starting from starts[i] in the dimension must be preserved.
118 * @param[in] return_unshrinked (Optional) Returns un-shrinked shape
Georgios Pinitas77589b52018-08-21 14:41:35 +0100119 *
120 * @return The output tensor shape
121 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122TensorShape compute_strided_slice_output_shape(TensorShape input_shape,
123 Coordinates starts,
124 Coordinates ends,
125 Coordinates strides,
126 int32_t begin_mask = 0,
127 int32_t end_mask = 0,
128 int32_t shrink_axis_mask = 0,
129 bool return_unshrinked = false);
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000130
131/** Constructs end mask in case we want to perform a slice operation using the strided slice interface
132 *
133 * @note Ends are inclusive in slice operations that is why construction an end mask is needed
134 *
135 * @param[in] ends End coordinates
136 *
137 * @return End mask
138 */
139int32_t construct_slice_end_mask(Coordinates ends);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140} // namespace tensor_transform
Georgios Pinitas77589b52018-08-21 14:41:35 +0100141} // namespace helpers
142} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000143#endif /* ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H */