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