blob: 5932fa7c218691adcfaa6f528e9067a66e0c961c [file] [log] [blame]
Gunes Bayir9d0c4de2023-04-13 18:22:58 +01001/*
2 * Copyright (c) 2023 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 "arm_compute/runtime/CL/CLTensor.h"
26
27#include "src/gpu/cl/kernels/ClMatMulLowpNativeKernel.h"
28
29#include "tests/datasets/LargeMatMulDataset.h"
30#include "tests/datasets/SmallMatMulDataset.h"
31#include "tests/framework/Macros.h"
32#include "tests/framework/datasets/Datasets.h"
33#include "tests/validation/Validation.h"
34#include "tests/validation/fixtures/MatMulKernelFixture.h"
35#include "tests/validation/reference/Permute.h"
36
37#include <tuple>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
47constexpr AbsoluteTolerance<float> tolerance_quant(1); /**< Tolerance value for comparing reference's output against implementation's output for quantized data types */
48}
49template <typename T>
50using CLMatMulLowpNativeKernelFixture = MatMulKernelValidationFixture<T, ClMatMulLowpNativeKernel>;
51
52/** M0 values to test --precommit*/
53const auto m0_values_precommit = framework::dataset::make("M0", { 1, 3 });
54
55/** N0 values to test --precommit*/
56const auto n0_values_precommit = framework::dataset::make("N0", { 2, 4 });
57
58/** K0 values to test --precommit*/
59const auto k0_values_precommit = framework::dataset::make("K0", { 2, 3 });
60
61/** M0 values to test --nightly*/
62const auto m0_values_nightly_lhs_nt = framework::dataset::make("M0", { 1, 2, 3, 4, 5, 6, 7, 8 });
63const auto m0_values_nightly_lhs_t = framework::dataset::make("M0", { 1, 2, 3, 4, 8 });
64
65/** N0 values to test --nightly*/
66const auto n0_values_nightly_rhs_nt = framework::dataset::make("N0", { 1, 2, 3, 4, 8, 16 });
67// const auto n0_values_nightly_rhs_t = framework::dataset::make("N0", { 1, 2, 3, 4, 8 });
68
69/** K0 values to test --nightly*/
70const auto k0_values_nightly_lhs_nt_rhs_nt = framework::dataset::make("K0", { 1, 2, 3, 4, 8, 16 });
71// const auto k0_values_nightly_rhs_t = framework::dataset::make("K0", { 1, 2, 3, 4, 8 });
72const auto k0_values_nightly_lhs_t_rhs_nt = framework::dataset::make("K0", { 1, 2, 3, 4, 5, 6, 7, 8 });
73
74TEST_SUITE(CL)
75TEST_SUITE(MatMulLowpNativeKernel)
76TEST_SUITE(Validate)
77
78TEST_CASE(SupportedKernelConfigurations, framework::DatasetMode::ALL)
79{
80 using MatMulConfigurationPair = std::pair<MatMulKernelInfo, bool>;
81
82 const std::vector<MatMulConfigurationPair> supported_block_sizes =
83 {
84 // MatMulKernelInfo(adj_lhs, adj_rhs, M0, N0, K0, export_rhs_to_cl_image = false)
85 // Lhs not-transposed, Rhs-not-transposed
86 { MatMulKernelInfo(false, false, 0, 1, 1), false }, // M0 should be > 0
87 { MatMulKernelInfo(false, false, 3, 5, 1), false }, // N0 not in {1, 2, 3, 4, 8, 16}
88 { MatMulKernelInfo(false, false, 3, 6, 1), false }, // N0 not in {1, 2, 3, 4, 8, 16}
89 { MatMulKernelInfo(false, false, 3, 3, 17), false }, // K0 not in {1, 2, 3, 4, 8, 16}
90 { MatMulKernelInfo(false, false, 3, 3, 7), false }, // K0 not in {1, 2, 3, 4, 8, 16}
91 { MatMulKernelInfo(false, false, 9, 1, 2), true },
92 { MatMulKernelInfo(false, false, 3, 16, 3), true },
93 { MatMulKernelInfo(false, false, 7, 3, 4), true },
94 { MatMulKernelInfo(false, false, 7, 3, 4, true), true }, // export to CLImage is unsupported for quantized types
95 };
96
97 // Set big enough shapes so that block sizes are not truncated. Also, set all dimensions equal
98 // so that it doesn't fail for different NT/T configurations. We aim to test the block sizes here,
99 // not the shapes themselves.
100 const TensorInfo lhs_info = TensorInfo(TensorShape(100U, 100U), 1, DataType::QASYMM8_SIGNED);
101 const TensorInfo rhs_info = TensorInfo(TensorShape(100U, 100U), 1, DataType::QASYMM8_SIGNED);
102
103 for(auto &pair : supported_block_sizes)
104 {
105 TensorInfo output_info;
106 Status status = ClMatMulLowpNativeKernel::validate(&lhs_info, &rhs_info, &output_info, pair.first);
107
108 ARM_COMPUTE_EXPECT(bool(status) == pair.second, framework::LogLevel::ERRORS);
109 }
110}
111
112TEST_CASE(ValidateInputShapes, framework::DatasetMode::ALL)
113{
114 // Configurations are assumed to be Nt/Nt, but will be transposed inside the test to test other configurations
115 using ShapeConfigurationTuple = std::tuple<TensorShape, TensorShape, bool>;
116 const std::vector<ShapeConfigurationTuple> shape_configurations =
117 {
118 { TensorShape(5U, 1U), TensorShape(3U, 5U), true },
119 { TensorShape(10U, 12U), TensorShape(3U, 10U), true },
120 { TensorShape(8U, 4U), TensorShape(2U, 8U), true },
121 { TensorShape(8U, 4U), TensorShape(2U, 5U), false }, // Mismatch in the K dimension
122 { TensorShape(5U, 0U), TensorShape(2U, 5U), false }, // Invalid dimension
123 { TensorShape(5U, 4U, 3U, 4U, 5U, 6U), TensorShape(2U, 5U, 3U, 4U, 5U, 6U), true },
124 { TensorShape(5U, 4U, 3U, 4U, 5U, 1U), TensorShape(2U, 5U, 3U, 4U, 5U, 6U), false }, // no batch broadcasting
125 { TensorShape(5U, 4U, 3U, 4U, 9U, 6U), TensorShape(2U, 5U, 3U, 4U, 5U, 6U), false }, // mismatch in batch dimension
126 };
127
128 for(auto &tuple : shape_configurations)
129 {
130 const bool expected = std::get<2>(tuple);
131
132 for(bool adj_lhs :
133 {
134 false, true
135 })
136 {
137 for(bool adj_rhs :
138 {
139 false, true
140 })
141 {
142 TensorShape lhs_shape = std::get<0>(tuple);
143 TensorShape rhs_shape = std::get<1>(tuple);
144
145 if(adj_lhs)
146 {
147 permute(lhs_shape, PermutationVector(1U, 0U));
148 }
149
150 if(adj_rhs)
151 {
152 permute(rhs_shape, PermutationVector(1U, 0U));
153 }
154
155 const TensorInfo lhs_info = TensorInfo(lhs_shape, 1, DataType::QASYMM8_SIGNED);
156 const TensorInfo rhs_info = TensorInfo(rhs_shape, 1, DataType::QASYMM8_SIGNED);
157 TensorInfo output_info;
158
159 MatMulKernelInfo matmul_kernel_info{ adj_lhs, adj_rhs, 1, 1, 1, false /* export_rhs_to_cl_image */ };
160
161 Status status = ClMatMulLowpNativeKernel::validate(&lhs_info, &rhs_info, &output_info, matmul_kernel_info);
162 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
163 }
164 }
165 }
166}
167
168TEST_CASE(ValidateDataTypes, framework::DatasetMode::ALL)
169{
170 using DataTypeConfigurationTuple = std::tuple<DataType, DataType, DataType, bool>;
171 const std::vector<DataTypeConfigurationTuple> data_type_configurations =
172 {
173 { DataType::F32, DataType::F32, DataType::F32, false }, // no floating point types
174 { DataType::F16, DataType::F16, DataType::F16, false }, // no floating point types
175 { DataType::F64, DataType::F64, DataType::F64, false }, // no double precision
176 { DataType::QASYMM8, DataType::QASYMM8, DataType::QASYMM8, true },
177 { DataType::QASYMM8_SIGNED, DataType::QASYMM8_SIGNED, DataType::QASYMM8_SIGNED, true },
178 { DataType::QSYMM8_PER_CHANNEL, DataType::QSYMM8_PER_CHANNEL, DataType::QSYMM8_PER_CHANNEL, false }, // only qasymm8/qasymm8_signed is supported
179 { DataType::QASYMM16, DataType::QASYMM16, DataType::QASYMM16, false }, // only qasymm8/qasymm8_signed is supported
180 { DataType::QSYMM16, DataType::QSYMM16, DataType::QSYMM16, false }, // only qasymm8/qasymm8_signed is supported
181 { DataType::QSYMM8, DataType::QSYMM8, DataType::QSYMM8, false }, // only qasymm8/qasymm8_signed is supported
182 { DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QASYMM8, false }, // no mixed data types
183 { DataType::S64, DataType::S64, DataType::S64, false }, // no integral types
184 { DataType::S32, DataType::S32, DataType::S32, false }, // no integral types
185 { DataType::S16, DataType::S16, DataType::S16, false }, // no integral types
186 { DataType::S8, DataType::S8, DataType::S8, false }, // no integral types
187 { DataType::U64, DataType::U64, DataType::U64, false }, // no integral types
188 { DataType::U32, DataType::U32, DataType::U32, false }, // no integral types
189 { DataType::U16, DataType::U16, DataType::U16, false }, // no integral types
190 { DataType::U8, DataType::U8, DataType::U8, false }, // no integral types
191 };
192
193 // It's enough to test a single shape and block size configuration while checking data types
194 const TensorShape shape = TensorShape(10U, 10U);
195 const MatMulKernelInfo matmul_kernel_info{ false, false, 1, 1, 1, false };
196 for(auto &tuple : data_type_configurations)
197 {
198 const bool expected = std::get<3>(tuple);
199
200 const TensorInfo lhs_info(shape, 1, std::get<0>(tuple));
201 const TensorInfo rhs_info(shape, 1, std::get<1>(tuple));
202 TensorInfo output_info(shape, 1, std::get<2>(tuple));
203
204 Status status = ClMatMulLowpNativeKernel::validate(&lhs_info, &rhs_info, &output_info, matmul_kernel_info);
205 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
206 }
207}
208
209TEST_SUITE_END() // Validate
210
211TEST_SUITE(Quantized)
212TEST_SUITE(QASYMM8_SIGNED)
213FIXTURE_DATA_TEST_CASE(RunTiny, CLMatMulLowpNativeKernelFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(datasets::TinyMatMulDataset(),
214 framework::dataset::make("TransposeA", { true, false })),
215 framework::dataset::make("TransposeB", { false })),
216 m0_values_precommit),
217 n0_values_precommit),
218 k0_values_precommit),
219 framework::dataset::make("ExportRhsToCLImage", { false })),
220 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)))
221{
222 // Validate output
223 validate(CLAccessor(_target), _reference, tolerance_quant);
224}
225FIXTURE_DATA_TEST_CASE(RunSmall, CLMatMulLowpNativeKernelFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(datasets::SmallMatMulDataset(),
226 framework::dataset::make("TransposeA", { true, false })),
227 framework::dataset::make("TransposeB", { false })),
228 m0_values_precommit),
229 n0_values_precommit),
230 k0_values_precommit),
231 framework::dataset::make("ExportRhsToCLImage", { false })),
232 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)))
233{
234 // Validate output
235 validate(CLAccessor(_target), _reference, tolerance_quant);
236}
237FIXTURE_DATA_TEST_CASE(RunLargeNoTranspose, CLMatMulLowpNativeKernelFixture<int8_t>, framework::DatasetMode::NIGHTLY,
238 combine(combine(combine(combine(combine(combine(combine(datasets::LargeMatMulDataset(),
239 framework::dataset::make("TransposeA", { false })),
240 framework::dataset::make("TransposeB", { false })),
241 m0_values_nightly_lhs_nt),
242 n0_values_nightly_rhs_nt),
243 k0_values_nightly_lhs_nt_rhs_nt),
244 framework::dataset::make("ExportRhsToCLImage", { false })),
245 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)))
246{
247 // Validate output
248 validate(CLAccessor(_target), _reference, tolerance_quant);
249}
250FIXTURE_DATA_TEST_CASE(RunLargeLhsTransposed, CLMatMulLowpNativeKernelFixture<int8_t>, framework::DatasetMode::NIGHTLY,
251 combine(combine(combine(combine(combine(combine(combine(datasets::LargeMatMulDataset(),
252 framework::dataset::make("TransposeA", { true })),
253 framework::dataset::make("TransposeB", { false })),
254 m0_values_nightly_lhs_t),
255 n0_values_nightly_rhs_nt),
256 k0_values_nightly_lhs_t_rhs_nt),
257 framework::dataset::make("ExportRhsToCLImage", { false })),
258 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)))
259{
260 // Validate output
261 validate(CLAccessor(_target), _reference, tolerance_quant);
262}
263// Running High Dimensional test is enough for qasymm8_signed, because we're stressing the number of dimensions, not data type or M0/N0/K0
264// It's a good idea to test for each Lhs/Rhs T/NT combinations because they're different CL kernels
265FIXTURE_DATA_TEST_CASE(RunHighDimensional, CLMatMulLowpNativeKernelFixture<int8_t>, framework::DatasetMode::ALL,
266 combine(combine(combine(combine(combine(combine(combine(datasets::HighDimensionalMatMulDataset(),
267 framework::dataset::make("TransposeA", { true, false })),
268 framework::dataset::make("TransposeB", { false })),
269 framework::dataset::make("M0", { 2 })),
270 framework::dataset::make("N0", { 2 })),
271 framework::dataset::make("K0", { 2 })),
272 framework::dataset::make("ExportRhsToCLImage", { false })),
273 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)))
274{
275 // Validate output
276 validate(CLAccessor(_target), _reference, tolerance_quant);
277}
278TEST_SUITE_END() // QASYMM8_SIGNED
279
280TEST_SUITE(QASYMM8)
281FIXTURE_DATA_TEST_CASE(RunTiny, CLMatMulLowpNativeKernelFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(datasets::TinyMatMulDataset(),
282 framework::dataset::make("TransposeA", { true, false })),
283 framework::dataset::make("TransposeB", { false })),
284 m0_values_precommit),
285 n0_values_precommit),
286 k0_values_precommit),
287 framework::dataset::make("ExportRhsToCLImage", { false })),
288 framework::dataset::make("DataType", DataType::QASYMM8)))
289{
290 // Validate output
291 validate(CLAccessor(_target), _reference, tolerance_quant);
292}
293FIXTURE_DATA_TEST_CASE(RunSmall, CLMatMulLowpNativeKernelFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(datasets::SmallMatMulDataset(),
294 framework::dataset::make("TransposeA", { true, false })),
295 framework::dataset::make("TransposeB", { false })),
296 m0_values_precommit),
297 n0_values_precommit),
298 k0_values_precommit),
299 framework::dataset::make("ExportRhsToCLImage", { false })),
300 framework::dataset::make("DataType", DataType::QASYMM8)))
301{
302 // Validate output
303 validate(CLAccessor(_target), _reference, tolerance_quant);
304}
305FIXTURE_DATA_TEST_CASE(RunLargeNoTranspose, CLMatMulLowpNativeKernelFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
306 combine(combine(combine(combine(combine(combine(combine(datasets::LargeMatMulDataset(),
307 framework::dataset::make("TransposeA", { false })),
308 framework::dataset::make("TransposeB", { false })),
309 m0_values_nightly_lhs_nt),
310 n0_values_nightly_rhs_nt),
311 k0_values_nightly_lhs_nt_rhs_nt),
312 framework::dataset::make("ExportRhsToCLImage", { false })),
313 framework::dataset::make("DataType", DataType::QASYMM8)))
314{
315 // Validate output
316 validate(CLAccessor(_target), _reference, tolerance_quant);
317}
318FIXTURE_DATA_TEST_CASE(RunLargeLhsTransposed, CLMatMulLowpNativeKernelFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
319 combine(combine(combine(combine(combine(combine(combine(datasets::LargeMatMulDataset(),
320 framework::dataset::make("TransposeA", { true })),
321 framework::dataset::make("TransposeB", { false })),
322 m0_values_nightly_lhs_t),
323 n0_values_nightly_rhs_nt),
324 k0_values_nightly_lhs_t_rhs_nt),
325 framework::dataset::make("ExportRhsToCLImage", { false })),
326 framework::dataset::make("DataType", DataType::QASYMM8)))
327{
328 // Validate output
329 validate(CLAccessor(_target), _reference, tolerance_quant);
330}
331TEST_SUITE_END() // QASYMM8
332TEST_SUITE_END() // Quantized
333TEST_SUITE_END() // MatMulLowpNativeKernel
334TEST_SUITE_END() // CL
335} // namespace validation
336} // namespace test
337} // namespace arm_compute