1 /*
2 * Copyright (c) 2017-2023 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/Types.h"
25 #include "arm_compute/runtime/CL/CLTensor.h"
26 #include "arm_compute/runtime/CL/CLTensorAllocator.h"
27 #include "arm_compute/runtime/CL/functions/CLGEMM.h"
28 #include "tests/CL/CLAccessor.h"
29 #include "tests/CL/Helper.h"
30 #include "tests/PaddingCalculator.h"
31 #include "tests/datasets/LargeGEMMDataset.h"
32 #include "tests/datasets/SmallGEMMDataset.h"
33 #include "tests/datasets/TinyGEMMDataset.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 namespace
47 {
48 RelativeTolerance<float> tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */
49 constexpr float abs_tolerance_f32(
50 0.0001f); /**< Absolute tolerance value for comparing reference's output against implementation's output for floating point data types in case using relative tolerance fails because of small values */
51 RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */
52 constexpr float tolerance_num = 0.02f; /**< Tolerance number */
53
54 /** CNN data types */
55 const auto CNNDataTypes = framework::dataset::make("DataType",
56 {
57 DataType::F16,
58 DataType::F32,
59 });
60 } // namespace
61
62 TEST_SUITE(CL)
TEST_SUITE(GEMM)63 TEST_SUITE(GEMM)
64
65 // *INDENT-OFF*
66 // clang-format off
67 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
68 framework::dataset::make("LhsInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::S32), // Unsupported data type
69 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32),
70 }),
71 framework::dataset::make("RhsInfo",{ TensorInfo(TensorShape(8U, 27U), 1, DataType::S32),
72 TensorInfo(TensorShape(8U, 27U), 1, DataType::F32),
73 })),
74 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(8U, 13U), 1, DataType::S32),
75 TensorInfo(TensorShape(8U, 13U), 1, DataType::F32),
76 })),
77 framework::dataset::make("Expected", { false, true })),
78 lhs_info, rhs_info, output_info, expected)
79 {
80 constexpr float alpha = 1.0;
81 constexpr float beta = 0.0;
82 const auto gemm_info = GEMMInfo();
83 bool is_valid = bool(CLGEMM::validate(&lhs_info.clone()->set_is_resizable(true), &rhs_info.clone()->set_is_resizable(true), nullptr, &output_info.clone()->set_is_resizable(true), alpha, beta, gemm_info));
84 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
85 }
86 // clang-format on
87 // *INDENT-ON*
88 template <typename T>
89 using CLGEMMFixture = GEMMValidationFixture<CLTensor, CLAccessor, CLGEMM, T>;
90
91 template <typename T>
92 using CLGEMMOutput3DFixture = GEMMValidationFixture<CLTensor, CLAccessor, CLGEMM, T, false, false, true>;
93
94 template <typename T>
95 using CLGEMMInputOutput3DFixture = GEMMValidationFixture<CLTensor, CLAccessor, CLGEMM, T, false, true, true>;
96
97 template <typename T>
98 using CLBatchedMatMulFixture = GEMMValidationFixture<CLTensor, CLAccessor, CLGEMM, T, true, false, false, false, false, true>;
99
100 TEST_SUITE(Float)
TEST_SUITE(FP16)101 TEST_SUITE(FP16)
102 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(),
103 framework::dataset::make("ReshapeWeights", { true, false })),
104 framework::dataset::make("DataType", DataType::F16)))
105 {
106 // Validate output
107 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
108 }
109 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(),
110 framework::dataset::make("ReshapeWeights", { true })),
111 framework::dataset::make("DataType", DataType::F16)))
112 {
113 // Validate output
114 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
115 }
116 TEST_SUITE_END()
117
TEST_SUITE(FP32)118 TEST_SUITE(FP32)
119 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(),
120 framework::dataset::make("ReshapeWeights", { true, false })),
121 framework::dataset::make("DataType", DataType::F32)))
122 {
123 // Validate output
124 validate(CLAccessor(_target), _reference, tolerance_f32);
125 }
126 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(),
127 framework::dataset::make("ReshapeWeights", { true, false })),
128 framework::dataset::make("DataType", DataType::F32)))
129 {
130 // Validate output
131 validate(CLAccessor(_target), _reference, tolerance_f32, 0.f, abs_tolerance_f32);
132 }
133 TEST_SUITE_END()
TEST_SUITE_END()134 TEST_SUITE_END()
135
136 TEST_SUITE(INPUT_OUTPUT_3D)
137 TEST_SUITE(Float)
138 TEST_SUITE(FP32)
139 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMInputOutput3DFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMInputOutput3DDataset(),
140 framework::dataset::make("ReshapeWeights", { true, false })),
141 framework::dataset::make("DataType", DataType::F32)))
142 {
143 // Validate output
144 validate(CLAccessor(_target), _reference, tolerance_f32);
145 }
146 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMInputOutput3DFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMInputOutput3DDataset(),
147 framework::dataset::make("ReshapeWeights", { true, false })),
148 framework::dataset::make("DataType", DataType::F32)))
149 {
150 // Validate output
151 validate(CLAccessor(_target), _reference, tolerance_f32, 0.f, abs_tolerance_f32);
152 }
153 TEST_SUITE_END() // FP32
154
TEST_SUITE(FP16)155 TEST_SUITE(FP16)
156 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMInputOutput3DFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMInputOutput3DDataset(),
157 framework::dataset::make("ReshapeWeights", { true, false })),
158 framework::dataset::make("DataType", DataType::F16)))
159 {
160 // Validate output
161 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
162 }
163 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMInputOutput3DFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMInputOutput3DDataset(),
164 framework::dataset::make("ReshapeWeights", { true, false })),
165 framework::dataset::make("DataType", DataType::F16)))
166 {
167 // Validate output
168 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
169 }
170 TEST_SUITE_END() // FP16
171
TEST_SUITE_END()172 TEST_SUITE_END() // Float
173 TEST_SUITE_END() // INPUT_OUTPUT_3D
174
175 TEST_SUITE(OUTPUT_3D)
176 TEST_SUITE(Float)
177 TEST_SUITE(FP32)
178 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMOutput3DFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMOutput3DDataset(),
179 framework::dataset::make("ReshapeWeights", { true, false })),
180 framework::dataset::make("DataType", DataType::F32)))
181 {
182 // Validate output
183 validate(CLAccessor(_target), _reference, tolerance_f32);
184 }
185 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMOutput3DFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMOutput3DDataset(),
186 framework::dataset::make("ReshapeWeights", { true, false })),
187 framework::dataset::make("DataType", DataType::F32)))
188 {
189 // Validate output
190 validate(CLAccessor(_target), _reference, tolerance_f32, 0.f, abs_tolerance_f32);
191 }
192 TEST_SUITE_END() // FP32
193
TEST_SUITE(FP16)194 TEST_SUITE(FP16)
195 FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMOutput3DFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMOutput3DDataset(),
196 framework::dataset::make("ReshapeWeights", { true, false })),
197 framework::dataset::make("DataType", DataType::F16)))
198 {
199 // Validate output
200 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
201 }
202 FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMOutput3DFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMOutput3DDataset(),
203 framework::dataset::make("ReshapeWeights", { true, false })),
204 framework::dataset::make("DataType", DataType::F16)))
205 {
206 // Validate output
207 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
208 }
209 TEST_SUITE_END() // FP16
TEST_SUITE_END()210 TEST_SUITE_END() // Float
211 TEST_SUITE_END() // OUTPUT_3D
212
213 TEST_SUITE(BATCHED_MATMUL)
214
215 TEST_SUITE(FP32)
216 FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchedMatMulFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallBatchedMatMulDataset(),
217 framework::dataset::make("ReshapeWeights", { false })),
218 framework::dataset::make("DataType", DataType::F32)))
219 {
220 // Validate output
221 validate(CLAccessor(_target), _reference, tolerance_f32, tolerance_num);
222 }
223 TEST_SUITE_END()
224
TEST_SUITE(FP16)225 TEST_SUITE(FP16)
226 FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchedMatMulFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallBatchedMatMulDataset(),
227 framework::dataset::make("ReshapeWeights", { false })),
228 framework::dataset::make("DataType", DataType::F16)))
229 {
230 // Validate output
231 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
232 }
233 TEST_SUITE_END()
234 TEST_SUITE_END() // BATCHED_MATMUL
235
236 TEST_SUITE_END() // GEMM
237 TEST_SUITE_END() // CL
238 } // namespace validation
239 } // namespace test
240 } // namespace arm_compute
241