blob: 7c56390fedebd59ca4d5a3b4a26566db3eaf872c [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Michalis Spyrou299fdd32019-05-01 13:03:59 +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 */
24#include "arm_compute/core/utils/helpers/tensor_transform.h"
25
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000026#include "arm_compute/core/utils/helpers/bit_ops.h"
27
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
39int calculate_start_on_index(TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask)
40{
41 // Early exit
42 if(index >= static_cast<int>(starts.num_dimensions()))
43 {
44 return 0;
45 }
46
47 // Get stride
48 const int stride = calculate_stride_on_index(index, strides);
49
50 // Calculate start
51 int start = starts[index];
52
53 // Reset in case of begin mask present
54 if(arm_compute::helpers::bit_ops::is_bit_set(begin_mask, index))
55 {
56 start = stride > 0 ? std::numeric_limits<int>::lowest() : std::numeric_limits<int>::max();
57 }
58
59 // Account negative start points
60 const int dim_size = input_shape[index];
61 if(start < 0)
62 {
63 start += dim_size;
64 }
65
66 // Final clamp
67 start = utility::clamp(start, 0, dim_size - 1);
68
69 return start;
70}
71
72int calculate_end_on_index(TensorShape input_shape, int index, int start_on_index,
73 Coordinates ends, Coordinates strides,
74 int32_t end_mask, int32_t shrink_axis_mask)
75{
76 // Early exit
77 if(index >= static_cast<int>(ends.num_dimensions()))
78 {
79 return input_shape[index];
80 }
81
82 const int stride = calculate_stride_on_index(index, strides);
83 const bool shrink_axis = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, index);
84
85 // Calculate start
86 int stop = ends[index];
87
88 // Shrink dimension
89 if(shrink_axis)
90 {
91 stop = start_on_index + 1;
92 }
93
94 // Reset in case of begin mask present
95 if(arm_compute::helpers::bit_ops::is_bit_set(end_mask, index) && !shrink_axis)
96 {
97 stop = (stride > 0) ? std::numeric_limits<int>::max() : std::numeric_limits<int>::lowest();
98 }
99
100 // Account negative end points
101 const int dim_size = input_shape[index];
102 if(stop < 0)
103 {
104 stop += dim_size;
105 }
106
107 // Final clamp
108 stop = (stride > 0) ? utility::clamp(stop, 0, dim_size) : utility::clamp(stop, -1, dim_size - 1);
109
110 return stop;
111}
112
113std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape,
114 Coordinates starts, Coordinates ends, Coordinates strides,
115 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
116{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100117 Coordinates starts_abs{};
118 Coordinates ends_abs{};
119 Coordinates final_strides{};
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000120 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
121 {
122 const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
123 starts_abs.set(i, start_i);
124 ends_abs.set(i, calculate_end_on_index(input_shape, i, start_i, ends, strides, end_mask, shrink_axis_mask));
125 final_strides.set(i, calculate_stride_on_index(i, strides));
126 }
127
128 return std::make_tuple(starts_abs, ends_abs, final_strides);
129}
130
131TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides,
132 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask, bool return_unshrinked)
133{
134 unsigned int index = 0;
135
136 TensorShape output_shape;
137 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
138 {
139 const int stride = calculate_stride_on_index(index, strides);
140 const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
141 const int end = calculate_end_on_index(input_shape, i, start, ends, strides, end_mask, shrink_axis_mask);
142 const int range = end - start;
143
144 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
145 if(return_unshrinked || !is_shrink)
146 {
147 if((range == 0) || // Zero range
148 (range < 0 && stride >= 0) || // Negative range with positive stride
149 (range > 0 && stride <= 0)) // Positive range with negative stride
150 {
151 output_shape.set(index, 0);
152 return output_shape;
153 }
154 else
155 {
156 int dim = range / stride + (range % stride != 0 ? 1 : 0);
157 output_shape.set(index++, dim);
158 }
159 }
160 }
161 return output_shape;
162}
163
164int32_t construct_slice_end_mask(Coordinates ends)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100165{
166 // Create end mask
167 int32_t end_mask = 0;
168 for(unsigned int i = 0; i < ends.num_dimensions(); ++i)
169 {
170 if(ends[i] < 0)
171 {
172 end_mask |= 1 << i;
173 }
174 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100175
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000176 return end_mask;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100177}
178} // namespace tensor_transform
179} // namespace helpers
180} // namespace arm_compute