Compute Library
 22.11
ActivationLayer.cpp
Go to the documentation of this file.
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"
32 #include "tests/NEON/Accessor.h"
34 #include "tests/datasets/ActivationFunctionsDataset.h"
35 #include "tests/datasets/ShapeDatasets.h"
37 #include "tests/framework/Macros.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  */
62 RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
63 {
64  switch(activation)
65  {
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  }
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  */
108 AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
109 {
110  switch(activation)
111  {
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  }
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  */
151 AbsoluteTolerance<uint8_t> tolerance_qasymm8(ActivationLayerInfo::ActivationFunction activation)
152 {
153  switch(activation)
154  {
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
174 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
176 });
177 
178 const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(),
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)>
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),
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)
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 {
235 
236  // Create context & Queue
237  acl::Context ctx(acl::Target::Cpu, &err);
239 
240  acl::Queue queue(ctx, &err);
242 
243  // Create activation operator
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);
250 
251  // Create tensors and feed
252  acl::Tensor src(ctx, src_info, &err);
254  acl::Tensor dst(ctx, dst_info, &err);
256 
257  acl::TensorPack pack(ctx);
258  err = pack.add(src, ACL_SRC);
259  err = pack.add(dst, ACL_DST);
261 
262  // Execute operator
263  err = act.run(queue, pack);
265 }
266 
267 // *INDENT-OFF*
268 // clang-format off
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  })),
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));
287 }
288 
290  combine(framework::dataset::make("CpuExt", std::string("NEON")),
296  })),
297  combine(framework::dataset::make("CpuExt", std::string("SVE")),
300  }))),
301  combine(framework::dataset::make("CpuExt", std::string("SVE2")),
305  }))),
306  cpu_ext, data_type)
307 {
308  using namespace cpu::kernels;
309 
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, cpu_isa,ActivationLayerInfo::ActivationFunction::BOUNDED_RELU}, cpu::KernelSelectionType::Preferred);
317 
319 
320  std::string expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_activation";
321  std::string actual = selected_impl->name;
322 
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
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 
348 TEST_SUITE(FP32)
349 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
350 {
351  test_float_sqrt_boundary_value<float>();
352 }
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 {
375 });
376 
379  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
380 
381 TEST_SUITE(Quantized)
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 
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 {
410 });
411 const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
412  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
413 
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
#define ARM_COMPUTE_ASSERT(cond)
Definition: Validate.h:37
Retrieve the best implementation available for the given Cpu ISA, ignoring the build flags...
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
Tensor Descriptor class.
Definition: Acl.hpp:503
Tensor pack class.
Definition: Acl.hpp:684
StatusCode add(Tensor &tensor, int32_t slot_id)
Add tensor to tensor pack.
Definition: Acl.hpp:728
const CpuCastKernel::CastKernel * selected_impl
Definition: Cast.cpp:205
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
[NEActivationLayer snippet]
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:353
Tensor class.
Definition: Acl.hpp:584
Activation Layer Information class.
Definition: Types.h:1639
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
StatusCode run(Queue &queue, TensorPack &pack)
Run an operator on a given input list.
Definition: Acl.hpp:768
cpuinfo::CpuIsaInfo cpu_isa
Definition: Cast.cpp:207
ActivationFunction
Available activation functions.
Definition: Types.h:1643
std::string cpu_impl_dt(const DataType &data_type)
Returns the suffix string of CPU kernel implementation names based on the given data type...
Definition: Utils.h:1245
1 channel, 1 F16 per channel
ActivationValidationFixture< Tensor, Accessor, NEActivationLayer, T > NEActivationLayerFixture
CPU ISA (Instruction Set Architecture) information.
Definition: CpuIsaInfo.h:37
Quantization information.
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
StatusCode
Status code enum.
Definition: Acl.hpp:50
const auto QuantizedActivationFunctionsDataset
Input data sets.
validate(CLAccessor(output_state), expected_output)
Cpu target that leverages SIMD.
Rectifier.
TensorInfo src_info(src_shape, 1, data_type)
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ARM_COMPUTE_ERROR_ON_NULLPTR(selected_impl)
ARM_COMPUTE_EXPECT_EQUAL(expected, actual, framework::LogLevel::ERRORS)
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
quantized, asymmetric fixed-point 8-bit number signed
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
DataType
Available data types.
Definition: Types.h:79
Queue classData type enumeration.
Definition: Acl.hpp:416
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
Context classAvailable tuning modes.
Definition: Acl.hpp:317