xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/reference/Unstack.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "Unstack.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "tests/validation/Helpers.h"
27*c217d954SCole Faust 
28*c217d954SCole Faust namespace arm_compute
29*c217d954SCole Faust {
30*c217d954SCole Faust namespace test
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace validation
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace reference
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace
37*c217d954SCole Faust {
expand_coordinates(Coordinates in_coord,size_t axis,size_t slice,size_t num_dimensions)38*c217d954SCole Faust inline Coordinates expand_coordinates(Coordinates in_coord, size_t axis, size_t slice, size_t num_dimensions)
39*c217d954SCole Faust {
40*c217d954SCole Faust     /*
41*c217d954SCole Faust         Reconstruct input_coord to read the corresponding value from the correct slice. This is done by adding an extra dimension
42*c217d954SCole Faust         to the coordinates and shuffling around the values based on the info below.
43*c217d954SCole Faust 
44*c217d954SCole Faust         For example, if input tensor shape is (X, Y, Z, W);
45*c217d954SCole Faust 
46*c217d954SCole Faust         If axis == 0, each slice will have the shape (Y, Z, W) and there will be X slices
47*c217d954SCole Faust 
48*c217d954SCole Faust         If axis == 1, each slice will have the shape (X, Z, W) and there will be Y slices.
49*c217d954SCole Faust     */
50*c217d954SCole Faust     Coordinates expanded_coord;
51*c217d954SCole Faust     expanded_coord.set_num_dimensions(num_dimensions);
52*c217d954SCole Faust     expanded_coord.set(axis, slice);
53*c217d954SCole Faust     for(size_t k = 0; k < axis; ++k)
54*c217d954SCole Faust     {
55*c217d954SCole Faust         expanded_coord.set(k, in_coord[k]);
56*c217d954SCole Faust     }
57*c217d954SCole Faust     for(size_t k = axis + 1; k < num_dimensions; ++k)
58*c217d954SCole Faust     {
59*c217d954SCole Faust         expanded_coord.set(k, in_coord[k - 1]);
60*c217d954SCole Faust     }
61*c217d954SCole Faust     return expanded_coord;
62*c217d954SCole Faust }
63*c217d954SCole Faust 
64*c217d954SCole Faust template <typename T>
get_slice(const SimpleTensor<T> & input_tensor,size_t axis,size_t slice)65*c217d954SCole Faust SimpleTensor<T> get_slice(const SimpleTensor<T> &input_tensor, size_t axis, size_t slice)
66*c217d954SCole Faust {
67*c217d954SCole Faust     TensorShape out_shape = input_tensor.shape();
68*c217d954SCole Faust     out_shape.remove_dimension(axis);
69*c217d954SCole Faust 
70*c217d954SCole Faust     const size_t unpacked_num_dimensions(input_tensor.shape().num_dimensions());
71*c217d954SCole Faust 
72*c217d954SCole Faust     SimpleTensor<T> output{ out_shape, input_tensor.data_type() };
73*c217d954SCole Faust 
74*c217d954SCole Faust     Window win;
75*c217d954SCole Faust     win.use_tensor_dimensions(out_shape);
76*c217d954SCole Faust     execute_window_loop(win, [&](const Coordinates & id)
77*c217d954SCole Faust     {
78*c217d954SCole Faust         const Coordinates input_coords     = expand_coordinates(id, axis, slice, unpacked_num_dimensions);
79*c217d954SCole Faust         *reinterpret_cast<T *>(output(id)) = *reinterpret_cast<const T *>(input_tensor(input_coords));
80*c217d954SCole Faust     });
81*c217d954SCole Faust 
82*c217d954SCole Faust     return output;
83*c217d954SCole Faust }
84*c217d954SCole Faust } // namespace
85*c217d954SCole Faust 
86*c217d954SCole Faust template <typename T>
unstack(const SimpleTensor<T> & input_tensor,std::vector<SimpleTensor<T>> & output_tensors,int axis)87*c217d954SCole Faust std::vector<SimpleTensor<T>> unstack(const SimpleTensor<T> &input_tensor, std::vector<SimpleTensor<T>> &output_tensors, int axis)
88*c217d954SCole Faust {
89*c217d954SCole Faust     // Wrap around negative values
90*c217d954SCole Faust     const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_tensor.shape().num_dimensions()));
91*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(axis_u >= input_tensor.shape().num_dimensions());
92*c217d954SCole Faust     for(size_t k = 0; k < output_tensors.size(); ++k)
93*c217d954SCole Faust     {
94*c217d954SCole Faust         SimpleTensor<T>      &output    = output_tensors[k];
95*c217d954SCole Faust         const SimpleTensor<T> kth_slice = get_slice(input_tensor, axis_u, k);
96*c217d954SCole Faust         output                          = copy_tensor<T>(kth_slice);
97*c217d954SCole Faust     }
98*c217d954SCole Faust     return output_tensors;
99*c217d954SCole Faust }
100*c217d954SCole Faust 
101*c217d954SCole Faust template std::vector<SimpleTensor<float>> unstack(const SimpleTensor<float> &input_tensor, std::vector<SimpleTensor<float>> &output_tensors, int axis);
102*c217d954SCole Faust template std::vector<SimpleTensor<half>> unstack(const SimpleTensor<half> &input_tensor, std::vector<SimpleTensor<half>> &output_tensors, int axis);
103*c217d954SCole Faust template std::vector<SimpleTensor<uint8_t>> unstack(const SimpleTensor<uint8_t> &input_tensor, std::vector<SimpleTensor<uint8_t>> &output_tensors, int axis);
104*c217d954SCole Faust 
105*c217d954SCole Faust } // namespace reference
106*c217d954SCole Faust } // namespace validation
107*c217d954SCole Faust } // namespace test
108*c217d954SCole Faust } // namespace arm_compute
109