xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/dynamic_fusion/gpu/cl/Clamp.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2022-2023 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/dynamic_fusion/sketch/attributes/ClampAttributes.h"
26 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuClamp.h"
27 #include "arm_compute/runtime/CL/CLTensor.h"
28 
29 #include "tests/CL/CLAccessor.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/dynamic_fusion/operators/ClampFixture.h"
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace validation
42 {
43 namespace
44 {
45 constexpr float                    epsilon = 1e-6f;
46 constexpr AbsoluteTolerance<float> tolerance(epsilon);
47 } // namespace
48 
49 TEST_SUITE(CL)
TEST_SUITE(DYNAMIC_FUSION)50 TEST_SUITE(DYNAMIC_FUSION)
51 TEST_SUITE(CLAMP)
52 // *INDENT-OFF*
53 // clang-format off
54 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
55                 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
56                                                         TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
57                                                         TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),    // Minimum value larger than maximum value
58                                                     }),
59                 framework::dataset::make("MinVal", { 0.2f,
60                                                      1.5f,
61                                                      9.0f,
62                                                     })),
63                 framework::dataset::make("MaxVal", { 0.5f,
64                                                      2.0f,
65                                                      1.0f,
66                                                     })),
67                 framework::dataset::make("Expected", { true, true, false })),
68                 input_info, min_val, max_val, expected)
69 {
70     // Create a new workload sketch
71     CLCompileContext cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
72     GpuWorkloadContext gpu_ctx{ &cl_compile_ctx };
73     GpuWorkloadSketch sketch{ &gpu_ctx };
74 
75     // Fuse Clamp
76     const TensorInfo src_info = sketch.create_tensor_info(input_info);
77 
78     ClampAttributes attributes {};
79     attributes.min_val(min_val)
80               .max_val(max_val);
81 
82     const bool res = static_cast<bool>(GpuClamp::validate_op(sketch, &src_info, attributes));
83     ARM_COMPUTE_EXPECT(res == expected, framework::LogLevel::ERRORS);
84 }
85 // clang-format on
86 // *INDENT-ON*
87 
88 template <typename T>
89 using DynamicFusionClampOpFixture = DynamicFusionClampValidationFixture<CLTensor, CLAccessor, GpuClamp, T>;
90 
91 TEST_SUITE(Float)
TEST_SUITE(FP16)92 TEST_SUITE(FP16)
93 FIXTURE_DATA_TEST_CASE(RunSmallOneOp,
94                        DynamicFusionClampOpFixture<half>,
95                        framework::DatasetMode::ALL,
96                        combine(combine(combine(datasets::SmallShapes(),
97                                                framework::dataset::make("ClampAttributes", { ClampAttributes().min_val(0.1f).max_val(0.6f) })),
98                                        framework::dataset::make("Fuse", { false })),
99                                framework::dataset::make("DataType", DataType::F16)))
100 {
101     // Validate output
102     validate(CLAccessor(_target), _reference, tolerance);
103 }
104 
105 FIXTURE_DATA_TEST_CASE(RunSmall5dOneOp,
106                        DynamicFusionClampOpFixture<half>,
107                        framework::DatasetMode::ALL,
108                        combine(combine(combine(datasets::Small5dShapes(),
109                                                framework::dataset::make("ClampAttributes", { ClampAttributes().min_val(0.1f).max_val(0.6f) })),
110                                        framework::dataset::make("Fuse", { false })),
111                                framework::dataset::make("DataType", DataType::F16)))
112 {
113     // Validate output
114     validate(CLAccessor(_target), _reference, tolerance);
115 }
116 
117 FIXTURE_DATA_TEST_CASE(RunSmallTwoOps,
118                        DynamicFusionClampOpFixture<half>,
119                        framework::DatasetMode::ALL,
120                        combine(combine(combine(datasets::SmallShapes(),
121                                                framework::dataset::make("ClampAttributes", { ClampAttributes().min_val(0.2f).max_val(0.4f) })),
122                                        framework::dataset::make("Fuse", { true })),
123                                framework::dataset::make("DataType", DataType::F16)))
124 {
125     // Validate output
126     validate(CLAccessor(_target), _reference, tolerance);
127 }
128 
129 TEST_SUITE_END() // FP16
130 
TEST_SUITE(FP32)131 TEST_SUITE(FP32)
132 FIXTURE_DATA_TEST_CASE(RunSmallOneOp,
133                        DynamicFusionClampOpFixture<float>,
134                        framework::DatasetMode::ALL,
135                        combine(combine(combine(datasets::SmallShapes(),
136                                                framework::dataset::make("ClampAttributes", { ClampAttributes().min_val(0.3f).max_val(0.7f) })),
137                                        framework::dataset::make("Fuse", { false })),
138                                framework::dataset::make("DataType", DataType::F32)))
139 {
140     // Validate output
141     validate(CLAccessor(_target), _reference, tolerance);
142 }
143 
144 FIXTURE_DATA_TEST_CASE(RunSmall5dOneOp,
145                        DynamicFusionClampOpFixture<float>,
146                        framework::DatasetMode::ALL,
147                        combine(combine(combine(datasets::Small5dShapes(),
148                                                framework::dataset::make("ClampAttributes", { ClampAttributes().min_val(0.3f).max_val(0.7f) })),
149                                        framework::dataset::make("Fuse", { false })),
150                                framework::dataset::make("DataType", DataType::F32)))
151 {
152     // Validate output
153     validate(CLAccessor(_target), _reference, tolerance);
154 }
155 
156 FIXTURE_DATA_TEST_CASE(RunSmallTwoOps,
157                        DynamicFusionClampOpFixture<float>,
158                        framework::DatasetMode::ALL,
159                        combine(combine(combine(datasets::SmallShapes(),
160                                                framework::dataset::make("ClampAttributes", { ClampAttributes().min_val(0.1f).max_val(0.9f) })),
161                                        framework::dataset::make("Fuse", { true })),
162                                framework::dataset::make("DataType", DataType::F32)))
163 {
164     // Validate output
165     validate(CLAccessor(_target), _reference, tolerance);
166 }
167 
168 TEST_SUITE_END() // FP32
169 TEST_SUITE_END() // Float
170 
171 TEST_SUITE_END() // CLAMP
172 TEST_SUITE_END() // DYNAMIC_FUSION
173 TEST_SUITE_END() // CL
174 } // namespace validation
175 } // namespace test
176 } // namespace arm_compute
177