Compute Library
 21.02
ActivationLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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"
30 #include "tests/NEON/Accessor.h"
32 #include "tests/datasets/ActivationFunctionsDataset.h"
33 #include "tests/datasets/ShapeDatasets.h"
35 #include "tests/framework/Macros.h"
38 #include "tests/validation/fixtures/ActivationLayerFixture.h"
39 
40 #include "support/Requires.h"
41 
42 namespace arm_compute
43 {
44 namespace test
45 {
46 namespace validation
47 {
48 namespace
49 {
50 RelativeTolerance<float> tolerance_float_sqrt(0.0001f);
51 
52 /** Define relative tolerance of the activation layer.
53  *
54  * @param[in] data_type The data type used.
55  * @param[in] activation The activation function used.
56  *
57  * @return Relative tolerance depending on the activation function.
58  */
59 RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
60 {
61  switch(activation)
62  {
68  switch(data_type)
69  {
70  case DataType::F16:
71 #if defined(__ARM_FEATURE_SVE)
72  return RelativeTolerance<float>(0.25f);
73 #else // !defined(__ARM_FEATURE_SVE)
74  return RelativeTolerance<float>(0.1f);
75 #endif // defined(__ARM_FEATURE_SVE)
76  default:
77  return RelativeTolerance<float>(0.05f);
78  }
80  switch(data_type)
81  {
82  case DataType::F16:
83 #if defined(__ARM_FEATURE_SVE)
84  return RelativeTolerance<float>(0.9f);
85 #else // !defined(__ARM_FEATURE_SVE)
86  return RelativeTolerance<float>(0.01f);
87 #endif // defined(__ARM_FEATURE_SVE)
88  default:
89  return RelativeTolerance<float>(0.00001f);
90  }
91  default:
92  return RelativeTolerance<float>(0.f);
93  }
94 }
95 
96 /** Define absolute tolerance of the activation layer.
97  *
98  * @param[in] data_type The data type used.
99  * @param[in] activation The activation function used.
100  *
101  * @return Absolute tolerance depending on the activation function.
102  */
103 AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
104 {
105  switch(activation)
106  {
111  switch(data_type)
112  {
113  case DataType::F16:
114 #if defined(__ARM_FEATURE_SVE)
115  return AbsoluteTolerance<float>(0.25f);
116 #else // !defined(__ARM_FEATURE_SVE)
117  return AbsoluteTolerance<float>(0.01f);
118 #endif // defined(__ARM_FEATURE_SVE)
119  default:
120  return AbsoluteTolerance<float>(0.00001f);
121  }
123  switch(data_type)
124  {
125  case DataType::F16:
126 #if defined(__ARM_FEATURE_SVE)
127  return AbsoluteTolerance<float>(0.9f);
128 #else // !defined(__ARM_FEATURE_SVE)
129  return AbsoluteTolerance<float>(0.01f);
130 #endif // defined(__ARM_FEATURE_SVE)
131  default:
132  return AbsoluteTolerance<float>(0.00001f);
133  }
134  default:
135  return AbsoluteTolerance<float>(0.f);
136  }
137 }
138 
139 /** Define absolute tolerance of the activation layer for qasymm8.
140  *
141  * @param[in] activation The activation function used.
142  *
143  * @return Absolute tolerance depending on the activation function.
144  */
145 AbsoluteTolerance<uint8_t> tolerance_qasymm8(ActivationLayerInfo::ActivationFunction activation)
146 {
147  switch(activation)
148  {
155  return AbsoluteTolerance<uint8_t>(1);
156  default:
157  return AbsoluteTolerance<uint8_t>(0);
158  }
159 }
160 
161 constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
162 
163 /** CNN data types */
164 const auto CNNDataTypes = framework::dataset::make("DataType",
165 {
166 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
168 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
170 });
171 
172 const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
173 
174 /** Input data sets. */
175 const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), NeonActivationFunctionsDataset), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
176 
177 template <typename T, ARM_COMPUTE_REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
178 void test_float_sqrt_boundary_value()
179 {
180  constexpr auto vector_size = uint32_t{ 16 };
181 
182  auto data_type = DataType::F32;
183 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
184  data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
185 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
186 
187  const auto boundary_value_vector = std::vector<T>
188  {
189  std::numeric_limits<T>::min(),
190  T(0),
192  std::numeric_limits<T>::max(),
193  };
194 
195  // the following size ensures that the whole logic (vector + left-over) to be tested
196  // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
197  auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
198  auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
199  auto src = create_tensor<Tensor>(shape, data_type);
200 
201  auto act = NEActivationLayer{};
202  act.configure(&src, nullptr, info);
203  src.allocator()->allocate();
204  library->fill_static_values(Accessor(src), boundary_value_vector);
205  act.run();
206 
207  auto reference_src = SimpleTensor<T> { shape, data_type };
208  library->fill_static_values(reference_src, boundary_value_vector);
209  auto reference_dst = reference::activation_layer<T>(reference_src, info);
210 
211  validate(Accessor(src), reference_dst, tolerance_float_sqrt);
212 }
213 } // namespace
214 
215 TEST_SUITE(NEON)
216 TEST_SUITE(ActivationLayer)
217 
218 // *INDENT-OFF*
219 // clang-format off
220 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
221  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
222  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
223  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
224  }),
225  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
226  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
227  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
228  })),
232  })),
233  framework::dataset::make("Expected", { false, true, false})),
234  input_info, output_info, act_info, expected)
235 {
236  bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
238 }
239 // clang-format on
240 // *INDENT-ON*
241 
242 template <typename T>
243 using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
244 
245 TEST_SUITE(Float)
246 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
247 TEST_SUITE(FP16)
248 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
249 {
250  test_float_sqrt_boundary_value<half>();
251 }
252 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
253  framework::dataset::make("DataType",
254  DataType::F16)))
255 {
256  // Validate output
257  validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
258 }
259 TEST_SUITE_END() // FP16
260 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
261 
262 TEST_SUITE(FP32)
263 TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
264 {
265  test_float_sqrt_boundary_value<float>();
266 }
268  DataType::F32)))
269 
270 {
271  // Validate output
272  validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
273 }
274 TEST_SUITE_END() // FP32
275 TEST_SUITE_END() // Float
276 
277 template <typename T>
278 using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
279 
280 /** Input data sets. */
281 const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction",
282 {
289 });
290 
293  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
294 
295 TEST_SUITE(Quantized)
297 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
298  framework::dataset::make("DataType",
299  DataType::QASYMM8)),
300  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
301 {
302  // Validate output
303  validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
304 }
305 TEST_SUITE_END() // QASYMM8
306 
308 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
309  framework::dataset::make("DataType",
310  DataType::QASYMM8_SIGNED)),
311  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
312 {
313  // Validate output
314  validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
315 }
316 TEST_SUITE_END() // QASYMM8_SIGNED
317 
318 /** Input data sets. */
319 const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
321  });
322 const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
323  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
324 
326 FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
327  framework::dataset::make("DataType",
328  DataType::QSYMM16)),
329  framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
330 {
331  // Validate output
332  validate(Accessor(_target), _reference, tolerance_qsymm16);
333 }
334 TEST_SUITE_END() // QSYMM16
335 TEST_SUITE_END() // Quantized
336 
337 TEST_SUITE_END() // ActivationLayer
338 TEST_SUITE_END() // Neon
339 } // namespace validation
340 } // namespace test
341 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
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.
Activation Layer Information class.
Definition: Types.h:1550
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
ActivationFunction
Available activation functions.
Definition: Types.h:1554
1 channel, 1 F16 per channel
ActivationValidationFixture< Tensor, Accessor, NEActivationLayer, T > NEActivationLayerFixture
const DataType data_type
Definition: Im2Col.cpp:150
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:78
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
const auto QuantizedActivationFunctionsDataset
Input data sets.
TEST_SUITE(U8_to_S8) FIXTURE_DATA_TEST_CASE(RunSmall
validate(CLAccessor(output_state), expected_output)
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)
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
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}))
DataType
Available data types.
Definition: Types.h:77
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65