blob: faa5b4433c4de620a0fe0ee5790d8fd540b1d4d5 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H
25#define ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H
Georgios Pinitas77589b52018-08-21 14:41:35 +010026
27#include "arm_compute/core/Types.h"
28
29namespace arm_compute
30{
31namespace helpers
32{
33namespace tensor_transform
34{
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000035/** Computes stride of a given index
Georgios Pinitasc1a72452018-08-24 11:25:32 +010036 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000037 * @param[in] index Index of tensor to calculate absolute start position
38 * @param[in] strides Slice strides
Georgios Pinitasc1a72452018-08-24 11:25:32 +010039 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000040 * @return Stride at a given index
Georgios Pinitasc1a72452018-08-24 11:25:32 +010041 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000042int calculate_stride_on_index(int index, Coordinates strides);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010043
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000044/** Computes absolute start position of a given index for a strided slice operation
Georgios Pinitasc1a72452018-08-24 11:25:32 +010045 *
46 * @param[in] input_shape Input tensor shape
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000047 * @param[in] index Index of tensor to calculate absolute start position
Georgios Pinitas77589b52018-08-21 14:41:35 +010048 * @param[in] starts Start coordinates
49 * @param[in] strides Slice strides
Georgios Pinitasc1a72452018-08-24 11:25:32 +010050 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and
Georgios Pinitas77589b52018-08-21 14:41:35 +010051 * the fullest possible range in that dimension is used instead.
52 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000053 * @return Absolute start position of a given index
Georgios Pinitas77589b52018-08-21 14:41:35 +010054 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000055int calculate_start_on_index(TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +010056
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000057/** Returns the absolute end position of a given index for a strided slice operation
Georgios Pinitasc1a72452018-08-24 11:25:32 +010058 *
Georgios Pinitas77589b52018-08-21 14:41:35 +010059 * @param[in] input_shape Input tensor shape
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000060 * @param[in] index Index of tensor to calculate absolute start position
61 * @param[in] start_on_index Absolute start coordinate for given index
Georgios Pinitas77589b52018-08-21 14:41:35 +010062 * @param[in] ends End coordinates
63 * @param[in] strides Slice strides
64 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and
65 * the fullest possible range in that dimension is used instead.
66 * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
Georgios Pinitasc1a72452018-08-24 11:25:32 +010067 * A slice of size 1 starting from starts[i] in the dimension must be preserved.
Georgios Pinitas77589b52018-08-21 14:41:35 +010068 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000069 * @return Absolute end position of a given index
Georgios Pinitas77589b52018-08-21 14:41:35 +010070 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000071int calculate_end_on_index(TensorShape input_shape, int index, int start_on_index, Coordinates ends, Coordinates strides,
72 int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
73
74/** Calculate start, end and stride coordinates for a strided slice
Georgios Pinitas77589b52018-08-21 14:41:35 +010075 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000076 * @param[in] input_shape Input tensor shape
77 * @param[in] starts Start coordinates
78 * @param[in] ends End coordinates
79 * @param[in] strides Slice strides
80 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and
81 * the fullest possible range in that dimension is used instead.
82 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and
83 * the fullest possible range in that dimension is used instead.
84 * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
85 * A slice of size 1 starting from starts[i] in the dimension must be preserved.
Georgios Pinitas77589b52018-08-21 14:41:35 +010086 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000087 * @return A tuple with <Start,End,Strides>
Georgios Pinitas77589b52018-08-21 14:41:35 +010088 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000089std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape,
90 Coordinates starts, Coordinates ends, Coordinates strides,
91 int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0);
Georgios Pinitas77589b52018-08-21 14:41:35 +010092
Georgios Pinitasc1a72452018-08-24 11:25:32 +010093/** Computes output shape of strided slice
94 *
95 * @warning Starts and ends must be non-negative
96 * @warning Starts, ends and final strides should have the same dimensions as the input shape
Georgios Pinitas77589b52018-08-21 14:41:35 +010097 *
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000098 * @param[in] input_shape Input tensor shape
99 * @param[in] starts Absolute start coordinates
100 * @param[in] ends Absolute end coordinates
101 * @param[in] strides Slice strides
102 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and
103 * the fullest possible range in that dimension is used instead.
104 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and
105 * the fullest possible range in that dimension is used instead.
106 * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
107 * A slice of size 1 starting from starts[i] in the dimension must be preserved.
108 * @param[in] return_unshrinked (Optional) Returns un-shrinked shape
Georgios Pinitas77589b52018-08-21 14:41:35 +0100109 *
110 * @return The output tensor shape
111 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000112TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides,
113 int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0,
114 bool return_unshrinked = false);
115
116/** Constructs end mask in case we want to perform a slice operation using the strided slice interface
117 *
118 * @note Ends are inclusive in slice operations that is why construction an end mask is needed
119 *
120 * @param[in] ends End coordinates
121 *
122 * @return End mask
123 */
124int32_t construct_slice_end_mask(Coordinates ends);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100125} // namespace tensor_tranform
126} // namespace helpers
127} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000128#endif /* ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H */