blob: f3f1c8b1b80c8fc5a5c3da0d1cc066b567e9d7c4 [file] [log] [blame]
giuros011e6e1b82019-05-14 16:12:53 +01001/*
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +00002 * Copyright (c) 2019-2021 Arm Limited.
giuros011e6e1b82019-05-14 16:12:53 +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 */
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/CLPReluLayer.h"
28#include "tests/CL/CLAccessor.h"
29#include "tests/PaddingCalculator.h"
30#include "tests/datasets/ConvertPolicyDataset.h"
31#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/ElementwiseOperationsFixture.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
45{
46RelativeTolerance<float> tolerance_fp32(0.000001f);
47RelativeTolerance<float> tolerance_fp16(0.001f);
48
giuros011e6e1b82019-05-14 16:12:53 +010049/** Input data sets **/
50const auto PReluLayerU8Dataset = combine(combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)),
51 framework::dataset::make("DataType",
52 DataType::U8));
53const auto PReluLayerQASYMM8Dataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8), framework::dataset::make("DataType", DataType::QASYMM8)),
54 framework::dataset::make("DataType",
55 DataType::QASYMM8));
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000056const auto PReluLayerQASYMM8SIGNEDDataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8_SIGNED), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
57 framework::dataset::make("DataType",
58 DataType::QASYMM8_SIGNED));
Georgios Pinitasda816752021-07-02 09:22:14 +010059const auto PReluLayerS16Dataset = combine(combine(framework::dataset::make("DataType", { DataType::S16 }), framework::dataset::make("DataType", DataType::S16)),
giuros011e6e1b82019-05-14 16:12:53 +010060 framework::dataset::make("DataType", DataType::S16));
61const auto PReluLayerFP16Dataset = combine(combine(framework::dataset::make("DataType", DataType::F16), framework::dataset::make("DataType", DataType::F16)),
62 framework::dataset::make("DataType", DataType::F16));
63const auto PReluLayerFP32Dataset = combine(combine(framework::dataset::make("DataType", DataType::F32), framework::dataset::make("DataType", DataType::F32)),
64 framework::dataset::make("DataType", DataType::F32));
65} // namespace
66
67TEST_SUITE(CL)
68TEST_SUITE(PReluLayer)
69
70// *INDENT-OFF*
71// clang-format off
72DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
73 framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
giuros011e6e1b82019-05-14 16:12:53 +010074 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid data type combination
75 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
76 }),
77 framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
giuros011e6e1b82019-05-14 16:12:53 +010078 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
79 TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
80 })),
Georgios Pinitasda816752021-07-02 09:22:14 +010081 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
giuros011e6e1b82019-05-14 16:12:53 +010082 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
83 TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
84 })),
Georgios Pinitasda816752021-07-02 09:22:14 +010085 framework::dataset::make("Expected", { true, false, false})),
giuros011e6e1b82019-05-14 16:12:53 +010086 input1_info, input2_info, output_info, expected)
87{
88 ARM_COMPUTE_EXPECT(bool(CLPReluLayer::validate(&input1_info.clone()->set_is_resizable(false), &input2_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))) == expected, framework::LogLevel::ERRORS);
89}
90// clang-format on
91// *INDENT-ON*
92
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +000093TEST_SUITE(InPlace)
94TEST_CASE(Validate, framework::DatasetMode::ALL)
95{
96 // PRelu operaotr should be able to take nullptr as output and do the in-place computation.
97 // Shape and data type are selected randomly since they shouldn't matter
98 const auto tensor_info = TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32);
99 const auto result = arm_compute::CLPReluLayer::validate(&tensor_info, &tensor_info, nullptr);
100 ARM_COMPUTE_EXPECT(bool(result) == true, framework::LogLevel::ERRORS);
101}
102
103SimpleTensor<float> compute_float_reference(const TensorInfo &tensor_info)
104{
105 SimpleTensor<float> ref_src1{ tensor_info.tensor_shape(), tensor_info.data_type() };
106 SimpleTensor<float> ref_src2{ tensor_info.tensor_shape(), tensor_info.data_type() };
107 SimpleTensor<float> ref_dst{ tensor_info.tensor_shape(), tensor_info.data_type() };
108
109 library->fill_tensor_uniform(ref_src1, 0);
110 library->fill_tensor_uniform(ref_src2, 1);
111
112 return reference::arithmetic_operation<float>(ArithmeticOperation::PRELU, ref_src1, ref_src2, ref_dst);
113}
114
115void compute_float_target_in_place(CLTensor &src1, CLTensor &src2, bool use_nullptr_output)
116{
117 auto fn = arm_compute::CLPReluLayer{};
118 fn.configure(&src1, &src2, use_nullptr_output ? nullptr : &src1);
119
120 src1.allocator()->allocate();
121 src2.allocator()->allocate();
122
123 library->fill_tensor_uniform(CLAccessor(src1), 0);
124 library->fill_tensor_uniform(CLAccessor(src2), 1);
125
126 fn.run();
127}
128
129TEST_CASE(ComputeWithNullPtr, framework::DatasetMode::ALL)
130{
131 const auto tensor_info = TensorInfo(TensorShape(33U, 13U, 2U), 1, DataType::F32);
132
133 auto src1 = create_tensor<CLTensor>(tensor_info);
134 auto src2 = create_tensor<CLTensor>(tensor_info);
135 compute_float_target_in_place(src1, src2, true);
136 validate(CLAccessor(src1), compute_float_reference(tensor_info));
137}
138
139TEST_CASE(ComputeWithSameTensor, framework::DatasetMode::ALL)
140{
141 const auto tensor_info = TensorInfo(TensorShape(33U, 13U, 2U), 1, DataType::F32);
142
143 auto src1 = create_tensor<CLTensor>(tensor_info);
144 auto src2 = create_tensor<CLTensor>(tensor_info);
145 compute_float_target_in_place(src1, src2, false);
146 validate(CLAccessor(src1), compute_float_reference(tensor_info));
147}
148TEST_SUITE_END() // InPlace
149
giuros011e6e1b82019-05-14 16:12:53 +0100150template <typename T>
151using CLPReluLayerFixture = PReluLayerValidationFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
152
153TEST_SUITE(U8)
giuros011e6e1b82019-05-14 16:12:53 +0100154FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), PReluLayerU8Dataset))
155{
156 // Validate output
157 validate(CLAccessor(_target), _reference);
158}
159TEST_SUITE_END()
160
161template <typename T>
162using CLPReluLayerQuantizedFixture = PReluLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
163
164TEST_SUITE(Quantized)
165TEST_SUITE(QASYMM8)
giuros011e6e1b82019-05-14 16:12:53 +0100166FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
167 PReluLayerQASYMM8Dataset),
168 framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 255.f, 20) })),
169 framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
170 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 5) }))
171
172 )
173{
174 // Validate output
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000175 validate(CLAccessor(_target), _reference);
176}
177TEST_SUITE_END()
178
179TEST_SUITE(QASYMM8_SIGNED)
180FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
181 PReluLayerQASYMM8SIGNEDDataset),
182 framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 127.f, 20) })),
183 framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 127.f, 10) })),
184 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 127.f, 5) }))
185
186 )
187{
188 // Validate output
189 validate(CLAccessor(_target), _reference);
giuros011e6e1b82019-05-14 16:12:53 +0100190}
191TEST_SUITE_END()
192TEST_SUITE_END()
193
194TEST_SUITE(S16)
giuros011e6e1b82019-05-14 16:12:53 +0100195FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<int16_t>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerS16Dataset))
196{
197 // Validate output
198 validate(CLAccessor(_target), _reference);
199}
Freddie Liardet20984452021-05-12 12:09:12 +0100200FIXTURE_DATA_TEST_CASE(RunOneDimensional, CLPReluLayerFixture<int16_t>, framework::DatasetMode::ALL, combine(framework::dataset::make("Shape", TensorShape(1U, 16U)), PReluLayerS16Dataset))
201{
202 // Validate output
203 validate(CLAccessor(_target), _reference);
204}
giuros011e6e1b82019-05-14 16:12:53 +0100205TEST_SUITE_END()
206
207TEST_SUITE(Float)
208TEST_SUITE(FP16)
209FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<half>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP16Dataset))
210{
211 // Validate output
212 validate(CLAccessor(_target), _reference, tolerance_fp16, 0.01);
213}
214TEST_SUITE_END()
215
216TEST_SUITE(FP32)
giuros011e6e1b82019-05-14 16:12:53 +0100217FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP32Dataset))
218{
219 // Validate output
220 validate(CLAccessor(_target), _reference, tolerance_fp32);
221}
222template <typename T>
223using CLPReluLayerBroadcastFixture = PReluLayerBroadcastValidationFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
224
225FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLPReluLayerBroadcastFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapesBroadcast(),
226 PReluLayerFP32Dataset))
227{
228 // Validate output
229 validate(CLAccessor(_target), _reference, tolerance_fp32);
230}
231TEST_SUITE_END()
232TEST_SUITE_END()
233
234TEST_SUITE_END()
235TEST_SUITE_END()
236} // namespace validation
237} // namespace test
238} // namespace arm_compute