blob: 8497dd14cf079eaae36bc298917fccc25eacdd2a [file] [log] [blame]
Pablo Tello299025a2017-09-29 11:30:12 +01001/*
SiCong Li11ab4512023-11-07 12:04:59 +00002 * Copyright (c) 2017-2024 Arm Limited.
Pablo Tello299025a2017-09-29 11:30:12 +01003 *
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"
Gian Marcoe75a02b2017-11-08 12:24:09 +000025#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
26#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
Pablo Tello299025a2017-09-29 11:30:12 +010027#include "arm_compute/runtime/Tensor.h"
28#include "arm_compute/runtime/TensorAllocator.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010029#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/cpu/operators/CpuGemmLowpMatrixMultiplyCore.h"
Pablo Tello299025a2017-09-29 11:30:12 +010031#include "tests/NEON/Accessor.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010032#include "tests/NEON/Helper.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000033#include "tests/PaddingCalculator.h"
George Wort2d7e6832019-02-22 16:37:41 +000034#include "tests/datasets/GEMMLowpFusedOffsetOutputDataset.h"
Gian Marcofa4cacd2017-10-18 17:05:02 +010035#include "tests/datasets/LargeGEMMLowpDataset.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000036#include "tests/datasets/ShapeDatasets.h"
Gian Marcofa4cacd2017-10-18 17:05:02 +010037#include "tests/datasets/SmallGEMMLowpDataset.h"
Pablo Tello299025a2017-09-29 11:30:12 +010038#include "tests/framework/Asserts.h"
39#include "tests/framework/Macros.h"
40#include "tests/framework/datasets/Datasets.h"
41#include "tests/validation/Validation.h"
42#include "tests/validation/fixtures/GEMMLowpFixture.h"
43
44namespace arm_compute
45{
46namespace test
47{
48namespace validation
49{
Radu Salavatf1f1f872024-02-27 18:32:26 +000050using framework::dataset::make;
51
52namespace
53{
54 constexpr AbsoluteTolerance<float> tolerance_batched(1);
55 constexpr AbsoluteTolerance<float> tolerance_quant(1);
56} // namespace
57
58
Pablo Tello299025a2017-09-29 11:30:12 +010059TEST_SUITE(NEON)
60TEST_SUITE(GEMMLowp)
Gian Marcoe75a02b2017-11-08 12:24:09 +000061TEST_SUITE(MatrixMultiplyCore)
SiCong Li11ab4512023-11-07 12:04:59 +000062
Gian Marcoe75a02b2017-11-08 12:24:09 +000063using NEGEMMLowpMatrixMultiplyCoreFixture = GEMMLowpMatrixMultiplyCoreValidationFixture<Tensor, Accessor, NEGEMMLowpMatrixMultiplyCore>;
Radu Salavatf1f1f872024-02-27 18:32:26 +000064using NEGEMMLowpMatrixMultiplyCoreAccumulateFixture = GEMMLowpMatrixMultiplyAccumulateValidationFixture<Tensor, Accessor, NEGEMMLowpMatrixMultiplyCore>;
SiCong Li11ab4512023-11-07 12:04:59 +000065
morgolock4adaddb2020-09-29 14:24:32 +010066DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, framework::dataset::concat(datasets::SmallGEMMLowpDataset(), datasets::LargeGEMMLowpDataset()),
67 shape_a, shape_b, shape_c, a_offset, b_offset)
68{
69 // Create tensors
70 Tensor a = create_tensor<Tensor>(shape_a, DataType::QASYMM8);
71 Tensor b = create_tensor<Tensor>(shape_b, DataType::QASYMM8);
72 Tensor c = create_tensor<Tensor>(shape_c, DataType::S32);
73
74 a.info()->set_quantization_info(QuantizationInfo(1.0f / 255, a_offset));
75 b.info()->set_quantization_info(QuantizationInfo(1.0f / 255, b_offset));
76
77 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
78 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
79 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
80
81 // Create and configure function
82 NEGEMMLowpMatrixMultiplyCore gemmlowp_mm;
83 gemmlowp_mm.configure(&a, &b, nullptr, &c);
84
85 // Validate padding is zero
86 validate(a.info()->padding(), PaddingSize());
87 validate(b.info()->padding(), PaddingSize());
88 validate(c.info()->padding(), PaddingSize());
89}
90
Radu Salavatf1f1f872024-02-27 18:32:26 +000091DATA_TEST_CASE(ValidateAccumulate, framework::DatasetMode::ALL, combine(
92 zip(
93 make("In0",{ TensorShape(21U, 1U) }),
94 make("In1", { TensorShape(1U, 21U) }),
95 make("Dst", { TensorShape(1U, 1U) }),
96 make("a_offset", { -2 }),
97 make("a_offset", { 13 })
98 ),
99 zip(
100 make("OutputDataType", { DataType::S32, DataType::QASYMM8, DataType::QASYMM8_SIGNED}),
101 make("Expected", { true, false, false })
102 )),
103 shape_a, shape_b, shape_dst, a_offset, b_offset, output_data_type, expected)
104{
105 DataType input_data_type = (output_data_type == DataType::S32 ? DataType::QASYMM8 : output_data_type);
106 // Accumulation test for GEMM kernels
107 TensorInfo a(shape_a, 1, input_data_type, QuantizationInfo(1.0f / 255, a_offset));
108 TensorInfo b(shape_b, 1, input_data_type, QuantizationInfo(1.0f / 255, b_offset));
109 TensorInfo dst(shape_dst, 1, output_data_type, QuantizationInfo());
110
111 // Create and configure function
112 GEMMInfo gemm_info = GEMMInfo();
113 gemm_info.set_accumulate(true);
114
115 if (is_data_type_quantized(output_data_type))
116 {
117 GEMMLowpOutputStageInfo gemmLowpOutputStageInfo = GEMMLowpOutputStageInfo();
118 gemmLowpOutputStageInfo.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
119
120 gemm_info.set_gemmlowp_output_stage(gemmLowpOutputStageInfo);
121 }
122
123 cpu::CpuGemmLowpMatrixMultiplyCore gemmlowp_mm;
124 Status status = gemmlowp_mm.validate(&a, &b, nullptr, &dst, gemm_info);
125
126 ARM_COMPUTE_EXPECT((expected == bool(status)), framework::LogLevel::ERRORS);
127}
128
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000129// *INDENT-OFF*
130// clang-format off
SiCong Li11ab4512023-11-07 12:04:59 +0000131DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(
132 make("InputAInfo", { TensorInfo(TensorShape(21U, 13U), 1, DataType::QASYMM8, QuantizationInfo(1.f/255, 10)), // Input not a multiple of 4
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100133 TensorInfo(TensorShape(21U, 13U), 1, DataType::S32), // Mismatching data type
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000134 TensorInfo(TensorShape(20U, 13U), 1, DataType::QASYMM8, QuantizationInfo(1.f/255, 10)), // Invalid dimensions
135 TensorInfo(TensorShape(21U, 13U), 1, DataType::QASYMM8, QuantizationInfo(1.f/255, 10)), // Invalid dimensions
136 TensorInfo(TensorShape(16U, 32U), 1, DataType::QASYMM8, QuantizationInfo(1.f/255, 10)),
137 }),
SiCong Li11ab4512023-11-07 12:04:59 +0000138 make("InputBInfo",{ TensorInfo(TensorShape(33U, 21U), 1, DataType::QASYMM8, QuantizationInfo(1.f/256, 10)),
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000139 TensorInfo(TensorShape(33U, 21U), 1, DataType::QASYMM8, QuantizationInfo(1.f/256, 10)),
140 TensorInfo(TensorShape(33U, 21U), 1, DataType::QASYMM8, QuantizationInfo(1.f/256, 10)),
141 TensorInfo(TensorShape(33U, 21U), 1, DataType::QASYMM8, QuantizationInfo(1.f/256, 10)),
142 TensorInfo(TensorShape(64U, 16U), 1, DataType::QASYMM8, QuantizationInfo(1.f/256, 10)),
SiCong Li11ab4512023-11-07 12:04:59 +0000143 }),
144 make("OutputInfo",{ TensorInfo(TensorShape(33U, 13U), 1, DataType::S32),
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000145 TensorInfo(TensorShape(33U, 13U), 1, DataType::S32),
146 TensorInfo(TensorShape(33U, 13U), 1, DataType::S32),
147 TensorInfo(TensorShape(8U, 11U), 1, DataType::S32),
148 TensorInfo(TensorShape(64U, 32U), 1, DataType::S32),
SiCong Li11ab4512023-11-07 12:04:59 +0000149 }),
150 make("Expected", { true, false, false, false, true })),
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000151 a_info, b_info, output_info, expected)
152{
153 // Lock tensors
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000154 Status status = NEGEMMLowpMatrixMultiplyCore::validate(&a_info.clone()->set_is_resizable(false),
155 &b_info.clone()->set_is_resizable(false),
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100156 nullptr,
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000157 &output_info.clone()->set_is_resizable(false));
158 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000159}
160// clang-format on
161// *INDENT-ON*
162
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100163/** Test case for memory injection in @ref cpu::CpuGemmLowpMatrixMultiplyCore.
164 *
165 * Configure the operator once and inject memory at run-time in multiple executions.
166 *
167 * Checks performed in order:
168 * - Both runs compute the same output
169 */
170TEST_CASE(MemoryInjection, framework::DatasetMode::ALL)
171{
172 auto gemm = std::make_unique<cpu::CpuGemmLowpMatrixMultiplyCore>();
173 auto a_info = TensorInfo(TensorShape(32U, 72U), 1, DataType::QASYMM8);
174 auto b_info = TensorInfo(TensorShape(17U, 32U), 1, DataType::QASYMM8);
175 auto dst_info = TensorInfo(TensorShape(17U, 72U), 1, DataType::S32);
176 a_info.set_quantization_info(QuantizationInfo(1.0f / 255, -9));
177 b_info.set_quantization_info(QuantizationInfo(1.0f / 255, 1));
178 const auto gemm_info = GEMMInfo{};
179 gemm->configure(&a_info, &b_info, nullptr, &dst_info, gemm_info);
180
181 // telhs are newly created every call of this lambda function
182 auto a = create_tensor<Tensor>(a_info);
183 auto b = create_tensor<Tensor>(b_info);
184 auto dst = create_tensor<Tensor>(dst_info);
185 a.allocator()->allocate();
186 b.allocator()->allocate();
187 dst.allocator()->allocate();
188
189 ITensorPack run_pack =
190 {
191 { TensorType::ACL_SRC_0, &a },
192 { TensorType::ACL_SRC_1, &b },
193 { TensorType::ACL_DST, &dst }
194 };
195 ITensorPack prep_pack =
196 {
197 { TensorType::ACL_SRC_1, &b },
198 };
199
200 auto mg = MemoryGroup{};
201 auto ws = manage_workspace<Tensor>(gemm->workspace(), mg, run_pack, prep_pack);
202
203 auto run_conv = [&]() -> Tensor
204 {
205 auto dst = create_tensor<Tensor>(dst_info);
206 dst.allocator()->allocate();
207 run_pack.add_tensor(TensorType::ACL_DST, &dst);
208
209 library->fill_tensor_value(Accessor(a), static_cast<uint8_t>(1));
210 library->fill_tensor_value(Accessor(b), static_cast<uint8_t>(2));
211 // This operator is configured once and captured by this lambda.
212 gemm->prepare(prep_pack);
213 gemm->run(run_pack);
214 return dst;
215 };
216 auto result_0 = run_conv();
217 auto result_1 = run_conv();
218 for(size_t i = 0; i < result_0.info()->tensor_shape().total_size(); ++i)
219 {
220 ARM_COMPUTE_EXPECT(((uint8_t *)result_0.buffer())[i] == ((uint8_t *)result_1.buffer())[i], framework::LogLevel::ERRORS);
221 }
222}
223
224/** Test case for memory injection in @ref NEGEMMLowpMatrixMultiplyCore.
225 *
226 * Make sure @ref NEGEMMLowpMatrixMultiplyCore still works through injecting the memory at configure time using the old API.
227 *
228 * Checks performed in order:
229 * - Both runs compute the same output
230 */
231TEST_CASE(MultipleExecutionWithConfigure, framework::DatasetMode::ALL)
232{
233 auto gemm = std::make_unique<NEGEMMLowpMatrixMultiplyCore>();
234 auto a_info = TensorInfo(TensorShape(32U, 72U), 1, DataType::QASYMM8);
235 auto b_info = TensorInfo(TensorShape(17U, 32U), 1, DataType::QASYMM8);
236 auto dst_info = TensorInfo(TensorShape(17U, 72U), 1, DataType::S32);
237 a_info.set_quantization_info(QuantizationInfo(1.0f / 255, -9));
238 b_info.set_quantization_info(QuantizationInfo(1.0f / 255, 1));
239 const auto gemm_info = GEMMInfo{};
240 auto run_conv = [&]()
241 {
242 auto a = create_tensor<Tensor>(a_info);
243 auto b = create_tensor<Tensor>(b_info);
244 auto dst = create_tensor<Tensor>(dst_info);
245 gemm->configure(&a, &b, nullptr, &dst, gemm_info);
246 a.allocator()->allocate();
247 b.allocator()->allocate();
248 dst.allocator()->allocate();
249 library->fill_tensor_value(Accessor(a), static_cast<uint8_t>(1));
250 library->fill_tensor_value(Accessor(b), static_cast<uint8_t>(2));
251 gemm->run();
252 return dst;
253 };
254 auto result_0 = run_conv();
255 auto result_1 = run_conv();
256 for(size_t i = 0; i < result_0.info()->tensor_shape().total_size(); ++i)
257 {
258 ARM_COMPUTE_EXPECT(((uint8_t *)result_0.buffer())[i] == ((uint8_t *)result_1.buffer())[i], framework::LogLevel::ERRORS);
259 }
260}
261
Gian Marcoe75a02b2017-11-08 12:24:09 +0000262FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMLowpMatrixMultiplyCoreFixture, framework::DatasetMode::ALL, datasets::SmallGEMMLowpDataset())
Pablo Tello299025a2017-09-29 11:30:12 +0100263{
264 // Validate output
Gian Marcofa4cacd2017-10-18 17:05:02 +0100265 validate(Accessor(_target), _reference);
266}
267
Gian Marcoe75a02b2017-11-08 12:24:09 +0000268FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMLowpMatrixMultiplyCoreFixture, framework::DatasetMode::NIGHTLY, datasets::LargeGEMMLowpDataset())
Gian Marcofa4cacd2017-10-18 17:05:02 +0100269{
270 // Validate output
271 validate(Accessor(_target), _reference);
Pablo Tello299025a2017-09-29 11:30:12 +0100272}
Pablo Tello299025a2017-09-29 11:30:12 +0100273
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100274TEST_SUITE(BatchedMatMul)
275TEST_SUITE(QASYMM8)
Radu Salavatf1f1f872024-02-27 18:32:26 +0000276using NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixtureBatchedUnsigned =
277 GEMMLowpBatchedMatrixMultiplyCoreFusedOffsetOutputFixture<Tensor, Accessor, NEGEMMLowpMatrixMultiplyCore, false, false, uint8_t, uint8_t, true>;
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100278FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixtureBatchedUnsigned, framework::DatasetMode::ALL,
SiCong Li11ab4512023-11-07 12:04:59 +0000279 combine(datasets::SmallGEMMLowpFusedBatchedMatMulDataset(),
280 make("DataType", { DataType::QASYMM8 }),
281 make("reshape_b_only_on_first_run", { false })))
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100282{
283 validate(Accessor(_target), _reference, tolerance_batched);
284}
285TEST_SUITE_END() // QASYMM8
286
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100287TEST_SUITE(QASYMM8_SIGNED)
Radu Salavatf1f1f872024-02-27 18:32:26 +0000288using NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixtureBatchedSigned =
289 GEMMLowpBatchedMatrixMultiplyCoreFusedOffsetOutputFixture<Tensor, Accessor, NEGEMMLowpMatrixMultiplyCore, false, false, int8_t, int8_t, true>;
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100290FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixtureBatchedSigned, framework::DatasetMode::ALL,
SiCong Li11ab4512023-11-07 12:04:59 +0000291 combine(datasets::SmallGEMMLowpFusedBatchedMatMulDataset(),
292 make("DataType", { DataType::QASYMM8_SIGNED }),
293 make("reshape_b_only_on_first_run", { false })))
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100294{
295 validate(Accessor(_target), _reference, tolerance_batched);
296}
297TEST_SUITE_END() // QASYMM8_SIGNED
298TEST_SUITE_END() // BatchedMatMul
299
George Wort2d7e6832019-02-22 16:37:41 +0000300TEST_SUITE(FusedOffsetOutput)
Radu Salavatf1f1f872024-02-27 18:32:26 +0000301using NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixture = GEMMLowpMatrixMultiplyCoreFusedOffsetOutputValidationFixture<Tensor, Accessor, NEGEMMLowpMatrixMultiplyCore>;
SiCong Li11ab4512023-11-07 12:04:59 +0000302FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixture, framework::DatasetMode::ALL,
303 combine(datasets::SmallGEMMLowpFusedOffsetOutputUint8Dataset(),
Radu Salavatf1f1f872024-02-27 18:32:26 +0000304 make("DataType", { DataType::QASYMM8 }),
305 make("reshape_b_only_on_first_run", { false })))
George Wort2d7e6832019-02-22 16:37:41 +0000306{
307 // Validate output
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100308 validate(Accessor(_target), _reference, tolerance_quant);
George Wort2d7e6832019-02-22 16:37:41 +0000309}
SiCong Li11ab4512023-11-07 12:04:59 +0000310FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMLowpMatrixMultiplyCoreFusedOffsetOutputFixture, framework::DatasetMode::NIGHTLY,
311 combine(datasets::LargeGEMMLowpFusedOffsetOutputUint8Dataset(),
Radu Salavatf1f1f872024-02-27 18:32:26 +0000312 make("DataType", { DataType::QASYMM8 }),
313 make("reshape_b_only_on_first_run", { false })))
George Wort2d7e6832019-02-22 16:37:41 +0000314{
315 // Validate output
Mohammed Suhail Munshi97a609b2022-10-21 11:15:54 +0100316 validate(Accessor(_target), _reference, tolerance_quant);
George Wort2d7e6832019-02-22 16:37:41 +0000317}
318TEST_SUITE_END() // FusedOffsetOutput
Radu Salavatf1f1f872024-02-27 18:32:26 +0000319
Radu Salavatcdce25b2024-04-12 12:26:50 +0000320// accumulation is not supported for Int8/UInt8 in aarch32
321#ifdef __aarch64__
Radu Salavatf1f1f872024-02-27 18:32:26 +0000322TEST_SUITE(ACCUMULATION)
323TEST_SUITE(S32)
324FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMLowpMatrixMultiplyCoreAccumulateFixture, framework::DatasetMode::ALL, datasets::SmallGEMMLowpDataset())
325{
326 // Validate output
327 validate(Accessor(_target), _reference);
328}
329FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMLowpMatrixMultiplyCoreAccumulateFixture, framework::DatasetMode::NIGHTLY, datasets::LargeGEMMLowpDataset())
330{
331 // Validate output
332 validate(Accessor(_target), _reference);
333}
334TEST_SUITE_END() // S32
335TEST_SUITE_END() // ACCUMULATION
Radu Salavatcdce25b2024-04-12 12:26:50 +0000336#endif // __arch64__
Radu Salavatf1f1f872024-02-27 18:32:26 +0000337
Gian Marcoe75a02b2017-11-08 12:24:09 +0000338TEST_SUITE_END() // MatrixMultiplyCore
Gian Marcoe75a02b2017-11-08 12:24:09 +0000339TEST_SUITE_END() // GEMMLowp
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100340TEST_SUITE_END() // NEON
Pablo Tello299025a2017-09-29 11:30:12 +0100341} // namespace validation
342} // namespace test
343} // namespace arm_compute