xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/GEMMReshapeLHSMatrixFixture.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_GEMMRESHAPELHSMATRIX_FIXTURE
25 #define ARM_COMPUTE_TEST_GEMMRESHAPELHSMATRIX_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/GEMMReshapeLHSMatrix.h"
37 #include "tests/validation/reference/Utils.h"
38 
39 #include <random>
40 
41 namespace arm_compute
42 {
43 namespace test
44 {
45 namespace validation
46 {
47 using namespace arm_compute::misc::shape_calculator;
48 
49 template <typename TensorType, typename AccessorType, typename OperatorType, typename T, bool reinterpret_input_as_3d = false>
50 class GEMMReshapeLHSMatrixValidationFixture : public framework::Fixture
51 {
52 public:
53     template <typename...>
setup(TensorShape shape_in,unsigned int batch_size,DataType data_type,unsigned int m0,unsigned int k0,unsigned int v0,bool interleave,bool transpose)54     void setup(TensorShape shape_in, unsigned int batch_size, DataType data_type, unsigned int m0, unsigned int k0, unsigned int v0, bool interleave, bool transpose)
55     {
56         GEMMLHSMatrixInfo lhs_info;
57         lhs_info.m0         = m0;
58         lhs_info.k0         = k0;
59         lhs_info.v0         = v0;
60         lhs_info.interleave = interleave;
61         lhs_info.transpose  = transpose;
62 
63         // Set the tensor shape
64         const TensorShape shape_src(shape_in[0],
65                                     shape_in[1],
66                                     reinterpret_input_as_3d ? shape_in[2] : batch_size,
67                                     reinterpret_input_as_3d ? batch_size : 1);
68 
69         _target    = compute_target(shape_src, data_type, lhs_info);
70         _reference = compute_reference(shape_src, data_type, lhs_info);
71     }
72 
73 protected:
74     template <typename U>
fill(U && tensor)75     void fill(U &&tensor)
76     {
77         library->fill_tensor_uniform(tensor, 0);
78     }
79 
compute_target(TensorShape input_shape,DataType data_type,const GEMMLHSMatrixInfo & lhs_info)80     TensorType compute_target(TensorShape input_shape, DataType data_type, const GEMMLHSMatrixInfo &lhs_info)
81     {
82         // Create tensors
83         TensorType src = create_tensor<TensorType>(input_shape, data_type, 1);
84         TensorType dst;
85 
86         // The output tensor will be auto-initialized within the function
87 
88         // Create and configure function
89         OperatorType gemm_lhs_reshape;
90         gemm_lhs_reshape.configure(src.info(), dst.info(), lhs_info, reinterpret_input_as_3d);
91 
92         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
93 
94         add_padding_x({ &src, &dst });
95 
96         // Allocate tensors
97         src.allocator()->allocate();
98         dst.allocator()->allocate();
99 
100         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
101         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
102 
103         // Fill tensors
104         fill(AccessorType(src));
105 
106         // Compute GEMM LHS matrix reshape function
107         ITensorPack tensors = { { ACL_SRC, &src }, { ACL_DST, &dst } };
108         gemm_lhs_reshape.run(tensors);
109 
110         return dst;
111     }
112 
compute_reference(const TensorShape & input_shape,DataType data_type,const GEMMLHSMatrixInfo & lhs_info)113     SimpleTensor<T> compute_reference(const TensorShape &input_shape, DataType data_type, const GEMMLHSMatrixInfo &lhs_info)
114     {
115         TensorShape src_shape = input_shape;
116 
117         // If the input has to be reinterpreted as 3D, collapse the second dimension with the 3rd
118         if(reinterpret_input_as_3d)
119         {
120             src_shape.collapse(2U, 1U);
121         }
122 
123         // Create reference
124         SimpleTensor<T> src{ src_shape, data_type, 1 };
125 
126         // Fill reference
127         fill(src);
128 
129         TensorShape output_shape = compute_lhs_reshaped_shape(TensorInfo(input_shape, 1, data_type), lhs_info, reinterpret_input_as_3d);
130 
131         return reference::gemm_reshape_lhs_matrix<T>(src, output_shape, lhs_info);
132     }
133 
134     TensorType      _target{};
135     SimpleTensor<T> _reference{};
136 };
137 } // namespace validation
138 } // namespace test
139 } // namespace arm_compute
140 #endif /* ARM_COMPUTE_TEST_GEMMRESHAPELHSMATRIX_FIXTURE */