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 
51 // Handles the logic for converting between an OperatorCode structure extracted
52 // from a flatbuffer and information about a registered operator
53 // implementation.
54 TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode,
55                                        const OpResolver& op_resolver,
56                                        ErrorReporter* error_reporter,
57                                        const TfLiteRegistration** registration);
58 
59 }  // namespace tflite
60 
61 #endif  // TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_
62