1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2019-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/core/Types.h"
25*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensor.h"
26*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensorAllocator.h"
27*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLPReluLayer.h"
28*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
29*c217d954SCole Faust #include "tests/PaddingCalculator.h"
30*c217d954SCole Faust #include "tests/datasets/ConvertPolicyDataset.h"
31*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
32*c217d954SCole Faust #include "tests/framework/Asserts.h"
33*c217d954SCole Faust #include "tests/framework/Macros.h"
34*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
35*c217d954SCole Faust #include "tests/validation/Validation.h"
36*c217d954SCole Faust #include "tests/validation/fixtures/ElementwiseOperationsFixture.h"
37*c217d954SCole Faust
38*c217d954SCole Faust namespace arm_compute
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace test
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace validation
43*c217d954SCole Faust {
44*c217d954SCole Faust namespace
45*c217d954SCole Faust {
46*c217d954SCole Faust RelativeTolerance<float> tolerance_fp32(0.000001f);
47*c217d954SCole Faust RelativeTolerance<float> tolerance_fp16(0.001f);
48*c217d954SCole Faust
49*c217d954SCole Faust /** Input data sets **/
50*c217d954SCole Faust const auto PReluLayerU8Dataset = combine(combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)),
51*c217d954SCole Faust framework::dataset::make("DataType",
52*c217d954SCole Faust DataType::U8));
53*c217d954SCole Faust const auto PReluLayerQASYMM8Dataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8), framework::dataset::make("DataType", DataType::QASYMM8)),
54*c217d954SCole Faust framework::dataset::make("DataType",
55*c217d954SCole Faust DataType::QASYMM8));
56*c217d954SCole Faust const auto PReluLayerQASYMM8SIGNEDDataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8_SIGNED), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
57*c217d954SCole Faust framework::dataset::make("DataType",
58*c217d954SCole Faust DataType::QASYMM8_SIGNED));
59*c217d954SCole Faust const auto PReluLayerS16Dataset = combine(combine(framework::dataset::make("DataType", { DataType::S16 }), framework::dataset::make("DataType", DataType::S16)),
60*c217d954SCole Faust framework::dataset::make("DataType", DataType::S16));
61*c217d954SCole Faust const auto PReluLayerFP16Dataset = combine(combine(framework::dataset::make("DataType", DataType::F16), framework::dataset::make("DataType", DataType::F16)),
62*c217d954SCole Faust framework::dataset::make("DataType", DataType::F16));
63*c217d954SCole Faust const auto PReluLayerFP32Dataset = combine(combine(framework::dataset::make("DataType", DataType::F32), framework::dataset::make("DataType", DataType::F32)),
64*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32));
65*c217d954SCole Faust } // namespace
66*c217d954SCole Faust
67*c217d954SCole Faust TEST_SUITE(CL)
TEST_SUITE(PReluLayer)68*c217d954SCole Faust TEST_SUITE(PReluLayer)
69*c217d954SCole Faust
70*c217d954SCole Faust // *INDENT-OFF*
71*c217d954SCole Faust // clang-format off
72*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
73*c217d954SCole Faust framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
74*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid data type combination
75*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
76*c217d954SCole Faust }),
77*c217d954SCole Faust framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
78*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
79*c217d954SCole Faust TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
80*c217d954SCole Faust })),
81*c217d954SCole Faust framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
82*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
83*c217d954SCole Faust TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
84*c217d954SCole Faust })),
85*c217d954SCole Faust framework::dataset::make("Expected", { true, false, false})),
86*c217d954SCole Faust input1_info, input2_info, output_info, expected)
87*c217d954SCole Faust {
88*c217d954SCole Faust 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*c217d954SCole Faust }
90*c217d954SCole Faust // clang-format on
91*c217d954SCole Faust // *INDENT-ON*
92*c217d954SCole Faust
93*c217d954SCole Faust TEST_SUITE(InPlace)
TEST_CASE(Validate,framework::DatasetMode::ALL)94*c217d954SCole Faust TEST_CASE(Validate, framework::DatasetMode::ALL)
95*c217d954SCole Faust {
96*c217d954SCole Faust // PRelu operaotr should be able to take nullptr as output and do the in-place computation.
97*c217d954SCole Faust // Shape and data type are selected randomly since they shouldn't matter
98*c217d954SCole Faust const auto tensor_info = TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32);
99*c217d954SCole Faust const auto result = arm_compute::CLPReluLayer::validate(&tensor_info, &tensor_info, nullptr);
100*c217d954SCole Faust ARM_COMPUTE_EXPECT(bool(result) == true, framework::LogLevel::ERRORS);
101*c217d954SCole Faust }
102*c217d954SCole Faust
compute_float_reference(const TensorInfo & tensor_info)103*c217d954SCole Faust SimpleTensor<float> compute_float_reference(const TensorInfo &tensor_info)
104*c217d954SCole Faust {
105*c217d954SCole Faust SimpleTensor<float> ref_src1{ tensor_info.tensor_shape(), tensor_info.data_type() };
106*c217d954SCole Faust SimpleTensor<float> ref_src2{ tensor_info.tensor_shape(), tensor_info.data_type() };
107*c217d954SCole Faust SimpleTensor<float> ref_dst{ tensor_info.tensor_shape(), tensor_info.data_type() };
108*c217d954SCole Faust
109*c217d954SCole Faust library->fill_tensor_uniform(ref_src1, 0);
110*c217d954SCole Faust library->fill_tensor_uniform(ref_src2, 1);
111*c217d954SCole Faust
112*c217d954SCole Faust return reference::arithmetic_operation<float>(ArithmeticOperation::PRELU, ref_src1, ref_src2, ref_dst);
113*c217d954SCole Faust }
114*c217d954SCole Faust
compute_float_target_in_place(CLTensor & src1,CLTensor & src2,bool use_nullptr_output)115*c217d954SCole Faust void compute_float_target_in_place(CLTensor &src1, CLTensor &src2, bool use_nullptr_output)
116*c217d954SCole Faust {
117*c217d954SCole Faust auto fn = arm_compute::CLPReluLayer{};
118*c217d954SCole Faust fn.configure(&src1, &src2, use_nullptr_output ? nullptr : &src1);
119*c217d954SCole Faust
120*c217d954SCole Faust src1.allocator()->allocate();
121*c217d954SCole Faust src2.allocator()->allocate();
122*c217d954SCole Faust
123*c217d954SCole Faust library->fill_tensor_uniform(CLAccessor(src1), 0);
124*c217d954SCole Faust library->fill_tensor_uniform(CLAccessor(src2), 1);
125*c217d954SCole Faust
126*c217d954SCole Faust fn.run();
127*c217d954SCole Faust }
128*c217d954SCole Faust
TEST_CASE(ComputeWithNullPtr,framework::DatasetMode::ALL)129*c217d954SCole Faust TEST_CASE(ComputeWithNullPtr, framework::DatasetMode::ALL)
130*c217d954SCole Faust {
131*c217d954SCole Faust const auto tensor_info = TensorInfo(TensorShape(33U, 13U, 2U), 1, DataType::F32);
132*c217d954SCole Faust
133*c217d954SCole Faust auto src1 = create_tensor<CLTensor>(tensor_info);
134*c217d954SCole Faust auto src2 = create_tensor<CLTensor>(tensor_info);
135*c217d954SCole Faust compute_float_target_in_place(src1, src2, true);
136*c217d954SCole Faust validate(CLAccessor(src1), compute_float_reference(tensor_info));
137*c217d954SCole Faust }
138*c217d954SCole Faust
TEST_CASE(ComputeWithSameTensor,framework::DatasetMode::ALL)139*c217d954SCole Faust TEST_CASE(ComputeWithSameTensor, framework::DatasetMode::ALL)
140*c217d954SCole Faust {
141*c217d954SCole Faust const auto tensor_info = TensorInfo(TensorShape(33U, 13U, 2U), 1, DataType::F32);
142*c217d954SCole Faust
143*c217d954SCole Faust auto src1 = create_tensor<CLTensor>(tensor_info);
144*c217d954SCole Faust auto src2 = create_tensor<CLTensor>(tensor_info);
145*c217d954SCole Faust compute_float_target_in_place(src1, src2, false);
146*c217d954SCole Faust validate(CLAccessor(src1), compute_float_reference(tensor_info));
147*c217d954SCole Faust }
148*c217d954SCole Faust TEST_SUITE_END() // InPlace
149*c217d954SCole Faust
150*c217d954SCole Faust template <typename T>
151*c217d954SCole Faust using CLPReluLayerFixture = PReluLayerValidationFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
152*c217d954SCole Faust
153*c217d954SCole Faust TEST_SUITE(U8)
FIXTURE_DATA_TEST_CASE(RunSmall,CLPReluLayerFixture<uint8_t>,framework::DatasetMode::PRECOMMIT,combine (datasets::SmallShapes (),PReluLayerU8Dataset))154*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), PReluLayerU8Dataset))
155*c217d954SCole Faust {
156*c217d954SCole Faust // Validate output
157*c217d954SCole Faust validate(CLAccessor(_target), _reference);
158*c217d954SCole Faust }
159*c217d954SCole Faust TEST_SUITE_END()
160*c217d954SCole Faust
161*c217d954SCole Faust template <typename T>
162*c217d954SCole Faust using CLPReluLayerQuantizedFixture = PReluLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
163*c217d954SCole Faust
164*c217d954SCole Faust TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)165*c217d954SCole Faust TEST_SUITE(QASYMM8)
166*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
167*c217d954SCole Faust PReluLayerQASYMM8Dataset),
168*c217d954SCole Faust framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 255.f, 20) })),
169*c217d954SCole Faust framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
170*c217d954SCole Faust framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 5) }))
171*c217d954SCole Faust
172*c217d954SCole Faust )
173*c217d954SCole Faust {
174*c217d954SCole Faust // Validate output
175*c217d954SCole Faust validate(CLAccessor(_target), _reference);
176*c217d954SCole Faust }
177*c217d954SCole Faust TEST_SUITE_END()
178*c217d954SCole Faust
TEST_SUITE(QASYMM8_SIGNED)179*c217d954SCole Faust TEST_SUITE(QASYMM8_SIGNED)
180*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
181*c217d954SCole Faust PReluLayerQASYMM8SIGNEDDataset),
182*c217d954SCole Faust framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 127.f, 20) })),
183*c217d954SCole Faust framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 127.f, 10) })),
184*c217d954SCole Faust framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 127.f, 5) }))
185*c217d954SCole Faust
186*c217d954SCole Faust )
187*c217d954SCole Faust {
188*c217d954SCole Faust // Validate output
189*c217d954SCole Faust validate(CLAccessor(_target), _reference);
190*c217d954SCole Faust }
191*c217d954SCole Faust TEST_SUITE_END()
TEST_SUITE_END()192*c217d954SCole Faust TEST_SUITE_END()
193*c217d954SCole Faust
194*c217d954SCole Faust TEST_SUITE(S16)
195*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<int16_t>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerS16Dataset))
196*c217d954SCole Faust {
197*c217d954SCole Faust // Validate output
198*c217d954SCole Faust validate(CLAccessor(_target), _reference);
199*c217d954SCole Faust }
200*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunOneDimensional, CLPReluLayerFixture<int16_t>, framework::DatasetMode::ALL, combine(framework::dataset::make("Shape", TensorShape(1U, 16U)), PReluLayerS16Dataset))
201*c217d954SCole Faust {
202*c217d954SCole Faust // Validate output
203*c217d954SCole Faust validate(CLAccessor(_target), _reference);
204*c217d954SCole Faust }
205*c217d954SCole Faust TEST_SUITE_END()
206*c217d954SCole Faust
TEST_SUITE(Float)207*c217d954SCole Faust TEST_SUITE(Float)
208*c217d954SCole Faust TEST_SUITE(FP16)
209*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<half>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP16Dataset))
210*c217d954SCole Faust {
211*c217d954SCole Faust // Validate output
212*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp16, 0.01);
213*c217d954SCole Faust }
214*c217d954SCole Faust TEST_SUITE_END()
215*c217d954SCole Faust
TEST_SUITE(FP32)216*c217d954SCole Faust TEST_SUITE(FP32)
217*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLPReluLayerFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP32Dataset))
218*c217d954SCole Faust {
219*c217d954SCole Faust // Validate output
220*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp32);
221*c217d954SCole Faust }
222*c217d954SCole Faust template <typename T>
223*c217d954SCole Faust using CLPReluLayerBroadcastFixture = PReluLayerBroadcastValidationFixture<CLTensor, CLAccessor, CLPReluLayer, T>;
224*c217d954SCole Faust
FIXTURE_DATA_TEST_CASE(RunSmallBroadcast,CLPReluLayerBroadcastFixture<float>,framework::DatasetMode::ALL,combine (datasets::SmallShapesBroadcast (),PReluLayerFP32Dataset))225*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLPReluLayerBroadcastFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapesBroadcast(),
226*c217d954SCole Faust PReluLayerFP32Dataset))
227*c217d954SCole Faust {
228*c217d954SCole Faust // Validate output
229*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp32);
230*c217d954SCole Faust }
231*c217d954SCole Faust TEST_SUITE_END()
232*c217d954SCole Faust TEST_SUITE_END()
233*c217d954SCole Faust
234*c217d954SCole Faust TEST_SUITE_END()
235*c217d954SCole Faust TEST_SUITE_END()
236*c217d954SCole Faust } // namespace validation
237*c217d954SCole Faust } // namespace test
238*c217d954SCole Faust } // namespace arm_compute
239