Pablo Tello | 5430369 | 2018-11-22 16:14:36 +0000 | [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 "Unstack.h" |
| 25 | |
| 26 | #include "tests/validation/Helpers.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
| 36 | namespace |
| 37 | { |
| 38 | inline Coordinates expand_coordinates(Coordinates in_coord, size_t axis, size_t slice, size_t num_dimensions) |
| 39 | { |
| 40 | /* |
| 41 | Reconstruct input_coord to read the corresponding value from the correct slice. This is done by adding an extra dimension |
| 42 | to the coordinates and shuffling around the values based on the info below. |
| 43 | |
| 44 | For example, if input tensor shape is (X, Y, Z, W); |
| 45 | |
| 46 | If axis == 0, each slice will have the shape (Y, Z, W) and there will be X slices |
| 47 | |
| 48 | If axis == 1, each slice will have the shape (X, Z, W) and there will be Y slices. |
| 49 | */ |
| 50 | Coordinates expanded_coord; |
| 51 | expanded_coord.set_num_dimensions(num_dimensions); |
| 52 | expanded_coord.set(axis, slice); |
| 53 | for(size_t k = 0; k < axis; ++k) |
| 54 | { |
| 55 | expanded_coord.set(k, in_coord[k]); |
| 56 | } |
| 57 | for(size_t k = axis + 1; k < num_dimensions; ++k) |
| 58 | { |
| 59 | expanded_coord.set(k, in_coord[k - 1]); |
| 60 | } |
| 61 | return expanded_coord; |
| 62 | } |
| 63 | |
| 64 | template <typename T> |
| 65 | SimpleTensor<T> get_slice(const SimpleTensor<T> &input_tensor, size_t axis, size_t slice) |
| 66 | { |
| 67 | TensorShape out_shape = input_tensor.shape(); |
| 68 | out_shape.remove_dimension(axis); |
| 69 | |
| 70 | const size_t unpacked_num_dimensions(input_tensor.shape().num_dimensions()); |
| 71 | |
| 72 | SimpleTensor<T> output{ out_shape, input_tensor.data_type() }; |
| 73 | |
| 74 | Window win; |
| 75 | win.use_tensor_dimensions(out_shape); |
| 76 | execute_window_loop(win, [&](const Coordinates & id) |
| 77 | { |
| 78 | const Coordinates input_coords = expand_coordinates(id, axis, slice, unpacked_num_dimensions); |
| 79 | *reinterpret_cast<T *>(output(id)) = *reinterpret_cast<const T *>(input_tensor(input_coords)); |
| 80 | }); |
| 81 | |
| 82 | return output; |
| 83 | } |
| 84 | } // namespace |
| 85 | |
| 86 | template <typename T> |
| 87 | std::vector<SimpleTensor<T>> unstack(const SimpleTensor<T> &input_tensor, std::vector<SimpleTensor<T>> &output_tensors, int axis) |
| 88 | { |
| 89 | // Wrap around negative values |
| 90 | const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_tensor.shape().num_dimensions())); |
| 91 | ARM_COMPUTE_ERROR_ON(axis_u >= input_tensor.shape().num_dimensions()); |
| 92 | for(size_t k = 0; k < output_tensors.size(); ++k) |
| 93 | { |
| 94 | SimpleTensor<T> &output = output_tensors[k]; |
| 95 | const SimpleTensor<T> kth_slice = get_slice(input_tensor, axis_u, k); |
| 96 | output = copy_tensor<T>(kth_slice); |
| 97 | } |
| 98 | return output_tensors; |
| 99 | } |
| 100 | |
| 101 | template std::vector<SimpleTensor<float>> unstack(const SimpleTensor<float> &input_tensor, std::vector<SimpleTensor<float>> &output_tensors, int axis); |
| 102 | template std::vector<SimpleTensor<half>> unstack(const SimpleTensor<half> &input_tensor, std::vector<SimpleTensor<half>> &output_tensors, int axis); |
| 103 | template std::vector<SimpleTensor<uint8_t>> unstack(const SimpleTensor<uint8_t> &input_tensor, std::vector<SimpleTensor<uint8_t>> &output_tensors, int axis); |
| 104 | |
| 105 | } // namespace reference |
| 106 | } // namespace validation |
| 107 | } // namespace test |
| 108 | } // namespace arm_compute |