xref: /aosp_15_r20/external/armnn/src/armnnDeserializer/test/DeserializeElementwiseUnary.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2021,2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ParserFlatbuffersSerializeFixture.hpp"
7 #include <armnnDeserializer/IDeserializer.hpp>
8 
9 #include <string>
10 
TEST_SUITE(Deserializer)11 TEST_SUITE(Deserializer)
12 {
13 struct ElementwiseUnaryFixture : public ParserFlatbuffersSerializeFixture
14 {
15     explicit ElementwiseUnaryFixture(const std::string& inputShape,
16                                      const std::string& outputShape,
17                                      const std::string& dataType,
18                                      const std::string& unaryOperation = "Abs")
19     {
20         m_JsonString = R"(
21             {
22                 inputIds: [0],
23                 outputIds: [2],
24                 layers: [
25                     {
26                         layer_type: "InputLayer",
27                         layer: {
28                             base: {
29                                 layerBindingId: 0,
30                                 base: {
31                                     index: 0,
32                                     layerName: "InputLayer",
33                                     layerType: "Input",
34                                     inputSlots: [{
35                                         index: 0,
36                                         connection: {sourceLayerIndex:0, outputSlotIndex:0 },
37                                     }],
38                                     outputSlots: [{
39                                         index: 0,
40                                         tensorInfo: {
41                                             dimensions: )" + inputShape + R"(,
42                                             dataType: )" + dataType + R"(
43                                         }
44                                     }]
45                                 }
46                             }
47                         }
48                     },
49                     {
50                         layer_type: "ElementwiseUnaryLayer",
51                         layer: {
52                             base: {
53                                 index: 1,
54                                 layerName: "ElementwiseUnaryLayer",
55                                 layerType: "ElementwiseUnary",
56                                 inputSlots: [{
57                                     index: 0,
58                                     connection: {sourceLayerIndex:0, outputSlotIndex:0 },
59                                 }],
60                                 outputSlots: [{
61                                     index: 0,
62                                     tensorInfo: {
63                                         dimensions: )" + outputShape + R"(,
64                                         dataType: )" + dataType + R"(
65                                     }
66                                 }]
67                             },
68                             descriptor: {
69                                 activationFunction: )" + unaryOperation + R"(
70                             },
71                         }
72                     },
73                     {
74                         layer_type: "OutputLayer",
75                         layer: {
76                             base:{
77                                 layerBindingId: 2,
78                                 base: {
79                                     index: 2,
80                                     layerName: "OutputLayer",
81                                     layerType: "Output",
82                                     inputSlots: [{
83                                         index: 0,
84                                         connection: {sourceLayerIndex:1, outputSlotIndex:0 },
85                                     }],
86                                     outputSlots: [{
87                                         index: 0,
88                                         tensorInfo: {
89                                             dimensions: )" + outputShape + R"(,
90                                             dataType: )" + dataType + R"(
91                                         },
92                                     }],
93                                 }
94                             }
95                         },
96                     }
97                 ]
98             }
99         )";
100         Setup();
101     }
102 };
103 
104 struct SimpleAbsFixture : ElementwiseUnaryFixture
105 {
106     SimpleAbsFixture() : ElementwiseUnaryFixture("[ 1, 2, 2, 2 ]", // inputShape
107                                                  "[ 1, 2, 2, 2 ]", // outputShape
108                                                  "Float32",        // dataType
109                                                  "Abs")            // unaryOperation
110     {}
111 };
112 
113 FIXTURE_TEST_CASE(SimpleAbsTest, SimpleAbsFixture)
114 {
115     RunTest<4, armnn::DataType::Float32>(
116         0,
117         {{"InputLayer", {-100.0f, -50.5f, -25.9999f, -0.5f, 0.0f, 1.5555f, 25.5f, 100.0f}}},
118         {{"OutputLayer", {100.0f, 50.5f, 25.9999f, 0.5f, 0.0f, 1.5555f, 25.5f, 100.0f}}});
119 }
120 
121 struct SimpleLogFixture : ElementwiseUnaryFixture
122 {
123     SimpleLogFixture() : ElementwiseUnaryFixture("[ 1, 2, 2, 2 ]", // inputShape
124                                                  "[ 1, 2, 2, 2 ]", // outputShape
125                                                  "Float32",        // dataType
126                                                  "Log")            // unaryOperation
127     {}
128 };
129 
130 FIXTURE_TEST_CASE(SimpleLogTest, SimpleLogFixture)
131 {
132     RunTest<4, armnn::DataType::Float32>(
133         0,
134         {{"InputLayer", {1.0f, 2.1f, 3.2f, 4.3f, 10.f, 100.f, 25.5f, 200.0f}}},
135         {{"OutputLayer", {0.f, 0.74193734472f, 1.16315080981f, 1.4586150227f,
136                                                 2.30258509299f, 4.60517018599f, 3.23867845216f, 5.29831736655f}}});
137 }
138 
139 struct SimpleNegFixture : ElementwiseUnaryFixture
140 {
141     SimpleNegFixture() : ElementwiseUnaryFixture("[ 1, 2, 2, 2 ]", // inputShape
142                                                  "[ 1, 2, 2, 2 ]", // outputShape
143                                                  "Float32",        // dataType
144                                                  "Neg")            // unaryOperation
145     {}
146 };
147 
148 FIXTURE_TEST_CASE(SimpleNegTest, SimpleNegFixture)
149 {
150     RunTest<4, armnn::DataType::Float32>(
151         0,
152         {{"InputLayer", {100.0f, 50.5f, 25.9999f, 0.5f, 0.0f, -1.5555f, -25.5f, -100.0f}}},
153         {{"OutputLayer", {-100.0f, -50.5f, -25.9999f, -0.5f, 0.0f, 1.5555f, 25.5f, 100.0f}}});
154 }
155 
156 struct SimpleSinFixture : ElementwiseUnaryFixture
157 {
158     SimpleSinFixture() : ElementwiseUnaryFixture("[ 1, 2, 2, 2 ]", // inputShape
159                                                  "[ 1, 2, 2, 2 ]", // outputShape
160                                                  "Float32",        // dataType
161                                                  "Sin")            // unaryOperation
162     {}
163 };
164 
165 FIXTURE_TEST_CASE(SimpleSinTest, SimpleSinFixture)
166 {
167     RunTest<4, armnn::DataType::Float32>(
168         0,
169         {{"InputLayer", {-100.0f, -50.5f, -25.9999f, -0.5f, 0.0f, 1.5555f, 25.5f, 100.0f}}},
170         {{"OutputLayer", {0.50636564111f, -0.23237376165f, -0.76249375473f, -0.4794255386f,
171                                                 0.0f, 0.99988301347f, 0.35905835402f, -0.50636564111f}}});
172 }
173 
174 struct SimpleCeilFixture : ElementwiseUnaryFixture
175 {
176     SimpleCeilFixture() : ElementwiseUnaryFixture("[ 1, 2, 2, 2 ]", // inputShape
177                                                   "[ 1, 2, 2, 2 ]", // outputShape
178                                                   "Float32",        // dataType
179                                                   "Ceil")           // unaryOperation
180     {}
181 };
182 
183 FIXTURE_TEST_CASE(SimpleCeilTest, SimpleCeilFixture)
184 {
185     RunTest<4, armnn::DataType::Float32>(
186             0,
187             {{"InputLayer", {-100.0f, -50.5f, -25.9999f, -0.5f, 0.0f, 1.5555f, 25.5f, 100.0f}}},
188             {{"OutputLayer", {-100.0f, -50.0f, -25.0f, 0.0f, 0.0f, 2.0f, 26.0f, 100.0f}}});
189 }
190 }