blob: 08803c7fb0696af3893fb78f3d9fa20fb0e51120 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
2 * Copyright (c) 2018 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#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{
117 Coordinates starts_abs, ends_abs, final_strides;
118 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
119 {
120 const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
121 starts_abs.set(i, start_i);
122 ends_abs.set(i, calculate_end_on_index(input_shape, i, start_i, ends, strides, end_mask, shrink_axis_mask));
123 final_strides.set(i, calculate_stride_on_index(i, strides));
124 }
125
126 return std::make_tuple(starts_abs, ends_abs, final_strides);
127}
128
129TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides,
130 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask, bool return_unshrinked)
131{
132 unsigned int index = 0;
133
134 TensorShape output_shape;
135 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
136 {
137 const int stride = calculate_stride_on_index(index, strides);
138 const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
139 const int end = calculate_end_on_index(input_shape, i, start, ends, strides, end_mask, shrink_axis_mask);
140 const int range = end - start;
141
142 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
143 if(return_unshrinked || !is_shrink)
144 {
145 if((range == 0) || // Zero range
146 (range < 0 && stride >= 0) || // Negative range with positive stride
147 (range > 0 && stride <= 0)) // Positive range with negative stride
148 {
149 output_shape.set(index, 0);
150 return output_shape;
151 }
152 else
153 {
154 int dim = range / stride + (range % stride != 0 ? 1 : 0);
155 output_shape.set(index++, dim);
156 }
157 }
158 }
159 return output_shape;
160}
161
162int32_t construct_slice_end_mask(Coordinates ends)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100163{
164 // Create end mask
165 int32_t end_mask = 0;
166 for(unsigned int i = 0; i < ends.num_dimensions(); ++i)
167 {
168 if(ends[i] < 0)
169 {
170 end_mask |= 1 << i;
171 }
172 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100173
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000174 return end_mask;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100175}
176} // namespace tensor_transform
177} // namespace helpers
178} // namespace arm_compute