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_CORE_API_OP_RESOLVER_H_ 16 #define TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ 17 18 #include <memory> 19 #include <vector> 20 21 #include "tensorflow/lite/c/common.h" 22 #include "tensorflow/lite/core/api/error_reporter.h" 23 #include "tensorflow/lite/schema/schema_generated.h" 24 25 namespace tflite { 26 27 /// Abstract interface that returns TfLiteRegistrations given op codes or custom 28 /// op names. This is the mechanism that ops being referenced in the flatbuffer 29 /// model are mapped to executable function pointers (TfLiteRegistrations). 30 class OpResolver { 31 public: 32 /// Finds the op registration for a builtin operator by enum code. 33 virtual const TfLiteRegistration* FindOp(tflite::BuiltinOperator op, 34 int version) const = 0; 35 /// Finds the op registration of a custom operator by op name. 36 virtual const TfLiteRegistration* FindOp(const char* op, 37 int version) const = 0; 38 39 // Returns optional delegates for resolving and handling ops in the flatbuffer 40 // model. This may be used in addition to the standard TfLiteRegistration 41 // lookup for graph resolution. 42 using TfLiteDelegatePtrVector = 43 std::vector<std::unique_ptr<TfLiteDelegate, void (*)(TfLiteDelegate*)>>; GetDelegates(int num_threads)44 virtual TfLiteDelegatePtrVector GetDelegates(int num_threads) const { 45 return TfLiteDelegatePtrVector(); 46 } 47 ~OpResolver()48 virtual ~OpResolver() {} 49 50 private: 51 /// Returns true if this OpResolver may contain any "user defined" ops. 52 /// By "user defined" ops, we mean any op definitions other than those 53 /// contained in tflite::ops::builtin::BuiltinOpResolver. 54 /// 55 /// If this method returns true, it doesn't necessarily mean that the 56 /// OpResolver contains a user-defined op, just that the absence of 57 /// user-defined ops can't be guaranteed. 58 /// 59 /// Note that "user-defined" ops are not the same as "custom" ops; 60 /// BuiltinOpResolver may support certain "custom" ops, in addition to 61 /// "builtin" ops, and may not support all of the "builtin" op enum values. MayContainUserDefinedOps()62 virtual bool MayContainUserDefinedOps() const { return true; } 63 64 friend class OpResolverInternal; 65 }; 66 67 // Handles the logic for converting between an OperatorCode structure extracted 68 // from a flatbuffer and information about a registered operator 69 // implementation. 70 TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode, 71 const OpResolver& op_resolver, 72 ErrorReporter* error_reporter, 73 const TfLiteRegistration** registration); 74 75 } // namespace tflite 76 77 #endif // TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ 78