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