1 /*
2  * Copyright (c) 2022 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 
25 #include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel.h"
26 #include "src/gpu/cl/kernels/ClGemmReshapeRhsMatrixKernel.h"
27 #include "tests/CL/CLAccessor.h"
28 #include "tests/CL/Helper.h"
29 #include "tests/framework/Macros.h"
30 #include "tests/framework/datasets/Datasets.h"
31 #include "tests/validation/Validation.h"
32 #include "tests/validation/fixtures/GEMMFixture.h"
33 
34 namespace arm_compute
35 {
36 namespace test
37 {
38 namespace validation
39 {
40 using namespace arm_compute::opencl::kernels;
41 
42 // Create function for ClGemmReshapeRhsMatrixKernel
43 using CLGEMMReshapeRHSMatrix = CLSynthetizeOperator<ClGemmReshapeRhsMatrixKernel>;
44 
45 // Create function for ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel
46 using CLGEMMMatrixMultiplyReshapedOnlyRhsMMUL = CLSynthetizeOperator<ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel>;
47 
48 // Fixture for CLGEMMMatrixMultiplyReshapedOnlyRhsMMUL
49 template <typename T>
50 using CLGEMMMatrixMultiplyReshapedOnlyRhsMMULFixture = GEMMMatrixMultiplyReshapedOnlyRhsMMULValidationFixture<CLTensor, CLAccessor, T, CLGEMMReshapeRHSMatrix, CLGEMMMatrixMultiplyReshapedOnlyRhsMMUL>;
51 
52 namespace
53 {
54 // *INDENT-OFF*
55 // clang-format off
56 RelativeTolerance<float> rel_tolerance_f32(0.001f);
57 constexpr float          abs_tolerance_f32(0.0001f);
58 RelativeTolerance<half_float::half> rel_tolerance_f16(half_float::half(0.001f));
59 constexpr float          abs_tolerance_f16(0.3f);
60 
61 /** Alpha values to test - Precommit */
62 const auto a_values = framework::dataset::make("alpha", {1.0f, 0.75f} );
63 
64 /** Beta values to test - Precommit */
65 const auto beta_values = framework::dataset::make("beta", {0.0f, -0.75f} );
66 
67 /** M values to test */
68 const auto m_values = framework::dataset::make("M", {49});
69 
70 /** N values to test */
71 const auto n_values = framework::dataset::make("N", {257});
72 
73 /** K values to test */
74 /** The test case requires this to be multiple of 4*/
75 const auto k_values = framework::dataset::make("K", {192});
76 
77 /** Batch size values to test */
78 const auto b_values = framework::dataset::make("batch_size", {1, 2});
79 
80 /** Activation values to test */
81 const auto act_values = framework::dataset::make("Activation",
82 {
83     ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
84     ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ELU),
85 });
86 
87 /** M0 values to test - Precommit */
88 const auto m0_values_precommit = framework::dataset::make("M0", { 1, 2, 4 });
89 
90 /** N0 values to test - Precommit */
91 const auto n0_values_precommit = framework::dataset::make("N0", { 4, 8 });
92 
93 /** K0 values to test - Precommit */
94 const auto k0_values_precommit = framework::dataset::make("K0", { 1 });
95 
96 /** Broadcast bias from vector to matrix */
97 const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", { false, true } );
98 
99 } // namespace
100 
101 TEST_SUITE(CL)
TEST_SUITE(GEMMMatrixMultiplyReshapedOnlyRhsMMUL)102 TEST_SUITE(GEMMMatrixMultiplyReshapedOnlyRhsMMUL)
103 TEST_SUITE(Float)
104 TEST_SUITE(FP32)
105 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedOnlyRhsMMULFixture<float>, framework::DatasetMode::ALL,
106                 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
107                                                                    m_values,
108                                                                    n_values),
109                                                                    k_values),
110                                                                    b_values),
111                                                                    m0_values_precommit),
112                                                                    n0_values_precommit),
113                                                                    k0_values_precommit),
114                                                                    framework::dataset::make("ExportToCLImage", false)),
115                                                                    framework::dataset::make("DataType", DataType::F32)),
116                                                                    a_values),
117                                                                    beta_values),
118                                                                    broadcast_bias_values),
119                                                                    act_values))
120 {
121     // Validate output
122     if(validate_result)
123     {
124         validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
125     }
126     else
127     {
128         ARM_COMPUTE_TEST_INFO("cl_arm_matrix_multiply not supported. TEST skipped");
129         framework::ARM_COMPUTE_PRINT_INFO();
130     }
131 }
132 
133 TEST_SUITE_END() // FP32
134 
TEST_SUITE(FP16)135 TEST_SUITE(FP16)
136 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedOnlyRhsMMULFixture<half>, framework::DatasetMode::ALL,
137                 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
138                                                                    m_values,
139                                                                    n_values),
140                                                                    k_values),
141                                                                    b_values),
142                                                                    m0_values_precommit),
143                                                                    n0_values_precommit),
144                                                                    k0_values_precommit),
145                                                                    framework::dataset::make("ExportToCLImage", false)),
146                                                                    framework::dataset::make("DataType", DataType::F16)),
147                                                                    a_values),
148                                                                    beta_values),
149                                                                    broadcast_bias_values),
150                                                                    act_values))
151 {
152     // Validate output
153     if(validate_result)
154     {
155         validate(CLAccessor(_target), _reference, rel_tolerance_f16, 0.f, abs_tolerance_f16);
156     }
157     else
158     {
159         ARM_COMPUTE_TEST_INFO("cl_arm_matrix_multiply not supported. TEST skipped");
160         framework::ARM_COMPUTE_PRINT_INFO();
161     }
162 }
163 TEST_SUITE_END() // FP16
164 
TEST_SUITE(ExportToCLImage)165 TEST_SUITE(ExportToCLImage)
166 TEST_SUITE(FP32)
167 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedOnlyRhsMMULFixture<float>, framework::DatasetMode::ALL,
168                 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
169                                                                    m_values,
170                                                                    n_values),
171                                                                    k_values),
172                                                                    b_values),
173                                                                    m0_values_precommit),
174                                                                    n0_values_precommit),
175                                                                    k0_values_precommit),
176                                                                    framework::dataset::make("ExportToCLImage", true)),
177                                                                    framework::dataset::make("DataType", DataType::F32)),
178                                                                    a_values),
179                                                                    beta_values),
180                                                                    broadcast_bias_values),
181                                                                    act_values))
182 {
183     // Validate output
184     if(validate_result)
185     {
186         validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
187     }
188     else
189     {
190         ARM_COMPUTE_TEST_INFO("cl_arm_matrix_multiply not supported. TEST skipped");
191         framework::ARM_COMPUTE_PRINT_INFO();
192     }
193 }
194 
195 TEST_SUITE_END() // FP32
196 
TEST_SUITE(FP16)197 TEST_SUITE(FP16)
198 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedOnlyRhsMMULFixture<half>, framework::DatasetMode::ALL,
199                 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
200                                                                    m_values,
201                                                                    n_values),
202                                                                    k_values),
203                                                                    b_values),
204                                                                    m0_values_precommit),
205                                                                    n0_values_precommit),
206                                                                    k0_values_precommit),
207                                                                    framework::dataset::make("ExportToCLImage", true)),
208                                                                    framework::dataset::make("DataType", DataType::F16)),
209                                                                    a_values),
210                                                                    beta_values),
211                                                                    broadcast_bias_values),
212                                                                    act_values))
213 {
214     // Validate output
215     if(validate_result)
216     {
217         validate(CLAccessor(_target), _reference, rel_tolerance_f16, 0.f, abs_tolerance_f16);
218     }
219     else
220     {
221         ARM_COMPUTE_TEST_INFO("cl_arm_matrix_multiply not supported. TEST skipped");
222         framework::ARM_COMPUTE_PRINT_INFO();
223     }
224 }
225 TEST_SUITE_END() // FP16
226 TEST_SUITE_END() // ExportToCLImage
227 TEST_SUITE_END() // Float
228 TEST_SUITE_END() // GEMMMatrixMultiplyReshapedOnlyRhsMMUL
229 TEST_SUITE_END() // CL
230 } // namespace validation
231 } // namespace test
232 } // namespace arm_compute
233