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/CLGEMMMatrixMultiplyKernel.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 CLGEMMMatrixMultiplyKernel
49 using CLGEMMMatrixMultiplyNative = CLSynthetizeFunction<CLGEMMMatrixMultiplyKernel>;
50
51 // Fixture for GEMMMatrixMultiplyValidationFixture
52 template <typename T>
53 using CLGEMMMatrixMultiplyNativeFixture = GEMMMatrixMultiplyValidationFixture<CLTensor, CLAccessor, T, CLGEMMMatrixMultiplyNative>;
54
55 // Fixture for GEMMMatrixMultiply3DValidationFixture
56 template <typename T>
57 using CLGEMMMatrixMultiplyNative3DFixture = GEMMMatrixMultiply3DValidationFixture<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 RelativeTolerance<half> rel_tolerance_f16(half(0.2));
67 constexpr float tolerance_num_f16 = 0.02f;
68
69 /** Alpha values to test */
70 const auto alpha_values = framework::dataset::make("alpha", {1.0f, -0.75f} );
71
72 /** Beta values to test */
73 const auto beta_values = framework::dataset::make("beta", {-0.35f, 0.0f} );
74
75 /** M values to test */
76 const auto m_values = framework::dataset::make("M", {37, 1});
77
78 /** N values to test */
79 const auto n_values = framework::dataset::make("N", {51, 1003});
80
81 /** K values to test */
82 const auto k_values = framework::dataset::make("K", 23);
83
84 /** M_W values to test */
85 const auto m_w_values = framework::dataset::make("M_W", 5);
86
87 /** M_H values to test */
88 const auto m_h_values = framework::dataset::make("M_H", 7);
89
90 /** Batch size values to test */
91 const auto b_values = framework::dataset::make("batch_size", 1, 3);
92
93 /** Activation values to test */
94 const auto act_values = framework::dataset::make("Activation",
95 {
96 ActivationLayerInfo(),
97 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
98 });
99
100 /** Broadcast bias from vector to matrix */
101 const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", { false, true } );
102
103 /** GPU architectures values to test */
104 const auto gpu_arch_values = framework::dataset::make("GPUArch",
105 {
106 GPUTarget::MIDGARD,
107 GPUTarget::BIFROST
108 });
109
110 /** Data types values to test in the configuration */
111 const auto data_type_values = framework::dataset::make("DataType",
112 {
113 DataType::F32,
114 DataType::F16
115 });
116
117 /** M values to test */
118 const auto fp16_mixed_precision_values = framework::dataset::make("fp16_mixed_precision", {true, false});
119 } // namespace
120
121 TEST_SUITE(CL)
TEST_SUITE(GEMMMatrixMultiply)122 TEST_SUITE(GEMMMatrixMultiply)
123 TEST_CASE(Negative, framework::DatasetMode::ALL)
124 {
125 // Unsupported QASYMM8 data type
126 {
127 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::QASYMM8);
128 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::QASYMM8);
129 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::QASYMM8);
130 constexpr float alpha = 1.3f;
131 constexpr float beta = 0.7f;
132 const bool is_interleaved_transposed = false;
133 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
134 const GPUTarget gpu_target = GPUTarget::MIDGARD;
135 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
136 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
137 }
138
139 // Unsupported SIZE_T data type
140 {
141 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::SIZET);
142 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::SIZET);
143 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::SIZET);
144 constexpr float alpha = 1.3f;
145 constexpr float beta = 0.7f;
146 const bool is_interleaved_transposed = false;
147 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
148 const GPUTarget gpu_target = GPUTarget::MIDGARD;
149 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
150 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
151 }
152
153 // Mixed precision with F32
154 {
155 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F32);
156 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
157 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
158 constexpr float alpha = 1.3f;
159 constexpr float beta = 0.7f;
160 const bool is_interleaved_transposed = false;
161 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
162 const GPUTarget gpu_target = GPUTarget::MIDGARD;
163 const bool fp_mixed_precision = true;
164 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target, fp_mixed_precision);
165 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
166 }
167
168 // Max number of dimensions LHS matrix
169 {
170 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U, 4U), 1, DataType::F32);
171 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
172 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
173 constexpr float alpha = 1.3f;
174 constexpr float beta = 0.7f;
175 const bool is_interleaved_transposed = false;
176 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
177 const GPUTarget gpu_target = GPUTarget::MIDGARD;
178 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
179 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
180 }
181
182 // Max number of dimensions RHS matrix
183 {
184 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 4U), 1, DataType::F32);
185 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 4U), 1, DataType::F32);
186 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 4U), 1, DataType::F32);
187 constexpr float alpha = 1.3f;
188 constexpr float beta = 0.7f;
189 const bool is_interleaved_transposed = false;
190 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
191 const GPUTarget gpu_target = GPUTarget::MIDGARD;
192 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
193 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
194 }
195
196 // Broadcast bias
197 {
198 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F16);
199 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F16);
200 // The correct shape should be bias = TensorInfo(TensorShape(14U, 1U, 1U, 1U), 1, DataType::F32);
201 const auto bias = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F16);
202 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F16);
203 constexpr float alpha = 1.3f;
204 constexpr float beta = 0.7f;
205 const bool is_interleaved_transposed = false;
206 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, true);
207 const GPUTarget gpu_target = GPUTarget::MIDGARD;
208 const bool fp_mixed_precision = false;
209 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, &bias, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target, fp_mixed_precision);
210 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
211 }
212
213 // Invalid dimensions for the bias
214 {
215 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F32);
216 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
217 // The correct shape should be bias = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
218 const auto bias = TensorInfo(TensorShape(14U, 8U, 1U, 1U), 1, DataType::F32);
219 const auto out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
220 constexpr float alpha = 1.3f;
221 constexpr float beta = 0.7f;
222 const bool is_interleaved_transposed = false;
223 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
224 const GPUTarget gpu_target = GPUTarget::MIDGARD;
225 const bool fp_mixed_precision = false;
226 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, &bias, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target, fp_mixed_precision);
227 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
228 }
229
230 // Invalid dimensions for the output
231 {
232 const auto lhs = TensorInfo(TensorShape(13U, 12U, 1U, 1U), 1, DataType::F32);
233 const auto rhs = TensorInfo(TensorShape(14U, 13U, 1U, 1U), 1, DataType::F32);
234 // The correct shape should be out = TensorInfo(TensorShape(14U, 12U, 1U, 1U), 1, DataType::F32);
235 const auto out = TensorInfo(TensorShape(14U, 7U, 1U, 1U), 1, DataType::F32);
236 constexpr float alpha = 1.3f;
237 constexpr float beta = 0.7f;
238 const bool is_interleaved_transposed = false;
239 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(12, 14, 13, 1, 1, 0, false, false);
240 const GPUTarget gpu_target = GPUTarget::MIDGARD;
241 const auto status = CLGEMMMatrixMultiplyKernel::validate(&lhs, &rhs, nullptr, &out, alpha, beta, is_interleaved_transposed, reshape_info, gpu_target);
242 ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
243 }
244 }
245
246 TEST_SUITE(Float)
TEST_SUITE(FP32)247 TEST_SUITE(FP32)
248
249 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
250 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
251 m_values,
252 n_values),
253 k_values),
254 b_values),
255 alpha_values),
256 beta_values),
257 broadcast_bias_values),
258 framework::dataset::make("fp16_mixed_precision", false)),
259 act_values),
260 framework::dataset::make("DataType", DataType::F32)),
261 gpu_arch_values))
262 {
263 // Validate output
264 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
265 }
266
267 FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyNative3DFixture<float>, framework::DatasetMode::ALL,
268 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
269 m_w_values,
270 m_h_values),
271 n_values),
272 k_values),
273 b_values),
274 alpha_values),
275 beta_values),
276 broadcast_bias_values),
277 framework::dataset::make("fp16_mixed_precision", false)),
278 act_values),
279 framework::dataset::make("DataType", DataType::F32)),
280 gpu_arch_values))
281 {
282 // Validate output
283 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
284 }
285
286 TEST_SUITE_END() // FP32
287
TEST_SUITE(FP16)288 TEST_SUITE(FP16)
289 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyNativeFixture<half>, framework::DatasetMode::ALL,
290 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
291 m_values,
292 n_values),
293 k_values),
294 b_values),
295 alpha_values),
296 beta_values),
297 broadcast_bias_values),
298 fp16_mixed_precision_values),
299 act_values),
300 framework::dataset::make("DataType", DataType::F16)),
301 gpu_arch_values))
302 {
303 // Validate output
304 validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16);
305 }
306
307 FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyNative3DFixture<half>, framework::DatasetMode::ALL,
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 alpha_values),
315 beta_values),
316 broadcast_bias_values),
317 fp16_mixed_precision_values),
318 act_values),
319 framework::dataset::make("DataType", DataType::F16)),
320 gpu_arch_values))
321 {
322 // Validate output
323 validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16);
324 }
325
326 TEST_SUITE_END() // FP16
327 TEST_SUITE_END() // Float
328 TEST_SUITE_END() // GEMMMatrixMuliplty
329 TEST_SUITE_END() // CL
330 } // namespace validation
331 } // namespace test
332 } // namespace arm_compute