Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 1 | /* |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [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 | */ |
| 24 | #include "arm_compute/runtime/NEON/functions/NEUnstack.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 32 | #include "src/common/utils/Log.h" |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace |
| 37 | { |
| 38 | inline unsigned int wrap_axis(int axis, const ITensorInfo *const tensor) |
| 39 | { |
| 40 | return wrap_around(axis, static_cast<int>(tensor->num_dimensions())); |
| 41 | } |
| 42 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 43 | inline void setup_slice_coordinates_and_mask(Coordinates &slice_start, |
| 44 | int32_t &slice_end_mask, |
| 45 | const unsigned int input_num_dimensions) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 46 | { |
| 47 | // Setups up coordinates to slice the input tensor: start coordinates to all 0s and the unstacking axis of both Start/End to slice just one 2d tensor at a time. |
| 48 | Coordinates slice_end; |
| 49 | slice_start.set_num_dimensions(input_num_dimensions); |
| 50 | slice_end.set_num_dimensions(input_num_dimensions); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 51 | for (size_t k = 0; k < input_num_dimensions; ++k) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 52 | { |
| 53 | slice_start.set(k, 0); |
| 54 | slice_end.set(k, -1); |
| 55 | } |
| 56 | slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(slice_end); |
| 57 | } |
| 58 | } // namespace |
| 59 | |
| 60 | NEUnstack::NEUnstack() // NOLINT |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 61 | : _num_slices(0), _strided_slice_vector() |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 62 | { |
| 63 | } |
| 64 | |
| 65 | void NEUnstack::configure(const ITensor *input, const std::vector<ITensor *> &output_vector, int axis) |
| 66 | { |
| 67 | std::vector<ITensorInfo *> outputs_vector_info(output_vector.size()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | std::transform(output_vector.begin(), output_vector.end(), outputs_vector_info.begin(), |
| 69 | [](ITensor *t) |
| 70 | { |
| 71 | ARM_COMPUTE_ERROR_ON_NULLPTR(t); |
| 72 | return t->info(); |
| 73 | }); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 74 | |
| 75 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 76 | ARM_COMPUTE_ERROR_THROW_ON(NEUnstack::validate(input->info(), outputs_vector_info, axis)); |
ramelg01 | 4a6d9e8 | 2021-10-02 14:34:36 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_LOG_PARAMS(input, output_vector, axis); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 78 | |
| 79 | // Wrap around negative values |
| 80 | const unsigned int axis_u = wrap_axis(axis, input->info()); |
| 81 | _num_slices = std::min(outputs_vector_info.size(), input->info()->dimension(axis_u)); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 82 | _strided_slice_vector.resize(_num_slices); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 83 | |
| 84 | Coordinates slice_start; |
| 85 | int32_t slice_end_mask; |
| 86 | setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->info()->tensor_shape().num_dimensions()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | for (unsigned int slice = 0; slice < _num_slices; ++slice) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 88 | { |
| 89 | // Adjusts start and end coordinates to take a 2D slice at a time |
| 90 | slice_start.set(axis_u, slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | _strided_slice_vector[slice].configure(input, output_vector[slice], slice_start, Coordinates(), BiStrides(), 0, |
| 92 | slice_end_mask, (1 << axis_u)); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | Status NEUnstack::validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &output_vector, int axis) |
| 97 | { |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON(output_vector.empty()); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON(axis < (-static_cast<int>(input->tensor_shape().num_dimensions()))); |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON(axis >= static_cast<int>(input->tensor_shape().num_dimensions())); |
| 102 | |
| 103 | const unsigned int num_slices = std::min(output_vector.size(), input->dimension(wrap_axis(axis, input))); |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(num_slices > input->dimension(wrap_axis(axis, input))); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON(num_slices > output_vector.size()); |
| 106 | |
| 107 | Coordinates slice_start; |
| 108 | int32_t slice_end_mask; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | for (size_t k = 0; k < num_slices; ++k) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 110 | { |
| 111 | slice_start.set(wrap_axis(axis, input), k); |
| 112 | setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->tensor_shape().num_dimensions()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | ARM_COMPUTE_RETURN_ON_ERROR(NEStridedSlice::validate(input, output_vector[k], slice_start, Coordinates(), |
| 114 | BiStrides(), 0, slice_end_mask, |
| 115 | (1 << wrap_axis(axis, input)))); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 116 | } |
| 117 | return Status{}; |
| 118 | } |
| 119 | |
| 120 | void NEUnstack::run() |
| 121 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | for (unsigned i = 0; i < _num_slices; ++i) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 123 | { |
| 124 | _strided_slice_vector[i].run(); |
| 125 | } |
| 126 | } |
| 127 | } // namespace arm_compute |