Home | History | Annotate | Download | only in writer
      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_EXPERIMENTAL_WRITER_ENUM_MAPPING_H_
     16 #define TENSORFLOW_LITE_EXPERIMENTAL_WRITER_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 
     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 kTfLiteActRelu1:
     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 
     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 
     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 kTfLiteInt32:
     66       return TensorType_INT32;
     67     case kTfLiteUInt8:
     68       return TensorType_UINT8;
     69     case kTfLiteInt64:
     70       return TensorType_INT64;
     71     case kTfLiteString:
     72       return TensorType_STRING;
     73     case kTfLiteBool:
     74       return TensorType_BOOL;
     75     case kTfLiteInt16:
     76       return TensorType_INT16;
     77     case kTfLiteComplex64:
     78       return TensorType_COMPLEX64;
     79   }
     80   // TODO(aselle): consider an error
     81 }
     82 
     83 inline FullyConnectedOptionsWeightsFormat
     84 FullyConnectedOptionsWeightsFormatToSchema(
     85     TfLiteFullyConnectedWeightsFormat format) {
     86   switch (format) {
     87     case kTfLiteFullyConnectedWeightsFormatDefault:
     88       return FullyConnectedOptionsWeightsFormat_DEFAULT;
     89     case kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8:
     90       return FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8;
     91   }
     92 }
     93 
     94 inline LSTMKernelType LSTMKernelTypeToSchema(TfLiteLSTMKernelType type) {
     95   switch (type) {
     96     case kTfLiteLSTMFullKernel:
     97       return LSTMKernelType_FULL;
     98     case kTfLiteLSTMBasicKernel:
     99       return LSTMKernelType_BASIC;
    100   }
    101 }
    102 
    103 inline LSHProjectionType LSHProjectionTypeToSchema(
    104     TfLiteLSHProjectionType type) {
    105   switch (type) {
    106     case kTfLiteLshProjectionUnknown:
    107       return LSHProjectionType_UNKNOWN;
    108     case kTfLiteLshProjectionSparse:
    109       return LSHProjectionType_SPARSE;
    110     case kTfLiteLshProjectionDense:
    111       return LSHProjectionType_DENSE;
    112   }
    113 }
    114 
    115 inline MirrorPadMode MirrorPaddingModeToSchema(TfLiteMirrorPaddingMode mode) {
    116   switch (mode) {
    117     case kTfLiteMirrorPaddingUnknown:
    118       return MirrorPadMode_REFLECT;  // TODO(aselle): consider an error
    119     case kTfLiteMirrorPaddingReflect:
    120       return MirrorPadMode_REFLECT;
    121     case kTfLiteMirrorPaddingSymmetric:
    122       return MirrorPadMode_SYMMETRIC;
    123   }
    124 }
    125 
    126 inline CombinerType CombinerTypeToSchema(TfLiteCombinerType type) {
    127   switch (type) {
    128     case kTfLiteCombinerTypeSum:
    129       return CombinerType_SUM;
    130     case kTfLiteCombinerTypeMean:
    131       return CombinerType_MEAN;
    132     case kTfLiteCombinerTypeSqrtn:
    133       return CombinerType_SQRTN;
    134   }
    135 }
    136 
    137 // int
    138 
    139 }  // namespace tflite
    140 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_WRITER_ENUM_MAPPING_H_
    141