• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2017,2019-2021,2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "SerializerUtils.hpp"
7 
8 namespace armnnSerializer
9 {
10 
GetFlatBufferComparisonOperation(armnn::ComparisonOperation comparisonOperation)11 armnnSerializer::ComparisonOperation GetFlatBufferComparisonOperation(armnn::ComparisonOperation comparisonOperation)
12 {
13     switch (comparisonOperation)
14     {
15         case armnn::ComparisonOperation::Equal:
16             return armnnSerializer::ComparisonOperation::ComparisonOperation_Equal;
17         case armnn::ComparisonOperation::Greater:
18             return armnnSerializer::ComparisonOperation::ComparisonOperation_Greater;
19         case armnn::ComparisonOperation::GreaterOrEqual:
20             return armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual;
21         case armnn::ComparisonOperation::Less:
22             return armnnSerializer::ComparisonOperation::ComparisonOperation_Less;
23         case armnn::ComparisonOperation::LessOrEqual:
24             return armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual;
25         case armnn::ComparisonOperation::NotEqual:
26         default:
27             return armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual;
28     }
29 }
30 
GetFlatBufferLogicalBinaryOperation(armnn::LogicalBinaryOperation logicalBinaryOperation)31 armnnSerializer::LogicalBinaryOperation GetFlatBufferLogicalBinaryOperation(
32     armnn::LogicalBinaryOperation logicalBinaryOperation)
33 {
34     switch (logicalBinaryOperation)
35     {
36         case armnn::LogicalBinaryOperation::LogicalAnd:
37             return armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalAnd;
38         case armnn::LogicalBinaryOperation::LogicalOr:
39             return armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalOr;
40         default:
41             throw armnn::InvalidArgumentException("Logical Binary operation unknown");
42     }
43 }
44 
GetFlatBufferConstTensorData(armnn::DataType dataType)45 armnnSerializer::ConstTensorData GetFlatBufferConstTensorData(armnn::DataType dataType)
46 {
47     switch (dataType)
48     {
49         case armnn::DataType::Float32:
50         case armnn::DataType::Signed32:
51             return armnnSerializer::ConstTensorData::ConstTensorData_IntData;
52         case armnn::DataType::Float16:
53         case armnn::DataType::QSymmS16:
54             return armnnSerializer::ConstTensorData::ConstTensorData_ShortData;
55         case armnn::DataType::QAsymmS8:
56         case armnn::DataType::QAsymmU8:
57         case armnn::DataType::QSymmS8:
58         case armnn::DataType::Boolean:
59             return armnnSerializer::ConstTensorData::ConstTensorData_ByteData;
60         case armnn::DataType::Signed64:
61             return armnnSerializer::ConstTensorData::ConstTensorData_LongData;
62         default:
63             return armnnSerializer::ConstTensorData::ConstTensorData_NONE;
64     }
65 }
66 
GetFlatBufferDataType(armnn::DataType dataType)67 armnnSerializer::DataType GetFlatBufferDataType(armnn::DataType dataType)
68 {
69     switch (dataType)
70     {
71         case armnn::DataType::Float32:
72             return armnnSerializer::DataType::DataType_Float32;
73         case armnn::DataType::Float16:
74             return armnnSerializer::DataType::DataType_Float16;
75         case armnn::DataType::Signed32:
76             return armnnSerializer::DataType::DataType_Signed32;
77         case armnn::DataType::Signed64:
78             return armnnSerializer::DataType::DataType_Signed64;
79         case armnn::DataType::QSymmS16:
80             return armnnSerializer::DataType::DataType_QSymmS16;
81         case armnn::DataType::QAsymmS8:
82             return armnnSerializer::DataType::DataType_QAsymmS8;
83         case armnn::DataType::QAsymmU8:
84             return armnnSerializer::DataType::DataType_QAsymmU8;
85         case armnn::DataType::QSymmS8:
86             return armnnSerializer::DataType::DataType_QSymmS8;
87         case armnn::DataType::Boolean:
88             return armnnSerializer::DataType::DataType_Boolean;
89         default:
90             return armnnSerializer::DataType::DataType_Float16;
91     }
92 }
93 
GetFlatBufferDataLayout(armnn::DataLayout dataLayout)94 armnnSerializer::DataLayout GetFlatBufferDataLayout(armnn::DataLayout dataLayout)
95 {
96     switch (dataLayout)
97     {
98         case armnn::DataLayout::NHWC:
99             return armnnSerializer::DataLayout::DataLayout_NHWC;
100         case armnn::DataLayout::NDHWC:
101             return armnnSerializer::DataLayout::DataLayout_NDHWC;
102         case armnn::DataLayout::NCDHW:
103             return armnnSerializer::DataLayout::DataLayout_NCDHW;
104         case armnn::DataLayout::NCHW:
105         default:
106             return armnnSerializer::DataLayout::DataLayout_NCHW;
107     }
108 }
109 
GetFlatBufferBinaryOperation(armnn::BinaryOperation binaryOperation)110 armnnSerializer::BinaryOperation GetFlatBufferBinaryOperation(armnn::BinaryOperation binaryOperation)
111 {
112     switch (binaryOperation)
113     {
114         case armnn::BinaryOperation::Add:
115             return armnnSerializer::BinaryOperation::BinaryOperation_Add;
116         case armnn::BinaryOperation::Div:
117             return armnnSerializer::BinaryOperation::BinaryOperation_Div;
118         case armnn::BinaryOperation::Maximum:
119             return armnnSerializer::BinaryOperation::BinaryOperation_Maximum;
120         case armnn::BinaryOperation::Minimum:
121             return armnnSerializer::BinaryOperation::BinaryOperation_Minimum;
122         case armnn::BinaryOperation::Mul:
123             return armnnSerializer::BinaryOperation::BinaryOperation_Mul;
124         case armnn::BinaryOperation::Sub:
125             return armnnSerializer::BinaryOperation::BinaryOperation_Sub;
126         default:
127             throw armnn::InvalidArgumentException("Elementwise Binary operation unknown");
128     }
129 }
130 
GetFlatBufferUnaryOperation(armnn::UnaryOperation unaryOperation)131 armnnSerializer::UnaryOperation GetFlatBufferUnaryOperation(armnn::UnaryOperation unaryOperation)
132 {
133     switch (unaryOperation)
134     {
135         case armnn::UnaryOperation::Abs:
136             return armnnSerializer::UnaryOperation::UnaryOperation_Abs;
137         case armnn::UnaryOperation::Ceil:
138             return armnnSerializer::UnaryOperation::UnaryOperation_Ceil;
139         case armnn::UnaryOperation::Rsqrt:
140             return armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt;
141         case armnn::UnaryOperation::Sqrt:
142             return armnnSerializer::UnaryOperation::UnaryOperation_Sqrt;
143         case armnn::UnaryOperation::Exp:
144             return armnnSerializer::UnaryOperation::UnaryOperation_Exp;
145         case armnn::UnaryOperation::Neg:
146             return armnnSerializer::UnaryOperation::UnaryOperation_Neg;
147         case armnn::UnaryOperation::LogicalNot:
148             return armnnSerializer::UnaryOperation::UnaryOperation_LogicalNot;
149         case armnn::UnaryOperation::Log:
150             return armnnSerializer::UnaryOperation::UnaryOperation_Log;
151         case armnn::UnaryOperation::Sin:
152             return armnnSerializer::UnaryOperation::UnaryOperation_Sin;
153         default:
154             throw armnn::InvalidArgumentException("Elementwise Unary operation unknown");
155     }
156 }
157 
GetFlatBufferPoolingAlgorithm(armnn::PoolingAlgorithm poolingAlgorithm)158 armnnSerializer::PoolingAlgorithm GetFlatBufferPoolingAlgorithm(armnn::PoolingAlgorithm poolingAlgorithm)
159 {
160     switch (poolingAlgorithm)
161     {
162         case armnn::PoolingAlgorithm::Average:
163             return armnnSerializer::PoolingAlgorithm::PoolingAlgorithm_Average;
164         case armnn::PoolingAlgorithm::L2:
165             return armnnSerializer::PoolingAlgorithm::PoolingAlgorithm_L2;
166         case armnn::PoolingAlgorithm::Max:
167         default:
168             return armnnSerializer::PoolingAlgorithm::PoolingAlgorithm_Max;
169     }
170 }
171 
GetFlatBufferOutputShapeRounding(armnn::OutputShapeRounding outputShapeRounding)172 armnnSerializer::OutputShapeRounding GetFlatBufferOutputShapeRounding(armnn::OutputShapeRounding outputShapeRounding)
173 {
174     switch (outputShapeRounding)
175     {
176         case armnn::OutputShapeRounding::Ceiling:
177             return armnnSerializer::OutputShapeRounding::OutputShapeRounding_Ceiling;
178         case armnn::OutputShapeRounding::Floor:
179         default:
180             return armnnSerializer::OutputShapeRounding::OutputShapeRounding_Floor;
181     }
182 }
183 
GetFlatBufferPaddingMethod(armnn::PaddingMethod paddingMethod)184 armnnSerializer::PaddingMethod GetFlatBufferPaddingMethod(armnn::PaddingMethod paddingMethod)
185 {
186     switch (paddingMethod)
187     {
188         case armnn::PaddingMethod::IgnoreValue:
189             return armnnSerializer::PaddingMethod::PaddingMethod_IgnoreValue;
190         case armnn::PaddingMethod::Exclude:
191         default:
192             return armnnSerializer::PaddingMethod::PaddingMethod_Exclude;
193     }
194 }
195 
GetFlatBufferPaddingMode(armnn::PaddingMode paddingMode)196 armnnSerializer::PaddingMode GetFlatBufferPaddingMode(armnn::PaddingMode paddingMode)
197 {
198     switch (paddingMode)
199     {
200         case armnn::PaddingMode::Reflect:
201             return armnnSerializer::PaddingMode::PaddingMode_Reflect;
202         case armnn::PaddingMode::Symmetric:
203             return armnnSerializer::PaddingMode::PaddingMode_Symmetric;
204         default:
205             return armnnSerializer::PaddingMode::PaddingMode_Constant;
206     }
207 }
208 
GetFlatBufferNormalizationAlgorithmChannel(armnn::NormalizationAlgorithmChannel normalizationAlgorithmChannel)209 armnnSerializer::NormalizationAlgorithmChannel GetFlatBufferNormalizationAlgorithmChannel(
210     armnn::NormalizationAlgorithmChannel normalizationAlgorithmChannel)
211 {
212     switch (normalizationAlgorithmChannel)
213     {
214         case armnn::NormalizationAlgorithmChannel::Across:
215             return armnnSerializer::NormalizationAlgorithmChannel::NormalizationAlgorithmChannel_Across;
216         case armnn::NormalizationAlgorithmChannel::Within:
217             return armnnSerializer::NormalizationAlgorithmChannel::NormalizationAlgorithmChannel_Within;
218         default:
219             return armnnSerializer::NormalizationAlgorithmChannel::NormalizationAlgorithmChannel_Across;
220     }
221 }
222 
GetFlatBufferNormalizationAlgorithmMethod(armnn::NormalizationAlgorithmMethod normalizationAlgorithmMethod)223 armnnSerializer::NormalizationAlgorithmMethod GetFlatBufferNormalizationAlgorithmMethod(
224     armnn::NormalizationAlgorithmMethod normalizationAlgorithmMethod)
225 {
226     switch (normalizationAlgorithmMethod)
227     {
228         case armnn::NormalizationAlgorithmMethod::LocalBrightness:
229             return armnnSerializer::NormalizationAlgorithmMethod::NormalizationAlgorithmMethod_LocalBrightness;
230         case armnn::NormalizationAlgorithmMethod::LocalContrast:
231             return armnnSerializer::NormalizationAlgorithmMethod::NormalizationAlgorithmMethod_LocalContrast;
232         default:
233             return armnnSerializer::NormalizationAlgorithmMethod::NormalizationAlgorithmMethod_LocalBrightness;
234     }
235 }
236 
GetFlatBufferResizeMethod(armnn::ResizeMethod method)237 armnnSerializer::ResizeMethod GetFlatBufferResizeMethod(armnn::ResizeMethod method)
238 {
239     switch (method)
240     {
241         case armnn::ResizeMethod::NearestNeighbor:
242             return armnnSerializer::ResizeMethod_NearestNeighbor;
243         case armnn::ResizeMethod::Bilinear:
244             return armnnSerializer::ResizeMethod_Bilinear;
245         default:
246             return armnnSerializer::ResizeMethod_NearestNeighbor;
247     }
248 }
249 
GetFlatBufferReduceOperation(armnn::ReduceOperation reduceOperation)250 armnnSerializer::ReduceOperation GetFlatBufferReduceOperation(armnn::ReduceOperation reduceOperation)
251 {
252     switch (reduceOperation)
253     {
254         case armnn::ReduceOperation::Sum:
255             return armnnSerializer::ReduceOperation::ReduceOperation_Sum;
256         case armnn::ReduceOperation::Max:
257             return armnnSerializer::ReduceOperation::ReduceOperation_Max;
258         case armnn::ReduceOperation::Mean:
259             return armnnSerializer::ReduceOperation::ReduceOperation_Mean;
260         case armnn::ReduceOperation::Min:
261             return armnnSerializer::ReduceOperation::ReduceOperation_Min;
262         case armnn::ReduceOperation::Prod:
263             return armnnSerializer::ReduceOperation::ReduceOperation_Prod;
264         default:
265             return armnnSerializer::ReduceOperation::ReduceOperation_Sum;
266     }
267 }
268 
269 } // namespace armnnSerializer
270