blob: f91dea1b4fc823cdedae0aaf5e01c101cc1a81e8 [file] [log] [blame]
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +00001/*
Gunes Bayir2aec5f12024-01-23 17:19:44 +00002 * Copyright (c) 2023-2024 Arm Limited.
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +00003 *
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/Types.h"
25#include "arm_compute/runtime/NEON/functions/NEMatMul.h"
26
27#include "tests/NEON/Accessor.h"
28#include "tests/framework/Asserts.h"
29#include "tests/framework/Macros.h"
30#include "tests/framework/datasets/Datasets.h"
31#include "tests/validation/Validation.h"
32
33#include "tests/datasets/LargeMatMulDataset.h"
34#include "tests/datasets/SmallMatMulDataset.h"
35#include "tests/validation/fixtures/MatMulFixture.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
Viet-Hoa Doc85edf12023-09-01 16:48:17 +010043using framework::dataset::make;
44
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000045TEST_SUITE(NEON)
46TEST_SUITE(MatMul)
47
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +010048constexpr AbsoluteTolerance<float> tolerance_fp32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for FP32 data types */
49const AbsoluteTolerance<half> tolerance_fp16(half(0.1f));
Ramy Elgammalaf150762023-04-25 17:19:27 +010050#ifdef __aarch64__
Gunes Bayir43ba0dd2024-03-19 15:31:11 +000051constexpr AbsoluteTolerance<int32_t> tolerance_qasymm8(1);
52constexpr AbsoluteTolerance<int32_t> tolerance_qasymm8_signed(1);
Ramy Elgammalaf150762023-04-25 17:19:27 +010053#endif // __aarch64__
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000054
55// clang-format off
56// *INDENT-OFF*
57// Validation Tests
Viet-Hoa Doc85edf12023-09-01 16:48:17 +010058DATA_TEST_CASE(Validate, framework::DatasetMode::ALL,
59 zip(
60 make("InputAInfo", {
61 TensorInfo(TensorShape(9U, 6U), 1, DataType::F32), // Mismatching datatype
62 TensorInfo(TensorShape(9U, 6U), 1, DataType::S32), // Unsupported datatypes
63 TensorInfo(TensorShape(9U, 6U, 2U), 1, DataType::F32), // Broadcasting in batch dimension not supported
64 TensorInfo(TensorShape(9U, 6U), 1, DataType::F32), // Invalid shape for multiplication
65 TensorInfo(TensorShape(9U, 6U), 1, DataType::F32),
66 TensorInfo(TensorShape(9U, 6U , 12U) , 1 , DataType::F32),
67 TensorInfo(TensorShape(9U, 6U , 12U) , 1 , DataType::F32), // Tensors are not dynamic
68 TensorInfo(TensorShape(9U, 6U), 1, DataType::QASYMM8),
69 TensorInfo(TensorShape(9U, 6U), 1, DataType::QASYMM8_SIGNED),
70 TensorInfo(TensorShape(9U, 6U), 1, DataType::QASYMM8_SIGNED), // Mismatching data type
71 }),
72 make("InputBInfo", {
73 TensorInfo(TensorShape(5U, 9U), 1, DataType::QASYMM8),
74 TensorInfo(TensorShape(5U, 9U), 1, DataType::S32),
75 TensorInfo(TensorShape(5U, 9U, 1U), 1, DataType::F32),
76 TensorInfo(TensorShape(5U, 12U), 1, DataType::F32),
77 TensorInfo(TensorShape(5U, 9U), 1, DataType::F32),
78 TensorInfo(TensorShape(5U, 9U, 12U), 1, DataType::F32),
79 TensorInfo(TensorShape(5U, 9U, 12U), 1, DataType::F32),
80 TensorInfo(TensorShape(5U, 9U), 1, DataType::QASYMM8),
81 TensorInfo(TensorShape(5U, 9U), 1, DataType::QASYMM8_SIGNED),
82 TensorInfo(TensorShape(5U, 9U), 1, DataType::QASYMM8_SIGNED),
83 }),
84 make("OutputInfo", {
85 TensorInfo(TensorShape(5U, 6U), 1, DataType::F32),
86 TensorInfo(TensorShape(5U, 6U), 1, DataType::S32),
87 TensorInfo(TensorShape(5U, 6U, 2U), 1, DataType::F32),
88 TensorInfo(TensorShape(5U, 6U), 1, DataType::F32),
89 TensorInfo(TensorShape(5U, 6U), 1, DataType::F32),
90 TensorInfo(TensorShape(5U, 6U, 12U) , 1, DataType::F32),
91 TensorInfo(TensorShape(5U, 6U, 12U) , 1, DataType::F32),
92 TensorInfo(TensorShape(5U, 6U), 1, DataType::QASYMM8),
93 TensorInfo(TensorShape(5U, 6U), 1, DataType::QASYMM8_SIGNED),
94 TensorInfo(TensorShape(5U, 6U), 1, DataType::QASYMM8),
95 }),
96 make("TensorIsConst", {false, false, false, false, false , false, true, false, false, false}),
97 make("Expected", { false, false, false, false, true, true, false, true, true, false })),
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000098 a_info, b_info, output_info, are_tensors_const, expected)
99{
100 TensorInfo a{a_info};
101 TensorInfo b{b_info};
102 a.set_are_values_constant(are_tensors_const);
103 b.set_are_values_constant(are_tensors_const);
104 Status status = NEMatMul::validate(&a,
105 &b,
106 &output_info,
107 MatMulInfo(),
108 CpuMatMulSettings());
109 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
110}
111// *INDENT-ON*
112// clang-format on
113
114// Generic Template
115template <typename T>
116using NEMatMulFixture = MatMulValidationWithActivationFixture<Tensor, Accessor, NEMatMul, CpuMatMulSettings, T>;
117
118// Fast math Template
119template <typename T>
120using NEMatMulFastMathFixture = MatMulGenericValidationFixture<Tensor, Accessor, NEMatMul, CpuMatMulSettings, T>;
121
122template <typename T>
123using NEMatMulDynamicTensorsFixture = MatMulValidationWithDynamicTensorsFixture<Tensor, Accessor, NEMatMul, CpuMatMulSettings, T>;
124
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100125template <typename T>
126using NEQuantizedMatMulFixture = QuantizedMatMulValidationFixture<Tensor, Accessor, NEMatMul, CpuMatMulSettings, T>;
127
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000128TEST_SUITE(Float)
129TEST_SUITE(FP32)
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100130FIXTURE_DATA_TEST_CASE(RunSmall, NEMatMulFixture<float>, framework::DatasetMode::PRECOMMIT,
131 combine(
132 datasets::SmallMatMulDataset(),
133 make("TransposeA", { false, true }),
134 make("TransposeB", { false, true }),
135 make("DataType", DataType::F32),
136 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) })))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000137{
138 // Validate output
139 validate(Accessor(_target), _reference, tolerance_fp32);
140}
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100141FIXTURE_DATA_TEST_CASE(RunLarge, NEMatMulFixture<float>, framework::DatasetMode::NIGHTLY,
142 combine(
143 datasets::LargeMatMulDataset(),
144 make("TransposeA", { false, true }),
145 make("TransposeB", { false, true }),
146 make("DataType", DataType::F32),
147 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) })))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000148{
149 // Validate output
150 validate(Accessor(_target), _reference, tolerance_fp32);
151}
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100152FIXTURE_DATA_TEST_CASE(RunHighDimensions, NEMatMulFixture<float>, framework::DatasetMode::NIGHTLY,
153 combine(
154 datasets::HighDimensionalMatMulDataset(),
155 make("TransposeA", { false, true }),
156 make("TransposeB", { false, true }),
157 make("DataType", DataType::F32),
158 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) })))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000159{
160 // Validate output
161 validate(Accessor(_target), _reference, tolerance_fp32);
162}
163
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100164FIXTURE_DATA_TEST_CASE(RunStressDynamicTensors, NEMatMulDynamicTensorsFixture<float>, framework::DatasetMode::PRECOMMIT,
165 combine(
166 datasets::SmallMatMulDataset(),
167 make("TransposeA", { false, true }),
168 make("TransposeB", { false, true }),
169 make("DataType", DataType::F32),
170 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) }),
171 make("NumberOfRuns", 5)))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000172{
173 // Validate output
174 validate(Accessor(_target), _reference, tolerance_fp32);
175}
176TEST_SUITE_END() // FP32
177
178#ifdef ARM_COMPUTE_ENABLE_BF16
179/* Note : MatMul BF16 is enabled by specifying FP32 datatype and enabling the fast math setting */
Gunes Bayir2aec5f12024-01-23 17:19:44 +0000180constexpr AbsoluteTolerance<float> tolerance_bf16(0.02f);
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000181TEST_SUITE(BF16)
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100182FIXTURE_DATA_TEST_CASE(RunSmall, NEMatMulFastMathFixture<float>, framework::DatasetMode::PRECOMMIT,
183 combine(
184 datasets::SmallMatMulDataset(),
185 make("TransposeA", { false, true }),
186 make("TransposeB", { false, true }),
187 make("DataType", DataType::F32),
188 make("ActivationInfo", { ActivationLayerInfo() }),
189 make("RunTimes", { 0 }),
190 make("Settings", { CpuMatMulSettings().fast_math(true) }),
191 make("LhsQInfo", { QuantizationInfo() }),
192 make("RhsQInfo", { QuantizationInfo() }),
193 make("OutQInfo", { QuantizationInfo() }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100194)
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000195{
196 // Validate output
197 validate(Accessor(_target), _reference, tolerance_bf16);
198}
Gunes Bayir2aec5f12024-01-23 17:19:44 +0000199
200FIXTURE_DATA_TEST_CASE(RunLarge, NEMatMulFastMathFixture<float>, framework::DatasetMode::NIGHTLY,
201 combine(
202 datasets::LargeMatMulDataset(),
203 make("TransposeA", { false, true }),
204 make("TransposeB", { false, true }),
205 make("DataType", DataType::F32),
206 make("ActivationInfo", { ActivationLayerInfo() }),
207 make("RunTimes", { 0 }),
208 make("Settings", { CpuMatMulSettings().fast_math(true) }),
209 make("LhsQInfo", { QuantizationInfo() }),
210 make("RhsQInfo", { QuantizationInfo() }),
211 make("OutQInfo", { QuantizationInfo() }))
212)
213{
214 // Validate output
215 validate(Accessor(_target), _reference, tolerance_bf16, 0.01 /* tolerance_num */);
216}
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000217TEST_SUITE_END() // BF16
218#endif /* ARM_COMPUTE_ENABLE_BF16 */
219
220#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
221TEST_SUITE(FP16)
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100222FIXTURE_DATA_TEST_CASE(RunSmall, NEMatMulFixture<half>, framework::DatasetMode::PRECOMMIT,
223 combine(
224 datasets::SmallMatMulDataset(),
225 make("TransposeA", { false, true }),
226 make("TransposeB", { false, true }),
227 make("DataType", DataType::F16),
228 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) })))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000229{
230 // Validate output
231 validate(Accessor(_target), _reference, tolerance_fp16);
232}
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100233FIXTURE_DATA_TEST_CASE(RunLarge, NEMatMulFixture<half>, framework::DatasetMode::NIGHTLY,
234 combine(
235 datasets::LargeMatMulDataset(),
236 make("TransposeA", { false, true }),
237 make("TransposeB", { false, true }),
238 make("DataType", DataType::F16),
239 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) })))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000240{
241 // Validate output
242 validate(Accessor(_target), _reference, tolerance_fp16);
243}
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100244FIXTURE_DATA_TEST_CASE(RunStressDynamicTensors, NEMatMulDynamicTensorsFixture<half>, framework::DatasetMode::PRECOMMIT,
245 combine(
246 datasets::SmallMatMulDataset(),
247 make("TransposeA", { false, true }),
248 make("TransposeB", { false, true }),
249 make("DataType", DataType::F16),
250 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) }),
251 make("NumberOfRuns", 5)))
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000252{
253 // Validate output
254 validate(Accessor(_target), _reference, tolerance_fp16);
255}
256TEST_SUITE_END() // FP16
257#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
258
259TEST_SUITE_END() // Float
260
Ramy Elgammal05a65e32023-04-24 01:58:21 +0100261#ifdef __aarch64__ // All the GeMM CPU assembly kernels for integer datatypes require aarch64
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100262TEST_SUITE(Quantized)
263
264TEST_SUITE(QASYMM8)
265
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100266FIXTURE_DATA_TEST_CASE(RunSmall, NEQuantizedMatMulFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
267 combine(
268 datasets::SmallMatMulDataset(),
269 make("TransposeA", { false, true }),
270 make("TransposeB", { false, true }),
271 make("DataType", DataType::QASYMM8),
272 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) }),
273 make("NumberOfExtraRuns", { 0, 1 }),
274 make("LhsQInfo", { QuantizationInfo(1.f / 50, 1) }),
275 make("RhsQInfo", { QuantizationInfo(1.f / 30, -1) }),
276 make("OutQInfo", { QuantizationInfo(1.f, 2) }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100277)
278{
279 // Validate output
280 validate(Accessor(_target), _reference, tolerance_qasymm8);
281}
282
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100283FIXTURE_DATA_TEST_CASE(RunSmallExtraActivation, NEQuantizedMatMulFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
284 combine(
285 datasets::SmallerMatMulDataset(),
286 make("TransposeA", { false, true }),
287 make("TransposeB", { false, true }),
288 make("DataType", DataType::QASYMM8),
289 make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) }),
290 make("NumberOfExtraRuns", { 0, 1 }),
291 make("LhsQInfo", { QuantizationInfo(1.f / 50, 1) }),
292 make("RhsQInfo", { QuantizationInfo(1.f / 30, -1) }),
293 make("OutQInfo", { QuantizationInfo(1.f, 2) }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100294)
295{
296 // Validate output
297 validate(Accessor(_target), _reference, tolerance_qasymm8);
298}
299
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100300FIXTURE_DATA_TEST_CASE(RunLarge, NEQuantizedMatMulFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
301 combine(
302 datasets::LargeMatMulDataset(),
303 make("TransposeA", { false, true }),
304 make("TransposeB", { false, true }),
305 make("DataType", DataType::QASYMM8),
306 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) }),
307 make("NumberOfExtraRuns", { 0, 1 }),
308 make("LhsQInfo", { QuantizationInfo(1.f / 100, 1) }),
309 make("RhsQInfo", { QuantizationInfo(1.f / 200, -1) }),
310 make("OutQInfo", { QuantizationInfo(1.f, 2) }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100311)
312{
313 // Validate output
314 validate(Accessor(_target), _reference, tolerance_qasymm8);
315}
316
317TEST_SUITE_END() // QASYMM8
318
319TEST_SUITE(QASYMM8_SIGNED)
320
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100321FIXTURE_DATA_TEST_CASE(RunSmall, NEQuantizedMatMulFixture<int8_t>, framework::DatasetMode::PRECOMMIT,
322 combine(
323 datasets::SmallMatMulDataset(),
324 make("TransposeA", { false, true }),
325 make("TransposeB", { false, true }),
326 make("DataType", DataType::QASYMM8_SIGNED),
327 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) }),
328 make("NumberOfExtraRuns", { 0, 1 }),
329 make("LhsQInfo", { QuantizationInfo(1.f / 40, -2) }),
330 make("RhsQInfo", { QuantizationInfo(1.f / 50, 1) }),
331 make("OutQInfo", { QuantizationInfo(1.f, 1) }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100332)
333{
334 // Validate output
335 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
336}
337
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100338FIXTURE_DATA_TEST_CASE(RunSmallExtraActivation, NEQuantizedMatMulFixture<int8_t>, framework::DatasetMode::NIGHTLY,
339 combine(
340 datasets::SmallerMatMulDataset(),
341 make("TransposeA", { false, true }),
342 make("TransposeB", { false, true }),
343 make("DataType", DataType::QASYMM8_SIGNED),
344 make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) }),
345 make("NumberOfExtraRuns", { 0, 1 }),
346 make("LhsQInfo", { QuantizationInfo(1.f / 40, -2) }),
347 make("RhsQInfo", { QuantizationInfo(1.f / 50, 1) }),
348 make("OutQInfo", { QuantizationInfo(1.f, 1) }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100349)
350{
351 // Validate output
352 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
353}
354
Viet-Hoa Doc85edf12023-09-01 16:48:17 +0100355FIXTURE_DATA_TEST_CASE(RunLarge, NEQuantizedMatMulFixture<int8_t>, framework::DatasetMode::NIGHTLY,
356 combine(
357 datasets::LargeMatMulDataset(),
358 make("TransposeA", { false, true }),
359 make("TransposeB", { false, true }),
360 make("DataType", DataType::QASYMM8_SIGNED),
361 make("ActivationInfo", { ActivationLayerInfo(), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) }),
362 make("NumberOfExtraRuns", { 0, 1 }),
363 make("LhsQInfo", { QuantizationInfo(1.f / 150, -2) }),
364 make("RhsQInfo", { QuantizationInfo(1.f / 250, 1) }),
365 make("OutQInfo", { QuantizationInfo(1.f, 1) }))
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100366)
367{
368 // Validate output
369 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
370}
371
372TEST_SUITE_END() // QASYMM8_SIGNED
373
374TEST_SUITE_END() // Quantized
Ramy Elgammal05a65e32023-04-24 01:58:21 +0100375#endif // __aarch64__
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100376
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000377TEST_SUITE_END() // MatMul
378TEST_SUITE_END() // NEON
379} // namespace validation
380} // namespace test
381} // namespace arm_compute