blob: f6a54a59ee9d7115b3660bbdbca916e65e304f19 [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{};
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100120
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000121 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
122 {
123 const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
124 starts_abs.set(i, start_i);
125 ends_abs.set(i, calculate_end_on_index(input_shape, i, start_i, ends, strides, end_mask, shrink_axis_mask));
126 final_strides.set(i, calculate_stride_on_index(i, strides));
127 }
128
129 return std::make_tuple(starts_abs, ends_abs, final_strides);
130}
131
132TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides,
133 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask, bool return_unshrinked)
134{
135 unsigned int index = 0;
136
137 TensorShape output_shape;
138 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
139 {
140 const int stride = calculate_stride_on_index(index, strides);
141 const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
142 const int end = calculate_end_on_index(input_shape, i, start, ends, strides, end_mask, shrink_axis_mask);
143 const int range = end - start;
144
145 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
146 if(return_unshrinked || !is_shrink)
147 {
148 if((range == 0) || // Zero range
149 (range < 0 && stride >= 0) || // Negative range with positive stride
150 (range > 0 && stride <= 0)) // Positive range with negative stride
151 {
152 output_shape.set(index, 0);
153 return output_shape;
154 }
155 else
156 {
157 int dim = range / stride + (range % stride != 0 ? 1 : 0);
158 output_shape.set(index++, dim);
159 }
160 }
161 }
162 return output_shape;
163}
164
165int32_t construct_slice_end_mask(Coordinates ends)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100166{
167 // Create end mask
168 int32_t end_mask = 0;
169 for(unsigned int i = 0; i < ends.num_dimensions(); ++i)
170 {
171 if(ends[i] < 0)
172 {
173 end_mask |= 1 << i;
174 }
175 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100176
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000177 return end_mask;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100178}
179} // namespace tensor_transform
180} // namespace helpers
181} // namespace arm_compute