Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 6 | #include "ParserFlatbuffersFixture.hpp" |
| 7 | #include "../TfLiteParser.hpp" |
| 8 | |
| 9 | #include <string> |
| 10 | #include <iostream> |
Rob Hughes | c013bc8 | 2021-07-14 09:31:31 +0100 | [diff] [blame] | 11 | #include <numeric> |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 12 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 13 | TEST_SUITE("TensorflowLiteParser_L2Normalization") |
| 14 | { |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 15 | struct L2NormalizationFixture : public ParserFlatbuffersFixture |
| 16 | { |
| 17 | explicit L2NormalizationFixture(const std::string & inputOutputShape) |
| 18 | { |
| 19 | m_JsonString = R"( |
| 20 | { |
| 21 | "version": 3, |
| 22 | "operator_codes": [ { "builtin_code": "L2_NORMALIZATION" } ], |
| 23 | "subgraphs": [ { |
| 24 | "tensors": [ |
| 25 | { |
| 26 | "shape": )" + inputOutputShape + R"(, |
| 27 | "type": "FLOAT32", |
| 28 | "buffer": 0, |
| 29 | "name": "inputTensor", |
| 30 | "quantization": { |
| 31 | "min": [ 0.0 ], |
| 32 | "max": [ 255.0 ], |
| 33 | "scale": [ 1.0 ], |
| 34 | "zero_point": [ 0 ], |
| 35 | } |
| 36 | }, |
| 37 | { |
| 38 | "shape": )" + inputOutputShape + R"(, |
| 39 | "type": "FLOAT32", |
| 40 | "buffer": 1, |
| 41 | "name": "outputTensor", |
| 42 | "quantization": { |
| 43 | "min": [ 0.0 ], |
| 44 | "max": [ 255.0 ], |
| 45 | "scale": [ 1.0 ], |
| 46 | "zero_point": [ 0 ], |
| 47 | } |
| 48 | } |
| 49 | ], |
| 50 | "inputs": [ 0 ], |
| 51 | "outputs": [ 1 ], |
| 52 | "operators": [ |
| 53 | { |
| 54 | "opcode_index": 0, |
| 55 | "inputs": [ 0 ], |
| 56 | "outputs": [ 1 ], |
| 57 | "custom_options_format": "FLEXBUFFERS" |
| 58 | } |
| 59 | ], |
| 60 | } ], |
| 61 | "buffers" : [ |
| 62 | { } |
| 63 | ] |
| 64 | } |
| 65 | )"; |
| 66 | Setup(); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | float CalcL2Norm(std::initializer_list<float> elements) |
| 71 | { |
| 72 | const float reduction = std::accumulate(elements.begin(), elements.end(), 0.0f, |
| 73 | [](float acc, float element) { return acc + element * element; }); |
| 74 | const float eps = 1e-12f; |
| 75 | const float max = reduction < eps ? eps : reduction; |
| 76 | return sqrtf(max); |
| 77 | } |
| 78 | |
| 79 | struct L2NormalizationFixture4D : L2NormalizationFixture |
| 80 | { |
| 81 | // TfLite uses NHWC shape |
| 82 | L2NormalizationFixture4D() : L2NormalizationFixture("[ 1, 1, 4, 3 ]") {} |
| 83 | }; |
| 84 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 85 | TEST_CASE_FIXTURE(L2NormalizationFixture4D, "ParseL2Normalization4D") |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 86 | { |
| 87 | RunTest<4, armnn::DataType::Float32>( |
| 88 | 0, |
| 89 | {{"inputTensor", { 1.0f, 2.0f, 3.0f, |
| 90 | 4.0f, 5.0f, 6.0f, |
| 91 | 7.0f, 8.0f, 9.0f, |
| 92 | 10.0f, 11.0f, 12.0f }}}, |
| 93 | |
| 94 | {{"outputTensor", { 1.0f / CalcL2Norm({ 1.0f, 2.0f, 3.0f }), |
| 95 | 2.0f / CalcL2Norm({ 1.0f, 2.0f, 3.0f }), |
| 96 | 3.0f / CalcL2Norm({ 1.0f, 2.0f, 3.0f }), |
| 97 | |
| 98 | 4.0f / CalcL2Norm({ 4.0f, 5.0f, 6.0f }), |
| 99 | 5.0f / CalcL2Norm({ 4.0f, 5.0f, 6.0f }), |
| 100 | 6.0f / CalcL2Norm({ 4.0f, 5.0f, 6.0f }), |
| 101 | |
| 102 | 7.0f / CalcL2Norm({ 7.0f, 8.0f, 9.0f }), |
| 103 | 8.0f / CalcL2Norm({ 7.0f, 8.0f, 9.0f }), |
| 104 | 9.0f / CalcL2Norm({ 7.0f, 8.0f, 9.0f }), |
| 105 | |
| 106 | 10.0f / CalcL2Norm({ 10.0f, 11.0f, 12.0f }), |
| 107 | 11.0f / CalcL2Norm({ 10.0f, 11.0f, 12.0f }), |
| 108 | 12.0f / CalcL2Norm({ 10.0f, 11.0f, 12.0f }) }}}); |
| 109 | } |
| 110 | |
| 111 | struct L2NormalizationSimpleFixture4D : L2NormalizationFixture |
| 112 | { |
| 113 | L2NormalizationSimpleFixture4D() : L2NormalizationFixture("[ 1, 1, 1, 4 ]") {} |
| 114 | }; |
| 115 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 116 | TEST_CASE_FIXTURE(L2NormalizationSimpleFixture4D, "ParseL2NormalizationEps4D") |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 117 | { |
| 118 | RunTest<4, armnn::DataType::Float32>( |
| 119 | 0, |
| 120 | {{"inputTensor", { 0.00000001f, 0.00000002f, 0.00000003f, 0.00000004f }}}, |
| 121 | |
| 122 | {{"outputTensor", { 0.00000001f / CalcL2Norm({ 0.00000001f, 0.00000002f, 0.00000003f, 0.00000004f }), |
| 123 | 0.00000002f / CalcL2Norm({ 0.00000001f, 0.00000002f, 0.00000003f, 0.00000004f }), |
| 124 | 0.00000003f / CalcL2Norm({ 0.00000001f, 0.00000002f, 0.00000003f, 0.00000004f }), |
| 125 | 0.00000004f / CalcL2Norm({ 0.00000001f, 0.00000002f, 0.00000003f, 0.00000004f }) }}}); |
| 126 | } |
| 127 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 128 | } |