blob: 7ac7759c22ff7bfb8242861c60c06782ad7b6863 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +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/NEON/functions/NESoftmaxLayer.h"
26#include "arm_compute/runtime/Tensor.h"
27#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010028#include "tests/NEON/Accessor.h"
29#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010030#include "tests/datasets/ShapeDatasets.h"
31#include "tests/framework/Asserts.h"
32#include "tests/framework/Macros.h"
33#include "tests/framework/datasets/Datasets.h"
34#include "tests/validation/Validation.h"
35#include "tests/validation/fixtures/SoftmaxLayerFixture.h"
36#include "tests/validation/half.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010037
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
45{
46/** Tolerance for float operations */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010047constexpr AbsoluteTolerance<float> tolerance_f32(0.000001f);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010048#ifdef ARM_COMPUTE_ENABLE_FP16
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010049constexpr AbsoluteTolerance<float> tolerance_f16(0.0001f);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010050#endif /* ARM_COMPUTE_ENABLE_FP16*/
51/** Tolerance for fixed point operations */
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +010052constexpr AbsoluteTolerance<int16_t> tolerance_fixed_point(2);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010053
54/** CNN data types */
55const auto CNNDataTypes = framework::dataset::make("DataType",
56{
57#ifdef ARM_COMPUTE_ENABLE_FP16
58 DataType::F16,
59#endif /* ARM_COMPUTE_ENABLE_FP16 */
60 DataType::F32,
61 DataType::QS8,
62 DataType::QS16,
63});
64} // namespace
65
66TEST_SUITE(NEON)
67TEST_SUITE(SoftmaxLayer)
68
69DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), shape, data_type)
70{
71 // Set fixed point position data type allowed
72 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
73
74 // Create tensors
75 Tensor src = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
76 Tensor dst = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
77
78 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
79 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
80
81 // Create and configure function
82 NESoftmaxLayer smx_layer;
83 smx_layer.configure(&src, &dst);
84
85 // Validate valid region
86 const ValidRegion valid_region = shape_to_valid_region(shape);
87 validate(src.info()->valid_region(), valid_region);
88 validate(dst.info()->valid_region(), valid_region);
89
90 // Validate padding
91 const int step = 16 / data_size_from_type(data_type);
92 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
93 validate(src.info()->padding(), padding);
94 validate(dst.info()->padding(), padding);
95}
96
97template <typename T>
98using NESoftmaxLayerFixture = SoftmaxValidationFixture<Tensor, Accessor, NESoftmaxLayer, T>;
99
100TEST_SUITE(Float)
101#ifdef ARM_COMPUTE_ENABLE_FP16
102TEST_SUITE(FP16)
103FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
104{
105 // Validate output
106 validate(Accessor(_target), _reference, tolerance_f16);
107}
108FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<half_float::half>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16)))
109{
110 // Validate output
111 validate(Accessor(_target), _reference, tolerance_f16);
112}
113TEST_SUITE_END()
114#endif /* ARM_COMPUTE_ENABLE_FP16 */
115
116TEST_SUITE(FP32)
117FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
118{
119 // Validate output
120 validate(Accessor(_target), _reference, tolerance_f32);
121}
122FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)))
123{
124 // Validate output
125 validate(Accessor(_target), _reference, tolerance_f32);
126}
127TEST_SUITE_END()
128TEST_SUITE_END()
129
130template <typename T>
131using NESoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<Tensor, Accessor, NESoftmaxLayer, T>;
132
133TEST_SUITE(Quantized)
134TEST_SUITE(QS8)
135// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
136FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
137 DataType::QS8)),
138 framework::dataset::make("FractionalBits", 1, 6)))
139{
140 // Validate output
141 validate(Accessor(_target), _reference, tolerance_fixed_point);
142}
143FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType",
144 DataType::QS8)),
145 framework::dataset::make("FractionalBits", 1, 6)))
146{
147 // Validate output
148 validate(Accessor(_target), _reference, tolerance_fixed_point);
149}
150TEST_SUITE_END()
151
152TEST_SUITE(QS16)
153// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100154FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(),
155 framework::dataset::make("DataType",
156 DataType::QS16)),
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100157 framework::dataset::make("FractionalBits", 1, 14)))
158{
159 // Validate output
160 validate(Accessor(_target), _reference, tolerance_fixed_point);
161}
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100162FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(),
163 framework::dataset::make("DataType",
164 DataType::QS16)),
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100165 framework::dataset::make("FractionalBits", 1, 14)))
166{
167 // Validate output
168 validate(Accessor(_target), _reference, tolerance_fixed_point);
169}
170TEST_SUITE_END()
171TEST_SUITE_END()
172
173TEST_SUITE_END()
174TEST_SUITE_END()
175} // namespace validation
176} // namespace test
177} // namespace arm_compute