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 | |
| 140 | template <typename T> |
| 141 | using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 142 | |
| 143 | TEST_SUITE(Float) |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 144 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 145 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 146 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 147 | framework::dataset::make("DataType", |
| 148 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 149 | { |
| 150 | // Validate output |
| 151 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 152 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 153 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 154 | framework::dataset::make("DataType", |
| 155 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 156 | { |
| 157 | // Validate output |
| 158 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 159 | } |
| 160 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 161 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 162 | |
| 163 | TEST_SUITE(FP32) |
| 164 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 165 | DataType::F32))) |
| 166 | { |
| 167 | // Validate output |
| 168 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 169 | } |
| 170 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 171 | DataType::F32))) |
| 172 | { |
| 173 | // Validate output |
| 174 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 175 | } |
| 176 | TEST_SUITE_END() |
| 177 | TEST_SUITE_END() |
| 178 | |
| 179 | template <typename T> |
| 180 | using NEActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<Tensor, Accessor, NEActivationLayer, T>; |
| 181 | |
| 182 | TEST_SUITE(Quantized) |
| 183 | TEST_SUITE(QS8) |
| 184 | // We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause |
| 185 | // overflowing issues in most of the transcendentals functions. |
| 186 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 187 | framework::dataset::make("DataType", |
| 188 | DataType::QS8)), |
| 189 | framework::dataset::make("FractionalBits", 3, 6))) |
| 190 | { |
| 191 | // Validate output |
| 192 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 193 | } |
| 194 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 195 | framework::dataset::make("DataType", |
| 196 | DataType::QS8)), |
| 197 | framework::dataset::make("FractionalBits", 3, 6))) |
| 198 | { |
| 199 | // Validate output |
| 200 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 201 | } |
| 202 | TEST_SUITE_END() |
| 203 | |
| 204 | TEST_SUITE(QS16) |
| 205 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
| 206 | FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 207 | framework::dataset::make("DataType", |
| 208 | DataType::QS16)), |
| 209 | framework::dataset::make("FractionalBits", 1, 14))) |
| 210 | { |
| 211 | // Validate output |
| 212 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 213 | } |
| 214 | FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 215 | framework::dataset::make("DataType", |
| 216 | DataType::QS16)), |
| 217 | framework::dataset::make("FractionalBits", 1, 14))) |
| 218 | { |
| 219 | // Validate output |
| 220 | validate(Accessor(_target), _reference, tolerance(_data_type, _function)); |
| 221 | } |
| 222 | TEST_SUITE_END() |
| 223 | TEST_SUITE_END() |
| 224 | |
| 225 | TEST_SUITE_END() |
| 226 | TEST_SUITE_END() |
| 227 | } // namespace validation |
| 228 | } // namespace test |
| 229 | } // namespace arm_compute |