• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2020 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 <tensorflow/lite/schema/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 ("Neg_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.f, 0.f, 3.f,
74         25.f, 64.f, 100.f
75     };
76     // Set output data
77     std::vector<float> expectedOutputValues
78     {
79         -1.f, 0.f, -3.f,
80         -25.f, -64.f, -100.f
81     };
82 
83     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues);
84 }
85 
86 TEST_CASE ("Rsqrt_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, 4.f, 16.f,
94         25.f, 64.f, 100.f
95     };
96     // Set output data
97     std::vector<float> expectedOutputValues
98     {
99         1.f, 0.5f, 0.25f,
100         0.2f, 0.125f, 0.1f
101     };
102 
103     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues);
104 }
105 
106 } // TEST_SUITE("ElementwiseUnary_GpuAccTests")
107 
108 
109 
110 TEST_SUITE("ElementwiseUnary_CpuAccTests")
111 {
112 
113 TEST_CASE ("Abs_Float32_CpuAcc_Test")
114 {
115     // Create the ArmNN Delegate
116     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
117     // Set input data
118     std::vector<float> inputValues
119     {
120         -0.1f, -0.2f, -0.3f,
121         0.1f,  0.2f,  0.3f
122     };
123     // Calculate output data
124     std::vector<float> expectedOutputValues(inputValues.size());
125     for (unsigned int i = 0; i < inputValues.size(); ++i)
126     {
127         expectedOutputValues[i] = std::abs(inputValues[i]);
128     }
129 
130     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues);
131 }
132 
133 TEST_CASE ("Exp_Float32_CpuAcc_Test")
134 {
135     // Create the ArmNN Delegate
136     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
137     // Set input data
138     std::vector<float> inputValues
139     {
140         5.0f, 4.0f,
141         3.0f, 2.0f,
142         1.0f, 1.1f
143     };
144     // Set output data
145     std::vector<float> expectedOutputValues
146     {
147         148.413159102577f, 54.598150033144f,
148         20.085536923188f,  7.389056098931f,
149         2.718281828459f,  3.004166023946f
150     };
151 
152     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues);
153 }
154 
155 TEST_CASE ("Neg_Float32_CpuAcc_Test")
156 {
157     // Create the ArmNN Delegate
158     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
159     // Set input data
160     std::vector<float> inputValues
161     {
162         1.f, 0.f, 3.f,
163         25.f, 64.f, 100.f
164     };
165     // Set output data
166     std::vector<float> expectedOutputValues
167     {
168         -1.f, 0.f, -3.f,
169         -25.f, -64.f, -100.f
170     };
171 
172     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues);
173 }
174 
175 TEST_CASE ("Rsqrt_Float32_CpuAcc_Test")
176 {
177     // Create the ArmNN Delegate
178     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc };
179     // Set input data
180     std::vector<float> inputValues
181     {
182         1.f, 4.f, 16.f,
183         25.f, 64.f, 100.f
184     };
185     // Set output data
186     std::vector<float> expectedOutputValues
187     {
188         1.f, 0.5f, 0.25f,
189         0.2f, 0.125f, 0.1f
190     };
191 
192     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues);
193 }
194 
195 } // TEST_SUITE("ElementwiseUnary_CpuAccTests")
196 
197 TEST_SUITE("ElementwiseUnary_CpuRefTests")
198 {
199 
200 TEST_CASE ("Abs_Float32_CpuRef_Test")
201 {
202     // Create the ArmNN Delegate
203     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
204     // Set input data
205     std::vector<float> inputValues
206     {
207         -0.1f, -0.2f, -0.3f,
208         0.1f,  0.2f,  0.3f
209     };
210     // Calculate output data
211     std::vector<float> expectedOutputValues(inputValues.size());
212     for (unsigned int i = 0; i < inputValues.size(); ++i)
213     {
214         expectedOutputValues[i] = std::abs(inputValues[i]);
215     }
216 
217     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues);
218 }
219 
220 TEST_CASE ("Exp_Float32_CpuRef_Test")
221 {
222     // Create the ArmNN Delegate
223     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
224     // Set input data
225     std::vector<float> inputValues
226     {
227         5.0f, 4.0f,
228         3.0f, 2.0f,
229         1.0f, 1.1f
230     };
231     // Set output data
232     std::vector<float> expectedOutputValues
233     {
234         148.413159102577f, 54.598150033144f,
235         20.085536923188f,  7.389056098931f,
236         2.718281828459f,  3.004166023946f
237     };
238 
239     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues);
240 }
241 
242 TEST_CASE ("Neg_Float32_CpuRef_Test")
243 {
244     // Create the ArmNN Delegate
245     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
246     // Set input data
247     std::vector<float> inputValues
248     {
249         1.f, 0.f, 3.f,
250         25.f, 64.f, 100.f
251     };
252     // Set output data
253     std::vector<float> expectedOutputValues
254     {
255         -1.f, 0.f, -3.f,
256         -25.f, -64.f, -100.f
257     };
258 
259     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues);
260 }
261 
262 TEST_CASE ("Rsqrt_Float32_CpuRef_Test")
263 {
264     // Create the ArmNN Delegate
265     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
266     // Set input data
267     std::vector<float> inputValues
268     {
269         1.f, 4.f, 16.f,
270         25.f, 64.f, 100.f
271     };
272     // Set output data
273     std::vector<float> expectedOutputValues
274     {
275         1.f, 0.5f, 0.25f,
276         0.2f, 0.125f, 0.1f
277     };
278 
279     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues);
280 }
281 
282 TEST_CASE ("Sqrt_Float32_CpuRef_Test")
283 {
284     std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
285     // Set input data
286     std::vector<float> inputValues
287     {
288         9.0f, 4.25f, 81.9f,
289         0.1f,  0.9f,  169.0f
290     };
291     // Calculate output data
292     std::vector<float> expectedOutputValues(inputValues.size());
293     for (unsigned int i = 0; i < inputValues.size(); ++i)
294     {
295         expectedOutputValues[i] = std::sqrt(inputValues[i]);
296     }
297 
298     ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SQRT, backends, inputValues, expectedOutputValues);
299 }
300 
301 } // TEST_SUITE("ElementwiseUnary_CpuRefTests")
302 
303 } // namespace armnnDelegate