xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/CL/ReduceMean.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2020 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/CLReduceMean.h"
28 #include "tests/CL/CLAccessor.h"
29 #include "tests/datasets/ShapeDatasets.h"
30 #include "tests/datasets/SplitDataset.h"
31 #include "tests/framework/Asserts.h"
32 #include "tests/framework/Macros.h"
33 #include "tests/validation/Validation.h"
34 #include "tests/validation/fixtures/ReduceMeanFixture.h"
35 
36 namespace arm_compute
37 {
38 namespace test
39 {
40 namespace validation
41 {
42 namespace
43 {
44 constexpr AbsoluteTolerance<float>   tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for 32-bit floating-point type */
45 constexpr AbsoluteTolerance<float>   tolerance_f16(0.03f);  /**< Tolerance value for comparing reference's output against implementation's output for 16-bit floating-point type */
46 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);  /**< Tolerance value for comparing reference's output against implementation's output for 8-bit asymmetric quantized type */
47 
48 const auto axis_keep = combine(framework::dataset::make("Axis", { Coordinates(0), Coordinates(1, 0), Coordinates(1, 2), Coordinates(0, 2), Coordinates(1, 3), Coordinates(0, 1, 2, 3) }),
49                                framework::dataset::make("KeepDims", { true }));
50 const auto axis_drop = combine(framework::dataset::make("Axis", { Coordinates(0), Coordinates(1), Coordinates(3), Coordinates(1, 2), Coordinates(2, 1) }), framework::dataset::make("KeepDims", { false }));
51 } // namespace
52 TEST_SUITE(CL)
TEST_SUITE(ReduceMean)53 TEST_SUITE(ReduceMean)
54 
55 // *INDENT-OFF*
56 // clang-format off
57 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
58         framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 3U, 16U, 2U), 1, DataType::F32), // Invalid axis
59                                                 TensorInfo(TensorShape(27U, 3U, 16U, 2U), 1, DataType::F32), // Invalid output shape
60                                                 TensorInfo(TensorShape(32U, 16U, 16U, 2U), 1, DataType::F32),// OK
61                                                 TensorInfo(TensorShape{228U, 19U, 2U, 2U}, 1, DataType::F32),// OK
62                                                 TensorInfo(TensorShape{228U, 19U, 2U, 1U}, 1, DataType::F32) // Cannot support axis 3 not valid
63         }),
64         framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(27U, 3U, 1U, 2U), 1, DataType::F32),
65                                                  TensorInfo(TensorShape(27U, 3U, 1U, 2U), 1, DataType::F32),
66                                                  TensorInfo(TensorShape(32U, 16U, 1U, 2U), 1, DataType::F32),
67                                                  TensorInfo(TensorShape(19U), 1, DataType::F32),
68                                                  TensorInfo(TensorShape(19U), 1, DataType::F32)
69 
70         })),
71         framework::dataset::make("Axis", { Coordinates(4), Coordinates(0,2), Coordinates(2), Coordinates(3,2,0), Coordinates(3,2,0) })),
72         framework::dataset::make("Keep", { true, true, true, false, false })),
73         framework::dataset::make("Expected", { false, false, true, true, false })),
74         input_info, output_info, axis, keep, expected)
75 {
76     const Status status = CLReduceMean::validate(&input_info.clone()->set_is_resizable(false), axis, keep, &output_info.clone()->set_is_resizable(false));
77     ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
78 }
79 // clang-format on
80 // *INDENT-ON*
81 
82 template <typename T>
83 using CLReduceMeanFixture = ReduceMeanFixture<CLTensor, CLAccessor, CLReduceMean, T>;
84 
85 TEST_SUITE(Float)
TEST_SUITE(FP16)86 TEST_SUITE(FP16)
87 FIXTURE_DATA_TEST_CASE(RunSmall,
88                        CLReduceMeanFixture<half>,
89                        framework::DatasetMode::PRECOMMIT,
90                        combine(combine(datasets::Small4DShapes(), framework::dataset::make("DataType", DataType::F16)), concat(axis_keep, axis_drop)))
91 {
92     // Validate output
93     validate(CLAccessor(_target), _reference, tolerance_f16);
94 }
95 
96 FIXTURE_DATA_TEST_CASE(RunLarge,
97                        CLReduceMeanFixture<half>,
98                        framework::DatasetMode::NIGHTLY,
99                        combine(combine(datasets::Large4DShapes(), framework::dataset::make("DataType", DataType::F16)), concat(axis_keep, axis_drop)))
100 {
101     // Validate output
102     validate(CLAccessor(_target), _reference, tolerance_f16);
103 }
104 TEST_SUITE_END() // FP16
105 
TEST_SUITE(FP32)106 TEST_SUITE(FP32)
107 FIXTURE_DATA_TEST_CASE(RunSmall,
108                        CLReduceMeanFixture<float>,
109                        framework::DatasetMode::PRECOMMIT,
110                        combine(combine(datasets::Small4DShapes(), framework::dataset::make("DataType", DataType::F32)), concat(axis_keep, axis_drop)))
111 {
112     // Validate output
113     validate(CLAccessor(_target), _reference, tolerance_f32);
114 }
115 
116 FIXTURE_DATA_TEST_CASE(RunLarge,
117                        CLReduceMeanFixture<float>,
118                        framework::DatasetMode::NIGHTLY,
119                        combine(combine(datasets::Large4DShapes(), framework::dataset::make("DataType", DataType::F32)), concat(axis_keep, axis_drop)))
120 {
121     // Validate output
122     validate(CLAccessor(_target), _reference, tolerance_f32);
123 }
124 TEST_SUITE_END() // FP32
125 TEST_SUITE_END() // Float
126 
127 template <typename T>
128 using CLReduceMeanQuantizedFixture = ReduceMeanQuantizedFixture<CLTensor, CLAccessor, CLReduceMean, T>;
129 
130 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)131 TEST_SUITE(QASYMM8)
132 FIXTURE_DATA_TEST_CASE(RunSmall,
133                        CLReduceMeanQuantizedFixture<uint8_t>,
134                        framework::DatasetMode::PRECOMMIT,
135                        combine(combine(combine(combine(datasets::Small4DShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), concat(axis_keep, axis_drop)),
136                                        framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.f / 255, 5) })),
137                                framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.f / 255, 5) })))
138 {
139     // Validate output
140     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
141 }
142 
143 TEST_SUITE(Requant)
144 FIXTURE_DATA_TEST_CASE(RunSmall,
145                        CLReduceMeanQuantizedFixture<uint8_t>,
146                        framework::DatasetMode::PRECOMMIT,
147                        combine(combine(combine(combine(datasets::Small4DShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), axis_drop),
148                                        framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.f / 255, 5) })),
149                                framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.f / 200, 16) })))
150 {
151     // Validate output
152     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
153 }
154 TEST_SUITE_END() // Requant
155 
156 FIXTURE_DATA_TEST_CASE(RunLarge,
157                        CLReduceMeanQuantizedFixture<uint8_t>,
158                        framework::DatasetMode::NIGHTLY,
159                        combine(combine(combine(combine(datasets::Large4DShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), concat(axis_keep, axis_drop)),
160                                        framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.f / 255, 5) })),
161                                framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.f / 255, 5) })))
162 {
163     // Validate output
164     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
165 }
166 TEST_SUITE_END() // QASYMM8
167 
TEST_SUITE(QASYMM8_SIGNED)168 TEST_SUITE(QASYMM8_SIGNED)
169 FIXTURE_DATA_TEST_CASE(RunSmall,
170                        CLReduceMeanQuantizedFixture<int8_t>,
171                        framework::DatasetMode::PRECOMMIT,
172                        combine(combine(combine(combine(datasets::Small4DShapes(), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), concat(axis_keep, axis_drop)),
173                                        framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.f / 102, 2) })),
174                                framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.f / 102, 2) })))
175 {
176     // Validate output
177     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
178 }
179 
180 TEST_SUITE(Requant)
181 FIXTURE_DATA_TEST_CASE(RunSmall,
182                        CLReduceMeanQuantizedFixture<int8_t>,
183                        framework::DatasetMode::PRECOMMIT,
184                        combine(combine(combine(combine(datasets::Small4DShapes(), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), axis_drop),
185                                        framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.f / 102, 2) })),
186                                framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.f / 113, 10) })))
187 {
188     // Validate output
189     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
190 }
191 TEST_SUITE_END() // Requant
192 
193 FIXTURE_DATA_TEST_CASE(RunLarge,
194                        CLReduceMeanQuantizedFixture<int8_t>,
195                        framework::DatasetMode::NIGHTLY,
196                        combine(combine(combine(combine(datasets::Large4DShapes(), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), concat(axis_keep, axis_drop)),
197                                        framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.f / 102, 2) })),
198                                framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.f / 102, 2) })))
199 {
200     // Validate output
201     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
202 }
203 TEST_SUITE_END() // QASYMM8_SIGNED
204 TEST_SUITE_END() // Quantized
205 TEST_SUITE_END() // ReduceMean
206 TEST_SUITE_END() // CL
207 } // namespace validation
208 } // namespace test
209 } // namespace arm_compute
210