1 /*
2 * Copyright (c) 2017-2021 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/CL/CLTensor.h"
26 #include "arm_compute/runtime/CL/CLTensorAllocator.h"
27 #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h"
28 #include "tests/CL/CLAccessor.h"
29 #include "tests/PaddingCalculator.h"
30 #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
37 #include "arm_compute/runtime/MemoryManagerOnDemand.h"
38 #include "arm_compute/runtime/PoolManager.h"
39 #include "arm_compute/runtime/BlobLifetimeManager.h"
40 #include "arm_compute/runtime/CL/CLBufferAllocator.h"
41 #include "arm_compute/runtime/BlobMemoryPool.h"
42
43 namespace arm_compute
44 {
45 namespace test
46 {
47 namespace validation
48 {
49 namespace
50 {
51 /** Tolerance for float operations */
52 RelativeTolerance<half> tolerance_f16(half(0.2));
53 RelativeTolerance<float> tolerance_f32(0.001f);
54
55 /** Tolerance for quantized operations */
56 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
57 constexpr AbsoluteTolerance<int8_t> tolerance_qasymm8_signed(1);
58
59 /** CNN data types */
60 const auto CNNDataTypes = framework::dataset::make("DataType",
61 {
62 DataType::QASYMM8,
63 DataType::F16,
64 DataType::F32,
65 });
66 } // namespace
67
68 TEST_SUITE(CL)
TEST_SUITE(SoftmaxLayer)69 TEST_SUITE(SoftmaxLayer)
70
71 TEST_CASE(SimpleMemoryManaged, framework::DatasetMode::ALL)
72 {
73 // The purpose of this test is to test if the function can
74 // run correctly even with the given memory manager from its caller
75 // (Similar scenario when the library is integrated into other software)
76 // especially when working with workspace() method of
77 // @ref arm_compute::opencl::ClSoftmax.
78 const auto shape = TensorShape{4,2}; // Random shape, not important
79 constexpr auto dt = DataType::F32; // Random data type, not important
80
81 // Create a memory manager
82 auto lm = std::make_shared<BlobLifetimeManager>();
83 auto pm = std::make_shared<arm_compute::PoolManager>();
84 auto alloc = std::make_unique<CLBufferAllocator>();
85 auto mm = std::make_shared<MemoryManagerOnDemand>(lm, pm);
86
87 auto src = create_tensor<CLTensor>(shape, dt);
88 auto dst = create_tensor<CLTensor>(shape, dt);
89 src.allocator()->allocate();
90 dst.allocator()->allocate();
91
92 // Create the function with the memory manager
93 CLSoftmaxLayer smx(mm);
94 smx.configure(&src, &dst);
95
96 // Populate the memory, acquire() will happen in run()
97 mm->populate(*alloc.get(), 1);
98
99 std::vector<float> input_vals{0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,};
100 library->fill_static_values(CLAccessor(src), input_vals);
101
102 smx.run();
103
104 // Compute reference to compare
105 SimpleTensor<float> ref_src{shape, dt};
106 library->fill_static_values(ref_src, input_vals);
107 auto ref_dst = reference::softmax_layer<float>(ref_src, 1., 0, false);
108
109 validate(CLAccessor(dst), ref_dst);
110 }
111
112 // *INDENT-OFF*
113 // clang-format off
114 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
115 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
116 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
117 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
118 QuantizationInfo(1.f/256, 12)),
119 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
120 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
121 QuantizationInfo(1.f/256, 12)),
122 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
123 QuantizationInfo(1.f/256, 12)),
124 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED, // Invalid axis high
125 QuantizationInfo(1.f/256, 12)),
126 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED, // Invalid axis low
127 QuantizationInfo(1.f/256, 12))
128 }),
129 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
130 TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
131 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
132 QuantizationInfo(1.f/256, 12)),
133 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
134 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
135 QuantizationInfo(1.f/256, 0)),
136 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
137 QuantizationInfo(1.f/256, -128)),
138 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
139 QuantizationInfo(1.f/256, -128)),
140 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
141 QuantizationInfo(1.f/256, -128)),
142 })),
143 framework::dataset::make("beta", { 1.0,
144 2.0,
145 1.0,
146 2.0,
147 1.0,
148 2.0,
149 1.0,
150 2.0,
151 })),
152 framework::dataset::make("axis", {
153 0,
154 0,
155 0,
156 1,
157 0,
158 -1,
159 2,
160 -3,
161 })),
162 framework::dataset::make("Expected", { false, false, false, true, true, true, false, false })),
163 input_info, output_info, beta, axis, expected)
164 {
165 ARM_COMPUTE_EXPECT(bool(CLSoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), beta, axis)) == expected, framework::LogLevel::ERRORS);
166 }
167 // clang-format on
168 // *INDENT-ON*
169
170 template <typename T>
171 using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
172
173 TEST_SUITE(Float)
TEST_SUITE(FP16)174 TEST_SUITE(FP16)
175 FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
176 framework::dataset::make("DataType", DataType::F16)),
177 framework::dataset::make("Beta", { 1.0f, 2.0f })),
178 framework::dataset::make("Axis", { 0, -1 })))
179 {
180 // Validate output
181 validate(CLAccessor(_target), _reference, tolerance_f16);
182 }
183 FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
184 framework::dataset::make("DataType", DataType::F16)),
185 framework::dataset::make("Beta", { 1.0f, 2.0f })),
186 framework::dataset::make("Axis", { 0 })))
187 {
188 // Validate output
189 validate(CLAccessor(_target), _reference, tolerance_f16);
190 }
191 FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
192 framework::dataset::make("DataType", DataType::F16)),
193 framework::dataset::make("Beta", { 1.0f, 2.0f })),
194 framework::dataset::make("Axis", { 0, -1, 2 })))
195 {
196 // Validate output
197 validate(CLAccessor(_target), _reference, tolerance_f16);
198 }
199 TEST_SUITE_END()
200
TEST_SUITE(FP32)201 TEST_SUITE(FP32)
202 FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
203 framework::dataset::make("DataType", DataType::F32)),
204 framework::dataset::make("Beta", { 1.0f, 2.0f })),
205 framework::dataset::make("Axis", { 0, 1 })))
206 {
207 // Validate output
208 validate(CLAccessor(_target), _reference, tolerance_f32);
209 }
210 FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
211 framework::dataset::make("DataType", DataType::F32)),
212 framework::dataset::make("Beta", { 1.0f, 2.0f })),
213 framework::dataset::make("Axis", { 0 })))
214 {
215 // Validate output
216 validate(CLAccessor(_target), _reference, tolerance_f32);
217 }
218 FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
219 framework::dataset::make("DataType", DataType::F32)),
220 framework::dataset::make("Beta", { 1.0f, 2.0f })),
221 framework::dataset::make("Axis", { 0, -2, 3 })))
222 {
223 // Validate output
224 validate(CLAccessor(_target), _reference, tolerance_f32);
225 }
226 TEST_SUITE_END()
227 TEST_SUITE_END()
228
229 template <typename T>
230 using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
231
232 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)233 TEST_SUITE(QASYMM8)
234 FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
235 framework::dataset::make("DataType", DataType::QASYMM8)),
236 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
237 framework::dataset::make("Beta", { 1.0f, 2.f }))),
238 framework::dataset::make("Axis", { 0, 1 })))
239 {
240 // Validate output
241 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
242 }
243 FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
244 framework::dataset::make("DataType", DataType::QASYMM8)),
245 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
246 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
247 framework::dataset::make("Axis", { 0 })))
248 {
249 // Validate output
250 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
251 }
252 FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
253 framework::dataset::make("DataType", DataType::QASYMM8)),
254 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
255 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
256 framework::dataset::make("Axis", { 0, -4, 1 })))
257 {
258 // Validate output
259 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
260 }
261
262 TEST_SUITE_END() // QASYMM8
263
TEST_SUITE(QASYMM8_SIGNED)264 TEST_SUITE(QASYMM8_SIGNED)
265
266 FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
267 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
268 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
269 framework::dataset::make("Beta", { 1.0f, 2.f }))),
270 framework::dataset::make("Axis", { 0, 1 })))
271 {
272 // Validate output
273 validate(CLAccessor(_target), _reference, tolerance_qasymm8_signed);
274 }
275
276 TEST_SUITE_END() // QASYMM8_SIGNED
277 TEST_SUITE_END() // Quantized
278 TEST_SUITE_END() // SoftmaxLayer
279 TEST_SUITE_END() // CL
280 } // namespace validation
281 } // namespace test
282 } // namespace arm_compute
283