blob: 997c510e42836fd014859a800fb903e88343d322 [file] [log] [blame]
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +00001/*
Gian Marco Iodicebacfec52019-01-11 11:30:55 +00002 * Copyright (c) 2018-2019 ARM Limited.
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +00003 *
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/CLGEMMMatrixMultiplyReshapedKernel.h"
25#include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
26#include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
Gian Marco Iodice7026b302019-06-26 17:18:11 +010027#include "arm_compute/core/KernelDescriptors.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000028#include "arm_compute/core/Types.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/runtime/CL/CLTensor.h"
31#include "arm_compute/runtime/CL/CLTensorAllocator.h"
32#include "tests/CL/CLAccessor.h"
33#include "tests/CL/Helper.h"
34#include "tests/PaddingCalculator.h"
35#include "tests/datasets/ShapeDatasets.h"
36#include "tests/framework/Asserts.h"
37#include "tests/framework/Macros.h"
38#include "tests/framework/datasets/Datasets.h"
39#include "tests/validation/Validation.h"
40#include "tests/validation/fixtures/GEMMFixture.h"
41
42namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000048using namespace arm_compute::misc::shape_calculator;
49
50// Create function for CLGEMMReshapeLHSMatrixKernel
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000051using CLGEMMReshapeLHSMatrix = CLSynthetizeFunction<CLGEMMReshapeLHSMatrixKernel>;
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000052
53// Create function for CLGEMMReshapeRHSMatrixKernel
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000054using CLGEMMReshapeRHSMatrix = CLSynthetizeFunction<CLGEMMReshapeRHSMatrixKernel>;
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000055
56// Create function for CLGEMMMatrixMultiplyReshapedKernel
57using CLGEMMMatrixMultiplyReshaped = CLSynthetizeFunction<CLGEMMMatrixMultiplyReshapedKernel>;
58
59// Fixture for CLGEMMMatrixMultiplyReshaped
60template <typename T>
61using CLGEMMMatrixMultiplyReshapedFixture = GEMMMatrixMultiplyReshapedValidationFixture<CLTensor, CLAccessor, T, CLGEMMReshapeLHSMatrix, CLGEMMReshapeRHSMatrix, CLGEMMMatrixMultiplyReshaped>;
62
63// Fixture for CLGEMMMatrixMultiplyReshaped3D
64template <typename T>
65using CLGEMMMatrixMultiplyReshaped3DFixture = GEMMMatrixMultiplyReshaped3DValidationFixture<CLTensor, CLAccessor, T, CLGEMMReshapeLHSMatrix, CLGEMMReshapeRHSMatrix, CLGEMMMatrixMultiplyReshaped>;
66
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000067namespace
68{
69// *INDENT-OFF*
70// clang-format off
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000071RelativeTolerance<float> rel_tolerance_f32(0.001f);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000072constexpr float abs_tolerance_f32(0.0001f);
73
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000074/** Alpha values to test - Precommit */
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000075const auto a_values = framework::dataset::make("alpha", {1.0f, -0.75f} );
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000076
Gian Marco Iodicee16c8902019-06-14 16:11:10 +010077/** Beta values to test - Precommit */
Gian Marco Iodiced820db62019-08-05 14:23:23 +010078const auto beta_values = framework::dataset::make("beta", {-0.35f, 0.0f} );
Gian Marco Iodicee16c8902019-06-14 16:11:10 +010079
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000080/** M values to test */
81const auto m_values = framework::dataset::make("M", 37);
82
Gian Marco Iodice9382ab32018-12-17 15:12:07 +000083/** M_W values to test */
84const auto m_w_values = framework::dataset::make("M_W", 5);
85
86/** M_H values to test */
87const auto m_h_values = framework::dataset::make("M_H", 7);
88
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000089/** N values to test */
90const auto n_values = framework::dataset::make("N", 51);
91
92/** K values to test */
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000093const auto k_values = framework::dataset::make("K", 23);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000094
95/** Batch size values to test */
96const auto b_values = framework::dataset::make("batch_size", 1, 3);
97
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +010098/** Activation values to test */
99const auto act_values = framework::dataset::make("Activation",
100{
101 ActivationLayerInfo(),
102 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
103});
104
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000105/** M0 values to test - Precommit */
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000106const auto m0_values_precommit = framework::dataset::make("M0", {4, 6});
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000107
108/** N0 values to test - Precommit */
Gian Marco Iodiced820db62019-08-05 14:23:23 +0100109const auto n0_values_precommit = framework::dataset::make("N0", { 4 });
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000110
111/** K0 values to test - Precommit */
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000112const auto k0_values_precommit = framework::dataset::make("K0", { 4 });
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000113
114/** V0 values to test - Precommit */
115const auto v0_values_precommit = framework::dataset::make("V0", 1, 3);
116
117/** H0 values to test - Precommit */
118const auto h0_values_precommit = framework::dataset::make("H0", 1, 3);
119
120/** M0 values to test - Nightly */
Gian Marco Iodicebacfec52019-01-11 11:30:55 +0000121const auto m0_values_nightly = framework::dataset::make("M0", 2, 7);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000122
123/** N0 values to test - Nightly */
Gian Marco Iodicebacfec52019-01-11 11:30:55 +0000124const auto n0_values_nightly = framework::dataset::make("N0", { 2, 3, 4, 8 });
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000125
126/** K0 values to test - Nightly */
Gian Marco Iodicebacfec52019-01-11 11:30:55 +0000127const auto k0_values_nightly = framework::dataset::make("K0", { 2, 3, 4, 8 });
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000128
129/** V0 values to test - Nightly */
130const auto v0_values_nightly = framework::dataset::make("V0", 1, 4);
131
132/** H0 values to test - Nightly */
133const auto h0_values_nightly = framework::dataset::make("H0", 1, 4);
134
135/** Interleave values to test with LHS matrix */
136const auto i_values_lhs = framework::dataset::make("interleave_lhs", { true, false });
137
138/** Interleave values to test with RHS matrix */
139const auto i_values_rhs = framework::dataset::make("interleave_rhs", { true, false });
140
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100141/** Broadcast bias from vector to matrix */
Gian Marco Iodiced820db62019-08-05 14:23:23 +0100142const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", { false, true } );
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100143
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000144/** Configuration test */
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100145void 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, unsigned int v0_value, unsigned int h0_value, bool i_value_lhs, bool i_value_rhs, bool broadcast_bias, DataType data_type, const ActivationLayerInfo &act_info)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000146{
147 const unsigned int M = m_value;
148 const unsigned int N = n_value;
149 const unsigned int K = k_value;
150
151 GEMMLHSMatrixInfo lhs_info;
152 lhs_info.m0 = m0_value;
153 lhs_info.k0 = k0_value;
154 lhs_info.v0 = v0_value;
155 lhs_info.interleave = i_value_lhs;
156 lhs_info.transpose = false;
157
158 GEMMRHSMatrixInfo rhs_info;
159 rhs_info.n0 = n0_value;
160 rhs_info.k0 = k0_value;
161 rhs_info.h0 = h0_value;
162 rhs_info.interleave = i_value_rhs;
163 rhs_info.transpose = true;
164
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100165 GEMMKernelInfo kernel_info;
166 kernel_info.m = M;
167 kernel_info.n = N;
168 kernel_info.k = K;
169 kernel_info.depth_output_gemm3d = 0;
170 kernel_info.reinterpret_input_as_3d = false;
171 kernel_info.broadcast_bias = broadcast_bias;
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100172 kernel_info.activation_info = act_info;
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000173
174 const TensorShape lhs_shape(K, M, b_value);
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000175 const TensorShape lhs_shape_reshaped = compute_lhs_reshaped_shape(TensorInfo(lhs_shape, 1, data_type),
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000176 lhs_info,
177 false);
178
179 const TensorShape rhs_shape(N, K, b_value);
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000180 const TensorShape rhs_shape_reshaped = compute_rhs_reshaped_shape(TensorInfo(rhs_shape, 1, data_type),
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000181 rhs_info);
182
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000183 const TensorShape dst_shape = compute_mm_shape(TensorInfo(lhs_shape_reshaped, 1, data_type),
184 TensorInfo(rhs_shape_reshaped, 1, data_type),
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100185 kernel_info);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000186
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100187 const TensorShape bias_shape(N,
188 broadcast_bias? 1 : M,
189 broadcast_bias? 1 : b_value);
190
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000191 // Create tensors
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000192 CLTensor lhs_reshaped = create_tensor<CLTensor>(lhs_shape_reshaped, data_type);
193 CLTensor rhs_reshaped = create_tensor<CLTensor>(rhs_shape_reshaped, data_type);
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100194 CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type);
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000195 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000196
197 ARM_COMPUTE_EXPECT(lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
198 ARM_COMPUTE_EXPECT(rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100199 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000200 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
201
202 // Create and configure function
203 CLGEMMMatrixMultiplyReshaped gemm;
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100204 gemm.configure(&lhs_reshaped, &rhs_reshaped, &bias, &dst, 1.0f, 1.0f, lhs_info, rhs_info, kernel_info);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000205}
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000206} // namespace
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000207
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000208TEST_SUITE(CL)
209TEST_SUITE(GEMMMatrixMultiplyReshaped)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000210TEST_SUITE(Float)
211TEST_SUITE(FP32)
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100212DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000213 m_values,
214 n_values),
215 k_values),
216 framework::dataset::make("batch_size", 1)),
217 m0_values_precommit),
218 n0_values_precommit),
219 k0_values_precommit),
220 v0_values_precommit),
221 h0_values_precommit),
222 i_values_lhs),
223 i_values_rhs),
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100224 broadcast_bias_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100225 act_values),
226m_value, n_value, k_value, b_value, m0_value, n0_value, k0_value, v0_value, h0_value, i_value_lhs, i_value_rhs, broadcast_bias, act_value)
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000227{
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100228 validate_configuration(m_value, n_value, k_value, b_value, m0_value, n0_value, k0_value, v0_value, h0_value, i_value_lhs, i_value_rhs, broadcast_bias, DataType::F32, act_value);
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000229}
230
231FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedFixture<float>, framework::DatasetMode::ALL,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100232 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000233 m_values,
234 n_values),
235 k_values),
236 b_values),
237 m0_values_precommit),
238 n0_values_precommit),
239 k0_values_precommit),
240 v0_values_precommit),
241 h0_values_precommit),
242 i_values_lhs),
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000243 i_values_rhs),
244 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100245 a_values),
246 beta_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100247 broadcast_bias_values),
248 act_values))
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000249{
250 // Validate output
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000251 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000252}
253
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000254FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMMatrixMultiplyReshapedFixture<float>, framework::DatasetMode::NIGHTLY,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100255 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000256 m_values,
257 n_values),
258 k_values),
259 b_values),
260 m0_values_nightly),
261 n0_values_nightly),
262 k0_values_nightly),
263 v0_values_nightly),
264 h0_values_nightly),
265 i_values_lhs),
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000266 i_values_rhs),
267 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100268 a_values),
269 beta_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100270 broadcast_bias_values),
271 act_values))
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000272{
273 // Validate output
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000274 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000275}
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000276
277FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyReshaped3DFixture<float>, framework::DatasetMode::ALL,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100278 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000279 m_w_values,
280 m_h_values),
281 n_values),
282 k_values),
283 b_values),
284 m0_values_precommit),
285 n0_values_precommit),
286 k0_values_precommit),
287 v0_values_precommit),
288 h0_values_precommit),
289 i_values_lhs),
290 i_values_rhs),
291 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100292 a_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100293 beta_values),
294 act_values))
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000295{
296 // Validate output
297 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
298}
299
300FIXTURE_DATA_TEST_CASE(RunLarge3D, CLGEMMMatrixMultiplyReshaped3DFixture<float>, framework::DatasetMode::NIGHTLY,
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100301 combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000302 m_w_values,
303 m_h_values),
304 n_values),
305 k_values),
306 b_values),
307 m0_values_nightly),
308 n0_values_nightly),
309 k0_values_nightly),
310 v0_values_nightly),
311 h0_values_nightly),
312 i_values_lhs),
313 i_values_rhs),
314 framework::dataset::make("DataType", DataType::F32)),
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100315 a_values),
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100316 beta_values),
317 act_values))
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000318{
319 // Validate output
320 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
321}
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000322TEST_SUITE_END() // FP32
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000323TEST_SUITE_END() // Float
Gian Marco Iodiced1f54762019-07-19 09:54:47 +0100324TEST_SUITE_END() // GEMMMatrixMultiplyReshaped
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000325TEST_SUITE_END() // CL
326} // namespace validation
327} // namespace test
328} // namespace arm_compute