xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/UnstackFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2021 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 #ifndef ARM_COMPUTE_TEST_UNSTACK_FIXTURE
25 #define ARM_COMPUTE_TEST_UNSTACK_FIXTURE
26 
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30 #include "tests/AssetsLibrary.h"
31 #include "tests/Globals.h"
32 #include "tests/IAccessor.h"
33 #include "tests/framework/Asserts.h"
34 #include "tests/framework/Fixture.h"
35 #include "tests/validation/Helpers.h"
36 #include "tests/validation/reference/Unstack.h"
37 
38 #include <random>
39 
40 namespace arm_compute
41 {
42 namespace test
43 {
44 namespace validation
45 {
46 template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T>
47 class UnstackValidationFixture : public framework::Fixture
48 {
49 public:
50     template <typename...>
setup(TensorShape input_shape,int axis,int num,DataType data_type)51     void setup(TensorShape input_shape, int axis, int num, DataType data_type)
52     {
53         _target    = compute_target(input_shape, axis, num, data_type);
54         _reference = compute_reference(input_shape, axis, num, data_type);
55     }
56 
57 protected:
58     template <typename U>
fill(U && tensor,int i)59     void fill(U &&tensor, int i)
60     {
61         library->fill_tensor_uniform(tensor, i);
62     }
63 
compute_target(TensorShape input_shape,int axis,unsigned int num,DataType data_type)64     std::vector<TensorType> compute_target(TensorShape input_shape, int axis, unsigned int num, DataType data_type)
65     {
66         TensorType                 input_tensor = create_tensor<TensorType>(input_shape, data_type);
67         const unsigned int         axis_u       = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
68         const unsigned int         axis_size    = input_shape[axis_u];
69         const unsigned int         num_slices   = std::min(num, axis_size);
70         std::vector<TensorType>    output_slices(num_slices);
71         std::vector<ITensorType *> output_ptrs(num_slices);
72         for(size_t k = 0; k < num_slices; ++k)
73         {
74             output_ptrs[k] = &output_slices[k];
75         }
76         // Create and configure function
77         FunctionType unstack;
78         unstack.configure(&input_tensor, output_ptrs, axis);
79         // Allocate tensors
80         for(auto &out : output_slices)
81         {
82             out.allocator()->allocate();
83             ARM_COMPUTE_ASSERT(!out.info()->is_resizable());
84         }
85         input_tensor.allocator()->allocate();
86         ARM_COMPUTE_ASSERT(!input_tensor.info()->is_resizable());
87         fill(AccessorType(input_tensor), 0);
88         // Compute function
89         unstack.run();
90         return output_slices;
91     }
92 
compute_reference(TensorShape input_shape,int axis,unsigned int num,DataType data_type)93     std::vector<SimpleTensor<T>> compute_reference(TensorShape input_shape, int axis, unsigned int num, DataType data_type)
94     {
95         const unsigned int axis_u             = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
96         const unsigned int axis_size          = input_shape[axis_u];
97         const unsigned int num_output_tensors = (num == 0) ? axis_size : std::min(axis_size, num);
98         // create and fill input tensor
99         SimpleTensor<T> input_tensor{ input_shape, data_type };
100         fill(input_tensor, 0);
101         // create output tensors
102         const TensorShape            slice_shape = arm_compute::misc::shape_calculator::calculate_unstack_shape(input_shape, axis_u);
103         std::vector<SimpleTensor<T>> output_tensors(num_output_tensors);
104         for(size_t k = 0; k < num_output_tensors; ++k)
105         {
106             output_tensors[k] = SimpleTensor<T>(slice_shape, data_type);
107         }
108 
109         return reference::unstack<T>(input_tensor, output_tensors, axis);
110     }
111 
112     std::vector<TensorType>      _target{};
113     std::vector<SimpleTensor<T>> _reference{};
114 };
115 } // namespace validation
116 } // namespace test
117 } // namespace arm_compute
118 #endif /* ARM_COMPUTE_TEST_UNSTACK_FIXTURE */
119