xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/StackLayerFixture.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_STACK_LAYER_FIXTURE
25 #define ARM_COMPUTE_TEST_STACK_LAYER_FIXTURE
26 
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/TensorShape.h"
29 #include "arm_compute/core/Types.h"
30 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
31 #include "tests/AssetsLibrary.h"
32 #include "tests/Globals.h"
33 #include "tests/IAccessor.h"
34 #include "tests/framework/Asserts.h"
35 #include "tests/framework/Fixture.h"
36 #include "tests/validation/Helpers.h"
37 #include "tests/validation/reference/StackLayer.h"
38 #include "tests/validation/reference/Utils.h"
39 
40 #include <random>
41 #include <vector>
42 
43 namespace arm_compute
44 {
45 namespace test
46 {
47 namespace validation
48 {
49 using namespace arm_compute::misc::shape_calculator;
50 
51 template <typename TensorType, typename AbstractTensorType, typename AccessorType, typename FunctionType, typename T>
52 class StackLayerValidationFixture : public framework::Fixture
53 {
54 public:
55     template <typename...>
setup(TensorShape shape_src,int axis,DataType data_type,int num_tensors)56     void setup(TensorShape shape_src, int axis, DataType data_type, int num_tensors)
57     {
58         _target    = compute_target(shape_src, axis, data_type, num_tensors);
59         _reference = compute_reference(shape_src, axis, data_type, num_tensors);
60     }
61 
62 protected:
63     template <typename U>
fill(U && tensor,unsigned int i)64     void fill(U &&tensor, unsigned int i)
65     {
66         library->fill_tensor_uniform(tensor, i);
67     }
68 
compute_target(TensorShape shape_src,int axis,DataType data_type,int num_tensors)69     TensorType compute_target(TensorShape shape_src, int axis, DataType data_type, int num_tensors)
70     {
71         std::vector<TensorType>           tensors(num_tensors);
72         std::vector<AbstractTensorType *> src(num_tensors);
73 
74         // Create vector of input tensors
75         for(int i = 0; i < num_tensors; ++i)
76         {
77             tensors[i] = create_tensor<TensorType>(shape_src, data_type);
78             src[i]     = &(tensors[i]);
79             ARM_COMPUTE_ASSERT(tensors[i].info()->is_resizable());
80         }
81 
82         // Create tensors
83         TensorType dst;
84 
85         // The output tensor will be auto-initialized within the function
86 
87         // Create and configure function
88         FunctionType stack;
89         stack.configure(src, axis, &dst);
90 
91         // Allocate and fill the input tensors
92         for(int i = 0; i < num_tensors; ++i)
93         {
94             ARM_COMPUTE_ASSERT(tensors[i].info()->is_resizable());
95             tensors[i].allocator()->allocate();
96             ARM_COMPUTE_ASSERT(!tensors[i].info()->is_resizable());
97 
98             // Fill input tensor
99             fill(AccessorType(tensors[i]), i);
100         }
101 
102         // Allocate output tensor
103         dst.allocator()->allocate();
104 
105         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
106 
107         // Compute stack function
108         stack.run();
109 
110         return dst;
111     }
112 
compute_reference(const TensorShape & shape_src,int axis,DataType data_type,int num_tensors)113     SimpleTensor<T> compute_reference(const TensorShape &shape_src, int axis, DataType data_type, int num_tensors)
114     {
115         std::vector<SimpleTensor<T>> src;
116 
117         for(int i = 0; i < num_tensors; ++i)
118         {
119             src.emplace_back(std::move(SimpleTensor<T>(shape_src, data_type, 1)));
120 
121             fill(src[i], i);
122         }
123 
124         // Wrap around negative values
125         const unsigned int axis_u = wrap_around(axis, static_cast<int>(shape_src.num_dimensions() + 1));
126 
127         const TensorShape shape_dst = compute_stack_shape(TensorInfo(shape_src, 1, data_type), axis_u, num_tensors);
128 
129         return reference::stack_layer<T>(src, shape_dst, data_type, axis_u);
130     }
131 
132     TensorType      _target{};
133     SimpleTensor<T> _reference{};
134 };
135 } // namespace validation
136 } // namespace test
137 } // namespace arm_compute
138 #endif /* ARM_COMPUTE_TEST_STACK_LAYER_FIXTURE */
139