Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 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/NEActivationLayer.h" |
| 26 | #include "arm_compute/runtime/Tensor.h" |
| 27 | #include "arm_compute/runtime/TensorAllocator.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 28 | #include "tests/NEON/Accessor.h" |
| 29 | #include "tests/PaddingCalculator.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 30 | #include "tests/datasets/ActivationFunctionsDataset.h" |
| 31 | #include "tests/datasets/ShapeDatasets.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Macros.h" |
| 34 | #include "tests/framework/datasets/Datasets.h" |
| 35 | #include "tests/validation/Validation.h" |
| 36 | #include "tests/validation/fixtures/ActivationLayerFixture.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | namespace |
| 45 | { |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 46 | /** Define relative tolerance of the activation layer. |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 47 | * |
| 48 | * @param[in] data_type The data type used. |
| 49 | * @param[in] activation The activation function used. |
| 50 | * |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 51 | * @return Relative tolerance depending on the activation function. |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 52 | */ |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 53 | RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation) |
| 54 | { |
| 55 | switch(activation) |
| 56 | { |
| 57 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 58 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 59 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 60 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 61 | switch(data_type) |
| 62 | { |
| 63 | case DataType::F16: |
| 64 | return RelativeTolerance<float>(0.1f); |
| 65 | default: |
| 66 | return RelativeTolerance<float>(0.05f); |
| 67 | } |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 68 | default: |
| 69 | return RelativeTolerance<float>(0.f); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** Define absolute tolerance of the activation layer. |
| 74 | * |
| 75 | * @param[in] data_type The data type used. |
| 76 | * @param[in] activation The activation function used. |
| 77 | * |
| 78 | * @return Absolute tolerance depending on the activation function. |
| 79 | */ |
| 80 | AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 81 | { |
| 82 | switch(activation) |
| 83 | { |
| 84 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 85 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 86 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 87 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 88 | switch(data_type) |
| 89 | { |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 90 | case DataType::F16: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 91 | return AbsoluteTolerance<float>(0.01f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 92 | default: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 93 | return AbsoluteTolerance<float>(0.00001f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 94 | } |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 95 | default: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 96 | return AbsoluteTolerance<float>(0.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Georgios Pinitas | 151fa87 | 2019-06-06 14:44:44 +0100 | [diff] [blame^] | 100 | /** Tolerance for quantized asymmetric operations */ |
| 101 | #if defined(__aarch64__) |
| 102 | constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(0); |
| 103 | #else // defined(__aarch64__) |
| 104 | constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1); |
| 105 | #endif // defined(__aarch64__) |
| 106 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 107 | /** CNN data types */ |
| 108 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 109 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 110 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 111 | DataType::F16, |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 112 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 113 | DataType::F32, |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 114 | }); |
| 115 | |
| 116 | /** Input data sets. */ |
| 117 | const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); |
| 118 | } // namespace |
| 119 | |
| 120 | TEST_SUITE(NEON) |
| 121 | TEST_SUITE(ActivationLayer) |
| 122 | |
Michalis Spyrou | 5c9f0c4 | 2019-01-16 14:48:48 +0000 | [diff] [blame] | 123 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), CNNDataTypes), framework::dataset::make("InPlace", { false, true })), |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 124 | shape, data_type, in_place) |
| 125 | { |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 126 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 127 | Tensor src = create_tensor<Tensor>(shape, data_type, 1); |
| 128 | Tensor dst = create_tensor<Tensor>(shape, data_type, 1); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 129 | |
| 130 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 131 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 132 | |
| 133 | // Create and configure function |
| 134 | NEActivationLayer act_layer; |
| 135 | |
| 136 | if(in_place) |
| 137 | { |
| 138 | act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 143 | } |
| 144 | |
| 145 | // Validate valid region |
| 146 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 147 | validate(src.info()->valid_region(), valid_region); |
| 148 | |
| 149 | if(!in_place) |
| 150 | { |
| 151 | validate(dst.info()->valid_region(), valid_region); |
| 152 | } |
| 153 | |
| 154 | // Validate padding |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 155 | validate(src.info()->padding(), PaddingSize()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 156 | if(!in_place) |
| 157 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 158 | validate(dst.info()->padding(), PaddingSize()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 162 | // *INDENT-OFF* |
| 163 | // clang-format off |
| 164 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( |
| 165 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 166 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 167 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 168 | }), |
| 169 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), |
| 170 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 171 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 172 | })), |
| 173 | framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 174 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 175 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 176 | })), |
Vidhya Sudhan Loganathan | 0fc2545 | 2018-06-18 14:40:56 +0100 | [diff] [blame] | 177 | framework::dataset::make("Expected", { false, true, false})), |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 178 | input_info, output_info, act_info, expected) |
| 179 | { |
| 180 | bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info)); |
| 181 | ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS); |
| 182 | } |
| 183 | // clang-format on |
| 184 | // *INDENT-ON* |
| 185 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 186 | template <typename T> |
| 187 | using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 188 | |
| 189 | TEST_SUITE(Float) |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 190 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 191 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 192 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 193 | framework::dataset::make("DataType", |
| 194 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 195 | { |
| 196 | // Validate output |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 197 | validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function)); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 198 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 199 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 200 | framework::dataset::make("DataType", |
| 201 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 202 | { |
| 203 | // Validate output |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 204 | validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function)); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 205 | } |
| 206 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 207 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 208 | |
| 209 | TEST_SUITE(FP32) |
| 210 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 211 | DataType::F32))) |
Pablo Tello | 7282d56 | 2018-06-14 15:35:49 +0100 | [diff] [blame] | 212 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 213 | { |
| 214 | // Validate output |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 215 | validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function)); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 216 | } |
Pablo Tello | 7282d56 | 2018-06-14 15:35:49 +0100 | [diff] [blame] | 217 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 218 | framework::dataset::make("DataType", DataType::F32))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 219 | { |
| 220 | // Validate output |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 221 | validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function)); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 222 | } |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 223 | TEST_SUITE_END() // FP32 |
| 224 | TEST_SUITE_END() // Float |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 225 | |
| 226 | template <typename T> |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 227 | using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 228 | |
| 229 | /** Input data sets. */ |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 230 | const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 231 | ActivationLayerInfo::ActivationFunction::RELU, |
| 232 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 233 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
| 234 | ActivationLayerInfo::ActivationFunction::TANH |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 235 | }); |
| 236 | |
Georgios Pinitas | 151fa87 | 2019-06-06 14:44:44 +0100 | [diff] [blame^] | 237 | const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), QuantizedActivationFunctionsDataset), |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 238 | framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); |
| 239 | |
| 240 | TEST_SUITE(Quantized) |
| 241 | TEST_SUITE(QASYMM8) |
| 242 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset), |
| 243 | framework::dataset::make("DataType", |
| 244 | DataType::QASYMM8)), |
| 245 | framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) }))) |
| 246 | { |
| 247 | // Validate output |
Georgios Pinitas | 151fa87 | 2019-06-06 14:44:44 +0100 | [diff] [blame^] | 248 | validate(Accessor(_target), _reference, tolerance_qasymm8); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 249 | } |
| 250 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset), |
| 251 | framework::dataset::make("DataType", |
| 252 | DataType::QASYMM8)), |
| 253 | framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) }))) |
| 254 | { |
| 255 | // Validate output |
Georgios Pinitas | 151fa87 | 2019-06-06 14:44:44 +0100 | [diff] [blame^] | 256 | validate(Accessor(_target), _reference, tolerance_qasymm8); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 257 | } |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 258 | TEST_SUITE_END() // QASYMM8 |
| 259 | TEST_SUITE_END() // Quantized |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 260 | |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 261 | TEST_SUITE_END() // ActivationLayer |
| 262 | TEST_SUITE_END() // NEON |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 263 | } // namespace validation |
| 264 | } // namespace test |
| 265 | } // namespace arm_compute |