xref: /aosp_15_r20/external/armnn/src/armnnDeserializer/test/DeserializeNormalization.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_Normalization")
12 {
13 struct NormalizationFixture : public ParserFlatbuffersSerializeFixture
14 {
NormalizationFixtureNormalizationFixture15     explicit NormalizationFixture(const std::string &inputShape,
16         const std::string & outputShape,
17         const std::string &dataType,
18         const std::string &normAlgorithmChannel,
19         const std::string &normAlgorithmMethod,
20         const std::string &dataLayout)
21     {
22         m_JsonString = R"(
23         {
24             inputIds: [0],
25             outputIds: [2],
26             layers: [{
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: "NormalizationLayer",
54             layer : {
55                 base: {
56                     index:1,
57                     layerName: "NormalizationLayer",
58                     layerType: "Normalization",
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                     normChannelType: )" + normAlgorithmChannel + R"(,
73                     normMethodType: )" + normAlgorithmMethod + R"(,
74                     normSize: 3,
75                     alpha: 1,
76                     beta: 1,
77                     k: 1,
78                     dataLayout: )" + dataLayout + R"(
79                     }
80                 },
81             },
82             {
83             layer_type: "OutputLayer",
84             layer: {
85                 base:{
86                     layerBindingId: 0,
87                     base: {
88                         index: 2,
89                         layerName: "OutputLayer",
90                         layerType: "Output",
91                         inputSlots: [{
92                             index: 0,
93                             connection: {sourceLayerIndex:1, outputSlotIndex:0 },
94                         }],
95                         outputSlots: [ {
96                             index: 0,
97                             tensorInfo: {
98                                 dimensions: )" + outputShape + R"(,
99                                 dataType: )" + dataType + R"(
100                             },
101                         }],
102                     }
103                 }},
104             }]
105         }
106  )";
107         SetupSingleInputSingleOutput("InputLayer", "OutputLayer");
108     }
109 };
110 
111 struct FloatNhwcLocalBrightnessAcrossNormalizationFixture : NormalizationFixture
112 {
FloatNhwcLocalBrightnessAcrossNormalizationFixtureFloatNhwcLocalBrightnessAcrossNormalizationFixture113     FloatNhwcLocalBrightnessAcrossNormalizationFixture() : NormalizationFixture("[ 2, 2, 2, 1 ]", "[ 2, 2, 2, 1 ]",
114         "Float32", "0", "0", "NHWC") {}
115 };
116 
117 
118 TEST_CASE_FIXTURE(FloatNhwcLocalBrightnessAcrossNormalizationFixture, "Float32NormalizationNhwcDataLayout")
119 {
120     RunTest<4, armnn::DataType::Float32>(0, { 1.0f, 2.0f, 3.0f, 4.0f,
121                                               5.0f, 6.0f, 7.0f, 8.0f },
122                                             { 0.5f, 0.400000006f, 0.300000012f, 0.235294119f,
123                                               0.192307696f, 0.16216217f, 0.140000001f, 0.123076923f });
124 }
125 
126 struct FloatNchwLocalBrightnessWithinNormalizationFixture : NormalizationFixture
127 {
FloatNchwLocalBrightnessWithinNormalizationFixtureFloatNchwLocalBrightnessWithinNormalizationFixture128     FloatNchwLocalBrightnessWithinNormalizationFixture() : NormalizationFixture("[ 2, 1, 2, 2 ]", "[ 2, 1, 2, 2 ]",
129         "Float32", "1", "0", "NCHW") {}
130 };
131 
132 TEST_CASE_FIXTURE(FloatNchwLocalBrightnessWithinNormalizationFixture, "Float32NormalizationNchwDataLayout")
133 {
134     RunTest<4, armnn::DataType::Float32>(0, { 1.0f, 2.0f, 3.0f, 4.0f,
135                                               5.0f, 6.0f, 7.0f, 8.0f },
136                                             { 0.0322581f, 0.0645161f, 0.0967742f, 0.1290323f,
137                                               0.0285714f, 0.0342857f, 0.04f, 0.0457143f });
138 }
139 
140 }