blob: bdf8248bb2b59db678d900f774df21f8f7e16a44 [file] [log] [blame]
giuros01b3204e72019-04-01 13:50:22 +01001/*
SiCong Li3a501662020-06-26 10:02:06 +01002 * Copyright (c) 2019-2020 Arm Limited.
giuros01b3204e72019-04-01 13:50:22 +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/CL/kernels/CLGEMMMatrixMultiplyNativeKernel.h"
Gian Marco Iodice7026b302019-06-26 17:18:11 +010025#include "arm_compute/core/KernelDescriptors.h"
giuros01b3204e72019-04-01 13:50:22 +010026#include "arm_compute/core/Types.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
28#include "arm_compute/runtime/CL/CLTensor.h"
29#include "arm_compute/runtime/CL/CLTensorAllocator.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
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46using namespace arm_compute::misc::shape_calculator;
47
48// Create function for CLGEMMMatrixMultiplyNativeKernel
49using CLGEMMMatrixMultiplyNative = CLSynthetizeFunction<CLGEMMMatrixMultiplyNativeKernel>;
50
51// Fixture for CLGEMMMatrixMultiplyNative
52template <typename T>
53using CLGEMMMatrixMultiplyNativeFixture = GEMMMatrixMultiplyNativeValidationFixture<CLTensor, CLAccessor, T, CLGEMMMatrixMultiplyNative>;
54
55// Fixture for CLGEMMMatrixMultiplyNative3D
56template <typename T>
57using CLGEMMMatrixMultiplyNative3DFixture = GEMMMatrixMultiplyNative3DValidationFixture<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
giuros01b3204e72019-04-01 13:50:22 +010066/** Alpha values to test - Precommit */
67const auto a_values = framework::dataset::make("alpha", {1.0f, -0.75f} );
68
Gian Marco Iodice944170e2019-06-24 14:40:30 +010069/** Beta values to test - Precommit */
70const auto beta_values = framework::dataset::make("beta", {-0.75f, 0.0f} );
71
giuros01b3204e72019-04-01 13:50:22 +010072/** M values to test */
73const auto m_values = framework::dataset::make("M", 37);
74
75/** M_W values to test */
76const auto m_w_values = framework::dataset::make("M_W", 5);
77
78/** M_H values to test */
79const auto m_h_values = framework::dataset::make("M_H", 7);
80
81/** N values to test */
82const auto n_values = framework::dataset::make("N", 51);
83
84/** K values to test */
85const auto k_values = framework::dataset::make("K", 23);
86
87/** Batch size values to test */
88const auto b_values = framework::dataset::make("batch_size", 1, 3);
89
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +010090/** Activation values to test */
91const auto act_values = framework::dataset::make("Activation",
92{
93 ActivationLayerInfo(),
94 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
95});
96
giuros01b3204e72019-04-01 13:50:22 +010097/** M0 values to test - Precommit */
Gian Marco Iodiced820db62019-08-05 14:23:23 +010098const auto m0_values_precommit = framework::dataset::make("M0", { 4, 6 });
giuros01b3204e72019-04-01 13:50:22 +010099
100/** N0 values to test - Precommit */
Gian Marco Iodiced820db62019-08-05 14:23:23 +0100101const auto n0_values_precommit = framework::dataset::make("N0", { 4 });
giuros01b3204e72019-04-01 13:50:22 +0100102
103/** K0 values to test - Precommit */
104const auto k0_values_precommit = framework::dataset::make("K0", { 4 });
105
106/** H0 values to test - Precommit */
107const auto h0_values_precommit = framework::dataset::make("H0", 1, 3);
108
109/** M0 values to test - Nightly */
110const auto m0_values_nightly = framework::dataset::make("M0", 1, 8);
111
112/** N0 values to test - Nightly */
113const auto n0_values_nightly = framework::dataset::make("N0", { 2, 3, 4, 8 });
114
115/** K0 values to test - Nightly */
116const auto k0_values_nightly = framework::dataset::make("K0", { 2, 3, 4, 8 });
117
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100118/** Broadcast bias from vector to matrix */
Gian Marco Iodiced820db62019-08-05 14:23:23 +0100119const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", { false, true } );
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100120
giuros01b3204e72019-04-01 13:50:22 +0100121/** Configuration test */
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100122void validate_configuration(unsigned int m_value, unsigned int n_value, unsigned int k_value, unsigned int b_value, unsigned int m0_value, unsigned int n0_value, unsigned int k0_value, bool broadcast_bias, DataType data_type, const ActivationLayerInfo &act_info)
giuros01b3204e72019-04-01 13:50:22 +0100123{
124 const unsigned int M = m_value;
125 const unsigned int N = n_value;
126 const unsigned int K = k_value;
127
128 GEMMLHSMatrixInfo lhs_info;
129 lhs_info.m0 = m0_value;
130 lhs_info.k0 = k0_value;
131
132 GEMMRHSMatrixInfo rhs_info;
133 rhs_info.n0 = n0_value;
134 rhs_info.k0 = k0_value;
135
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100136 GEMMKernelInfo kernel_info;
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100137 kernel_info.m = M;
138 kernel_info.n = N;
139 kernel_info.k = K;
140 kernel_info.broadcast_bias = broadcast_bias;
141 kernel_info.activation_info = act_info;
giuros01b3204e72019-04-01 13:50:22 +0100142
143 const TensorShape lhs_shape(K, M, b_value);
144 const TensorShape rhs_shape(N, K, b_value);
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100145 const TensorShape bias_shape(N,
146 broadcast_bias? 1 : M,
147 broadcast_bias? 1 : b_value);
giuros01b3204e72019-04-01 13:50:22 +0100148 const TensorShape dst_shape = compute_mm_shape(TensorInfo(lhs_shape, 1, data_type),
149 TensorInfo(rhs_shape, 1, data_type),
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100150 kernel_info);
151
giuros01b3204e72019-04-01 13:50:22 +0100152 // Create tensors
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100153 CLTensor lhs = create_tensor<CLTensor>(lhs_shape, data_type);
giuros01b3204e72019-04-01 13:50:22 +0100154 CLTensor rhs = create_tensor<CLTensor>(rhs_shape, data_type);
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100155 CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type);
156 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type);
giuros01b3204e72019-04-01 13:50:22 +0100157
158 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
159 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100160 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
giuros01b3204e72019-04-01 13:50:22 +0100161 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
162
163 // Create and configure function
164 CLGEMMMatrixMultiplyNative gemm;
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100165 gemm.configure(&lhs, &rhs, &bias, &dst, 1.0f, 1.0f, lhs_info, rhs_info, kernel_info);
giuros01b3204e72019-04-01 13:50:22 +0100166}
SiCong Li3a501662020-06-26 10:02:06 +0100167/** Zero padding test */
168bool validate_zero_padding(unsigned int m_value, unsigned int n_value, unsigned int k_value, unsigned int b_value, unsigned int m0_value, unsigned int n0_value, unsigned int k0_value, bool broadcast_bias, DataType data_type, const ActivationLayerInfo &act_info)
169{
170 const unsigned int M = m_value;
171 const unsigned int N = n_value;
172 const unsigned int K = k_value;
173
174 GEMMLHSMatrixInfo lhs_info;
175 lhs_info.m0 = m0_value;
176 lhs_info.k0 = k0_value;
177
178 GEMMRHSMatrixInfo rhs_info;
179 rhs_info.n0 = n0_value;
180 rhs_info.k0 = k0_value;
181
182 GEMMKernelInfo kernel_info;
183 kernel_info.m = M;
184 kernel_info.n = N;
185 kernel_info.k = K;
186 kernel_info.broadcast_bias = broadcast_bias;
187 kernel_info.activation_info = act_info;
188
189 const TensorShape lhs_shape(K, M, b_value);
190 const TensorShape rhs_shape(N, K, b_value);
191 const TensorShape bias_shape(N,
192 broadcast_bias? 1 : M,
193 broadcast_bias? 1 : b_value);
194 const TensorShape dst_shape = compute_mm_shape(TensorInfo(lhs_shape, 1, data_type),
195 TensorInfo(rhs_shape, 1, data_type),
196 kernel_info);
197
198 // Create tensors
199 CLTensor lhs = create_tensor<CLTensor>(lhs_shape, data_type);
200 CLTensor rhs = create_tensor<CLTensor>(rhs_shape, data_type);
201 CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type);
202 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type);
203
204 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
205 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
206 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
207 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
208
209 // Create and configure function
210 CLGEMMMatrixMultiplyNative gemm;
211 gemm.configure(&lhs, &rhs, &bias, &dst, 1.0f, 1.0f, lhs_info, rhs_info, kernel_info);
212
213 return dst.info()->padding().empty();
214}
giuros01b3204e72019-04-01 13:50:22 +0100215} // namespace
216
217TEST_SUITE(CL)
218TEST_SUITE(GEMMMatrixMultiplyNative)
219TEST_SUITE(Float)
220TEST_SUITE(FP32)
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100221DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(combine(
giuros01b3204e72019-04-01 13:50:22 +0100222 m_values,
223 n_values),
224 k_values),
225 framework::dataset::make("batch_size", 1)),
226 m0_values_precommit),
227 n0_values_precommit),
228 k0_values_precommit),
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100229 broadcast_bias_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100230 act_values),
231m_value, n_value, k_value, b_value, m0_value, n0_value, k0_value, broadcast_bias, act_value)
giuros01b3204e72019-04-01 13:50:22 +0100232{
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100233 validate_configuration(m_value, n_value, k_value, b_value, m0_value, n0_value, k0_value, broadcast_bias, DataType::F32, act_value);
giuros01b3204e72019-04-01 13:50:22 +0100234}
235
SiCong Li3a501662020-06-26 10:02:06 +0100236/** Validate zero padding tests
237 *
238 * A series of validation tests to check that no padding is added as part of configuration for 4 different scenarios.
239 *
240 * Checks performed in order:
241 * - No partial blocks in both x and y dimensions
242 * - Partial blocks in x dimension
243 * - Partial blocks in y dimension
244 * - Partial blocks in both x and y dimensions
245 * - No blocks in both x and y dimensions, scalar store (N0==1)
246 */
247DATA_TEST_CASE(ValidateZeroPadding, framework::DatasetMode::ALL, zip(zip(zip(
248framework::dataset::make("M", { 24, 64, 101, 1, 50 }),
249framework::dataset::make("N", { 48, 29, 16, 122, 20 })),
250framework::dataset::make("M0", { 4, 8, 7, 2, 1 })),
251framework::dataset::make("N0", { 4, 4, 16, 3, 1 })),
252m_value, n_value, m0_value, n0_value)
253{
254 bool status = validate_zero_padding(m_value, n_value, 23, 1, m0_value, n0_value, 4, false, DataType::F32, ActivationLayerInfo());
255 ARM_COMPUTE_EXPECT(status, framework::LogLevel::ERRORS);
256}
257
giuros01b3204e72019-04-01 13:50:22 +0100258FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::ALL,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100259 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
giuros01b3204e72019-04-01 13:50:22 +0100260 m_values,
261 n_values),
262 k_values),
263 b_values),
264 m0_values_precommit),
265 n0_values_precommit),
266 k0_values_precommit),
267 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100268 a_values),
269 beta_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100270 broadcast_bias_values),
271 act_values))
giuros01b3204e72019-04-01 13:50:22 +0100272{
273 // Validate output
274 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
275}
276
Michalis Spyrou1d897772019-12-09 18:47:29 +0000277FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMMatrixMultiplyNativeFixture<float>, framework::DatasetMode::DISABLED,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100278 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
giuros01b3204e72019-04-01 13:50:22 +0100279 m_values,
280 n_values),
281 k_values),
282 b_values),
283 m0_values_nightly),
284 n0_values_nightly),
285 k0_values_nightly),
286 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100287 a_values),
288 beta_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100289 broadcast_bias_values),
290 act_values))
giuros01b3204e72019-04-01 13:50:22 +0100291{
292 // Validate output
293 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
294}
295
296FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyNative3DFixture<float>, framework::DatasetMode::ALL,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100297 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
giuros01b3204e72019-04-01 13:50:22 +0100298 m_w_values,
299 m_h_values),
300 n_values),
301 k_values),
302 b_values),
303 m0_values_precommit),
304 n0_values_precommit),
305 k0_values_precommit),
306 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100307 a_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100308 beta_values),
309 act_values))
giuros01b3204e72019-04-01 13:50:22 +0100310{
311 // Validate output
312 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
313}
314
Michalis Spyrou1d897772019-12-09 18:47:29 +0000315FIXTURE_DATA_TEST_CASE(RunLarge3D, CLGEMMMatrixMultiplyNative3DFixture<float>, framework::DatasetMode::DISABLED,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100316 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
giuros01b3204e72019-04-01 13:50:22 +0100317 m_w_values,
318 m_h_values),
319 n_values),
320 k_values),
321 b_values),
322 m0_values_nightly),
323 n0_values_nightly),
324 k0_values_nightly),
325 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodice944170e2019-06-24 14:40:30 +0100326 a_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100327 beta_values),
328 act_values))
giuros01b3204e72019-04-01 13:50:22 +0100329{
330 // Validate output
331 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
332}
333TEST_SUITE_END() // FP32
giuros01b3204e72019-04-01 13:50:22 +0100334TEST_SUITE_END() // Float
335TEST_SUITE_END() // GEMMMatrixMulipltyNative
336TEST_SUITE_END() // CL
337} // namespace validation
338} // namespace test
339} // namespace arm_compute