Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame^] | 1 | /* |
| 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 | |
| 26 | namespace arm_compute |
| 27 | { |
| 28 | namespace helpers |
| 29 | { |
| 30 | namespace tensor_transform |
| 31 | { |
| 32 | Coordinates strided_slice_absolute_start_coords(TensorShape input_shape, Coordinates starts, Coordinates strides, int32_t begin_mask) |
| 33 | { |
| 34 | Coordinates starts_abs; |
| 35 | for(unsigned int i = 0; i < starts.num_dimensions(); ++i) |
| 36 | { |
| 37 | // Get start index |
| 38 | int start_i = starts[i]; |
| 39 | |
| 40 | // Reset in case of begin mask present |
| 41 | if((begin_mask & 1 << i) != 0) |
| 42 | { |
| 43 | start_i = strides[i] > 0 ? std::numeric_limits<int>::lowest() : std::numeric_limits<int>::max(); |
| 44 | } |
| 45 | |
| 46 | // Account negative start points |
| 47 | const int dim_size = input_shape[i]; |
| 48 | if(start_i < 0) |
| 49 | { |
| 50 | start_i += dim_size; |
| 51 | } |
| 52 | |
| 53 | // Final clamp |
| 54 | start_i = utility::clamp(start_i, 0, dim_size - 1); |
| 55 | starts_abs.set(i, start_i); |
| 56 | } |
| 57 | |
| 58 | // Fill remaining |
| 59 | for(unsigned int i = starts_abs.num_dimensions(); i < input_shape.num_dimensions(); ++i) |
| 60 | { |
| 61 | starts_abs.set(i, 0); |
| 62 | } |
| 63 | |
| 64 | return starts_abs; |
| 65 | } |
| 66 | |
| 67 | Coordinates strided_slice_absolute_end_coords(TensorShape input_shape, Coordinates starts_abs, Coordinates ends, Coordinates strides, |
| 68 | int32_t end_mask, int32_t shrink_axis_mask) |
| 69 | { |
| 70 | Coordinates ends_abs; |
| 71 | for(unsigned int i = 0; i < ends.num_dimensions(); ++i) |
| 72 | { |
| 73 | // Get end index |
| 74 | int stop_i = ends[i]; |
| 75 | |
| 76 | // Shrink dimension |
| 77 | if((shrink_axis_mask & (1 << i)) != 0) |
| 78 | { |
| 79 | stop_i = starts_abs[i] + 1; |
| 80 | } |
| 81 | |
| 82 | // Reset in case of begin mask present |
| 83 | if((end_mask & 1 << i) != 0) |
| 84 | { |
| 85 | stop_i = (strides[i] > 0) ? std::numeric_limits<int>::max() : std::numeric_limits<int>::lowest(); |
| 86 | } |
| 87 | |
| 88 | // Account negative end points |
| 89 | const int dim_size = input_shape[i]; |
| 90 | if(stop_i < 0) |
| 91 | { |
| 92 | stop_i += dim_size; |
| 93 | } |
| 94 | |
| 95 | // Final clamp |
| 96 | stop_i = (strides[i] > 0) ? utility::clamp(stop_i, 0, dim_size) : utility::clamp(stop_i, -1, dim_size - 1); |
| 97 | ends_abs.set(i, stop_i); |
| 98 | } |
| 99 | |
| 100 | // Fill remaining ends |
| 101 | for(unsigned int i = ends_abs.num_dimensions(); i < input_shape.num_dimensions(); ++i) |
| 102 | { |
| 103 | ends_abs.set(i, input_shape[i]); |
| 104 | } |
| 105 | |
| 106 | return ends_abs; |
| 107 | } |
| 108 | |
| 109 | Coordinates strided_slice_strides(TensorShape input_shape, Coordinates strides) |
| 110 | { |
| 111 | for(unsigned int i = strides.num_dimensions(); i < input_shape.num_dimensions(); ++i) |
| 112 | { |
| 113 | strides.set(i, 1); |
| 114 | } |
| 115 | return strides; |
| 116 | } |
| 117 | |
| 118 | TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts_abs, Coordinates ends_abs, Coordinates final_strides) |
| 119 | { |
| 120 | TensorShape output_shape = input_shape; |
| 121 | for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) |
| 122 | { |
| 123 | const int stride_i = final_strides[i]; |
| 124 | const int range = ends_abs[i] - starts_abs[i]; |
| 125 | if((range == 0) || // Zero range |
| 126 | (range < 0 && stride_i >= 0) || // Negative range with positive stride |
| 127 | (range > 0 && stride_i <= 0)) // Positive range with negative stride |
| 128 | { |
| 129 | output_shape.set(i, 0); |
| 130 | return output_shape; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | int dim = range / stride_i + (range % stride_i != 0 ? 1 : 0); |
| 135 | output_shape.set(i, dim); |
| 136 | } |
| 137 | } |
| 138 | return output_shape; |
| 139 | } |
| 140 | } // namespace tensor_transform |
| 141 | } // namespace helpers |
| 142 | } // namespace arm_compute |