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_MUTABLE_OP_RESOLVER_H_ 16 #define TENSORFLOW_LITE_MUTABLE_OP_RESOLVER_H_ 17 18 #include <stddef.h> 19 20 #include <string> 21 #include <unordered_map> 22 #include <utility> 23 24 #include "tensorflow/lite/c/common.h" 25 #include "tensorflow/lite/core/api/op_resolver.h" 26 #include "tensorflow/lite/schema/schema_generated.h" 27 #include "tensorflow/lite/util.h" 28 29 namespace tflite { 30 31 // Some versions of gcc don't support partial specialization in class scope, 32 // so these are defined in a namescope. 33 namespace op_resolver_hasher { 34 template <typename V> 35 struct ValueHasher { operatorValueHasher36 size_t operator()(const V& v) const { return std::hash<V>()(v); } 37 }; 38 39 template <> 40 struct ValueHasher<tflite::BuiltinOperator> { 41 size_t operator()(const tflite::BuiltinOperator& v) const { 42 return std::hash<int>()(static_cast<int>(v)); 43 } 44 }; 45 46 template <typename T> 47 struct OperatorKeyHasher { 48 size_t operator()(const T& x) const { 49 size_t a = ValueHasher<typename T::first_type>()(x.first); 50 size_t b = ValueHasher<typename T::second_type>()(x.second); 51 return CombineHashes({a, b}); 52 } 53 }; 54 } // namespace op_resolver_hasher 55 56 // An OpResolver that is mutable, also used as the op in gen_op_registration. 57 // A typical usage: 58 // MutableOpResolver resolver; 59 // resolver.AddBuiltin(BuiltinOperator_ADD, Register_ADD()); 60 // resolver.AddCustom("CustomOp", Register_CUSTOM_OP()); 61 // InterpreterBuilder(model, resolver)(&interpreter); 62 class MutableOpResolver : public OpResolver { 63 public: 64 const TfLiteRegistration* FindOp(tflite::BuiltinOperator op, 65 int version) const override; 66 const TfLiteRegistration* FindOp(const char* op, int version) const override; 67 void AddBuiltin(tflite::BuiltinOperator op, 68 const TfLiteRegistration* registration, int version = 1); 69 void AddBuiltin(tflite::BuiltinOperator op, 70 const TfLiteRegistration* registration, int min_version, 71 int max_version); 72 void AddCustom(const char* name, const TfLiteRegistration* registration, 73 int version = 1); 74 void AddCustom(const char* name, const TfLiteRegistration* registration, 75 int min_version, int max_version); 76 void AddAll(const MutableOpResolver& other); 77 78 private: 79 typedef std::pair<tflite::BuiltinOperator, int> BuiltinOperatorKey; 80 typedef std::pair<std::string, int> CustomOperatorKey; 81 82 std::unordered_map<BuiltinOperatorKey, TfLiteRegistration, 83 op_resolver_hasher::OperatorKeyHasher<BuiltinOperatorKey> > 84 builtins_; 85 std::unordered_map<CustomOperatorKey, TfLiteRegistration, 86 op_resolver_hasher::OperatorKeyHasher<CustomOperatorKey> > 87 custom_ops_; 88 }; 89 90 } // namespace tflite 91 92 #endif // TENSORFLOW_LITE_MUTABLE_OP_RESOLVER_H_ 93