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 <functional> 19 #include <memory> 20 #include <vector> 21 22 #include "tensorflow/lite/c/common.h" 23 #include "tensorflow/lite/core/api/error_reporter.h" 24 #include "tensorflow/lite/schema/schema_generated.h" 25 26 // Opaque type similar to TfLiteDelegate / TfLiteOpaqueDelegate. 27 // This is used for cases (e.g. when using "TF Lite with Google Play Services") 28 // where the TF Lite runtime might be built using a newer (or older) 29 // version of the TF Lite sources than the app, and hence might have a 30 // different definition of the TfLiteDelegate type. TF Lite APIs use 31 // TfLiteOpaqueDelegate rather than TfLiteDelegate when they want to 32 // refer to a delegate defined with that potentially different version 33 // of the TfLiteDelegate type. 34 struct TfLiteOpaqueDelegateStruct; 35 36 namespace tflite { 37 38 /// Abstract interface that returns TfLiteRegistrations given op codes or custom 39 /// op names. This is the mechanism that ops being referenced in the flatbuffer 40 /// model are mapped to executable function pointers (TfLiteRegistrations). 41 class OpResolver { 42 public: 43 /// Finds the op registration for a builtin operator by enum code. 44 virtual const TfLiteRegistration* FindOp(tflite::BuiltinOperator op, 45 int version) const = 0; 46 /// Finds the op registration of a custom operator by op name. 47 virtual const TfLiteRegistration* FindOp(const char* op, 48 int version) const = 0; 49 50 // Represents a sequence of delegates. 51 using TfLiteDelegatePtrVector = 52 std::vector<std::unique_ptr<TfLiteDelegate, void (*)(TfLiteDelegate*)>>; 53 54 // Returns optional delegates for resolving and handling ops in the flatbuffer 55 // model. This may be used in addition to the standard TfLiteRegistration 56 // lookup for graph resolution. 57 // WARNING: This API is deprecated, GetDelegateCreators is preferred. GetDelegates(int num_threads)58 virtual TfLiteDelegatePtrVector GetDelegates(int num_threads) const { 59 return {}; 60 } 61 62 // Represents a function that creates a TfLite delegate instance. 63 using TfLiteDelegateCreator = 64 std::function<std::unique_ptr<TfLiteDelegate, void (*)(TfLiteDelegate*)>( 65 int /*num_threads*/)>; 66 67 // Represents a sequence of delegate creator functions. 68 using TfLiteDelegateCreators = std::vector<TfLiteDelegateCreator>; 69 70 // Returns a vector of delegate creators to create optional delegates for 71 // resolving and handling ops in the flatbuffer model. This may be used in 72 // addition to the standard TfLiteRegistration lookup for graph resolution. 73 // 74 // Note that this method is not used (will not be called) if you are using 75 // TF Lite in Google Play Services; the GetOpaqueDelegateCreators method 76 // (see below) is used for that case. GetDelegateCreators()77 virtual TfLiteDelegateCreators GetDelegateCreators() const { return {}; } 78 79 // TODO(b/202712825): it would be nice if we could avoid the need for separate 80 // "opaque" types & methods for use only with TF Lite in Google Play Services. 81 82 // Represents an opaque delegate instance. 83 // WARNING: Experimental interface, subject to change. 84 using TfLiteOpaqueDelegatePtr = 85 std::unique_ptr<TfLiteOpaqueDelegateStruct, 86 void (*)(TfLiteOpaqueDelegateStruct*)>; 87 88 // Represents a function that creates an opaque delegate instance. 89 // WARNING: Experimental interface, subject to change. 90 using TfLiteOpaqueDelegateCreator = 91 std::function<TfLiteOpaqueDelegatePtr(int /*num_threads*/)>; 92 93 // Represents a sequence of opaque delegate creator functions. 94 // WARNING: Experimental interface, subject to change. 95 using TfLiteOpaqueDelegateCreators = std::vector<TfLiteOpaqueDelegateCreator>; 96 97 // Returns a vector of opaque delegate creators to create optional opaque 98 // delegates for resolving and handling ops in the flatbuffer model. This may 99 // be used in addition to the standard TfLiteRegistration lookup for graph 100 // resolution. 101 // 102 // Note that this method will be called only if you are using TF Lite in 103 // Google Play Services; if you are using regular TF Lite, GetDelegateCreators 104 // (see above) is used instead. 105 // 106 // WARNING: Experimental interface, subject to change. GetOpaqueDelegateCreators()107 virtual TfLiteOpaqueDelegateCreators GetOpaqueDelegateCreators() const { 108 return {}; 109 } 110 ~OpResolver()111 virtual ~OpResolver() {} 112 113 private: 114 /// Returns true if this OpResolver may contain any "user defined" ops. 115 /// By "user defined" ops, we mean any op definitions other than those 116 /// contained in tflite::ops::builtin::BuiltinOpResolver. 117 /// 118 /// If this method returns true, it doesn't necessarily mean that the 119 /// OpResolver contains a user-defined op, just that the absence of 120 /// user-defined ops can't be guaranteed. 121 /// 122 /// Note that "user-defined" ops are not the same as "custom" ops; 123 /// BuiltinOpResolver may support certain "custom" ops, in addition to 124 /// "builtin" ops, and may not support all of the "builtin" op enum values. MayContainUserDefinedOps()125 virtual bool MayContainUserDefinedOps() const { return true; } 126 127 friend class OpResolverInternal; 128 }; 129 130 // Handles the logic for converting between an OperatorCode structure extracted 131 // from a flatbuffer and information about a registered operator 132 // implementation. 133 TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode, 134 const OpResolver& op_resolver, 135 ErrorReporter* error_reporter, 136 const TfLiteRegistration** registration); 137 138 } // namespace tflite 139 140 #endif // TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ 141