blob: 19d0badd74f9ed72f557741690636a1d55b3a560 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2018-2020 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
120 stop = (stride > 0) ? utility::clamp(stop, 0, dim_size) : utility::clamp(stop, -1, dim_size - 1);
121
122 return stop;
123}
124
125std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 Coordinates starts,
127 Coordinates ends,
128 Coordinates strides,
129 int32_t begin_mask,
130 int32_t end_mask,
131 int32_t shrink_axis_mask)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000132{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100133 Coordinates starts_abs{};
134 Coordinates ends_abs{};
135 Coordinates final_strides{};
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100136
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000138 {
139 const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
140 starts_abs.set(i, start_i);
141 ends_abs.set(i, calculate_end_on_index(input_shape, i, start_i, ends, strides, end_mask, shrink_axis_mask));
142 final_strides.set(i, calculate_stride_on_index(i, strides));
143 }
144
145 return std::make_tuple(starts_abs, ends_abs, final_strides);
146}
147
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148TensorShape compute_strided_slice_output_shape(TensorShape input_shape,
149 Coordinates starts,
150 Coordinates ends,
151 Coordinates strides,
152 int32_t begin_mask,
153 int32_t end_mask,
154 int32_t shrink_axis_mask,
155 bool return_unshrinked)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000156{
157 unsigned int index = 0;
158
159 TensorShape output_shape;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000161 {
162 const int stride = calculate_stride_on_index(index, strides);
163 const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
164 const int end = calculate_end_on_index(input_shape, i, start, ends, strides, end_mask, shrink_axis_mask);
165 const int range = end - start;
166
167 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 if (return_unshrinked || !is_shrink)
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000169 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 if ((range == 0) || // Zero range
171 (range < 0 && stride >= 0) || // Negative range with positive stride
172 (range > 0 && stride <= 0)) // Positive range with negative stride
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000173 {
174 output_shape.set(index, 0);
175 return output_shape;
176 }
177 else
178 {
179 int dim = range / stride + (range % stride != 0 ? 1 : 0);
180 output_shape.set(index++, dim);
181 }
182 }
183 }
184 return output_shape;
185}
186
187int32_t construct_slice_end_mask(Coordinates ends)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100188{
189 // Create end mask
190 int32_t end_mask = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 for (unsigned int i = 0; i < ends.num_dimensions(); ++i)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100192 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193 if (ends[i] < 0)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100194 {
195 end_mask |= 1 << i;
196 }
197 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100198
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000199 return end_mask;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100200}
201} // namespace tensor_transform
202} // namespace helpers
203} // namespace arm_compute