blob: 66ca0b8ca796233036ae736b2711a2b59aea4f50 [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{
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010067 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 +010068
69 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010070 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, quantization_info);
71 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, QuantizationInfo(1.f / 256.f, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010072
73 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
74 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
75
76 // Create and configure function
77 CLSoftmaxLayer smx_layer;
78 smx_layer.configure(&src, &dst);
79
80 // Validate valid region
81 const ValidRegion valid_region = shape_to_valid_region(shape);
82 validate(src.info()->valid_region(), valid_region);
83 validate(dst.info()->valid_region(), valid_region);
84
Chunosovd6afedc2017-11-06 22:09:45 +070085 // Get reduction kernel info
86 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(shape.x());
87
88 // Validate src padding
Giorgio Arena4402cb92018-02-15 13:37:40 +000089 const PaddingSize padding_src = PaddingCalculator(shape.x(), std::get<1>(reduction_info)).required_padding();
90 validate(src.info()->padding(), padding_src);
Chunosovd6afedc2017-11-06 22:09:45 +070091
92 // Validate dst padding
93 const PaddingSize padding_dst = PaddingCalculator(shape.x(), 16).required_padding();
94 validate(dst.info()->padding(), padding_dst);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010095}
96
Georgios Pinitas30902ed2017-11-14 15:32:57 +000097// *INDENT-OFF*
98// clang-format off
99DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100100 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
101 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
102 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000103 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100104 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Window shrink
105 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),// Invalid input dimensionality
106 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
107 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000108 QuantizationInfo(1.f/256, 12)),
109 }),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100110 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
111 TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
112 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000113 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100114 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000115 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100116 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
117 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000118 QuantizationInfo(1.f/256, 0)),
119 })),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100120 framework::dataset::make("Expected", { false, false, false, false, false, true, true })),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000121 input_info, output_info, expected)
122{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000123 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 +0000124}
125// clang-format on
126// *INDENT-ON*
127
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100128template <typename T>
129using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
130
131TEST_SUITE(Float)
132TEST_SUITE(FP16)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100133FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
134 framework::dataset::make("DataType", DataType::F16)),
135 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100136{
137 // Validate output
138 validate(CLAccessor(_target), _reference, tolerance_f16);
139}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100140FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
141 framework::dataset::make("DataType", DataType::F16)),
142 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100143{
144 // Validate output
145 validate(CLAccessor(_target), _reference, tolerance_f16);
146}
147TEST_SUITE_END()
148
149TEST_SUITE(FP32)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100150FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
151 framework::dataset::make("DataType", DataType::F32)),
152 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100153{
154 // Validate output
155 validate(CLAccessor(_target), _reference, tolerance_f32);
156}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100157FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
158 framework::dataset::make("DataType", DataType::F32)),
159 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100160{
161 // Validate output
162 validate(CLAccessor(_target), _reference, tolerance_f32);
163}
164TEST_SUITE_END()
165TEST_SUITE_END()
166
167template <typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700168using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
169
170TEST_SUITE(Quantized)
171TEST_SUITE(QASYMM8)
172FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100173 framework::dataset::make("DataType", DataType::QASYMM8)),
174 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
175 framework::dataset::make("Beta", { 1.0f, 2.f }))))
Chunosovf450caa2017-11-08 16:09:35 +0700176{
177 // Validate output
178 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
179}
180FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100181 framework::dataset::make("DataType", DataType::QASYMM8)),
182 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
183 framework::dataset::make("Beta", { 1.0f, 2.0f }))))
Chunosovf450caa2017-11-08 16:09:35 +0700184{
185 // Validate output
186 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
187}
188TEST_SUITE_END()
189TEST_SUITE_END()
190
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100191TEST_SUITE_END()
192TEST_SUITE_END()
193} // namespace validation
194} // namespace test
195} // namespace arm_compute