blob: 23821d35fa9c3b1536fd987d4ae5c79d69652f2b [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
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/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:
64 if(is_data_type_fixed_point(data_type))
65 {
66 return AbsoluteTolerance<float>(5.f);
67 }
68 else
69 {
70 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
71 }
72 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
73 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
74 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
75 case ActivationLayerInfo::ActivationFunction::SQRT:
76 if(is_data_type_fixed_point(data_type))
77 {
78 return AbsoluteTolerance<float>(5.f);
79 }
80 else
81 {
82 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
83 }
84 case ActivationLayerInfo::ActivationFunction::TANH:
85 if(is_data_type_fixed_point(data_type))
86 {
87 return AbsoluteTolerance<float>(5.f);
88 }
89 else
90 {
91 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
92 }
93 default:
94 return AbsoluteTolerance<float>(epsilon);
95 }
96}
97
98/** CNN data types */
99const auto CNNDataTypes = framework::dataset::make("DataType",
100{
101 DataType::F16,
102 DataType::F32,
103});
104
105/** Input data sets. */
106const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
107} // namespace
108
109TEST_SUITE(GC)
110TEST_SUITE(ActivationLayer)
111
112DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
113 shape, data_type, in_place)
114{
115 // Set fixed point position data type allowed
116 const int fixed_point_position = 0;
117
118 // Create tensors
119 GCTensor src = create_tensor<GCTensor>(shape, data_type, 1, fixed_point_position);
120 GCTensor dst = create_tensor<GCTensor>(shape, data_type, 1, fixed_point_position);
121
122 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
123 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
124
125 // Create and configure function
126 GCActivationLayer act_layer;
127
128 if(in_place)
129 {
130 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
131 }
132 else
133 {
134 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
135 }
136
137 // Validate valid region
138 const ValidRegion valid_region = shape_to_valid_region(shape);
139 validate(src.info()->valid_region(), valid_region);
140
141 if(!in_place)
142 {
143 validate(dst.info()->valid_region(), valid_region);
144 }
145
146 // Validate padding
147 const int step = (arm_compute::data_size_from_type(data_type) == 4 ? 1 : 2);
148 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
149 validate(src.info()->padding(), padding);
150
151 if(!in_place)
152 {
153 validate(dst.info()->padding(), padding);
154 }
155}
156
157template <typename T>
158using GCActivationLayerFixture = ActivationValidationFixture<GCTensor, GCAccessor, GCActivationLayer, T>;
159
160TEST_SUITE(Float)
161TEST_SUITE(FP16)
162FIXTURE_DATA_TEST_CASE(RunSmall, GCActivationLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
163 framework::dataset::make("DataType",
164 DataType::F16)))
165{
166 // Validate output
167 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
168}
169FIXTURE_DATA_TEST_CASE(RunLarge, GCActivationLayerFixture<half_float::half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
170 framework::dataset::make("DataType",
171 DataType::F16)))
172{
173 // Validate output
174 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
175}
176TEST_SUITE_END()
177
178TEST_SUITE(FP32)
179FIXTURE_DATA_TEST_CASE(RunSmall, GCActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
180 DataType::F32)))
181{
182 // Validate output
183 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
184}
185FIXTURE_DATA_TEST_CASE(RunLarge, GCActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
186 DataType::F32)))
187{
188 // Validate output
189 validate(GCAccessor(_target), _reference, tolerance(_function, _data_type));
190}
191TEST_SUITE_END()
192TEST_SUITE_END()
193
194TEST_SUITE_END()
195TEST_SUITE_END()
196} // namespace validation
197} // namespace test
198} // namespace arm_compute