blob: 21718d8d16ce5267c5cffcea0ed62830df5011af [file] [log] [blame]
Matthew Sloyaned7fce42021-04-15 20:46:24 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matthew Sloyaned7fce42021-04-15 20:46:24 +01006#include "ParserFlatbuffersFixture.hpp"
7#include "../TfLiteParser.hpp"
8
9#include <string>
10
Sadik Armagan1625efc2021-06-10 18:24:34 +010011TEST_SUITE("TensorflowLiteParser_ElementwiseUnary")
12{
Matthew Sloyaned7fce42021-04-15 20:46:24 +010013struct ElementWiseUnaryFixture : public ParserFlatbuffersFixture
14{
15 explicit ElementWiseUnaryFixture(const std::string& operatorCode,
16 const std::string& dataType,
17 const std::string& inputShape,
18 const std::string& outputShape)
19 {
20 m_JsonString = R"(
21 {
22 "version": 3,
23 "operator_codes": [ { "builtin_code": )" + operatorCode + R"( } ],
24 "subgraphs": [ {
25 "tensors": [
26 {
27 "shape": )" + inputShape + R"(,
28 "type": )" + dataType + R"( ,
29 "buffer": 0,
30 "name": "inputTensor",
31 "quantization": {
32 "min": [ 0.0 ],
33 "max": [ 255.0 ],
34 "scale": [ 1.0 ],
35 "zero_point": [ 0 ],
36 }
37 },
38 {
39 "shape": )" + outputShape + R"( ,
40 "type": )" + dataType + R"( ,
41 "buffer": 1,
42 "name": "outputTensor",
43 "quantization": {
44 "min": [ 0.0 ],
45 "max": [ 255.0 ],
46 "scale": [ 1.0 ],
47 "zero_point": [ 0 ],
48 }
49 }
50 ],
51 "inputs": [ 0 ],
52 "outputs": [ 1 ],
53 "operators": [
54 {
55 "opcode_index": 0,
56 "inputs": [ 0 ],
57 "outputs": [ 1 ],
58 "custom_options_format": "FLEXBUFFERS"
59 }
60 ],
61 } ],
62 "buffers" : [
63 { },
64 { }
65 ]
66 }
67 )";
68 Setup();
69 }
70};
71
72struct SimpleAbsFixture : public ElementWiseUnaryFixture
73{
74 SimpleAbsFixture() : ElementWiseUnaryFixture("ABS", "FLOAT32", "[ 2, 2 ]", "[ 2, 2 ]") {}
75};
76
Sadik Armagan1625efc2021-06-10 18:24:34 +010077TEST_CASE_FIXTURE(SimpleAbsFixture, "ParseAbs")
Matthew Sloyaned7fce42021-04-15 20:46:24 +010078{
79 std::vector<float> inputValues
80 {
81 -0.1f, 0.2f,
82 0.3f, -0.4f
83 };
84
85 // Calculate output data
86 std::vector<float> expectedOutputValues(inputValues.size());
87 for (unsigned int i = 0; i < inputValues.size(); ++i)
88 {
89 expectedOutputValues[i] = std::abs(inputValues[i]);
90 }
91
92 RunTest<2, armnn::DataType::Float32>(0, {{ "inputTensor", { inputValues } }},
93 {{ "outputTensor",{ expectedOutputValues } } });
94}
95
96struct SimpleExpFixture : public ElementWiseUnaryFixture
97{
98 SimpleExpFixture() : ElementWiseUnaryFixture("EXP", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
99};
100
Sadik Armagan1625efc2021-06-10 18:24:34 +0100101TEST_CASE_FIXTURE(SimpleExpFixture, "ParseExp")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100102{
103 RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 0.0f, 1.0f, 2.0f,
104 3.0f, 4.0f, 5.0f} }},
105 {{ "outputTensor",{ 1.0f, 2.718281f, 7.3890515f,
106 20.0855185f, 54.5980834f, 148.4129329f} } });
107}
108
109struct SimpleLogicalNotFixture : public ElementWiseUnaryFixture
110{
111 SimpleLogicalNotFixture() : ElementWiseUnaryFixture("LOGICAL_NOT", "BOOL", "[ 1, 1, 1, 4 ]", "[ 1, 1, 1, 4 ]") {}
112};
113
Sadik Armagan1625efc2021-06-10 18:24:34 +0100114TEST_CASE_FIXTURE(SimpleLogicalNotFixture, "ParseLogicalNot")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100115{
116 RunTest<4, armnn::DataType::Boolean>(0, {{ "inputTensor", { 0, 1, 0, 1 } }},
117 {{ "outputTensor",{ 1, 0, 1, 0 } } });
118}
119
120struct SimpleNegFixture : public ElementWiseUnaryFixture
121{
122 SimpleNegFixture() : ElementWiseUnaryFixture("NEG", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
123};
124
Sadik Armagan1625efc2021-06-10 18:24:34 +0100125TEST_CASE_FIXTURE(SimpleNegFixture, "ParseNeg")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100126{
127 RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 0.0f, 1.0f, -2.0f,
128 20.0855185f, -54.5980834f, 5.0f} }},
129 {{ "outputTensor",{ 0.0f, -1.0f, 2.0f,
130 -20.0855185f, 54.5980834f, -5.0f} }});
131}
132
133struct SimpleRsqrtFixture : public ElementWiseUnaryFixture
134{
135 SimpleRsqrtFixture() : ElementWiseUnaryFixture("RSQRT", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {}
136};
137
Sadik Armagan1625efc2021-06-10 18:24:34 +0100138TEST_CASE_FIXTURE(SimpleRsqrtFixture, "ParseRsqrt")
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100139{
140 RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 1.0f, 4.0f, 16.0f,
141 25.0f, 64.0f, 100.0f } }},
142 {{ "outputTensor",{ 1.0f, 0.5f, 0.25f,
143 0.2f, 0.125f, 0.1f} }});
144}
145
Sadik Armagan1625efc2021-06-10 18:24:34 +0100146}