Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 6 | #include "ParserFlatbuffersFixture.hpp" |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 7 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 8 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 9 | TEST_SUITE("TensorflowLiteParser_ElementwiseUnary") |
| 10 | { |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 11 | struct 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 | |
| 70 | struct SimpleAbsFixture : public ElementWiseUnaryFixture |
| 71 | { |
| 72 | SimpleAbsFixture() : ElementWiseUnaryFixture("ABS", "FLOAT32", "[ 2, 2 ]", "[ 2, 2 ]") {} |
| 73 | }; |
| 74 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 75 | TEST_CASE_FIXTURE(SimpleAbsFixture, "ParseAbs") |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 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 | { |
| 96 | SimpleExpFixture() : ElementWiseUnaryFixture("EXP", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {} |
| 97 | }; |
| 98 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 99 | TEST_CASE_FIXTURE(SimpleExpFixture, "ParseExp") |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 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 SimpleLogicalNotFixture : public ElementWiseUnaryFixture |
| 108 | { |
| 109 | SimpleLogicalNotFixture() : ElementWiseUnaryFixture("LOGICAL_NOT", "BOOL", "[ 1, 1, 1, 4 ]", "[ 1, 1, 1, 4 ]") {} |
| 110 | }; |
| 111 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 112 | TEST_CASE_FIXTURE(SimpleLogicalNotFixture, "ParseLogicalNot") |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 113 | { |
| 114 | RunTest<4, armnn::DataType::Boolean>(0, {{ "inputTensor", { 0, 1, 0, 1 } }}, |
| 115 | {{ "outputTensor",{ 1, 0, 1, 0 } } }); |
| 116 | } |
| 117 | |
| 118 | struct SimpleNegFixture : public ElementWiseUnaryFixture |
| 119 | { |
| 120 | SimpleNegFixture() : ElementWiseUnaryFixture("NEG", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {} |
| 121 | }; |
| 122 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 123 | TEST_CASE_FIXTURE(SimpleNegFixture, "ParseNeg") |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 124 | { |
| 125 | RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 0.0f, 1.0f, -2.0f, |
| 126 | 20.0855185f, -54.5980834f, 5.0f} }}, |
| 127 | {{ "outputTensor",{ 0.0f, -1.0f, 2.0f, |
| 128 | -20.0855185f, 54.5980834f, -5.0f} }}); |
| 129 | } |
| 130 | |
| 131 | struct SimpleRsqrtFixture : public ElementWiseUnaryFixture |
| 132 | { |
| 133 | SimpleRsqrtFixture() : ElementWiseUnaryFixture("RSQRT", "FLOAT32", "[ 1, 2, 3, 1 ]", "[ 1, 2, 3, 1 ]") {} |
| 134 | }; |
| 135 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 136 | TEST_CASE_FIXTURE(SimpleRsqrtFixture, "ParseRsqrt") |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 137 | { |
| 138 | RunTest<4, armnn::DataType::Float32>(0, {{ "inputTensor", { 1.0f, 4.0f, 16.0f, |
| 139 | 25.0f, 64.0f, 100.0f } }}, |
| 140 | {{ "outputTensor",{ 1.0f, 0.5f, 0.25f, |
| 141 | 0.2f, 0.125f, 0.1f} }}); |
| 142 | } |
| 143 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 144 | } |