xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/NEON/PReluLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-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/NEON/functions/NEPReluLayer.h"
26 #include "arm_compute/runtime/Tensor.h"
27 #include "arm_compute/runtime/TensorAllocator.h"
28 #include "tests/NEON/Accessor.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/ElementwiseOperationsFixture.h"
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace validation
42 {
43 namespace
44 {
45 RelativeTolerance<float>  tolerance_fp32(0.000001f);
46 AbsoluteTolerance<int8_t> tolerance_s8(1);
47 
48 /** Input data sets **/
49 const auto PReluLayerQASYMM8Dataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8), framework::dataset::make("DataType", DataType::QASYMM8)),
50                                               framework::dataset::make("DataType",
51                                                                        DataType::QASYMM8));
52 const auto PReluLayerQASYMM8SignedDataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8_SIGNED), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
53                                                     framework::dataset::make("DataType", DataType::QASYMM8_SIGNED));
54 const auto PReluLayerFP32Dataset = combine(combine(framework::dataset::make("DataType", DataType::F32), framework::dataset::make("DataType", DataType::F32)),
55                                            framework::dataset::make("DataType", DataType::F32));
56 
57 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
58 RelativeTolerance<float> tolerance_fp16(0.001f);
59 
60 const auto PReluLayerFP16Dataset = combine(combine(framework::dataset::make("DataType", DataType::F16), framework::dataset::make("DataType", DataType::F16)),
61                                            framework::dataset::make("DataType", DataType::F16));
62 
63 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
64 
65 } // namespace
66 
67 TEST_SUITE(NEON)
TEST_SUITE(PReluLayer)68 TEST_SUITE(PReluLayer)
69 
70 // *INDENT-OFF*
71 // clang-format off
72 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
73                framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
74                                                         TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
75                                                         TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8),      // Window shrink
76                                                         TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),      // Invalid data type combination
77                                                         TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),     // Mismatching shapes
78                                                       }),
79                framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
80                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
81                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8),
82                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
83                                                        TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
84                                                      })),
85                framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
86                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
87                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8),
88                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
89                                                        TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
90                                                      })),
91                framework::dataset::make("Expected", { true, true, false, false, false})),
92                input1_info, input2_info, output_info, expected)
93 {
94     ARM_COMPUTE_EXPECT(bool(NEPReluLayer::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);
95 }
96 // clang-format on
97 // *INDENT-ON*
98 
99 template <typename T>
100 using NEPReluLayerFixture = PReluLayerValidationFixture<Tensor, Accessor, NEPReluLayer, T>;
101 
102 template <typename T>
103 using NEPReluLayerQuantizedFixture = PReluLayerValidationQuantizedFixture<Tensor, Accessor, NEPReluLayer, T>;
104 
105 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)106 TEST_SUITE(QASYMM8)
107 FIXTURE_DATA_TEST_CASE(RunSmall, NEPReluLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallShapes(),
108                                                                                                                      PReluLayerQASYMM8Dataset),
109                                                                                                                      framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 255.f, 20) })),
110                                                                                                                      framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
111                                                                                                              framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 5) }))
112 
113                       )
114 {
115     // Validate output
116     validate(Accessor(_target), _reference, tolerance_fp32, 0.01);
117 }
118 
119 FIXTURE_DATA_TEST_CASE(RunLarge, NEPReluLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::LargeShapes(),
120                                                                                                                  PReluLayerQASYMM8Dataset),
121                                                                                                                  framework::dataset::make("QuantizationInfo", { QuantizationInfo(5.f / 255.f, 20) })),
122                                                                                                                  framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
123                                                                                                                  framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 5) }))
124 
125                       )
126 {
127     // Validate output
128     validate(Accessor(_target), _reference, tolerance_fp32, 0.01);
129 }
130 TEST_SUITE_END() // QASYMM8
131 
TEST_SUITE(QASYMM8_SIGNED)132 TEST_SUITE(QASYMM8_SIGNED)
133 FIXTURE_DATA_TEST_CASE(RunSmall, NEPReluLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallShapes(),
134                                                                                                                     PReluLayerQASYMM8SignedDataset),
135                                                                                                                     framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.2f, 127) })),
136                                                                                                                     framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 64) })),
137                                                                                                             framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -128) }))
138 
139                       )
140 {
141     // Validate output
142     validate(Accessor(_target), _reference, tolerance_s8, 0.01);
143 }
144 
145 FIXTURE_DATA_TEST_CASE(RunLarge, NEPReluLayerQuantizedFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::LargeShapes(),
146                                                                                                                         PReluLayerQASYMM8SignedDataset),
147                                                                                                                         framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 20) })),
148                                                                                                                         framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })),
149                                                                                                                 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 5) }))
150 
151                       )
152 {
153     // Validate output
154     validate(Accessor(_target), _reference, tolerance_s8, 0.01);
155 }
156 TEST_SUITE_END() // QASYMM8_SIGNED
TEST_SUITE_END()157 TEST_SUITE_END() // Quantized
158 
159 TEST_SUITE(Float)
160 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
161 TEST_SUITE(FP16)
162 FIXTURE_DATA_TEST_CASE(RunSmall, NEPReluLayerFixture<half>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP16Dataset))
163 {
164     // Validate output
165     validate(Accessor(_target), _reference, tolerance_fp16, 0.01);
166 }
167 
FIXTURE_DATA_TEST_CASE(RunLarge,NEPReluLayerFixture<half>,framework::DatasetMode::NIGHTLY,combine (datasets::LargeShapes (),PReluLayerFP16Dataset))168 FIXTURE_DATA_TEST_CASE(RunLarge, NEPReluLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), PReluLayerFP16Dataset))
169 {
170     // Validate output
171     validate(Accessor(_target), _reference, tolerance_fp16, 0.01);
172 }
173 TEST_SUITE_END() // FP16
174 #endif           /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
175 
TEST_SUITE(FP32)176 TEST_SUITE(FP32)
177 FIXTURE_DATA_TEST_CASE(RunSmall, NEPReluLayerFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), PReluLayerFP32Dataset))
178 {
179     // Validate output
180     validate(Accessor(_target), _reference, tolerance_fp32);
181 }
182 
FIXTURE_DATA_TEST_CASE(RunLarge,NEPReluLayerFixture<float>,framework::DatasetMode::NIGHTLY,combine (datasets::LargeShapes (),PReluLayerFP32Dataset))183 FIXTURE_DATA_TEST_CASE(RunLarge, NEPReluLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), PReluLayerFP32Dataset))
184 {
185     // Validate output
186     validate(Accessor(_target), _reference, tolerance_fp32);
187 }
188 
189 template <typename T>
190 using NEPReluLayerBroadcastFixture = PReluLayerBroadcastValidationFixture<Tensor, Accessor, NEPReluLayer, T>;
191 
FIXTURE_DATA_TEST_CASE(RunSmallBroadcast,NEPReluLayerBroadcastFixture<float>,framework::DatasetMode::ALL,combine (datasets::SmallShapesBroadcast (),PReluLayerFP32Dataset))192 FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, NEPReluLayerBroadcastFixture<float>, framework::DatasetMode::ALL, combine(datasets::SmallShapesBroadcast(),
193                                                                                                                     PReluLayerFP32Dataset))
194 {
195     // Validate output
196     validate(Accessor(_target), _reference, tolerance_fp32);
197 }
198 
FIXTURE_DATA_TEST_CASE(RunLargeBroadcast,NEPReluLayerBroadcastFixture<float>,framework::DatasetMode::NIGHTLY,combine (datasets::LargeShapesBroadcast (),PReluLayerFP32Dataset))199 FIXTURE_DATA_TEST_CASE(RunLargeBroadcast, NEPReluLayerBroadcastFixture<float>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapesBroadcast(),
200                                                                                                                         PReluLayerFP32Dataset))
201 {
202     // Validate output
203     validate(Accessor(_target), _reference, tolerance_fp32);
204 }
205 TEST_SUITE_END() // FP32
206 TEST_SUITE_END() // Float
207 
208 TEST_SUITE_END() // PReluLayer
209 TEST_SUITE_END() // Neon
210 } // namespace validation
211 } // namespace test
212 } // namespace arm_compute
213