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