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
25 #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
26 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuAdd.h"
27
28 #include "tests/CL/CLAccessor.h"
29 #include "tests/framework/Fixture.h"
30 #include "tests/framework/Macros.h"
31 #include "tests/framework/datasets/Datasets.h"
32 #include "tests/validation/Validation.h"
33
34 #include "tests/datasets/DynamicFusionDataset.h"
35 #include "tests/datasets/ShapeDatasets.h"
36 #include "tests/validation/fixtures/dynamic_fusion/gpu/cl/ElementwiseBinaryFixture.h"
37
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 /* Synced with tests/validation/CL/ArithmeticAddition.cpp from the standard interface.
45 *
46 * Difference | Why the difference
47 * No quantized tests | Not supported yet
48 * No in place tests | Not supported yet
49 * No activation tests | Not needed in dynamic fusion interface
50 *
51 */
52 TEST_SUITE(CL)
TEST_SUITE(DYNAMIC_FUSION)53 TEST_SUITE(DYNAMIC_FUSION)
54 TEST_SUITE(ADD)
55
56 // *INDENT-OFF*
57 // clang-format off
58 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
59 framework::dataset::make("LhsInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
60 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid data type combination
61 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16), // S16 is valid data type for Add
62 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S32), // S32 is valid data type for Add
63 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
64 TensorInfo(TensorShape(32U, 1U, 1U), 1, DataType::F32), // Broadcasting allowed for lhs
65 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), // Unsupported data type QASYMM8
66 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8_SIGNED), // Unsupported data type QASYMM8
67 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
68 TensorInfo(TensorShape(15U, 23U, 3U), 1, DataType::F32), // Broadcast Y dimension is not allowed
69 TensorInfo(TensorShape( 3U, 8U, 9U), 1, DataType::S16), // Broadcast Z dimension is not allowed
70 TensorInfo(TensorShape(32U, 13U, 2U, 2), 1, DataType::F32), // Batching is allowed
71 }),
72 framework::dataset::make("RhsInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
73 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
74 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
75 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S32),
76 TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
77 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
78 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), // Unsupported data type QASYMM8
79 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8_SIGNED), // Unsupported data type QASYMM8
80 TensorInfo(TensorShape(32U, 1U, 1U), 1, DataType::F32), // Broadcasting allowed for rhs
81 TensorInfo(TensorShape(15U, 1U, 3U), 1, DataType::F32),
82 TensorInfo(TensorShape( 3U, 8U, 1U), 1, DataType::S16),
83 TensorInfo(TensorShape(32U, 13U, 2U, 2), 1, DataType::F32),
84 })),
85 framework::dataset::make("Expected", { true, false, true, true, false, true, false, false, true, false, false, true})),
86 input1_info, input2_info, expected)
87 {
88 // Create a new workload sketch
89 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
90 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
91 GpuWorkloadSketch sketch{ &gpu_ctx };
92
93 // Validate Elementwise Add
94 auto lhs_info = sketch.create_tensor_info(input1_info);
95 auto rhs_info = sketch.create_tensor_info(input2_info);
96
97 bool res = bool(GpuAdd::validate_op(sketch, &lhs_info, &rhs_info));
98 ARM_COMPUTE_EXPECT(res == expected, framework::LogLevel::ERRORS);
99 }
100 // clang-format on
101 // *INDENT-ON*
102
103 constexpr AbsoluteTolerance<float> tolerance_f16(0.0001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
104 constexpr AbsoluteTolerance<float> tolerance_f32(0.0001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
105 constexpr float tolerance_num = 0.0001f; /**< Tolerance number */
106
107 template <typename T>
108 using DynamicFusionCLAddFixture = DynamicFusionGpuElementwiseBinaryOneOpValidationFixture<CLTensor, CLAccessor, GpuAdd, T>;
109
110 template <typename T>
111 using DynamicFusionCLAddBroadcastFixture = DynamicFusionGpuElementwiseBinaryBroadcastOneOpValidationFixture<CLTensor, CLAccessor, GpuAdd, T>;
112
113 template <typename T>
114 using DynamicFusionCLAddTwoOpsFixture = DynamicFusionGpuElementwiseBinaryTwoOpsValidationFixture<CLTensor, CLAccessor, GpuAdd, T>;
115
116 TEST_SUITE(FP32)
117 FIXTURE_DATA_TEST_CASE(RunSmallOneOp,
118 DynamicFusionCLAddFixture<float>,
119 framework::DatasetMode::PRECOMMIT,
120 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
121 datasets::SmallShapes()),
122 framework::dataset::make("DataType", { DataType::F32 })),
123 framework::dataset::make("InPlace", { false })))
124 {
125 // Validate output
126 validate(CLAccessor(_target), _reference, tolerance_f32);
127 }
128 FIXTURE_DATA_TEST_CASE(RunLargeOneOp,
129 DynamicFusionCLAddFixture<float>,
130 framework::DatasetMode::NIGHTLY,
131 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
132 datasets::LargeShapes()),
133 framework::dataset::make("DataType", { DataType::F32 })),
134 framework::dataset::make("InPlace", { false })))
135 {
136 // Validate output
137 validate(CLAccessor(_target), _reference, tolerance_f32);
138 }
139 FIXTURE_DATA_TEST_CASE(RunSmallBroadcastOneOp,
140 DynamicFusionCLAddBroadcastFixture<float>,
141 framework::DatasetMode::PRECOMMIT,
142 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
143 datasets::TemporaryLimitedSmallShapesBroadcast()),
144 framework::dataset::make("DataType", { DataType::F32 })),
145 framework::dataset::make("InPlace", { false })))
146 {
147 // Validate output
148 validate(CLAccessor(_target), _reference, tolerance_f32);
149 }
150
151 FIXTURE_DATA_TEST_CASE(RunLargeBroadcastOneOp,
152 DynamicFusionCLAddBroadcastFixture<float>,
153 framework::DatasetMode::NIGHTLY,
154 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
155 datasets::TemporaryLimitedLargeShapesBroadcast()),
156 framework::dataset::make("DataType", { DataType::F32 })),
157 framework::dataset::make("InPlace", { false })))
158 {
159 // Validate output
160 validate(CLAccessor(_target), _reference, tolerance_f32);
161 }
162 FIXTURE_DATA_TEST_CASE(RunSmallTwoOps,
163 DynamicFusionCLAddTwoOpsFixture<float>,
164 framework::DatasetMode::PRECOMMIT,
165 combine(combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
166 datasets::DynamicFusionElementwiseBinaryTwoOpsSmallShapes()),
167 framework::dataset::make("DataType", { DataType::F32 })),
168 framework::dataset::make("InPlace", { false })),
169 framework::dataset::make("FuseTwoOps", { true })))
170 {
171 // Validate output
172 validate(CLAccessor(_target), _reference, tolerance_f32);
173 }
174 TEST_SUITE_END() // FP32
175
TEST_SUITE(FP16)176 TEST_SUITE(FP16)
177 FIXTURE_DATA_TEST_CASE(RunSmallOneOp,
178 DynamicFusionCLAddFixture<half>,
179 framework::DatasetMode::ALL,
180 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
181 datasets::SmallShapes()),
182 framework::dataset::make("DataType", { DataType::F16 })),
183 framework::dataset::make("InPlace", { false })))
184 {
185 // Validate output
186 validate(CLAccessor(_target), _reference, tolerance_f32, tolerance_num);
187 }
188
189 FIXTURE_DATA_TEST_CASE(RunSmallBroadcastOneOp,
190 DynamicFusionCLAddBroadcastFixture<half>,
191 framework::DatasetMode::ALL,
192 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
193 datasets::TemporaryLimitedSmallShapesBroadcast()),
194 framework::dataset::make("DataType", { DataType::F16 })),
195 framework::dataset::make("InPlace", { false })))
196 {
197 // Validate output
198 validate(CLAccessor(_target), _reference, tolerance_f32, tolerance_num);
199 }
200
201 TEST_SUITE_END() // FP16
202
TEST_SUITE(S32)203 TEST_SUITE(S32)
204 FIXTURE_DATA_TEST_CASE(RunSmall,
205 DynamicFusionCLAddFixture<int32_t>,
206 framework::DatasetMode::PRECOMMIT,
207 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
208 datasets::SmallShapes()),
209 framework::dataset::make("DataType", { DataType::S32 })),
210 framework::dataset::make("InPlace", { false })))
211 {
212 // Validate output
213 validate(CLAccessor(_target), _reference);
214 }
215 TEST_SUITE_END() // S32
216
TEST_SUITE(S16)217 TEST_SUITE(S16)
218 FIXTURE_DATA_TEST_CASE(RunSmall,
219 DynamicFusionCLAddFixture<int16_t>,
220 framework::DatasetMode::PRECOMMIT,
221 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
222 datasets::SmallShapes()),
223 framework::dataset::make("DataType", { DataType::S16 })),
224 framework::dataset::make("InPlace", { false })))
225 {
226 // Validate output
227 validate(CLAccessor(_target), _reference);
228 }
229 FIXTURE_DATA_TEST_CASE(RunLarge,
230 DynamicFusionCLAddFixture<int16_t>,
231 framework::DatasetMode::NIGHTLY,
232 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
233 datasets::LargeShapes()),
234 framework::dataset::make("DataType", { DataType::S16 })),
235 framework::dataset::make("InPlace", { false })))
236 {
237 // Validate output
238 validate(CLAccessor(_target), _reference);
239 }
240 TEST_SUITE_END() // S16
241
TEST_SUITE(U8)242 TEST_SUITE(U8)
243 FIXTURE_DATA_TEST_CASE(RunSmall,
244 DynamicFusionCLAddFixture<uint8_t>,
245 framework::DatasetMode::PRECOMMIT,
246 combine(combine(combine(framework::dataset::make("ElementwiseOp", { ArithmeticOperation::ADD }),
247 datasets::SmallShapes()),
248 framework::dataset::make("DataType", { DataType::U8 })),
249 framework::dataset::make("InPlace", { false })))
250 {
251 // Validate output
252 validate(CLAccessor(_target), _reference);
253 }
254 TEST_SUITE_END() // U8
255
256 TEST_SUITE_END() // ADD
257 TEST_SUITE_END() // DYNAMIC_FUSION
258 TEST_SUITE_END() // CL
259 } // namespace validation
260 } // namespace test
261 } // namespace arm_compute
262