blob: 0b688dfd1b2b8697f732878c6df3909f565e8dde [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"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43namespace
44{
45/** Tolerance for float operations */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010046constexpr AbsoluteTolerance<float> tolerance_f32(0.000001f);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000047#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010048constexpr AbsoluteTolerance<float> tolerance_f16(0.0001f);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000049#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010050/** Tolerance for fixed point operations */
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +010051constexpr AbsoluteTolerance<int16_t> tolerance_fixed_point(2);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010052
53/** CNN data types */
54const auto CNNDataTypes = framework::dataset::make("DataType",
55{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000056#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010057 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000058#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010059 DataType::F32,
60 DataType::QS8,
61 DataType::QS16,
62});
63} // namespace
64
65TEST_SUITE(NEON)
66TEST_SUITE(SoftmaxLayer)
67
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010068DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SoftmaxLayerSmallShapes(), datasets::SoftmaxLayerLargeShapes()), CNNDataTypes), shape, data_type)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010069{
70 // Set fixed point position data type allowed
71 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
72
73 // Create tensors
74 Tensor src = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
75 Tensor dst = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
76
77 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
78 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
79
80 // Create and configure function
81 NESoftmaxLayer smx_layer;
82 smx_layer.configure(&src, &dst);
83
84 // Validate valid region
85 const ValidRegion valid_region = shape_to_valid_region(shape);
86 validate(src.info()->valid_region(), valid_region);
87 validate(dst.info()->valid_region(), valid_region);
88
89 // Validate padding
90 const int step = 16 / data_size_from_type(data_type);
91 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
92 validate(src.info()->padding(), padding);
93 validate(dst.info()->padding(), padding);
94}
95
Michalis Spyrouafa5d812017-11-30 14:25:57 +000096// *INDENT-OFF*
97// clang-format off
98DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
99 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
100 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
101 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point
102 TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),
103 }),
104 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
105 TensorInfo(TensorShape(27U, 11U, 2U), 1, DataType::F32),
106 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3),
107 TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),
108 })),
109 framework::dataset::make("Expected", { false, false, false, true })),
110 input_info, output_info, expected)
111{
112 bool is_valid = bool(NESoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false)));
113 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
114}
115// clang-format on
116// *INDENT-ON*
117
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100118template <typename T>
119using NESoftmaxLayerFixture = SoftmaxValidationFixture<Tensor, Accessor, NESoftmaxLayer, T>;
120
121TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000122#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100123TEST_SUITE(FP16)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100124FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(),
125 framework::dataset::make("DataType", DataType::F16)),
126 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100127{
128 // Validate output
129 validate(Accessor(_target), _reference, tolerance_f16);
130}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100131FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerSmallShapes(),
132 framework::dataset::make("DataType", DataType::F16)),
133 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100134{
135 // Validate output
136 validate(Accessor(_target), _reference, tolerance_f16);
137}
138TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000139#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100140
141TEST_SUITE(FP32)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100142FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(),
143 framework::dataset::make("DataType", DataType::F32)),
144 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100145{
146 // Validate output
147 validate(Accessor(_target), _reference, tolerance_f32);
148}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100149FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
150 framework::dataset::make("DataType", DataType::F32)),
151 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100152{
153 // Validate output
154 validate(Accessor(_target), _reference, tolerance_f32);
155}
156TEST_SUITE_END()
157TEST_SUITE_END()
158
159template <typename T>
160using NESoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<Tensor, Accessor, NESoftmaxLayer, T>;
161
162TEST_SUITE(Quantized)
163TEST_SUITE(QS8)
164// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100165FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(), framework::dataset::make("DataType",
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100166 DataType::QS8)),
167 framework::dataset::make("FractionalBits", 1, 6)))
168{
169 // Validate output
170 validate(Accessor(_target), _reference, tolerance_fixed_point);
171}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100172FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), framework::dataset::make("DataType",
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100173 DataType::QS8)),
174 framework::dataset::make("FractionalBits", 1, 6)))
175{
176 // Validate output
177 validate(Accessor(_target), _reference, tolerance_fixed_point);
178}
179TEST_SUITE_END()
180
181TEST_SUITE(QS16)
182// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100183FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(),
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100184 framework::dataset::make("DataType",
185 DataType::QS16)),
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100186 framework::dataset::make("FractionalBits", 1, 14)))
187{
188 // Validate output
189 validate(Accessor(_target), _reference, tolerance_fixed_point);
190}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100191FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100192 framework::dataset::make("DataType",
193 DataType::QS16)),
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100194 framework::dataset::make("FractionalBits", 1, 14)))
195{
196 // Validate output
197 validate(Accessor(_target), _reference, tolerance_fixed_point);
198}
199TEST_SUITE_END()
200TEST_SUITE_END()
201
202TEST_SUITE_END()
203TEST_SUITE_END()
204} // namespace validation
205} // namespace test
206} // namespace arm_compute