xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/CL/ElementwiseSquaredDiff.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-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/CLElementwiseOperations.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 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 namespace
45 {
46 RelativeTolerance<float> tolerance_fp32(0.000001f);
47 RelativeTolerance<float> tolerance_fp16(0.001f);
48 AbsoluteTolerance<float> tolerance_qsymm16(1);
49 
50 /** Input data sets **/
51 const auto ElementwiseSquaredDiffU8Dataset = combine(combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)),
52                                                      framework::dataset::make("DataType",
53                                                                               DataType::U8));
54 const auto ElementwiseSquaredDiffQASYMM8Dataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8), framework::dataset::make("DataType", DataType::QASYMM8)),
55                                                           framework::dataset::make("DataType",
56                                                                                    DataType::QASYMM8));
57 const auto ElementwiseSquaredDiffQSYMM16Dataset = combine(combine(framework::dataset::make("DataType", DataType::QSYMM16), framework::dataset::make("DataType", DataType::QSYMM16)),
58                                                           framework::dataset::make("DataType",
59                                                                                    DataType::QSYMM16));
60 const auto ElementwiseSquaredDiffS16Dataset = combine(combine(framework::dataset::make("DataType", { DataType::S16 }), framework::dataset::make("DataType", DataType::S16)),
61                                                       framework::dataset::make("DataType", DataType::S16));
62 const auto ElementwiseSquaredDiffFP16Dataset = combine(combine(framework::dataset::make("DataType", DataType::F16), framework::dataset::make("DataType", DataType::F16)),
63                                                        framework::dataset::make("DataType", DataType::F16));
64 const auto ElementwiseSquaredDiffFP32Dataset = combine(combine(framework::dataset::make("DataType", DataType::F32), framework::dataset::make("DataType", DataType::F32)),
65                                                        framework::dataset::make("DataType", DataType::F32));
66 const auto EmptyActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
67 { ActivationLayerInfo() });
68 const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
69 {
70     ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.75f, 0.25f),
71     ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC, 0.75f, 0.25f)
72 });
73 const auto InPlaceDataSet    = framework::dataset::make("InPlace", { false, true });
74 const auto OutOfPlaceDataSet = framework::dataset::make("InPlace", { false });
75 } // namespace
76 
77 TEST_SUITE(CL)
TEST_SUITE(ElementwiseSquaredDiff)78 TEST_SUITE(ElementwiseSquaredDiff)
79 
80 // *INDENT-OFF*
81 // clang-format off
82 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
83                framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
84                                                         TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),      // Invalid data type combination
85                                                         TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),     // Mismatching shapes
86                                                       }),
87                framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
88                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
89                                                        TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
90                                                      })),
91                framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
92                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
93                                                        TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
94                                                      })),
95                framework::dataset::make("Expected", { true, false, false})),
96                input1_info, input2_info, output_info, expected)
97 {
98     ARM_COMPUTE_EXPECT(bool(CLElementwiseSquaredDiff::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);
99 }
100 // clang-format on
101 // *INDENT-ON*
102 
103 template <typename T>
104 using CLElementwiseSquaredDiffFixture = ElementwiseSquaredDiffValidationFixture<CLTensor, CLAccessor, CLElementwiseSquaredDiff, T>;
105 
106 TEST_SUITE(Integer)
TEST_SUITE(U8)107 TEST_SUITE(U8)
108 FIXTURE_DATA_TEST_CASE(RunSmall, CLElementwiseSquaredDiffFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ElementwiseSquaredDiffU8Dataset),
109                                                                                                                       OutOfPlaceDataSet))
110 {
111     // Validate output
112     validate(CLAccessor(_target), _reference);
113 }
114 TEST_SUITE_END()
115 
TEST_SUITE(S16)116 TEST_SUITE(S16)
117 FIXTURE_DATA_TEST_CASE(RunSmall, CLElementwiseSquaredDiffFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ElementwiseSquaredDiffS16Dataset),
118                                                                                                                 OutOfPlaceDataSet))
119 {
120     // Validate output
121     validate(CLAccessor(_target), _reference);
122 }
123 TEST_SUITE_END()
124 TEST_SUITE_END()
125 
126 template <typename T>
127 using CLElementwiseSquaredDiffQuantizedFixture = ElementwiseSquaredDiffValidationQuantizedFixture<CLTensor, CLAccessor, CLElementwiseSquaredDiff, T>;
128 
129 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)130 TEST_SUITE(QASYMM8)
131 FIXTURE_DATA_TEST_CASE(RunSmall, CLElementwiseSquaredDiffQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(datasets::SmallShapes(),
132                        ElementwiseSquaredDiffQASYMM8Dataset),
133                        framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 20) })),
134                        framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
135                        framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })),
136                        OutOfPlaceDataSet))
137 {
138     // Validate output
139     validate(CLAccessor(_target), _reference, tolerance_fp32, 0.01);
140 }
141 TEST_SUITE_END()
TEST_SUITE(QSYMM16)142 TEST_SUITE(QSYMM16)
143 FIXTURE_DATA_TEST_CASE(RunSmall, CLElementwiseSquaredDiffQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(datasets::SmallShapes(),
144                        ElementwiseSquaredDiffQSYMM16Dataset),
145                        framework::dataset::make("Src0QInfo", { QuantizationInfo(1.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
146                        framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
147                        framework::dataset::make("OutQInfo", { QuantizationInfo(5.f / 32768.f, 0) })),
148                        OutOfPlaceDataSet))
149 {
150     // Validate output
151     validate(CLAccessor(_target), _reference, tolerance_qsymm16);
152 }
153 TEST_SUITE_END()
154 TEST_SUITE_END()
155 
156 template <typename T>
157 using CLElementwiseSquaredDiffFloatFixture = ElementwiseSquaredDiffValidationFloatFixture<CLTensor, CLAccessor, CLElementwiseSquaredDiff, T>;
158 
159 TEST_SUITE(Float)
TEST_SUITE(FP16)160 TEST_SUITE(FP16)
161 FIXTURE_DATA_TEST_CASE(RunSmall, CLElementwiseSquaredDiffFloatFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), ElementwiseSquaredDiffFP16Dataset),
162                                                                                                                   EmptyActivationFunctionsDataset),
163                                                                                                                   OutOfPlaceDataSet))
164 {
165     // Validate output
166     validate(CLAccessor(_target), _reference, tolerance_fp16, 0.01);
167 }
FIXTURE_DATA_TEST_CASE(RunWithActivation,CLElementwiseSquaredDiffFloatFixture<half>,framework::DatasetMode::ALL,combine (combine (combine (datasets::TinyShapes (),ElementwiseSquaredDiffFP16Dataset),ActivationFunctionsDataset),OutOfPlaceDataSet))168 FIXTURE_DATA_TEST_CASE(RunWithActivation, CLElementwiseSquaredDiffFloatFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::TinyShapes(), ElementwiseSquaredDiffFP16Dataset),
169                        ActivationFunctionsDataset),
170                        OutOfPlaceDataSet))
171 {
172     // Validate output
173     validate(CLAccessor(_target), _reference, tolerance_fp16, 0.01);
174 }
175 TEST_SUITE_END()
176 
TEST_SUITE(FP32)177 TEST_SUITE(FP32)
178 FIXTURE_DATA_TEST_CASE(RunSmall, CLElementwiseSquaredDiffFloatFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), ElementwiseSquaredDiffFP32Dataset),
179                                                                                                                    EmptyActivationFunctionsDataset),
180                                                                                                                    OutOfPlaceDataSet))
181 {
182     // Validate output
183     validate(CLAccessor(_target), _reference, tolerance_fp32);
184 }
FIXTURE_DATA_TEST_CASE(RunWithActivation,CLElementwiseSquaredDiffFloatFixture<float>,framework::DatasetMode::ALL,combine (combine (combine (datasets::TinyShapes (),ElementwiseSquaredDiffFP32Dataset),ActivationFunctionsDataset),OutOfPlaceDataSet))185 FIXTURE_DATA_TEST_CASE(RunWithActivation, CLElementwiseSquaredDiffFloatFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::TinyShapes(), ElementwiseSquaredDiffFP32Dataset),
186                        ActivationFunctionsDataset),
187                        OutOfPlaceDataSet))
188 {
189     // Validate output
190     validate(CLAccessor(_target), _reference, tolerance_fp32);
191 }
192 template <typename T>
193 using CLElementwiseSquaredDiffBroadcastFloatFixture = ElementwiseSquaredDiffBroadcastValidationFloatFixture<CLTensor, CLAccessor, CLElementwiseSquaredDiff, T>;
194 
FIXTURE_DATA_TEST_CASE(RunSmallBroadcast,CLElementwiseSquaredDiffBroadcastFloatFixture<float>,framework::DatasetMode::ALL,combine (combine (combine (datasets::SmallShapesBroadcast (),ElementwiseSquaredDiffFP32Dataset),EmptyActivationFunctionsDataset),OutOfPlaceDataSet))195 FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLElementwiseSquaredDiffBroadcastFloatFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapesBroadcast(),
196                        ElementwiseSquaredDiffFP32Dataset),
197                        EmptyActivationFunctionsDataset),
198                        OutOfPlaceDataSet))
199 {
200     // Validate output
201     validate(CLAccessor(_target), _reference, tolerance_fp32);
202 }
FIXTURE_DATA_TEST_CASE(RunWithActivationBroadcast,CLElementwiseSquaredDiffBroadcastFloatFixture<float>,framework::DatasetMode::ALL,combine (combine (combine (datasets::TinyShapesBroadcast (),ElementwiseSquaredDiffFP32Dataset),ActivationFunctionsDataset),OutOfPlaceDataSet))203 FIXTURE_DATA_TEST_CASE(RunWithActivationBroadcast, CLElementwiseSquaredDiffBroadcastFloatFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::TinyShapesBroadcast(),
204                        ElementwiseSquaredDiffFP32Dataset),
205                        ActivationFunctionsDataset),
206                        OutOfPlaceDataSet))
207 {
208     // Validate output
209     validate(CLAccessor(_target), _reference, tolerance_fp32);
210 }
211 TEST_SUITE_END()
212 TEST_SUITE_END()
213 
214 TEST_SUITE_END() // ElementwiseSquaredDiff
215 TEST_SUITE_END() // CL
216 } // namespace validation
217 } // namespace test
218 } // namespace arm_compute
219