blob: 7676b858f6c5f82ff6292c9b88a9be7ecf3283fd [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +01002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
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 */
53AbsoluteTolerance<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 Loganathan7485d5a2018-07-04 09:34:00 +010064 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
Anthony Barbier7068f992017-10-26 15:23:08 +010065 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
66 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
67 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
68 case ActivationLayerInfo::ActivationFunction::SQRT:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010069 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
Anthony Barbier7068f992017-10-26 15:23:08 +010070 case ActivationLayerInfo::ActivationFunction::TANH:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010071 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
Anthony Barbier7068f992017-10-26 15:23:08 +010072 default:
73 return AbsoluteTolerance<float>(epsilon);
74 }
75}
76
77/** CNN data types */
78const auto CNNDataTypes = framework::dataset::make("DataType",
79{
80 DataType::F16,
81 DataType::F32,
82});
83
84/** Input data sets. */
85const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
86} // namespace
87
88TEST_SUITE(GC)
89TEST_SUITE(ActivationLayer)
90
91DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
92 shape, data_type, in_place)
93{
Anthony Barbier7068f992017-10-26 15:23:08 +010094 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010095 GCTensor src = create_tensor<GCTensor>(shape, data_type, 1);
96 GCTensor dst = create_tensor<GCTensor>(shape, data_type, 1);
Anthony Barbier7068f992017-10-26 15:23:08 +010097
98 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Create and configure function
102 GCActivationLayer act_layer;
103
104 if(in_place)
105 {
106 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
107 }
108 else
109 {
110 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
111 }
112
113 // Validate valid region
114 const ValidRegion valid_region = shape_to_valid_region(shape);
115 validate(src.info()->valid_region(), valid_region);
116
117 if(!in_place)
118 {
119 validate(dst.info()->valid_region(), valid_region);
120 }
121
122 // Validate padding
123 const int step = (arm_compute::data_size_from_type(data_type) == 4 ? 1 : 2);
124 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
125 validate(src.info()->padding(), padding);
126
127 if(!in_place)
128 {
129 validate(dst.info()->padding(), padding);
130 }
131}
132
133template <typename T>
134using GCActivationLayerFixture = ActivationValidationFixture<GCTensor, GCAccessor, GCActivationLayer, T>;
135
136TEST_SUITE(Float)
137TEST_SUITE(FP16)
138FIXTURE_DATA_TEST_CASE(RunSmall, GCActivationLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
139 framework::dataset::make("DataType",
140 DataType::F16)))
141{
142 // Validate output
143 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
144}
145FIXTURE_DATA_TEST_CASE(RunLarge, GCActivationLayerFixture<half_float::half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
146 framework::dataset::make("DataType",
147 DataType::F16)))
148{
149 // Validate output
150 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
151}
152TEST_SUITE_END()
153
154TEST_SUITE(FP32)
155FIXTURE_DATA_TEST_CASE(RunSmall, GCActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
156 DataType::F32)))
157{
158 // Validate output
159 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
160}
161FIXTURE_DATA_TEST_CASE(RunLarge, GCActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
162 DataType::F32)))
163{
164 // Validate output
165 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
166}
167TEST_SUITE_END()
168TEST_SUITE_END()
169
170TEST_SUITE_END()
171TEST_SUITE_END()
172} // namespace validation
173} // namespace test
174} // namespace arm_compute