1 /*
2 * Copyright (c) 2017-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/Types.h"
25 #include "arm_compute/runtime/NEON/functions/NEGEMM.h"
26 #include "arm_compute/runtime/Tensor.h"
27 #include "arm_compute/runtime/TensorAllocator.h"
28 #include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
29 #include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
30 #include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
31 #include "tests/NEON/Accessor.h"
32 #include "tests/NEON/Helper.h"
33 #include "tests/PaddingCalculator.h"
34 #include "tests/datasets/LargeGEMMDataset.h"
35 #include "tests/datasets/SmallGEMMDataset.h"
36 #include "tests/datasets/TinyGEMMDataset.h"
37 #include "tests/framework/Asserts.h"
38 #include "tests/framework/Macros.h"
39 #include "tests/framework/datasets/Datasets.h"
40 #include "tests/validation/Validation.h"
41 #include "tests/validation/fixtures/GEMMFixture.h"
42 #include "tests/validation/fixtures/GEMMInterleave4x4Fixture.h"
43 #include "tests/validation/fixtures/GEMMTranspose1xWFixture.h"
44
45 namespace arm_compute
46 {
47 namespace test
48 {
49 namespace validation
50 {
51 namespace
52 {
53 constexpr AbsoluteTolerance<float> tolerance_f(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for FP32 data types */
54 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
55 RelativeTolerance<half_float::half> rel_tolerance_f16(half(0.2)); /**< Relative tolerance value for comparing reference's output against implementation's output for FP16 data types */
56 const AbsoluteTolerance<float> abs_tolerance_f16(0.2f); /**< Absolute tolerance value for comparing reference's output against implementation's output for FP16 data types */
57 constexpr float tolerance_num = 0.07f; /**< Tolerance number for FP16 data types */
58 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
59 /** CNN data types */
60 const auto CNNDataTypes = framework::dataset::make("DataType",
61 {
62 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
63 DataType::F16,
64 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
65 DataType::F32,
66 });
67
68 const auto data_interleave = framework::dataset::make("M", 8, 12) * framework::dataset::make("N", 8, 12);
69 const auto data_transpose = framework::dataset::make("M", 8, 14) * framework::dataset::make("N", 7, 14);
70
71 /** Zero padding test */
72 template <typename FunctionType>
validate_zero_padding(unsigned int dim0_value,unsigned int dim1_value)73 bool validate_zero_padding(unsigned int dim0_value, unsigned int dim1_value)
74 {
75 const TensorShape in_shape(dim0_value, dim1_value);
76
77 // Create tensors
78 Tensor in = create_tensor<Tensor>(in_shape, DataType::U32);
79 Tensor dst;
80
81 ARM_COMPUTE_EXPECT(in.info()->is_resizable(), framework::LogLevel::ERRORS);
82
83 // Validate zero-padding
84 FunctionType func;
85
86 func.configure(&in, &dst);
87
88 return in.info()->padding().empty();
89 }
90
91 /* Zero padding test for GEMM kernels */
validate_gemm_zero_padding(const TensorShape shape0,const TensorShape shape1)92 bool validate_gemm_zero_padding(const TensorShape shape0, const TensorShape shape1)
93 {
94 // Create tensors
95 Tensor in0 = create_tensor<Tensor>(shape0, DataType::F32);
96 Tensor in1 = create_tensor<Tensor>(shape1, DataType::F32);
97 Tensor dst;
98
99 // Validate zero-padding
100 NEGEMMMatrixMultiplyKernel gemm;
101 gemm.configure(&in0, &in1, &dst, 1.0, false);
102
103 return in0.info()->padding().empty() && in1.info()->padding().empty() && dst.info()->padding().empty();
104 }
105 } // namespace
106
107 TEST_SUITE(NEON)
108 TEST_SUITE(GEMM)
109
110 TEST_SUITE(TRANSPOSE_1XW)
111 using NEGEMMTranspose1xW = NESynthetizeFunctionWithZeroConstantBorder<NEGEMMTranspose1xWKernel, 4>;
112 DATA_TEST_CASE(ValidateZeroPadding, framework::DatasetMode::ALL, zip(
113 framework::dataset::make("N", { 1, 23, 63, 101 }),
114 framework::dataset::make("K", { 1, 47, 29, 27 })),
115 n_value, k_value)
116 {
117 bool status = validate_zero_padding<NEGEMMTranspose1xWKernel>(n_value, k_value);
118 ARM_COMPUTE_EXPECT(status, framework::LogLevel::ERRORS);
119 }
120
121 TEST_SUITE(U32)
122 using NEGEMMTranspose1xWFixture = GEMMTranspose1xWValidationFixture<Tensor, Accessor, NEGEMMTranspose1xW, uint32_t>;
123 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMTranspose1xWFixture, framework::DatasetMode::PRECOMMIT, data_transpose * framework::dataset::make("DataType", DataType::U32))
124 {
125 // Validate output
126 validate(Accessor(_target), _reference);
127 }
128 TEST_SUITE_END() // U32
129
130 TEST_SUITE(U16)
131 using NEGEMMTranspose1xWFixture = GEMMTranspose1xWValidationFixture<Tensor, Accessor, NEGEMMTranspose1xW, uint16_t>;
132 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMTranspose1xWFixture, framework::DatasetMode::PRECOMMIT, data_transpose * framework::dataset::make("DataType", DataType::U16))
133 {
134 // Validate output
135 validate(Accessor(_target), _reference);
136 }
137 TEST_SUITE_END() // U16
138
139 TEST_SUITE(U8)
140 using NEGEMMTranspose1xWFixture = GEMMTranspose1xWValidationFixture<Tensor, Accessor, NEGEMMTranspose1xW, uint8_t>;
141 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMTranspose1xWFixture, framework::DatasetMode::PRECOMMIT, data_transpose * framework::dataset::make("DataType", DataType::U8))
142 {
143 // Validate output
144 validate(Accessor(_target), _reference);
145 }
146 TEST_SUITE_END() // U8
147
148 TEST_SUITE_END() // TRANSPOSE_1XW
149
150 TEST_SUITE(INTERLEAVE_4X4)
151 using NEGEMMInterleave4x4 = NESynthetizeFunctionWithZeroConstantBorder<NEGEMMInterleave4x4Kernel, 4>;
152
153 DATA_TEST_CASE(ValidateZeroPadding, framework::DatasetMode::ALL, zip(
154 framework::dataset::make("M", { 1, 23, 63, 101 }),
155 framework::dataset::make("K", { 1, 47, 29, 27 })),
156 m_value, k_value)
157 {
158 bool status = validate_zero_padding<NEGEMMInterleave4x4Kernel>(m_value, k_value);
159 ARM_COMPUTE_EXPECT(status, framework::LogLevel::ERRORS);
160 }
161
162 TEST_SUITE(U32)
163 using NEGEMMInterleave4x4Fixture = GEMMInterleave4x4ValidationFixture<Tensor, Accessor, NEGEMMInterleave4x4, uint32_t>;
164 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMInterleave4x4Fixture, framework::DatasetMode::PRECOMMIT, data_interleave * framework::dataset::make("DataType", DataType::U32))
165 {
166 // Validate output
167 validate(Accessor(_target), _reference);
168 }
169 TEST_SUITE_END() // U32
170
171 TEST_SUITE(U16)
172 using NEGEMMInterleave4x4Fixture = GEMMInterleave4x4ValidationFixture<Tensor, Accessor, NEGEMMInterleave4x4, uint16_t>;
173 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMInterleave4x4Fixture, framework::DatasetMode::PRECOMMIT, data_interleave * framework::dataset::make("DataType", DataType::U16))
174 {
175 // Validate output
176 validate(Accessor(_target), _reference);
177 }
178 TEST_SUITE_END() // U16
179
180 TEST_SUITE(U8)
181 using NEGEMMInterleave4x4Fixture = GEMMInterleave4x4ValidationFixture<Tensor, Accessor, NEGEMMInterleave4x4, uint8_t>;
182 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMInterleave4x4Fixture, framework::DatasetMode::PRECOMMIT, data_interleave * framework::dataset::make("DataType", DataType::QASYMM8))
183 {
184 // Validate output
185 validate(Accessor(_target), _reference);
186 }
187 TEST_SUITE_END() // U8
188
189 TEST_SUITE_END() // INTERLEAVE_4X4
190
191 //TODO(COMPMID-415): Validate valid region
192
193 template <typename T>
194 using NEGEMMFixture = GEMMValidationFixture<Tensor, Accessor, NEGEMM, T>;
195
196 template <typename T>
197 using NEGEMMFixtureDisabledC = GEMMValidationFixture<Tensor, Accessor, NEGEMM, T, true>;
198
199 TEST_SUITE(Float)
200 DATA_TEST_CASE(ValidateZeroPadding, framework::DatasetMode::ALL, zip(framework::dataset::make("In0", { TensorShape(21U, 13U),
201 TensorShape(31U, 1U),
202 TensorShape(31U, 1U),
203 TensorShape(8U, 2U),
204 TensorShape(38U, 12U),
205 TensorShape(32U, 1U)
206 }),
207 framework::dataset::make("In1", { TensorShape(33U, 21U),
208 TensorShape(23U, 31U),
209 TensorShape(23U, 31U),
210 TensorShape(16U, 8U),
211 TensorShape(21U, 38U),
212 TensorShape(17U, 32U)
213 })),
214 shape0, shape1)
215 {
216 bool status = validate_gemm_zero_padding(shape0, shape1);
217 ARM_COMPUTE_EXPECT(status, framework::LogLevel::ERRORS);
218 }
219
220 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
221 TEST_SUITE(FP16)
222 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(),
223 framework::dataset::make("ReshapeWeights", { true, false })),
224 framework::dataset::make("DataType", DataType::F16)))
225 {
226 // Validate output
227 validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num, abs_tolerance_f16);
228 }
229 FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(),
230 framework::dataset::make("ReshapeWeights", { true, false })),
231
232 framework::dataset::make("DataType", DataType::F16)))
233 {
234 // Validate output
235 validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num, abs_tolerance_f16);
236 }
237 TEST_SUITE_END()
238 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
239
TEST_SUITE(FP32)240 TEST_SUITE(FP32)
241 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(),
242 framework::dataset::make("ReshapeWeights", { true, false })),
243
244 framework::dataset::make("DataType", DataType::F32)))
245 {
246 // Validate output
247 validate(Accessor(_target), _reference, tolerance_f);
248 }
249 FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(),
250 framework::dataset::make("ReshapeWeights", { true, false })),
251
252 framework::dataset::make("DataType", DataType::F32)))
253 {
254 // Validate output
255 validate(Accessor(_target), _reference, tolerance_f);
256 }
257 TEST_SUITE(DisabledC)
258 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixtureDisabledC<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(),
259 framework::dataset::make("ReshapeWeights", { true, false })),
260
261 framework::dataset::make("DataType", DataType::F32)))
262 {
263 // Validate output
264 validate(Accessor(_target), _reference, tolerance_f);
265 }
266 TEST_SUITE_END()
267
268 TEST_SUITE_END()
269 TEST_SUITE_END()
270
271 TEST_SUITE_END()
272 TEST_SUITE_END()
273 } // namespace validation
274 } // namespace test
275 } // namespace arm_compute
276