blob: ffdf1cca613907c15742c5645375b27bff8a8bc8 [file] [log] [blame]
Matthew Sloyaned7fce42021-04-15 20:46:24 +01001//
Teresa Charlin93f0ad02023-03-23 15:28:02 +00002// Copyright © 2021-2023 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003// SPDX-License-Identifier: MIT
4//
5
Matthew Sloyaned7fce42021-04-15 20:46:24 +01006#include "ParserFlatbuffersFixture.hpp"
Matthew Sloyaned7fce42021-04-15 20:46:24 +01007
Matthew Sloyaned7fce42021-04-15 20:46:24 +01008
Sadik Armagan1625efc2021-06-10 18:24:34 +01009TEST_SUITE("TensorflowLiteParser_ElementwiseUnary")
10{
Matthew Sloyaned7fce42021-04-15 20:46:24 +010011struct ElementWiseUnaryFixture : public ParserFlatbuffersFixture
12{
13 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
70struct SimpleAbsFixture : public ElementWiseUnaryFixture
71{
72 SimpleAbsFixture() : ElementWiseUnaryFixture("ABS", "FLOAT32", "[ 2, 2 ]", "[ 2, 2 ]") {}
73};
74
Sadik Armagan1625efc2021-06-10 18:24:34 +010075TEST_CASE_FIXTURE(SimpleAbsFixture, "ParseAbs")
Matthew Sloyaned7fce42021-04-15 20:46:24 +010076{
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
94struct SimpleExpFixture : public ElementWiseUnaryFixture
95{
96 SimpleExpFixture() : ElementWiseUnaryFixture("EXP", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
97};
98
Sadik Armagan1625efc2021-06-10 18:24:34 +010099TEST_CASE_FIXTURE(SimpleExpFixture, "ParseExp")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100100{
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
Teresa Charlin28aa6692022-07-12 11:18:44 +0100107struct SimpleLogFixture : public ElementWiseUnaryFixture
108{
109 SimpleLogFixture() : ElementWiseUnaryFixture("LOG", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
110};
111
112TEST_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
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100120struct SimpleLogicalNotFixture : public ElementWiseUnaryFixture
121{
122 SimpleLogicalNotFixture() : ElementWiseUnaryFixture("LOGICAL_NOT", "BOOL", "[ 1, 1, 1, 4 ]", "[ 1, 1, 1, 4 ]") {}
123};
124
Sadik Armagan1625efc2021-06-10 18:24:34 +0100125TEST_CASE_FIXTURE(SimpleLogicalNotFixture, "ParseLogicalNot")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100126{
127 RunTest<4, armnn::DataType::Boolean>(0, {{ "inputTensor", { 0, 1, 0, 1 } }},
128 {{ "outputTensor",{ 1, 0, 1, 0 } } });
129}
130
131struct SimpleNegFixture : public ElementWiseUnaryFixture
132{
133 SimpleNegFixture() : ElementWiseUnaryFixture("NEG", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
134};
135
Sadik Armagan1625efc2021-06-10 18:24:34 +0100136TEST_CASE_FIXTURE(SimpleNegFixture, "ParseNeg")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100137{
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
144struct SimpleRsqrtFixture : public ElementWiseUnaryFixture
145{
146 SimpleRsqrtFixture() : ElementWiseUnaryFixture("RSQRT", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
147};
148
Sadik Armagan1625efc2021-06-10 18:24:34 +0100149TEST_CASE_FIXTURE(SimpleRsqrtFixture, "ParseRsqrt")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100150{
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
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100157struct SimpleSqrtFixture : public ElementWiseUnaryFixture
158{
159 SimpleSqrtFixture() : ElementWiseUnaryFixture("SQRT", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
160};
161
162TEST_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
Teresa Charlin28aa6692022-07-12 11:18:44 +0100170struct SimpleSinFixture : public ElementWiseUnaryFixture
171{
172 SimpleSinFixture() : ElementWiseUnaryFixture("SIN", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
173};
174
175TEST_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
Teresa Charlin93f0ad02023-03-23 15:28:02 +0000183struct SimpleCeilFixture : public ElementWiseUnaryFixture
184{
185 SimpleCeilFixture() : ElementWiseUnaryFixture("CEIL", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
186};
187
188TEST_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
Sadik Armagan1625efc2021-06-10 18:24:34 +0100194}