xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/ReverseFixture.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_REVERSE_FIXTURE
25 #define ARM_COMPUTE_TEST_REVERSE_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/runtime/Tensor.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/reference/Reverse.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45 class ReverseValidationFixture : public framework::Fixture
46 {
47 public:
48     template <typename...>
setup(TensorShape shape,TensorShape axis_shape,DataType data_type)49     void setup(TensorShape shape, TensorShape axis_shape, DataType data_type)
50     {
51         _target    = compute_target(shape, axis_shape, data_type);
52         _reference = compute_reference(shape, axis_shape, data_type);
53     }
54 
55 protected:
56     template <typename U>
fill(U && tensor)57     void fill(U &&tensor)
58     {
59         library->fill_tensor_uniform(tensor, 0);
60     }
generate_random_axis()61     std::vector<int> generate_random_axis()
62     {
63         std::vector<int> axis_v = { 0, 1, 2, 3 };
64         std::mt19937     g(0);
65         std::shuffle(axis_v.begin(), axis_v.end(), g);
66 
67         return axis_v;
68     }
69 
compute_target(const TensorShape & shape,const TensorShape & axis_shape,DataType data_type)70     TensorType compute_target(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type)
71     {
72         // Create tensors
73         TensorType src  = create_tensor<TensorType>(shape, data_type, 1);
74         TensorType axis = create_tensor<TensorType>(axis_shape, DataType::U32, 1);
75         TensorType dst;
76 
77         // Create and configure function
78         FunctionType reverse_func;
79         reverse_func.configure(&src, &dst, &axis);
80 
81         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
82         ARM_COMPUTE_ASSERT(axis.info()->is_resizable());
83         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
84 
85         // Allocate tensors
86         src.allocator()->allocate();
87         axis.allocator()->allocate();
88         dst.allocator()->allocate();
89 
90         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
91         ARM_COMPUTE_ASSERT(!axis.info()->is_resizable());
92         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
93 
94         // Fill tensors
95         fill(AccessorType(src));
96         {
97             auto axis_data = AccessorType(axis);
98             auto axis_v    = generate_random_axis();
99             std::copy(axis_v.begin(), axis_v.begin() + axis_shape.x(), static_cast<int32_t *>(axis_data.data()));
100         }
101 
102         // Compute function
103         reverse_func.run();
104 
105         return dst;
106     }
107 
compute_reference(const TensorShape & shape,const TensorShape & axis_shape,DataType data_type)108     SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type)
109     {
110         // Create reference
111         SimpleTensor<T>        src{ shape, data_type };
112         SimpleTensor<uint32_t> axis{ axis_shape, DataType::U32 };
113 
114         // Fill reference
115         fill(src);
116         auto axis_v = generate_random_axis();
117         std::copy(axis_v.begin(), axis_v.begin() + axis_shape.x(), axis.data());
118 
119         return reference::reverse<T>(src, axis);
120     }
121 
122     TensorType      _target{};
123     SimpleTensor<T> _reference{};
124 };
125 } // namespace validation
126 } // namespace test
127 } // namespace arm_compute
128 #endif /* ARM_COMPUTE_TEST_REVERSE_FIXTURE */
129