Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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/GLES_COMPUTE/GCTensor.h" |
| 26 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h" |
| 27 | #include "arm_compute/runtime/GLES_COMPUTE/functions/GCActivationLayer.h" |
| 28 | #include "tests/GLES_COMPUTE/GCAccessor.h" |
| 29 | #include "tests/PaddingCalculator.h" |
| 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" |
| 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 | */ |
| 53 | AbsoluteTolerance<float> tolerance(ActivationLayerInfo::ActivationFunction activation, DataType data_type) |
| 54 | { |
| 55 | constexpr float epsilon = 1e-6f; |
| 56 | |
| 57 | switch(activation) |
| 58 | { |
| 59 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 60 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.2f : epsilon); |
| 61 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 62 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.1f : epsilon); |
| 63 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 64 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 65 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 66 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon); |
| 67 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 68 | case ActivationLayerInfo::ActivationFunction::ELU: |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 69 | case ActivationLayerInfo::ActivationFunction::SQRT: |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 70 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 71 | case ActivationLayerInfo::ActivationFunction::TANH: |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 72 | return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 73 | default: |
| 74 | return AbsoluteTolerance<float>(epsilon); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** CNN data types */ |
| 79 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 80 | { |
| 81 | DataType::F16, |
| 82 | DataType::F32, |
| 83 | }); |
| 84 | |
| 85 | /** Input data sets. */ |
| 86 | const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); |
| 87 | } // namespace |
| 88 | |
| 89 | TEST_SUITE(GC) |
| 90 | TEST_SUITE(ActivationLayer) |
| 91 | |
| 92 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })), |
| 93 | shape, data_type, in_place) |
| 94 | { |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 95 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 96 | GCTensor src = create_tensor<GCTensor>(shape, data_type, 1); |
| 97 | GCTensor dst = create_tensor<GCTensor>(shape, data_type, 1); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 98 | |
| 99 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | |
| 102 | // Create and configure function |
| 103 | GCActivationLayer act_layer; |
| 104 | |
| 105 | if(in_place) |
| 106 | { |
| 107 | act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 112 | } |
| 113 | |
| 114 | // Validate valid region |
| 115 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 116 | validate(src.info()->valid_region(), valid_region); |
| 117 | |
| 118 | if(!in_place) |
| 119 | { |
| 120 | validate(dst.info()->valid_region(), valid_region); |
| 121 | } |
| 122 | |
| 123 | // Validate padding |
| 124 | const int step = (arm_compute::data_size_from_type(data_type) == 4 ? 1 : 2); |
| 125 | const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); |
| 126 | validate(src.info()->padding(), padding); |
| 127 | |
| 128 | if(!in_place) |
| 129 | { |
| 130 | validate(dst.info()->padding(), padding); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | template <typename T> |
| 135 | using GCActivationLayerFixture = ActivationValidationFixture<GCTensor, GCAccessor, GCActivationLayer, T>; |
| 136 | |
| 137 | TEST_SUITE(Float) |
| 138 | TEST_SUITE(FP16) |
| 139 | FIXTURE_DATA_TEST_CASE(RunSmall, GCActivationLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), |
| 140 | framework::dataset::make("DataType", |
| 141 | DataType::F16))) |
| 142 | { |
| 143 | // Validate output |
| 144 | validate(GCAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 145 | } |
| 146 | FIXTURE_DATA_TEST_CASE(RunLarge, GCActivationLayerFixture<half_float::half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), |
| 147 | framework::dataset::make("DataType", |
| 148 | DataType::F16))) |
| 149 | { |
| 150 | // Validate output |
| 151 | validate(GCAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 152 | } |
| 153 | TEST_SUITE_END() |
| 154 | |
| 155 | TEST_SUITE(FP32) |
| 156 | FIXTURE_DATA_TEST_CASE(RunSmall, GCActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 157 | DataType::F32))) |
| 158 | { |
| 159 | // Validate output |
| 160 | validate(GCAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 161 | } |
| 162 | FIXTURE_DATA_TEST_CASE(RunLarge, GCActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType", |
| 163 | DataType::F32))) |
| 164 | { |
| 165 | // Validate output |
| 166 | validate(GCAccessor(_target), _reference, tolerance(_function, _data_type)); |
| 167 | } |
| 168 | TEST_SUITE_END() |
| 169 | TEST_SUITE_END() |
| 170 | |
| 171 | TEST_SUITE_END() |
| 172 | TEST_SUITE_END() |
| 173 | } // namespace validation |
| 174 | } // namespace test |
| 175 | } // namespace arm_compute |