1 /*
2 * Copyright (c) 2019-2020 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 #include "arm_compute/core/KernelDescriptors.h"
25 #include "arm_compute/core/Types.h"
26 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
27 #include "arm_compute/runtime/CL/CLTensor.h"
28 #include "arm_compute/runtime/CL/CLTensorAllocator.h"
29 #include "src/core/CL/kernels/CLGEMMMatrixMultiplyNativeKernel.h"
30 #include "tests/CL/CLAccessor.h"
31 #include "tests/CL/Helper.h"
32 #include "tests/PaddingCalculator.h"
33 #include "tests/datasets/ShapeDatasets.h"
34 #include "tests/framework/Asserts.h"
35 #include "tests/framework/Macros.h"
36 #include "tests/framework/datasets/Datasets.h"
37 #include "tests/validation/Validation.h"
38 #include "tests/validation/fixtures/GEMMFixture.h"
39
40 namespace arm_compute
41 {
42 namespace test
43 {
44 namespace validation
45 {
46 using namespace arm_compute::misc::shape_calculator;
47
48 // Create function for CLGEMMMatrixMultiplyNativeKernel
49 using CLGEMMMatrixMultiplyNative = CLSynthetizeFunction<CLGEMMMatrixMultiplyNativeKernel>;
50
51 // Fixture for CLGEMMMatrixMultiplyNative
52 template <typename T>
53 using CLGEMMMatrixMultiplyNativeFixture = GEMMMatrixMultiplyNativeValidationFixture<CLTensor, CLAccessor, T, CLGEMMMatrixMultiplyNative>;
54
55 // Fixture for CLGEMMMatrixMultiplyNative3D
56 template <typename T>
57 using CLGEMMMatrixMultiplyNative3DFixture = GEMMMatrixMultiplyNative3DValidationFixture<CLTensor, CLAccessor, T, CLGEMMMatrixMultiplyNative>;
58
59 namespace
60 {
61 // *INDENT-OFF*
62 // clang-format off
63 RelativeTolerance<float> rel_tolerance_f32(0.001f);
64 constexpr float abs_tolerance_f32(0.0001f);
65
66 /** Alpha values to test - Precommit */
67 const auto a_values = framework::dataset::make("alpha", {1.0f, -0.75f} );
68
69 /** Beta values to test - Precommit */
70 const auto beta_values = framework::dataset::make("beta", {-0.75f, 0.0f} );
71
72 /** M values to test */
73 const auto m_values = framework::dataset::make("M", 37);
74
75 /** M_W values to test */
76 const auto m_w_values = framework::dataset::make("M_W", 5);
77
78 /** M_H values to test */
79 const auto m_h_values = framework::dataset::make("M_H", 7);
80
81 /** N values to test */
82 const auto n_values = framework::dataset::make("N", 51);
83
84 /** K values to test */
85 const auto k_values = framework::dataset::make("K", 23);
86
87 /** Batch size values to test */
88 const auto b_values = framework::dataset::make("batch_size", 1, 3);
89
90 /** Activation values to test */
91 const auto act_values = framework::dataset::make("Activation",
92 {
93 ActivationLayerInfo(),
94 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
95 });
96
97 /** M0 values to test - Precommit */
98 const auto m0_values_precommit = framework::dataset::make("M0", { 4, 6 });
99
100 /** N0 values to test - Precommit */
101 const auto n0_values_precommit = framework::dataset::make("N0", { 4 });
102
103 /** K0 values to test - Precommit */
104 const auto k0_values_precommit = framework::dataset::make("K0", { 4 });
105
106 /** H0 values to test - Precommit */
107 const auto h0_values_precommit = framework::dataset::make("H0", 1, 3);
108
109 /** M0 values to test - Nightly */
110 const auto m0_values_nightly = framework::dataset::make("M0", 1, 8);
111
112 /** N0 values to test - Nightly */
113 const auto n0_values_nightly = framework::dataset::make("N0", { 2, 3, 4, 8 });
114
115 /** K0 values to test - Nightly */
116 const auto k0_values_nightly = framework::dataset::make("K0", { 2, 3, 4, 8 });
117
118 /** Broadcast bias from vector to matrix */
119 const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", { false, true } );
120
121 /** Boundary handling cases for testing partial/non-partial (full) block dimensions, resulting from different combinations
122 * of M, M0, N and N0 values.
123 * M0 and N0 are kept constant, while the different test cases need to vary M and N.
124 *
125 * Eg. M = 64 and N = 33 result in a block dimension that has no partial blocks (all full blocks) in Y dimension and
126 * parital blocks in X dimension.
127 */
128 const auto boundary_handling_cases = combine(combine(combine(combine(combine(combine(combine(combine(combine(
129 // Large k to force potential out-of-bound reads on input0
130 framework::dataset::make("K", 315),
131 // Batch size == 1 to force potential out-of-bound reads on input0
132 framework::dataset::make("batch_size", 1)),
133 framework::dataset::make("M0", 4)),
134 framework::dataset::make("N0", 4)),
135 framework::dataset::make("K0", 4)),
136 // Only need to test F32 as F16 shares identical boundary handling logics
137 framework::dataset::make("DataType", DataType::F32)),
138 framework::dataset::make("alpha", -0.75f )),
139 framework::dataset::make("beta", -0.35f )),
140 broadcast_bias_values),
141 framework::dataset::make("Activation", ActivationLayerInfo()));
142
143 /** Configuration test */
validate_configuration(unsigned int m_value,unsigned int n_value,unsigned int k_value,unsigned int b_value,unsigned int m0_value,unsigned int n0_value,unsigned int k0_value,bool broadcast_bias,DataType data_type,const ActivationLayerInfo & act_info)144 void validate_configuration(unsigned int m_value, unsigned int n_value, unsigned int k_value, unsigned int b_value, unsigned int m0_value, unsigned int n0_value, unsigned int k0_value, bool broadcast_bias, DataType data_type, const ActivationLayerInfo &act_info)
145 {
146 const unsigned int M = m_value;
147 const unsigned int N = n_value;
148 const unsigned int K = k_value;
149
150 GEMMLHSMatrixInfo lhs_info;
151 lhs_info.m0 = m0_value;
152 lhs_info.k0 = k0_value;
153
154 GEMMRHSMatrixInfo rhs_info;
155 rhs_info.n0 = n0_value;
156 rhs_info.k0 = k0_value;
157
158 GEMMKernelInfo kernel_info;
159 kernel_info.m = M;
160 kernel_info.n = N;
161 kernel_info.k = K;
162 kernel_info.broadcast_bias = broadcast_bias;
163 kernel_info.activation_info = act_info;
164
165 const TensorShape lhs_shape(K, M, b_value);
166 const TensorShape rhs_shape(N, K, b_value);
167 const TensorShape bias_shape(N,
168 broadcast_bias? 1 : M,
169 broadcast_bias? 1 : b_value);
170 const TensorShape dst_shape = compute_mm_shape(TensorInfo(lhs_shape, 1, data_type),
171 TensorInfo(rhs_shape, 1, data_type),
172 kernel_info);
173
174 // Create tensors
175 CLTensor lhs = create_tensor<CLTensor>(lhs_shape, data_type);
176 CLTensor rhs = create_tensor<CLTensor>(rhs_shape, data_type);
177 CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type);
178 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type);
179
180 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
181 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
182 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
183 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
184
185 // Create and configure function
186 CLGEMMMatrixMultiplyNative gemm;
187 gemm.configure(&lhs, &rhs, &bias, &dst, 1.0f, 1.0f, lhs_info, rhs_info, kernel_info);
188 }
189 } // namespace
190
191 TEST_SUITE(CL)
TEST_SUITE(GEMMMatrixMultiplyNative)192 TEST_SUITE(GEMMMatrixMultiplyNative)
193 TEST_SUITE(Float)
194 TEST_SUITE(FP32)
195 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(combine(
196 m_values,
197 n_values),
198 k_values),
199 framework::dataset::make("batch_size", 1)),
200 m0_values_precommit),
201 n0_values_precommit),
202 k0_values_precommit),
203 broadcast_bias_values),
204 act_values),
205 m_value, n_value, k_value, b_value, m0_value, n0_value, k0_value, broadcast_bias, act_value)
206 {
207 validate_configuration(m_value, n_value, k_value, b_value, m0_value, n0_value, k0_value, broadcast_bias, DataType::F32, act_value);
208 }
209
210 FIXTURE_DATA_TEST_CASE(RunSmallBoundaryHandlingPartialInXPartialInY, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
211 combine(combine(
212 framework::dataset::make("M", 3),
213 framework::dataset::make("N", 1)),
214 boundary_handling_cases))
215 {
216 // Validate output
217 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
218 }
219
220 FIXTURE_DATA_TEST_CASE(RunSmallBoundaryHandlingPartialInXFullInY, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
221 combine(combine(
222 framework::dataset::make("M", 64),
223 framework::dataset::make("N", 51)),
224 boundary_handling_cases))
225 {
226 // Validate output
227 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
228 }
229
230 FIXTURE_DATA_TEST_CASE(RunSmallBoundaryHandlingFullInXFullInY, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
231 combine(combine(
232 framework::dataset::make("M", 64),
233 framework::dataset::make("N", 32)),
234 boundary_handling_cases))
235 {
236 // Validate output
237 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
238 }
239
240 FIXTURE_DATA_TEST_CASE(RunSmallBoundaryHandlingFullInXPartialInY, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
241 combine(combine(
242 framework::dataset::make("M", 37),
243 framework::dataset::make("N", 32)),
244 boundary_handling_cases))
245 {
246 // Validate output
247 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
248 }
249
250 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
251 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
252 m_values,
253 n_values),
254 k_values),
255 b_values),
256 m0_values_precommit),
257 n0_values_precommit),
258 k0_values_precommit),
259 framework::dataset::make("DataType", DataType::F32)),
260 a_values),
261 beta_values),
262 broadcast_bias_values),
263 act_values))
264 {
265 // Validate output
266 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
267 }
268
269 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::DISABLED,
270 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
271 m_values,
272 n_values),
273 k_values),
274 b_values),
275 m0_values_nightly),
276 n0_values_nightly),
277 k0_values_nightly),
278 framework::dataset::make("DataType", DataType::F32)),
279 a_values),
280 beta_values),
281 broadcast_bias_values),
282 act_values))
283 {
284 // Validate output
285 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
286 }
287
288 FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyNative3DFixture<float>, framework::DatasetMode::ALL,
289 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
290 m_w_values,
291 m_h_values),
292 n_values),
293 k_values),
294 b_values),
295 m0_values_precommit),
296 n0_values_precommit),
297 k0_values_precommit),
298 framework::dataset::make("DataType", DataType::F32)),
299 a_values),
300 beta_values),
301 act_values))
302 {
303 // Validate output
304 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
305 }
306
307 FIXTURE_DATA_TEST_CASE(RunLarge3D, CLGEMMMatrixMultiplyNative3DFixture<float>, framework::DatasetMode::DISABLED,
308 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
309 m_w_values,
310 m_h_values),
311 n_values),
312 k_values),
313 b_values),
314 m0_values_nightly),
315 n0_values_nightly),
316 k0_values_nightly),
317 framework::dataset::make("DataType", DataType::F32)),
318 a_values),
319 beta_values),
320 act_values))
321 {
322 // Validate output
323 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
324 }
325 TEST_SUITE_END() // FP32
326 TEST_SUITE_END() // Float
327 TEST_SUITE_END() // GEMMMatrixMulipltyNative
328 TEST_SUITE_END() // CL
329 } // namespace validation
330 } // namespace test
331 } // namespace arm_compute
332