xref: /aosp_15_r20/external/armnn/delegate/test/ElementwiseUnaryTest.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ElementwiseUnaryTestHelper.hpp"
7 
8 #include <armnn_delegate.hpp>
9 
10 #include <flatbuffers/flatbuffers.h>
11 #include <tensorflow/lite/interpreter.h>
12 #include <tensorflow/lite/kernels/register.h>
13 #include <tensorflow/lite/model.h>
14 #include <schema_generated.h>
15 #include <tensorflow/lite/version.h>
16 
17 #include <doctest/doctest.h>
18 
19 namespace armnnDelegate
20 {
21 
22 TEST_SUITE("ElementwiseUnary_GpuAccTests")
23 {
24 
25 TEST_CASE ("Abs_Float32_GpuAcc_Test")
26 {
27     // Create the ArmNN Delegate
28     std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
29     // Set input data
30     std::vector<float> inputValues
31     {
32         -0.1f, -0.2f, -0.3f,
33         0.1f,  0.2f,  0.3f
34     };
35     // Calculate output data
36     std::vector<float> expectedOutputValues(inputValues.size());
37     for (unsigned int i = 0; i < inputValues.size(); ++i)
38     {
39         expectedOutputValues[i] = std::abs(inputValues[i]);
40     }
41     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues);
42 }
43 
44 TEST_CASE ("Exp_Float32_GpuAcc_Test")
45 {
46     // Create the ArmNN Delegate
47     std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
48     // Set input data
49     std::vector<float> inputValues
50     {
51         5.0f, 4.0f,
52         3.0f, 2.0f,
53         1.0f, 1.1f
54     };
55     // Set output data
56     std::vector<float> expectedOutputValues
57     {
58         148.413159102577f, 54.598150033144f,
59         20.085536923188f,  7.389056098931f,
60         2.718281828459f,  3.004166023946f
61     };
62 
63     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues);
64 }
65 
66 TEST_CASE ("Log_Float32_GpuAcc_Test")
67 {
68     // Create the ArmNN Delegate
69     std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
70     // Set input data
71     std::vector<float> inputValues
72     {
73         1.0f, 1.0f,  2.0f,
74         3.0f,  4.0f, 2.71828f
75     };
76     // Set output data
77     std::vector<float> expectedOutputValues
78     {
79         0.f,  0.f,  0.69314718056f,
80         1.09861228867f, 1.38629436112f, 0.99999932734f
81     };
82 
83     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_LOG, backends, inputValues, expectedOutputValues);
84 }
85 
86 TEST_CASE ("Neg_Float32_GpuAcc_Test")
87 {
88     // Create the ArmNN Delegate
89     std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
90     // Set input data
91     std::vector<float> inputValues
92     {
93         1.f, 0.f, 3.f,
94         25.f, 64.f, 100.f
95     };
96     // Set output data
97     std::vector<float> expectedOutputValues
98     {
99         -1.f, 0.f, -3.f,
100         -25.f, -64.f, -100.f
101     };
102 
103     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues);
104 }
105 
106 TEST_CASE ("Rsqrt_Float32_GpuAcc_Test")
107 {
108     // Create the ArmNN Delegate
109     std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
110     // Set input data
111     std::vector<float> inputValues
112     {
113         1.f, 4.f, 16.f,
114         25.f, 64.f, 100.f
115     };
116     // Set output data
117     std::vector<float> expectedOutputValues
118     {
119         1.f, 0.5f, 0.25f,
120         0.2f, 0.125f, 0.1f
121     };
122 
123     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues);
124 }
125 
126 TEST_CASE ("Sin_Float32_GpuAcc_Test")
127 {
128     // Create the ArmNN Delegate
129     std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
130     // Set input data
131     std::vector<float> inputValues
132     {
133             0.0f, 1.0f, 16.0f,
134             0.5f, 36.0f, -1.f
135     };
136     // Set output data
137     std::vector<float> expectedOutputValues
138     {
139             0.0f, 0.8414709848f, -0.28790331666f,
140             0.4794255386f, -0.99177885344f, -0.8414709848f
141     };
142 
143     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SIN, backends, inputValues, expectedOutputValues);
144 }
145 } // TEST_SUITE("ElementwiseUnary_GpuAccTests")
146 
147 
148 
149 TEST_SUITE("ElementwiseUnary_CpuAccTests")
150 {
151 
152 TEST_CASE ("Abs_Float32_CpuAcc_Test")
153 {
154     // Create the ArmNN Delegate
155     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
156     // Set input data
157     std::vector<float> inputValues
158     {
159         -0.1f, -0.2f, -0.3f,
160         0.1f,  0.2f,  0.3f
161     };
162     // Calculate output data
163     std::vector<float> expectedOutputValues(inputValues.size());
164     for (unsigned int i = 0; i < inputValues.size(); ++i)
165     {
166         expectedOutputValues[i] = std::abs(inputValues[i]);
167     }
168 
169     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues);
170 }
171 
172 TEST_CASE ("Exp_Float32_CpuAcc_Test")
173 {
174     // Create the ArmNN Delegate
175     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
176     // Set input data
177     std::vector<float> inputValues
178     {
179         5.0f, 4.0f,
180         3.0f, 2.0f,
181         1.0f, 1.1f
182     };
183     // Set output data
184     std::vector<float> expectedOutputValues
185     {
186         148.413159102577f, 54.598150033144f,
187         20.085536923188f,  7.389056098931f,
188         2.718281828459f,  3.004166023946f
189     };
190 
191     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues);
192 }
193 
194 TEST_CASE ("Log_Float32_CpuAcc_Test")
195 {
196     // Create the ArmNN Delegate
197     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
198     // Set input data
199     std::vector<float> inputValues
200     {
201         1.0f, 1.0f,  2.0f,
202         3.0f,  4.0f, 2.71828f
203     };
204     // Set output data
205     std::vector<float> expectedOutputValues
206     {
207         0.f,  0.f,  0.69314718056f,
208         1.09861228867f, 1.38629436112f, 0.99999932734f
209     };
210 
211     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_LOG, backends, inputValues, expectedOutputValues);
212 }
213 
214 TEST_CASE ("Neg_Float32_CpuAcc_Test")
215 {
216     // Create the ArmNN Delegate
217     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
218     // Set input data
219     std::vector<float> inputValues
220     {
221         1.f, 0.f, 3.f,
222         25.f, 64.f, 100.f
223     };
224     // Set output data
225     std::vector<float> expectedOutputValues
226     {
227         -1.f, 0.f, -3.f,
228         -25.f, -64.f, -100.f
229     };
230 
231     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues);
232 }
233 
234 TEST_CASE ("Rsqrt_Float32_CpuAcc_Test")
235 {
236     // Create the ArmNN Delegate
237     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
238     // Set input data
239     std::vector<float> inputValues
240     {
241         1.f, 4.f, 16.f,
242         25.f, 64.f, 100.f
243     };
244     // Set output data
245     std::vector<float> expectedOutputValues
246     {
247         1.f, 0.5f, 0.25f,
248         0.2f, 0.125f, 0.1f
249     };
250 
251     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues);
252 }
253 
254 TEST_CASE ("Sin_Float32_CpuAcc_Test")
255 {
256     // Create the ArmNN Delegate
257     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
258     // Set input data
259     std::vector<float> inputValues
260     {
261         0.0f, 1.0f, 16.0f,
262         0.5f, 36.0f, -1.f
263     };
264     // Set output data
265     std::vector<float> expectedOutputValues
266     {
267         0.0f, 0.8414709848f, -0.28790331666f,
268         0.4794255386f, -0.99177885344f, -0.8414709848f
269     };
270 
271     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SIN, backends, inputValues, expectedOutputValues);
272 }
273 } // TEST_SUITE("ElementwiseUnary_CpuAccTests")
274 
275 TEST_SUITE("ElementwiseUnary_CpuRefTests")
276 {
277 
278 TEST_CASE ("Abs_Float32_CpuRef_Test")
279 {
280     // Create the ArmNN Delegate
281     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
282     // Set input data
283     std::vector<float> inputValues
284     {
285         -0.1f, -0.2f, -0.3f,
286         0.1f,  0.2f,  0.3f
287     };
288     // Calculate output data
289     std::vector<float> expectedOutputValues(inputValues.size());
290     for (unsigned int i = 0; i < inputValues.size(); ++i)
291     {
292         expectedOutputValues[i] = std::abs(inputValues[i]);
293     }
294 
295     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues);
296 }
297 
298 TEST_CASE ("Ceil_Float32_CpuRef_Test")
299 {
300     // Create the ArmNN Delegate
301     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
302     // Set input data
303     std::vector<float> inputValues
304     {
305         0.0f, 1.1f, -16.1f,
306         0.5f, -0.5f, -1.3f
307     };
308     // Set output data
309     std::vector<float> expectedOutputValues
310     {
311         0.0f, 2.0f, -16.0f,
312         1.0f, 0.0f, -1.0f
313     };
314 
315     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_CEIL, backends, inputValues, expectedOutputValues);
316 }
317 
318 TEST_CASE ("Exp_Float32_CpuRef_Test")
319 {
320     // Create the ArmNN Delegate
321     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
322     // Set input data
323     std::vector<float> inputValues
324     {
325         5.0f, 4.0f,
326         3.0f, 2.0f,
327         1.0f, 1.1f
328     };
329     // Set output data
330     std::vector<float> expectedOutputValues
331     {
332         148.413159102577f, 54.598150033144f,
333         20.085536923188f,  7.389056098931f,
334         2.718281828459f,  3.004166023946f
335     };
336 
337     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues);
338 }
339 
340 TEST_CASE ("Log_Float32_CpuRef_Test")
341 {
342     // Create the ArmNN Delegate
343     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
344     // Set input data
345     std::vector<float> inputValues
346     {
347         1.0f, 1.0f,  2.0f,
348         3.0f,  4.0f, 2.71828f
349     };
350     // Set output data
351     std::vector<float> expectedOutputValues
352     {
353         0.f,  0.f,  0.69314718056f,
354         1.09861228867f, 1.38629436112f, 0.99999932734f
355     };
356 
357     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_LOG, backends, inputValues, expectedOutputValues);
358 }
359 
360 TEST_CASE ("Neg_Float32_CpuRef_Test")
361 {
362     // Create the ArmNN Delegate
363     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
364     // Set input data
365     std::vector<float> inputValues
366     {
367         1.f, 0.f, 3.f,
368         25.f, 64.f, 100.f
369     };
370     // Set output data
371     std::vector<float> expectedOutputValues
372     {
373         -1.f, 0.f, -3.f,
374         -25.f, -64.f, -100.f
375     };
376 
377     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues);
378 }
379 
380 TEST_CASE ("Rsqrt_Float32_CpuRef_Test")
381 {
382     // Create the ArmNN Delegate
383     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
384     // Set input data
385     std::vector<float> inputValues
386     {
387         1.f, 4.f, 16.f,
388         25.f, 64.f, 100.f
389     };
390     // Set output data
391     std::vector<float> expectedOutputValues
392     {
393         1.f, 0.5f, 0.25f,
394         0.2f, 0.125f, 0.1f
395     };
396 
397     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues);
398 }
399 
400 TEST_CASE ("Sqrt_Float32_CpuRef_Test")
401 {
402     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
403     // Set input data
404     std::vector<float> inputValues
405     {
406         9.0f, 4.25f, 81.9f,
407         0.1f,  0.9f,  169.0f
408     };
409     // Calculate output data
410     std::vector<float> expectedOutputValues(inputValues.size());
411     for (unsigned int i = 0; i < inputValues.size(); ++i)
412     {
413         expectedOutputValues[i] = std::sqrt(inputValues[i]);
414     }
415 
416     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SQRT, backends, inputValues, expectedOutputValues);
417 }
418 
419 TEST_CASE ("Sin_Float32_CpuRef_Test")
420 {
421     // Create the ArmNN Delegate
422     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
423     // Set input data
424     std::vector<float> inputValues
425     {
426             0.0f, 1.0f, 16.0f,
427             0.5f, 36.0f, -1.f
428     };
429     // Set output data
430     std::vector<float> expectedOutputValues
431     {
432             0.0f, 0.8414709848f, -0.28790331666f,
433             0.4794255386f, -0.99177885344f, -0.8414709848f
434     };
435 
436     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SIN, backends, inputValues, expectedOutputValues);
437 }
438 } // TEST_SUITE("ElementwiseUnary_CpuRefTests")
439 
440 } // namespace armnnDelegate