blob: fdf7f503ec64f5d0375dc0240cdca9a82212ebcc [file] [log] [blame]
Gian Marco Iodiced1f54762019-07-19 09:54:47 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Gian Marco Iodiced1f54762019-07-19 09:54:47 +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 */
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010024#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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010029#include "src/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010030#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
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46using namespace arm_compute::misc::shape_calculator;
47
48// Create function for CLGEMMMatrixMultiplyKernel
49using CLGEMMMatrixMultiplyNative = CLSynthetizeFunction<CLGEMMMatrixMultiplyKernel>;
50
51// Fixture for GEMMMatrixMultiplyValidationFixture
52template <typename T>
53using CLGEMMMatrixMultiplyNativeFixture = GEMMMatrixMultiplyValidationFixture<CLTensor, CLAccessor, T, CLGEMMMatrixMultiplyNative>;
54
55// Fixture for GEMMMatrixMultiply3DValidationFixture
56template <typename T>
57using CLGEMMMatrixMultiplyNative3DFixture = GEMMMatrixMultiply3DValidationFixture<CLTensor, CLAccessor, T, CLGEMMMatrixMultiplyNative>;
58
59namespace
60{
61// *INDENT-OFF*
62// clang-format off
63RelativeTolerance<float> rel_tolerance_f32(0.001f);
64constexpr float abs_tolerance_f32(0.0001f);
65
66RelativeTolerance<half> rel_tolerance_f16(half(0.2));
67constexpr float tolerance_num_f16 = 0.02f;
68
Gian Marco Iodicec630e942020-05-11 12:15:54 +010069/** Alpha values to test */
Gian Marco Iodicef3622be2019-07-29 14:27:16 +010070const auto alpha_values = framework::dataset::make("alpha", {1.0f, -0.75f} );
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010071
Gian Marco Iodicec630e942020-05-11 12:15:54 +010072/** Beta values to test */
Gian Marco Iodiced820db62019-08-05 14:23:23 +010073const auto beta_values = framework::dataset::make("beta", {-0.35f, 0.0f} );
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010074
SiCong Li7e5b7bf2020-11-17 09:41:13 +000075/** M, N combinations to test
76 * 1: Special 1x1 case
77 * 2: Special multples of processor size in both dimensions
78 * 3: Non multiples of processor size in both dimensions
79 * 4: Special 1x1003 case
80*/
81const auto m_n_values = zip(
82 framework::dataset::make("M", {1, 16, 37, 1}),
83 framework::dataset::make("N", {1, 16, 51, 1003})
84 );
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010085
Gian Marco Iodicec630e942020-05-11 12:15:54 +010086/** N values to test */
87const auto n_values = framework::dataset::make("N", {51, 1003});
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010088
Gian Marco Iodicec630e942020-05-11 12:15:54 +010089/** K values to test */
90const auto k_values = framework::dataset::make("K", 23);
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010091
Gian Marco Iodicec630e942020-05-11 12:15:54 +010092/** M_W values to test */
93const auto m_w_values = framework::dataset::make("M_W", 5);
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010094
Gian Marco Iodicec630e942020-05-11 12:15:54 +010095/** M_H values to test */
96const auto m_h_values = framework::dataset::make("M_H", 7);
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010097
98/** Batch size values to test */
99const auto b_values = framework::dataset::make("batch_size", 1, 3);
100
101/** Activation values to test */
102const auto act_values = framework::dataset::make("Activation",
103{
104 ActivationLayerInfo(),
105 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
106});
107
108/** Broadcast bias from vector to matrix */
Gian Marco Iodiced820db62019-08-05 14:23:23 +0100109const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", { false, true } );
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100110
111/** GPU architectures values to test */
112const auto gpu_arch_values = framework::dataset::make("GPUArch",
113{
114 GPUTarget::MIDGARD,
115 GPUTarget::BIFROST
116});
117
118/** Data types values to test in the configuration */
119const auto data_type_values = framework::dataset::make("DataType",
120{
121 DataType::F32,
122 DataType::F16
123});
124
125/** M values to test */
126const auto fp16_mixed_precision_values = framework::dataset::make("fp16_mixed_precision", {true, false});
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100127} // namespace
128
129TEST_SUITE(CL)
130TEST_SUITE(GEMMMatrixMultiply)
Gian Marco Iodicec630e942020-05-11 12:15:54 +0100131TEST_CASE(Negative, framework::DatasetMode::ALL)
132{
133 // Unsupported QASYMM8 data type
134 {
135 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::QASYMM8);
136 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::QASYMM8);
137 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::QASYMM8);
138 constexpr float alpha = 1.3f;
139 constexpr float beta = 0.7f;
140 const bool is_interleaved_transposed = false;
141 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
142 const GPUTarget gpu_target = GPUTarget::MIDGARD;
143 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
144 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
145 }
146
147 // Unsupported SIZE_T data type
148 {
149 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::SIZET);
150 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::SIZET);
151 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::SIZET);
152 constexpr float alpha = 1.3f;
153 constexpr float beta = 0.7f;
154 const bool is_interleaved_transposed = false;
155 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
156 const GPUTarget gpu_target = GPUTarget::MIDGARD;
157 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
158 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
159 }
160
161 // Mixed precision with F32
162 {
163 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F32);
164 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
165 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
166 constexpr float alpha = 1.3f;
167 constexpr float beta = 0.7f;
168 const bool is_interleaved_transposed = false;
169 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
170 const GPUTarget gpu_target = GPUTarget::MIDGARD;
171 const bool fp_mixed_precision = true;
172 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target, fp_mixed_precision);
173 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
174 }
175
176 // Max number of dimensions LHS matrix
177 {
178 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U, 4U), 1, DataType::F32);
179 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
180 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
181 constexpr float alpha = 1.3f;
182 constexpr float beta = 0.7f;
183 const bool is_interleaved_transposed = false;
184 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
185 const GPUTarget gpu_target = GPUTarget::MIDGARD;
186 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
187 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
188 }
189
190 // Max number of dimensions RHS matrix
191 {
192 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 4U), 1, DataType::F32);
193 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 4U), 1, DataType::F32);
194 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 4U), 1, DataType::F32);
195 constexpr float alpha = 1.3f;
196 constexpr float beta = 0.7f;
197 const bool is_interleaved_transposed = false;
198 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
199 const GPUTarget gpu_target = GPUTarget::MIDGARD;
200 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
201 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
202 }
203
204 // Broadcast bias
205 {
206 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F16);
207 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F16);
208 // The correct shape should be bias = TensorInfo(TensorShape(14U, 1U, 1U, 1U), 1, DataType::F32);
209 const auto bias = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F16);
210 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F16);
211 constexpr float alpha = 1.3f;
212 constexpr float beta = 0.7f;
213 const bool is_interleaved_transposed = false;
214 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, true);
215 const GPUTarget gpu_target = GPUTarget::MIDGARD;
216 const bool fp_mixed_precision = false;
217 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, &bias, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target, fp_mixed_precision);
218 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
219 }
220
221 // Invalid dimensions for the bias
222 {
223 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F32);
224 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
225 // The correct shape should be bias = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
226 const auto bias = TensorInfo(TensorShape(14U, 8U, 1U, 1U), 1, DataType::F32);
227 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
228 constexpr float alpha = 1.3f;
229 constexpr float beta = 0.7f;
230 const bool is_interleaved_transposed = false;
231 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
232 const GPUTarget gpu_target = GPUTarget::MIDGARD;
233 const bool fp_mixed_precision = false;
234 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, &bias, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target, fp_mixed_precision);
235 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
236 }
237
238 // Invalid dimensions for the output
239 {
240 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F32);
241 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
242 // The correct shape should be out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
243 const auto out = TensorInfo(TensorShape(14U, 7U, 1U, 1U), 1, DataType::F32);
244 constexpr float alpha = 1.3f;
245 constexpr float beta = 0.7f;
246 const bool is_interleaved_transposed = false;
247 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
248 const GPUTarget gpu_target = GPUTarget::MIDGARD;
249 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
250 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
251 }
252}
253
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100254TEST_SUITE(Float)
255TEST_SUITE(FP32)
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100256
257FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
SiCong Li7e5b7bf2020-11-17 09:41:13 +0000258 combine(combine(combine(combine(combine(combine(combine(combine(combine(
259 m_n_values,
Gian Marco Iodicec630e942020-05-11 12:15:54 +0100260 k_values),
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100261 b_values),
262 alpha_values),
263 beta_values),
264 broadcast_bias_values),
265 framework::dataset::make("fp16_mixed_precision", false)),
266 act_values),
267 framework::dataset::make("DataType", DataType::F32)),
268 gpu_arch_values))
269{
270 // Validate output
271 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
272}
273
274FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyNative3DFixture<float>, framework::DatasetMode::ALL,
275 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodicec630e942020-05-11 12:15:54 +0100276 m_w_values,
277 m_h_values),
278 n_values),
279 k_values),
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100280 b_values),
281 alpha_values),
282 beta_values),
283 broadcast_bias_values),
284 framework::dataset::make("fp16_mixed_precision", false)),
285 act_values),
286 framework::dataset::make("DataType", DataType::F32)),
287 gpu_arch_values))
288{
289 // Validate output
290 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
291}
292
293TEST_SUITE_END() // FP32
294
295TEST_SUITE(FP16)
296FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyNativeFixture<half>, framework::DatasetMode::ALL,
SiCong Li7e5b7bf2020-11-17 09:41:13 +0000297 combine(combine(combine(combine(combine(combine(combine(combine(combine(
298 m_n_values,
Gian Marco Iodicec630e942020-05-11 12:15:54 +0100299 k_values),
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100300 b_values),
301 alpha_values),
302 beta_values),
303 broadcast_bias_values),
304 fp16_mixed_precision_values),
305 act_values),
306 framework::dataset::make("DataType", DataType::F16)),
307 gpu_arch_values))
308{
309 // Validate output
310 validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16);
311}
312
313FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyNative3DFixture<half>, framework::DatasetMode::ALL,
314 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodicec630e942020-05-11 12:15:54 +0100315 m_w_values,
316 m_h_values),
317 n_values),
318 k_values),
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100319 b_values),
320 alpha_values),
321 beta_values),
322 broadcast_bias_values),
323 fp16_mixed_precision_values),
324 act_values),
325 framework::dataset::make("DataType", DataType::F16)),
326 gpu_arch_values))
327{
328 // Validate output
329 validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16);
330}
331
332TEST_SUITE_END() // FP16
333TEST_SUITE_END() // Float
334TEST_SUITE_END() // GEMMMatrixMuliplty
335TEST_SUITE_END() // CL
336} // namespace validation
337} // namespace test
338} // namespace arm_compute