blob: b47f84f8cd707a361faebfbc5d2a172fda7dcefd [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +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 */
Chunosovd6afedc2017-11-06 22:09:45 +070024#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010025#include "arm_compute/core/Types.h"
26#include "arm_compute/runtime/CL/CLTensor.h"
27#include "arm_compute/runtime/CL/CLTensorAllocator.h"
28#include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010029#include "tests/CL/CLAccessor.h"
30#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#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/SoftmaxLayerFixture.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 */
Georgios Pinitas583137c2017-08-31 18:12:42 +010047RelativeTolerance<half> tolerance_f16(half(0.2));
48RelativeTolerance<float> tolerance_f32(0.001f);
steniu013e05e4e2017-08-25 17:18:01 +010049
Chunosovf450caa2017-11-08 16:09:35 +070050/** Tolerance for quantized operations */
51constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
52
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010053/** CNN data types */
54const auto CNNDataTypes = framework::dataset::make("DataType",
55{
Chunosovf450caa2017-11-08 16:09:35 +070056 DataType::QASYMM8,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010057 DataType::F16,
58 DataType::F32,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010059});
60} // namespace
61
62TEST_SUITE(CL)
63TEST_SUITE(SoftmaxLayer)
64
Chunosovd6afedc2017-11-06 22:09:45 +070065DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SoftmaxLayerSmallShapes(), datasets::SoftmaxLayerLargeShapes()), CNNDataTypes), shape, data_type)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010066{
Chunosovf450caa2017-11-08 16:09:35 +070067 // Set fixed point position and quantization info if is allowed
68 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
69 const QuantizationInfo quantization_info = is_data_type_quantized_asymmetric(data_type) ? QuantizationInfo(1.f / 255.f, 0) : QuantizationInfo();
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010070
71 // Create tensors
Chunosovf450caa2017-11-08 16:09:35 +070072 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position, quantization_info);
73 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position, QuantizationInfo(1.f / 256.f, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010074
75 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
76 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
77
78 // Create and configure function
79 CLSoftmaxLayer smx_layer;
80 smx_layer.configure(&src, &dst);
81
82 // Validate valid region
83 const ValidRegion valid_region = shape_to_valid_region(shape);
84 validate(src.info()->valid_region(), valid_region);
85 validate(dst.info()->valid_region(), valid_region);
86
Chunosovd6afedc2017-11-06 22:09:45 +070087 // Get reduction kernel info
88 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(shape.x());
89
90 // Validate src padding
Giorgio Arena4402cb92018-02-15 13:37:40 +000091 const PaddingSize padding_src = PaddingCalculator(shape.x(), std::get<1>(reduction_info)).required_padding();
92 validate(src.info()->padding(), padding_src);
Chunosovd6afedc2017-11-06 22:09:45 +070093
94 // Validate dst padding
95 const PaddingSize padding_dst = PaddingCalculator(shape.x(), 16).required_padding();
96 validate(dst.info()->padding(), padding_dst);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010097}
98
Georgios Pinitas30902ed2017-11-14 15:32:57 +000099// *INDENT-OFF*
100// clang-format off
101DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100102 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
103 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
104 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000105 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100106 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Window shrink
107 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),// Invalid input dimensionality
108 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
109 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000110 QuantizationInfo(1.f/256, 12)),
111 }),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100112 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
113 TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
114 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000115 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100116 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000117 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100118 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
119 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000120 QuantizationInfo(1.f/256, 0)),
121 })),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100122 framework::dataset::make("Expected", { false, false, false, false, false, true, true })),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000123 input_info, output_info, expected)
124{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000125 ARM_COMPUTE_EXPECT(bool(CLSoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))) == expected, framework::LogLevel::ERRORS);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000126}
127// clang-format on
128// *INDENT-ON*
129
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100130template <typename T>
131using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
132
133TEST_SUITE(Float)
134TEST_SUITE(FP16)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100135FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
136 framework::dataset::make("DataType", DataType::F16)),
137 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100138{
139 // Validate output
140 validate(CLAccessor(_target), _reference, tolerance_f16);
141}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100142FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
143 framework::dataset::make("DataType", DataType::F16)),
144 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100145{
146 // Validate output
147 validate(CLAccessor(_target), _reference, tolerance_f16);
148}
149TEST_SUITE_END()
150
151TEST_SUITE(FP32)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100152FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
153 framework::dataset::make("DataType", DataType::F32)),
154 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100155{
156 // Validate output
157 validate(CLAccessor(_target), _reference, tolerance_f32);
158}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100159FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
160 framework::dataset::make("DataType", DataType::F32)),
161 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100162{
163 // Validate output
164 validate(CLAccessor(_target), _reference, tolerance_f32);
165}
166TEST_SUITE_END()
167TEST_SUITE_END()
168
169template <typename T>
170using CLSoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
171
Chunosovf450caa2017-11-08 16:09:35 +0700172template <typename T>
173using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
174
175TEST_SUITE(Quantized)
176TEST_SUITE(QASYMM8)
177FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100178 framework::dataset::make("DataType", DataType::QASYMM8)),
179 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
180 framework::dataset::make("Beta", { 1.0f, 2.f }))))
Chunosovf450caa2017-11-08 16:09:35 +0700181{
182 // Validate output
183 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
184}
185FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100186 framework::dataset::make("DataType", DataType::QASYMM8)),
187 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
188 framework::dataset::make("Beta", { 1.0f, 2.0f }))))
Chunosovf450caa2017-11-08 16:09:35 +0700189{
190 // Validate output
191 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
192}
193TEST_SUITE_END()
194TEST_SUITE_END()
195
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100196TEST_SUITE_END()
197TEST_SUITE_END()
198} // namespace validation
199} // namespace test
200} // namespace arm_compute