1 /*
2 * Copyright (c) 2017-2022 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/core/utils/misc/Traits.h"
26 #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
27 #include "arm_compute/runtime/RuntimeContext.h"
28 #include "arm_compute/runtime/Tensor.h"
29 #include "arm_compute/runtime/TensorAllocator.h"
30 #include "src/common/cpuinfo/CpuIsaInfo.h"
31 #include "src/cpu/kernels/CpuActivationKernel.h"
32 #include "tests/NEON/Accessor.h"
33 #include "tests/PaddingCalculator.h"
34 #include "tests/datasets/ActivationFunctionsDataset.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/ActivationLayerFixture.h"
41
42 #include "arm_compute/Acl.hpp"
43 #include "support/Requires.h"
44
45 namespace arm_compute
46 {
47 namespace test
48 {
49 namespace validation
50 {
51 namespace
52 {
53 RelativeTolerance<float> tolerance_float_sqrt(0.0001f);
54
55 /** Define relative tolerance of the activation layer.
56 *
57 * @param[in] data_type The data type used.
58 * @param[in] activation The activation function used.
59 *
60 * @return Relative tolerance depending on the activation function.
61 */
relative_tolerance(DataType data_type,ActivationLayerInfo::ActivationFunction activation)62 RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
63 {
64 switch(activation)
65 {
66 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
67 case ActivationLayerInfo::ActivationFunction::ELU:
68 case ActivationLayerInfo::ActivationFunction::SQRT:
69 case ActivationLayerInfo::ActivationFunction::TANH:
70 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
71 case ActivationLayerInfo::ActivationFunction::SWISH:
72 case ActivationLayerInfo::ActivationFunction::GELU:
73 switch(data_type)
74 {
75 case DataType::F16:
76 #if defined(ENABLE_SVE)
77 return RelativeTolerance<float>(0.25f);
78 #else // !defined(ENABLE_SVE)
79 return RelativeTolerance<float>(0.1f);
80 #endif // defined(ENABLE_SVE)
81 default:
82 return RelativeTolerance<float>(0.05f);
83 }
84 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
85 switch(data_type)
86 {
87 case DataType::F16:
88 #if defined(ENABLE_SVE)
89 return RelativeTolerance<float>(0.9f);
90 #else // !defined(ENABLE_SVE)
91 return RelativeTolerance<float>(0.01f);
92 #endif // defined(ENABLE_SVE)
93 default:
94 return RelativeTolerance<float>(0.00001f);
95 }
96 default:
97 return RelativeTolerance<float>(0.f);
98 }
99 }
100
101 /** Define absolute tolerance of the activation layer.
102 *
103 * @param[in] data_type The data type used.
104 * @param[in] activation The activation function used.
105 *
106 * @return Absolute tolerance depending on the activation function.
107 */
absolute_tolerance(DataType data_type,ActivationLayerInfo::ActivationFunction activation)108 AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
109 {
110 switch(activation)
111 {
112 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
113 case ActivationLayerInfo::ActivationFunction::SQRT:
114 case ActivationLayerInfo::ActivationFunction::TANH:
115 case ActivationLayerInfo::ActivationFunction::SWISH:
116 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
117 switch(data_type)
118 {
119 case DataType::F16:
120 #if defined(ENABLE_SVE)
121 return AbsoluteTolerance<float>(0.25f);
122 #else // !defined(ENABLE_SVE)
123 return AbsoluteTolerance<float>(0.01f);
124 #endif // defined(ENABLE_SVE)
125 default:
126 return AbsoluteTolerance<float>(0.00001f);
127 }
128 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
129 switch(data_type)
130 {
131 case DataType::F16:
132 #if defined(ENABLE_SVE)
133 return AbsoluteTolerance<float>(0.9f);
134 #else // !defined(ENABLE_SVE)
135 return AbsoluteTolerance<float>(0.01f);
136 #endif // defined(ENABLE_SVE)
137 default:
138 return AbsoluteTolerance<float>(0.00001f);
139 }
140 default:
141 return AbsoluteTolerance<float>(0.f);
142 }
143 }
144
145 /** Define absolute tolerance of the activation layer for qasymm8.
146 *
147 * @param[in] activation The activation function used.
148 *
149 * @return Absolute tolerance depending on the activation function.
150 */
tolerance_qasymm8(ActivationLayerInfo::ActivationFunction activation)151 AbsoluteTolerance<uint8_t> tolerance_qasymm8(ActivationLayerInfo::ActivationFunction activation)
152 {
153 switch(activation)
154 {
155 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
156 case ActivationLayerInfo::ActivationFunction::SQRT:
157 case ActivationLayerInfo::ActivationFunction::TANH:
158 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
159 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
160 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
161 return AbsoluteTolerance<uint8_t>(1);
162 default:
163 return AbsoluteTolerance<uint8_t>(0);
164 }
165 }
166
167 constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
168
169 /** CNN data types */
170 const auto CNNDataTypes = framework::dataset::make("DataType",
171 {
172 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
173 DataType::F16,
174 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
175 DataType::F32,
176 });
177
178 const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(),
179 framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::HARD_SWISH, ActivationLayerInfo::ActivationFunction::SWISH }));
180
181 /** Input data sets. */
182 const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), NeonActivationFunctionsDataset), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
183
184 template <typename T, ARM_COMPUTE_REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
test_float_sqrt_boundary_value()185 void test_float_sqrt_boundary_value()
186 {
187 constexpr auto vector_size = uint32_t{ 16 };
188
189 auto data_type = DataType::F32;
190 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
191 data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
192 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
193
194 const auto boundary_value_vector = std::vector<T>
195 {
196 std::numeric_limits<T>::min(),
197 T(0),
198 std::numeric_limits<T>::epsilon(),
199 std::numeric_limits<T>::max(),
200 };
201
202 // the following size ensures that the whole logic (vector + left-over) to be tested
203 // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
204 auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
205 auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
206 auto src = create_tensor<Tensor>(shape, data_type);
207
208 auto act = NEActivationLayer{};
209 act.configure(&src, nullptr, info);
210 src.allocator()->allocate();
211 library->fill_static_values(Accessor(src), boundary_value_vector);
212 act.run();
213
214 auto reference_src = SimpleTensor<T> { shape, data_type };
215 library->fill_static_values(reference_src, boundary_value_vector);
216 auto reference_dst = reference::activation_layer<T>(reference_src, info);
217
218 validate(Accessor(src), reference_dst, tolerance_float_sqrt);
219 }
220 } // namespace
221
222 TEST_SUITE(NEON)
TEST_SUITE(ActivationLayer)223 TEST_SUITE(ActivationLayer)
224
225 /** Test case for memory injection in @ref cpu::CpuWinogradConv2d.
226 *
227 * Configure the operator once and inject memory at run-time in multiple executions.
228 *
229 * Checks performed in order:
230 * - Both runs compute the same output
231 */
232 TEST_CASE(ActivationAPI, framework::DatasetMode::ALL)
233 {
234 acl::StatusCode err = acl::StatusCode::Success;
235
236 // Create context & Queue
237 acl::Context ctx(acl::Target::Cpu, &err);
238 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
239
240 acl::Queue queue(ctx, &err);
241 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
242
243 // Create activation operator
244 acl::TensorDescriptor src_info({ 2, 3 }, acl::DataType::Float32);
245 acl::TensorDescriptor dst_info({ 2, 3 }, acl::DataType::Float32);
246 acl::ActivationDesc desc{ AclRelu, 6.f, 0.f, false };
247
248 acl::Activation act(ctx, src_info, dst_info, desc, &err);
249 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
250
251 // Create tensors and feed
252 acl::Tensor src(ctx, src_info, &err);
253 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
254 acl::Tensor dst(ctx, dst_info, &err);
255 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
256
257 acl::TensorPack pack(ctx);
258 err = pack.add(src, ACL_SRC);
259 err = pack.add(dst, ACL_DST);
260 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
261
262 // Execute operator
263 err = act.run(queue, pack);
264 ARM_COMPUTE_ASSERT(err == acl::StatusCode::Success);
265 }
266
267 // *INDENT-OFF*
268 // clang-format off
269 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
270 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
271 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
272 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
273 }),
274 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
275 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
276 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
277 })),
278 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
279 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
280 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
281 })),
282 framework::dataset::make("Expected", { false, true, false})),
283 input_info, output_info, act_info, expected)
284 {
285 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
286 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
287 }
288
289 DATA_TEST_CASE(KernelSelection, framework::DatasetMode::ALL, concat(concat(
290 combine(framework::dataset::make("CpuExt", std::string("NEON")),
291 framework::dataset::make("DataType", { DataType::F32,
292 DataType::F16,
293 DataType::QASYMM8,
294 DataType::QASYMM8_SIGNED,
295 DataType::QSYMM16
296 })),
297 combine(framework::dataset::make("CpuExt", std::string("SVE")),
298 framework::dataset::make("DataType", { DataType::F32,
299 DataType::F16,
300 }))),
301 combine(framework::dataset::make("CpuExt", std::string("SVE2")),
302 framework::dataset::make("DataType", { DataType::QASYMM8,
303 DataType::QASYMM8_SIGNED,
304 DataType::QSYMM16
305 }))),
306 cpu_ext, data_type)
307 {
308 using namespace cpu::kernels;
309
310 cpuinfo::CpuIsaInfo cpu_isa{};
311 cpu_isa.neon = (cpu_ext == "NEON");
312 cpu_isa.sve = (cpu_ext == "SVE");
313 cpu_isa.sve2 = (cpu_ext == "SVE2");
314 cpu_isa.fp16 = (data_type == DataType::F16);
315
316 const auto *selected_impl = CpuActivationKernel::get_implementation(ActivationDataTypeISASelectorData{data_type, CPUModel::GENERIC, cpu_isa,ActivationLayerInfo::ActivationFunction::BOUNDED_RELU}, cpu::KernelSelectionType::Preferred);
317
318 ARM_COMPUTE_ERROR_ON_NULLPTR(selected_impl);
319
320 std::string expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_activation";
321 std::string actual = selected_impl->name;
322
323 ARM_COMPUTE_EXPECT_EQUAL(expected, actual, framework::LogLevel::ERRORS);
324 }
325 // clang-format on
326 // *INDENT-ON*
327
328 template <typename T>
329 using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
330
331 TEST_SUITE(Float)
332 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
TEST_SUITE(FP16)333 TEST_SUITE(FP16)
334 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
335 {
336 test_float_sqrt_boundary_value<half>();
337 }
338 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
339 framework::dataset::make("DataType",
340 DataType::F16)))
341 {
342 // Validate output
343 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
344 }
345 TEST_SUITE_END() // FP16
346 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
347
TEST_SUITE(FP32)348 TEST_SUITE(FP32)
349 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
350 {
351 test_float_sqrt_boundary_value<float>();
352 }
353 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
354 DataType::F32)))
355
356 {
357 // Validate output
358 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
359 }
360 TEST_SUITE_END() // FP32
361 TEST_SUITE_END() // Float
362
363 template <typename T>
364 using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
365
366 /** Input data sets. */
367 const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction",
368 {
369 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
370 ActivationLayerInfo::ActivationFunction::RELU,
371 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
372 ActivationLayerInfo::ActivationFunction::LOGISTIC,
373 ActivationLayerInfo::ActivationFunction::TANH,
374 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
375 });
376
377 const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }),
378 concat(QuantizedActivationFunctionsDataset, framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
379 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
380
381 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)382 TEST_SUITE(QASYMM8)
383 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
384 framework::dataset::make("DataType",
385 DataType::QASYMM8)),
386 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
387 {
388 // Validate output
389 validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
390 }
391 TEST_SUITE_END() // QASYMM8
392
TEST_SUITE(QASYMM8_SIGNED)393 TEST_SUITE(QASYMM8_SIGNED)
394 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
395 framework::dataset::make("DataType",
396 DataType::QASYMM8_SIGNED)),
397 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
398 {
399 // Validate output
400 validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
401 }
402 TEST_SUITE_END() // QASYMM8_SIGNED
403
404 /** Input data sets. */
405 const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction",
406 {
407 ActivationLayerInfo::ActivationFunction::LOGISTIC,
408 ActivationLayerInfo::ActivationFunction::TANH,
409 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
410 });
411 const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
412 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
413
414 TEST_SUITE(QSYMM16)
415 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
416 framework::dataset::make("DataType",
417 DataType::QSYMM16)),
418 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
419 {
420 // Validate output
421 validate(Accessor(_target), _reference, tolerance_qsymm16);
422 }
423 TEST_SUITE_END() // QSYMM16
424 TEST_SUITE_END() // Quantized
425
426 TEST_SUITE_END() // ActivationLayer
427 TEST_SUITE_END() // Neon
428 } // namespace validation
429 } // namespace test
430 } // namespace arm_compute
431