blob: 212cfdabaa35b85d5cd8ac83dbabe19ff1a9e1ef [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Pablo Marquez Tello553e2412024-04-04 16:35:28 +01002 * Copyright (c) 2018-2020, 2024 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 */
24#include "arm_compute/core/utils/helpers/tensor_transform.h"
25
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010026#include "bit_ops.h"
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000027
Georgios Pinitas77589b52018-08-21 14:41:35 +010028namespace arm_compute
29{
30namespace helpers
31{
32namespace tensor_transform
33{
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000034int calculate_stride_on_index(int index, Coordinates strides)
35{
36 return index >= static_cast<int>(strides.num_dimensions()) ? 1 : strides[index];
37}
38
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039int calculate_start_on_index(
40 TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000041{
42 // Early exit
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 if (index >= static_cast<int>(starts.num_dimensions()))
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000044 {
45 return 0;
46 }
47
48 // Get stride
49 const int stride = calculate_stride_on_index(index, strides);
50
51 // Calculate start
52 int start = starts[index];
53
54 // Reset in case of begin mask present
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 if (arm_compute::helpers::bit_ops::is_bit_set(begin_mask, index))
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000056 {
57 start = stride > 0 ? std::numeric_limits<int>::lowest() : std::numeric_limits<int>::max();
58 }
59
60 // Account negative start points
61 const int dim_size = input_shape[index];
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 if (start < 0)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000063 {
64 start += dim_size;
65 }
66
67 // Final clamp
68 start = utility::clamp(start, 0, dim_size - 1);
69
70 return start;
71}
72
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073int calculate_end_on_index(TensorShape input_shape,
74 int index,
75 int start_on_index,
76 Coordinates ends,
77 Coordinates strides,
78 int32_t end_mask,
79 int32_t shrink_axis_mask)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000080{
81 // Early exit
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 if (index >= static_cast<int>(ends.num_dimensions()))
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000083 {
84 return input_shape[index];
85 }
86
87 const int stride = calculate_stride_on_index(index, strides);
88 const bool shrink_axis = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, index);
89
90 // Calculate start
91 int stop = ends[index];
92
93 // Shrink dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (shrink_axis)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000095 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (start_on_index == std::numeric_limits<int>::max())
Michalis Spyroufae513c2019-10-16 17:41:33 +010097 {
98 stop = start_on_index;
99 }
100 else
101 {
102 stop = start_on_index + 1;
103 }
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000104 }
105
106 // Reset in case of begin mask present
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (arm_compute::helpers::bit_ops::is_bit_set(end_mask, index) && !shrink_axis)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000108 {
109 stop = (stride > 0) ? std::numeric_limits<int>::max() : std::numeric_limits<int>::lowest();
110 }
111
112 // Account negative end points
113 const int dim_size = input_shape[index];
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (stop < 0)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000115 {
116 stop += dim_size;
117 }
118
119 // Final clamp
Pablo Marquez Tello553e2412024-04-04 16:35:28 +0100120 if (stride > 0)
121 stop = utility::clamp(stop, 0, dim_size);
122 else
123 stop = utility::clamp(stop, -1, dim_size - 1);
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000124
125 return stop;
126}
127
128std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 Coordinates starts,
130 Coordinates ends,
131 Coordinates strides,
132 int32_t begin_mask,
133 int32_t end_mask,
134 int32_t shrink_axis_mask)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000135{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100136 Coordinates starts_abs{};
137 Coordinates ends_abs{};
138 Coordinates final_strides{};
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100139
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000141 {
142 const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
143 starts_abs.set(i, start_i);
144 ends_abs.set(i, calculate_end_on_index(input_shape, i, start_i, ends, strides, end_mask, shrink_axis_mask));
145 final_strides.set(i, calculate_stride_on_index(i, strides));
146 }
147
148 return std::make_tuple(starts_abs, ends_abs, final_strides);
149}
150
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151TensorShape compute_strided_slice_output_shape(TensorShape input_shape,
152 Coordinates starts,
153 Coordinates ends,
154 Coordinates strides,
155 int32_t begin_mask,
156 int32_t end_mask,
157 int32_t shrink_axis_mask,
158 bool return_unshrinked)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000159{
160 unsigned int index = 0;
161
162 TensorShape output_shape;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163 for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000164 {
165 const int stride = calculate_stride_on_index(index, strides);
166 const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
167 const int end = calculate_end_on_index(input_shape, i, start, ends, strides, end_mask, shrink_axis_mask);
168 const int range = end - start;
169
170 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171 if (return_unshrinked || !is_shrink)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000172 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 if ((range == 0) || // Zero range
174 (range < 0 && stride >= 0) || // Negative range with positive stride
175 (range > 0 && stride <= 0)) // Positive range with negative stride
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000176 {
177 output_shape.set(index, 0);
178 return output_shape;
179 }
180 else
181 {
182 int dim = range / stride + (range % stride != 0 ? 1 : 0);
183 output_shape.set(index++, dim);
184 }
185 }
186 }
187 return output_shape;
188}
189
190int32_t construct_slice_end_mask(Coordinates ends)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100191{
192 // Create end mask
193 int32_t end_mask = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 for (unsigned int i = 0; i < ends.num_dimensions(); ++i)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100195 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 if (ends[i] < 0)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100197 {
198 end_mask |= 1 << i;
199 }
200 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100201
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000202 return end_mask;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100203}
204} // namespace tensor_transform
205} // namespace helpers
206} // namespace arm_compute