1 /* Copyright 2017 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_TOCO_TFLITE_OPERATOR_H_ 16 #define TENSORFLOW_LITE_TOCO_TFLITE_OPERATOR_H_ 17 18 #include "flatbuffers/flatbuffers.h" 19 #include "flatbuffers/flexbuffers.h" 20 #include "tensorflow/lite/schema/schema_generated.h" 21 #include "tensorflow/lite/toco/model.h" 22 23 namespace toco { 24 25 namespace tflite { 26 27 class BaseOperator; 28 29 // Return a map contained all know TF Lite Operators, keyed by their names. 30 // TODO(ycling): The pattern to propagate parameters (e.g. enable_select_tf_ops) 31 // is ugly here. Consider refactoring. 32 std::map<string, std::unique_ptr<BaseOperator>> BuildOperatorByNameMap( 33 bool enable_select_tf_ops = false); 34 35 // Return a map contained all know TF Lite Operators, keyed by the type of 36 // their tf.mini counterparts. 37 std::map<OperatorType, std::unique_ptr<BaseOperator>> BuildOperatorByTypeMap( 38 bool enable_select_tf_ops = false); 39 40 // Write the custom option FlexBuffer with a serialized TensorFlow NodeDef 41 // for a Flex op. 42 std::unique_ptr<flexbuffers::Builder> WriteFlexOpOptions( 43 const string& tensorflow_node_def); 44 45 // These are the flatbuffer types for custom and builtin options. 46 using CustomOptions = flatbuffers::Vector<uint8_t>; 47 using BuiltinOptions = void; 48 49 // A simple wrapper around the flatbuffer objects used to describe options that 50 // configure operators. 51 struct Options { 52 // Build custom options. CustomOptions53 static Options Custom(flatbuffers::Offset<CustomOptions> offset) { 54 return {::tflite::BuiltinOptions_NONE, 0, offset}; 55 } 56 57 // Build builtin options of the given type. BuiltinOptions58 static Options Builtin(::tflite::BuiltinOptions type, 59 flatbuffers::Offset<BuiltinOptions> offset) { 60 return {type, offset, 0}; 61 } 62 63 ::tflite::BuiltinOptions type; 64 flatbuffers::Offset<BuiltinOptions> builtin; 65 flatbuffers::Offset<CustomOptions> custom; 66 }; 67 68 // A BaseOperator encapsulates the relationship between operators in tf.mini 69 // and TF lite, and provides methods for converting between those two formats. 70 class BaseOperator { 71 public: 72 // Build an operator with the given TF Lite name and tf.mini type. BaseOperator(const string & name,OperatorType type)73 BaseOperator(const string& name, OperatorType type) 74 : name_(name), type_(type) {} 75 virtual ~BaseOperator() = default; 76 name()77 string name() const { return name_; } type()78 OperatorType type() const { return type_; } 79 80 // Given a tf.mini operator, create the corresponding flatbuffer options and 81 // return their offsets. 82 virtual Options Serialize(const Operator& op, 83 flatbuffers::FlatBufferBuilder* builder) const = 0; 84 85 // Read TF Lite options and create the appropriate tf.mini operator. 86 virtual std::unique_ptr<Operator> Deserialize( 87 const BuiltinOptions* builtin_options, 88 const CustomOptions* custom_options) const = 0; 89 90 // Get the op version using the OperatorSignature. 91 // The function needs to be overridden to return the op version based on the 92 // parameters. Note: 93 // * The first version for each op should be 1 (to be consistent with the 94 // default value in Flatbuffer. `return 1;` is okay for newly implemented 95 // ops. 96 // * When multiple versions are defined for an op, this function needs to be 97 // overridden. (See example in `operator_test.cc`) 98 virtual int GetVersion(const OperatorSignature& op_signature) const = 0; 99 100 // Given a Toco `Operator`, return a list of booleans indicating the op 101 // mutates which input variables. 102 // * If the op mutates any input variables, it should return a list of bool 103 // with the same length as inputs. 104 // * Otherwise, it will return an empty list. GetMutatingInputVariables(const Operator & op)105 virtual std::vector<bool> GetMutatingInputVariables( 106 const Operator& op) const { 107 // Most ops don't have variable tensors. This function can be overridden. 108 return std::vector<bool>(); 109 } 110 111 private: 112 string name_; 113 OperatorType type_; 114 }; 115 116 // Helper function to determine if a unsupported TensorFlow op should be 117 // exported as an Flex op or a regular custom op. 118 bool ShouldExportAsFlexOp(bool enable_select_tf_ops, 119 const string& tensorflow_op_name); 120 121 } // namespace tflite 122 123 } // namespace toco 124 125 #endif // TENSORFLOW_LITE_TOCO_TFLITE_OPERATOR_H_ 126