ArmNN
 20.02
L2Normalization.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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 #include <iostream>
12 
13 BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
14 
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 
85 BOOST_FIXTURE_TEST_CASE(ParseL2Normalization4D, L2NormalizationFixture4D)
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 
116 BOOST_FIXTURE_TEST_CASE(ParseL2NormalizationEps4D, L2NormalizationSimpleFixture4D)
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 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_FIXTURE_TEST_CASE(ParseL2Normalization4D, L2NormalizationFixture4D)
float CalcL2Norm(std::initializer_list< float > elements)
BOOST_AUTO_TEST_SUITE_END()