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)25inline 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)46inline 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)58inline 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 kTfLiteInt16: 86 return TensorType_INT16; 87 case kTfLiteComplex64: 88 return TensorType_COMPLEX64; 89 case kTfLiteComplex128: 90 return TensorType_COMPLEX128; 91 case kTfLiteResource: 92 return TensorType_RESOURCE; 93 case kTfLiteVariant: 94 return TensorType_VARIANT; 95 } 96 // TODO(aselle): consider an error 97 } 98 99 inline FullyConnectedOptionsWeightsFormat FullyConnectedOptionsWeightsFormatToSchema(TfLiteFullyConnectedWeightsFormat format)100FullyConnectedOptionsWeightsFormatToSchema( 101 TfLiteFullyConnectedWeightsFormat format) { 102 switch (format) { 103 case kTfLiteFullyConnectedWeightsFormatDefault: 104 return FullyConnectedOptionsWeightsFormat_DEFAULT; 105 case kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8: 106 return FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8; 107 } 108 } 109 LSTMKernelTypeToSchema(TfLiteLSTMKernelType type)110inline LSTMKernelType LSTMKernelTypeToSchema(TfLiteLSTMKernelType type) { 111 switch (type) { 112 case kTfLiteLSTMFullKernel: 113 return LSTMKernelType_FULL; 114 case kTfLiteLSTMBasicKernel: 115 return LSTMKernelType_BASIC; 116 } 117 } 118 LSHProjectionTypeToSchema(TfLiteLSHProjectionType type)119inline LSHProjectionType LSHProjectionTypeToSchema( 120 TfLiteLSHProjectionType type) { 121 switch (type) { 122 case kTfLiteLshProjectionUnknown: 123 return LSHProjectionType_UNKNOWN; 124 case kTfLiteLshProjectionSparse: 125 return LSHProjectionType_SPARSE; 126 case kTfLiteLshProjectionDense: 127 return LSHProjectionType_DENSE; 128 } 129 } 130 MirrorPaddingModeToSchema(TfLiteMirrorPaddingMode mode)131inline MirrorPadMode MirrorPaddingModeToSchema(TfLiteMirrorPaddingMode mode) { 132 switch (mode) { 133 case kTfLiteMirrorPaddingUnknown: 134 return MirrorPadMode_REFLECT; // TODO(aselle): consider an error 135 case kTfLiteMirrorPaddingReflect: 136 return MirrorPadMode_REFLECT; 137 case kTfLiteMirrorPaddingSymmetric: 138 return MirrorPadMode_SYMMETRIC; 139 } 140 } 141 CombinerTypeToSchema(TfLiteCombinerType type)142inline CombinerType CombinerTypeToSchema(TfLiteCombinerType type) { 143 switch (type) { 144 case kTfLiteCombinerTypeSum: 145 return CombinerType_SUM; 146 case kTfLiteCombinerTypeMean: 147 return CombinerType_MEAN; 148 case kTfLiteCombinerTypeSqrtn: 149 return CombinerType_SQRTN; 150 } 151 } 152 153 // int 154 155 } // namespace tflite 156 #endif // TENSORFLOW_LITE_TOOLS_SERIALIZATION_ENUM_MAPPING_H_ 157