d8604a5cd5dafe357543303602e751e8074805ef
[platform/upstream/armnn.git] / src / armnnDeserializer / test / DeserializeL2Normalization.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include <boost/test/unit_test.hpp>
7 #include "ParserFlatbuffersSerializeFixture.hpp"
8 #include "../Deserializer.hpp"
9
10 #include <string>
11 #include <iostream>
12
13 BOOST_AUTO_TEST_SUITE(Deserializer)
14
15 struct L2NormalizationFixture : public ParserFlatbuffersSerializeFixture
16 {
17     explicit L2NormalizationFixture(const std::string &inputShape,
18                                     const std::string &outputShape,
19                                     const std::string &dataType,
20                                     const std::string &dataLayout,
21                                     const std::string epsilon)
22     {
23         m_JsonString = R"(
24     {
25         inputIds: [0],
26         outputIds: [2],
27         layers: [
28            {
29             layer_type: "InputLayer",
30             layer: {
31                 base: {
32                     layerBindingId: 0,
33                     base: {
34                         index: 0,
35                         layerName: "InputLayer",
36                         layerType: "Input",
37                         inputSlots: [{
38                             index: 0,
39                             connection: {sourceLayerIndex:0, outputSlotIndex:0 },
40                             }],
41                         outputSlots: [{
42                             index: 0,
43                             tensorInfo: {
44                                 dimensions: )" + inputShape + R"(,
45                                 dataType: ")" + dataType + R"(",
46                                 quantizationScale: 0.5,
47                                 quantizationOffset: 0
48                                 },
49                             }]
50                         },
51                     }
52                 },
53             },
54         {
55         layer_type: "L2NormalizationLayer",
56         layer : {
57             base: {
58                 index:1,
59                 layerName: "L2NormalizationLayer",
60                 layerType: "L2Normalization",
61                 inputSlots: [{
62                         index: 0,
63                         connection: {sourceLayerIndex:0, outputSlotIndex:0 },
64                    }],
65                 outputSlots: [{
66                     index: 0,
67                     tensorInfo: {
68                         dimensions: )" + outputShape + R"(,
69                         dataType: ")" + dataType + R"("
70                     },
71                     }],
72                 },
73             descriptor: {
74                 dataLayout: ")" + dataLayout + R"(",
75                 eps: )" + epsilon + R"(
76                 },
77             },
78         },
79         {
80         layer_type: "OutputLayer",
81         layer: {
82             base:{
83                 layerBindingId: 0,
84                 base: {
85                     index: 2,
86                     layerName: "OutputLayer",
87                     layerType: "Output",
88                     inputSlots: [{
89                         index: 0,
90                         connection: {sourceLayerIndex:1, outputSlotIndex:0 },
91                     }],
92                     outputSlots: [ {
93                         index: 0,
94                         tensorInfo: {
95                             dimensions: )" + outputShape + R"(,
96                             dataType: ")" + dataType + R"("
97                         },
98                     }],
99                 }
100             }},
101         }]
102     }
103 )";
104         Setup();
105     }
106 };
107
108 struct L2NormFixture : L2NormalizationFixture
109 {
110     // Using a non standard epsilon value of 1e-8
111     L2NormFixture():L2NormalizationFixture("[ 1, 3, 1, 1 ]",
112                                            "[ 1, 3, 1, 1 ]",
113                                            "Float32",
114                                            "NCHW",
115                                            "0.00000001"){}
116 };
117
118 BOOST_FIXTURE_TEST_CASE(L2NormalizationFloat32, L2NormFixture)
119 {
120     // 1 / sqrt(1^2 + 2^2 + 3^2)
121     const float approxInvL2Norm = 0.267261f;
122
123     RunTest<4, armnn::DataType::Float32>(0,
124                                          {{"InputLayer", { 1.0f, 2.0f, 3.0f }}},
125                                          {{"OutputLayer",{ 1.0f * approxInvL2Norm,
126                                                            2.0f * approxInvL2Norm,
127                                                            3.0f * approxInvL2Norm }}});
128 }
129
130 BOOST_FIXTURE_TEST_CASE(L2NormalizationEpsilonLimitFloat32, L2NormFixture)
131 {
132     // 1 / sqrt(1e-8)
133     const float approxInvL2Norm = 10000;
134
135     RunTest<4, armnn::DataType::Float32>(0,
136                                          {{"InputLayer", { 0.00000001f, 0.00000002f, 0.00000003f }}},
137                                          {{"OutputLayer",{ 0.00000001f * approxInvL2Norm,
138                                                            0.00000002f * approxInvL2Norm,
139                                                            0.00000003f * approxInvL2Norm }}});
140 }
141
142 BOOST_AUTO_TEST_SUITE_END()