From 543036904d5324c4738cdc8b586c301471046c8e Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Thu, 22 Nov 2018 16:14:36 +0000 Subject: COMPMID-1726: Implement CLUnstack. Change-Id: I94b0707d19757c5f5d7ca66d9c47e378867126a3 Reviewed-on: https://review.mlplatform.org/325 Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- tests/validation/reference/Unstack.cpp | 108 +++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/validation/reference/Unstack.cpp (limited to 'tests/validation/reference/Unstack.cpp') diff --git a/tests/validation/reference/Unstack.cpp b/tests/validation/reference/Unstack.cpp new file mode 100644 index 0000000000..3474c15173 --- /dev/null +++ b/tests/validation/reference/Unstack.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "Unstack.h" + +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +inline Coordinates expand_coordinates(Coordinates in_coord, size_t axis, size_t slice, size_t num_dimensions) +{ + /* + Reconstruct input_coord to read the corresponding value from the correct slice. This is done by adding an extra dimension + to the coordinates and shuffling around the values based on the info below. + + For example, if input tensor shape is (X, Y, Z, W); + + If axis == 0, each slice will have the shape (Y, Z, W) and there will be X slices + + If axis == 1, each slice will have the shape (X, Z, W) and there will be Y slices. + */ + Coordinates expanded_coord; + expanded_coord.set_num_dimensions(num_dimensions); + expanded_coord.set(axis, slice); + for(size_t k = 0; k < axis; ++k) + { + expanded_coord.set(k, in_coord[k]); + } + for(size_t k = axis + 1; k < num_dimensions; ++k) + { + expanded_coord.set(k, in_coord[k - 1]); + } + return expanded_coord; +} + +template +SimpleTensor get_slice(const SimpleTensor &input_tensor, size_t axis, size_t slice) +{ + TensorShape out_shape = input_tensor.shape(); + out_shape.remove_dimension(axis); + + const size_t unpacked_num_dimensions(input_tensor.shape().num_dimensions()); + + SimpleTensor output{ out_shape, input_tensor.data_type() }; + + Window win; + win.use_tensor_dimensions(out_shape); + execute_window_loop(win, [&](const Coordinates & id) + { + const Coordinates input_coords = expand_coordinates(id, axis, slice, unpacked_num_dimensions); + *reinterpret_cast(output(id)) = *reinterpret_cast(input_tensor(input_coords)); + }); + + return output; +} +} // namespace + +template +std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis) +{ + // Wrap around negative values + const unsigned int axis_u = wrap_around(axis, static_cast(input_tensor.shape().num_dimensions())); + ARM_COMPUTE_ERROR_ON(axis_u >= input_tensor.shape().num_dimensions()); + for(size_t k = 0; k < output_tensors.size(); ++k) + { + SimpleTensor &output = output_tensors[k]; + const SimpleTensor kth_slice = get_slice(input_tensor, axis_u, k); + output = copy_tensor(kth_slice); + } + return output_tensors; +} + +template std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis); +template std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis); +template std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute -- cgit v1.2.1