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/CL/CLTensor.h" |
| 26 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 27 | #include "arm_compute/runtime/CL/functions/CLActivationLayer.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 28 | #include "tests/CL/CLAccessor.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] activation The activation function used. |
| 49 | * @param[in] data_type Data type. |
| 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(ActivationLayerInfo::ActivationFunction activation, DataType data_type) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 54 | { |
Moritz Pflanzer | f07f145 | 2017-08-08 17:28:39 +0100 | [diff] [blame] | 55 | constexpr float epsilon = 1e-6f; |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 56 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 57 | switch(activation) |
| 58 | { |
| 59 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 60 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.2f : epsilon); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 61 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 62 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.1f : epsilon); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 63 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 64 | if(is_data_type_fixed_point(data_type)) |
| 65 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 66 | return AbsoluteTolerance<float>(5.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 67 | } |
| 68 | else |
| 69 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 70 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 71 | } |
| 72 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 73 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 74 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 75 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 76 | if(is_data_type_fixed_point(data_type)) |
| 77 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 78 | return AbsoluteTolerance<float>(5.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 79 | } |
| 80 | else |
| 81 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 82 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 83 | } |
| 84 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 85 | if(is_data_type_fixed_point(data_type)) |
| 86 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 87 | return AbsoluteTolerance<float>(5.f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 91 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 92 | } |
| 93 | default: |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 94 | return AbsoluteTolerance<float>(epsilon); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | /** CNN data types */ |
| 99 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 100 | { |
| 101 | DataType::F16, |
| 102 | DataType::F32, |
| 103 | DataType::QS8, |
| 104 | DataType::QS16, |
| 105 | }); |
| 106 | |
| 107 | /** Input data sets. */ |
| 108 | const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); |
| 109 | } // namespace |
| 110 | |
| 111 | TEST_SUITE(CL) |
| 112 | TEST_SUITE(ActivationLayer) |
| 113 | |
| 114 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })), |
| 115 | shape, data_type, in_place) |
| 116 | { |
| 117 | // Set fixed point position data type allowed |
| 118 | const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 119 | |
| 120 | // Create tensors |
| 121 | CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position); |
| 122 | CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position); |
| 123 | |
| 124 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 125 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 126 | |
| 127 | // Create and configure function |
| 128 | CLActivationLayer act_layer; |
| 129 | |
| 130 | if(in_place) |
| 131 | { |
| 132 | act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 137 | } |
| 138 | |
| 139 | // Validate valid region |
| 140 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 141 | validate(src.info()->valid_region(), valid_region); |
| 142 | |
| 143 | if(!in_place) |
| 144 | { |
| 145 | validate(dst.info()->valid_region(), valid_region); |
| 146 | } |
| 147 | |
| 148 | // Validate padding |
| 149 | const int step = 16 / arm_compute::data_size_from_type(data_type); |
| 150 | const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); |
| 151 | validate(src.info()->padding(), padding); |
| 152 | |
| 153 | if(!in_place) |
| 154 | { |
| 155 | validate(dst.info()->padding(), padding); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | template <typename T> |
| 160 | using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>; |
| 161 | |
| 162 | TEST_SUITE(Float) |
| 163 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 164 | FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 165 | framework::dataset::make("DataType", |
| 166 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 167 | { |
| 168 | // Validate output |
| 169 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 170 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 171 | FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 172 | framework::dataset::make("DataType", |
| 173 | DataType::F16))) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 174 | { |
| 175 | // Validate output |
| 176 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 177 | } |
| 178 | TEST_SUITE_END() |
| 179 | |
| 180 | TEST_SUITE(FP32) |
| 181 | FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 182 | DataType::F32))) |
| 183 | { |
| 184 | // Validate output |
| 185 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 186 | } |
| 187 | FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 188 | DataType::F32))) |
| 189 | { |
| 190 | // Validate output |
| 191 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 192 | } |
| 193 | TEST_SUITE_END() |
| 194 | TEST_SUITE_END() |
| 195 | |
| 196 | template <typename T> |
| 197 | using CLActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<CLTensor, CLAccessor, CLActivationLayer, T>; |
| 198 | |
| 199 | TEST_SUITE(Quantized) |
| 200 | TEST_SUITE(QS8) |
| 201 | // We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause |
| 202 | // overflowing issues in most of the transcendentals functions. |
| 203 | FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 204 | framework::dataset::make("DataType", |
| 205 | DataType::QS8)), |
| 206 | framework::dataset::make("FractionalBits", 3, 6))) |
| 207 | { |
| 208 | // Validate output |
| 209 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 210 | } |
| 211 | FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 212 | framework::dataset::make("DataType", |
| 213 | DataType::QS8)), |
| 214 | framework::dataset::make("FractionalBits", 3, 6))) |
| 215 | { |
| 216 | // Validate output |
| 217 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 218 | } |
| 219 | TEST_SUITE_END() |
| 220 | |
| 221 | TEST_SUITE(QS16) |
| 222 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
| 223 | FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 224 | framework::dataset::make("DataType", |
| 225 | DataType::QS16)), |
| 226 | framework::dataset::make("FractionalBits", 1, 14))) |
| 227 | { |
| 228 | // Validate output |
| 229 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 230 | } |
| 231 | FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 232 | framework::dataset::make("DataType", |
| 233 | DataType::QS16)), |
| 234 | framework::dataset::make("FractionalBits", 1, 14))) |
| 235 | { |
| 236 | // Validate output |
| 237 | validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 238 | } |
| 239 | TEST_SUITE_END() |
| 240 | TEST_SUITE_END() |
| 241 | |
| 242 | TEST_SUITE_END() |
| 243 | TEST_SUITE_END() |
| 244 | } // namespace validation |
| 245 | } // namespace test |
| 246 | } // namespace arm_compute |