Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Arm Limited. |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H |
| 25 | #define ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Types.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace helpers |
| 32 | { |
| 33 | namespace tensor_transform |
| 34 | { |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 35 | /** Computes stride of a given index |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 36 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 37 | * @param[in] index Index of tensor to calculate absolute start position |
| 38 | * @param[in] strides Slice strides |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 39 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 40 | * @return Stride at a given index |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 41 | */ |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 42 | int calculate_stride_on_index(int index, Coordinates strides); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 43 | |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 44 | /** Computes absolute start position of a given index for a strided slice operation |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 45 | * |
| 46 | * @param[in] input_shape Input tensor shape |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 47 | * @param[in] index Index of tensor to calculate absolute start position |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 48 | * @param[in] starts Start coordinates |
| 49 | * @param[in] strides Slice strides |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 50 | * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 51 | * the fullest possible range in that dimension is used instead. |
| 52 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 53 | * @return Absolute start position of a given index |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 54 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 55 | int calculate_start_on_index( |
| 56 | TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 57 | |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 58 | /** Returns the absolute end position of a given index for a strided slice operation |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 59 | * |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 60 | * @param[in] input_shape Input tensor shape |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 61 | * @param[in] index Index of tensor to calculate absolute start position |
| 62 | * @param[in] start_on_index Absolute start coordinate for given index |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 63 | * @param[in] ends End coordinates |
| 64 | * @param[in] strides Slice strides |
| 65 | * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and |
| 66 | * the fullest possible range in that dimension is used instead. |
| 67 | * @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 Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 68 | * A slice of size 1 starting from starts[i] in the dimension must be preserved. |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 69 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 70 | * @return Absolute end position of a given index |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 71 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 72 | int calculate_end_on_index(TensorShape input_shape, |
| 73 | int index, |
| 74 | int start_on_index, |
| 75 | Coordinates ends, |
| 76 | Coordinates strides, |
| 77 | int32_t end_mask = 0, |
| 78 | int32_t shrink_axis_mask = 0); |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 79 | |
| 80 | /** Calculate start, end and stride coordinates for a strided slice |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 81 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 82 | * @param[in] input_shape Input tensor shape |
| 83 | * @param[in] starts Start coordinates |
| 84 | * @param[in] ends End coordinates |
| 85 | * @param[in] strides Slice strides |
| 86 | * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and |
| 87 | * the fullest possible range in that dimension is used instead. |
| 88 | * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and |
| 89 | * the fullest possible range in that dimension is used instead. |
| 90 | * @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. |
| 91 | * A slice of size 1 starting from starts[i] in the dimension must be preserved. |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 92 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 93 | * @return A tuple with <Start,End,Strides> |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 94 | */ |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 95 | std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 96 | Coordinates starts, |
| 97 | Coordinates ends, |
| 98 | Coordinates strides, |
| 99 | int32_t begin_mask = 0, |
| 100 | int32_t end_mask = 0, |
| 101 | int32_t shrink_axis_mask = 0); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 102 | |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 103 | /** Computes output shape of strided slice |
| 104 | * |
| 105 | * @warning Starts and ends must be non-negative |
| 106 | * @warning Starts, ends and final strides should have the same dimensions as the input shape |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 107 | * |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 108 | * @param[in] input_shape Input tensor shape |
| 109 | * @param[in] starts Absolute start coordinates |
| 110 | * @param[in] ends Absolute end coordinates |
| 111 | * @param[in] strides Slice strides |
| 112 | * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and |
| 113 | * the fullest possible range in that dimension is used instead. |
| 114 | * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and |
| 115 | * the fullest possible range in that dimension is used instead. |
| 116 | * @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. |
| 117 | * A slice of size 1 starting from starts[i] in the dimension must be preserved. |
| 118 | * @param[in] return_unshrinked (Optional) Returns un-shrinked shape |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 119 | * |
| 120 | * @return The output tensor shape |
| 121 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 122 | TensorShape compute_strided_slice_output_shape(TensorShape input_shape, |
| 123 | Coordinates starts, |
| 124 | Coordinates ends, |
| 125 | Coordinates strides, |
| 126 | int32_t begin_mask = 0, |
| 127 | int32_t end_mask = 0, |
| 128 | int32_t shrink_axis_mask = 0, |
| 129 | bool return_unshrinked = false); |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 130 | |
| 131 | /** Constructs end mask in case we want to perform a slice operation using the strided slice interface |
| 132 | * |
| 133 | * @note Ends are inclusive in slice operations that is why construction an end mask is needed |
| 134 | * |
| 135 | * @param[in] ends End coordinates |
| 136 | * |
| 137 | * @return End mask |
| 138 | */ |
| 139 | int32_t construct_slice_end_mask(Coordinates ends); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 140 | } // namespace tensor_transform |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 141 | } // namespace helpers |
| 142 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 143 | #endif /* ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H */ |