Compute Library
 22.05
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  {
71  switch(data_type)
72  {
73  case DataType::F16:
74 #if defined(ENABLE_SVE)
75  return RelativeTolerance<float>(0.25f);
76 #else // !defined(ENABLE_SVE)
77  return RelativeTolerance<float>(0.1f);
78 #endif // defined(ENABLE_SVE)
79  default:
80  return RelativeTolerance<float>(0.05f);
81  }
83  switch(data_type)
84  {
85  case DataType::F16:
86 #if defined(ENABLE_SVE)
87  return RelativeTolerance<float>(0.9f);
88 #else // !defined(ENABLE_SVE)
89  return RelativeTolerance<float>(0.01f);
90 #endif // defined(ENABLE_SVE)
91  default:
92  return RelativeTolerance<float>(0.00001f);
93  }
94  default:
95  return RelativeTolerance<float>(0.f);
96  }
97 }
98 
99 /** Define absolute tolerance of the activation layer.
100  *
101  * @param[in] data_type The data type used.
102  * @param[in] activation The activation function used.
103  *
104  * @return Absolute tolerance depending on the activation function.
105  */
106 AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
107 {
108  switch(activation)
109  {
114  switch(data_type)
115  {
116  case DataType::F16:
117 #if defined(ENABLE_SVE)
118  return AbsoluteTolerance<float>(0.25f);
119 #else // !defined(ENABLE_SVE)
120  return AbsoluteTolerance<float>(0.01f);
121 #endif // defined(ENABLE_SVE)
122  default:
123  return AbsoluteTolerance<float>(0.00001f);
124  }
126  switch(data_type)
127  {
128  case DataType::F16:
129 #if defined(ENABLE_SVE)
130  return AbsoluteTolerance<float>(0.9f);
131 #else // !defined(ENABLE_SVE)
132  return AbsoluteTolerance<float>(0.01f);
133 #endif // defined(ENABLE_SVE)
134  default:
135  return AbsoluteTolerance<float>(0.00001f);
136  }
137  default:
138  return AbsoluteTolerance<float>(0.f);
139  }
140 }
141 
142 /** Define absolute tolerance of the activation layer for qasymm8.
143  *
144  * @param[in] activation The activation function used.
145  *
146  * @return Absolute tolerance depending on the activation function.
147  */
148 AbsoluteTolerance<uint8_t> tolerance_qasymm8(ActivationLayerInfo::ActivationFunction activation)
149 {
150  switch(activation)
151  {
158  return AbsoluteTolerance<uint8_t>(1);
159  default:
160  return AbsoluteTolerance<uint8_t>(0);
161  }
162 }
163 
164 constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
165 
166 /** CNN data types */
167 const auto CNNDataTypes = framework::dataset::make("DataType",
168 {
169 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
171 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
173 });
174 
175 const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
176 
177 /** Input data sets. */
178 const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), NeonActivationFunctionsDataset), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
179 
180 template <typename T, ARM_COMPUTE_REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
181 void test_float_sqrt_boundary_value()
182 {
183  constexpr auto vector_size = uint32_t{ 16 };
184 
185  auto data_type = DataType::F32;
186 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
187  data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
188 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
189 
190  const auto boundary_value_vector = std::vector<T>
191  {
192  std::numeric_limits<T>::min(),
193  T(0),
195  std::numeric_limits<T>::max(),
196  };
197 
198  // the following size ensures that the whole logic (vector + left-over) to be tested
199  // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
200  auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
201  auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
202  auto src = create_tensor<Tensor>(shape, data_type);
203 
204  auto act = NEActivationLayer{};
205  act.configure(&src, nullptr, info);
206  src.allocator()->allocate();
207  library->fill_static_values(Accessor(src), boundary_value_vector);
208  act.run();
209 
210  auto reference_src = SimpleTensor<T> { shape, data_type };
211  library->fill_static_values(reference_src, boundary_value_vector);
212  auto reference_dst = reference::activation_layer<T>(reference_src, info);
213 
214  validate(Accessor(src), reference_dst, tolerance_float_sqrt);
215 }
216 } // namespace
217 
218 TEST_SUITE(NEON)
219 TEST_SUITE(ActivationLayer)
220 
221 /** Test case for memory injection in @ref cpu::CpuWinogradConv2d.
222  *
223  * Configure the operator once and inject memory at run-time in multiple executions.
224  *
225  * Checks performed in order:
226  * - Both runs compute the same output
227  */
228 TEST_CASE(ActivationAPI, framework::DatasetMode::ALL)
229 {
231 
232  // Create context & Queue
233  acl::Context ctx(acl::Target::Cpu, &err);
235 
236  acl::Queue queue(ctx, &err);
238 
239  // Create activation operator
241  acl::TensorDescriptor dst_info({ 2, 3 }, acl::DataType::Float32);
242  acl::ActivationDesc desc{ AclRelu, 6.f, 0.f, false };
243 
244  acl::Activation act(ctx, src_info, dst_info, desc, &err);
246 
247  // Create tensors and feed
248  acl::Tensor src(ctx, src_info, &err);
250  acl::Tensor dst(ctx, dst_info, &err);
252 
253  acl::TensorPack pack(ctx);
254  err = pack.add(src, ACL_SRC);
255  err = pack.add(dst, ACL_DST);
257 
258  // Execute operator
259  err = act.run(queue, pack);
261 }
262 
263 // *INDENT-OFF*
264 // clang-format off
266  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
267  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
268  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
269  }),
270  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
271  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
272  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
273  })),
277  })),
278  framework::dataset::make("Expected", { false, true, false})),
279  input_info, output_info, act_info, expected)
280 {
281  bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
283 }
284 
286  combine(framework::dataset::make("CpuExt", std::string("NEON")),
292  })),
293  combine(framework::dataset::make("CpuExt", std::string("SVE")),
296  }))),
297  combine(framework::dataset::make("CpuExt", std::string("SVE2")),
301  }))),
302  cpu_ext, data_type)
303 {
304  using namespace cpu::kernels;
305 
307  cpu_isa.neon = (cpu_ext == "NEON");
308  cpu_isa.sve = (cpu_ext == "SVE");
309  cpu_isa.sve2 = (cpu_ext == "SVE2");
310  cpu_isa.fp16 = (data_type == DataType::F16);
311 
312  const auto *selected_impl = CpuActivationKernel::get_implementation(DataTypeISASelectorData{data_type, cpu_isa}, cpu::KernelSelectionType::Preferred);
313 
315 
316  std::string expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_activation";
317  std::string actual = selected_impl->name;
318 
320 }
321 // clang-format on
322 // *INDENT-ON*
323 
324 template <typename T>
325 using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
326 
327 TEST_SUITE(Float)
328 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
329 TEST_SUITE(FP16)
330 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
331 {
332  test_float_sqrt_boundary_value<half>();
333 }
334 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
335  framework::dataset::make("DataType",
336  DataType::F16)))
337 {
338  // Validate output
339  validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
340 }
341 TEST_SUITE_END() // FP16
342 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
343 
344 TEST_SUITE(FP32)
345 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
346 {
347  test_float_sqrt_boundary_value<float>();
348 }
350  DataType::F32)))
351 
352 {
353  // Validate output
354  validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
355 }
356 TEST_SUITE_END() // FP32
357 TEST_SUITE_END() // Float
358 
359 template <typename T>
360 using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
361 
362 /** Input data sets. */
363 const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction",
364 {
371 });
372 
375  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
376 
377 TEST_SUITE(Quantized)
379 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
380  framework::dataset::make("DataType",
381  DataType::QASYMM8)),
382  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
383 {
384  // Validate output
385  validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
386 }
387 TEST_SUITE_END() // QASYMM8
388 
390 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
391  framework::dataset::make("DataType",
392  DataType::QASYMM8_SIGNED)),
393  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
394 {
395  // Validate output
396  validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
397 }
398 TEST_SUITE_END() // QASYMM8_SIGNED
399 
400 /** Input data sets. */
401 const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction",
402 {
406 });
407 const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
408  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
409 
411 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
412  framework::dataset::make("DataType",
413  DataType::QSYMM16)),
414  framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
415 {
416  // Validate output
417  validate(Accessor(_target), _reference, tolerance_qsymm16);
418 }
419 TEST_SUITE_END() // QSYMM16
420 TEST_SUITE_END() // Quantized
421 
422 TEST_SUITE_END() // ActivationLayer
423 TEST_SUITE_END() // Neon
424 } // namespace validation
425 } // namespace test
426 } // 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:351
Tensor class.
Definition: Acl.hpp:584
Activation Layer Information class.
Definition: Types.h:1625
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:1629
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