xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/tools/serialization/enum_mapping.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_TOOLS_SERIALIZATION_ENUM_MAPPING_H_
16 #define TENSORFLOW_LITE_TOOLS_SERIALIZATION_ENUM_MAPPING_H_
17 
18 #include "tensorflow/lite/builtin_op_data.h"
19 #include "tensorflow/lite/schema/reflection/schema_generated.h"
20 
21 // TODO(aselle): Ideally extract this from the schema.
22 
23 namespace tflite {
24 
TfLiteActivationToSchemaActivation(TfLiteFusedActivation act)25 inline ActivationFunctionType TfLiteActivationToSchemaActivation(
26     TfLiteFusedActivation act) {
27   switch (act) {
28     case kTfLiteActNone:
29       return ActivationFunctionType_NONE;
30     case kTfLiteActRelu:
31       return ActivationFunctionType_RELU;
32     case kTfLiteActReluN1To1:
33       return ActivationFunctionType_RELU_N1_TO_1;
34     case kTfLiteActRelu6:
35       return ActivationFunctionType_RELU6;
36     case kTfLiteActTanh:
37       return ActivationFunctionType_TANH;
38     case kTfLiteActSignBit:
39       return ActivationFunctionType_SIGN_BIT;
40     case kTfLiteActSigmoid:
41       return ActivationFunctionType_NONE;  // TODO(aselle): Add to schema
42   }
43   return ActivationFunctionType_NONE;
44 }
45 
TfLitePaddingToSchemaPadding(TfLitePadding padding)46 inline Padding TfLitePaddingToSchemaPadding(TfLitePadding padding) {
47   switch (padding) {
48     case kTfLitePaddingUnknown:
49       return Padding_SAME;  // TODO(aselle): Consider an error.
50     case kTfLitePaddingSame:
51       return Padding_SAME;
52     case kTfLitePaddingValid:
53       return Padding_VALID;
54   }
55   return Padding_SAME;  // TODO(aselle): Consider an error.
56 }
57 
TfLiteTypeToSchemaType(TfLiteType type)58 inline TensorType TfLiteTypeToSchemaType(TfLiteType type) {
59   switch (type) {
60     // case kTfLiteNoType: return TensorType_NONE;
61     case kTfLiteNoType:
62       return TensorType_FLOAT32;  // TODO(aselle): Consider an error.
63     case kTfLiteFloat32:
64       return TensorType_FLOAT32;
65     case kTfLiteFloat16:
66       return TensorType_FLOAT16;
67     case kTfLiteFloat64:
68       return TensorType_FLOAT64;
69     case kTfLiteInt32:
70       return TensorType_INT32;
71     case kTfLiteUInt32:
72       return TensorType_UINT32;
73     case kTfLiteUInt8:
74       return TensorType_UINT8;
75     case kTfLiteInt8:
76       return TensorType_INT8;
77     case kTfLiteInt64:
78       return TensorType_INT64;
79     case kTfLiteUInt64:
80       return TensorType_UINT64;
81     case kTfLiteString:
82       return TensorType_STRING;
83     case kTfLiteBool:
84       return TensorType_BOOL;
85     case kTfLiteUInt16:
86       return TensorType_UINT16;
87     case kTfLiteInt16:
88       return TensorType_INT16;
89     case kTfLiteComplex64:
90       return TensorType_COMPLEX64;
91     case kTfLiteComplex128:
92       return TensorType_COMPLEX128;
93     case kTfLiteResource:
94       return TensorType_RESOURCE;
95     case kTfLiteVariant:
96       return TensorType_VARIANT;
97   }
98   // TODO(aselle): consider an error
99 }
100 
101 inline FullyConnectedOptionsWeightsFormat
FullyConnectedOptionsWeightsFormatToSchema(TfLiteFullyConnectedWeightsFormat format)102 FullyConnectedOptionsWeightsFormatToSchema(
103     TfLiteFullyConnectedWeightsFormat format) {
104   switch (format) {
105     case kTfLiteFullyConnectedWeightsFormatDefault:
106       return FullyConnectedOptionsWeightsFormat_DEFAULT;
107     case kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8:
108       return FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8;
109   }
110 }
111 
LSTMKernelTypeToSchema(TfLiteLSTMKernelType type)112 inline LSTMKernelType LSTMKernelTypeToSchema(TfLiteLSTMKernelType type) {
113   switch (type) {
114     case kTfLiteLSTMFullKernel:
115       return LSTMKernelType_FULL;
116     case kTfLiteLSTMBasicKernel:
117       return LSTMKernelType_BASIC;
118   }
119 }
120 
LSHProjectionTypeToSchema(TfLiteLSHProjectionType type)121 inline LSHProjectionType LSHProjectionTypeToSchema(
122     TfLiteLSHProjectionType type) {
123   switch (type) {
124     case kTfLiteLshProjectionUnknown:
125       return LSHProjectionType_UNKNOWN;
126     case kTfLiteLshProjectionSparse:
127       return LSHProjectionType_SPARSE;
128     case kTfLiteLshProjectionDense:
129       return LSHProjectionType_DENSE;
130   }
131 }
132 
MirrorPaddingModeToSchema(TfLiteMirrorPaddingMode mode)133 inline MirrorPadMode MirrorPaddingModeToSchema(TfLiteMirrorPaddingMode mode) {
134   switch (mode) {
135     case kTfLiteMirrorPaddingUnknown:
136       return MirrorPadMode_REFLECT;  // TODO(aselle): consider an error
137     case kTfLiteMirrorPaddingReflect:
138       return MirrorPadMode_REFLECT;
139     case kTfLiteMirrorPaddingSymmetric:
140       return MirrorPadMode_SYMMETRIC;
141   }
142 }
143 
CombinerTypeToSchema(TfLiteCombinerType type)144 inline CombinerType CombinerTypeToSchema(TfLiteCombinerType type) {
145   switch (type) {
146     case kTfLiteCombinerTypeSum:
147       return CombinerType_SUM;
148     case kTfLiteCombinerTypeMean:
149       return CombinerType_MEAN;
150     case kTfLiteCombinerTypeSqrtn:
151       return CombinerType_SQRTN;
152   }
153 }
154 
155 // int
156 
157 }  // namespace tflite
158 #endif  // TENSORFLOW_LITE_TOOLS_SERIALIZATION_ENUM_MAPPING_H_
159