xref: /aosp_15_r20/external/armnn/src/armnnTfLiteParser/test/ElementWiseUnary.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2021-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ParserFlatbuffersFixture.hpp"
7 
8 
9 TEST_SUITE("TensorflowLiteParser_ElementwiseUnary")
10 {
11 struct ElementWiseUnaryFixture : public ParserFlatbuffersFixture
12 {
ElementWiseUnaryFixtureElementWiseUnaryFixture13     explicit ElementWiseUnaryFixture(const std::string& operatorCode,
14                                      const std::string& dataType,
15                                      const std::string& inputShape,
16                                      const std::string& outputShape)
17     {
18         m_JsonString = R"(
19             {
20                 "version": 3,
21                 "operator_codes": [ { "builtin_code": )" + operatorCode + R"( } ],
22                 "subgraphs": [ {
23                     "tensors": [
24                         {
25                             "shape": )" + inputShape + R"(,
26                             "type": )" + dataType + R"( ,
27                             "buffer": 0,
28                             "name": "inputTensor",
29                             "quantization": {
30                                 "min": [ 0.0 ],
31                                 "max": [ 255.0 ],
32                                 "scale": [ 1.0 ],
33                                 "zero_point": [ 0 ],
34                             }
35                         },
36                         {
37                             "shape": )" + outputShape + R"( ,
38                             "type": )" + dataType + R"( ,
39                             "buffer": 1,
40                             "name": "outputTensor",
41                             "quantization": {
42                                 "min": [ 0.0 ],
43                                 "max": [ 255.0 ],
44                                 "scale": [ 1.0 ],
45                                 "zero_point": [ 0 ],
46                             }
47                         }
48                     ],
49                     "inputs": [ 0 ],
50                     "outputs": [ 1 ],
51                     "operators": [
52                         {
53                             "opcode_index": 0,
54                             "inputs": [ 0 ],
55                             "outputs": [ 1 ],
56                             "custom_options_format": "FLEXBUFFERS"
57                         }
58                     ],
59                 } ],
60                 "buffers" : [
61                     { },
62                     { }
63                 ]
64             }
65         )";
66         Setup();
67     }
68 };
69 
70 struct SimpleAbsFixture : public ElementWiseUnaryFixture
71 {
SimpleAbsFixtureSimpleAbsFixture72     SimpleAbsFixture() : ElementWiseUnaryFixture("ABS", "FLOAT32", "[ 2, 2 ]", "[ 2, 2 ]") {}
73 };
74 
75 TEST_CASE_FIXTURE(SimpleAbsFixture, "ParseAbs")
76 {
77     std::vector<float> inputValues
78     {
79         -0.1f, 0.2f,
80         0.3f, -0.4f
81     };
82 
83     // Calculate output data
84     std::vector<float> expectedOutputValues(inputValues.size());
85     for (unsigned int i = 0; i < inputValues.size(); ++i)
86     {
87         expectedOutputValues[i] = std::abs(inputValues[i]);
88     }
89 
90     RunTest<2, armnn::DataType::Float32>(0, {{ "inputTensor", { inputValues } }},
91                                             {{ "outputTensor",{ expectedOutputValues } } });
92 }
93 
94 struct SimpleExpFixture : public ElementWiseUnaryFixture
95 {
SimpleExpFixtureSimpleExpFixture96     SimpleExpFixture() : ElementWiseUnaryFixture("EXP", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
97 };
98 
99 TEST_CASE_FIXTURE(SimpleExpFixture, "ParseExp")
100 {
101     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 0.0f,  1.0f,  2.0f,
102                                                                 3.0f,  4.0f,  5.0f} }},
103                                             {{ "outputTensor",{ 1.0f,  2.718281f,  7.3890515f,
104                                                                 20.0855185f, 54.5980834f, 148.4129329f} } });
105 }
106 
107 struct SimpleLogFixture : public ElementWiseUnaryFixture
108 {
SimpleLogFixtureSimpleLogFixture109     SimpleLogFixture() : ElementWiseUnaryFixture("LOG", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
110 };
111 
112 TEST_CASE_FIXTURE(SimpleLogFixture, "ParseLog")
113 {
114     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", {  1.0f, 1.0f,  2.0f,
115                                                                 3.0f,  4.0f, 2.71828f} }},
116                                          {{ "outputTensor",{ 0.f,  0.f,  0.69314718056f,
117                                                              1.09861228867f, 1.38629436112f, 0.99999932734f} } });
118 }
119 
120 struct SimpleLogicalNotFixture : public ElementWiseUnaryFixture
121 {
SimpleLogicalNotFixtureSimpleLogicalNotFixture122     SimpleLogicalNotFixture() : ElementWiseUnaryFixture("LOGICAL_NOT", "BOOL", "[ 1, 1, 1, 4 ]", "[ 1, 1, 1, 4 ]") {}
123 };
124 
125 TEST_CASE_FIXTURE(SimpleLogicalNotFixture, "ParseLogicalNot")
126 {
127     RunTest<4, armnn::DataType::Boolean>(0, {{ "inputTensor", { 0, 1, 0, 1 } }},
128                                             {{ "outputTensor",{ 1, 0, 1, 0 } } });
129 }
130 
131 struct SimpleNegFixture : public ElementWiseUnaryFixture
132 {
SimpleNegFixtureSimpleNegFixture133     SimpleNegFixture() : ElementWiseUnaryFixture("NEG", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
134 };
135 
136 TEST_CASE_FIXTURE(SimpleNegFixture, "ParseNeg")
137 {
138     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 0.0f, 1.0f, -2.0f,
139                                                                 20.0855185f, -54.5980834f, 5.0f} }},
140                                             {{ "outputTensor",{ 0.0f, -1.0f, 2.0f,
141                                                                 -20.0855185f, 54.5980834f, -5.0f} }});
142 }
143 
144 struct SimpleRsqrtFixture : public ElementWiseUnaryFixture
145 {
SimpleRsqrtFixtureSimpleRsqrtFixture146     SimpleRsqrtFixture() : ElementWiseUnaryFixture("RSQRT", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
147 };
148 
149 TEST_CASE_FIXTURE(SimpleRsqrtFixture, "ParseRsqrt")
150 {
151     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 1.0f, 4.0f, 16.0f,
152                                                                 25.0f, 64.0f, 100.0f } }},
153                                             {{ "outputTensor",{ 1.0f, 0.5f, 0.25f,
154                                                                 0.2f, 0.125f, 0.1f} }});
155 }
156 
157 struct SimpleSqrtFixture : public ElementWiseUnaryFixture
158 {
SimpleSqrtFixtureSimpleSqrtFixture159     SimpleSqrtFixture() : ElementWiseUnaryFixture("SQRT", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
160 };
161 
162 TEST_CASE_FIXTURE(SimpleSqrtFixture, "ParseSqrt")
163 {
164     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 9.0f, 4.0f, 16.0f,
165                                                                 25.0f, 36.0f, 49.0f } }},
166                                          {{ "outputTensor",{ 3.0f, 2.0f, 4.0f,
167                                                              5.0f, 6.0f, 7.0f} }});
168 }
169 
170 struct SimpleSinFixture : public ElementWiseUnaryFixture
171 {
SimpleSinFixtureSimpleSinFixture172     SimpleSinFixture() : ElementWiseUnaryFixture("SIN", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
173 };
174 
175 TEST_CASE_FIXTURE(SimpleSinFixture, "ParseSin")
176 {
177     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 0.0f, 1.0f, 16.0f,
178                                                                 0.5f, 36.0f, -1.f } }},
179                                          {{ "outputTensor",{ 0.0f, 0.8414709848f, -0.28790331666f,
180                                                              0.4794255386f, -0.99177885344f, -0.8414709848f} }});
181 }
182 
183 struct SimpleCeilFixture : public ElementWiseUnaryFixture
184 {
SimpleCeilFixtureSimpleCeilFixture185     SimpleCeilFixture() : ElementWiseUnaryFixture("CEIL", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
186 };
187 
188 TEST_CASE_FIXTURE(SimpleCeilFixture, "ParseCeil")
189 {
190     RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { -50.5f, -25.9999f, -0.5f, 0.0f, 1.5555f, 25.5f } }},
191                                             {{ "outputTensor",{ -50.0f, -25.0f, 0.0f, 0.0f, 2.0f, 26.0f} }});
192 }
193 
194 }
195