Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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/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 | { |
| 46 | /** Define tolerance of the activation layer. |
| 47 | * |
| 48 | * @param[in] data_type The data type used. |
| 49 | * @param[in] activation The activation function used. |
| 50 | * |
| 51 | * @return Tolerance depending on the activation function. |
| 52 | */ |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 53 | AbsoluteTolerance<float> tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 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::QS8: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 64 | return AbsoluteTolerance<float>(5.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 65 | case DataType::QS16: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 66 | return AbsoluteTolerance<float>(11.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 67 | case DataType::F16: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 68 | return AbsoluteTolerance<float>(0.01f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 69 | default: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 70 | return AbsoluteTolerance<float>(0.00001f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 71 | } |
| 72 | break; |
| 73 | default: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 74 | return AbsoluteTolerance<float>(0.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /** CNN data types */ |
| 79 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 80 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 81 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 82 | DataType::F16, |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 83 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 84 | DataType::F32, |
| 85 | DataType::QS8, |
| 86 | DataType::QS16, |
| 87 | }); |
| 88 | |
| 89 | /** Input data sets. */ |
| 90 | const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); |
| 91 | } // namespace |
| 92 | |
| 93 | TEST_SUITE(NEON) |
| 94 | TEST_SUITE(ActivationLayer) |
| 95 | |
| 96 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })), |
| 97 | shape, data_type, in_place) |
| 98 | { |
| 99 | // Set fixed point position data type allowed |
| 100 | const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 101 | |
| 102 | // Create tensors |
| 103 | Tensor src = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position); |
| 104 | Tensor dst = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position); |
| 105 | |
| 106 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 107 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 108 | |
| 109 | // Create and configure function |
| 110 | NEActivationLayer act_layer; |
| 111 | |
| 112 | if(in_place) |
| 113 | { |
| 114 | act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 119 | } |
| 120 | |
| 121 | // Validate valid region |
| 122 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 123 | validate(src.info()->valid_region(), valid_region); |
| 124 | |
| 125 | if(!in_place) |
| 126 | { |
| 127 | validate(dst.info()->valid_region(), valid_region); |
| 128 | } |
| 129 | |
| 130 | // Validate padding |
| 131 | const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); |
| 132 | validate(src.info()->padding(), padding); |
| 133 | |
| 134 | if(!in_place) |
| 135 | { |
| 136 | validate(dst.info()->padding(), padding); |
| 137 | } |
| 138 | } |
| 139 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 140 | // *INDENT-OFF* |
| 141 | // clang-format off |
| 142 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( |
| 143 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 144 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 145 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes |
| 146 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point |
| 147 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
| 148 | }), |
| 149 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), |
| 150 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 151 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 152 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3), |
| 153 | TensorInfo(), |
| 154 | })), |
| 155 | framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 156 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 157 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 158 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 159 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 160 | })), |
| 161 | framework::dataset::make("Expected", { false, true, false, false, true })), |
| 162 | input_info, output_info, act_info, expected) |
| 163 | { |
| 164 | bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info)); |
| 165 | ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS); |
| 166 | } |
| 167 | // clang-format on |
| 168 | // *INDENT-ON* |
| 169 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 170 | template <typename T> |
| 171 | using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 172 | |
| 173 | TEST_SUITE(Float) |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 174 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 175 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 176 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 177 | framework::dataset::make("DataType", |
| 178 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 179 | { |
| 180 | // Validate output |
| 181 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 182 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 183 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 184 | framework::dataset::make("DataType", |
| 185 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 186 | { |
| 187 | // Validate output |
| 188 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 189 | } |
| 190 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 191 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 192 | |
| 193 | TEST_SUITE(FP32) |
| 194 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 195 | DataType::F32))) |
| 196 | { |
| 197 | // Validate output |
| 198 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 199 | } |
| 200 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 201 | DataType::F32))) |
| 202 | { |
| 203 | // Validate output |
| 204 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 205 | } |
| 206 | TEST_SUITE_END() |
| 207 | TEST_SUITE_END() |
| 208 | |
| 209 | template <typename T> |
| 210 | using NEActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 211 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame^] | 212 | TEST_SUITE(FixedPoint) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 213 | TEST_SUITE(QS8) |
| 214 | // We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause |
| 215 | // overflowing issues in most of the transcendentals functions. |
| 216 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 217 | framework::dataset::make("DataType", |
| 218 | DataType::QS8)), |
| 219 | framework::dataset::make("FractionalBits", 3, 6))) |
| 220 | { |
| 221 | // Validate output |
| 222 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 223 | } |
| 224 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 225 | framework::dataset::make("DataType", |
| 226 | DataType::QS8)), |
| 227 | framework::dataset::make("FractionalBits", 3, 6))) |
| 228 | { |
| 229 | // Validate output |
| 230 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 231 | } |
| 232 | TEST_SUITE_END() |
| 233 | |
| 234 | TEST_SUITE(QS16) |
| 235 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
| 236 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 237 | framework::dataset::make("DataType", |
| 238 | DataType::QS16)), |
| 239 | framework::dataset::make("FractionalBits", 1, 14))) |
| 240 | { |
| 241 | // Validate output |
| 242 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 243 | } |
| 244 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 245 | framework::dataset::make("DataType", |
| 246 | DataType::QS16)), |
| 247 | framework::dataset::make("FractionalBits", 1, 14))) |
| 248 | { |
| 249 | // Validate output |
| 250 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 251 | } |
| 252 | TEST_SUITE_END() |
| 253 | TEST_SUITE_END() |
| 254 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame^] | 255 | template <typename T> |
| 256 | using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 257 | |
| 258 | /** Input data sets. */ |
| 259 | const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU })), |
| 260 | framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); |
| 261 | |
| 262 | TEST_SUITE(Quantized) |
| 263 | TEST_SUITE(QASYMM8) |
| 264 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset), |
| 265 | framework::dataset::make("DataType", |
| 266 | DataType::QASYMM8)), |
| 267 | framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) }))) |
| 268 | { |
| 269 | // Validate output |
| 270 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 271 | } |
| 272 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset), |
| 273 | framework::dataset::make("DataType", |
| 274 | DataType::QASYMM8)), |
| 275 | framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) }))) |
| 276 | { |
| 277 | // Validate output |
| 278 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 279 | } |
| 280 | TEST_SUITE_END() |
| 281 | TEST_SUITE_END() |
| 282 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 283 | TEST_SUITE_END() |
| 284 | TEST_SUITE_END() |
| 285 | } // namespace validation |
| 286 | } // namespace test |
| 287 | } // namespace arm_compute |